blob: dfbef31bd13bd6459fd6e8b792e373fb39b3e82e [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070085import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080086import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070087import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070090import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070093import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700101
Sunny Goyal651077b2014-06-30 14:15:31 -0700102import com.android.launcher3.DropTarget.DragObject;
103import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700107import com.android.launcher3.compat.PackageInstallerCompat;
108import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100109import com.android.launcher3.compat.UserHandleCompat;
110import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
170 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800172 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: boolean
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
192 // Type: long
193 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
198 // Type: parcelable
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: int[]
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Adam Cohen432609a2014-03-13 17:03:22 -0700205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
207
Adam Cohen3ed316a2014-07-23 18:21:20 -0700208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
211
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100212 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700213 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100214 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700215 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100216 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217
Adam Cohen39a06042013-07-19 14:30:12 -0700218 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700219 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700224 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700225 private State mState = State.WORKSPACE;
226 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700228 private boolean mIsSafeModeEnabled;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700231 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
232 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700233 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800236 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700244 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700245 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragLayer mDragLayer;
257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Sunny Goyalffe83f12014-08-14 17:39:34 -0700260 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Michael Jurkac9d95c52011-08-29 14:03:34 -0700263 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800265 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700266
Michael Jurka0280c3b2010-09-17 15:00:07 -0700267 private int[] mTmpAddItemCellCoordinates = new int[2];
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private FolderInfo mFolderInfo;
270
Winson Chung3d503fb2011-07-13 17:25:49 -0700271 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800272 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273
Michael Jurka838a4ca2011-02-07 13:33:06 -0800274 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700275
Winson Chungc51db6a2011-10-05 11:44:49 -0700276 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 private AppsCustomizeTabHost mAppsCustomizeTabHost;
278 private AppsCustomizePagedView mAppsCustomizeContent;
279 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800280 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private boolean mWorkspaceLoading = true;
291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mUserPresent = true;
305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309 private static LocaleConfiguration sLocaleConfiguration = null;
310
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700311 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Michael Jurka4ef207b2010-11-29 17:05:45 -0800328 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700329 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
330 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700331
Craig Mautner360310b2012-10-26 15:13:08 -0700332 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700333
Adam Cohen1462de32012-07-24 22:34:36 -0700334 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700335 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700336
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700338 static Date sDateStamp = new Date();
339 static DateFormat sDateFormat =
340 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
341 static long sRunStart = System.currentTimeMillis();
342 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700343
Winson Chung46353de2012-02-16 14:05:10 -0800344 // We only want to get the SharedPreferences once since it does an FS stat each time we get
345 // it from the context.
346 private SharedPreferences mSharedPrefs;
347
Adam Cohene25af792013-06-06 23:08:25 -0700348 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
349
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 // Holds the page that we need to animate to, and the icon views that we need to animate up
351 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700352 private ImageView mFolderIconImageView;
353 private Bitmap mFolderIconBitmap;
354 private Canvas mFolderIconCanvas;
355 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700356
Michael Jurkaddd62e92011-02-16 17:49:14 -0800357 private BubbleTextView mWaitingForResume;
358
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 private Runnable mBuildLayersRunnable = new Runnable() {
360 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700361 if (mWorkspace != null) {
362 mWorkspace.buildPageHardwareLayers();
363 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 }
365 };
366
Adam Cohendb364c32014-05-20 14:23:40 -0700367 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368
Michael Jurka7267fa52013-09-26 15:29:57 -0700369 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800370
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 private static class PendingAddArguments {
372 int requestCode;
373 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700374 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700375 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 int cellX;
377 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700378 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 }
380
Daniel Sandlerff02d492013-08-05 02:12:05 -0400381 private Stats mStats;
382
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700383 FocusIndicatorView mFocusHandler;
384
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800385 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800386 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700387 }
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
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 Chung07e045c2013-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
Sunny Goyal2350bc92014-10-14 16:42:54 -07001437 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
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 Chung07e045c2013-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;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002593
2594 if (shortcut.isDisabled != 0) {
2595 int error = R.string.activity_not_available;
2596 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2597 error = R.string.safemode_shortcut_error;
2598 }
2599 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2600 return;
2601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 final Intent intent = shortcut.intent;
2604
2605 // Check for special shortcuts
2606 if (intent.getComponent() != null) {
2607 final String shortcutClass = intent.getComponent().getClassName();
2608
2609 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2610 MemoryDumpActivity.startDump(this);
2611 return;
2612 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2613 toggleShowWeightWatcher();
2614 return;
2615 }
2616 }
2617
Chris Wren40c5ed32014-06-24 18:24:23 -04002618 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002619 if ((v instanceof BubbleTextView)
2620 && shortcut.isPromise()
2621 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002623 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 new DialogInterface.OnClickListener() {
2625 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002626 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 return;
2630 }
2631
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002633 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 }
2635
Sunny Goyal508da152014-08-14 10:53:27 -07002636 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002638 final ShortcutInfo shortcut;
2639 final Intent intent;
2640 if (tag instanceof ShortcutInfo) {
2641 shortcut = (ShortcutInfo) tag;
2642 intent = shortcut.intent;
2643 int[] pos = new int[2];
2644 v.getLocationOnScreen(pos);
2645 intent.setSourceBounds(new Rect(pos[0], pos[1],
2646 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002647
Sunny Goyal508da152014-08-14 10:53:27 -07002648 } else if (tag instanceof AppInfo) {
2649 shortcut = null;
2650 intent = ((AppInfo) tag).intent;
2651 } else {
2652 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2653 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654
2655 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 mStats.recordLaunch(intent, shortcut);
2657
2658 if (success && v instanceof BubbleTextView) {
2659 mWaitingForResume = (BubbleTextView) v;
2660 mWaitingForResume.setStayPressed(true);
2661 }
2662 }
2663
2664 /**
2665 * Event handler for a folder icon click.
2666 *
2667 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2668 */
2669 protected void onClickFolderIcon(View v) {
2670 if (LOGD) Log.d(TAG, "onClickFolder");
2671 if (!(v instanceof FolderIcon)){
2672 throw new IllegalArgumentException("Input must be a FolderIcon");
2673 }
2674
2675 FolderIcon folderIcon = (FolderIcon) v;
2676 final FolderInfo info = folderIcon.getFolderInfo();
2677 Folder openFolder = mWorkspace.getFolderForTag(info);
2678
2679 // If the folder info reports that the associated folder is open, then verify that
2680 // it is actually opened. There have been a few instances where this gets out of sync.
2681 if (info.opened && openFolder == null) {
2682 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2683 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2684 info.opened = false;
2685 }
2686
2687 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2688 // Close any open folder
2689 closeFolder();
2690 // Open the requested folder
2691 openFolder(folderIcon);
2692 } else {
2693 // Find the open folder...
2694 int folderScreen;
2695 if (openFolder != null) {
2696 folderScreen = mWorkspace.getPageForView(openFolder);
2697 // .. and close it
2698 closeFolder(openFolder);
2699 if (folderScreen != mWorkspace.getCurrentPage()) {
2700 // Close any folder open on the current screen
2701 closeFolder();
2702 // Pull the folder onto this screen
2703 openFolder(folderIcon);
2704 }
2705 }
2706 }
Michael Jurka5130e402011-10-13 04:55:35 -07002707 }
2708
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002709 /**
2710 * Event handler for the (Add) Widgets button that appears after a long press
2711 * on the home screen.
2712 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002713 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002715 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2716 }
2717
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 /**
2719 * Event handler for the wallpaper picker button that appears after a long press
2720 * on the home screen.
2721 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002722 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2724 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2725 pickWallpaper.setComponent(getWallpaperPickerComponent());
2726 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2727 }
2728
2729 /**
2730 * Event handler for a click on the settings button that appears after a long press
2731 * on the home screen.
2732 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002733 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2735 }
2736
Michael Jurka5130e402011-10-13 04:55:35 -07002737 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002738 // Provide the same haptic feedback that the system offers for virtual keys.
2739 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002740 }
2741
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 public void performHapticFeedbackOnTouchDown(View v) {
2743 // Provide the same haptic feedback that the system offers for virtual keys.
2744 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2745 }
2746
2747 public View.OnTouchListener getHapticFeedbackTouchListener() {
2748 if (mHapticFeedbackTouchListener == null) {
2749 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2750 @Override
2751 public boolean onTouch(View v, MotionEvent event) {
2752 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2753 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2754 }
2755 return false;
2756 }
2757 };
2758 }
2759 return mHapticFeedbackTouchListener;
2760 }
2761
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002762 public void onDragStarted(View view) {}
2763
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002764 /**
2765 * Called when the user stops interacting with the launcher.
2766 * This implies that the user is now on the homescreen and is not doing housekeeping.
2767 */
2768 protected void onInteractionEnd() {}
2769
2770 /**
2771 * Called when the user starts interacting with the launcher.
2772 * The possible interactions are:
2773 * - open all apps
2774 * - reorder an app shortcut, or a widget
2775 * - open the overview mode.
2776 * This is a good time to stop doing things that only make sense
2777 * when the user is on the homescreen and not doing housekeeping.
2778 */
2779 protected void onInteractionBegin() {}
2780
Kenny Guyf07af7b2014-07-31 11:39:16 +01002781 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002782 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002783 try {
2784 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2785 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2786 launcherApps.showAppDetailsForProfile(componentName, user);
2787 } catch (SecurityException e) {
2788 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2789 Log.e(TAG, "Launcher does not have permission to launch settings");
2790 } catch (ActivityNotFoundException e) {
2791 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2792 Log.e(TAG, "Unable to launch settings");
2793 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002794 }
2795
Michael Jurka1e2f4652013-07-08 18:03:46 -07002796 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002797 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2798 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002799 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002800 // System applications cannot be installed. For now, show a toast explaining that.
2801 // We may give them the option of disabling apps this way.
2802 int messageId = R.string.uninstall_system_app_text;
2803 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002804 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002805 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002806 String packageName = componentName.getPackageName();
2807 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002808 Intent intent = new Intent(
2809 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002810 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2811 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002812 if (user != null) {
2813 user.addToIntent(intent, Intent.EXTRA_USER);
2814 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002815 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002816 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002817 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002818 }
2819
Michael Jurka86a720e2012-05-09 11:23:23 -07002820 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002821 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002823 // Only launch using the new animation if the shortcut has not opted out (this is a
2824 // private contract between launcher and may be ignored in the future).
2825 boolean useLaunchAnimation = (v != null) &&
2826 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002827 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2828 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002829
Kenny Guy1317e2d2014-05-08 18:52:50 +01002830 UserHandleCompat user = null;
2831 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2832 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2833 user = userManager.getUserForSerialNumber(serialNumber);
2834 }
2835
2836 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002837 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002838 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002839 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2840 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002841 optsBundle = opts.toBundle();
2842 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002843
2844 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2845 // Could be launching some bookkeeping activity
2846 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002847 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002848 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002849 launcherApps.startActivityForProfile(intent.getComponent(), user,
2850 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002851 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002852 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 } catch (SecurityException e) {
2854 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002855 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002857 "or use the exported attribute for this activity. "
2858 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002860 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002862
Michael Jurka86a720e2012-05-09 11:23:23 -07002863 boolean startActivitySafely(View v, Intent intent, Object tag) {
2864 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002865 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2866 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2867 return false;
2868 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002869 try {
2870 success = startActivity(v, intent, tag);
2871 } catch (ActivityNotFoundException e) {
2872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2873 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2874 }
2875 return success;
2876 }
2877
Adam Cohen268c4752012-06-06 17:47:33 -07002878 /**
2879 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2880 * in the DragLayer in the exact absolute location of the original FolderIcon.
2881 */
2882 private void copyFolderIconToImage(FolderIcon fi) {
2883 final int width = fi.getMeasuredWidth();
2884 final int height = fi.getMeasuredHeight();
2885
2886 // Lazy load ImageView, Bitmap and Canvas
2887 if (mFolderIconImageView == null) {
2888 mFolderIconImageView = new ImageView(this);
2889 }
2890 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2891 mFolderIconBitmap.getHeight() != height) {
2892 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2893 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2894 }
2895
2896 DragLayer.LayoutParams lp;
2897 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2898 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2899 } else {
2900 lp = new DragLayer.LayoutParams(width, height);
2901 }
2902
Adam Cohen307fe232012-08-16 17:55:58 -07002903 // The layout from which the folder is being opened may be scaled, adjust the starting
2904 // view size by this scale factor.
2905 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002906 lp.customPosition = true;
2907 lp.x = mRectForFolderAnimation.left;
2908 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002909 lp.width = (int) (scale * width);
2910 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002911
2912 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2913 fi.draw(mFolderIconCanvas);
2914 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002915 if (fi.getFolder() != null) {
2916 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2917 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002918 }
Adam Cohen268c4752012-06-06 17:47:33 -07002919 // Just in case this image view is still in the drag layer from a previous animation,
2920 // we remove it and re-add it.
2921 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2922 mDragLayer.removeView(mFolderIconImageView);
2923 }
2924 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002925 if (fi.getFolder() != null) {
2926 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002927 }
Adam Cohen268c4752012-06-06 17:47:33 -07002928 }
2929
Adam Cohen2801caf2011-05-13 20:57:39 -07002930 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002931 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002932 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2933 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2934 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2935
Adam Cohenc51934b2011-07-26 21:07:43 -07002936 FolderInfo info = (FolderInfo) fi.getTag();
2937 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2938 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002939 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2940 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002941 }
2942
Adam Cohen268c4752012-06-06 17:47:33 -07002943 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2944 copyFolderIconToImage(fi);
2945 fi.setVisibility(View.INVISIBLE);
2946
Michael Jurka2ecf9952012-06-18 12:52:28 -07002947 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002948 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002949 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002950 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2951 }
2952 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002953 oa.start();
2954 }
2955
Adam Cohen268c4752012-06-06 17:47:33 -07002956 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002957 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002958 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2959 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2960 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2961
Adam Cohen268c4752012-06-06 17:47:33 -07002962 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002963
Adam Cohen268c4752012-06-06 17:47:33 -07002964 // We remove and re-draw the FolderIcon in-case it has changed
2965 mDragLayer.removeView(mFolderIconImageView);
2966 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002967 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002968 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002969 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002970 oa.addListener(new AnimatorListenerAdapter() {
2971 @Override
2972 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002973 if (cl != null) {
2974 cl.clearFolderLeaveBehind();
2975 // Remove the ImageView copy of the FolderIcon and make the original visible.
2976 mDragLayer.removeView(mFolderIconImageView);
2977 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002978 }
2979 }
2980 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002981 oa.start();
2982 }
2983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002985 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986 * is animated relative to the specified View. If the View is null, no animation
2987 * is played.
2988 *
2989 * @param folderInfo The FolderInfo describing the folder to open.
2990 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002991 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002992 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002993 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994
Adam Cohena9cf38f2011-05-02 15:36:58 -07002995 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002996
Adam Cohen4554ee12011-08-03 16:13:21 -07002997 // Just verify that the folder hasn't already been added to the DragLayer.
2998 // There was a one-off crash where the folder had a parent already.
2999 if (folder.getParent() == null) {
3000 mDragLayer.addView(folder);
3001 mDragController.addDropTarget((DropTarget) folder);
3002 } else {
3003 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3004 folder.getParent() + ").");
3005 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003006 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003007 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003008
3009 // Notify the accessibility manager that this folder "window" has appeared and occluded
3010 // the workspace items
3011 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3012 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003013 }
3014
3015 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003016 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003017 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003018 if (folder.isEditingName()) {
3019 folder.dismissEditingName();
3020 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003021 closeFolder(folder);
3022 }
3023 }
3024
3025 void closeFolder(Folder folder) {
3026 folder.getInfo().opened = false;
3027
3028 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3029 if (parent != null) {
3030 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3031 shrinkAndFadeInFolderIcon(fi);
3032 }
3033 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003034
3035 // Notify the accessibility manager that this folder "window" has disappeard and no
3036 // longer occludeds the workspace items
3037 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003041 if (!isDraggingEnabled()) return false;
3042 if (isWorkspaceLocked()) return false;
3043 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044
Adam Cohen1697b792013-09-17 19:08:21 -07003045 if (v instanceof Workspace) {
3046 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003047 if (mWorkspace.enterOverviewMode()) {
3048 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3049 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3050 return true;
3051 } else {
3052 return false;
3053 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003054 } else {
3055 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003056 }
Adam Cohen1697b792013-09-17 19:08:21 -07003057 }
3058
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003059 CellLayout.CellInfo longClickCellInfo = null;
3060 View itemUnderLongClick = null;
3061 if (v.getTag() instanceof ItemInfo) {
3062 ItemInfo info = (ItemInfo) v.getTag();
3063 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3064 itemUnderLongClick = longClickCellInfo.cell;
3065 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067
Winson Chung3d503fb2011-07-13 17:25:49 -07003068 // The hotseat touch handling does not go through Workspace, and we always allow long press
3069 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003070 final boolean inHotseat = isHotseatLayout(v);
3071 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003072 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003073 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003074 // User long pressed on empty space
3075 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3076 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003077 if (mWorkspace.isInOverviewMode()) {
3078 mWorkspace.startReordering(v);
3079 } else {
3080 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003083 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3084 mHotseat.getOrderInHotseat(
3085 longClickCellInfo.cellX,
3086 longClickCellInfo.cellY));
3087 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 }
3091 }
3092 }
3093 return true;
3094 }
3095
Winson Chung3d503fb2011-07-13 17:25:49 -07003096 boolean isHotseatLayout(View layout) {
3097 return mHotseat != null && layout != null &&
3098 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3099 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003100
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 /**
3102 * Returns the CellLayout of the specified container at the specified screen.
3103 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003104 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003105 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3106 if (mHotseat != null) {
3107 return mHotseat.getLayout();
3108 } else {
3109 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003110 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003112 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003113 }
3114 }
3115
Daniel Sandler843e8602010-06-07 14:59:01 -04003116 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003117 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003118 }
3119
Craig Mautner360310b2012-10-26 15:13:08 -07003120 private void setWorkspaceBackground(boolean workspace) {
3121 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003122 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003123 }
3124
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003125 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003126 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3127 int curflags = getWindow().getAttributes().flags
3128 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3129 if (wpflags != curflags) {
3130 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3131 }
Craig Mautner360310b2012-10-26 15:13:08 -07003132 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003133 }
3134
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003135 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3136 if (v instanceof LauncherTransitionable) {
3137 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3138 }
3139 }
3140
Michael Jurkabed61d22012-02-14 22:51:29 -08003141 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3142 if (v instanceof LauncherTransitionable) {
3143 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3144 }
Winson Chung70442722012-02-10 15:43:22 -08003145
3146 // Update the workspace transition step as well
3147 dispatchOnLauncherTransitionStep(v, 0f);
3148 }
3149
3150 private void dispatchOnLauncherTransitionStep(View v, float t) {
3151 if (v instanceof LauncherTransitionable) {
3152 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3153 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003154 }
3155
3156 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3157 if (v instanceof LauncherTransitionable) {
3158 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3159 }
Winson Chung70442722012-02-10 15:43:22 -08003160
3161 // Update the workspace transition step as well
3162 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003163 }
3164
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003165 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003166 * Things to test when changing the following seven functions.
3167 * - Home from workspace
3168 * - from center screen
3169 * - from other screens
3170 * - Home from all apps
3171 * - from center screen
3172 * - from other screens
3173 * - Back from all apps
3174 * - from center screen
3175 * - from other screens
3176 * - Launch app from workspace and quit
3177 * - with back
3178 * - with home
3179 * - Launch app from all apps and quit
3180 * - with back
3181 * - with home
3182 * - Go to a screen that's not the default, then all
3183 * apps, and launch and app, and go back
3184 * - with back
3185 * -with home
3186 * - On workspace, long press power and go back
3187 * - with back
3188 * - with home
3189 * - On all apps, long press power and go back
3190 * - with back
3191 * - with home
3192 * - On workspace, power off
3193 * - On all apps, power off
3194 * - Launch an app and turn off the screen while in that app
3195 * - Go back with home key
3196 * - Go back with back key TODO: make this not go to workspace
3197 * - From all apps
3198 * - From workspace
3199 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3200 * - From all apps
3201 * - From the center workspace
3202 * - From another workspace
3203 */
3204
3205 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003206 * Zoom the camera out from the workspace to reveal 'toView'.
3207 * Assumes that the view to show is anchored at either the very top or very bottom
3208 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003209 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003210 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003211 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3212 showAppsCustomizeHelper(animated, springLoaded, contentType);
3213 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003214
Winson Chungc58497e2013-09-03 17:48:37 -07003215 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3216 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003217 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003218 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003219 mStateAnimation.cancel();
3220 mStateAnimation = null;
3221 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003222
Kenny Guyd794a3f2014-09-16 15:17:58 +01003223 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003224
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003225 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003226
Winson Chungf0ea4d32011-06-06 14:27:16 -07003227 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3228 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003229 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003230 final int itemsAlphaStagger =
3231 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003232
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003234 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003235 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003236
Adam Cohen2854d252014-08-27 16:04:07 -07003237 final ArrayList<View> layerViews = new ArrayList<View>();
3238
Adam Cohen6c5891a2014-07-09 23:53:15 -07003239 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3240 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003241 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003242 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003243 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003244 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3245 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003246 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3247 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003248
Adam Cohena38dc902014-09-07 17:48:55 +02003249 // If for some reason our views aren't initialized, don't animate
3250 boolean initialized = getAllAppsButton() != null;
3251
3252 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003253 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003254 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3255 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003256
Adam Cohen9bfdb762014-07-21 17:44:06 -07003257 final View page = content.getPageAt(content.getCurrentPage());
3258 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003259
Adam Cohen63f1ec02014-08-12 09:23:13 -07003260 final float initialPanelAlpha = 1f;
3261
3262 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3263 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003264 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3265 } else {
3266 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3267 }
3268
Adam Cohen9bfdb762014-07-21 17:44:06 -07003269 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003270 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003271 revealView.setVisibility(View.VISIBLE);
3272 // We need to hide this view as the animation start will be posted.
3273 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003274
Adam Cohen9bfdb762014-07-21 17:44:06 -07003275 int width = revealView.getMeasuredWidth();
3276 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003277 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003278
Adam Cohen63f1ec02014-08-12 09:23:13 -07003279 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003280 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003281
Adam Cohen63f1ec02014-08-12 09:23:13 -07003282 // Get the y delta between the center of the page and the center of the all apps button
3283 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3284 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003285
Adam Cohen2854d252014-08-27 16:04:07 -07003286 float alpha = 0;
3287 float xDrift = 0;
3288 float yDrift = 0;
3289 if (material) {
3290 alpha = isWidgetTray ? 0.3f : 1f;
3291 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3292 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3293 } else {
3294 yDrift = 2 * height / 3;
3295 xDrift = 0;
3296 }
3297 final float initAlpha = alpha;
3298
Adam Cohen9bfdb762014-07-21 17:44:06 -07003299 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003300 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003301 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003302 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003303 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003304 PropertyValuesHolder panelDriftX =
3305 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003306
Adam Cohen2854d252014-08-27 16:04:07 -07003307 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3308 panelAlpha, panelDriftY, panelDriftX);
3309
Adam Cohen9bfdb762014-07-21 17:44:06 -07003310 panelAlphaAndDrift.setDuration(revealDuration);
3311 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003312
Adam Cohen9bfdb762014-07-21 17:44:06 -07003313 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003314
Adam Cohen4e243a22014-08-10 18:30:55 -07003315 if (page != null) {
3316 page.setVisibility(View.VISIBLE);
3317 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003318 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003319
Adam Cohen2854d252014-08-27 16:04:07 -07003320 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3321 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003322 pageDrift.setDuration(revealDuration);
3323 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003324 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003325 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003326
Adam Cohen63f1ec02014-08-12 09:23:13 -07003327 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003328 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003329 itemsAlpha.setDuration(revealDuration);
3330 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3331 itemsAlpha.setStartDelay(itemsAlphaStagger);
3332 mStateAnimation.play(itemsAlpha);
3333 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334
Adam Cohen4e243a22014-08-10 18:30:55 -07003335 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3336 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003337 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003338 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003339 indicatorsAlpha.setDuration(revealDuration);
3340 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003341
Adam Cohen9bfdb762014-07-21 17:44:06 -07003342 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003343 final View allApps = getAllAppsButton();
3344 int allAppsButtonSize = LauncherAppState.getInstance().
3345 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3346 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003347 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003348 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003349 reveal.setDuration(revealDuration);
3350 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003351
3352 reveal.addListener(new AnimatorListenerAdapter() {
3353 public void onAnimationStart(Animator animation) {
3354 if (!isWidgetTray) {
3355 allApps.setVisibility(View.INVISIBLE);
3356 }
3357 }
3358 public void onAnimationEnd(Animator animation) {
3359 if (!isWidgetTray) {
3360 allApps.setVisibility(View.VISIBLE);
3361 }
3362 }
3363 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003364 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003365 }
Michael Jurka1899a362011-11-03 13:50:45 -07003366
Adam Cohen9bfdb762014-07-21 17:44:06 -07003367 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3368 @Override
3369 public void onAnimationEnd(Animator animation) {
3370 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3371 dispatchOnLauncherTransitionEnd(toView, animated, false);
3372
3373 revealView.setVisibility(View.INVISIBLE);
3374 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003375 if (page != null) {
3376 page.setLayerType(View.LAYER_TYPE_NONE, null);
3377 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 content.setPageBackgroundsVisible(true);
3379
3380 // Hide the search bar
3381 if (mSearchDropTargetBar != null) {
3382 mSearchDropTargetBar.hideSearchBar(false);
3383 }
3384 }
3385
Adam Cohen9bfdb762014-07-21 17:44:06 -07003386 });
3387
Adam Cohen6c5891a2014-07-09 23:53:15 -07003388 if (workspaceAnim != null) {
3389 mStateAnimation.play(workspaceAnim);
3390 }
Adam Cohen2854d252014-08-27 16:04:07 -07003391
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003392 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3393 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003394 final AnimatorSet stateAnimation = mStateAnimation;
3395 final Runnable startAnimRunnable = new Runnable() {
3396 public void run() {
3397 // Check that mStateAnimation hasn't changed while
3398 // we waited for a layout/draw pass
3399 if (mStateAnimation != stateAnimation)
3400 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003401 dispatchOnLauncherTransitionStart(fromView, animated, false);
3402 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003403
3404 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003405 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003406 for (int i = 0; i < layerViews.size(); i++) {
3407 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003408 if (v != null) {
3409 boolean attached = true;
3410 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3411 attached = v.isAttachedToWindow();
3412 }
3413 if (attached) v.buildLayer();
3414 }
Adam Cohen2854d252014-08-27 16:04:07 -07003415 }
3416 }
3417 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003418 }
3419 };
Adam Cohen2854d252014-08-27 16:04:07 -07003420 toView.bringToFront();
3421 toView.setVisibility(View.VISIBLE);
3422 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003423 } else {
3424 toView.setTranslationX(0.0f);
3425 toView.setTranslationY(0.0f);
3426 toView.setScaleX(1.0f);
3427 toView.setScaleY(1.0f);
3428 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003429 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003430
Daniel Sandlere4f98912013-06-25 15:13:26 -04003431 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003432 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003433 if (mSearchDropTargetBar != null) {
3434 mSearchDropTargetBar.hideSearchBar(false);
3435 }
Michael Jurkaabded662011-03-04 12:06:57 -08003436 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003437 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003438 dispatchOnLauncherTransitionStart(fromView, animated, false);
3439 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003440 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003441 dispatchOnLauncherTransitionStart(toView, animated, false);
3442 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003443 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003444 }
3445
3446 /**
3447 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003449 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003450 */
Adam Cohened307df2013-10-02 09:37:31 -07003451 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003452 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003453
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003455 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456 mStateAnimation.cancel();
3457 mStateAnimation = null;
3458 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003459
Kenny Guyd794a3f2014-09-16 15:17:58 +01003460 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003461 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003462
Winson Chungf0ea4d32011-06-06 14:27:16 -07003463 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003464 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003465 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003466 final int itemsAlphaStagger =
3467 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003468
Winson Chungf0ea4d32011-06-06 14:27:16 -07003469 final float scaleFactor = (float)
3470 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3471 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003472 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003473 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003474 final ArrayList<View> layerViews = new ArrayList<View>();
3475
Adam Cohened307df2013-10-02 09:37:31 -07003476 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003477 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003478 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003479 } else if (toState == Workspace.State.SPRING_LOADED ||
3480 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003481 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003482 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003483 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003484
Adam Cohena38dc902014-09-07 17:48:55 +02003485 // If for some reason our views aren't initialized, don't animate
3486 boolean initialized = getAllAppsButton() != null;
3487
3488 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003489 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003490 if (workspaceAnim != null) {
3491 mStateAnimation.play(workspaceAnim);
3492 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003493
Adam Cohen9bfdb762014-07-21 17:44:06 -07003494 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3495 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003496
Adam Cohen9bfdb762014-07-21 17:44:06 -07003497 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003498
3499 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3500 int count = content.getChildCount();
3501 for (int i = 0; i < count; i++) {
3502 View child = content.getChildAt(i);
3503 if (child != page) {
3504 child.setVisibility(View.INVISIBLE);
3505 }
3506 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003507 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003508
Adam Cohen2854d252014-08-27 16:04:07 -07003509 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3510 // don't perform all these no-op animations. In particularly, this was causing
3511 // the all-apps button to pop in and out.
3512 if (fromView.getVisibility() == View.VISIBLE) {
3513 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3514 final boolean isWidgetTray =
3515 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003516
Adam Cohen2854d252014-08-27 16:04:07 -07003517 if (isWidgetTray) {
3518 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3519 } else {
3520 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003521 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003522
Adam Cohen2854d252014-08-27 16:04:07 -07003523 int width = revealView.getMeasuredWidth();
3524 int height = revealView.getMeasuredHeight();
3525 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3526
3527 // Hide the real page background, and swap in the fake one
3528 revealView.setVisibility(View.VISIBLE);
3529 content.setPageBackgroundsVisible(false);
3530
3531 final View allAppsButton = getAllAppsButton();
3532 revealView.setTranslationY(0);
3533 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3534 allAppsButton, null);
3535
3536 float xDrift = 0;
3537 float yDrift = 0;
3538 if (material) {
3539 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3540 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3541 } else {
3542 yDrift = 5 * height / 4;
3543 xDrift = 0;
3544 }
3545
3546 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3547 TimeInterpolator decelerateInterpolator = material ?
3548 new LogDecelerateInterpolator(100, 0) :
3549 new LogDecelerateInterpolator(30, 0);
3550
3551 // The vertical motion of the apps panel should be delayed by one frame
3552 // from the conceal animation in order to give the right feel. We correpsondingly
3553 // shorten the duration so that the slide and conceal end at the same time.
3554 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3555 0, yDrift);
3556 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3557 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3558 panelDriftY.setInterpolator(decelerateInterpolator);
3559 mStateAnimation.play(panelDriftY);
3560
3561 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3562 0, xDrift);
3563 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3564 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3565 panelDriftX.setInterpolator(decelerateInterpolator);
3566 mStateAnimation.play(panelDriftX);
3567
3568 if (isWidgetTray || !material) {
3569 float finalAlpha = material ? 0.4f : 0f;
3570 revealView.setAlpha(1f);
3571 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3572 1f, finalAlpha);
3573 panelAlpha.setDuration(revealDuration);
3574 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3575 new AccelerateInterpolator(1.5f));
3576 mStateAnimation.play(panelAlpha);
3577 }
3578
3579 if (page != null) {
3580 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3581
3582 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3583 0, yDrift);
3584 page.setTranslationY(0);
3585 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3586 pageDrift.setInterpolator(decelerateInterpolator);
3587 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3588 mStateAnimation.play(pageDrift);
3589
3590 page.setAlpha(1f);
3591 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3592 itemsAlpha.setDuration(100);
3593 itemsAlpha.setInterpolator(decelerateInterpolator);
3594 mStateAnimation.play(itemsAlpha);
3595 }
3596
3597 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3598 pageIndicators.setAlpha(1f);
3599 ObjectAnimator indicatorsAlpha =
3600 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3601 indicatorsAlpha.setDuration(revealDuration);
3602 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3603 mStateAnimation.play(indicatorsAlpha);
3604
3605 width = revealView.getMeasuredWidth();
3606
3607 if (material) {
3608 if (!isWidgetTray) {
3609 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003610 }
Adam Cohen2854d252014-08-27 16:04:07 -07003611 int allAppsButtonSize = LauncherAppState.getInstance().
3612 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3613 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3614 Animator reveal =
3615 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3616 height / 2, revealRadius, finalRadius);
3617 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3618 reveal.setDuration(revealDuration);
3619 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003620
Adam Cohen2854d252014-08-27 16:04:07 -07003621 reveal.addListener(new AnimatorListenerAdapter() {
3622 public void onAnimationEnd(Animator animation) {
3623 revealView.setVisibility(View.INVISIBLE);
3624 if (!isWidgetTray) {
3625 allAppsButton.setVisibility(View.VISIBLE);
3626 }
3627 }
3628 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003629
Adam Cohen2854d252014-08-27 16:04:07 -07003630 mStateAnimation.play(reveal);
3631 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003632
Adam Cohen2854d252014-08-27 16:04:07 -07003633 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3634 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3635 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003636 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003637
3638 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003639 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003640 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003641 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003642 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3643 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003644 if (onCompleteRunnable != null) {
3645 onCompleteRunnable.run();
3646 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003647
3648 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003649 if (page != null) {
3650 page.setLayerType(View.LAYER_TYPE_NONE, null);
3651 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003652 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003653 // Unhide side pages
3654 int count = content.getChildCount();
3655 for (int i = 0; i < count; i++) {
3656 View child = content.getChildAt(i);
3657 child.setVisibility(View.VISIBLE);
3658 }
3659
3660 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003661 if (page != null) {
3662 page.setTranslationX(0);
3663 page.setTranslationY(0);
3664 page.setAlpha(1);
3665 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003666 content.setCurrentPage(content.getNextPage());
3667
Chet Haasebc2f0822012-10-26 17:59:53 -07003668 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003669 }
3670 });
3671
Adam Cohen2854d252014-08-27 16:04:07 -07003672 final AnimatorSet stateAnimation = mStateAnimation;
3673 final Runnable startAnimRunnable = new Runnable() {
3674 public void run() {
3675 // Check that mStateAnimation hasn't changed while
3676 // we waited for a layout/draw pass
3677 if (mStateAnimation != stateAnimation)
3678 return;
3679 dispatchOnLauncherTransitionStart(fromView, animated, false);
3680 dispatchOnLauncherTransitionStart(toView, animated, false);
3681
Kenny Guyd794a3f2014-09-16 15:17:58 +01003682 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003683 for (int i = 0; i < layerViews.size(); i++) {
3684 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003685 if (v != null) {
3686 boolean attached = true;
3687 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3688 attached = v.isAttachedToWindow();
3689 }
3690 if (attached) v.buildLayer();
3691 }
Adam Cohen2854d252014-08-27 16:04:07 -07003692 }
3693 }
3694 mStateAnimation.start();
3695 }
3696 };
3697 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003698 } else {
3699 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003700 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003701 dispatchOnLauncherTransitionStart(fromView, animated, true);
3702 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003703 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003704 dispatchOnLauncherTransitionStart(toView, animated, true);
3705 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003706 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003707 }
3708
Michael Jurkae326f182011-11-21 14:05:46 -08003709 @Override
3710 public void onTrimMemory(int level) {
3711 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003712 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003713 mAppsCustomizeTabHost.onTrimMemory();
3714 }
3715 }
3716
Adam Cohened307df2013-10-02 09:37:31 -07003717 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003718 showWorkspace(animated, null);
3719 }
3720
Adam Cohened307df2013-10-02 09:37:31 -07003721 protected void showWorkspace() {
3722 showWorkspace(true);
3723 }
3724
Adam Cohened66b2b2012-01-23 17:28:51 -08003725 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003726 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003727 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003728 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003729 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003730
Winson Chungc7d2b602012-05-16 17:02:20 -07003731 // Show the search bar (only animate if we were showing the drop target bar in spring
3732 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003733 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003734 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003735 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003736
Michael Jurkab3e22d92011-10-31 15:58:33 -07003737 // Set focus to the AppsCustomize button
3738 if (mAllAppsButton != null) {
3739 mAllAppsButton.requestFocus();
3740 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003741 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003743 // Change the state *after* we've called all the transition code
3744 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003745
Winson Chung5fb63472011-02-02 17:03:37 -08003746 // Resume the auto-advance of widgets
3747 mUserPresent = true;
3748 updateRunning();
3749
alanv1d4fde62012-10-17 13:15:47 -07003750 // Send an accessibility event to announce the context change
3751 getWindow().getDecorView()
3752 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003753
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003754 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003755 }
3756
Adam Cohened307df2013-10-02 09:37:31 -07003757 void showOverviewMode(boolean animated) {
3758 mWorkspace.setVisibility(View.VISIBLE);
3759 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3760 mState = State.WORKSPACE;
3761 onWorkspaceShown(animated);
3762 }
3763
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003764 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003765 }
3766
Winson Chung82963d52013-10-09 11:20:57 -07003767 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3768 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003769 if (mState != State.WORKSPACE) return;
3770
Winson Chung82963d52013-10-09 11:20:57 -07003771 if (resetPageToZero) {
3772 mAppsCustomizeTabHost.reset();
3773 }
Winson Chungc58497e2013-09-03 17:48:37 -07003774 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003775 mAppsCustomizeTabHost.post(new Runnable() {
3776 @Override
3777 public void run() {
3778 // We post this in-case the all apps view isn't yet constructed.
3779 mAppsCustomizeTabHost.requestFocus();
3780 }
3781 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003782
Michael Jurkab3e22d92011-10-31 15:58:33 -07003783 // Change the state *after* we've called all the transition code
3784 mState = State.APPS_CUSTOMIZE;
3785
3786 // Pause the auto-advance of widgets until we are out of AllApps
3787 mUserPresent = false;
3788 updateRunning();
3789 closeFolder();
3790
3791 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003792 getWindow().getDecorView()
3793 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003794 }
3795
Adam Cohen7777d962011-08-18 18:58:38 -07003796 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003797 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003798 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003799 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003800 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003801 }
Adam Cohen7777d962011-08-18 18:58:38 -07003802
Adam Cohenad4e15c2013-10-17 16:21:35 -07003803 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003804 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003805 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3806
Winson Chunge7a03942011-08-05 15:05:12 -07003807 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003808 @Override
3809 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003810 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003811 // Before we show workspace, hide all apps again because
3812 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3813 // clean up our state transition functions
3814 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003815 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003816 } else {
3817 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003818 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003819 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003820 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003821 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003822
Michael Jurkad3ef3062010-11-23 16:23:58 -08003823 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003824 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003825 final boolean animated = true;
3826 final boolean springLoaded = true;
3827 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003828 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003829 }
3830 // Otherwise, we are not in spring loaded mode, so don't do anything.
3831 }
3832
Michael Jurkab3e22d92011-10-31 15:58:33 -07003833 void lockAllApps() {
3834 // TODO
3835 }
3836
3837 void unlockAllApps() {
3838 // TODO
3839 }
3840
Winson Chungf0ea4d32011-06-06 14:27:16 -07003841 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003842 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003843 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003844 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003845 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003847 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003848 int duration = 0;
3849 if (mSearchDropTargetBar != null) {
3850 duration = mSearchDropTargetBar.getTransitionOutDuration();
3851 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003852 mHotseat.animate().alpha(0f).setDuration(duration);
3853 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003854 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003855 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003856 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003857 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003858 }
3859
Patrick Dubroy5f445422011-02-18 14:35:21 -08003860 /**
3861 * Add an item from all apps or customize onto the given workspace screen.
3862 * If layout is null, add to the current screen.
3863 */
3864 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003865 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003866 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003867 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003868 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003869
Winson Chungdff8ebb2011-09-08 17:25:31 -07003870 /** Maps the current orientation to an index for referencing orientation correct global icons */
3871 private int getCurrentOrientationIndexForGlobalIcons() {
3872 // default - 0, landscape - 1
3873 switch (getResources().getConfiguration().orientation) {
3874 case Configuration.ORIENTATION_LANDSCAPE:
3875 return 1;
3876 default:
3877 return 0;
3878 }
3879 }
3880
Michael Jurkaae65ee32012-04-30 11:45:54 -07003881 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003882 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003883 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003884 // Look for the toolbar icon specified in the activity meta-data
3885 Bundle metaData = packageManager.getActivityInfo(
3886 activityName, PackageManager.GET_META_DATA).metaData;
3887 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003888 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003889 if (iconResId != 0) {
3890 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003891 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003892 }
3893 }
3894 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003895 // This can happen if the activity defines an invalid drawable
3896 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3897 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003898 } catch (Resources.NotFoundException nfe) {
3899 // This can happen if the activity defines an invalid drawable
3900 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3901 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003902 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003903 return null;
3904 }
3905
3906 // if successful in getting icon, return it; otherwise, set button to use default drawable
3907 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003908 int buttonId, ComponentName activityName, int fallbackDrawableId,
3909 String toolbarResourceName) {
3910 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003911 Resources r = getResources();
3912 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3913 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003914
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003915 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003916 // If we were unable to find the icon via the meta-data, use a generic one
3917 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003918 toolbarIcon = r.getDrawable(fallbackDrawableId);
3919 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003920 if (button != null) {
3921 button.setCompoundDrawables(toolbarIcon, null, null, null);
3922 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003923 return null;
3924 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003925 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003926 if (button != null) {
3927 button.setCompoundDrawables(toolbarIcon, null, null, null);
3928 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003929 return toolbarIcon.getConstantState();
3930 }
3931 }
3932
3933 // if successful in getting icon, return it; otherwise, set button to use default drawable
3934 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003935 int buttonId, ComponentName activityName, int fallbackDrawableId,
3936 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003937 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003938 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003939
Michael Jurka19e0fc52011-07-22 18:00:21 -07003940 if (button != null) {
3941 // If we were unable to find the icon via the meta-data, use a
3942 // generic one
3943 if (toolbarIcon == null) {
3944 button.setImageResource(fallbackDrawableId);
3945 } else {
3946 button.setImageDrawable(toolbarIcon);
3947 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003948 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003949
3950 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3951
Michael Jurka0423dcf2010-10-05 14:56:18 -07003952 }
3953
Winson Chung1b884092012-06-08 17:13:02 -07003954 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003955 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003956 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003957 }
3958
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003959 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003960 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003961 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003962 }
3963
Winson Chungbb185bd2011-11-21 12:31:42 -08003964 private void invalidatePressedFocusedStates(View container, View button) {
3965 if (container instanceof HolographicLinearLayout) {
3966 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3967 layout.invalidatePressedFocusedStates();
3968 } else if (button instanceof HolographicImageView) {
3969 HolographicImageView view = (HolographicImageView) button;
3970 view.invalidatePressedFocusedStates();
3971 }
3972 }
3973
Cristina Stancu476493b2013-08-07 17:20:14 +01003974 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003975 if (mQsb == null) {
3976 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3977 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003978 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003979 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003980 }
3981
3982 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003983 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003984 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003985 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003986 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003987
Winson Chung1cad91e2011-05-25 17:41:01 -07003988 final SearchManager searchManager =
3989 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3990 ComponentName activityName = searchManager.getGlobalSearchActivity();
3991 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003992 int coi = getCurrentOrientationIndexForGlobalIcons();
3993 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003994 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3995 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3996 if (sGlobalSearchIcon[coi] == null) {
3997 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3998 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3999 TOOLBAR_ICON_METADATA_NAME);
4000 }
4001
Winson Chungc51db6a2011-10-05 11:44:49 -07004002 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4003 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004004 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004005 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004006 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004007 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004008 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4009 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004010 if (searchButton != null) searchButton.setVisibility(View.GONE);
4011 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004012 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004013 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004014 }
4015 }
4016
Cristina Stancu476493b2013-08-07 17:20:14 +01004017 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004018 final View searchButtonContainer = findViewById(R.id.search_button_container);
4019 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004020 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004021 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004022 }
4023
Cristina Stancu476493b2013-08-07 17:20:14 +01004024 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004025 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004026 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004027
Winson Chungc51db6a2011-10-05 11:44:49 -07004028 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004029 final SearchManager searchManager =
4030 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4031 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4032
4033 ComponentName activityName = null;
4034 if (globalSearchActivity != null) {
4035 // Check if the global search activity handles voice search
4036 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4037 intent.setPackage(globalSearchActivity.getPackageName());
4038 activityName = intent.resolveActivity(getPackageManager());
4039 }
4040
4041 if (activityName == null) {
4042 // Fallback: check if an activity other than the global search activity
4043 // resolves this
4044 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4045 activityName = intent.resolveActivity(getPackageManager());
4046 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004047 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004048 int coi = getCurrentOrientationIndexForGlobalIcons();
4049 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004050 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4051 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4052 if (sVoiceSearchIcon[coi] == null) {
4053 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4054 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4055 TOOLBAR_ICON_METADATA_NAME);
4056 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004057 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004058 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004059 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004060 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004061 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004062 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004063 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004064 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004065 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004066 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004067 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004068 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004069
Cristina Stancu476493b2013-08-07 17:20:14 +01004070 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004071 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4072 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004073 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004074 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004075 }
4076
Winson Chung5841efa2013-09-30 18:06:44 -07004077 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004078 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4079 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004080 boolean visible = !forceDisableVoiceButtonProxy &&
4081 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004082 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004083 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004084 }
4085 }
Winson Chung5841efa2013-09-30 18:06:44 -07004086
4087 /**
4088 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4089 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4090 */
4091 public void disableVoiceButtonProxy(boolean disabled) {
4092 updateVoiceButtonProxyVisible(disabled);
4093 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004094
Michael Jurkad7c28052012-04-27 15:43:36 -07004095 @Override
4096 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004097 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004098 final List<CharSequence> text = event.getText();
4099 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004100 // Populate event with a fake title based on the current state.
4101 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004102 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004103 } else {
4104 text.add(getString(R.string.all_apps_home_button_label));
4105 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004106 return result;
4107 }
4108
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004109 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004110 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004111 */
4112 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4113 @Override
4114 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004115 closeSystemDialogs();
4116 }
4117 }
4118
4119 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004120 * Receives notifications whenever the appwidgets are reset.
4121 */
4122 private class AppWidgetResetObserver extends ContentObserver {
4123 public AppWidgetResetObserver() {
4124 super(new Handler());
4125 }
4126
4127 @Override
4128 public void onChange(boolean selfChange) {
4129 onAppWidgetReset();
4130 }
4131 }
4132
4133 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004134 * If the activity is currently paused, signal that we need to run the passed Runnable
4135 * in onResume.
4136 *
4137 * This needs to be called from incoming places where resources might have been loaded
4138 * while we are paused. That is becaues the Configuration might be wrong
4139 * when we're not running, and if it comes back to what it was when we
4140 * were paused, we are not restarted.
4141 *
4142 * Implementation of the method from LauncherModel.Callbacks.
4143 *
4144 * @return true if we are currently paused. The caller might be able to
4145 * skip some work in that case since we will come back again.
4146 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004147 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004148 if (mPaused) {
4149 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004150 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004151 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004152 }
4153 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004154 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004155 return true;
4156 } else {
4157 return false;
4158 }
4159 }
4160
Michael Jurkac402cd92013-05-20 15:49:32 +02004161 private boolean waitUntilResume(Runnable run) {
4162 return waitUntilResume(run, false);
4163 }
4164
Michael Jurka1e2f4652013-07-08 18:03:46 -07004165 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004166 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004167 }
4168
Michael Jurka7607c2f2013-04-03 14:33:19 -07004169 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004170 * If the activity is currently paused, signal that we need to re-run the loader
4171 * in onResume.
4172 *
4173 * This needs to be called from incoming places where resources might have been loaded
4174 * while we are paused. That is becaues the Configuration might be wrong
4175 * when we're not running, and if it comes back to what it was when we
4176 * were paused, we are not restarted.
4177 *
4178 * Implementation of the method from LauncherModel.Callbacks.
4179 *
4180 * @return true if we are currently paused. The caller might be able to
4181 * skip some work in that case since we will come back again.
4182 */
4183 public boolean setLoadOnResume() {
4184 if (mPaused) {
4185 Log.i(TAG, "setLoadOnResume");
4186 mOnResumeNeedsLoad = true;
4187 return true;
4188 } else {
4189 return false;
4190 }
4191 }
4192
4193 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004195 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004197 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004198 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004199 } else {
4200 return SCREEN_COUNT / 2;
4201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004203
Joe Onorato9c1289c2009-08-17 11:03:03 -04004204 /**
4205 * Refreshes the shortcuts shown on the workspace.
4206 *
4207 * Implementation of the method from LauncherModel.Callbacks.
4208 */
4209 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004210 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004211
Michael Jurka7607c2f2013-04-03 14:33:19 -07004212 // If we're starting binding all over again, clear any bind calls we'd postponed in
4213 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4214 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004215 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004216
Winson Chungd64d1762013-08-20 14:37:16 -07004217 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004218 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004219 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004220
Michael Jurka05bf6442011-11-30 20:28:53 -08004221 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004222 if (mHotseat != null) {
4223 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004224 }
4225 }
4226
Adam Cohendcd297f2013-06-18 13:13:40 -07004227 @Override
4228 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004229 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004230
Adam Cohen5084cba2013-09-03 12:01:16 -07004231 // If there are no screens, we need to have an empty screen
4232 if (orderedScreenIds.size() == 0) {
4233 mWorkspace.addExtraEmptyScreen();
4234 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004235
4236 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004237 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004238 if (hasCustomContentToLeft()) {
4239 mWorkspace.createCustomContentContainer();
4240 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004241 }
Winson Chung64359a52013-07-08 17:17:08 -07004242 }
4243
4244 @Override
4245 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004246 // Log to disk
4247 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4248 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4249 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004250 int count = orderedScreenIds.size();
4251 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004252 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004253 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004254 }
4255
Adam Cohen39a06042013-07-19 14:30:12 -07004256 private boolean shouldShowWeightWatcher() {
4257 String spKey = LauncherAppState.getSharedPreferencesKey();
4258 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004259 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004260
4261 return show;
4262 }
4263
4264 private void toggleShowWeightWatcher() {
4265 String spKey = LauncherAppState.getSharedPreferencesKey();
4266 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4267 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4268
4269 show = !show;
4270
4271 SharedPreferences.Editor editor = sp.edit();
4272 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4273 editor.commit();
4274
4275 if (mWeightWatcher != null) {
4276 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4277 }
4278 }
4279
Winson Chungd64d1762013-08-20 14:37:16 -07004280 public void bindAppsAdded(final ArrayList<Long> newScreens,
4281 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004282 final ArrayList<ItemInfo> addAnimated,
4283 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004284 Runnable r = new Runnable() {
4285 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004286 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004287 }
4288 };
4289 if (waitUntilResume(r)) {
4290 return;
4291 }
4292
Winson Chungd64d1762013-08-20 14:37:16 -07004293 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004294 if (newScreens != null) {
4295 bindAddScreens(newScreens);
4296 }
Winson Chungd64d1762013-08-20 14:37:16 -07004297
4298 // We add the items without animation on non-visible pages, and with
4299 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004300 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004301 bindItems(addNotAnimated, 0,
4302 addNotAnimated.size(), false);
4303 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004304 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004305 bindItems(addAnimated, 0,
4306 addAnimated.size(), true);
4307 }
Winson Chungc58497e2013-09-03 17:48:37 -07004308
Winson Chung87412982013-10-03 18:34:14 -07004309 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004310 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004311
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004312 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004313 addedApps != null && mAppsCustomizeContent != null) {
4314 mAppsCustomizeContent.addApps(addedApps);
4315 }
Winson Chungd64d1762013-08-20 14:37:16 -07004316 }
4317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004318 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004319 * Bind the items start-end from the list.
4320 *
4321 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004322 */
Winson Chung64359a52013-07-08 17:17:08 -07004323 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4324 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004325 Runnable r = new Runnable() {
4326 public void run() {
4327 bindItems(shortcuts, start, end, forceAnimateIcons);
4328 }
4329 };
4330 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004331 return;
4332 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004333
Winson Chungf0c6ae02012-03-21 16:10:31 -07004334 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004335 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4336 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004337 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004338 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004339 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004340 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004341 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004342
4343 // Short circuit if we are loading dock items for a configuration which has no dock
4344 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4345 mHotseat == null) {
4346 continue;
4347 }
4348
Joe Onorato9c1289c2009-08-17 11:03:03 -04004349 switch (item.itemType) {
4350 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4351 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004352 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004353 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004354
Winson Chung64359a52013-07-08 17:17:08 -07004355 /*
4356 * TODO: FIX collision case
4357 */
Winson Chungce376632013-07-11 16:12:41 -07004358 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4359 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4360 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004361 View v = cl.getChildAt(item.cellX, item.cellY);
4362 Object tag = v.getTag();
4363 String desc = "Collision while binding workspace item: " + item
4364 + ". Collides with " + tag;
4365 if (LauncherAppState.isDogfoodBuild()) {
4366 throw (new RuntimeException(desc));
4367 } else {
4368 Log.d(TAG, desc);
4369 }
Winson Chungce376632013-07-11 16:12:41 -07004370 }
Winson Chung64359a52013-07-08 17:17:08 -07004371 }
4372
Adam Cohendcd297f2013-06-18 13:13:40 -07004373 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4374 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004375 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004376 // Animate all the applications up now
4377 shortcut.setAlpha(0f);
4378 shortcut.setScaleX(0f);
4379 shortcut.setScaleY(0f);
4380 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004381 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004383 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004384 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004385 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004386 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004387 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004388 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4389 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004390 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004391 default:
4392 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004394 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004395
Winson Chung997a9232013-07-24 15:33:46 -07004396 if (animateIcons) {
4397 // Animate to the correct page
4398 if (newShortcutsScreenId > -1) {
4399 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004400 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004401 final Runnable startBounceAnimRunnable = new Runnable() {
4402 public void run() {
4403 anim.playTogether(bounceAnims);
4404 anim.start();
4405 }
4406 };
Winson Chung997a9232013-07-24 15:33:46 -07004407 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004408 // We post the animation slightly delayed to prevent slowdowns
4409 // when we are loading right after we return to launcher.
4410 mWorkspace.postDelayed(new Runnable() {
4411 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004412 if (mWorkspace != null) {
4413 mWorkspace.snapToPage(newScreenIndex);
4414 mWorkspace.postDelayed(startBounceAnimRunnable,
4415 NEW_APPS_ANIMATION_DELAY);
4416 }
Winson Chung94d67682013-09-25 16:29:40 -07004417 }
4418 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004419 } else {
4420 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004421 }
4422 }
Winson Chung64359a52013-07-08 17:17:08 -07004423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004424 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004425 }
4426
Joe Onorato9c1289c2009-08-17 11:03:03 -04004427 /**
4428 * Implementation of the method from LauncherModel.Callbacks.
4429 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004430 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004431 Runnable r = new Runnable() {
4432 public void run() {
4433 bindFolders(folders);
4434 }
4435 };
4436 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004437 return;
4438 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004439 sFolders.clear();
4440 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004442
4443 /**
4444 * Add the views for a widget to the workspace.
4445 *
4446 * Implementation of the method from LauncherModel.Callbacks.
4447 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004448 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004449 Runnable r = new Runnable() {
4450 public void run() {
4451 bindAppWidget(item);
4452 }
4453 };
4454 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004455 return;
4456 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004457
Daniel Sandler843e8602010-06-07 14:59:01 -04004458 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4459 if (DEBUG_WIDGETS) {
4460 Log.d(TAG, "bindAppWidget: " + item);
4461 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004462 final Workspace workspace = mWorkspace;
4463
Sunny Goyalff572272014-07-23 13:58:07 -07004464 AppWidgetProviderInfo appWidgetInfo;
4465 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4466 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4467
4468 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4469 if (appWidgetInfo == null) {
4470 if (DEBUG_WIDGETS) {
4471 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4472 + " belongs to component " + item.providerName
4473 + ", as the povider is null");
4474 }
4475 LauncherModel.deleteItemFromDatabase(this, item);
4476 return;
4477 }
4478 // Note: This assumes that the id remap broadcast is received before this step.
4479 // If that is not the case, the id remap will be ignored and user may see the
4480 // click to setup view.
4481 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4482 pendingInfo.spanX = item.spanX;
4483 pendingInfo.spanY = item.spanY;
4484 pendingInfo.minSpanX = item.minSpanX;
4485 pendingInfo.minSpanY = item.minSpanY;
4486 Bundle options =
4487 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4488
Sunny Goyalff572272014-07-23 13:58:07 -07004489 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004490 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4491 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004492
4493 // TODO consider showing a permission dialog when the widget is clicked.
4494 if (!success) {
4495 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4496 if (DEBUG_WIDGETS) {
4497 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4498 + " belongs to component " + item.providerName
4499 + ", as the launcher is unable to bing a new widget id");
4500 }
4501 LauncherModel.deleteItemFromDatabase(this, item);
4502 return;
4503 }
4504
4505 item.appWidgetId = newWidgetId;
4506
4507 // If the widget has a configure activity, it is still needs to set it up, otherwise
4508 // the widget is ready to go.
4509 item.restoreStatus = (appWidgetInfo.configure == null)
4510 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4511 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4512
4513 LauncherModel.updateItemInDatabase(this, item);
4514 }
4515
Sunny Goyal651077b2014-06-30 14:15:31 -07004516 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4517 final int appWidgetId = item.appWidgetId;
4518 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4519 if (DEBUG_WIDGETS) {
4520 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4521 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004522
Sunny Goyal651077b2014-06-30 14:15:31 -07004523 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4524 } else {
4525 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004526 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4527 view.updateIcon(mIconCache);
4528 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004529 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004530 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004532
Joe Onorato9c1289c2009-08-17 11:03:03 -04004533 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004534 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004535
Adam Cohendcd297f2013-06-18 13:13:40 -07004536 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004537 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004538 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4539
Joe Onorato9c1289c2009-08-17 11:03:03 -04004540 workspace.requestLayout();
4541
Daniel Sandler843e8602010-06-07 14:59:01 -04004542 if (DEBUG_WIDGETS) {
4543 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4544 + (SystemClock.uptimeMillis()-start) + "ms");
4545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004546 }
4547
Sunny Goyalff572272014-07-23 13:58:07 -07004548 /**
4549 * Restores a pending widget.
4550 *
4551 * @param appWidgetId The app widget id
4552 * @param cellInfo The position on screen where to create the widget.
4553 */
4554 private void completeRestoreAppWidget(final int appWidgetId) {
4555 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4556 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4557 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4558 return;
4559 }
4560
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004561 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004562 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4563
4564 mWorkspace.reinflateWidgetsIfNecessary();
4565 LauncherModel.updateItemInDatabase(this, info);
4566 }
4567
Adam Cohen1462de32012-07-24 22:34:36 -07004568 public void onPageBoundSynchronously(int page) {
4569 mSynchronouslyBoundPages.add(page);
4570 }
4571
Joe Onorato9c1289c2009-08-17 11:03:03 -04004572 /**
4573 * Callback saying that there aren't any more items to bind.
4574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Adam Cohene25af792013-06-06 23:08:25 -07004577 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004578 Runnable r = new Runnable() {
4579 public void run() {
4580 finishBindingItems(upgradePath);
4581 }
4582 };
4583 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004584 return;
4585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004586 if (mSavedState != null) {
4587 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004588 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004590 mSavedState = null;
4591 }
4592
Adam Cohen1462de32012-07-24 22:34:36 -07004593 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004594
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004595 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004596 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004597
4598 // If we received the result of any pending adds while the loader was running (e.g. the
4599 // widget configuration forced an orientation change), process them now.
4600 if (sPendingAddItem != null) {
4601 final long screenId = completeAdd(sPendingAddItem);
4602
4603 // TODO: this moves the user to the page where the pending item was added. Ideally,
4604 // the screen would be guaranteed to exist after bind, and the page would be set through
4605 // the workspace restore process.
4606 mWorkspace.post(new Runnable() {
4607 @Override
4608 public void run() {
4609 mWorkspace.snapToScreenId(screenId);
4610 }
4611 });
4612 sPendingAddItem = null;
4613 }
4614
Adam Cohene25af792013-06-06 23:08:25 -07004615 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004616 mWorkspace.getUniqueComponents(true, null);
4617 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004618 }
Sunny Goyale755d462014-07-22 13:48:29 -07004619 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004620 }
4621
Adam Cohen3ed316a2014-07-23 18:21:20 -07004622 private void sendLoadingCompleteBroadcastIfNecessary() {
4623 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4624 String permission =
4625 getResources().getString(R.string.receive_first_load_broadcast_permission);
4626 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4627 sendBroadcast(intent, permission);
4628 SharedPreferences.Editor editor = mSharedPrefs.edit();
4629 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4630 editor.apply();
4631 }
4632 }
4633
Winson Chunga0b7e862013-09-05 16:03:15 -07004634 public boolean isAllAppsButtonRank(int rank) {
4635 if (mHotseat != null) {
4636 return mHotseat.isAllAppsButtonRank(rank);
4637 }
4638 return false;
4639 }
4640
Winson Chunga2413752012-04-03 14:22:34 -07004641 private boolean canRunNewAppsAnimation() {
4642 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4643 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4644 }
4645
Winson Chungc9168342013-06-26 14:54:55 -07004646 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4647 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4648 PropertyValuesHolder.ofFloat("alpha", 1f),
4649 PropertyValuesHolder.ofFloat("scaleX", 1f),
4650 PropertyValuesHolder.ofFloat("scaleY", 1f));
4651 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4652 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4653 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4654 return bounceAnim;
4655 }
4656
Adam Cohen27772732013-11-11 14:00:56 +00004657 public boolean useVerticalBarLayout() {
4658 return LauncherAppState.getInstance().getDynamicGrid().
4659 getDeviceProfile().isVerticalBarLayout();
4660 }
4661
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004662 protected Rect getSearchBarBounds() {
4663 return LauncherAppState.getInstance().getDynamicGrid().
4664 getDeviceProfile().getSearchBarBounds();
4665 }
4666
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004667 @Override
4668 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004669 boolean searchVisible = updateGlobalSearchIcon();
4670 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004671 if (mSearchDropTargetBar != null) {
4672 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4673 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004674 }
4675
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004676 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004677 * Add the icons for all apps.
4678 *
4679 * Implementation of the method from LauncherModel.Callbacks.
4680 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004681 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004682 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004683 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4684 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4685 getHotseat().addAllAppsFolder(mIconCache, apps,
4686 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4687 }
4688 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004689 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004690 if (mAppsCustomizeContent != null) {
4691 mAppsCustomizeContent.onPackagesUpdated(
4692 LauncherModel.getSortedWidgetsAndShortcuts(this));
4693 }
Winson Chungc58497e2013-09-03 17:48:37 -07004694 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004695 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004696 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004697 mAppsCustomizeContent.onPackagesUpdated(
4698 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004699 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004701 }
4702
4703 /**
4704 * A package was updated.
4705 *
4706 * Implementation of the method from LauncherModel.Callbacks.
4707 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004708 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004709 Runnable r = new Runnable() {
4710 public void run() {
4711 bindAppsUpdated(apps);
4712 }
4713 };
4714 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004715 return;
4716 }
4717
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004718 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004719 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004720 }
Winson Chungc58497e2013-09-03 17:48:37 -07004721
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004722 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004723 mAppsCustomizeContent != null) {
4724 mAppsCustomizeContent.updateApps(apps);
4725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004726 }
4727
4728 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004729 * Some shortcuts were updated in the background.
4730 *
4731 * Implementation of the method from LauncherModel.Callbacks.
4732 */
4733 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4734 Runnable r = new Runnable() {
4735 public void run() {
4736 bindShortcutsUpdated(shortcuts);
4737 }
4738 };
4739 if (waitUntilResume(r)) {
4740 return;
4741 }
4742
4743 if (mWorkspace != null) {
4744 mWorkspace.updateShortcuts(shortcuts);
4745 }
4746 }
4747
4748 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004749 * Update the state of a package, typically related to install state.
4750 *
4751 * Implementation of the method from LauncherModel.Callbacks.
4752 */
Sunny Goyale755d462014-07-22 13:48:29 -07004753 @Override
4754 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004755 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004756 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004757 }
4758 }
4759
4760 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004761 * Update the label and icon of all the icons in a package
4762 *
4763 * Implementation of the method from LauncherModel.Callbacks.
4764 */
4765 @Override
4766 public void updatePackageBadge(String packageName) {
4767 if (mWorkspace != null) {
4768 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4769 }
4770 }
4771
4772 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004773 * A package was uninstalled. We take both the super set of packageNames
4774 * in addition to specific applications to remove, the reason being that
4775 * this can be called when a package is updated as well. In that scenario,
4776 * we only remove specific components from the workspace, where as
4777 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004778 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004779 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004780 * Implementation of the method from LauncherModel.Callbacks.
4781 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004782 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004783 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004784 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004785 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004786 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004787 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004788 }
Winson Chungd64d1762013-08-20 14:37:16 -07004789 };
4790 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004791 return;
4792 }
4793
Sunny Goyal1a745e82014-10-02 15:58:31 -07004794 if (reason == 0) {
4795 if (!packageNames.isEmpty()) {
4796 mWorkspace.removeItemsByPackageName(packageNames, user);
4797 }
4798 if (!appInfos.isEmpty()) {
4799 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
4800 }
4801 } else {
4802 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004803 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004804
Winson Chunga1820962011-10-03 16:31:06 -07004805 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004806 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004807
Winson Chungdf95eb12013-10-16 14:57:07 -07004808 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004809 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004810 mAppsCustomizeContent != null) {
4811 mAppsCustomizeContent.removeApps(appInfos);
4812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004813 }
Joe Onoratobe386092009-11-17 17:32:16 -08004814
4815 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004816 * A number of packages were updated.
4817 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004818 private ArrayList<Object> mWidgetsAndShortcuts;
4819 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004820 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004821 bindPackagesUpdated(mWidgetsAndShortcuts);
4822 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004823 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004824 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004825 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4826 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4827 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004828 return;
4829 }
4830
Winson Chung64359a52013-07-08 17:17:08 -07004831 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004832 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004833 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004834 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004835 }
4836
Winson Chung400438b2011-01-16 17:53:48 -08004837 private int mapConfigurationOriActivityInfoOri(int configOri) {
4838 final Display d = getWindowManager().getDefaultDisplay();
4839 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4840 switch (d.getRotation()) {
4841 case Surface.ROTATION_0:
4842 case Surface.ROTATION_180:
4843 // We are currently in the same basic orientation as the natural orientation
4844 naturalOri = configOri;
4845 break;
4846 case Surface.ROTATION_90:
4847 case Surface.ROTATION_270:
4848 // We are currently in the other basic orientation to the natural orientation
4849 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4850 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4851 break;
4852 }
4853
4854 int[] oriMap = {
4855 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4856 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4857 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4858 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4859 };
4860 // Since the map starts at portrait, we need to offset if this device's natural orientation
4861 // is landscape.
4862 int indexOffset = 0;
4863 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4864 indexOffset = 1;
4865 }
4866 return oriMap[(d.getRotation() + indexOffset) % 4];
4867 }
Adam Cohen446e9402011-09-15 18:21:21 -07004868
Winson Chung4b919f82012-05-01 10:44:08 -07004869 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004870 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004871 getResources().getBoolean(R.bool.allow_rotation);
4872 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004873 }
Winson Chung4b919f82012-05-01 10:44:08 -07004874 public void lockScreenOrientation() {
4875 if (isRotationEnabled()) {
4876 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4877 .getConfiguration().orientation));
4878 }
4879 }
4880 public void unlockScreenOrientation(boolean immediate) {
4881 if (isRotationEnabled()) {
4882 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004883 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004884 } else {
4885 mHandler.postDelayed(new Runnable() {
4886 public void run() {
4887 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4888 }
4889 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004890 }
Winson Chung4b919f82012-05-01 10:44:08 -07004891 }
Winson Chung400438b2011-01-16 17:53:48 -08004892 }
4893
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004894 /**
4895 * Called when the SearchBar hint should be changed.
4896 *
4897 * @param hint the hint to be displayed in the search bar.
4898 */
4899 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004900
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004901 }
4902
Winson Chunge43a1e72014-01-15 10:33:02 -08004903 protected boolean isLauncherPreinstalled() {
4904 PackageManager pm = getPackageManager();
4905 try {
4906 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4907 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4908 return true;
4909 } else {
4910 return false;
4911 }
4912 } catch (NameNotFoundException e) {
4913 e.printStackTrace();
4914 return false;
4915 }
4916 }
4917
Adam Cohenea90f832014-05-21 15:03:34 -07004918 /**
4919 * This method indicates whether or not we should suggest default wallpaper dimensions
4920 * when our wallpaper cropper was not yet used to set a wallpaper.
4921 */
4922 protected boolean overrideWallpaperDimensions() {
4923 return true;
4924 }
4925
Adam Cohen432609a2014-03-13 17:03:22 -07004926 /**
4927 * To be overridden by subclasses to indicate that there is an activity to launch
4928 * before showing the standard launcher experience.
4929 */
4930 protected boolean hasFirstRunActivity() {
4931 return false;
4932 }
4933
4934 /**
4935 * To be overridden by subclasses to launch any first run activity
4936 */
4937 protected Intent getFirstRunActivity() {
4938 return null;
4939 }
4940
Winson Chunga6945242014-01-08 14:04:34 -08004941 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004942 return !ActivityManager.isRunningInTestHarness() &&
4943 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004944 }
4945
Adam Cohen4a9423d2014-03-28 14:22:31 -07004946 protected boolean hasRunFirstRunActivity() {
4947 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4948 }
4949
Adam Cohen432609a2014-03-13 17:03:22 -07004950 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004951 if (shouldRunFirstRunActivity() &&
4952 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004953 Intent firstRunIntent = getFirstRunActivity();
4954 if (firstRunIntent != null) {
4955 startActivity(firstRunIntent);
4956 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004957 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004958 }
4959 }
Adam Cohen432609a2014-03-13 17:03:22 -07004960 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004961 }
4962
4963 private void markFirstRunActivityShown() {
4964 SharedPreferences.Editor editor = mSharedPrefs.edit();
4965 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4966 editor.apply();
4967 }
4968
Adam Cohen432609a2014-03-13 17:03:22 -07004969 /**
4970 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4971 * screen that must be displayed and dismissed.
4972 */
4973 protected boolean hasDismissableIntroScreen() {
4974 return false;
4975 }
4976
4977 /**
4978 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4979 */
4980 protected View getIntroScreen() {
4981 return null;
4982 }
4983
4984 /**
4985 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4986 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4987 */
4988 private boolean shouldShowIntroScreen() {
4989 return hasDismissableIntroScreen() &&
4990 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4991 }
4992
4993 protected void showIntroScreen() {
4994 View introScreen = getIntroScreen();
4995 changeWallpaperVisiblity(false);
4996 if (introScreen != null) {
4997 mDragLayer.showOverlayView(introScreen);
4998 }
4999 }
5000
5001 public void dismissIntroScreen() {
5002 markIntroScreenDismissed();
5003 if (showFirstRunActivity()) {
5004 // We delay hiding the intro view until the first run activity is showing. This
5005 // avoids a blip.
5006 mWorkspace.postDelayed(new Runnable() {
5007 @Override
5008 public void run() {
5009 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005010 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005011 }
5012 }, ACTIVITY_START_DELAY);
5013 } else {
5014 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005015 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005016 }
5017 changeWallpaperVisiblity(true);
5018 }
5019
5020 private void markIntroScreenDismissed() {
5021 SharedPreferences.Editor editor = mSharedPrefs.edit();
5022 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5023 editor.apply();
5024 }
5025
Sunny Goyal88d60f12014-09-08 03:47:29 -07005026 private void showFirstRunClings() {
5027 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5028 // on the device, then we always show the first run cling experience (or if there is no
5029 // launcher2). Otherwise, we prompt the user upon started for migration
5030 LauncherClings launcherClings = new LauncherClings(this);
5031 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5032 if (mModel.canMigrateFromOldLauncherDb(this)) {
5033 launcherClings.showMigrationCling();
5034 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005035 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005036 }
5037 }
5038 }
5039
Winson Chunga6945242014-01-08 14:04:34 -08005040 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005041 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5042 if (mHotseat != null) mHotseat.setAlpha(1f);
5043 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5044 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005045 }
5046
5047 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005048 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5049 if (mHotseat != null) mHotseat.setAlpha(0f);
5050 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5051 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005052 }
5053
Mathew Inwood72fbec12013-11-19 15:45:07 +00005054 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005055 // Called from search suggestion, not supported in other profiles.
5056 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5057 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5058 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5059 myUser);
5060 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005061 return null;
5062 }
Kenny Guyed131872014-04-30 03:02:21 +01005063 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005064 }
5065
5066 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5067 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005068 // Called from search suggestion, not supported in other profiles.
5069 return createShortcutDragInfo(shortcutIntent, caption, icon,
5070 UserHandleCompat.myUserHandle());
5071 }
5072
5073 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5074 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005075 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005076 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005077 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005078 }
5079
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005080 protected void moveWorkspaceToDefaultScreen() {
5081 mWorkspace.moveToDefaultScreen(false);
5082 }
5083
Mathew Inwood72fbec12013-11-19 15:45:07 +00005084 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5085 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005086 mWorkspace.onExternalDragStartedWithItem(dragView);
5087 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005088 }
5089
Anjali Koppalf5d29132014-02-28 13:33:27 -08005090 @Override
5091 public void onPageSwitch(View newPage, int newPageIndex) {
5092 }
5093
Winson Chung80baf5a2010-08-09 16:03:15 -07005094 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005095 * Prints out out state for debugging.
5096 */
5097 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005098 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005099 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005100 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5101 Log.d(TAG, "mRestoring=" + mRestoring);
5102 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5103 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005104 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005105 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005106
Winson Chung785d2eb2011-04-14 16:08:02 -07005107 if (mAppsCustomizeContent != null) {
5108 mAppsCustomizeContent.dumpState();
5109 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005110 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005111 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005112
5113 @Override
5114 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5115 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005116 synchronized (sDumpLogs) {
5117 writer.println(" ");
5118 writer.println("Debug logs: ");
5119 for (int i = 0; i < sDumpLogs.size(); i++) {
5120 writer.println(" " + sDumpLogs.get(i));
5121 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005122 }
5123 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005124
5125 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005126 if (DEBUG_DUMP_LOG) {
5127 synchronized (sDumpLogs) {
5128 Log.d(TAG, "");
5129 Log.d(TAG, "*********************");
5130 Log.d(TAG, "Launcher debug logs: ");
5131 for (int i = 0; i < sDumpLogs.size(); i++) {
5132 Log.d(TAG, " " + sDumpLogs.get(i));
5133 }
5134 Log.d(TAG, "*********************");
5135 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005136 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005137 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005138 }
5139
5140 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005141 addDumpLog(tag, log, null, debugLog);
5142 }
5143
5144 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005145 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005146 if (e != null) {
5147 Log.d(tag, log, e);
5148 } else {
5149 Log.d(tag, log);
5150 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005151 }
Winson Chungede41292013-09-19 16:27:36 -07005152 if (DEBUG_DUMP_LOG) {
5153 sDateStamp.setTime(System.currentTimeMillis());
5154 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005155 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5156 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005157 }
Winson Chungede41292013-09-19 16:27:36 -07005158 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005159 }
5160
Winson Chungede41292013-09-19 16:27:36 -07005161 public void dumpLogsToLocalData() {
5162 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005163 new AsyncTask<Void, Void, Void>() {
5164 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005165 boolean success = false;
5166 sDateStamp.setTime(sRunStart);
5167 String FILENAME = sDateStamp.getMonth() + "-"
5168 + sDateStamp.getDay() + "_"
5169 + sDateStamp.getHours() + "-"
5170 + sDateStamp.getMinutes() + "_"
5171 + sDateStamp.getSeconds() + ".txt";
5172
5173 FileOutputStream fos = null;
5174 File outFile = null;
5175 try {
5176 outFile = new File(getFilesDir(), FILENAME);
5177 outFile.createNewFile();
5178 fos = new FileOutputStream(outFile);
5179 } catch (Exception e) {
5180 e.printStackTrace();
5181 }
5182 if (fos != null) {
5183 PrintWriter writer = new PrintWriter(fos);
5184
5185 writer.println(" ");
5186 writer.println("Debug logs: ");
5187 synchronized (sDumpLogs) {
5188 for (int i = 0; i < sDumpLogs.size(); i++) {
5189 writer.println(" " + sDumpLogs.get(i));
5190 }
5191 }
5192 writer.close();
5193 }
5194 try {
5195 if (fos != null) {
5196 fos.close();
5197 success = true;
5198 }
5199 } catch (IOException e) {
5200 e.printStackTrace();
5201 }
Michael Jurka43467462013-11-14 12:03:58 +01005202 return null;
Winson Chungede41292013-09-19 16:27:36 -07005203 }
Michael Jurka43467462013-11-14 12:03:58 +01005204 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005205 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005207}
Michael Jurka2763be32011-02-24 11:19:57 -08005208
Michael Jurkaabded662011-03-04 12:06:57 -08005209interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005210 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005211 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005212 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005213 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005214 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005215}
Dan Sandlerd5024042014-01-09 15:01:33 -05005216
5217interface DebugIntents {
5218 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5219 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005220}