blob: 24e2d6a0bb3d3b42a13815966772e0038926648b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070085import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080086import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070087import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070090import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070093import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700101
Sunny Goyal651077b2014-06-30 14:15:31 -0700102import com.android.launcher3.DropTarget.DragObject;
103import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700107import com.android.launcher3.compat.PackageInstallerCompat;
108import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100109import com.android.launcher3.compat.UserHandleCompat;
110import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
170 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800172 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: boolean
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
192 // Type: long
193 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
198 // Type: parcelable
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: int[]
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Adam Cohen432609a2014-03-13 17:03:22 -0700205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
207
Adam Cohen3ed316a2014-07-23 18:21:20 -0700208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
211
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100212 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700213 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100214 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700215 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100216 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217
Adam Cohen39a06042013-07-19 14:30:12 -0700218 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700219 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700224 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700225 private State mState = State.WORKSPACE;
226 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700228 private boolean mIsSafeModeEnabled;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700231 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
232 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700233 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800236 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700244 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700245 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragLayer mDragLayer;
257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Sunny Goyalffe83f12014-08-14 17:39:34 -0700260 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Michael Jurkac9d95c52011-08-29 14:03:34 -0700263 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800265 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700266
Michael Jurka0280c3b2010-09-17 15:00:07 -0700267 private int[] mTmpAddItemCellCoordinates = new int[2];
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private FolderInfo mFolderInfo;
270
Winson Chung3d503fb2011-07-13 17:25:49 -0700271 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800272 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273
Michael Jurka838a4ca2011-02-07 13:33:06 -0800274 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700275
Winson Chungc51db6a2011-10-05 11:44:49 -0700276 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 private AppsCustomizeTabHost mAppsCustomizeTabHost;
278 private AppsCustomizePagedView mAppsCustomizeContent;
279 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800280 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private boolean mWorkspaceLoading = true;
291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mUserPresent = true;
305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309 private static LocaleConfiguration sLocaleConfiguration = null;
310
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700311 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Michael Jurka4ef207b2010-11-29 17:05:45 -0800328 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700329 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
330 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700331
Craig Mautner360310b2012-10-26 15:13:08 -0700332 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700333
Adam Cohen1462de32012-07-24 22:34:36 -0700334 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700335 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700336
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700338 static Date sDateStamp = new Date();
339 static DateFormat sDateFormat =
340 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
341 static long sRunStart = System.currentTimeMillis();
342 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700343
Winson Chung46353de2012-02-16 14:05:10 -0800344 // We only want to get the SharedPreferences once since it does an FS stat each time we get
345 // it from the context.
346 private SharedPreferences mSharedPrefs;
347
Adam Cohene25af792013-06-06 23:08:25 -0700348 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
349
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 // Holds the page that we need to animate to, and the icon views that we need to animate up
351 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700352 private ImageView mFolderIconImageView;
353 private Bitmap mFolderIconBitmap;
354 private Canvas mFolderIconCanvas;
355 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700356
Michael Jurkaddd62e92011-02-16 17:49:14 -0800357 private BubbleTextView mWaitingForResume;
358
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 private Runnable mBuildLayersRunnable = new Runnable() {
360 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700361 if (mWorkspace != null) {
362 mWorkspace.buildPageHardwareLayers();
363 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 }
365 };
366
Adam Cohendb364c32014-05-20 14:23:40 -0700367 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368
Michael Jurka7267fa52013-09-26 15:29:57 -0700369 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800370
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 private static class PendingAddArguments {
372 int requestCode;
373 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700374 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700375 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 int cellX;
377 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700378 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 }
380
Daniel Sandlerff02d492013-08-05 02:12:05 -0400381 private Stats mStats;
382
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700383 FocusIndicatorView mFocusHandler;
384
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800385 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800386 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700387 }
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700410 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700411 // Determine the dynamic grid properties
412 Point smallestSize = new Point();
413 Point largestSize = new Point();
414 Point realSize = new Point();
415 Display display = getWindowManager().getDefaultDisplay();
416 display.getCurrentSizeRange(smallestSize, largestSize);
417 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700418 DisplayMetrics dm = new DisplayMetrics();
419 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700420
Winson Chung5f8afe62013-08-12 16:19:28 -0700421 // Lazy-initialize the dynamic grid
422 DeviceProfile grid = app.initDynamicGrid(this,
423 Math.min(smallestSize.x, smallestSize.y),
424 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700425 realSize.x, realSize.y,
426 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700427
428 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400429 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700430 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700431 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mModel = app.setLauncher(this);
433 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700434 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400435 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Daniel Sandlerff02d492013-08-05 02:12:05 -0400438 mStats = new Stats(this);
439
Sunny Goyalffe83f12014-08-14 17:39:34 -0700440 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700441
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
443 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700445 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
446 // this also ensures that any synchronous binding below doesn't re-trigger another
447 // LauncherModel load.
448 mPaused = false;
449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200451 android.os.Debug.startMethodTracing(
452 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
455 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700457
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100459 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800461 registerContentObservers();
462
Joe Onorato7c312c12009-08-13 21:36:53 -0700463 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700464
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 mSavedState = savedInstanceState;
466 restoreState(mSavedState);
467
468 if (PROFILE_STARTUP) {
469 android.os.Debug.stopMethodTracing();
470 }
471
472 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700473 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 // If the user leaves launcher, then we should just load items asynchronously when
475 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500476 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 } else {
478 // We only load the page synchronously if the user rotates (or triggers a
479 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800480 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 }
483
484 // For handling default keys
485 mDefaultKeySsb = new SpannableStringBuilder();
486 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800487
488 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
489 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800490
Adam Cohenf9426d52012-06-04 17:26:21 -0700491 updateGlobalIcons();
492
493 // On large interfaces, we want the screen to auto-rotate based on the current orientation
494 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700495
Adam Cohen432609a2014-03-13 17:03:22 -0700496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
Sunny Goyal88d60f12014-09-08 03:47:29 -0700500 showFirstRunClings();
Winson Chunga6945242014-01-08 14:04:34 -0800501 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700502 }
503
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700504 @Override
505 public void onLauncherProviderChange() { }
506
Winson Chung98ca0f72013-07-29 12:58:51 -0700507 /** To be overriden by subclasses to hint to Launcher that we have custom content */
508 protected boolean hasCustomContentToLeft() {
509 return false;
510 }
511
Dave Hawkeya8881582013-09-17 15:55:33 -0600512 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500513 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600514 * {@link #addToCustomContentPage}. This will only be invoked if
515 * {@link #hasCustomContentToLeft()} is {@code true}.
516 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500517 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600518 }
519
520 /**
521 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
522 * ensure the custom content page is added or removed if necessary.
523 */
524 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600525 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600526 // Not bound yet, wait for bindScreens to be called.
527 return;
528 }
529
530 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
531 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500532 mWorkspace.createCustomContentContainer();
533 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600534 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
535 mWorkspace.removeCustomContentPage();
536 }
537 }
538
Adam Cohenf9426d52012-06-04 17:26:21 -0700539 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700540 boolean searchVisible = false;
541 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800542 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700543 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700544 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700545 searchVisible = updateGlobalSearchIcon();
546 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700547 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700548 if (sGlobalSearchIcon[coi] != null) {
549 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700551 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700552 if (sVoiceSearchIcon[coi] != null) {
553 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700554 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700555 }
Winson Chungadf0c182012-08-23 14:59:07 -0700556 if (mSearchDropTargetBar != null) {
557 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
Romain Guycbb89e42009-06-08 15:52:54 -0700560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700562 if (sLocaleConfiguration == null) {
563 new AsyncTask<Void, Void, LocaleConfiguration>() {
564 @Override
565 protected LocaleConfiguration doInBackground(Void... unused) {
566 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
567 readConfiguration(Launcher.this, localeConfiguration);
568 return localeConfiguration;
569 }
570
571 @Override
572 protected void onPostExecute(LocaleConfiguration result) {
573 sLocaleConfiguration = result;
574 checkForLocaleChange(); // recursive, but now with a locale configuration
575 }
576 }.execute();
577 return;
578 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 final Configuration configuration = getResources().getConfiguration();
581
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700582 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 final String locale = configuration.locale.toString();
584
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 final int mcc = configuration.mcc;
587
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700588 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final int mnc = configuration.mnc;
590
Romain Guy84f296c2009-11-04 15:00:44 -0800591 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592
Romain Guy84f296c2009-11-04 15:00:44 -0800593 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 sLocaleConfiguration.locale = locale;
595 sLocaleConfiguration.mcc = mcc;
596 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700597
Joe Onorato0589f0f2010-02-08 13:44:00 -0800598 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599
600 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100601 new AsyncTask<Void, Void, Void>() {
602 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100604 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 }
Michael Jurka43467462013-11-14 12:03:58 +0100606 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700607 }
608 }
609
610 private static class LocaleConfiguration {
611 public String locale;
612 public int mcc = -1;
613 public int mnc = -1;
614 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700615
Romain Guy98d01652009-06-30 16:21:04 -0700616 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
617 DataInputStream in = null;
618 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100619 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFS));
Romain Guy98d01652009-06-30 16:21:04 -0700620 configuration.locale = in.readUTF();
621 configuration.mcc = in.readInt();
622 configuration.mnc = in.readInt();
623 } catch (FileNotFoundException e) {
624 // Ignore
625 } catch (IOException e) {
626 // Ignore
627 } finally {
628 if (in != null) {
629 try {
630 in.close();
631 } catch (IOException e) {
632 // Ignore
633 }
634 }
635 }
636 }
637
638 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
639 DataOutputStream out = null;
640 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100641 out = new DataOutputStream(context.openFileOutput(
642 LauncherFiles.LAUNCHER_PREFS, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700643 out.writeUTF(configuration.locale);
644 out.writeInt(configuration.mcc);
645 out.writeInt(configuration.mnc);
646 out.flush();
647 } catch (FileNotFoundException e) {
648 // Ignore
649 } catch (IOException e) {
650 //noinspection ResultOfMethodCallIgnored
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100651 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFS).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700652 } finally {
653 if (out != null) {
654 try {
655 out.close();
656 } catch (IOException e) {
657 // Ignore
658 }
659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
661 }
662
Anton Hanssona2f665f2013-09-26 12:18:32 +0100663 public Stats getStats() {
664 return mStats;
665 }
666
Bjorn Bringertc459e522013-06-07 19:36:01 +0100667 public LayoutInflater getInflater() {
668 return mInflater;
669 }
670
Winson Chung36a62fe2012-05-06 18:04:42 -0700671 boolean isDraggingEnabled() {
672 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
673 // that is subsequently removed from the workspace in startBinding().
674 return !mModel.isLoadingWorkspace();
675 }
676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 static int getScreen() {
678 synchronized (sLock) {
679 return sScreen;
680 }
681 }
682
683 static void setScreen(int screen) {
684 synchronized (sLock) {
685 sScreen = screen;
686 }
687 }
688
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000689 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100690 if (Build.VERSION.SDK_INT >= 17) {
691 return View.generateViewId();
692 } else {
693 // View.generateViewId() is not available. The following fallback logic is a copy
694 // of its implementation.
695 for (;;) {
696 final int result = sNextGeneratedId.get();
697 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
698 int newValue = result + 1;
699 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
700 if (sNextGeneratedId.compareAndSet(result, newValue)) {
701 return result;
702 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000703 }
704 }
705 }
706
707 public int getViewIdForItem(ItemInfo info) {
708 // This cast is safe given the > 2B range for int.
709 int itemId = (int) info.id;
710 if (mItemIdToViewId.containsKey(itemId)) {
711 return mItemIdToViewId.get(itemId);
712 }
713 int viewId = generateViewId();
714 mItemIdToViewId.put(itemId, viewId);
715 return viewId;
716 }
717
718 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700719 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
720 * a configuration step, this allows the proper animations to run after other transitions.
721 */
Adam Cohendb364c32014-05-20 14:23:40 -0700722 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700723 long screenId = args.screenId;
724 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
725 // When the screen id represents an actual screen (as opposed to a rank) we make sure
726 // that the drop page actually exists.
727 screenId = ensurePendingDropLayoutExists(args.screenId);
728 }
Adam Cohendb364c32014-05-20 14:23:40 -0700729
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700732 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700734 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700736 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700737 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700738 case REQUEST_RECONFIGURE_APPWIDGET:
739 completeRestoreAppWidget(args.appWidgetId);
740 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 }
Michael Jurka27614d22012-04-02 06:40:22 -0700742 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
743 // if you turned the screen off and then back while in All Apps, Launcher would not
744 // return to the workspace. Clearing mAddInfo.container here fixes this issue
745 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700746 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 }
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 protected void onActivityResult(
751 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700752 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700753 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700754 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800755 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700756
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 Runnable exitSpringLoaded = new Runnable() {
758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
761 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
762 }
763 };
764
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
768 if (resultCode == RESULT_CANCELED) {
769 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700770 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
774 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 }
776 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200777 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
778 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
779 mWorkspace.exitOverviewMode(false);
780 }
781 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200783
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
785 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700786
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 // We have special handling for widgets
789 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800790 final int appWidgetId;
791 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
792 : -1;
793 if (widgetId < 0) {
794 appWidgetId = pendingAddWidgetId;
795 } else {
796 appWidgetId = widgetId;
797 }
798
Adam Cohenad4e15c2013-10-17 16:21:35 -0700799 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800800 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700801 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
802 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 result = RESULT_CANCELED;
804 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700805 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 @Override
807 public void run() {
808 exitSpringLoadedDragModeDelayed(false, 0, null);
809 }
810 };
Adam Cohendb364c32014-05-20 14:23:40 -0700811 if (workspaceLocked) {
812 // No need to remove the empty screen if we're mid-binding, as the
813 // the bind will not add the empty screen.
814 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
815 } else {
816 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
817 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
818 }
Winson Chung5aaab772012-04-27 15:24:02 -0700819 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700820 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700821 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
822 // When the screen id represents an actual screen (as opposed to a rank)
823 // we make sure that the drop page actually exists.
824 mPendingAddInfo.screenId =
825 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
826 }
Adam Cohendb364c32014-05-20 14:23:40 -0700827 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
828
829 dropLayout.setDropPending(true);
830 final Runnable onComplete = new Runnable() {
831 @Override
832 public void run() {
833 completeTwoStageWidgetDrop(resultCode, appWidgetId);
834 dropLayout.setDropPending(false);
835 }
836 };
837 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
839 } else {
840 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
841 mPendingAddInfo);
842 sPendingAddItem = args;
843 }
Winson Chung5aaab772012-04-27 15:24:02 -0700844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 return;
846 }
847
Sunny Goyalff572272014-07-23 13:58:07 -0700848 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
849 if (resultCode == RESULT_OK) {
850 // Update the widget view.
851 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
852 pendingAddWidgetId, mPendingAddInfo);
853 if (workspaceLocked) {
854 sPendingAddItem = args;
855 } else {
856 completeAdd(args);
857 }
858 }
859 // Leave the widget in the pending state if the user canceled the configure.
860 return;
861 }
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 // The pattern used here is that a user PICKs a specific application,
864 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
867 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700868 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700869 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
870 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800871 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700872 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800873 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700874 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700875 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
876 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
Adam Cohen00074722013-10-11 17:46:09 -0700878 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700879 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700880 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884
Adam Cohendb364c32014-05-20 14:23:40 -0700885 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
886 appWidgetId, ItemInfo info) {
887 PendingAddArguments args = new PendingAddArguments();
888 args.requestCode = requestCode;
889 args.intent = data;
890 args.container = info.container;
891 args.screenId = info.screenId;
892 args.cellX = info.cellX;
893 args.cellY = info.cellY;
894 args.appWidgetId = appWidgetId;
895 return args;
896 }
897
898 /**
899 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
900 *
901 * @param screenId the screen id to check
902 * @return the new screen, or screenId if it exists
903 */
904 private long ensurePendingDropLayoutExists(long screenId) {
905 CellLayout dropLayout =
906 (CellLayout) mWorkspace.getScreenWithId(screenId);
907 if (dropLayout == null) {
908 // it's possible that the add screen was removed because it was
909 // empty and a re-bind occurred
910 mWorkspace.addExtraEmptyScreen();
911 return mWorkspace.commitExtraEmptyScreen();
912 } else {
913 return screenId;
914 }
915 }
916
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700918 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 Runnable onCompleteRunnable = null;
921 int animationType = 0;
922
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 if (resultCode == RESULT_OK) {
925 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
926 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700927 mPendingAddWidgetInfo);
928 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 onCompleteRunnable = new Runnable() {
930 @Override
931 public void run() {
932 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
935 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 }
937 };
938 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800939 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 if (mDragLayer.getAnimatedView() != null) {
943 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
944 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
945 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 // The animated view may be null in the case of a rotation during widget configuration
948 onCompleteRunnable.run();
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
952 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 protected void onStop() {
954 super.onStop();
955 FirstFrameAnimatorHelper.setIsVisible(false);
956 }
957
958 @Override
959 protected void onStart() {
960 super.onStart();
961 FirstFrameAnimatorHelper.setIsVisible(true);
962 }
963
964 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200966 long startTime = 0;
967 if (DEBUG_RESUME_TIME) {
968 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400969 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700972
Winson Chung4a2afa32012-07-19 14:53:05 -0700973 // Restore the previous launcher state
974 if (mOnResumeState == State.WORKSPACE) {
975 showWorkspace(false);
976 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800977 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700978 }
979 mOnResumeState = State.NONE;
980
Craig Mautner360310b2012-10-26 15:13:08 -0700981 // Background was set to gradient in onPause(), restore to black if in all apps.
982 setWorkspaceBackground(mState == State.WORKSPACE);
983
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800984 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700985 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700986 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500987 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700989 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700991 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200992 // We might have postponed some bind calls until onResume (see waitUntilResume) --
993 // execute them here
994 long startTimeCallbacks = 0;
995 if (DEBUG_RESUME_TIME) {
996 startTimeCallbacks = System.currentTimeMillis();
997 }
998
999 if (mAppsCustomizeContent != null) {
1000 mAppsCustomizeContent.setBulkBind(true);
1001 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1003 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001004 }
1005 if (mAppsCustomizeContent != null) {
1006 mAppsCustomizeContent.setBulkBind(false);
1007 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001009 if (DEBUG_RESUME_TIME) {
1010 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1011 (System.currentTimeMillis() - startTimeCallbacks));
1012 }
Michael Jurka447bf842013-05-15 14:52:15 +02001013 }
Michael Jurka54554252013-08-01 12:52:23 +02001014 if (mOnResumeCallbacks.size() > 0) {
1015 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1016 mOnResumeCallbacks.get(i).run();
1017 }
1018 mOnResumeCallbacks.clear();
1019 }
Winson Chunge4e50662012-01-23 14:45:13 -08001020
1021 // Reset the pressed state of icons that were locked in the press state while activities
1022 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001023 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001024 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001025 mWaitingForResume.setStayPressed(false);
1026 }
Winson Chung82963d52013-10-09 11:20:57 -07001027
Adam Cohen06dff352012-06-01 17:17:08 -07001028 // It is possible that widgets can receive updates while launcher is not in the foreground.
1029 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1030 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1031 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001032 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001033
Winson Chung780fe592013-09-26 14:48:44 -07001034 // Process any items that were added while Launcher was away.
1035 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1036
Winson Chung5841efa2013-09-30 18:06:44 -07001037 // Update the voice search button proxy
1038 updateVoiceButtonProxyVisible(false);
1039
Adam Cohenf9426d52012-06-04 17:26:21 -07001040 // Again, as with the above scenario, it's possible that one or more of the global icons
1041 // were updated in the wrong orientation.
1042 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1045 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001046
1047 if (mWorkspace.getCustomContentCallbacks() != null) {
1048 // If we are resuming and the custom content is the current page, we call onShow().
1049 // It is also poassible that onShow will instead be called slightly after first layout
1050 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1051 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 }
1054 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001055 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001056 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001057
1058 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001059 }
1060
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001063 // Ensure that items added to Launcher are queued until Launcher returns
1064 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001065 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001066
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001068 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001069 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001070 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001071
1072 // We call onHide() aggressively. The custom content callbacks should be able to
1073 // debounce excess onHide calls.
1074 if (mWorkspace.getCustomContentCallbacks() != null) {
1075 mWorkspace.getCustomContentCallbacks().onHide();
1076 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Adam Cohenbffe7452013-07-22 18:21:45 -07001079 QSBScroller mQsbScroller = new QSBScroller() {
1080 int scrollY = 0;
1081
1082 @Override
1083 public void setScrollY(int scroll) {
1084 scrollY = scroll;
1085
1086 if (mWorkspace.isOnOrMovingToCustomContent()) {
1087 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001088 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001089 }
1090 }
1091 };
1092
1093 public void resetQSBScroll() {
1094 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001095 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 }
1097
1098 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1100 // by a onResume or by scrolling otherwise.
1101 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001102
1103 // Custom content is completely hidden
1104 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001105
1106 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1107 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001108
1109 // Indicates whether the user is allowed to scroll away from the custom content.
1110 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 }
1112
Jorim Jaggid017f882014-01-14 17:08:48 -08001113 protected boolean hasSettings() {
1114 return false;
1115 }
1116
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 public interface QSBScroller {
1118 public void setScrollY(int scrollY);
1119 }
1120
Winson Chung98ca0f72013-07-29 12:58:51 -07001121 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001122 CustomContentCallbacks callbacks, String description) {
1123 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001125 }
1126
Adam Cohenedb40762013-07-18 16:45:45 -07001127 // The custom content needs to offset its content to account for the QSB
1128 public int getTopOffsetForCustomContent() {
1129 return mWorkspace.getPaddingTop();
1130 }
1131
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001132 @Override
1133 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001134 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001135 if (mModel.isCurrentCallbacks(this)) {
1136 mModel.stopLoader();
1137 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001138 if (mAppsCustomizeContent != null) {
1139 mAppsCustomizeContent.surrender();
1140 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001141 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001142 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001143
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001144 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001145 @Override
1146 public void onWindowFocusChanged(boolean hasFocus) {
1147 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001148 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001149 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 private boolean acceptFilter() {
1152 final InputMethodManager inputManager = (InputMethodManager)
1153 getSystemService(Context.INPUT_METHOD_SERVICE);
1154 return !inputManager.isFullscreenMode();
1155 }
1156
1157 @Override
1158 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001159 final int uniChar = event.getUnicodeChar();
1160 final boolean handled = super.onKeyDown(keyCode, event);
1161 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1162 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1164 keyCode, event);
1165 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001166 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001167 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001168 // showSearchDialog()
1169 // If there are multiple keystrokes before the search dialog takes focus,
1170 // onSearchRequested() will be called for every keystroke,
1171 // but it is idempotent, so it's fine.
1172 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 }
1174 }
1175
Joe Onorato8a9625e2010-01-28 15:55:35 -08001176 // Eat the long press event so the keyboard doesn't come up.
1177 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1178 return true;
1179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 return handled;
1182 }
1183
Karl Rosaen138a0412009-04-23 19:00:21 -07001184 private String getTypedText() {
1185 return mDefaultKeySsb.toString();
1186 }
1187
1188 private void clearTypedText() {
1189 mDefaultKeySsb.clear();
1190 mDefaultKeySsb.clearSpans();
1191 Selection.setSelection(mDefaultKeySsb, 0);
1192 }
1193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001195 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1196 * State
1197 */
1198 private static State intToState(int stateOrdinal) {
1199 State state = State.WORKSPACE;
1200 final State[] stateValues = State.values();
1201 for (int i = 0; i < stateValues.length; i++) {
1202 if (stateValues[i].ordinal() == stateOrdinal) {
1203 state = stateValues[i];
1204 break;
1205 }
1206 }
1207 return state;
1208 }
1209
1210 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 * Restores the previous state, if it exists.
1212 *
1213 * @param savedState The previous state.
1214 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001215 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 private void restoreState(Bundle savedState) {
1217 if (savedState == null) {
1218 return;
1219 }
1220
Michael Jurka883f55b2010-10-21 15:47:14 -07001221 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001222 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001223 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001224 }
1225
Adam Cohen21cd0022013-10-09 18:57:02 -07001226 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1227 PagedView.INVALID_RESTORE_PAGE);
1228 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001229 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
Winson Chung3d503fb2011-07-13 17:25:49 -07001232 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001233 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234
Winson Chung3d503fb2011-07-13 17:25:49 -07001235 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1236 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001237 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1239 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001240 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1241 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1242 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001243 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001244 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 mRestoring = true;
1246 }
1247
1248 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1249 if (renameFolder) {
1250 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001251 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 mRestoring = true;
1253 }
Winson Chunga12a2502010-12-20 14:41:35 -08001254
Winson Chung785d2eb2011-04-14 16:08:02 -07001255 // Restore the AppsCustomize tab
1256 if (mAppsCustomizeTabHost != null) {
1257 String curTab = savedState.getString("apps_customize_currentTab");
1258 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001259 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001260 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001261 mAppsCustomizeContent.loadAssociatedPages(
1262 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 }
1264
Winson Chung5afbf7b2011-07-25 11:53:08 -07001265 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1266 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001267 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001268 mItemIdToViewId = (HashMap<Integer, Integer>)
1269 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 }
1271
1272 /**
1273 * Finds all the views we need and configure them properly.
1274 */
1275 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001276 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001277
Craig Mautner360310b2012-10-26 15:13:08 -07001278 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001279 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001280 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1281 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001282 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001283 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001284
John Spurlock77e1f472013-09-11 10:09:51 -04001285 mLauncherView.setSystemUiVisibility(
1286 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001287 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288
Winson Chung4c98d922011-05-31 16:50:48 -07001289 // Setup the drag layer
1290 mDragLayer.setup(this, dragController);
1291
Winson Chung3d503fb2011-07-13 17:25:49 -07001292 // Setup the hotseat
1293 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1294 if (mHotseat != null) {
1295 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001296 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001297 }
1298
Jorim Jaggid017f882014-01-14 17:08:48 -08001299 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001300 View widgetButton = findViewById(R.id.widget_button);
1301 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001302 @Override
1303 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001304 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001305 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001306 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001307 }
1308 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001309 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1310
1311 View wallpaperButton = findViewById(R.id.wallpaper_button);
1312 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001313 @Override
1314 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001315 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001316 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001317 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001318 }
1319 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001320 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1321
1322 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001323 if (hasSettings()) {
1324 settingsButton.setOnClickListener(new OnClickListener() {
1325 @Override
1326 public void onClick(View arg0) {
1327 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001328 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001329 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001330 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001331 });
1332 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1333 } else {
1334 settingsButton.setVisibility(View.GONE);
1335 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1336 lp.gravity = Gravity.END | Gravity.TOP;
1337 widgetButton.requestLayout();
1338 }
1339
Adam Cohendbdff6b2013-09-18 19:09:15 -07001340 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001341
Winson Chung4c98d922011-05-31 16:50:48 -07001342 // Setup the workspace
1343 mWorkspace.setHapticFeedbackEnabled(false);
1344 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001345 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001346 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001347
Winson Chungf0ea4d32011-06-06 14:27:16 -07001348 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001349 mSearchDropTargetBar = (SearchDropTargetBar)
1350 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001351
Winson Chungf0ea4d32011-06-06 14:27:16 -07001352 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001354 mAppsCustomizeContent = (AppsCustomizePagedView)
1355 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1356 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001357
Winson Chung3d503fb2011-07-13 17:25:49 -07001358 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001359 dragController.setDragScoller(mWorkspace);
1360 dragController.setScrollView(mDragLayer);
1361 dragController.setMoveTarget(mWorkspace);
1362 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001363 if (mSearchDropTargetBar != null) {
1364 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001365 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001366
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001367 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001368 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001369 mWeightWatcher = new WeightWatcher(this);
1370 mWeightWatcher.setAlpha(0.5f);
1371 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001372 new FrameLayout.LayoutParams(
1373 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001374 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001375 Gravity.BOTTOM)
1376 );
Adam Cohen39a06042013-07-19 14:30:12 -07001377
1378 boolean show = shouldShowWeightWatcher();
1379 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
1383 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001384 * Sets the all apps button. This method is called from {@link Hotseat}.
1385 */
1386 public void setAllAppsButton(View allAppsButton) {
1387 mAllAppsButton = allAppsButton;
1388 }
1389
1390 public View getAllAppsButton() {
1391 return mAllAppsButton;
1392 }
1393
1394 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 * Creates a view representing a shortcut.
1396 *
1397 * @param info The data structure describing the shortcut.
1398 *
1399 * @return A View inflated from R.layout.application.
1400 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001401 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001403 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 /**
1407 * Creates a view representing a shortcut inflated from the specified resource.
1408 *
1409 * @param layoutResId The id of the XML layout used to create the shortcut.
1410 * @param parent The group the shortcut belongs to.
1411 * @param info The data structure describing the shortcut.
1412 *
1413 * @return A View inflated from layoutResId.
1414 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001415 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001416 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001417 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001419 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 return favorite;
1421 }
1422
1423 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 * Add a shortcut to the workspace.
1425 *
1426 * @param data The intent describing the shortcut.
1427 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001429 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 int cellY) {
1431 int[] cellXY = mTmpAddItemCellCoordinates;
1432 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001433 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Michael Jurkad3ef3062010-11-23 16:23:58 -08001435 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001436
Adam Cohen558baaf2011-08-15 15:22:57 -07001437 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001438 if (info == null) {
1439 return;
1440 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001441 final View view = createShortcut(info);
1442
Adam Cohenfbba09b2011-07-18 21:43:05 -07001443 // First we check if we already know the exact location where we want to add this item.
1444 if (cellX >= 0 && cellY >= 0) {
1445 cellXY[0] = cellX;
1446 cellXY[1] = cellY;
1447 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001448
1449 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001450 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001451 true, null,null)) {
1452 return;
1453 }
1454 DragObject dragObject = new DragObject();
1455 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001456 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1457 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001458 return;
1459 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001460 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001461 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001462 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001463 foundCellSpan = (result != null);
1464 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001465 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001466 }
1467
1468 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001469 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 return;
1471 }
1472
Adam Cohendcd297f2013-06-18 13:13:40 -07001473 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474
1475 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001477 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479 }
1480
Adam Cohen2f093b62012-04-30 18:59:53 -07001481 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1482 int minHeight) {
1483 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001484 // We want to account for the extra amount of padding that we are adding to the widget
1485 // to ensure that it gets the full amount of space that it has requested
1486 int requiredWidth = minWidth + padding.left + padding.right;
1487 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001488 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001489 }
1490
Adam Cohen2f093b62012-04-30 18:59:53 -07001491 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1492 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001493 }
1494
Adam Cohen2f093b62012-04-30 18:59:53 -07001495 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1496 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001497 }
1498
Adam Cohen2f093b62012-04-30 18:59:53 -07001499 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1500 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001501 }
1502
Adam Cohen2f093b62012-04-30 18:59:53 -07001503 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1504 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001505 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001506 }
1507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001509 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001511 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1516 if (appWidgetInfo == null) {
1517 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1518 }
Romain Guycbb89e42009-06-08 15:52:54 -07001519
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001520 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001521 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001522
Adam Cohen2f093b62012-04-30 18:59:53 -07001523 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1524 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001527 // We have saved the position to which the widget was dragged-- this really only matters
1528 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001529 int[] cellXY = mTmpAddItemCellCoordinates;
1530 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001531 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001533 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1534 cellXY[0] = mPendingAddInfo.cellX;
1535 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001536 spanXY[0] = mPendingAddInfo.spanX;
1537 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 foundCellSpan = true;
1539 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001541 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001542 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1543 spanXY[1], cellXY, finalSpan);
1544 spanXY[0] = finalSpan[0];
1545 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001548 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001549 }
1550
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001552 if (appWidgetId != -1) {
1553 // Deleting an app widget ID is a void call but writes to disk before returning
1554 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001555 new AsyncTask<Void, Void, Void>() {
1556 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001557 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001558 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001559 }
Michael Jurka43467462013-11-14 12:03:58 +01001560 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001561 }
Winson Chung93eef082012-03-23 15:59:27 -07001562 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001563 return;
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001566 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001567 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1568 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 launcherInfo.spanX = spanXY[0];
1570 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001571 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1572 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001573 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001576 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577
1578 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001579 if (hostView == null) {
1580 // Perform actual inflation because we're live
1581 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1582 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1583 } else {
1584 // The AppWidgetHostView has already been inflated and instantiated
1585 launcherInfo.hostView = hostView;
1586 }
Romain Guycbb89e42009-06-08 15:52:54 -07001587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001589 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001590 launcherInfo.notifyWidgetSizeChanged(this);
1591
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001593 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001594
1595 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001597 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
Romain Guycbb89e42009-06-08 15:52:54 -07001599
Adam Cohended9f8d2010-11-03 13:25:16 -07001600 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1601 @Override
1602 public void onReceive(Context context, Intent intent) {
1603 final String action = intent.getAction();
1604 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1605 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001606 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001608
Winson Chungc100e8e2011-08-09 16:02:43 -07001609 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001610 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001611 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001612 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1615 mUserPresent = true;
1616 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001617 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1618 mModel.resetLoadedState(false, true);
1619 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1620 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1621 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1622 mModel.resetLoadedState(false, true);
1623 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1624 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1625 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001626 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1627 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1628 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 }
1630 }
1631 };
1632
1633 @Override
1634 public void onAttachedToWindow() {
1635 super.onAttachedToWindow();
1636
1637 // Listen for broadcasts related to user-presence
1638 final IntentFilter filter = new IntentFilter();
1639 filter.addAction(Intent.ACTION_SCREEN_OFF);
1640 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001641 // For handling managed profiles
1642 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1643 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001644 if (ENABLE_DEBUG_INTENTS) {
1645 filter.addAction(DebugIntents.DELETE_DATABASE);
1646 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001649 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001650 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001651 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 mVisible = true;
1653 }
1654
Adam Cohen0b395352014-06-09 22:54:36 +00001655 /**
1656 * Sets up transparent navigation and status bars in LMP.
1657 * This method is a no-op for other platform versions.
1658 */
1659 @TargetApi(19)
1660 private void setupTransparentSystemBarsForLmp() {
1661 // TODO(sansid): use the APIs directly when compiling against L sdk.
1662 // Currently we use reflection to access the flags and the API to set the transparency
1663 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001664 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001665 try {
1666 getWindow().getAttributes().systemUiVisibility |=
1667 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1668 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1669 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1670 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1671 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1672 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1673 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1674 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1675
1676 Method setStatusBarColorMethod =
1677 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1678 Method setNavigationBarColorMethod =
1679 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1680 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1681 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1682 } catch (NoSuchFieldException e) {
1683 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1684 } catch (NoSuchMethodException ex) {
1685 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1686 } catch (IllegalAccessException e) {
1687 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1688 } catch (IllegalArgumentException e) {
1689 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1690 } catch (InvocationTargetException e) {
1691 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1692 } finally {}
1693 }
1694 }
1695
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 @Override
1697 public void onDetachedFromWindow() {
1698 super.onDetachedFromWindow();
1699 mVisible = false;
1700
Adam Cohend113e0c2010-11-11 10:48:05 -08001701 if (mAttached) {
1702 unregisterReceiver(mReceiver);
1703 mAttached = false;
1704 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 updateRunning();
1706 }
1707
1708 public void onWindowVisibilityChanged(int visibility) {
1709 mVisible = visibility == View.VISIBLE;
1710 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001711 // The following code used to be in onResume, but it turns out onResume is called when
1712 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1713 // is a more appropriate event to handle
1714 if (mVisible) {
1715 mAppsCustomizeTabHost.onWindowVisible();
1716 if (!mWorkspaceLoading) {
1717 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001718 // We want to let Launcher draw itself at least once before we force it to build
1719 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001720 // apps is nice and speedy.
1721 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001722 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001723 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001724 if (mStarted) return;
1725 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001726 // We delay the layer building a bit in order to give
1727 // other message processing a time to run. In particular
1728 // this avoids a delay in hiding the IME if it was
1729 // currently shown, because doing that may involve
1730 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001731 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001732 final ViewTreeObserver.OnDrawListener listener = this;
1733 mWorkspace.post(new Runnable() {
1734 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001735 if (mWorkspace != null &&
1736 mWorkspace.getViewTreeObserver() != null) {
1737 mWorkspace.getViewTreeObserver().
1738 removeOnDrawListener(listener);
1739 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001740 }
1741 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001742 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001743 }
1744 });
1745 }
1746 clearTypedText();
1747 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001748 }
1749
1750 private void sendAdvanceMessage(long delay) {
1751 mHandler.removeMessages(ADVANCE_MSG);
1752 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1753 mHandler.sendMessageDelayed(msg, delay);
1754 mAutoAdvanceSentTime = System.currentTimeMillis();
1755 }
1756
1757 private void updateRunning() {
1758 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1759 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1760 mAutoAdvanceRunning = autoAdvanceRunning;
1761 if (autoAdvanceRunning) {
1762 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1763 sendAdvanceMessage(delay);
1764 } else {
1765 if (!mWidgetsToAdvance.isEmpty()) {
1766 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1767 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1768 }
1769 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001770 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 }
1772 }
1773 }
1774
1775 private final Handler mHandler = new Handler() {
1776 @Override
1777 public void handleMessage(Message msg) {
1778 if (msg.what == ADVANCE_MSG) {
1779 int i = 0;
1780 for (View key: mWidgetsToAdvance.keySet()) {
1781 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1782 final int delay = mAdvanceStagger * i;
1783 if (v instanceof Advanceable) {
1784 postDelayed(new Runnable() {
1785 public void run() {
1786 ((Advanceable) v).advance();
1787 }
1788 }, delay);
1789 }
1790 i++;
1791 }
1792 sendAdvanceMessage(mAdvanceInterval);
1793 }
1794 }
1795 };
1796
1797 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001798 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1800 if (v instanceof Advanceable) {
1801 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001802 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 updateRunning();
1804 }
1805 }
1806
1807 void removeWidgetToAutoAdvance(View hostView) {
1808 if (mWidgetsToAdvance.containsKey(hostView)) {
1809 mWidgetsToAdvance.remove(hostView);
1810 updateRunning();
1811 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001812 }
1813
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 launcherInfo.hostView = null;
1817 }
1818
Winson Chung93eef082012-03-23 15:59:27 -07001819 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1820 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1821 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 }
1823
Winson Chunga6945242014-01-08 14:04:34 -08001824 public DragLayer getDragLayer() {
1825 return mDragLayer;
1826 }
1827
1828 public Workspace getWorkspace() {
1829 return mWorkspace;
1830 }
1831
1832 public Hotseat getHotseat() {
1833 return mHotseat;
1834 }
1835
Jorim Jaggid017f882014-01-14 17:08:48 -08001836 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001837 return mOverviewPanel;
1838 }
1839
1840 public SearchDropTargetBar getSearchBar() {
1841 return mSearchDropTargetBar;
1842 }
1843
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001844 public LauncherAppWidgetHost getAppWidgetHost() {
1845 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
Romain Guycbb89e42009-06-08 15:52:54 -07001847
Winson Chunga9abd0e2010-10-27 17:18:37 -07001848 public LauncherModel getModel() {
1849 return mModel;
1850 }
1851
Winson Chunga6945242014-01-08 14:04:34 -08001852 protected SharedPreferences getSharedPrefs() {
1853 return mSharedPrefs;
1854 }
1855
Bjorn Bringertc459e522013-06-07 19:36:01 +01001856 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001857 getWindow().closeAllPanels();
1858
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001859 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001860 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 @Override
1864 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001865 long startTime = 0;
1866 if (DEBUG_RESUME_TIME) {
1867 startTime = System.currentTimeMillis();
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 super.onNewIntent(intent);
1870
1871 // Close the menu
1872 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001873 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001874 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001875
Adam Cohened307df2013-10-02 09:37:31 -07001876 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1877 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1878 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001879
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 if (mWorkspace == null) {
1881 // Can be cases where mWorkspace is null, this prevents a NPE
1882 return;
1883 }
1884 Folder openFolder = mWorkspace.getOpenFolder();
1885 // In all these cases, only animate if we're already on home
1886 mWorkspace.exitWidgetResizeMode();
1887 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001888 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001890 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001891
Adam Cohen6fecd412013-10-02 17:41:50 -07001892 closeFolder();
1893 exitSpringLoadedDragMode();
1894
1895 // If we are already on home, then just animate back to the workspace,
1896 // otherwise, just wait until onResume to set the state back to Workspace
1897 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001898 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001899 } else {
1900 mOnResumeState = State.WORKSPACE;
1901 }
1902
1903 final View v = getWindow().peekDecorView();
1904 if (v != null && v.getWindowToken() != null) {
1905 InputMethodManager imm = (InputMethodManager)getSystemService(
1906 INPUT_METHOD_SERVICE);
1907 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1908 }
1909
1910 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001911 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001912 mAppsCustomizeTabHost.reset();
1913 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001914
1915 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
Adam Cohened307df2013-10-02 09:37:31 -07001917
Michael Jurka447bf842013-05-15 14:52:15 +02001918 if (DEBUG_RESUME_TIME) {
1919 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922
Adam Coppa120b8e2013-11-12 16:26:04 +00001923 /**
1924 * Override point for subclasses to prevent movement to the default screen when the home
1925 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1926 */
1927 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1928 return true;
1929 }
1930
1931 /**
1932 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1933 */
1934 protected void onHomeIntent() {
1935 // Do nothing
1936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001939 public void onRestoreInstanceState(Bundle state) {
1940 super.onRestoreInstanceState(state);
1941 for (int page: mSynchronouslyBoundPages) {
1942 mWorkspace.restoreInstanceStateForChild(page);
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
1946 @Override
1947 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001948 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001949 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1950 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001952 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
Michael Jurka883f55b2010-10-21 15:47:14 -07001954 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001955 // We close any open folder since it will not be re-opened, and we need to make sure
1956 // this state is reflected.
1957 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 mWaitingForResult) {
1961 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1964 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1966 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1967 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 if (mFolderInfo != null && mWaitingForResult) {
1972 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1973 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1974 }
Michael Jurka946ad472010-07-09 18:05:18 -07001975
Winson Chung785d2eb2011-04-14 16:08:02 -07001976 // Save the current AppsCustomize tab
1977 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001978 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1979 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001980 if (currentTabTag != null) {
1981 outState.putString("apps_customize_currentTab", currentTabTag);
1982 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001983 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1984 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001985 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001986 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988
1989 @Override
1990 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001992
Winson Chunge7a03942011-08-05 15:05:12 -07001993 // Remove all pending runnables
1994 mHandler.removeMessages(ADVANCE_MSG);
1995 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001996 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001997
Winson Chungcd2b0142011-06-08 16:02:26 -07001998 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001999 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002000
2001 // It's possible to receive onDestroy after a new Launcher activity has
2002 // been created. In this case, don't interfere with the new Launcher.
2003 if (mModel.isCurrentCallbacks(this)) {
2004 mModel.stopLoader();
2005 app.setLauncher(null);
2006 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002009 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002011 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002013 mAppWidgetHost = null;
2014
2015 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
2017 TextKeyListener.getInstance().release();
2018
Winson Chung81b52252012-08-27 15:34:29 -07002019 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2020 // to prevent leaking Launcher activities on orientation change.
2021 if (mModel != null) {
2022 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2023 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002024
2025 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002026 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002027
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002028 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002029 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002030 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002031 mWorkspace = null;
2032 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002033
Sunny Goyale755d462014-07-22 13:48:29 -07002034 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002035 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
Adam Cohena9cf38f2011-05-02 15:36:58 -07002038 public DragController getDragController() {
2039 return mDragController;
2040 }
2041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
2043 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002044 if (requestCode >= 0) {
2045 setWaitingForResult(true);
2046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 super.startActivityForResult(intent, requestCode);
2048 }
2049
Winson Chung70d72102011-08-12 11:24:35 -07002050 /**
2051 * Indicates that we want global search for this activity by setting the globalSearch
2052 * argument for {@link #startSearch} to true.
2053 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002055 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002057
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002058 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Karl Rosaen138a0412009-04-23 19:00:21 -07002060 if (initialQuery == null) {
2061 // Use any text typed in the launcher as the initial query
2062 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 if (appSearchData == null) {
2065 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002066 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
Winson Chungadf0c182012-08-23 14:59:07 -07002068 Rect sourceBounds = new Rect();
2069 if (mSearchDropTargetBar != null) {
2070 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2071 }
Romain Guycbb89e42009-06-08 15:52:54 -07002072
Ian Parkinson047036e2014-09-01 15:40:53 +01002073 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002075 if (clearTextImmediately) {
2076 clearTypedText();
2077 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 }
2079
Ian Parkinson047036e2014-09-01 15:40:53 +01002080 /**
2081 * Start a text search.
2082 *
2083 * @return {@code true} if the search will start immediately, so any further keypresses
2084 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2085 * to buffer keypresses.
2086 */
2087 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002091 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 }
2093
2094 /**
2095 * Starts the global search activity. This code is a copied from SearchManager
2096 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002098 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2101 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2102 if (globalSearchActivity == null) {
2103 Log.w(TAG, "No global search activity found.");
2104 return;
2105 }
2106 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2107 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2108 intent.setComponent(globalSearchActivity);
2109 // Make sure that we have a Bundle to put source in
2110 if (appSearchData == null) {
2111 appSearchData = new Bundle();
2112 } else {
2113 appSearchData = new Bundle(appSearchData);
2114 }
2115 // Set source to package name of app that starts global search, if not set already.
2116 if (!appSearchData.containsKey("source")) {
2117 appSearchData.putString("source", getPackageName());
2118 }
2119 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2120 if (!TextUtils.isEmpty(initialQuery)) {
2121 intent.putExtra(SearchManager.QUERY, initialQuery);
2122 }
2123 if (selectInitialQuery) {
2124 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2125 }
2126 intent.setSourceBounds(sourceBounds);
2127 try {
2128 startActivity(intent);
2129 } catch (ActivityNotFoundException ex) {
2130 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 }
2133
Yura4f93ec62014-02-04 14:15:21 +00002134 public boolean isOnCustomContent() {
2135 return mWorkspace.isOnOrMovingToCustomContent();
2136 }
2137
Winson Chung70d72102011-08-12 11:24:35 -07002138 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002139 public boolean onPrepareOptionsMenu(Menu menu) {
2140 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002141 if (!isOnCustomContent()) {
2142 // Close any open folders
2143 closeFolder();
2144 // Stop resizing any widgets
2145 mWorkspace.exitWidgetResizeMode();
2146 if (!mWorkspace.isInOverviewMode()) {
2147 // Show the overview mode
2148 showOverviewMode(true);
2149 } else {
2150 showWorkspace(true);
2151 }
Winson Chunge0298742014-01-17 12:03:00 -08002152 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002153 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002156 @Override
2157 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002158 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002159 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002160 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002161 }
2162
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 public boolean isWorkspaceLocked() {
2164 return mWorkspaceLoading || mWaitingForResult;
2165 }
2166
Adam Cohen517a7f52014-03-01 12:12:59 -08002167 public boolean isWorkspaceLoading() {
2168 return mWorkspaceLoading;
2169 }
2170
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002171 private void setWorkspaceLoading(boolean value) {
2172 boolean isLocked = isWorkspaceLocked();
2173 mWorkspaceLoading = value;
2174 if (isLocked != isWorkspaceLocked()) {
2175 onWorkspaceLockedChanged();
2176 }
2177 }
2178
2179 private void setWaitingForResult(boolean value) {
2180 boolean isLocked = isWorkspaceLocked();
2181 mWaitingForResult = value;
2182 if (isLocked != isWorkspaceLocked()) {
2183 onWorkspaceLockedChanged();
2184 }
2185 }
2186
2187 protected void onWorkspaceLockedChanged() { }
2188
Michael Jurka0280c3b2010-09-17 15:00:07 -07002189 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002190 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2193 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002194 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002195 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002197
Adam Cohenad4e15c2013-10-17 16:21:35 -07002198 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2199 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2200 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2201 }
2202
2203 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2204 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2205 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002206 if (appWidgetInfo.configure != null) {
2207 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002208 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002209
Bjorn Bringert7984c942009-12-09 15:38:25 +00002210 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002211 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2212 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002215 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002216 Runnable onComplete = new Runnable() {
2217 @Override
2218 public void run() {
2219 // Exit spring loaded mode if necessary after adding the widget
2220 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2221 null);
2222 }
2223 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002225 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002226 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228 }
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002231 // Close any folders that may be open.
2232 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 mWorkspace.moveToCustomContentScreen(animate);
2234 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 /**
2236 * Process a shortcut drop.
2237 *
2238 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 * @param cell The cell it should be added to, optional
2241 * @param position The location on the screen where it was dropped, optional
2242 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249
2250 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.cellX = cell[0];
2252 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254
2255 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2256 createShortcutIntent.setComponent(componentName);
2257 processShortcut(createShortcutIntent);
2258 }
2259
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 /**
2261 * Process a widget drop.
2262 *
2263 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 * @param cell The cell it should be added to, optional
2266 * @param position The location on the screen where it was dropped, optional
2267 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002269 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 mPendingAddInfo.minSpanX = info.minSpanX;
2275 mPendingAddInfo.minSpanY = info.minSpanY;
2276
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.cellX = cell[0];
2279 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 if (span != null) {
2282 mPendingAddInfo.spanX = span[0];
2283 mPendingAddInfo.spanY = span[1];
2284 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285
Adam Cohened66b2b2012-01-23 17:28:51 -08002286 AppWidgetHostView hostView = info.boundWidget;
2287 int appWidgetId;
2288 if (hostView != null) {
2289 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 // In this case, we either need to start an activity to get permission to bind
2293 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002295 Bundle options = info.bindOptions;
2296
Sunny Goyalffe83f12014-08-14 17:39:34 -07002297 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2298 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002299 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002300 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002301 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002302 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2304 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2305 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002306 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2307 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002308 // TODO: we need to make sure that this accounts for the options bundle.
2309 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002310 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2311 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002312 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313 }
2314
Joe Onoratodeb98af2010-02-19 14:59:39 -08002315 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002316 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 }
2318
Winson Chung24ab2f12010-09-16 14:10:47 -07002319 void processWallpaper(Intent intent) {
2320 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2321 }
2322
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002325 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 folderInfo.title = getText(R.string.folder_name);
2327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002331 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332
2333 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 FolderIcon newFolder =
2335 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002338 // Force measure the new folder icon
2339 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2340 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002341 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
Romain Guycbb89e42009-06-08 15:52:54 -07002343
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002345 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 }
2347
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002348 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002349 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002350 }
2351
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002352 /**
2353 * Registers various content observers. The current implementation registers
2354 * only a favorites observer to keep track of the favorites applications.
2355 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002356 private void registerContentObservers() {
2357 ContentResolver resolver = getContentResolver();
2358 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2359 true, mWidgetObserver);
2360 }
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 @Override
2363 public boolean dispatchKeyEvent(KeyEvent event) {
2364 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2365 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002367 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002368 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002369 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002370 dumpState();
2371 return true;
2372 }
2373 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002374 }
2375 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2376 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002377 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 return true;
2379 }
2380 }
2381
2382 return super.dispatchKeyEvent(event);
2383 }
2384
Joe Onorato88ec0992009-11-19 13:16:06 -08002385 @Override
2386 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002387 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002388 if (mAppsCustomizeContent.getContentType() ==
2389 AppsCustomizePagedView.ContentType.Applications) {
2390 showWorkspace(true);
2391 } else {
2392 showOverviewMode(true);
2393 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002394 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002395 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002396 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002397 Folder openFolder = mWorkspace.getOpenFolder();
2398 if (openFolder.isEditingName()) {
2399 openFolder.dismissEditingName();
2400 } else {
2401 closeFolder();
2402 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002403 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002404 mWorkspace.exitWidgetResizeMode();
2405
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002406 // Back button is a no-op here, but give at least some feedback for the button press
2407 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002408 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002412 * Re-listen when widgets are reset.
2413 */
2414 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002415 if (mAppWidgetHost != null) {
2416 mAppWidgetHost.startListening();
2417 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002418 }
2419
2420 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 * Launches the intent referred by the clicked shortcut.
2422 *
2423 * @param v The view representing the clicked shortcut.
2424 */
2425 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002426 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2427 // view has detached (it's possible for this to happen if the view is removed mid touch).
2428 if (v.getWindowToken() == null) {
2429 return;
2430 }
2431
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002432 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002433 return;
2434 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002435
Adam Cohen1697b792013-09-17 19:08:21 -07002436 if (v instanceof Workspace) {
2437 if (mWorkspace.isInOverviewMode()) {
2438 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 }
2440 return;
2441 }
2442
2443 if (v instanceof CellLayout) {
2444 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002445 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002446 }
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002450 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002451 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002453 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002455 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002456 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002457 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002458 } else if (tag instanceof AppInfo) {
2459 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002460 } else if (tag instanceof LauncherAppWidgetInfo) {
2461 if (v instanceof PendingAppWidgetHostView) {
2462 onClickPendingWidget((PendingAppWidgetHostView) v);
2463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 }
2465 }
2466
Sunny Goyal508da152014-08-14 10:53:27 -07002467 public void onClickPagedViewIcon(View v) {
2468 startAppShortcutOrInfoActivity(v);
2469 }
2470
Michael Jurka0e260592010-06-30 17:07:39 -07002471 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002472 return false;
2473 }
2474
Michael Jurkaaf442092010-06-10 17:01:57 -07002475 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002476 * Event handler for the app widget view which has not fully restored.
2477 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002478 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2479 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002480 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002481 int widgetId = info.appWidgetId;
2482 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2483 if (appWidgetInfo != null) {
2484 mPendingAddWidgetInfo = appWidgetInfo;
2485 mPendingAddInfo.copyFrom(info);
2486 mPendingAddWidgetId = widgetId;
2487
Sunny Goyalffe83f12014-08-14 17:39:34 -07002488 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2489 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002490 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002491 } else if (info.installProgress < 0) {
2492 // The install has not been queued
2493 final String packageName = info.providerName.getPackageName();
2494 showBrokenAppInstallDialog(packageName,
2495 new DialogInterface.OnClickListener() {
2496 public void onClick(DialogInterface dialog, int id) {
2497 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2498 }
2499 });
2500 } else {
2501 // Download has started.
2502 final String packageName = info.providerName.getPackageName();
2503 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002504 }
2505 }
2506
2507 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002508 * Event handler for the search button
2509 *
2510 * @param v The view that was clicked.
2511 */
2512 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002513 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2514
Amith Yamasania135ba82011-08-09 17:42:01 -07002515 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002516 }
2517
2518 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002519 * Event handler for the voice button
2520 *
2521 * @param v The view that was clicked.
2522 */
2523 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002524 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002525
Bjorn Bringertc459e522013-06-07 19:36:01 +01002526 startVoice();
2527 }
2528
2529 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002530 try {
2531 final SearchManager searchManager =
2532 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2533 ComponentName activityName = searchManager.getGlobalSearchActivity();
2534 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002535 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002536 if (activityName != null) {
2537 intent.setPackage(activityName.getPackageName());
2538 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002539 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002540 } catch (ActivityNotFoundException e) {
2541 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002542 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002543 startActivitySafely(null, intent, "onClickVoiceButton");
2544 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002545 }
2546
2547 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002548 * Event handler for the "grid" button that appears on the home screen, which
2549 * enters all apps mode.
2550 *
2551 * @param v The view that was clicked.
2552 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 protected void onClickAllAppsButton(View v) {
2554 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2555 if (isAllAppsVisible()) {
2556 showWorkspace(true);
2557 } else {
2558 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2559 }
2560 }
2561
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 private void showBrokenAppInstallDialog(final String packageName,
2563 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002564 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 .setTitle(R.string.abandoned_promises_title)
2566 .setMessage(R.string.abandoned_promise_explanation)
2567 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2568 .setNeutralButton(R.string.abandoned_clean_this,
2569 new DialogInterface.OnClickListener() {
2570 public void onClick(DialogInterface dialog, int id) {
2571 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2572 mWorkspace.removeAbandonedPromise(packageName, user);
2573 }
2574 })
2575 .create().show();
2576 return;
2577 }
2578
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 /**
2580 * Event handler for an app shortcut click.
2581 *
2582 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2583 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002584 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2586 Object tag = v.getTag();
2587 if (!(tag instanceof ShortcutInfo)) {
2588 throw new IllegalArgumentException("Input must be a Shortcut");
2589 }
2590
2591 // Open shortcut
2592 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2593 final Intent intent = shortcut.intent;
2594
2595 // Check for special shortcuts
2596 if (intent.getComponent() != null) {
2597 final String shortcutClass = intent.getComponent().getClassName();
2598
2599 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2600 MemoryDumpActivity.startDump(this);
2601 return;
2602 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2603 toggleShowWeightWatcher();
2604 return;
2605 }
2606 }
2607
Chris Wren40c5ed32014-06-24 18:24:23 -04002608 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002609 if ((v instanceof BubbleTextView)
2610 && shortcut.isPromise()
2611 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002613 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002614 new DialogInterface.OnClickListener() {
2615 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002616 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002617 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002618 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002619 return;
2620 }
2621
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002623 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 }
2625
Sunny Goyal508da152014-08-14 10:53:27 -07002626 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002628 final ShortcutInfo shortcut;
2629 final Intent intent;
2630 if (tag instanceof ShortcutInfo) {
2631 shortcut = (ShortcutInfo) tag;
2632 intent = shortcut.intent;
2633 int[] pos = new int[2];
2634 v.getLocationOnScreen(pos);
2635 intent.setSourceBounds(new Rect(pos[0], pos[1],
2636 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002637
Sunny Goyal508da152014-08-14 10:53:27 -07002638 } else if (tag instanceof AppInfo) {
2639 shortcut = null;
2640 intent = ((AppInfo) tag).intent;
2641 } else {
2642 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2643 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644
2645 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 mStats.recordLaunch(intent, shortcut);
2647
2648 if (success && v instanceof BubbleTextView) {
2649 mWaitingForResume = (BubbleTextView) v;
2650 mWaitingForResume.setStayPressed(true);
2651 }
2652 }
2653
2654 /**
2655 * Event handler for a folder icon click.
2656 *
2657 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2658 */
2659 protected void onClickFolderIcon(View v) {
2660 if (LOGD) Log.d(TAG, "onClickFolder");
2661 if (!(v instanceof FolderIcon)){
2662 throw new IllegalArgumentException("Input must be a FolderIcon");
2663 }
2664
2665 FolderIcon folderIcon = (FolderIcon) v;
2666 final FolderInfo info = folderIcon.getFolderInfo();
2667 Folder openFolder = mWorkspace.getFolderForTag(info);
2668
2669 // If the folder info reports that the associated folder is open, then verify that
2670 // it is actually opened. There have been a few instances where this gets out of sync.
2671 if (info.opened && openFolder == null) {
2672 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2673 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2674 info.opened = false;
2675 }
2676
2677 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2678 // Close any open folder
2679 closeFolder();
2680 // Open the requested folder
2681 openFolder(folderIcon);
2682 } else {
2683 // Find the open folder...
2684 int folderScreen;
2685 if (openFolder != null) {
2686 folderScreen = mWorkspace.getPageForView(openFolder);
2687 // .. and close it
2688 closeFolder(openFolder);
2689 if (folderScreen != mWorkspace.getCurrentPage()) {
2690 // Close any folder open on the current screen
2691 closeFolder();
2692 // Pull the folder onto this screen
2693 openFolder(folderIcon);
2694 }
2695 }
2696 }
Michael Jurka5130e402011-10-13 04:55:35 -07002697 }
2698
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002699 /**
2700 * Event handler for the (Add) Widgets button that appears after a long press
2701 * on the home screen.
2702 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002703 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002705 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2706 }
2707
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 /**
2709 * Event handler for the wallpaper picker button that appears after a long press
2710 * on the home screen.
2711 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002712 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2714 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2715 pickWallpaper.setComponent(getWallpaperPickerComponent());
2716 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2717 }
2718
2719 /**
2720 * Event handler for a click on the settings button that appears after a long press
2721 * on the home screen.
2722 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002723 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2725 }
2726
Michael Jurka5130e402011-10-13 04:55:35 -07002727 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002728 // Provide the same haptic feedback that the system offers for virtual keys.
2729 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002730 }
2731
Adam Cohen61f560d2013-09-30 15:58:20 -07002732 public void performHapticFeedbackOnTouchDown(View v) {
2733 // Provide the same haptic feedback that the system offers for virtual keys.
2734 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2735 }
2736
2737 public View.OnTouchListener getHapticFeedbackTouchListener() {
2738 if (mHapticFeedbackTouchListener == null) {
2739 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2740 @Override
2741 public boolean onTouch(View v, MotionEvent event) {
2742 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2743 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2744 }
2745 return false;
2746 }
2747 };
2748 }
2749 return mHapticFeedbackTouchListener;
2750 }
2751
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002752 public void onDragStarted(View view) {}
2753
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002754 /**
2755 * Called when the user stops interacting with the launcher.
2756 * This implies that the user is now on the homescreen and is not doing housekeeping.
2757 */
2758 protected void onInteractionEnd() {}
2759
2760 /**
2761 * Called when the user starts interacting with the launcher.
2762 * The possible interactions are:
2763 * - open all apps
2764 * - reorder an app shortcut, or a widget
2765 * - open the overview mode.
2766 * This is a good time to stop doing things that only make sense
2767 * when the user is on the homescreen and not doing housekeeping.
2768 */
2769 protected void onInteractionBegin() {}
2770
Kenny Guyf07af7b2014-07-31 11:39:16 +01002771 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002772 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002773 try {
2774 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2775 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2776 launcherApps.showAppDetailsForProfile(componentName, user);
2777 } catch (SecurityException e) {
2778 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2779 Log.e(TAG, "Launcher does not have permission to launch settings");
2780 } catch (ActivityNotFoundException e) {
2781 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2782 Log.e(TAG, "Unable to launch settings");
2783 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002784 }
2785
Michael Jurka1e2f4652013-07-08 18:03:46 -07002786 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002787 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2788 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002789 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002790 // System applications cannot be installed. For now, show a toast explaining that.
2791 // We may give them the option of disabling apps this way.
2792 int messageId = R.string.uninstall_system_app_text;
2793 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002794 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002795 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002796 String packageName = componentName.getPackageName();
2797 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002798 Intent intent = new Intent(
2799 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002800 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2801 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002802 if (user != null) {
2803 user.addToIntent(intent, Intent.EXTRA_USER);
2804 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002805 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002806 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002807 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002808 }
2809
Michael Jurka86a720e2012-05-09 11:23:23 -07002810 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002811 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002813 // Only launch using the new animation if the shortcut has not opted out (this is a
2814 // private contract between launcher and may be ignored in the future).
2815 boolean useLaunchAnimation = (v != null) &&
2816 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002817 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2818 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002819
Kenny Guy1317e2d2014-05-08 18:52:50 +01002820 UserHandleCompat user = null;
2821 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2822 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2823 user = userManager.getUserForSerialNumber(serialNumber);
2824 }
2825
2826 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002827 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002828 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002829 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2830 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002831 optsBundle = opts.toBundle();
2832 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002833
2834 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2835 // Could be launching some bookkeeping activity
2836 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002837 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002838 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002839 launcherApps.startActivityForProfile(intent.getComponent(), user,
2840 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002841 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002842 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 } catch (SecurityException e) {
2844 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002845 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002847 "or use the exported attribute for this activity. "
2848 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002850 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002852
Michael Jurka86a720e2012-05-09 11:23:23 -07002853 boolean startActivitySafely(View v, Intent intent, Object tag) {
2854 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002855 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2856 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2857 return false;
2858 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002859 try {
2860 success = startActivity(v, intent, tag);
2861 } catch (ActivityNotFoundException e) {
2862 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2863 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2864 }
2865 return success;
2866 }
2867
Adam Cohen268c4752012-06-06 17:47:33 -07002868 /**
2869 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2870 * in the DragLayer in the exact absolute location of the original FolderIcon.
2871 */
2872 private void copyFolderIconToImage(FolderIcon fi) {
2873 final int width = fi.getMeasuredWidth();
2874 final int height = fi.getMeasuredHeight();
2875
2876 // Lazy load ImageView, Bitmap and Canvas
2877 if (mFolderIconImageView == null) {
2878 mFolderIconImageView = new ImageView(this);
2879 }
2880 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2881 mFolderIconBitmap.getHeight() != height) {
2882 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2883 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2884 }
2885
2886 DragLayer.LayoutParams lp;
2887 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2888 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2889 } else {
2890 lp = new DragLayer.LayoutParams(width, height);
2891 }
2892
Adam Cohen307fe232012-08-16 17:55:58 -07002893 // The layout from which the folder is being opened may be scaled, adjust the starting
2894 // view size by this scale factor.
2895 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002896 lp.customPosition = true;
2897 lp.x = mRectForFolderAnimation.left;
2898 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002899 lp.width = (int) (scale * width);
2900 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002901
2902 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2903 fi.draw(mFolderIconCanvas);
2904 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002905 if (fi.getFolder() != null) {
2906 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2907 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002908 }
Adam Cohen268c4752012-06-06 17:47:33 -07002909 // Just in case this image view is still in the drag layer from a previous animation,
2910 // we remove it and re-add it.
2911 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2912 mDragLayer.removeView(mFolderIconImageView);
2913 }
2914 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002915 if (fi.getFolder() != null) {
2916 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002917 }
Adam Cohen268c4752012-06-06 17:47:33 -07002918 }
2919
Adam Cohen2801caf2011-05-13 20:57:39 -07002920 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002921 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002922 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2923 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2924 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2925
Adam Cohenc51934b2011-07-26 21:07:43 -07002926 FolderInfo info = (FolderInfo) fi.getTag();
2927 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2928 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002929 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2930 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002931 }
2932
Adam Cohen268c4752012-06-06 17:47:33 -07002933 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2934 copyFolderIconToImage(fi);
2935 fi.setVisibility(View.INVISIBLE);
2936
Michael Jurka2ecf9952012-06-18 12:52:28 -07002937 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002938 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002939 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002940 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2941 }
2942 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002943 oa.start();
2944 }
2945
Adam Cohen268c4752012-06-06 17:47:33 -07002946 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002947 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002948 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2949 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2950 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2951
Adam Cohen268c4752012-06-06 17:47:33 -07002952 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002953
Adam Cohen268c4752012-06-06 17:47:33 -07002954 // We remove and re-draw the FolderIcon in-case it has changed
2955 mDragLayer.removeView(mFolderIconImageView);
2956 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002957 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002958 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002959 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002960 oa.addListener(new AnimatorListenerAdapter() {
2961 @Override
2962 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002963 if (cl != null) {
2964 cl.clearFolderLeaveBehind();
2965 // Remove the ImageView copy of the FolderIcon and make the original visible.
2966 mDragLayer.removeView(mFolderIconImageView);
2967 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002968 }
2969 }
2970 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002971 oa.start();
2972 }
2973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002975 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 * is animated relative to the specified View. If the View is null, no animation
2977 * is played.
2978 *
2979 * @param folderInfo The FolderInfo describing the folder to open.
2980 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002981 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002982 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002983 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984
Adam Cohena9cf38f2011-05-02 15:36:58 -07002985 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986
Adam Cohen4554ee12011-08-03 16:13:21 -07002987 // Just verify that the folder hasn't already been added to the DragLayer.
2988 // There was a one-off crash where the folder had a parent already.
2989 if (folder.getParent() == null) {
2990 mDragLayer.addView(folder);
2991 mDragController.addDropTarget((DropTarget) folder);
2992 } else {
2993 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2994 folder.getParent() + ").");
2995 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002996 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002997 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002998
2999 // Notify the accessibility manager that this folder "window" has appeared and occluded
3000 // the workspace items
3001 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3002 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003003 }
3004
3005 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003006 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003007 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003008 if (folder.isEditingName()) {
3009 folder.dismissEditingName();
3010 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003011 closeFolder(folder);
3012 }
3013 }
3014
3015 void closeFolder(Folder folder) {
3016 folder.getInfo().opened = false;
3017
3018 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3019 if (parent != null) {
3020 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3021 shrinkAndFadeInFolderIcon(fi);
3022 }
3023 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003024
3025 // Notify the accessibility manager that this folder "window" has disappeard and no
3026 // longer occludeds the workspace items
3027 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003031 if (!isDraggingEnabled()) return false;
3032 if (isWorkspaceLocked()) return false;
3033 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034
Adam Cohen1697b792013-09-17 19:08:21 -07003035 if (v instanceof Workspace) {
3036 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003037 if (mWorkspace.enterOverviewMode()) {
3038 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3039 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3040 return true;
3041 } else {
3042 return false;
3043 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003044 } else {
3045 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003046 }
Adam Cohen1697b792013-09-17 19:08:21 -07003047 }
3048
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003049 CellLayout.CellInfo longClickCellInfo = null;
3050 View itemUnderLongClick = null;
3051 if (v.getTag() instanceof ItemInfo) {
3052 ItemInfo info = (ItemInfo) v.getTag();
3053 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3054 itemUnderLongClick = longClickCellInfo.cell;
3055 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 }
3057
Winson Chung3d503fb2011-07-13 17:25:49 -07003058 // The hotseat touch handling does not go through Workspace, and we always allow long press
3059 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003060 final boolean inHotseat = isHotseatLayout(v);
3061 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003063 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003064 // User long pressed on empty space
3065 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3066 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003067 if (mWorkspace.isInOverviewMode()) {
3068 mWorkspace.startReordering(v);
3069 } else {
3070 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003073 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3074 mHotseat.getOrderInHotseat(
3075 longClickCellInfo.cellX,
3076 longClickCellInfo.cellY));
3077 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003079 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 }
3081 }
3082 }
3083 return true;
3084 }
3085
Winson Chung3d503fb2011-07-13 17:25:49 -07003086 boolean isHotseatLayout(View layout) {
3087 return mHotseat != null && layout != null &&
3088 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3089 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003090
Winson Chung3d503fb2011-07-13 17:25:49 -07003091 /**
3092 * Returns the CellLayout of the specified container at the specified screen.
3093 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003094 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003095 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3096 if (mHotseat != null) {
3097 return mHotseat.getLayout();
3098 } else {
3099 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003100 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003102 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003103 }
3104 }
3105
Daniel Sandler843e8602010-06-07 14:59:01 -04003106 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003107 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003108 }
3109
Craig Mautner360310b2012-10-26 15:13:08 -07003110 private void setWorkspaceBackground(boolean workspace) {
3111 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003112 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003113 }
3114
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003115 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003116 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3117 int curflags = getWindow().getAttributes().flags
3118 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3119 if (wpflags != curflags) {
3120 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3121 }
Craig Mautner360310b2012-10-26 15:13:08 -07003122 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003123 }
3124
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003125 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3126 if (v instanceof LauncherTransitionable) {
3127 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3128 }
3129 }
3130
Michael Jurkabed61d22012-02-14 22:51:29 -08003131 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3132 if (v instanceof LauncherTransitionable) {
3133 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3134 }
Winson Chung70442722012-02-10 15:43:22 -08003135
3136 // Update the workspace transition step as well
3137 dispatchOnLauncherTransitionStep(v, 0f);
3138 }
3139
3140 private void dispatchOnLauncherTransitionStep(View v, float t) {
3141 if (v instanceof LauncherTransitionable) {
3142 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3143 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003144 }
3145
3146 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3147 if (v instanceof LauncherTransitionable) {
3148 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3149 }
Winson Chung70442722012-02-10 15:43:22 -08003150
3151 // Update the workspace transition step as well
3152 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003153 }
3154
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003155 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003156 * Things to test when changing the following seven functions.
3157 * - Home from workspace
3158 * - from center screen
3159 * - from other screens
3160 * - Home from all apps
3161 * - from center screen
3162 * - from other screens
3163 * - Back from all apps
3164 * - from center screen
3165 * - from other screens
3166 * - Launch app from workspace and quit
3167 * - with back
3168 * - with home
3169 * - Launch app from all apps and quit
3170 * - with back
3171 * - with home
3172 * - Go to a screen that's not the default, then all
3173 * apps, and launch and app, and go back
3174 * - with back
3175 * -with home
3176 * - On workspace, long press power and go back
3177 * - with back
3178 * - with home
3179 * - On all apps, long press power and go back
3180 * - with back
3181 * - with home
3182 * - On workspace, power off
3183 * - On all apps, power off
3184 * - Launch an app and turn off the screen while in that app
3185 * - Go back with home key
3186 * - Go back with back key TODO: make this not go to workspace
3187 * - From all apps
3188 * - From workspace
3189 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3190 * - From all apps
3191 * - From the center workspace
3192 * - From another workspace
3193 */
3194
3195 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003196 * Zoom the camera out from the workspace to reveal 'toView'.
3197 * Assumes that the view to show is anchored at either the very top or very bottom
3198 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003199 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003200 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003201 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3202 showAppsCustomizeHelper(animated, springLoaded, contentType);
3203 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003204
Winson Chungc58497e2013-09-03 17:48:37 -07003205 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3206 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003207 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003208 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003209 mStateAnimation.cancel();
3210 mStateAnimation = null;
3211 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003212
Kenny Guyd794a3f2014-09-16 15:17:58 +01003213 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003214
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003215 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003216
Winson Chungf0ea4d32011-06-06 14:27:16 -07003217 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3218 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003219 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003220 final int itemsAlphaStagger =
3221 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003222
Winson Chungf0ea4d32011-06-06 14:27:16 -07003223 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003224 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003225 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003226
Adam Cohen2854d252014-08-27 16:04:07 -07003227 final ArrayList<View> layerViews = new ArrayList<View>();
3228
Adam Cohen6c5891a2014-07-09 23:53:15 -07003229 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3230 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003231 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003232 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003233 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003234 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3235 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003236 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3237 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003238
Adam Cohena38dc902014-09-07 17:48:55 +02003239 // If for some reason our views aren't initialized, don't animate
3240 boolean initialized = getAllAppsButton() != null;
3241
3242 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003243 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003244 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3245 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003246
Adam Cohen9bfdb762014-07-21 17:44:06 -07003247 final View page = content.getPageAt(content.getCurrentPage());
3248 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003249
Adam Cohen63f1ec02014-08-12 09:23:13 -07003250 final float initialPanelAlpha = 1f;
3251
3252 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3253 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003254 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3255 } else {
3256 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3257 }
3258
Adam Cohen9bfdb762014-07-21 17:44:06 -07003259 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003260 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003261 revealView.setVisibility(View.VISIBLE);
3262 // We need to hide this view as the animation start will be posted.
3263 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003264
Adam Cohen9bfdb762014-07-21 17:44:06 -07003265 int width = revealView.getMeasuredWidth();
3266 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003267 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003268
Adam Cohen63f1ec02014-08-12 09:23:13 -07003269 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003270 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003271
Adam Cohen63f1ec02014-08-12 09:23:13 -07003272 // Get the y delta between the center of the page and the center of the all apps button
3273 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3274 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003275
Adam Cohen2854d252014-08-27 16:04:07 -07003276 float alpha = 0;
3277 float xDrift = 0;
3278 float yDrift = 0;
3279 if (material) {
3280 alpha = isWidgetTray ? 0.3f : 1f;
3281 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3282 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3283 } else {
3284 yDrift = 2 * height / 3;
3285 xDrift = 0;
3286 }
3287 final float initAlpha = alpha;
3288
Adam Cohen9bfdb762014-07-21 17:44:06 -07003289 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003290 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003291 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003292 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003293 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003294 PropertyValuesHolder panelDriftX =
3295 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003296
Adam Cohen2854d252014-08-27 16:04:07 -07003297 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3298 panelAlpha, panelDriftY, panelDriftX);
3299
Adam Cohen9bfdb762014-07-21 17:44:06 -07003300 panelAlphaAndDrift.setDuration(revealDuration);
3301 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003302
Adam Cohen9bfdb762014-07-21 17:44:06 -07003303 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003304
Adam Cohen4e243a22014-08-10 18:30:55 -07003305 if (page != null) {
3306 page.setVisibility(View.VISIBLE);
3307 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003308 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003309
Adam Cohen2854d252014-08-27 16:04:07 -07003310 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3311 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003312 pageDrift.setDuration(revealDuration);
3313 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003314 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003315 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003316
Adam Cohen63f1ec02014-08-12 09:23:13 -07003317 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003318 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003319 itemsAlpha.setDuration(revealDuration);
3320 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3321 itemsAlpha.setStartDelay(itemsAlphaStagger);
3322 mStateAnimation.play(itemsAlpha);
3323 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003324
Adam Cohen4e243a22014-08-10 18:30:55 -07003325 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3326 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003327 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003328 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003329 indicatorsAlpha.setDuration(revealDuration);
3330 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003331
Adam Cohen9bfdb762014-07-21 17:44:06 -07003332 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003333 final View allApps = getAllAppsButton();
3334 int allAppsButtonSize = LauncherAppState.getInstance().
3335 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3336 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003337 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003338 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003339 reveal.setDuration(revealDuration);
3340 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003341
3342 reveal.addListener(new AnimatorListenerAdapter() {
3343 public void onAnimationStart(Animator animation) {
3344 if (!isWidgetTray) {
3345 allApps.setVisibility(View.INVISIBLE);
3346 }
3347 }
3348 public void onAnimationEnd(Animator animation) {
3349 if (!isWidgetTray) {
3350 allApps.setVisibility(View.VISIBLE);
3351 }
3352 }
3353 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003354 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003355 }
Michael Jurka1899a362011-11-03 13:50:45 -07003356
Adam Cohen9bfdb762014-07-21 17:44:06 -07003357 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3358 @Override
3359 public void onAnimationEnd(Animator animation) {
3360 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3361 dispatchOnLauncherTransitionEnd(toView, animated, false);
3362
3363 revealView.setVisibility(View.INVISIBLE);
3364 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003365 if (page != null) {
3366 page.setLayerType(View.LAYER_TYPE_NONE, null);
3367 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003368 content.setPageBackgroundsVisible(true);
3369
3370 // Hide the search bar
3371 if (mSearchDropTargetBar != null) {
3372 mSearchDropTargetBar.hideSearchBar(false);
3373 }
3374 }
3375
Adam Cohen9bfdb762014-07-21 17:44:06 -07003376 });
3377
Adam Cohen6c5891a2014-07-09 23:53:15 -07003378 if (workspaceAnim != null) {
3379 mStateAnimation.play(workspaceAnim);
3380 }
Adam Cohen2854d252014-08-27 16:04:07 -07003381
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003382 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3383 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003384 final AnimatorSet stateAnimation = mStateAnimation;
3385 final Runnable startAnimRunnable = new Runnable() {
3386 public void run() {
3387 // Check that mStateAnimation hasn't changed while
3388 // we waited for a layout/draw pass
3389 if (mStateAnimation != stateAnimation)
3390 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003391 dispatchOnLauncherTransitionStart(fromView, animated, false);
3392 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003393
3394 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003395 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003396 for (int i = 0; i < layerViews.size(); i++) {
3397 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003398 if (v != null) {
3399 boolean attached = true;
3400 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3401 attached = v.isAttachedToWindow();
3402 }
3403 if (attached) v.buildLayer();
3404 }
Adam Cohen2854d252014-08-27 16:04:07 -07003405 }
3406 }
3407 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003408 }
3409 };
Adam Cohen2854d252014-08-27 16:04:07 -07003410 toView.bringToFront();
3411 toView.setVisibility(View.VISIBLE);
3412 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003413 } else {
3414 toView.setTranslationX(0.0f);
3415 toView.setTranslationY(0.0f);
3416 toView.setScaleX(1.0f);
3417 toView.setScaleY(1.0f);
3418 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003419 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003420
Daniel Sandlere4f98912013-06-25 15:13:26 -04003421 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003422 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003423 if (mSearchDropTargetBar != null) {
3424 mSearchDropTargetBar.hideSearchBar(false);
3425 }
Michael Jurkaabded662011-03-04 12:06:57 -08003426 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003427 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003428 dispatchOnLauncherTransitionStart(fromView, animated, false);
3429 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003430 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003431 dispatchOnLauncherTransitionStart(toView, animated, false);
3432 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003433 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003434 }
3435
3436 /**
3437 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003438 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003439 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003440 */
Adam Cohened307df2013-10-02 09:37:31 -07003441 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003442 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003443
Michael Jurkab3e22d92011-10-31 15:58:33 -07003444 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003445 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 mStateAnimation.cancel();
3447 mStateAnimation = null;
3448 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003449
Kenny Guyd794a3f2014-09-16 15:17:58 +01003450 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003451 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003452
Winson Chungf0ea4d32011-06-06 14:27:16 -07003453 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003454 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003455 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003456 final int itemsAlphaStagger =
3457 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003458
Winson Chungf0ea4d32011-06-06 14:27:16 -07003459 final float scaleFactor = (float)
3460 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3461 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003462 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003463 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003464 final ArrayList<View> layerViews = new ArrayList<View>();
3465
Adam Cohened307df2013-10-02 09:37:31 -07003466 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003467 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003468 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003469 } else if (toState == Workspace.State.SPRING_LOADED ||
3470 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003471 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003472 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003473 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003474
Adam Cohena38dc902014-09-07 17:48:55 +02003475 // If for some reason our views aren't initialized, don't animate
3476 boolean initialized = getAllAppsButton() != null;
3477
3478 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003479 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003480 if (workspaceAnim != null) {
3481 mStateAnimation.play(workspaceAnim);
3482 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003483
Adam Cohen9bfdb762014-07-21 17:44:06 -07003484 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3485 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003486
Adam Cohen9bfdb762014-07-21 17:44:06 -07003487 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003488
3489 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3490 int count = content.getChildCount();
3491 for (int i = 0; i < count; i++) {
3492 View child = content.getChildAt(i);
3493 if (child != page) {
3494 child.setVisibility(View.INVISIBLE);
3495 }
3496 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003497 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003498
Adam Cohen2854d252014-08-27 16:04:07 -07003499 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3500 // don't perform all these no-op animations. In particularly, this was causing
3501 // the all-apps button to pop in and out.
3502 if (fromView.getVisibility() == View.VISIBLE) {
3503 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3504 final boolean isWidgetTray =
3505 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003506
Adam Cohen2854d252014-08-27 16:04:07 -07003507 if (isWidgetTray) {
3508 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3509 } else {
3510 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003511 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003512
Adam Cohen2854d252014-08-27 16:04:07 -07003513 int width = revealView.getMeasuredWidth();
3514 int height = revealView.getMeasuredHeight();
3515 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3516
3517 // Hide the real page background, and swap in the fake one
3518 revealView.setVisibility(View.VISIBLE);
3519 content.setPageBackgroundsVisible(false);
3520
3521 final View allAppsButton = getAllAppsButton();
3522 revealView.setTranslationY(0);
3523 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3524 allAppsButton, null);
3525
3526 float xDrift = 0;
3527 float yDrift = 0;
3528 if (material) {
3529 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3530 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3531 } else {
3532 yDrift = 5 * height / 4;
3533 xDrift = 0;
3534 }
3535
3536 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3537 TimeInterpolator decelerateInterpolator = material ?
3538 new LogDecelerateInterpolator(100, 0) :
3539 new LogDecelerateInterpolator(30, 0);
3540
3541 // The vertical motion of the apps panel should be delayed by one frame
3542 // from the conceal animation in order to give the right feel. We correpsondingly
3543 // shorten the duration so that the slide and conceal end at the same time.
3544 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3545 0, yDrift);
3546 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3547 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3548 panelDriftY.setInterpolator(decelerateInterpolator);
3549 mStateAnimation.play(panelDriftY);
3550
3551 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3552 0, xDrift);
3553 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3554 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3555 panelDriftX.setInterpolator(decelerateInterpolator);
3556 mStateAnimation.play(panelDriftX);
3557
3558 if (isWidgetTray || !material) {
3559 float finalAlpha = material ? 0.4f : 0f;
3560 revealView.setAlpha(1f);
3561 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3562 1f, finalAlpha);
3563 panelAlpha.setDuration(revealDuration);
3564 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3565 new AccelerateInterpolator(1.5f));
3566 mStateAnimation.play(panelAlpha);
3567 }
3568
3569 if (page != null) {
3570 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3571
3572 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3573 0, yDrift);
3574 page.setTranslationY(0);
3575 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3576 pageDrift.setInterpolator(decelerateInterpolator);
3577 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3578 mStateAnimation.play(pageDrift);
3579
3580 page.setAlpha(1f);
3581 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3582 itemsAlpha.setDuration(100);
3583 itemsAlpha.setInterpolator(decelerateInterpolator);
3584 mStateAnimation.play(itemsAlpha);
3585 }
3586
3587 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3588 pageIndicators.setAlpha(1f);
3589 ObjectAnimator indicatorsAlpha =
3590 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3591 indicatorsAlpha.setDuration(revealDuration);
3592 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3593 mStateAnimation.play(indicatorsAlpha);
3594
3595 width = revealView.getMeasuredWidth();
3596
3597 if (material) {
3598 if (!isWidgetTray) {
3599 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003600 }
Adam Cohen2854d252014-08-27 16:04:07 -07003601 int allAppsButtonSize = LauncherAppState.getInstance().
3602 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3603 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3604 Animator reveal =
3605 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3606 height / 2, revealRadius, finalRadius);
3607 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3608 reveal.setDuration(revealDuration);
3609 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003610
Adam Cohen2854d252014-08-27 16:04:07 -07003611 reveal.addListener(new AnimatorListenerAdapter() {
3612 public void onAnimationEnd(Animator animation) {
3613 revealView.setVisibility(View.INVISIBLE);
3614 if (!isWidgetTray) {
3615 allAppsButton.setVisibility(View.VISIBLE);
3616 }
3617 }
3618 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003619
Adam Cohen2854d252014-08-27 16:04:07 -07003620 mStateAnimation.play(reveal);
3621 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003622
Adam Cohen2854d252014-08-27 16:04:07 -07003623 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3624 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3625 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003626 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003627
3628 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003629 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003630 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003631 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003632 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3633 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003634 if (onCompleteRunnable != null) {
3635 onCompleteRunnable.run();
3636 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003637
3638 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003639 if (page != null) {
3640 page.setLayerType(View.LAYER_TYPE_NONE, null);
3641 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003642 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003643 // Unhide side pages
3644 int count = content.getChildCount();
3645 for (int i = 0; i < count; i++) {
3646 View child = content.getChildAt(i);
3647 child.setVisibility(View.VISIBLE);
3648 }
3649
3650 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003651 if (page != null) {
3652 page.setTranslationX(0);
3653 page.setTranslationY(0);
3654 page.setAlpha(1);
3655 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003656 content.setCurrentPage(content.getNextPage());
3657
Chet Haasebc2f0822012-10-26 17:59:53 -07003658 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003659 }
3660 });
3661
Adam Cohen2854d252014-08-27 16:04:07 -07003662 final AnimatorSet stateAnimation = mStateAnimation;
3663 final Runnable startAnimRunnable = new Runnable() {
3664 public void run() {
3665 // Check that mStateAnimation hasn't changed while
3666 // we waited for a layout/draw pass
3667 if (mStateAnimation != stateAnimation)
3668 return;
3669 dispatchOnLauncherTransitionStart(fromView, animated, false);
3670 dispatchOnLauncherTransitionStart(toView, animated, false);
3671
Kenny Guyd794a3f2014-09-16 15:17:58 +01003672 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003673 for (int i = 0; i < layerViews.size(); i++) {
3674 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003675 if (v != null) {
3676 boolean attached = true;
3677 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3678 attached = v.isAttachedToWindow();
3679 }
3680 if (attached) v.buildLayer();
3681 }
Adam Cohen2854d252014-08-27 16:04:07 -07003682 }
3683 }
3684 mStateAnimation.start();
3685 }
3686 };
3687 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003688 } else {
3689 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003690 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003691 dispatchOnLauncherTransitionStart(fromView, animated, true);
3692 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003693 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003694 dispatchOnLauncherTransitionStart(toView, animated, true);
3695 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003696 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003697 }
3698
Michael Jurkae326f182011-11-21 14:05:46 -08003699 @Override
3700 public void onTrimMemory(int level) {
3701 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003702 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003703 mAppsCustomizeTabHost.onTrimMemory();
3704 }
3705 }
3706
Adam Cohened307df2013-10-02 09:37:31 -07003707 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003708 showWorkspace(animated, null);
3709 }
3710
Adam Cohened307df2013-10-02 09:37:31 -07003711 protected void showWorkspace() {
3712 showWorkspace(true);
3713 }
3714
Adam Cohened66b2b2012-01-23 17:28:51 -08003715 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003716 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003717 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003718 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003719 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003720
Winson Chungc7d2b602012-05-16 17:02:20 -07003721 // Show the search bar (only animate if we were showing the drop target bar in spring
3722 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003723 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003724 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003725 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003726
Michael Jurkab3e22d92011-10-31 15:58:33 -07003727 // Set focus to the AppsCustomize button
3728 if (mAllAppsButton != null) {
3729 mAllAppsButton.requestFocus();
3730 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003731 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003732
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003733 // Change the state *after* we've called all the transition code
3734 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003735
Winson Chung5fb63472011-02-02 17:03:37 -08003736 // Resume the auto-advance of widgets
3737 mUserPresent = true;
3738 updateRunning();
3739
alanv1d4fde62012-10-17 13:15:47 -07003740 // Send an accessibility event to announce the context change
3741 getWindow().getDecorView()
3742 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003743
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003744 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003745 }
3746
Adam Cohened307df2013-10-02 09:37:31 -07003747 void showOverviewMode(boolean animated) {
3748 mWorkspace.setVisibility(View.VISIBLE);
3749 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3750 mState = State.WORKSPACE;
3751 onWorkspaceShown(animated);
3752 }
3753
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003754 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003755 }
3756
Winson Chung82963d52013-10-09 11:20:57 -07003757 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3758 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003759 if (mState != State.WORKSPACE) return;
3760
Winson Chung82963d52013-10-09 11:20:57 -07003761 if (resetPageToZero) {
3762 mAppsCustomizeTabHost.reset();
3763 }
Winson Chungc58497e2013-09-03 17:48:37 -07003764 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003765 mAppsCustomizeTabHost.post(new Runnable() {
3766 @Override
3767 public void run() {
3768 // We post this in-case the all apps view isn't yet constructed.
3769 mAppsCustomizeTabHost.requestFocus();
3770 }
3771 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003772
Michael Jurkab3e22d92011-10-31 15:58:33 -07003773 // Change the state *after* we've called all the transition code
3774 mState = State.APPS_CUSTOMIZE;
3775
3776 // Pause the auto-advance of widgets until we are out of AllApps
3777 mUserPresent = false;
3778 updateRunning();
3779 closeFolder();
3780
3781 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003782 getWindow().getDecorView()
3783 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003784 }
3785
Adam Cohen7777d962011-08-18 18:58:38 -07003786 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003787 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003788 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003789 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003790 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003791 }
Adam Cohen7777d962011-08-18 18:58:38 -07003792
Adam Cohenad4e15c2013-10-17 16:21:35 -07003793 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003794 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003795 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3796
Winson Chunge7a03942011-08-05 15:05:12 -07003797 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003798 @Override
3799 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003800 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003801 // Before we show workspace, hide all apps again because
3802 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3803 // clean up our state transition functions
3804 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003805 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003806 } else {
3807 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003808 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003809 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003810 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003811 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003812
Michael Jurkad3ef3062010-11-23 16:23:58 -08003813 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003814 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003815 final boolean animated = true;
3816 final boolean springLoaded = true;
3817 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003818 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003819 }
3820 // Otherwise, we are not in spring loaded mode, so don't do anything.
3821 }
3822
Michael Jurkab3e22d92011-10-31 15:58:33 -07003823 void lockAllApps() {
3824 // TODO
3825 }
3826
3827 void unlockAllApps() {
3828 // TODO
3829 }
3830
Winson Chungf0ea4d32011-06-06 14:27:16 -07003831 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003832 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003833 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003834 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003835 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003836 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003837 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003838 int duration = 0;
3839 if (mSearchDropTargetBar != null) {
3840 duration = mSearchDropTargetBar.getTransitionOutDuration();
3841 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003842 mHotseat.animate().alpha(0f).setDuration(duration);
3843 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003844 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003845 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003847 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003848 }
3849
Patrick Dubroy5f445422011-02-18 14:35:21 -08003850 /**
3851 * Add an item from all apps or customize onto the given workspace screen.
3852 * If layout is null, add to the current screen.
3853 */
3854 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003855 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003856 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003857 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003858 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003859
Winson Chungdff8ebb2011-09-08 17:25:31 -07003860 /** Maps the current orientation to an index for referencing orientation correct global icons */
3861 private int getCurrentOrientationIndexForGlobalIcons() {
3862 // default - 0, landscape - 1
3863 switch (getResources().getConfiguration().orientation) {
3864 case Configuration.ORIENTATION_LANDSCAPE:
3865 return 1;
3866 default:
3867 return 0;
3868 }
3869 }
3870
Michael Jurkaae65ee32012-04-30 11:45:54 -07003871 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003872 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003873 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003874 // Look for the toolbar icon specified in the activity meta-data
3875 Bundle metaData = packageManager.getActivityInfo(
3876 activityName, PackageManager.GET_META_DATA).metaData;
3877 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003878 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003879 if (iconResId != 0) {
3880 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003881 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003882 }
3883 }
3884 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003885 // This can happen if the activity defines an invalid drawable
3886 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3887 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003888 } catch (Resources.NotFoundException nfe) {
3889 // This can happen if the activity defines an invalid drawable
3890 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3891 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003892 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003893 return null;
3894 }
3895
3896 // if successful in getting icon, return it; otherwise, set button to use default drawable
3897 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003898 int buttonId, ComponentName activityName, int fallbackDrawableId,
3899 String toolbarResourceName) {
3900 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003901 Resources r = getResources();
3902 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3903 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003904
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003905 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003906 // If we were unable to find the icon via the meta-data, use a generic one
3907 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003908 toolbarIcon = r.getDrawable(fallbackDrawableId);
3909 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003910 if (button != null) {
3911 button.setCompoundDrawables(toolbarIcon, null, null, null);
3912 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003913 return null;
3914 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003915 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003916 if (button != null) {
3917 button.setCompoundDrawables(toolbarIcon, null, null, null);
3918 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003919 return toolbarIcon.getConstantState();
3920 }
3921 }
3922
3923 // if successful in getting icon, return it; otherwise, set button to use default drawable
3924 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003925 int buttonId, ComponentName activityName, int fallbackDrawableId,
3926 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003927 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003928 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003929
Michael Jurka19e0fc52011-07-22 18:00:21 -07003930 if (button != null) {
3931 // If we were unable to find the icon via the meta-data, use a
3932 // generic one
3933 if (toolbarIcon == null) {
3934 button.setImageResource(fallbackDrawableId);
3935 } else {
3936 button.setImageDrawable(toolbarIcon);
3937 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003938 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003939
3940 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3941
Michael Jurka0423dcf2010-10-05 14:56:18 -07003942 }
3943
Winson Chung1b884092012-06-08 17:13:02 -07003944 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003945 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003946 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003947 }
3948
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003949 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003950 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003951 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003952 }
3953
Winson Chungbb185bd2011-11-21 12:31:42 -08003954 private void invalidatePressedFocusedStates(View container, View button) {
3955 if (container instanceof HolographicLinearLayout) {
3956 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3957 layout.invalidatePressedFocusedStates();
3958 } else if (button instanceof HolographicImageView) {
3959 HolographicImageView view = (HolographicImageView) button;
3960 view.invalidatePressedFocusedStates();
3961 }
3962 }
3963
Cristina Stancu476493b2013-08-07 17:20:14 +01003964 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003965 if (mQsb == null) {
3966 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3967 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003968 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003969 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003970 }
3971
3972 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003973 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003974 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003975 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003976 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003977
Winson Chung1cad91e2011-05-25 17:41:01 -07003978 final SearchManager searchManager =
3979 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3980 ComponentName activityName = searchManager.getGlobalSearchActivity();
3981 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003982 int coi = getCurrentOrientationIndexForGlobalIcons();
3983 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003984 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3985 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3986 if (sGlobalSearchIcon[coi] == null) {
3987 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3988 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3989 TOOLBAR_ICON_METADATA_NAME);
3990 }
3991
Winson Chungc51db6a2011-10-05 11:44:49 -07003992 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3993 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003994 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003995 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003996 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003997 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003998 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3999 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004000 if (searchButton != null) searchButton.setVisibility(View.GONE);
4001 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004002 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004003 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004004 }
4005 }
4006
Cristina Stancu476493b2013-08-07 17:20:14 +01004007 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004008 final View searchButtonContainer = findViewById(R.id.search_button_container);
4009 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004010 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004011 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004012 }
4013
Cristina Stancu476493b2013-08-07 17:20:14 +01004014 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004015 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004016 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004017
Winson Chungc51db6a2011-10-05 11:44:49 -07004018 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004019 final SearchManager searchManager =
4020 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4021 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4022
4023 ComponentName activityName = null;
4024 if (globalSearchActivity != null) {
4025 // Check if the global search activity handles voice search
4026 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4027 intent.setPackage(globalSearchActivity.getPackageName());
4028 activityName = intent.resolveActivity(getPackageManager());
4029 }
4030
4031 if (activityName == null) {
4032 // Fallback: check if an activity other than the global search activity
4033 // resolves this
4034 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4035 activityName = intent.resolveActivity(getPackageManager());
4036 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004037 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004038 int coi = getCurrentOrientationIndexForGlobalIcons();
4039 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004040 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4041 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4042 if (sVoiceSearchIcon[coi] == null) {
4043 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4044 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4045 TOOLBAR_ICON_METADATA_NAME);
4046 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004047 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004048 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004049 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004050 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004051 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004052 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004053 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004054 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004055 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004056 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004057 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004058 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004059
Cristina Stancu476493b2013-08-07 17:20:14 +01004060 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004061 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4062 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004063 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004064 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004065 }
4066
Winson Chung5841efa2013-09-30 18:06:44 -07004067 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004068 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4069 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004070 boolean visible = !forceDisableVoiceButtonProxy &&
4071 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004072 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004073 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004074 }
4075 }
Winson Chung5841efa2013-09-30 18:06:44 -07004076
4077 /**
4078 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4079 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4080 */
4081 public void disableVoiceButtonProxy(boolean disabled) {
4082 updateVoiceButtonProxyVisible(disabled);
4083 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004084
Michael Jurkad7c28052012-04-27 15:43:36 -07004085 @Override
4086 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004087 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004088 final List<CharSequence> text = event.getText();
4089 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004090 // Populate event with a fake title based on the current state.
4091 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004092 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004093 } else {
4094 text.add(getString(R.string.all_apps_home_button_label));
4095 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004096 return result;
4097 }
4098
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004099 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004100 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004101 */
4102 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4103 @Override
4104 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004105 closeSystemDialogs();
4106 }
4107 }
4108
4109 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004110 * Receives notifications whenever the appwidgets are reset.
4111 */
4112 private class AppWidgetResetObserver extends ContentObserver {
4113 public AppWidgetResetObserver() {
4114 super(new Handler());
4115 }
4116
4117 @Override
4118 public void onChange(boolean selfChange) {
4119 onAppWidgetReset();
4120 }
4121 }
4122
4123 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004124 * If the activity is currently paused, signal that we need to run the passed Runnable
4125 * in onResume.
4126 *
4127 * This needs to be called from incoming places where resources might have been loaded
4128 * while we are paused. That is becaues the Configuration might be wrong
4129 * when we're not running, and if it comes back to what it was when we
4130 * were paused, we are not restarted.
4131 *
4132 * Implementation of the method from LauncherModel.Callbacks.
4133 *
4134 * @return true if we are currently paused. The caller might be able to
4135 * skip some work in that case since we will come back again.
4136 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004137 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004138 if (mPaused) {
4139 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004140 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004141 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004142 }
4143 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004144 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004145 return true;
4146 } else {
4147 return false;
4148 }
4149 }
4150
Michael Jurkac402cd92013-05-20 15:49:32 +02004151 private boolean waitUntilResume(Runnable run) {
4152 return waitUntilResume(run, false);
4153 }
4154
Michael Jurka1e2f4652013-07-08 18:03:46 -07004155 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004156 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004157 }
4158
Michael Jurka7607c2f2013-04-03 14:33:19 -07004159 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004160 * If the activity is currently paused, signal that we need to re-run the loader
4161 * in onResume.
4162 *
4163 * This needs to be called from incoming places where resources might have been loaded
4164 * while we are paused. That is becaues the Configuration might be wrong
4165 * when we're not running, and if it comes back to what it was when we
4166 * were paused, we are not restarted.
4167 *
4168 * Implementation of the method from LauncherModel.Callbacks.
4169 *
4170 * @return true if we are currently paused. The caller might be able to
4171 * skip some work in that case since we will come back again.
4172 */
4173 public boolean setLoadOnResume() {
4174 if (mPaused) {
4175 Log.i(TAG, "setLoadOnResume");
4176 mOnResumeNeedsLoad = true;
4177 return true;
4178 } else {
4179 return false;
4180 }
4181 }
4182
4183 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004185 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004187 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004188 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004189 } else {
4190 return SCREEN_COUNT / 2;
4191 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004193
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 /**
4195 * Refreshes the shortcuts shown on the workspace.
4196 *
4197 * Implementation of the method from LauncherModel.Callbacks.
4198 */
4199 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004200 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004201
Michael Jurka7607c2f2013-04-03 14:33:19 -07004202 // If we're starting binding all over again, clear any bind calls we'd postponed in
4203 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4204 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004205 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004206
Winson Chungd64d1762013-08-20 14:37:16 -07004207 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004208 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004209 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004210
Michael Jurka05bf644e2011-11-30 20:28:53 -08004211 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004212 if (mHotseat != null) {
4213 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004214 }
4215 }
4216
Adam Cohendcd297f2013-06-18 13:13:40 -07004217 @Override
4218 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004219 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004220
Adam Cohen5084cba2013-09-03 12:01:16 -07004221 // If there are no screens, we need to have an empty screen
4222 if (orderedScreenIds.size() == 0) {
4223 mWorkspace.addExtraEmptyScreen();
4224 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004225
4226 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004227 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004228 if (hasCustomContentToLeft()) {
4229 mWorkspace.createCustomContentContainer();
4230 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004231 }
Winson Chung64359a52013-07-08 17:17:08 -07004232 }
4233
4234 @Override
4235 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004236 // Log to disk
4237 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4238 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4239 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004240 int count = orderedScreenIds.size();
4241 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004242 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004243 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004244 }
4245
Adam Cohen39a06042013-07-19 14:30:12 -07004246 private boolean shouldShowWeightWatcher() {
4247 String spKey = LauncherAppState.getSharedPreferencesKey();
4248 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004249 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004250
4251 return show;
4252 }
4253
4254 private void toggleShowWeightWatcher() {
4255 String spKey = LauncherAppState.getSharedPreferencesKey();
4256 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4257 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4258
4259 show = !show;
4260
4261 SharedPreferences.Editor editor = sp.edit();
4262 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4263 editor.commit();
4264
4265 if (mWeightWatcher != null) {
4266 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4267 }
4268 }
4269
Winson Chungd64d1762013-08-20 14:37:16 -07004270 public void bindAppsAdded(final ArrayList<Long> newScreens,
4271 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004272 final ArrayList<ItemInfo> addAnimated,
4273 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004274 Runnable r = new Runnable() {
4275 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004276 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004277 }
4278 };
4279 if (waitUntilResume(r)) {
4280 return;
4281 }
4282
Winson Chungd64d1762013-08-20 14:37:16 -07004283 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004284 if (newScreens != null) {
4285 bindAddScreens(newScreens);
4286 }
Winson Chungd64d1762013-08-20 14:37:16 -07004287
4288 // We add the items without animation on non-visible pages, and with
4289 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004290 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004291 bindItems(addNotAnimated, 0,
4292 addNotAnimated.size(), false);
4293 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004294 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004295 bindItems(addAnimated, 0,
4296 addAnimated.size(), true);
4297 }
Winson Chungc58497e2013-09-03 17:48:37 -07004298
Winson Chung87412982013-10-03 18:34:14 -07004299 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004300 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004301
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004302 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004303 addedApps != null && mAppsCustomizeContent != null) {
4304 mAppsCustomizeContent.addApps(addedApps);
4305 }
Winson Chungd64d1762013-08-20 14:37:16 -07004306 }
4307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004308 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004309 * Bind the items start-end from the list.
4310 *
4311 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004312 */
Winson Chung64359a52013-07-08 17:17:08 -07004313 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4314 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004315 Runnable r = new Runnable() {
4316 public void run() {
4317 bindItems(shortcuts, start, end, forceAnimateIcons);
4318 }
4319 };
4320 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004321 return;
4322 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004323
Winson Chungf0c6ae02012-03-21 16:10:31 -07004324 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004325 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4326 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004327 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004328 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004329 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004330 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004332
4333 // Short circuit if we are loading dock items for a configuration which has no dock
4334 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4335 mHotseat == null) {
4336 continue;
4337 }
4338
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339 switch (item.itemType) {
4340 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4341 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004342 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004343 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004344
Winson Chung64359a52013-07-08 17:17:08 -07004345 /*
4346 * TODO: FIX collision case
4347 */
Winson Chungce376632013-07-11 16:12:41 -07004348 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4349 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4350 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004351 View v = cl.getChildAt(item.cellX, item.cellY);
4352 Object tag = v.getTag();
4353 String desc = "Collision while binding workspace item: " + item
4354 + ". Collides with " + tag;
4355 if (LauncherAppState.isDogfoodBuild()) {
4356 throw (new RuntimeException(desc));
4357 } else {
4358 Log.d(TAG, desc);
4359 }
Winson Chungce376632013-07-11 16:12:41 -07004360 }
Winson Chung64359a52013-07-08 17:17:08 -07004361 }
4362
Adam Cohendcd297f2013-06-18 13:13:40 -07004363 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4364 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004365 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004366 // Animate all the applications up now
4367 shortcut.setAlpha(0f);
4368 shortcut.setScaleX(0f);
4369 shortcut.setScaleY(0f);
4370 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004371 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004372 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004373 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004374 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004375 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004376 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004377 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004378 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4379 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004380 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004381 default:
4382 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004384 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004385
Winson Chung997a9232013-07-24 15:33:46 -07004386 if (animateIcons) {
4387 // Animate to the correct page
4388 if (newShortcutsScreenId > -1) {
4389 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004390 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004391 final Runnable startBounceAnimRunnable = new Runnable() {
4392 public void run() {
4393 anim.playTogether(bounceAnims);
4394 anim.start();
4395 }
4396 };
Winson Chung997a9232013-07-24 15:33:46 -07004397 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004398 // We post the animation slightly delayed to prevent slowdowns
4399 // when we are loading right after we return to launcher.
4400 mWorkspace.postDelayed(new Runnable() {
4401 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004402 if (mWorkspace != null) {
4403 mWorkspace.snapToPage(newScreenIndex);
4404 mWorkspace.postDelayed(startBounceAnimRunnable,
4405 NEW_APPS_ANIMATION_DELAY);
4406 }
Winson Chung94d67682013-09-25 16:29:40 -07004407 }
4408 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004409 } else {
4410 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004411 }
4412 }
Winson Chung64359a52013-07-08 17:17:08 -07004413 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004414 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004415 }
4416
Joe Onorato9c1289c2009-08-17 11:03:03 -04004417 /**
4418 * Implementation of the method from LauncherModel.Callbacks.
4419 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004420 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004421 Runnable r = new Runnable() {
4422 public void run() {
4423 bindFolders(folders);
4424 }
4425 };
4426 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004427 return;
4428 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004429 sFolders.clear();
4430 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004432
4433 /**
4434 * Add the views for a widget to the workspace.
4435 *
4436 * Implementation of the method from LauncherModel.Callbacks.
4437 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004438 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004439 Runnable r = new Runnable() {
4440 public void run() {
4441 bindAppWidget(item);
4442 }
4443 };
4444 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004445 return;
4446 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004447
Daniel Sandler843e8602010-06-07 14:59:01 -04004448 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4449 if (DEBUG_WIDGETS) {
4450 Log.d(TAG, "bindAppWidget: " + item);
4451 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004452 final Workspace workspace = mWorkspace;
4453
Sunny Goyalff572272014-07-23 13:58:07 -07004454 AppWidgetProviderInfo appWidgetInfo;
4455 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4456 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4457
4458 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4459 if (appWidgetInfo == null) {
4460 if (DEBUG_WIDGETS) {
4461 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4462 + " belongs to component " + item.providerName
4463 + ", as the povider is null");
4464 }
4465 LauncherModel.deleteItemFromDatabase(this, item);
4466 return;
4467 }
4468 // Note: This assumes that the id remap broadcast is received before this step.
4469 // If that is not the case, the id remap will be ignored and user may see the
4470 // click to setup view.
4471 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4472 pendingInfo.spanX = item.spanX;
4473 pendingInfo.spanY = item.spanY;
4474 pendingInfo.minSpanX = item.minSpanX;
4475 pendingInfo.minSpanY = item.minSpanY;
4476 Bundle options =
4477 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4478
Sunny Goyalff572272014-07-23 13:58:07 -07004479 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004480 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4481 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004482
4483 // TODO consider showing a permission dialog when the widget is clicked.
4484 if (!success) {
4485 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4486 if (DEBUG_WIDGETS) {
4487 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4488 + " belongs to component " + item.providerName
4489 + ", as the launcher is unable to bing a new widget id");
4490 }
4491 LauncherModel.deleteItemFromDatabase(this, item);
4492 return;
4493 }
4494
4495 item.appWidgetId = newWidgetId;
4496
4497 // If the widget has a configure activity, it is still needs to set it up, otherwise
4498 // the widget is ready to go.
4499 item.restoreStatus = (appWidgetInfo.configure == null)
4500 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4501 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4502
4503 LauncherModel.updateItemInDatabase(this, item);
4504 }
4505
Sunny Goyal651077b2014-06-30 14:15:31 -07004506 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4507 final int appWidgetId = item.appWidgetId;
4508 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4509 if (DEBUG_WIDGETS) {
4510 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4511 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004512
Sunny Goyal651077b2014-06-30 14:15:31 -07004513 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4514 } else {
4515 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004516 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4517 view.updateIcon(mIconCache);
4518 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004519 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004520 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004522
Joe Onorato9c1289c2009-08-17 11:03:03 -04004523 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004524 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525
Adam Cohendcd297f2013-06-18 13:13:40 -07004526 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004527 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004528 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4529
Joe Onorato9c1289c2009-08-17 11:03:03 -04004530 workspace.requestLayout();
4531
Daniel Sandler843e8602010-06-07 14:59:01 -04004532 if (DEBUG_WIDGETS) {
4533 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4534 + (SystemClock.uptimeMillis()-start) + "ms");
4535 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004536 }
4537
Sunny Goyalff572272014-07-23 13:58:07 -07004538 /**
4539 * Restores a pending widget.
4540 *
4541 * @param appWidgetId The app widget id
4542 * @param cellInfo The position on screen where to create the widget.
4543 */
4544 private void completeRestoreAppWidget(final int appWidgetId) {
4545 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4546 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4547 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4548 return;
4549 }
4550
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004551 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004552 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4553
4554 mWorkspace.reinflateWidgetsIfNecessary();
4555 LauncherModel.updateItemInDatabase(this, info);
4556 }
4557
Adam Cohen1462de32012-07-24 22:34:36 -07004558 public void onPageBoundSynchronously(int page) {
4559 mSynchronouslyBoundPages.add(page);
4560 }
4561
Joe Onorato9c1289c2009-08-17 11:03:03 -04004562 /**
4563 * Callback saying that there aren't any more items to bind.
4564 *
4565 * Implementation of the method from LauncherModel.Callbacks.
4566 */
Adam Cohene25af792013-06-06 23:08:25 -07004567 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004568 Runnable r = new Runnable() {
4569 public void run() {
4570 finishBindingItems(upgradePath);
4571 }
4572 };
4573 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004574 return;
4575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004576 if (mSavedState != null) {
4577 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004578 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004579 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004580 mSavedState = null;
4581 }
4582
Adam Cohen1462de32012-07-24 22:34:36 -07004583 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004584
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004585 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004586 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004587
4588 // If we received the result of any pending adds while the loader was running (e.g. the
4589 // widget configuration forced an orientation change), process them now.
4590 if (sPendingAddItem != null) {
4591 final long screenId = completeAdd(sPendingAddItem);
4592
4593 // TODO: this moves the user to the page where the pending item was added. Ideally,
4594 // the screen would be guaranteed to exist after bind, and the page would be set through
4595 // the workspace restore process.
4596 mWorkspace.post(new Runnable() {
4597 @Override
4598 public void run() {
4599 mWorkspace.snapToScreenId(screenId);
4600 }
4601 });
4602 sPendingAddItem = null;
4603 }
4604
Adam Cohene25af792013-06-06 23:08:25 -07004605 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004606 mWorkspace.getUniqueComponents(true, null);
4607 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004608 }
Sunny Goyale755d462014-07-22 13:48:29 -07004609 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004610 }
4611
Adam Cohen3ed316a2014-07-23 18:21:20 -07004612 private void sendLoadingCompleteBroadcastIfNecessary() {
4613 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4614 String permission =
4615 getResources().getString(R.string.receive_first_load_broadcast_permission);
4616 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4617 sendBroadcast(intent, permission);
4618 SharedPreferences.Editor editor = mSharedPrefs.edit();
4619 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4620 editor.apply();
4621 }
4622 }
4623
Winson Chunga0b7e862013-09-05 16:03:15 -07004624 public boolean isAllAppsButtonRank(int rank) {
4625 if (mHotseat != null) {
4626 return mHotseat.isAllAppsButtonRank(rank);
4627 }
4628 return false;
4629 }
4630
Winson Chunga2413752012-04-03 14:22:34 -07004631 private boolean canRunNewAppsAnimation() {
4632 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4633 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4634 }
4635
Winson Chungc9168342013-06-26 14:54:55 -07004636 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4637 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4638 PropertyValuesHolder.ofFloat("alpha", 1f),
4639 PropertyValuesHolder.ofFloat("scaleX", 1f),
4640 PropertyValuesHolder.ofFloat("scaleY", 1f));
4641 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4642 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4643 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4644 return bounceAnim;
4645 }
4646
Adam Cohen27772732013-11-11 14:00:56 +00004647 public boolean useVerticalBarLayout() {
4648 return LauncherAppState.getInstance().getDynamicGrid().
4649 getDeviceProfile().isVerticalBarLayout();
4650 }
4651
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004652 protected Rect getSearchBarBounds() {
4653 return LauncherAppState.getInstance().getDynamicGrid().
4654 getDeviceProfile().getSearchBarBounds();
4655 }
4656
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004657 @Override
4658 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004659 boolean searchVisible = updateGlobalSearchIcon();
4660 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004661 if (mSearchDropTargetBar != null) {
4662 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4663 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004664 }
4665
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004666 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004667 * Add the icons for all apps.
4668 *
4669 * Implementation of the method from LauncherModel.Callbacks.
4670 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004671 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004672 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004673 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4674 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4675 getHotseat().addAllAppsFolder(mIconCache, apps,
4676 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4677 }
4678 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004679 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004680 if (mAppsCustomizeContent != null) {
4681 mAppsCustomizeContent.onPackagesUpdated(
4682 LauncherModel.getSortedWidgetsAndShortcuts(this));
4683 }
Winson Chungc58497e2013-09-03 17:48:37 -07004684 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004685 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004686 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004687 mAppsCustomizeContent.onPackagesUpdated(
4688 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004689 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004690 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004691 }
4692
4693 /**
4694 * A package was updated.
4695 *
4696 * Implementation of the method from LauncherModel.Callbacks.
4697 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004698 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004699 Runnable r = new Runnable() {
4700 public void run() {
4701 bindAppsUpdated(apps);
4702 }
4703 };
4704 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004705 return;
4706 }
4707
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004708 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004709 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004710 }
Winson Chungc58497e2013-09-03 17:48:37 -07004711
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004712 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004713 mAppsCustomizeContent != null) {
4714 mAppsCustomizeContent.updateApps(apps);
4715 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004716 }
4717
4718 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004719 * Some shortcuts were updated in the background.
4720 *
4721 * Implementation of the method from LauncherModel.Callbacks.
4722 */
4723 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4724 Runnable r = new Runnable() {
4725 public void run() {
4726 bindShortcutsUpdated(shortcuts);
4727 }
4728 };
4729 if (waitUntilResume(r)) {
4730 return;
4731 }
4732
4733 if (mWorkspace != null) {
4734 mWorkspace.updateShortcuts(shortcuts);
4735 }
4736 }
4737
4738 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004739 * Update the state of a package, typically related to install state.
4740 *
4741 * Implementation of the method from LauncherModel.Callbacks.
4742 */
Sunny Goyale755d462014-07-22 13:48:29 -07004743 @Override
4744 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004745 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004746 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004747 }
4748 }
4749
4750 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004751 * Update the label and icon of all the icons in a package
4752 *
4753 * Implementation of the method from LauncherModel.Callbacks.
4754 */
4755 @Override
4756 public void updatePackageBadge(String packageName) {
4757 if (mWorkspace != null) {
4758 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4759 }
4760 }
4761
4762 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004763 * A package was uninstalled. We take both the super set of packageNames
4764 * in addition to specific applications to remove, the reason being that
4765 * this can be called when a package is updated as well. In that scenario,
4766 * we only remove specific components from the workspace, where as
4767 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004768 *
4769 * Implementation of the method from LauncherModel.Callbacks.
4770 */
Winson Chung83892cc2013-05-01 16:53:33 -07004771 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004772 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004773 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004774 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004775 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004776 }
Winson Chungd64d1762013-08-20 14:37:16 -07004777 };
4778 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004779 return;
4780 }
4781
Winson Chungdf95eb12013-10-16 14:57:07 -07004782 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004783 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004784 }
4785 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004786 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004787 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004788
Winson Chunga1820962011-10-03 16:31:06 -07004789 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004790 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004791
Winson Chungdf95eb12013-10-16 14:57:07 -07004792 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004793 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004794 mAppsCustomizeContent != null) {
4795 mAppsCustomizeContent.removeApps(appInfos);
4796 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004797 }
Joe Onoratobe386092009-11-17 17:32:16 -08004798
4799 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004800 * A number of packages were updated.
4801 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004802 private ArrayList<Object> mWidgetsAndShortcuts;
4803 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004804 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004805 bindPackagesUpdated(mWidgetsAndShortcuts);
4806 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004807 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004808 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004809 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4810 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4811 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004812 return;
4813 }
4814
Winson Chung64359a52013-07-08 17:17:08 -07004815 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004816 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004817 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004818 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004819 }
4820
Winson Chung400438b2011-01-16 17:53:48 -08004821 private int mapConfigurationOriActivityInfoOri(int configOri) {
4822 final Display d = getWindowManager().getDefaultDisplay();
4823 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4824 switch (d.getRotation()) {
4825 case Surface.ROTATION_0:
4826 case Surface.ROTATION_180:
4827 // We are currently in the same basic orientation as the natural orientation
4828 naturalOri = configOri;
4829 break;
4830 case Surface.ROTATION_90:
4831 case Surface.ROTATION_270:
4832 // We are currently in the other basic orientation to the natural orientation
4833 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4834 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4835 break;
4836 }
4837
4838 int[] oriMap = {
4839 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4840 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4841 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4842 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4843 };
4844 // Since the map starts at portrait, we need to offset if this device's natural orientation
4845 // is landscape.
4846 int indexOffset = 0;
4847 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4848 indexOffset = 1;
4849 }
4850 return oriMap[(d.getRotation() + indexOffset) % 4];
4851 }
Adam Cohen446e9402011-09-15 18:21:21 -07004852
Winson Chung4b919f82012-05-01 10:44:08 -07004853 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004854 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004855 getResources().getBoolean(R.bool.allow_rotation);
4856 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004857 }
Winson Chung4b919f82012-05-01 10:44:08 -07004858 public void lockScreenOrientation() {
4859 if (isRotationEnabled()) {
4860 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4861 .getConfiguration().orientation));
4862 }
4863 }
4864 public void unlockScreenOrientation(boolean immediate) {
4865 if (isRotationEnabled()) {
4866 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004867 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004868 } else {
4869 mHandler.postDelayed(new Runnable() {
4870 public void run() {
4871 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4872 }
4873 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004874 }
Winson Chung4b919f82012-05-01 10:44:08 -07004875 }
Winson Chung400438b2011-01-16 17:53:48 -08004876 }
4877
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004878 /**
4879 * Called when the SearchBar hint should be changed.
4880 *
4881 * @param hint the hint to be displayed in the search bar.
4882 */
4883 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004884
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004885 }
4886
Winson Chunge43a1e72014-01-15 10:33:02 -08004887 protected boolean isLauncherPreinstalled() {
4888 PackageManager pm = getPackageManager();
4889 try {
4890 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4891 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4892 return true;
4893 } else {
4894 return false;
4895 }
4896 } catch (NameNotFoundException e) {
4897 e.printStackTrace();
4898 return false;
4899 }
4900 }
4901
Adam Cohenea90f832014-05-21 15:03:34 -07004902 /**
4903 * This method indicates whether or not we should suggest default wallpaper dimensions
4904 * when our wallpaper cropper was not yet used to set a wallpaper.
4905 */
4906 protected boolean overrideWallpaperDimensions() {
4907 return true;
4908 }
4909
Adam Cohen5eed5d82014-05-19 13:12:13 -07004910 protected boolean shouldClingFocusHotseatApp() {
4911 return false;
4912 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004913 protected String getFirstRunClingSearchBarHint() {
4914 return "";
4915 }
4916 protected String getFirstRunCustomContentHint() {
4917 return "";
4918 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004919 protected int getFirstRunFocusedHotseatAppDrawableId() {
4920 return -1;
4921 }
4922 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4923 return null;
4924 }
4925 protected int getFirstRunFocusedHotseatAppRank() {
4926 return -1;
4927 }
4928 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4929 return "";
4930 }
4931 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4932 return "";
4933 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004934
Adam Cohen432609a2014-03-13 17:03:22 -07004935 /**
4936 * To be overridden by subclasses to indicate that there is an activity to launch
4937 * before showing the standard launcher experience.
4938 */
4939 protected boolean hasFirstRunActivity() {
4940 return false;
4941 }
4942
4943 /**
4944 * To be overridden by subclasses to launch any first run activity
4945 */
4946 protected Intent getFirstRunActivity() {
4947 return null;
4948 }
4949
Winson Chunga6945242014-01-08 14:04:34 -08004950 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004951 return !ActivityManager.isRunningInTestHarness() &&
4952 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004953 }
4954
Adam Cohen4a9423d2014-03-28 14:22:31 -07004955 protected boolean hasRunFirstRunActivity() {
4956 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4957 }
4958
Adam Cohen432609a2014-03-13 17:03:22 -07004959 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004960 if (shouldRunFirstRunActivity() &&
4961 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004962 Intent firstRunIntent = getFirstRunActivity();
4963 if (firstRunIntent != null) {
4964 startActivity(firstRunIntent);
4965 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004966 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004967 }
4968 }
Adam Cohen432609a2014-03-13 17:03:22 -07004969 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004970 }
4971
4972 private void markFirstRunActivityShown() {
4973 SharedPreferences.Editor editor = mSharedPrefs.edit();
4974 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4975 editor.apply();
4976 }
4977
Adam Cohen432609a2014-03-13 17:03:22 -07004978 /**
4979 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4980 * screen that must be displayed and dismissed.
4981 */
4982 protected boolean hasDismissableIntroScreen() {
4983 return false;
4984 }
4985
4986 /**
4987 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4988 */
4989 protected View getIntroScreen() {
4990 return null;
4991 }
4992
4993 /**
4994 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4995 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4996 */
4997 private boolean shouldShowIntroScreen() {
4998 return hasDismissableIntroScreen() &&
4999 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5000 }
5001
5002 protected void showIntroScreen() {
5003 View introScreen = getIntroScreen();
5004 changeWallpaperVisiblity(false);
5005 if (introScreen != null) {
5006 mDragLayer.showOverlayView(introScreen);
5007 }
5008 }
5009
5010 public void dismissIntroScreen() {
5011 markIntroScreenDismissed();
5012 if (showFirstRunActivity()) {
5013 // We delay hiding the intro view until the first run activity is showing. This
5014 // avoids a blip.
5015 mWorkspace.postDelayed(new Runnable() {
5016 @Override
5017 public void run() {
5018 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005019 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005020 }
5021 }, ACTIVITY_START_DELAY);
5022 } else {
5023 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005024 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005025 }
5026 changeWallpaperVisiblity(true);
5027 }
5028
5029 private void markIntroScreenDismissed() {
5030 SharedPreferences.Editor editor = mSharedPrefs.edit();
5031 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5032 editor.apply();
5033 }
5034
Sunny Goyal88d60f12014-09-08 03:47:29 -07005035 private void showFirstRunClings() {
5036 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5037 // on the device, then we always show the first run cling experience (or if there is no
5038 // launcher2). Otherwise, we prompt the user upon started for migration
5039 LauncherClings launcherClings = new LauncherClings(this);
5040 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5041 if (mModel.canMigrateFromOldLauncherDb(this)) {
5042 launcherClings.showMigrationCling();
5043 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005044 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005045 }
5046 }
5047 }
5048
Winson Chunga6945242014-01-08 14:04:34 -08005049 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005050 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5051 if (mHotseat != null) mHotseat.setAlpha(1f);
5052 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5053 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005054 }
5055
5056 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005057 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5058 if (mHotseat != null) mHotseat.setAlpha(0f);
5059 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5060 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005061 }
5062
Mathew Inwood72fbec12013-11-19 15:45:07 +00005063 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005064 // Called from search suggestion, not supported in other profiles.
5065 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5066 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5067 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5068 myUser);
5069 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005070 return null;
5071 }
Kenny Guyed131872014-04-30 03:02:21 +01005072 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005073 }
5074
5075 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5076 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005077 // Called from search suggestion, not supported in other profiles.
5078 return createShortcutDragInfo(shortcutIntent, caption, icon,
5079 UserHandleCompat.myUserHandle());
5080 }
5081
5082 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5083 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005084 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005085 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005086 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005087 }
5088
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005089 protected void moveWorkspaceToDefaultScreen() {
5090 mWorkspace.moveToDefaultScreen(false);
5091 }
5092
Mathew Inwood72fbec12013-11-19 15:45:07 +00005093 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5094 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005095 mWorkspace.onExternalDragStartedWithItem(dragView);
5096 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005097 }
5098
Anjali Koppalf5d29132014-02-28 13:33:27 -08005099 @Override
5100 public void onPageSwitch(View newPage, int newPageIndex) {
5101 }
5102
Winson Chung80baf5a2010-08-09 16:03:15 -07005103 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005104 * Prints out out state for debugging.
5105 */
5106 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005107 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005108 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005109 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5110 Log.d(TAG, "mRestoring=" + mRestoring);
5111 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5112 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005113 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005114 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005115
Winson Chung785d2eb2011-04-14 16:08:02 -07005116 if (mAppsCustomizeContent != null) {
5117 mAppsCustomizeContent.dumpState();
5118 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005119 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005120 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005121
5122 @Override
5123 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5124 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005125 synchronized (sDumpLogs) {
5126 writer.println(" ");
5127 writer.println("Debug logs: ");
5128 for (int i = 0; i < sDumpLogs.size(); i++) {
5129 writer.println(" " + sDumpLogs.get(i));
5130 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005131 }
5132 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005133
5134 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005135 if (DEBUG_DUMP_LOG) {
5136 synchronized (sDumpLogs) {
5137 Log.d(TAG, "");
5138 Log.d(TAG, "*********************");
5139 Log.d(TAG, "Launcher debug logs: ");
5140 for (int i = 0; i < sDumpLogs.size(); i++) {
5141 Log.d(TAG, " " + sDumpLogs.get(i));
5142 }
5143 Log.d(TAG, "*********************");
5144 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005145 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005146 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005147 }
5148
5149 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005150 addDumpLog(tag, log, null, debugLog);
5151 }
5152
5153 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005154 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005155 if (e != null) {
5156 Log.d(tag, log, e);
5157 } else {
5158 Log.d(tag, log);
5159 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005160 }
Winson Chungede41292013-09-19 16:27:36 -07005161 if (DEBUG_DUMP_LOG) {
5162 sDateStamp.setTime(System.currentTimeMillis());
5163 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005164 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5165 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005166 }
Winson Chungede41292013-09-19 16:27:36 -07005167 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005168 }
5169
Winson Chungede41292013-09-19 16:27:36 -07005170 public void dumpLogsToLocalData() {
5171 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005172 new AsyncTask<Void, Void, Void>() {
5173 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005174 boolean success = false;
5175 sDateStamp.setTime(sRunStart);
5176 String FILENAME = sDateStamp.getMonth() + "-"
5177 + sDateStamp.getDay() + "_"
5178 + sDateStamp.getHours() + "-"
5179 + sDateStamp.getMinutes() + "_"
5180 + sDateStamp.getSeconds() + ".txt";
5181
5182 FileOutputStream fos = null;
5183 File outFile = null;
5184 try {
5185 outFile = new File(getFilesDir(), FILENAME);
5186 outFile.createNewFile();
5187 fos = new FileOutputStream(outFile);
5188 } catch (Exception e) {
5189 e.printStackTrace();
5190 }
5191 if (fos != null) {
5192 PrintWriter writer = new PrintWriter(fos);
5193
5194 writer.println(" ");
5195 writer.println("Debug logs: ");
5196 synchronized (sDumpLogs) {
5197 for (int i = 0; i < sDumpLogs.size(); i++) {
5198 writer.println(" " + sDumpLogs.get(i));
5199 }
5200 }
5201 writer.close();
5202 }
5203 try {
5204 if (fos != null) {
5205 fos.close();
5206 success = true;
5207 }
5208 } catch (IOException e) {
5209 e.printStackTrace();
5210 }
Michael Jurka43467462013-11-14 12:03:58 +01005211 return null;
Winson Chungede41292013-09-19 16:27:36 -07005212 }
Michael Jurka43467462013-11-14 12:03:58 +01005213 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005214 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005216}
Michael Jurka2763be32011-02-24 11:19:57 -08005217
Michael Jurkaabded662011-03-04 12:06:57 -08005218interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005219 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005220 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005221 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005222 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005223 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005224}
Dan Sandlerd5024042014-01-09 15:01:33 -05005225
5226interface DebugIntents {
5227 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5228 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005229}