blob: ad501a31cfbeadded16e085bb582ca31f4f8619f [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080045import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohen0b395352014-06-09 22:54:36 +000083import android.view.Window;
Adam Cohenf358a4b2013-07-23 16:47:31 -070084import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080085import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
87import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080088import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
101import com.android.launcher3.compat.UserHandleCompat;
102import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400103import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800104import com.android.launcher3.PagedView.PageSwitchListener;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.DataInputStream;
106import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000113import java.lang.reflect.Field;
114import java.lang.reflect.InvocationTargetException;
115import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700118import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700121import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000122import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123
Winson Chunga6945242014-01-08 14:04:34 -0800124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125/**
126 * Default launcher application.
127 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100128public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700129 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700130 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800131 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700132 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400135 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700136 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400137 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700138 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700139
Dan Sandlerd5024042014-01-09 15:01:33 -0500140 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
149
Mathew Inwood876a8462013-06-14 14:12:41 +0100150 /**
151 * IntentStarter uses request codes starting with this. This must be greater than all activity
152 * request codes used internally.
153 */
154 protected static final int REQUEST_LAST = 100;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
157
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 static final int SCREEN_COUNT = 5;
159 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Romain Guy98d01652009-06-30 16:21:04 -0700161 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 // To turn on these properties, type
163 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
164 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800166 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohenb54a5982014-01-08 15:21:04 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100203 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700204 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100205 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700206 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100207 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700215 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700216 private State mState = State.WORKSPACE;
217 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700220 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
221 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700222 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800225 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000227 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
228 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
229
Winson Chunga2413752012-04-03 14:22:34 -0700230 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700231 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
232 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700233 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700234
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800235 private final BroadcastReceiver mCloseSystemDialogsReceiver
236 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800237 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private LayoutInflater mInflater;
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700242 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800243 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800244 private DragLayer mDragLayer;
245 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700246 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700249 private AppWidgetManager mAppWidgetManager;
250 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Michael Jurkac9d95c52011-08-29 14:03:34 -0700252 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700253 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800254 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255
Michael Jurka0280c3b2010-09-17 15:00:07 -0700256 private int[] mTmpAddItemCellCoordinates = new int[2];
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private FolderInfo mFolderInfo;
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 private AppsCustomizeTabHost mAppsCustomizeTabHost;
267 private AppsCustomizePagedView mAppsCustomizeContent;
268 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800269 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
273 // scroll issues (because the workspace may not have been measured yet) and extra work.
274 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
275 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
277 private SpannableStringBuilder mDefaultKeySsb = null;
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private boolean mWorkspaceLoading = true;
280
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800281 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private boolean mRestoring;
283 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700284 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
Michael Jurka1e2f4652013-07-08 18:03:46 -0700286 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private Bundle mSavedInstanceState;
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800292 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mUserPresent = true;
294 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700295 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700298 private static LocaleConfiguration sLocaleConfiguration = null;
299
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700300 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Adam Cohen61f560d2013-09-30 15:58:20 -0700302 private View.OnTouchListener mHapticFeedbackTouchListener;
303
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 // Related to the auto-advancing of widgets
305 private final int ADVANCE_MSG = 1;
306 private final int mAdvanceInterval = 20000;
307 private final int mAdvanceStagger = 250;
308 private long mAutoAdvanceSentTime;
309 private long mAutoAdvanceTimeLeft = -1;
310 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
311 new HashMap<View, AppWidgetProviderInfo>();
312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
315 private final int mRestoreScreenOrientationDelay = 500;
316
Michael Jurka4ef207b2010-11-29 17:05:45 -0800317 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700318 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
319 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
320 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800321
Winson Chungd64a6662013-09-30 11:06:59 -0700322 private Intent mAppMarketIntent = null;
323 private static final boolean DISABLE_MARKET_BUTTON = true;
324
Craig Mautner360310b2012-10-26 15:13:08 -0700325 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Adam Cohene25af792013-06-06 23:08:25 -0700341 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
342
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 // Holds the page that we need to animate to, and the icon views that we need to animate up
344 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700345 private ImageView mFolderIconImageView;
346 private Bitmap mFolderIconBitmap;
347 private Canvas mFolderIconCanvas;
348 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349
Michael Jurkaddd62e92011-02-16 17:49:14 -0800350 private BubbleTextView mWaitingForResume;
351
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 private Runnable mBuildLayersRunnable = new Runnable() {
353 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700354 if (mWorkspace != null) {
355 mWorkspace.buildPageHardwareLayers();
356 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700357 }
358 };
359
Adam Cohendb364c32014-05-20 14:23:40 -0700360 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361
Michael Jurka7267fa52013-09-26 15:29:57 -0700362 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800363
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 private static class PendingAddArguments {
365 int requestCode;
366 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700367 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700368 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 int cellX;
370 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700371 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 }
373
Daniel Sandlerff02d492013-08-05 02:12:05 -0400374 private Stats mStats;
375
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800376 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800377 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700378 }
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 @Override
381 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700382 if (DEBUG_STRICT_MODE) {
383 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
384 .detectDiskReads()
385 .detectDiskWrites()
386 .detectNetwork() // or .detectAll() for all detectable problems
387 .penaltyLog()
388 .build());
389 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
390 .detectLeakedSqlLiteObjects()
391 .detectLeakedClosableObjects()
392 .penaltyLog()
393 .penaltyDeath()
394 .build());
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400398
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400399 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400400 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700401 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700402 // Determine the dynamic grid properties
403 Point smallestSize = new Point();
404 Point largestSize = new Point();
405 Point realSize = new Point();
406 Display display = getWindowManager().getDefaultDisplay();
407 display.getCurrentSizeRange(smallestSize, largestSize);
408 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700409 DisplayMetrics dm = new DisplayMetrics();
410 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Winson Chung5f8afe62013-08-12 16:19:28 -0700412 // Lazy-initialize the dynamic grid
413 DeviceProfile grid = app.initDynamicGrid(this,
414 Math.min(smallestSize.x, smallestSize.y),
415 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700416 realSize.x, realSize.y,
417 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700418
419 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700421 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700424 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800426 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429 mStats = new Stats(this);
430
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
Adam Cohen53805212013-10-01 10:39:23 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100451 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800453 registerContentObservers();
454
Joe Onorato7c312c12009-08-13 21:36:53 -0700455 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mSavedState = savedInstanceState;
458 restoreState(mSavedState);
459
460 if (PROFILE_STARTUP) {
461 android.os.Debug.stopMethodTracing();
462 }
463
464 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700465 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 // If the user leaves launcher, then we should just load items asynchronously when
467 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500468 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 } else {
470 // We only load the page synchronously if the user rotates (or triggers a
471 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800472 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
481 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Adam Cohenf9426d52012-06-04 17:26:21 -0700483 updateGlobalIcons();
484
485 // On large interfaces, we want the screen to auto-rotate based on the current orientation
486 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700487
Adam Cohen432609a2014-03-13 17:03:22 -0700488 if (shouldShowIntroScreen()) {
489 showIntroScreen();
490 } else {
491 showFirstRunActivity();
492 }
493
Winson Chunge43a1e72014-01-15 10:33:02 -0800494 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
495 // on the device, then we always show the first run cling experience (or if there is no
496 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800497 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
498 if (mModel.canMigrateFromOldLauncherDb(this)) {
499 mLauncherClings.showMigrationCling();
500 } else {
501 mLauncherClings.showFirstRunCling();
502 }
Winson Chunga6945242014-01-08 14:04:34 -0800503 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800504 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800505 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700506 }
507
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700508 @Override
509 public void onLauncherProviderChange() { }
510
Winson Chung98ca0f72013-07-29 12:58:51 -0700511 /** To be overriden by subclasses to hint to Launcher that we have custom content */
512 protected boolean hasCustomContentToLeft() {
513 return false;
514 }
515
Dave Hawkeya8881582013-09-17 15:55:33 -0600516 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500517 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600518 * {@link #addToCustomContentPage}. This will only be invoked if
519 * {@link #hasCustomContentToLeft()} is {@code true}.
520 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500521 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 }
523
524 /**
525 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
526 * ensure the custom content page is added or removed if necessary.
527 */
528 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600529 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600530 // Not bound yet, wait for bindScreens to be called.
531 return;
532 }
533
534 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
535 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500536 mWorkspace.createCustomContentContainer();
537 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
539 mWorkspace.removeCustomContentPage();
540 }
541 }
542
Adam Cohenf9426d52012-06-04 17:26:21 -0700543 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700544 boolean searchVisible = false;
545 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800546 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700547 int coi = getCurrentOrientationIndexForGlobalIcons();
548 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
549 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700550 if (!DISABLE_MARKET_BUTTON) {
551 updateAppMarketIcon();
552 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700553 searchVisible = updateGlobalSearchIcon();
554 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700555 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700556 if (sGlobalSearchIcon[coi] != null) {
557 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700558 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700559 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700560 if (sVoiceSearchIcon[coi] != null) {
561 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700562 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700563 }
Winson Chungd64a6662013-09-30 11:06:59 -0700564 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700565 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800566 }
Winson Chungadf0c182012-08-23 14:59:07 -0700567 if (mSearchDropTargetBar != null) {
568 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
569 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 }
Romain Guycbb89e42009-06-08 15:52:54 -0700571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700573 if (sLocaleConfiguration == null) {
574 new AsyncTask<Void, Void, LocaleConfiguration>() {
575 @Override
576 protected LocaleConfiguration doInBackground(Void... unused) {
577 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
578 readConfiguration(Launcher.this, localeConfiguration);
579 return localeConfiguration;
580 }
581
582 @Override
583 protected void onPostExecute(LocaleConfiguration result) {
584 sLocaleConfiguration = result;
585 checkForLocaleChange(); // recursive, but now with a locale configuration
586 }
587 }.execute();
588 return;
589 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700590
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 final Configuration configuration = getResources().getConfiguration();
592
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700593 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 final String locale = configuration.locale.toString();
595
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final int mcc = configuration.mcc;
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final int mnc = configuration.mnc;
601
Romain Guy84f296c2009-11-04 15:00:44 -0800602 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603
Romain Guy84f296c2009-11-04 15:00:44 -0800604 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 sLocaleConfiguration.locale = locale;
606 sLocaleConfiguration.mcc = mcc;
607 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700608
Joe Onorato0589f0f2010-02-08 13:44:00 -0800609 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610
611 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100612 new AsyncTask<Void, Void, Void>() {
613 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100615 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 }
Michael Jurka43467462013-11-14 12:03:58 +0100617 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700618 }
619 }
620
621 private static class LocaleConfiguration {
622 public String locale;
623 public int mcc = -1;
624 public int mnc = -1;
625 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700626
Romain Guy98d01652009-06-30 16:21:04 -0700627 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
628 DataInputStream in = null;
629 try {
630 in = new DataInputStream(context.openFileInput(PREFERENCES));
631 configuration.locale = in.readUTF();
632 configuration.mcc = in.readInt();
633 configuration.mnc = in.readInt();
634 } catch (FileNotFoundException e) {
635 // Ignore
636 } catch (IOException e) {
637 // Ignore
638 } finally {
639 if (in != null) {
640 try {
641 in.close();
642 } catch (IOException e) {
643 // Ignore
644 }
645 }
646 }
647 }
648
649 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
650 DataOutputStream out = null;
651 try {
652 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
653 out.writeUTF(configuration.locale);
654 out.writeInt(configuration.mcc);
655 out.writeInt(configuration.mnc);
656 out.flush();
657 } catch (FileNotFoundException e) {
658 // Ignore
659 } catch (IOException e) {
660 //noinspection ResultOfMethodCallIgnored
661 context.getFileStreamPath(PREFERENCES).delete();
662 } finally {
663 if (out != null) {
664 try {
665 out.close();
666 } catch (IOException e) {
667 // Ignore
668 }
669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Anton Hanssona2f665f2013-09-26 12:18:32 +0100673 public Stats getStats() {
674 return mStats;
675 }
676
Bjorn Bringertc459e522013-06-07 19:36:01 +0100677 public LayoutInflater getInflater() {
678 return mInflater;
679 }
680
Winson Chung36a62fe2012-05-06 18:04:42 -0700681 boolean isDraggingEnabled() {
682 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
683 // that is subsequently removed from the workspace in startBinding().
684 return !mModel.isLoadingWorkspace();
685 }
686
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 static int getScreen() {
688 synchronized (sLock) {
689 return sScreen;
690 }
691 }
692
693 static void setScreen(int screen) {
694 synchronized (sLock) {
695 sScreen = screen;
696 }
697 }
698
Winson Chung557d6ed2011-07-08 15:34:52 -0700699 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000700 * Copied from View -- the View version of the method isn't called
701 * anywhere else in our process and only exists for API level 17+,
702 * so it's ok to keep our own version with no API requirement.
703 */
704 public static int generateViewId() {
705 for (;;) {
706 final int result = sNextGeneratedId.get();
707 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
708 int newValue = result + 1;
709 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
710 if (sNextGeneratedId.compareAndSet(result, newValue)) {
711 return result;
712 }
713 }
714 }
715
716 public int getViewIdForItem(ItemInfo info) {
717 // This cast is safe given the > 2B range for int.
718 int itemId = (int) info.id;
719 if (mItemIdToViewId.containsKey(itemId)) {
720 return mItemIdToViewId.get(itemId);
721 }
722 int viewId = generateViewId();
723 mItemIdToViewId.put(itemId, viewId);
724 return viewId;
725 }
726
727 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700728 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
729 * a configuration step, this allows the proper animations to run after other transitions.
730 */
Adam Cohendb364c32014-05-20 14:23:40 -0700731 private long completeAdd(PendingAddArguments args) {
732
733 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700736 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700737 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700740 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700741 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 }
Michael Jurka27614d22012-04-02 06:40:22 -0700743 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
744 // if you turned the screen off and then back while in All Apps, Launcher would not
745 // return to the workspace. Clearing mAddInfo.container here fixes this issue
746 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700747 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 protected void onActivityResult(
752 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700753 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700754 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700755 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800756 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 Runnable exitSpringLoaded = new Runnable() {
759 @Override
760 public void run() {
761 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
762 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
763 }
764 };
765
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
769 if (resultCode == RESULT_CANCELED) {
770 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700771 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
775 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 }
777 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200778 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
779 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
780 mWorkspace.exitOverviewMode(false);
781 }
782 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
786 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700787
Adam Cohendb364c32014-05-20 14:23:40 -0700788 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 // We have special handling for widgets
790 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800791 final int appWidgetId;
792 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
793 : -1;
794 if (widgetId < 0) {
795 appWidgetId = pendingAddWidgetId;
796 } else {
797 appWidgetId = widgetId;
798 }
799
Adam Cohenad4e15c2013-10-17 16:21:35 -0700800 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800801 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700802 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
803 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 result = RESULT_CANCELED;
805 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700806 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700807 @Override
808 public void run() {
809 exitSpringLoadedDragModeDelayed(false, 0, null);
810 }
811 };
Adam Cohendb364c32014-05-20 14:23:40 -0700812 if (workspaceLocked) {
813 // No need to remove the empty screen if we're mid-binding, as the
814 // the bind will not add the empty screen.
815 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
816 } else {
817 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
818 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
819 }
Winson Chung5aaab772012-04-27 15:24:02 -0700820 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700821 if (!workspaceLocked) {
822 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
823 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
824
825 dropLayout.setDropPending(true);
826 final Runnable onComplete = new Runnable() {
827 @Override
828 public void run() {
829 completeTwoStageWidgetDrop(resultCode, appWidgetId);
830 dropLayout.setDropPending(false);
831 }
832 };
833 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
835 } else {
836 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
837 mPendingAddInfo);
838 sPendingAddItem = args;
839 }
Winson Chung5aaab772012-04-27 15:24:02 -0700840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 return;
842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 // The pattern used here is that a user PICKs a specific application,
845 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
848 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700849 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700850 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
851 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800852 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700853 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700855 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700856 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
857 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohen00074722013-10-11 17:46:09 -0700859 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700860 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700861 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800863 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 }
865
Adam Cohendb364c32014-05-20 14:23:40 -0700866 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
867 appWidgetId, ItemInfo info) {
868 PendingAddArguments args = new PendingAddArguments();
869 args.requestCode = requestCode;
870 args.intent = data;
871 args.container = info.container;
872 args.screenId = info.screenId;
873 args.cellX = info.cellX;
874 args.cellY = info.cellY;
875 args.appWidgetId = appWidgetId;
876 return args;
877 }
878
879 /**
880 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
881 *
882 * @param screenId the screen id to check
883 * @return the new screen, or screenId if it exists
884 */
885 private long ensurePendingDropLayoutExists(long screenId) {
886 CellLayout dropLayout =
887 (CellLayout) mWorkspace.getScreenWithId(screenId);
888 if (dropLayout == null) {
889 // it's possible that the add screen was removed because it was
890 // empty and a re-bind occurred
891 mWorkspace.addExtraEmptyScreen();
892 return mWorkspace.commitExtraEmptyScreen();
893 } else {
894 return screenId;
895 }
896 }
897
Adam Cohened66b2b2012-01-23 17:28:51 -0800898 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700899 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700900 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800901 Runnable onCompleteRunnable = null;
902 int animationType = 0;
903
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700904 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800905 if (resultCode == RESULT_OK) {
906 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
907 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700908 mPendingAddWidgetInfo);
909 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 onCompleteRunnable = new Runnable() {
911 @Override
912 public void run() {
913 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700914 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700915 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
916 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 }
918 };
919 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800920 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 if (mDragLayer.getAnimatedView() != null) {
924 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
925 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
926 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 // The animated view may be null in the case of a rotation during widget configuration
929 onCompleteRunnable.run();
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 }
932
933 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700934 protected void onStop() {
935 super.onStop();
936 FirstFrameAnimatorHelper.setIsVisible(false);
937 }
938
939 @Override
940 protected void onStart() {
941 super.onStart();
942 FirstFrameAnimatorHelper.setIsVisible(true);
943 }
944
945 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200947 long startTime = 0;
948 if (DEBUG_RESUME_TIME) {
949 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400950 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700953
Winson Chung4a2afa32012-07-19 14:53:05 -0700954 // Restore the previous launcher state
955 if (mOnResumeState == State.WORKSPACE) {
956 showWorkspace(false);
957 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800958 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 }
960 mOnResumeState = State.NONE;
961
Craig Mautner360310b2012-10-26 15:13:08 -0700962 // Background was set to gradient in onPause(), restore to black if in all apps.
963 setWorkspaceBackground(mState == State.WORKSPACE);
964
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800965 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700966 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700967 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500968 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400969 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700970 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700972 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200973 // We might have postponed some bind calls until onResume (see waitUntilResume) --
974 // execute them here
975 long startTimeCallbacks = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTimeCallbacks = System.currentTimeMillis();
978 }
979
980 if (mAppsCustomizeContent != null) {
981 mAppsCustomizeContent.setBulkBind(true);
982 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700983 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
984 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200985 }
986 if (mAppsCustomizeContent != null) {
987 mAppsCustomizeContent.setBulkBind(false);
988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 if (DEBUG_RESUME_TIME) {
991 Log.d(TAG, "Time spent processing callbacks in onResume: " +
992 (System.currentTimeMillis() - startTimeCallbacks));
993 }
Michael Jurka447bf842013-05-15 14:52:15 +0200994 }
Michael Jurka54554252013-08-01 12:52:23 +0200995 if (mOnResumeCallbacks.size() > 0) {
996 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
997 mOnResumeCallbacks.get(i).run();
998 }
999 mOnResumeCallbacks.clear();
1000 }
Winson Chunge4e50662012-01-23 14:45:13 -08001001
1002 // Reset the pressed state of icons that were locked in the press state while activities
1003 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001004 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001005 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001006 mWaitingForResume.setStayPressed(false);
1007 }
Winson Chunge4e50662012-01-23 14:45:13 -08001008 if (mAppsCustomizeContent != null) {
1009 // Resets the previous all apps icon press state
1010 mAppsCustomizeContent.resetDrawableState();
1011 }
Winson Chung82963d52013-10-09 11:20:57 -07001012
Adam Cohen06dff352012-06-01 17:17:08 -07001013 // It is possible that widgets can receive updates while launcher is not in the foreground.
1014 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1015 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1016 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001017 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001018
Winson Chung780fe592013-09-26 14:48:44 -07001019 // Process any items that were added while Launcher was away.
1020 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1021
Winson Chung5841efa2013-09-30 18:06:44 -07001022 // Update the voice search button proxy
1023 updateVoiceButtonProxyVisible(false);
1024
Adam Cohenf9426d52012-06-04 17:26:21 -07001025 // Again, as with the above scenario, it's possible that one or more of the global icons
1026 // were updated in the wrong orientation.
1027 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001028 if (DEBUG_RESUME_TIME) {
1029 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1030 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001031
1032 if (mWorkspace.getCustomContentCallbacks() != null) {
1033 // If we are resuming and the custom content is the current page, we call onShow().
1034 // It is also poassible that onShow will instead be called slightly after first layout
1035 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1036 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001037 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001038 }
1039 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001040 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001041 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001042 }
1043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001046 // Ensure that items added to Launcher are queued until Launcher returns
1047 InstallShortcutReceiver.enableInstallQueue();
1048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001050 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001051 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001052 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001053
1054 // We call onHide() aggressively. The custom content callbacks should be able to
1055 // debounce excess onHide calls.
1056 if (mWorkspace.getCustomContentCallbacks() != null) {
1057 mWorkspace.getCustomContentCallbacks().onHide();
1058 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 }
Romain Guycbb89e42009-06-08 15:52:54 -07001060
Adam Cohenbffe7452013-07-22 18:21:45 -07001061 QSBScroller mQsbScroller = new QSBScroller() {
1062 int scrollY = 0;
1063
1064 @Override
1065 public void setScrollY(int scroll) {
1066 scrollY = scroll;
1067
1068 if (mWorkspace.isOnOrMovingToCustomContent()) {
1069 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001070 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 }
1072 }
1073 };
1074
1075 public void resetQSBScroll() {
1076 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001077 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 }
1079
1080 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001081 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1082 // by a onResume or by scrolling otherwise.
1083 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001084
1085 // Custom content is completely hidden
1086 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001087
1088 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1089 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
1091
Jorim Jaggid017f882014-01-14 17:08:48 -08001092 protected boolean hasSettings() {
1093 return false;
1094 }
1095
Adam Cohenbffe7452013-07-22 18:21:45 -07001096 public interface QSBScroller {
1097 public void setScrollY(int scrollY);
1098 }
1099
Winson Chung98ca0f72013-07-29 12:58:51 -07001100 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001101 CustomContentCallbacks callbacks, String description) {
1102 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001104 }
1105
Adam Cohenedb40762013-07-18 16:45:45 -07001106 // The custom content needs to offset its content to account for the QSB
1107 public int getTopOffsetForCustomContent() {
1108 return mWorkspace.getPaddingTop();
1109 }
1110
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001111 @Override
1112 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001113 // Flag the loader to stop early before switching
1114 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001115 if (mAppsCustomizeContent != null) {
1116 mAppsCustomizeContent.surrender();
1117 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001118 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001119 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001121 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001122 @Override
1123 public void onWindowFocusChanged(boolean hasFocus) {
1124 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001125 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001126 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 private boolean acceptFilter() {
1129 final InputMethodManager inputManager = (InputMethodManager)
1130 getSystemService(Context.INPUT_METHOD_SERVICE);
1131 return !inputManager.isFullscreenMode();
1132 }
1133
1134 @Override
1135 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001136 final int uniChar = event.getUnicodeChar();
1137 final boolean handled = super.onKeyDown(keyCode, event);
1138 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1139 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1141 keyCode, event);
1142 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001143 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001144 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001145 // showSearchDialog()
1146 // If there are multiple keystrokes before the search dialog takes focus,
1147 // onSearchRequested() will be called for every keystroke,
1148 // but it is idempotent, so it's fine.
1149 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
1151 }
1152
Joe Onorato8a9625e2010-01-28 15:55:35 -08001153 // Eat the long press event so the keyboard doesn't come up.
1154 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1155 return true;
1156 }
1157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 return handled;
1159 }
1160
Karl Rosaen138a0412009-04-23 19:00:21 -07001161 private String getTypedText() {
1162 return mDefaultKeySsb.toString();
1163 }
1164
1165 private void clearTypedText() {
1166 mDefaultKeySsb.clear();
1167 mDefaultKeySsb.clearSpans();
1168 Selection.setSelection(mDefaultKeySsb, 0);
1169 }
1170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001172 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1173 * State
1174 */
1175 private static State intToState(int stateOrdinal) {
1176 State state = State.WORKSPACE;
1177 final State[] stateValues = State.values();
1178 for (int i = 0; i < stateValues.length; i++) {
1179 if (stateValues[i].ordinal() == stateOrdinal) {
1180 state = stateValues[i];
1181 break;
1182 }
1183 }
1184 return state;
1185 }
1186
1187 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 * Restores the previous state, if it exists.
1189 *
1190 * @param savedState The previous state.
1191 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001192 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 private void restoreState(Bundle savedState) {
1194 if (savedState == null) {
1195 return;
1196 }
1197
Michael Jurka883f55b2010-10-21 15:47:14 -07001198 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001199 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001200 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001201 }
1202
Adam Cohen21cd0022013-10-09 18:57:02 -07001203 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1204 PagedView.INVALID_RESTORE_PAGE);
1205 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001206 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
Winson Chung3d503fb2011-07-13 17:25:49 -07001209 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001210 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1213 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001214 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001215 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1216 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001217 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1218 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1219 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001220 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001221 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 mRestoring = true;
1223 }
1224
1225 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1226 if (renameFolder) {
1227 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001228 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 mRestoring = true;
1230 }
Winson Chunga12a2502010-12-20 14:41:35 -08001231
Winson Chung785d2eb2011-04-14 16:08:02 -07001232 // Restore the AppsCustomize tab
1233 if (mAppsCustomizeTabHost != null) {
1234 String curTab = savedState.getString("apps_customize_currentTab");
1235 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001236 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001237 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001238 mAppsCustomizeContent.loadAssociatedPages(
1239 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001240 }
1241
Winson Chung5afbf7b2011-07-25 11:53:08 -07001242 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1243 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001244 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001245 mItemIdToViewId = (HashMap<Integer, Integer>)
1246 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248
1249 /**
1250 * Finds all the views we need and configure them properly.
1251 */
1252 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001253 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001254
Craig Mautner360310b2012-10-26 15:13:08 -07001255 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001256 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1257 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001258 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001259 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001260
John Spurlock77e1f472013-09-11 10:09:51 -04001261 mLauncherView.setSystemUiVisibility(
1262 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001263 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
Winson Chung4c98d922011-05-31 16:50:48 -07001265 // Setup the drag layer
1266 mDragLayer.setup(this, dragController);
1267
Winson Chung3d503fb2011-07-13 17:25:49 -07001268 // Setup the hotseat
1269 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1270 if (mHotseat != null) {
1271 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001272 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001273 }
1274
Jorim Jaggid017f882014-01-14 17:08:48 -08001275 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001276 View widgetButton = findViewById(R.id.widget_button);
1277 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001278 @Override
1279 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001280 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001281 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001282 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001283 }
1284 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001285 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen71b04732014-06-11 10:36:14 -07001286 if (Utilities.isLmp()) {
1287 ((TextView) widgetButton).setCompoundDrawablesWithIntrinsicBounds(0,
1288 R.drawable.widget_button_l, 0, 0);
1289 }
Adam Cohen61f560d2013-09-30 15:58:20 -07001290
1291 View wallpaperButton = findViewById(R.id.wallpaper_button);
1292 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001293 @Override
1294 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001295 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001296 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001297 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001298 }
1299 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001300 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1301
Adam Cohen71b04732014-06-11 10:36:14 -07001302 if (Utilities.isLmp()) {
1303 ((TextView) wallpaperButton).setCompoundDrawablesWithIntrinsicBounds(0,
1304 R.drawable.wallpaper_button_l, 0, 0);
1305 }
1306
Adam Cohen61f560d2013-09-30 15:58:20 -07001307 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001308 if (hasSettings()) {
1309 settingsButton.setOnClickListener(new OnClickListener() {
1310 @Override
1311 public void onClick(View arg0) {
1312 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001313 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001314 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001315 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001316 });
1317 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen71b04732014-06-11 10:36:14 -07001318 if (Utilities.isLmp()) {
1319 ((TextView) settingsButton).setCompoundDrawablesWithIntrinsicBounds(0,
1320 R.drawable.setting_button_l, 0, 0);
1321 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001322 } else {
1323 settingsButton.setVisibility(View.GONE);
1324 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1325 lp.gravity = Gravity.END | Gravity.TOP;
1326 widgetButton.requestLayout();
1327 }
1328
Adam Cohendbdff6b2013-09-18 19:09:15 -07001329 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001330
Winson Chung4c98d922011-05-31 16:50:48 -07001331 // Setup the workspace
1332 mWorkspace.setHapticFeedbackEnabled(false);
1333 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001334 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001335 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001336
Winson Chungf0ea4d32011-06-06 14:27:16 -07001337 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001338 mSearchDropTargetBar = (SearchDropTargetBar)
1339 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001340
Winson Chungf0ea4d32011-06-06 14:27:16 -07001341 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001342 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001343 mAppsCustomizeContent = (AppsCustomizePagedView)
1344 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1345 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001346
Winson Chung3d503fb2011-07-13 17:25:49 -07001347 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001348 dragController.setDragScoller(mWorkspace);
1349 dragController.setScrollView(mDragLayer);
1350 dragController.setMoveTarget(mWorkspace);
1351 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001352 if (mSearchDropTargetBar != null) {
1353 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001354 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001355
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001356 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001357 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001358 mWeightWatcher = new WeightWatcher(this);
1359 mWeightWatcher.setAlpha(0.5f);
1360 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001361 new FrameLayout.LayoutParams(
1362 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001363 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001364 Gravity.BOTTOM)
1365 );
Adam Cohen39a06042013-07-19 14:30:12 -07001366
1367 boolean show = shouldShowWeightWatcher();
1368 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
1372 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001373 * Sets the all apps button. This method is called from {@link Hotseat}.
1374 */
1375 public void setAllAppsButton(View allAppsButton) {
1376 mAllAppsButton = allAppsButton;
1377 }
1378
1379 public View getAllAppsButton() {
1380 return mAllAppsButton;
1381 }
1382
1383 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 * Creates a view representing a shortcut.
1385 *
1386 * @param info The data structure describing the shortcut.
1387 *
1388 * @return A View inflated from R.layout.application.
1389 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001390 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001392 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394
1395 /**
1396 * Creates a view representing a shortcut inflated from the specified resource.
1397 *
1398 * @param layoutResId The id of the XML layout used to create the shortcut.
1399 * @param parent The group the shortcut belongs to.
1400 * @param info The data structure describing the shortcut.
1401 *
1402 * @return A View inflated from layoutResId.
1403 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001404 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001405 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1406 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 return favorite;
1409 }
1410
1411 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 * Add a shortcut to the workspace.
1413 *
1414 * @param data The intent describing the shortcut.
1415 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001417 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001418 int cellY) {
1419 int[] cellXY = mTmpAddItemCellCoordinates;
1420 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001421 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Michael Jurkad3ef3062010-11-23 16:23:58 -08001423 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001424
Adam Cohen558baaf2011-08-15 15:22:57 -07001425 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001426 if (info == null) {
1427 return;
1428 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001429 final View view = createShortcut(info);
1430
Adam Cohenfbba09b2011-07-18 21:43:05 -07001431 // First we check if we already know the exact location where we want to add this item.
1432 if (cellX >= 0 && cellY >= 0) {
1433 cellXY[0] = cellX;
1434 cellXY[1] = cellY;
1435 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001436
1437 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001438 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001439 true, null,null)) {
1440 return;
1441 }
1442 DragObject dragObject = new DragObject();
1443 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001444 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1445 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001446 return;
1447 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001448 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001449 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001450 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001451 foundCellSpan = (result != null);
1452 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001453 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001454 }
1455
1456 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001457 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001458 return;
1459 }
1460
Adam Cohendcd297f2013-06-18 13:13:40 -07001461 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
1463 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001464 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467 }
1468
Adam Cohen2f093b62012-04-30 18:59:53 -07001469 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1470 int minHeight) {
1471 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001472 // We want to account for the extra amount of padding that we are adding to the widget
1473 // to ensure that it gets the full amount of space that it has requested
1474 int requiredWidth = minWidth + padding.left + padding.right;
1475 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001476 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001477 }
1478
Adam Cohen2f093b62012-04-30 18:59:53 -07001479 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1480 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001481 }
1482
Adam Cohen2f093b62012-04-30 18:59:53 -07001483 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1484 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001485 }
1486
Adam Cohen2f093b62012-04-30 18:59:53 -07001487 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1488 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001489 }
1490
Adam Cohen2f093b62012-04-30 18:59:53 -07001491 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1492 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001493 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001494 }
1495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001497 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001499 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001500 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001503 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1504 if (appWidgetInfo == null) {
1505 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1506 }
Romain Guycbb89e42009-06-08 15:52:54 -07001507
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001508 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001510
Adam Cohen2f093b62012-04-30 18:59:53 -07001511 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1512 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001515 // We have saved the position to which the widget was dragged-- this really only matters
1516 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 int[] cellXY = mTmpAddItemCellCoordinates;
1518 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001519 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1522 cellXY[0] = mPendingAddInfo.cellX;
1523 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001524 spanXY[0] = mPendingAddInfo.spanX;
1525 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 foundCellSpan = true;
1527 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001528 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001529 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001530 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1531 spanXY[1], cellXY, finalSpan);
1532 spanXY[0] = finalSpan[0];
1533 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001536 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001537 }
1538
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001540 if (appWidgetId != -1) {
1541 // Deleting an app widget ID is a void call but writes to disk before returning
1542 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001543 new AsyncTask<Void, Void, Void>() {
1544 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001545 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001546 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001547 }
Michael Jurka43467462013-11-14 12:03:58 +01001548 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001549 }
Winson Chung93eef082012-03-23 15:59:27 -07001550 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001551 return;
1552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001554 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001555 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1556 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 launcherInfo.spanX = spanXY[0];
1558 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001559 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1560 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564
1565 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 if (hostView == null) {
1567 // Perform actual inflation because we're live
1568 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1569 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1570 } else {
1571 // The AppWidgetHostView has already been inflated and instantiated
1572 launcherInfo.hostView = hostView;
1573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001576 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001577 launcherInfo.notifyWidgetSizeChanged(this);
1578
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001580 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001581
1582 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001584 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Adam Cohended9f8d2010-11-03 13:25:16 -07001587 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1588 @Override
1589 public void onReceive(Context context, Intent intent) {
1590 final String action = intent.getAction();
1591 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1592 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001593 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001595
Winson Chungc100e8e2011-08-09 16:02:43 -07001596 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001597 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001598 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001599 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001600 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1602 mUserPresent = true;
1603 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001604 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1605 mModel.resetLoadedState(false, true);
1606 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1607 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1608 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1609 mModel.resetLoadedState(false, true);
1610 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1611 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1612 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001613 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1614 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1615 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 }
1617 }
1618 };
1619
1620 @Override
1621 public void onAttachedToWindow() {
1622 super.onAttachedToWindow();
1623
1624 // Listen for broadcasts related to user-presence
1625 final IntentFilter filter = new IntentFilter();
1626 filter.addAction(Intent.ACTION_SCREEN_OFF);
1627 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001628 // For handling managed profiles
1629 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1630 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001631 if (ENABLE_DEBUG_INTENTS) {
1632 filter.addAction(DebugIntents.DELETE_DATABASE);
1633 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1634 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001636 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001637 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001638 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001639 mVisible = true;
1640 }
1641
Adam Cohen0b395352014-06-09 22:54:36 +00001642 /**
1643 * Sets up transparent navigation and status bars in LMP.
1644 * This method is a no-op for other platform versions.
1645 */
1646 @TargetApi(19)
1647 private void setupTransparentSystemBarsForLmp() {
1648 // TODO(sansid): use the APIs directly when compiling against L sdk.
1649 // Currently we use reflection to access the flags and the API to set the transparency
1650 // on the System bars.
1651 if (Utilities.isLmp()) {
1652 try {
1653 getWindow().getAttributes().systemUiVisibility |=
1654 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1655 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1656 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1657 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1658 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1659 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1660 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1661 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1662
1663 Method setStatusBarColorMethod =
1664 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1665 Method setNavigationBarColorMethod =
1666 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1667 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1668 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1669 } catch (NoSuchFieldException e) {
1670 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1671 } catch (NoSuchMethodException ex) {
1672 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1673 } catch (IllegalAccessException e) {
1674 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1675 } catch (IllegalArgumentException e) {
1676 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1677 } catch (InvocationTargetException e) {
1678 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1679 } finally {}
1680 }
1681 }
1682
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 @Override
1684 public void onDetachedFromWindow() {
1685 super.onDetachedFromWindow();
1686 mVisible = false;
1687
Adam Cohend113e0c2010-11-11 10:48:05 -08001688 if (mAttached) {
1689 unregisterReceiver(mReceiver);
1690 mAttached = false;
1691 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 updateRunning();
1693 }
1694
1695 public void onWindowVisibilityChanged(int visibility) {
1696 mVisible = visibility == View.VISIBLE;
1697 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001698 // The following code used to be in onResume, but it turns out onResume is called when
1699 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1700 // is a more appropriate event to handle
1701 if (mVisible) {
1702 mAppsCustomizeTabHost.onWindowVisible();
1703 if (!mWorkspaceLoading) {
1704 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001705 // We want to let Launcher draw itself at least once before we force it to build
1706 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001707 // apps is nice and speedy.
1708 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001709 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001710 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001711 if (mStarted) return;
1712 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001713 // We delay the layer building a bit in order to give
1714 // other message processing a time to run. In particular
1715 // this avoids a delay in hiding the IME if it was
1716 // currently shown, because doing that may involve
1717 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001718 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001719 final ViewTreeObserver.OnDrawListener listener = this;
1720 mWorkspace.post(new Runnable() {
1721 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001722 if (mWorkspace != null &&
1723 mWorkspace.getViewTreeObserver() != null) {
1724 mWorkspace.getViewTreeObserver().
1725 removeOnDrawListener(listener);
1726 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001727 }
1728 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001729 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001730 }
1731 });
1732 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001733 // When Launcher comes back to foreground, a different Activity might be responsible for
1734 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001735 if (!DISABLE_MARKET_BUTTON) {
1736 updateAppMarketIcon();
1737 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001738 clearTypedText();
1739 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741
1742 private void sendAdvanceMessage(long delay) {
1743 mHandler.removeMessages(ADVANCE_MSG);
1744 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1745 mHandler.sendMessageDelayed(msg, delay);
1746 mAutoAdvanceSentTime = System.currentTimeMillis();
1747 }
1748
1749 private void updateRunning() {
1750 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1751 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1752 mAutoAdvanceRunning = autoAdvanceRunning;
1753 if (autoAdvanceRunning) {
1754 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1755 sendAdvanceMessage(delay);
1756 } else {
1757 if (!mWidgetsToAdvance.isEmpty()) {
1758 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1759 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1760 }
1761 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001762 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 }
1764 }
1765 }
1766
1767 private final Handler mHandler = new Handler() {
1768 @Override
1769 public void handleMessage(Message msg) {
1770 if (msg.what == ADVANCE_MSG) {
1771 int i = 0;
1772 for (View key: mWidgetsToAdvance.keySet()) {
1773 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1774 final int delay = mAdvanceStagger * i;
1775 if (v instanceof Advanceable) {
1776 postDelayed(new Runnable() {
1777 public void run() {
1778 ((Advanceable) v).advance();
1779 }
1780 }, delay);
1781 }
1782 i++;
1783 }
1784 sendAdvanceMessage(mAdvanceInterval);
1785 }
1786 }
1787 };
1788
1789 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001790 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1792 if (v instanceof Advanceable) {
1793 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001794 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 updateRunning();
1796 }
1797 }
1798
1799 void removeWidgetToAutoAdvance(View hostView) {
1800 if (mWidgetsToAdvance.containsKey(hostView)) {
1801 mWidgetsToAdvance.remove(hostView);
1802 updateRunning();
1803 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001804 }
1805
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 launcherInfo.hostView = null;
1809 }
1810
Winson Chung93eef082012-03-23 15:59:27 -07001811 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1812 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1813 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001814 }
1815
Winson Chunga6945242014-01-08 14:04:34 -08001816 public DragLayer getDragLayer() {
1817 return mDragLayer;
1818 }
1819
1820 public Workspace getWorkspace() {
1821 return mWorkspace;
1822 }
1823
1824 public Hotseat getHotseat() {
1825 return mHotseat;
1826 }
1827
Jorim Jaggid017f882014-01-14 17:08:48 -08001828 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001829 return mOverviewPanel;
1830 }
1831
1832 public SearchDropTargetBar getSearchBar() {
1833 return mSearchDropTargetBar;
1834 }
1835
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001836 public LauncherAppWidgetHost getAppWidgetHost() {
1837 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 }
Romain Guycbb89e42009-06-08 15:52:54 -07001839
Winson Chunga9abd0e2010-10-27 17:18:37 -07001840 public LauncherModel getModel() {
1841 return mModel;
1842 }
1843
Winson Chunga6945242014-01-08 14:04:34 -08001844 public LauncherClings getLauncherClings() {
1845 return mLauncherClings;
1846 }
1847
1848 protected SharedPreferences getSharedPrefs() {
1849 return mSharedPrefs;
1850 }
1851
Bjorn Bringertc459e522013-06-07 19:36:01 +01001852 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001853 getWindow().closeAllPanels();
1854
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001855 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001856 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001857 }
1858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 @Override
1860 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001861 long startTime = 0;
1862 if (DEBUG_RESUME_TIME) {
1863 startTime = System.currentTimeMillis();
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 super.onNewIntent(intent);
1866
1867 // Close the menu
1868 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001869 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001870 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001871
Adam Cohened307df2013-10-02 09:37:31 -07001872 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1873 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1874 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001875
Adam Cohen6fecd412013-10-02 17:41:50 -07001876 if (mWorkspace == null) {
1877 // Can be cases where mWorkspace is null, this prevents a NPE
1878 return;
1879 }
1880 Folder openFolder = mWorkspace.getOpenFolder();
1881 // In all these cases, only animate if we're already on home
1882 mWorkspace.exitWidgetResizeMode();
1883 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001884 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001886 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001887
Adam Cohen6fecd412013-10-02 17:41:50 -07001888 closeFolder();
1889 exitSpringLoadedDragMode();
1890
1891 // If we are already on home, then just animate back to the workspace,
1892 // otherwise, just wait until onResume to set the state back to Workspace
1893 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001894 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001895 } else {
1896 mOnResumeState = State.WORKSPACE;
1897 }
1898
1899 final View v = getWindow().peekDecorView();
1900 if (v != null && v.getWindowToken() != null) {
1901 InputMethodManager imm = (InputMethodManager)getSystemService(
1902 INPUT_METHOD_SERVICE);
1903 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1904 }
1905
1906 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001907 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001908 mAppsCustomizeTabHost.reset();
1909 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001910
1911 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Adam Cohened307df2013-10-02 09:37:31 -07001913
Michael Jurka447bf842013-05-15 14:52:15 +02001914 if (DEBUG_RESUME_TIME) {
1915 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
1918
Adam Coppa120b8e2013-11-12 16:26:04 +00001919 /**
1920 * Override point for subclasses to prevent movement to the default screen when the home
1921 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1922 */
1923 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1924 return true;
1925 }
1926
1927 /**
1928 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1929 */
1930 protected void onHomeIntent() {
1931 // Do nothing
1932 }
1933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001935 public void onRestoreInstanceState(Bundle state) {
1936 super.onRestoreInstanceState(state);
1937 for (int page: mSynchronouslyBoundPages) {
1938 mWorkspace.restoreInstanceStateForChild(page);
1939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941
1942 @Override
1943 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001944 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001945 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1946 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001947 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001948 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
Michael Jurka883f55b2010-10-21 15:47:14 -07001950 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001951 // We close any open folder since it will not be re-opened, and we need to make sure
1952 // this state is reflected.
1953 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Adam Cohendcd297f2013-06-18 13:13:40 -07001955 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 mWaitingForResult) {
1957 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001958 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001959 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1960 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001961 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1962 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1963 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001964 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966
1967 if (mFolderInfo != null && mWaitingForResult) {
1968 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1969 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1970 }
Michael Jurka946ad472010-07-09 18:05:18 -07001971
Winson Chung785d2eb2011-04-14 16:08:02 -07001972 // Save the current AppsCustomize tab
1973 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001974 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1975 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001976 if (currentTabTag != null) {
1977 outState.putString("apps_customize_currentTab", currentTabTag);
1978 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001979 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1980 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001981 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001982 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 @Override
1986 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001988
Winson Chunge7a03942011-08-05 15:05:12 -07001989 // Remove all pending runnables
1990 mHandler.removeMessages(ADVANCE_MSG);
1991 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001992 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001993
Winson Chungcd2b0142011-06-08 16:02:26 -07001994 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001995 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001996 mModel.stopLoader();
1997 app.setLauncher(null);
1998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002000 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002002 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002004 mAppWidgetHost = null;
2005
2006 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007
2008 TextKeyListener.getInstance().release();
2009
Winson Chung81b52252012-08-27 15:34:29 -07002010 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2011 // to prevent leaking Launcher activities on orientation change.
2012 if (mModel != null) {
2013 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2014 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002015
2016 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002017 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002018
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002019 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002020 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002021 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002022 mWorkspace = null;
2023 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002024
Michael Jurka2ecf9952012-06-18 12:52:28 -07002025 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027
Adam Cohena9cf38f2011-05-02 15:36:58 -07002028 public DragController getDragController() {
2029 return mDragController;
2030 }
2031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 @Override
2033 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002034 if (requestCode >= 0) {
2035 setWaitingForResult(true);
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 super.startActivityForResult(intent, requestCode);
2038 }
2039
Winson Chung70d72102011-08-12 11:24:35 -07002040 /**
2041 * Indicates that we want global search for this activity by setting the globalSearch
2042 * argument for {@link #startSearch} to true.
2043 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002045 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002047
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002048 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002049
Karl Rosaen138a0412009-04-23 19:00:21 -07002050 if (initialQuery == null) {
2051 // Use any text typed in the launcher as the initial query
2052 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 if (appSearchData == null) {
2055 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002056 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
Winson Chungadf0c182012-08-23 14:59:07 -07002058 Rect sourceBounds = new Rect();
2059 if (mSearchDropTargetBar != null) {
2060 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2061 }
Romain Guycbb89e42009-06-08 15:52:54 -07002062
Bjorn Bringertc459e522013-06-07 19:36:01 +01002063 startSearch(initialQuery, selectInitialQuery,
2064 appSearchData, sourceBounds);
2065 }
2066
2067 public void startSearch(String initialQuery,
2068 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002069 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002071 }
2072
2073 /**
2074 * Starts the global search activity. This code is a copied from SearchManager
2075 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002078 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002079 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2080 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2081 if (globalSearchActivity == null) {
2082 Log.w(TAG, "No global search activity found.");
2083 return;
2084 }
2085 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2086 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2087 intent.setComponent(globalSearchActivity);
2088 // Make sure that we have a Bundle to put source in
2089 if (appSearchData == null) {
2090 appSearchData = new Bundle();
2091 } else {
2092 appSearchData = new Bundle(appSearchData);
2093 }
2094 // Set source to package name of app that starts global search, if not set already.
2095 if (!appSearchData.containsKey("source")) {
2096 appSearchData.putString("source", getPackageName());
2097 }
2098 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2099 if (!TextUtils.isEmpty(initialQuery)) {
2100 intent.putExtra(SearchManager.QUERY, initialQuery);
2101 }
2102 if (selectInitialQuery) {
2103 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2104 }
2105 intent.setSourceBounds(sourceBounds);
2106 try {
2107 startActivity(intent);
2108 } catch (ActivityNotFoundException ex) {
2109 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
2112
Yura4f93ec62014-02-04 14:15:21 +00002113 public boolean isOnCustomContent() {
2114 return mWorkspace.isOnOrMovingToCustomContent();
2115 }
2116
Winson Chung70d72102011-08-12 11:24:35 -07002117 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002118 public boolean onPrepareOptionsMenu(Menu menu) {
2119 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002120 if (!isOnCustomContent()) {
2121 // Close any open folders
2122 closeFolder();
2123 // Stop resizing any widgets
2124 mWorkspace.exitWidgetResizeMode();
2125 if (!mWorkspace.isInOverviewMode()) {
2126 // Show the overview mode
2127 showOverviewMode(true);
2128 } else {
2129 showWorkspace(true);
2130 }
Winson Chunge0298742014-01-17 12:03:00 -08002131 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002132 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 @Override
2136 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002137 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002138 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002139 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002140 }
2141
Joe Onorato9c1289c2009-08-17 11:03:03 -04002142 public boolean isWorkspaceLocked() {
2143 return mWorkspaceLoading || mWaitingForResult;
2144 }
2145
Adam Cohen517a7f52014-03-01 12:12:59 -08002146 public boolean isWorkspaceLoading() {
2147 return mWorkspaceLoading;
2148 }
2149
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002150 private void setWorkspaceLoading(boolean value) {
2151 boolean isLocked = isWorkspaceLocked();
2152 mWorkspaceLoading = value;
2153 if (isLocked != isWorkspaceLocked()) {
2154 onWorkspaceLockedChanged();
2155 }
2156 }
2157
2158 private void setWaitingForResult(boolean value) {
2159 boolean isLocked = isWorkspaceLocked();
2160 mWaitingForResult = value;
2161 if (isLocked != isWorkspaceLocked()) {
2162 onWorkspaceLockedChanged();
2163 }
2164 }
2165
2166 protected void onWorkspaceLockedChanged() { }
2167
Michael Jurka0280c3b2010-09-17 15:00:07 -07002168 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002170 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002171 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2172 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002173 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002174 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002176
Adam Cohenad4e15c2013-10-17 16:21:35 -07002177 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2178 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2179 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2180 }
2181
2182 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2183 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2184 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002185 if (appWidgetInfo.configure != null) {
2186 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002187 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002188
Bjorn Bringert7984c942009-12-09 15:38:25 +00002189 // Launch over to configure widget, if needed
2190 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002191 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002192 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002193 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002195 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002196 Runnable onComplete = new Runnable() {
2197 @Override
2198 public void run() {
2199 // Exit spring loaded mode if necessary after adding the widget
2200 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2201 null);
2202 }
2203 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002204 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002206 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208 }
Romain Guycbb89e42009-06-08 15:52:54 -07002209
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002210 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002211 // Close any folders that may be open.
2212 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002213 mWorkspace.moveToCustomContentScreen(animate);
2214 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002215 /**
2216 * Process a shortcut drop.
2217 *
2218 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002220 * @param cell The cell it should be added to, optional
2221 * @param position The location on the screen where it was dropped, optional
2222 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002224 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002225 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002226 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229
2230 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 mPendingAddInfo.cellX = cell[0];
2232 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234
2235 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2236 createShortcutIntent.setComponent(componentName);
2237 processShortcut(createShortcutIntent);
2238 }
2239
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 /**
2241 * Process a widget drop.
2242 *
2243 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 * @param cell The cell it should be added to, optional
2246 * @param position The location on the screen where it was dropped, optional
2247 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002249 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002254 mPendingAddInfo.minSpanX = info.minSpanX;
2255 mPendingAddInfo.minSpanY = info.minSpanY;
2256
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.cellX = cell[0];
2259 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002261 if (span != null) {
2262 mPendingAddInfo.spanX = span[0];
2263 mPendingAddInfo.spanY = span[1];
2264 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265
Adam Cohened66b2b2012-01-23 17:28:51 -08002266 AppWidgetHostView hostView = info.boundWidget;
2267 int appWidgetId;
2268 if (hostView != null) {
2269 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002270 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002271 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002272 // In this case, we either need to start an activity to get permission to bind
2273 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002274 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002275 Bundle options = info.bindOptions;
2276
2277 boolean success = false;
2278 if (options != null) {
2279 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2280 info.componentName, options);
2281 } else {
2282 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2283 info.componentName);
2284 }
2285 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002286 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002287 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002288 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002289 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2290 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2291 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002292 // TODO: we need to make sure that this accounts for the options bundle.
2293 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002294 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2295 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 }
2298
Joe Onoratodeb98af2010-02-19 14:59:39 -08002299 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002300 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
2302
Winson Chung24ab2f12010-09-16 14:10:47 -07002303 void processWallpaper(Intent intent) {
2304 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2305 }
2306
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002309 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 folderInfo.title = getText(R.string.folder_name);
2311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002313 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002315 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316
2317 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 FolderIcon newFolder =
2319 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002322 // Force measure the new folder icon
2323 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2324 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002325 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 }
Romain Guycbb89e42009-06-08 15:52:54 -07002327
Joe Onorato9c1289c2009-08-17 11:03:03 -04002328 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002329 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002330 }
2331
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002332 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002333 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002334 }
2335
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002336 /**
2337 * Registers various content observers. The current implementation registers
2338 * only a favorites observer to keep track of the favorites applications.
2339 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002340 private void registerContentObservers() {
2341 ContentResolver resolver = getContentResolver();
2342 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2343 true, mWidgetObserver);
2344 }
2345
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 @Override
2347 public boolean dispatchKeyEvent(KeyEvent event) {
2348 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2349 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002351 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002352 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002353 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002354 dumpState();
2355 return true;
2356 }
2357 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002358 }
2359 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2360 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002361 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 return true;
2363 }
2364 }
2365
2366 return super.dispatchKeyEvent(event);
2367 }
2368
Joe Onorato88ec0992009-11-19 13:16:06 -08002369 @Override
2370 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002371 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002372 if (mAppsCustomizeContent.getContentType() ==
2373 AppsCustomizePagedView.ContentType.Applications) {
2374 showWorkspace(true);
2375 } else {
2376 showOverviewMode(true);
2377 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002378 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002379 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002380 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002381 Folder openFolder = mWorkspace.getOpenFolder();
2382 if (openFolder.isEditingName()) {
2383 openFolder.dismissEditingName();
2384 } else {
2385 closeFolder();
2386 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002387 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002388 mWorkspace.exitWidgetResizeMode();
2389
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002390 // Back button is a no-op here, but give at least some feedback for the button press
2391 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002392 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002393 }
2394
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002396 * Re-listen when widgets are reset.
2397 */
2398 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002399 if (mAppWidgetHost != null) {
2400 mAppWidgetHost.startListening();
2401 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002402 }
2403
2404 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 * Launches the intent referred by the clicked shortcut.
2406 *
2407 * @param v The view representing the clicked shortcut.
2408 */
2409 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002410 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2411 // view has detached (it's possible for this to happen if the view is removed mid touch).
2412 if (v.getWindowToken() == null) {
2413 return;
2414 }
2415
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002416 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002417 return;
2418 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002419
Adam Cohen1697b792013-09-17 19:08:21 -07002420 if (v instanceof Workspace) {
2421 if (mWorkspace.isInOverviewMode()) {
2422 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002423 }
2424 return;
2425 }
2426
2427 if (v instanceof CellLayout) {
2428 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002429 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002430 }
2431 }
2432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002434 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002435 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002437 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002438 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002439 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002440 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002441 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
2443 }
2444
Michael Jurka0e260592010-06-30 17:07:39 -07002445 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002446 return false;
2447 }
2448
Michael Jurkaaf442092010-06-10 17:01:57 -07002449 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002450 * Event handler for the search button
2451 *
2452 * @param v The view that was clicked.
2453 */
2454 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002455 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2456
Amith Yamasania135ba82011-08-09 17:42:01 -07002457 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002458 }
2459
2460 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002461 * Event handler for the voice button
2462 *
2463 * @param v The view that was clicked.
2464 */
2465 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002466 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002467
Bjorn Bringertc459e522013-06-07 19:36:01 +01002468 startVoice();
2469 }
2470
2471 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002472 try {
2473 final SearchManager searchManager =
2474 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2475 ComponentName activityName = searchManager.getGlobalSearchActivity();
2476 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002477 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002478 if (activityName != null) {
2479 intent.setPackage(activityName.getPackageName());
2480 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002481 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002482 } catch (ActivityNotFoundException e) {
2483 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002484 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002485 startActivitySafely(null, intent, "onClickVoiceButton");
2486 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002487 }
2488
2489 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002490 * Event handler for the "grid" button that appears on the home screen, which
2491 * enters all apps mode.
2492 *
2493 * @param v The view that was clicked.
2494 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002495 protected void onClickAllAppsButton(View v) {
2496 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2497 if (isAllAppsVisible()) {
2498 showWorkspace(true);
2499 } else {
2500 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2501 }
2502 }
2503
2504 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002505 * Event handler for a paged view icon click.
2506 * @param v The view that was clicked.
2507 * @param appInfo The {link AppInfo} of the view.
2508 */
2509 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2510 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2511 startActivitySafely(v, appInfo.intent, appInfo);
2512 getStats().recordLaunch(appInfo.intent);
2513 }
2514
2515 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 * Event handler for an app shortcut click.
2517 *
2518 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2519 */
2520 protected void onClickAppShortcut(View v) {
2521 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2522 Object tag = v.getTag();
2523 if (!(tag instanceof ShortcutInfo)) {
2524 throw new IllegalArgumentException("Input must be a Shortcut");
2525 }
2526
2527 // Open shortcut
2528 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2529 final Intent intent = shortcut.intent;
2530
2531 // Check for special shortcuts
2532 if (intent.getComponent() != null) {
2533 final String shortcutClass = intent.getComponent().getClassName();
2534
2535 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2536 MemoryDumpActivity.startDump(this);
2537 return;
2538 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2539 toggleShowWeightWatcher();
2540 return;
2541 }
2542 }
2543
2544 // Start activities
2545 int[] pos = new int[2];
2546 v.getLocationOnScreen(pos);
2547 intent.setSourceBounds(new Rect(pos[0], pos[1],
2548 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2549
2550 boolean success = startActivitySafely(v, intent, tag);
2551
2552 mStats.recordLaunch(intent, shortcut);
2553
2554 if (success && v instanceof BubbleTextView) {
2555 mWaitingForResume = (BubbleTextView) v;
2556 mWaitingForResume.setStayPressed(true);
2557 }
2558 }
2559
2560 /**
2561 * Event handler for a folder icon click.
2562 *
2563 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2564 */
2565 protected void onClickFolderIcon(View v) {
2566 if (LOGD) Log.d(TAG, "onClickFolder");
2567 if (!(v instanceof FolderIcon)){
2568 throw new IllegalArgumentException("Input must be a FolderIcon");
2569 }
2570
2571 FolderIcon folderIcon = (FolderIcon) v;
2572 final FolderInfo info = folderIcon.getFolderInfo();
2573 Folder openFolder = mWorkspace.getFolderForTag(info);
2574
2575 // If the folder info reports that the associated folder is open, then verify that
2576 // it is actually opened. There have been a few instances where this gets out of sync.
2577 if (info.opened && openFolder == null) {
2578 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2579 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2580 info.opened = false;
2581 }
2582
2583 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2584 // Close any open folder
2585 closeFolder();
2586 // Open the requested folder
2587 openFolder(folderIcon);
2588 } else {
2589 // Find the open folder...
2590 int folderScreen;
2591 if (openFolder != null) {
2592 folderScreen = mWorkspace.getPageForView(openFolder);
2593 // .. and close it
2594 closeFolder(openFolder);
2595 if (folderScreen != mWorkspace.getCurrentPage()) {
2596 // Close any folder open on the current screen
2597 closeFolder();
2598 // Pull the folder onto this screen
2599 openFolder(folderIcon);
2600 }
2601 }
2602 }
Michael Jurka5130e402011-10-13 04:55:35 -07002603 }
2604
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002605 /**
2606 * Event handler for the (Add) Widgets button that appears after a long press
2607 * on the home screen.
2608 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002609 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002611 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2612 }
2613
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002614 /**
2615 * Event handler for the wallpaper picker button that appears after a long press
2616 * on the home screen.
2617 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002618 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2620 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2621 pickWallpaper.setComponent(getWallpaperPickerComponent());
2622 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2623 }
2624
2625 /**
2626 * Event handler for a click on the settings button that appears after a long press
2627 * on the home screen.
2628 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002629 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2631 }
2632
Michael Jurka5130e402011-10-13 04:55:35 -07002633 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002634 // Provide the same haptic feedback that the system offers for virtual keys.
2635 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002636 }
2637
Adam Cohen61f560d2013-09-30 15:58:20 -07002638 public void performHapticFeedbackOnTouchDown(View v) {
2639 // Provide the same haptic feedback that the system offers for virtual keys.
2640 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2641 }
2642
2643 public View.OnTouchListener getHapticFeedbackTouchListener() {
2644 if (mHapticFeedbackTouchListener == null) {
2645 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2646 @Override
2647 public boolean onTouch(View v, MotionEvent event) {
2648 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2649 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2650 }
2651 return false;
2652 }
2653 };
2654 }
2655 return mHapticFeedbackTouchListener;
2656 }
2657
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002658 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002659 if (!DISABLE_MARKET_BUTTON) {
2660 if (mAppMarketIntent != null) {
2661 startActivitySafely(v, mAppMarketIntent, "app market");
2662 } else {
2663 Log.e(TAG, "Invalid app market intent.");
2664 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002665 }
2666 }
2667
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002668 public void onDragStarted(View view) {}
2669
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002670 /**
2671 * Called when the user stops interacting with the launcher.
2672 * This implies that the user is now on the homescreen and is not doing housekeeping.
2673 */
2674 protected void onInteractionEnd() {}
2675
2676 /**
2677 * Called when the user starts interacting with the launcher.
2678 * The possible interactions are:
2679 * - open all apps
2680 * - reorder an app shortcut, or a widget
2681 * - open the overview mode.
2682 * This is a good time to stop doing things that only make sense
2683 * when the user is on the homescreen and not doing housekeeping.
2684 */
2685 protected void onInteractionBegin() {}
2686
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002687 void startApplicationDetailsActivity(ComponentName componentName) {
2688 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002689 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2690 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002691 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2692 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002693 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002694 }
2695
Michael Jurka1e2f4652013-07-08 18:03:46 -07002696 // returns true if the activity was started
2697 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002698 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002699 // System applications cannot be installed. For now, show a toast explaining that.
2700 // We may give them the option of disabling apps this way.
2701 int messageId = R.string.uninstall_system_app_text;
2702 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002703 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002704 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002705 String packageName = componentName.getPackageName();
2706 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002707 Intent intent = new Intent(
2708 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002709 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2710 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002711 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002712 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002713 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002714 }
2715
Michael Jurka86a720e2012-05-09 11:23:23 -07002716 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002717 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002719 // Only launch using the new animation if the shortcut has not opted out (this is a
2720 // private contract between launcher and may be ignored in the future).
2721 boolean useLaunchAnimation = (v != null) &&
2722 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002723 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2724 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002725
Kenny Guy1317e2d2014-05-08 18:52:50 +01002726 UserHandleCompat user = null;
2727 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2728 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2729 user = userManager.getUserForSerialNumber(serialNumber);
2730 }
2731
2732 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002733 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002734 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2735 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002736 optsBundle = opts.toBundle();
2737 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002738 if (useLaunchAnimation && Utilities.isLmp()) {
2739 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2740 optsBundle = opts.toBundle();
2741 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002742
2743 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2744 // Could be launching some bookkeeping activity
2745 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002746 } else {
Kenny Guy1317e2d2014-05-08 18:52:50 +01002747 launcherApps.startActivityForProfile(intent.getComponent(),
2748 intent.getSourceBounds(), optsBundle, user);
Winson Chungc7450e32012-04-17 17:34:08 -07002749 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002750 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 } catch (SecurityException e) {
2752 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002753 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002755 "or use the exported attribute for this activity. "
2756 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002758 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002760
Michael Jurka86a720e2012-05-09 11:23:23 -07002761 boolean startActivitySafely(View v, Intent intent, Object tag) {
2762 boolean success = false;
2763 try {
2764 success = startActivity(v, intent, tag);
2765 } catch (ActivityNotFoundException e) {
2766 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2767 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2768 }
2769 return success;
2770 }
2771
Adam Cohen268c4752012-06-06 17:47:33 -07002772 /**
2773 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2774 * in the DragLayer in the exact absolute location of the original FolderIcon.
2775 */
2776 private void copyFolderIconToImage(FolderIcon fi) {
2777 final int width = fi.getMeasuredWidth();
2778 final int height = fi.getMeasuredHeight();
2779
2780 // Lazy load ImageView, Bitmap and Canvas
2781 if (mFolderIconImageView == null) {
2782 mFolderIconImageView = new ImageView(this);
2783 }
2784 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2785 mFolderIconBitmap.getHeight() != height) {
2786 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2787 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2788 }
2789
2790 DragLayer.LayoutParams lp;
2791 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2792 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2793 } else {
2794 lp = new DragLayer.LayoutParams(width, height);
2795 }
2796
Adam Cohen307fe232012-08-16 17:55:58 -07002797 // The layout from which the folder is being opened may be scaled, adjust the starting
2798 // view size by this scale factor.
2799 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002800 lp.customPosition = true;
2801 lp.x = mRectForFolderAnimation.left;
2802 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002803 lp.width = (int) (scale * width);
2804 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002805
2806 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2807 fi.draw(mFolderIconCanvas);
2808 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002809 if (fi.getFolder() != null) {
2810 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2811 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002812 }
Adam Cohen268c4752012-06-06 17:47:33 -07002813 // Just in case this image view is still in the drag layer from a previous animation,
2814 // we remove it and re-add it.
2815 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2816 mDragLayer.removeView(mFolderIconImageView);
2817 }
2818 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002819 if (fi.getFolder() != null) {
2820 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002821 }
Adam Cohen268c4752012-06-06 17:47:33 -07002822 }
2823
Adam Cohen2801caf2011-05-13 20:57:39 -07002824 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002825 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002826 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2827 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2828 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2829
Adam Cohenc51934b2011-07-26 21:07:43 -07002830 FolderInfo info = (FolderInfo) fi.getTag();
2831 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2832 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002833 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2834 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002835 }
2836
Adam Cohen268c4752012-06-06 17:47:33 -07002837 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2838 copyFolderIconToImage(fi);
2839 fi.setVisibility(View.INVISIBLE);
2840
Michael Jurka2ecf9952012-06-18 12:52:28 -07002841 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002842 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002843 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2844 oa.start();
2845 }
2846
Adam Cohen268c4752012-06-06 17:47:33 -07002847 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002848 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002849 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2850 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2851 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2852
Adam Cohen268c4752012-06-06 17:47:33 -07002853 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002854
Adam Cohen268c4752012-06-06 17:47:33 -07002855 // We remove and re-draw the FolderIcon in-case it has changed
2856 mDragLayer.removeView(mFolderIconImageView);
2857 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002858 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002859 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002860 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002861 oa.addListener(new AnimatorListenerAdapter() {
2862 @Override
2863 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002864 if (cl != null) {
2865 cl.clearFolderLeaveBehind();
2866 // Remove the ImageView copy of the FolderIcon and make the original visible.
2867 mDragLayer.removeView(mFolderIconImageView);
2868 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002869 }
2870 }
2871 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002872 oa.start();
2873 }
2874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002876 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 * is animated relative to the specified View. If the View is null, no animation
2878 * is played.
2879 *
2880 * @param folderInfo The FolderInfo describing the folder to open.
2881 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002882 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002883 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002884 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885
Adam Cohena9cf38f2011-05-02 15:36:58 -07002886 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002887
Adam Cohen4554ee12011-08-03 16:13:21 -07002888 // Just verify that the folder hasn't already been added to the DragLayer.
2889 // There was a one-off crash where the folder had a parent already.
2890 if (folder.getParent() == null) {
2891 mDragLayer.addView(folder);
2892 mDragController.addDropTarget((DropTarget) folder);
2893 } else {
2894 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2895 folder.getParent() + ").");
2896 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002897 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002898 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002899
2900 // Notify the accessibility manager that this folder "window" has appeared and occluded
2901 // the workspace items
2902 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2903 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002904 }
2905
2906 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002907 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002908 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002909 if (folder.isEditingName()) {
2910 folder.dismissEditingName();
2911 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002912 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002913
2914 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002915 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002916 }
2917 }
2918
2919 void closeFolder(Folder folder) {
2920 folder.getInfo().opened = false;
2921
2922 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2923 if (parent != null) {
2924 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2925 shrinkAndFadeInFolderIcon(fi);
2926 }
2927 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002928
2929 // Notify the accessibility manager that this folder "window" has disappeard and no
2930 // longer occludeds the workspace items
2931 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002935 if (!isDraggingEnabled()) return false;
2936 if (isWorkspaceLocked()) return false;
2937 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938
Adam Cohen1697b792013-09-17 19:08:21 -07002939 if (v instanceof Workspace) {
2940 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002941 if (mWorkspace.enterOverviewMode()) {
2942 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2943 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2944 return true;
2945 } else {
2946 return false;
2947 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002948 } else {
2949 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002950 }
Adam Cohen1697b792013-09-17 19:08:21 -07002951 }
2952
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002953 CellLayout.CellInfo longClickCellInfo = null;
2954 View itemUnderLongClick = null;
2955 if (v.getTag() instanceof ItemInfo) {
2956 ItemInfo info = (ItemInfo) v.getTag();
2957 longClickCellInfo = new CellLayout.CellInfo(v, info);;
2958 itemUnderLongClick = longClickCellInfo.cell;
2959 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 }
2961
Winson Chung3d503fb2011-07-13 17:25:49 -07002962 // The hotseat touch handling does not go through Workspace, and we always allow long press
2963 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05002964 final boolean inHotseat = isHotseatLayout(v);
2965 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002966 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002967 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002968 // User long pressed on empty space
2969 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2970 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07002971 if (mWorkspace.isInOverviewMode()) {
2972 mWorkspace.startReordering(v);
2973 } else {
2974 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002977 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2978 mHotseat.getOrderInHotseat(
2979 longClickCellInfo.cellX,
2980 longClickCellInfo.cellY));
2981 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002983 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 }
2985 }
2986 }
2987 return true;
2988 }
2989
Winson Chung3d503fb2011-07-13 17:25:49 -07002990 boolean isHotseatLayout(View layout) {
2991 return mHotseat != null && layout != null &&
2992 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2993 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002994
Winson Chung3d503fb2011-07-13 17:25:49 -07002995 /**
2996 * Returns the CellLayout of the specified container at the specified screen.
2997 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002998 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002999 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3000 if (mHotseat != null) {
3001 return mHotseat.getLayout();
3002 } else {
3003 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003004 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003005 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003006 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003007 }
3008 }
3009
Daniel Sandler843e8602010-06-07 14:59:01 -04003010 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003011 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003012 }
3013
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003014 /**
3015 * Helper method for the cameraZoomIn/cameraZoomOut animations
3016 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003017 * @param scaleFactor The scale factor used for the zoom
3018 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003019 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003020 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003021 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003022 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003023
Craig Mautner360310b2012-10-26 15:13:08 -07003024 private void setWorkspaceBackground(boolean workspace) {
3025 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003026 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003027 }
3028
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003029 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003030 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3031 int curflags = getWindow().getAttributes().flags
3032 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3033 if (wpflags != curflags) {
3034 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3035 }
Craig Mautner360310b2012-10-26 15:13:08 -07003036 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003037 }
3038
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003039 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3040 if (v instanceof LauncherTransitionable) {
3041 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3042 }
3043 }
3044
Michael Jurkabed61d22012-02-14 22:51:29 -08003045 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3046 if (v instanceof LauncherTransitionable) {
3047 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3048 }
Winson Chung70442722012-02-10 15:43:22 -08003049
3050 // Update the workspace transition step as well
3051 dispatchOnLauncherTransitionStep(v, 0f);
3052 }
3053
3054 private void dispatchOnLauncherTransitionStep(View v, float t) {
3055 if (v instanceof LauncherTransitionable) {
3056 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3057 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003058 }
3059
3060 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3061 if (v instanceof LauncherTransitionable) {
3062 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3063 }
Winson Chung70442722012-02-10 15:43:22 -08003064
3065 // Update the workspace transition step as well
3066 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003067 }
3068
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003069 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003070 * Things to test when changing the following seven functions.
3071 * - Home from workspace
3072 * - from center screen
3073 * - from other screens
3074 * - Home from all apps
3075 * - from center screen
3076 * - from other screens
3077 * - Back from all apps
3078 * - from center screen
3079 * - from other screens
3080 * - Launch app from workspace and quit
3081 * - with back
3082 * - with home
3083 * - Launch app from all apps and quit
3084 * - with back
3085 * - with home
3086 * - Go to a screen that's not the default, then all
3087 * apps, and launch and app, and go back
3088 * - with back
3089 * -with home
3090 * - On workspace, long press power and go back
3091 * - with back
3092 * - with home
3093 * - On all apps, long press power and go back
3094 * - with back
3095 * - with home
3096 * - On workspace, power off
3097 * - On all apps, power off
3098 * - Launch an app and turn off the screen while in that app
3099 * - Go back with home key
3100 * - Go back with back key TODO: make this not go to workspace
3101 * - From all apps
3102 * - From workspace
3103 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3104 * - From all apps
3105 * - From the center workspace
3106 * - From another workspace
3107 */
3108
3109 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003110 * Zoom the camera out from the workspace to reveal 'toView'.
3111 * Assumes that the view to show is anchored at either the very top or very bottom
3112 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003113 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003114 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003115 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3116 showAppsCustomizeHelper(animated, springLoaded, contentType);
3117 }
3118 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3119 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003120 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003121 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003122 mStateAnimation.cancel();
3123 mStateAnimation = null;
3124 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003125 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003126
Winson Chungf0ea4d32011-06-06 14:27:16 -07003127 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3128 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3129 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003130 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003131 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003132 final int startDelay =
3133 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003134
Michael Jurkab3e22d92011-10-31 15:58:33 -07003135 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003136
Michael Jurkad74c9842011-07-10 12:44:21 -07003137 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003138 Animator workspaceAnim =
3139 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003140 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003141 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3142 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003143 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3144 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003145
3146 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003147 toView.setScaleX(scale);
3148 toView.setScaleY(scale);
3149 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3150 scaleAnim.
3151 scaleX(1f).scaleY(1f).
3152 setDuration(duration).
3153 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003154
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003156 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003157 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003158 .ofFloat(toView, "alpha", 0f, 1f)
3159 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003161 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3162 @Override
3163 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003164 if (animation == null) {
3165 throw new RuntimeException("animation is null");
3166 }
Winson Chung70442722012-02-10 15:43:22 -08003167 float t = (Float) animation.getAnimatedValue();
3168 dispatchOnLauncherTransitionStep(fromView, t);
3169 dispatchOnLauncherTransitionStep(toView, t);
3170 }
3171 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003172
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003173 // toView should appear right at the end of the workspace shrink
3174 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003175 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003176 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003177 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003178
3179 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003180 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003181 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003182 // Prepare the position
3183 toView.setTranslationX(0.0f);
3184 toView.setTranslationY(0.0f);
3185 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003186 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003187 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003188 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003189 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003190 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3191 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003192
Winson Chungc7d2b602012-05-16 17:02:20 -07003193 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003194 if (mSearchDropTargetBar != null) {
3195 mSearchDropTargetBar.hideSearchBar(false);
3196 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003197 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003198 });
3199
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003200 if (workspaceAnim != null) {
3201 mStateAnimation.play(workspaceAnim);
3202 }
Michael Jurka1899a362011-11-03 13:50:45 -07003203
3204 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003205
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003206 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3207 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003208
3209 // If any of the objects being animated haven't been measured/laid out
3210 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003211 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003212 (mWorkspace.getMeasuredWidth() == 0) ||
3213 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003214 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003215 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003216
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003217 final AnimatorSet stateAnimation = mStateAnimation;
3218 final Runnable startAnimRunnable = new Runnable() {
3219 public void run() {
3220 // Check that mStateAnimation hasn't changed while
3221 // we waited for a layout/draw pass
3222 if (mStateAnimation != stateAnimation)
3223 return;
3224 setPivotsForZoom(toView, scale);
3225 dispatchOnLauncherTransitionStart(fromView, animated, false);
3226 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003227 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003228 }
3229 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003230 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003231 final ViewTreeObserver observer = toView.getViewTreeObserver();
3232 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3233 public void onGlobalLayout() {
3234 startAnimRunnable.run();
3235 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3236 }
3237 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003238 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003239 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003240 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003241 } else {
3242 toView.setTranslationX(0.0f);
3243 toView.setTranslationY(0.0f);
3244 toView.setScaleX(1.0f);
3245 toView.setScaleY(1.0f);
3246 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003247 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003248
Daniel Sandlere4f98912013-06-25 15:13:26 -04003249 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003250 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003251 if (mSearchDropTargetBar != null) {
3252 mSearchDropTargetBar.hideSearchBar(false);
3253 }
Michael Jurkaabded662011-03-04 12:06:57 -08003254 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003255 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003256 dispatchOnLauncherTransitionStart(fromView, animated, false);
3257 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003258 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003259 dispatchOnLauncherTransitionStart(toView, animated, false);
3260 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003261 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003262 }
3263
3264 /**
3265 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003266 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003267 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003268 */
Adam Cohened307df2013-10-02 09:37:31 -07003269 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003270 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003271
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003273 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003274 mStateAnimation.cancel();
3275 mStateAnimation = null;
3276 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003277 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003278
Winson Chungf0ea4d32011-06-06 14:27:16 -07003279 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003280 final int fadeOutDuration =
3281 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003282 final float scaleFactor = (float)
3283 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3284 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003285 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003286 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003287 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003288 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3289 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003290 toState, animated, stagger, -1);
3291 } else if (toState == Workspace.State.SPRING_LOADED ||
3292 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003293 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003294 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003295 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003296
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003298 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003299 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003300 final LauncherViewPropertyAnimator scaleAnim =
3301 new LauncherViewPropertyAnimator(fromView);
3302 scaleAnim.
3303 scaleX(scaleFactor).scaleY(scaleFactor).
3304 setDuration(duration).
3305 setInterpolator(new Workspace.ZoomInInterpolator());
3306
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003307 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003308 .ofFloat(fromView, "alpha", 1f, 0f)
3309 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003310 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003311 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3312 @Override
3313 public void onAnimationUpdate(ValueAnimator animation) {
3314 float t = 1f - (Float) animation.getAnimatedValue();
3315 dispatchOnLauncherTransitionStep(fromView, t);
3316 dispatchOnLauncherTransitionStep(toView, t);
3317 }
3318 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003319
Michael Jurka2ecf9952012-06-18 12:52:28 -07003320 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003321
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003322 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3323 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003324 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003325
3326 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003327 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003328 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003329 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003330 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3331 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003332 if (onCompleteRunnable != null) {
3333 onCompleteRunnable.run();
3334 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003335 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003336 }
3337 });
3338
Winson Chungf0ea4d32011-06-06 14:27:16 -07003339 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003340 if (workspaceAnim != null) {
3341 mStateAnimation.play(workspaceAnim);
3342 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003343 dispatchOnLauncherTransitionStart(fromView, animated, true);
3344 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003345 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003346 } else {
3347 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003348 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003349 dispatchOnLauncherTransitionStart(fromView, animated, true);
3350 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003351 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003352 dispatchOnLauncherTransitionStart(toView, animated, true);
3353 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003354 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003355 }
3356
Michael Jurkae326f182011-11-21 14:05:46 -08003357 @Override
3358 public void onTrimMemory(int level) {
3359 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003360 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003361 mAppsCustomizeTabHost.onTrimMemory();
3362 }
3363 }
3364
Adam Cohened307df2013-10-02 09:37:31 -07003365 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003366 showWorkspace(animated, null);
3367 }
3368
Adam Cohened307df2013-10-02 09:37:31 -07003369 protected void showWorkspace() {
3370 showWorkspace(true);
3371 }
3372
Adam Cohened66b2b2012-01-23 17:28:51 -08003373 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003374 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003375 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003376 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003377 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378
Winson Chungc7d2b602012-05-16 17:02:20 -07003379 // Show the search bar (only animate if we were showing the drop target bar in spring
3380 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003381 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003382 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003383 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003384
Michael Jurkab3e22d92011-10-31 15:58:33 -07003385 // Set focus to the AppsCustomize button
3386 if (mAllAppsButton != null) {
3387 mAllAppsButton.requestFocus();
3388 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003389 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003390
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003391 // Change the state *after* we've called all the transition code
3392 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003393
Winson Chung5fb63472011-02-02 17:03:37 -08003394 // Resume the auto-advance of widgets
3395 mUserPresent = true;
3396 updateRunning();
3397
alanv1d4fde62012-10-17 13:15:47 -07003398 // Send an accessibility event to announce the context change
3399 getWindow().getDecorView()
3400 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003401
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003402 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003403 }
3404
Adam Cohened307df2013-10-02 09:37:31 -07003405 void showOverviewMode(boolean animated) {
3406 mWorkspace.setVisibility(View.VISIBLE);
3407 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3408 mState = State.WORKSPACE;
3409 onWorkspaceShown(animated);
3410 }
3411
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003412 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003413 }
3414
Winson Chung82963d52013-10-09 11:20:57 -07003415 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3416 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417 if (mState != State.WORKSPACE) return;
3418
Winson Chung82963d52013-10-09 11:20:57 -07003419 if (resetPageToZero) {
3420 mAppsCustomizeTabHost.reset();
3421 }
Winson Chungc58497e2013-09-03 17:48:37 -07003422 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423 mAppsCustomizeTabHost.requestFocus();
3424
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425 // Change the state *after* we've called all the transition code
3426 mState = State.APPS_CUSTOMIZE;
3427
3428 // Pause the auto-advance of widgets until we are out of AllApps
3429 mUserPresent = false;
3430 updateRunning();
3431 closeFolder();
3432
3433 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003434 getWindow().getDecorView()
3435 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003436 }
3437
Adam Cohen7777d962011-08-18 18:58:38 -07003438 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003439 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003440 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003441 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003442 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003443 }
Adam Cohen7777d962011-08-18 18:58:38 -07003444
Adam Cohenad4e15c2013-10-17 16:21:35 -07003445 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003446 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003447 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3448
Winson Chunge7a03942011-08-05 15:05:12 -07003449 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003450 @Override
3451 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003452 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003453 // Before we show workspace, hide all apps again because
3454 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3455 // clean up our state transition functions
3456 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003457 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003458 } else {
3459 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003460 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003461 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003462 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003463 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464
Michael Jurkad3ef3062010-11-23 16:23:58 -08003465 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003466 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003467 final boolean animated = true;
3468 final boolean springLoaded = true;
3469 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003470 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003471 }
3472 // Otherwise, we are not in spring loaded mode, so don't do anything.
3473 }
3474
Michael Jurkab3e22d92011-10-31 15:58:33 -07003475 void lockAllApps() {
3476 // TODO
3477 }
3478
3479 void unlockAllApps() {
3480 // TODO
3481 }
3482
Winson Chungf0ea4d32011-06-06 14:27:16 -07003483 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003484 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003485 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003486 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003487 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003488 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003489 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003490 int duration = 0;
3491 if (mSearchDropTargetBar != null) {
3492 duration = mSearchDropTargetBar.getTransitionInDuration();
3493 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003494 mHotseat.animate().alpha(1f).setDuration(duration);
3495 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003497 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003498 }
3499 }
3500 }
3501
3502 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003503 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003504 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003505 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003506 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003507 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003508 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003509 int duration = 0;
3510 if (mSearchDropTargetBar != null) {
3511 duration = mSearchDropTargetBar.getTransitionOutDuration();
3512 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003513 mHotseat.animate().alpha(0f).setDuration(duration);
3514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003515 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003516 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003518 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003519 }
3520
Patrick Dubroy5f445422011-02-18 14:35:21 -08003521 /**
3522 * Add an item from all apps or customize onto the given workspace screen.
3523 * If layout is null, add to the current screen.
3524 */
3525 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003526 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003527 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003528 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003529 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003530
Winson Chungdff8ebb2011-09-08 17:25:31 -07003531 /** Maps the current orientation to an index for referencing orientation correct global icons */
3532 private int getCurrentOrientationIndexForGlobalIcons() {
3533 // default - 0, landscape - 1
3534 switch (getResources().getConfiguration().orientation) {
3535 case Configuration.ORIENTATION_LANDSCAPE:
3536 return 1;
3537 default:
3538 return 0;
3539 }
3540 }
3541
Michael Jurkaae65ee32012-04-30 11:45:54 -07003542 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003543 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003544 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003545 // Look for the toolbar icon specified in the activity meta-data
3546 Bundle metaData = packageManager.getActivityInfo(
3547 activityName, PackageManager.GET_META_DATA).metaData;
3548 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003549 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003550 if (iconResId != 0) {
3551 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003552 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003553 }
3554 }
3555 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003556 // This can happen if the activity defines an invalid drawable
3557 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3558 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003559 } catch (Resources.NotFoundException nfe) {
3560 // This can happen if the activity defines an invalid drawable
3561 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3562 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003563 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003564 return null;
3565 }
3566
3567 // if successful in getting icon, return it; otherwise, set button to use default drawable
3568 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003569 int buttonId, ComponentName activityName, int fallbackDrawableId,
3570 String toolbarResourceName) {
3571 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003572 Resources r = getResources();
3573 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3574 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003575
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003576 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003577 // If we were unable to find the icon via the meta-data, use a generic one
3578 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003579 toolbarIcon = r.getDrawable(fallbackDrawableId);
3580 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003581 if (button != null) {
3582 button.setCompoundDrawables(toolbarIcon, null, null, null);
3583 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003584 return null;
3585 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003586 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003587 if (button != null) {
3588 button.setCompoundDrawables(toolbarIcon, null, null, null);
3589 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003590 return toolbarIcon.getConstantState();
3591 }
3592 }
3593
3594 // if successful in getting icon, return it; otherwise, set button to use default drawable
3595 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003596 int buttonId, ComponentName activityName, int fallbackDrawableId,
3597 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003598 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003599 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003600
Michael Jurka19e0fc52011-07-22 18:00:21 -07003601 if (button != null) {
3602 // If we were unable to find the icon via the meta-data, use a
3603 // generic one
3604 if (toolbarIcon == null) {
3605 button.setImageResource(fallbackDrawableId);
3606 } else {
3607 button.setImageDrawable(toolbarIcon);
3608 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003609 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003610
3611 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3612
Michael Jurka0423dcf2010-10-05 14:56:18 -07003613 }
3614
Winson Chung1b884092012-06-08 17:13:02 -07003615 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003616 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003617 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003618 }
3619
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003620 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003621 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003622 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003623 }
3624
Winson Chungbb185bd2011-11-21 12:31:42 -08003625 private void invalidatePressedFocusedStates(View container, View button) {
3626 if (container instanceof HolographicLinearLayout) {
3627 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3628 layout.invalidatePressedFocusedStates();
3629 } else if (button instanceof HolographicImageView) {
3630 HolographicImageView view = (HolographicImageView) button;
3631 view.invalidatePressedFocusedStates();
3632 }
3633 }
3634
Cristina Stancu476493b2013-08-07 17:20:14 +01003635 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003636 if (mQsb == null) {
3637 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3638 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003639 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003640 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003641 }
3642
3643 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003644 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003645 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003646 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003647 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003648
Winson Chung1cad91e2011-05-25 17:41:01 -07003649 final SearchManager searchManager =
3650 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3651 ComponentName activityName = searchManager.getGlobalSearchActivity();
3652 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003653 int coi = getCurrentOrientationIndexForGlobalIcons();
3654 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003655 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3656 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3657 if (sGlobalSearchIcon[coi] == null) {
3658 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3659 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3660 TOOLBAR_ICON_METADATA_NAME);
3661 }
3662
Winson Chungc51db6a2011-10-05 11:44:49 -07003663 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3664 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003665 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003666 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003667 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003668 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003669 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3670 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003671 if (searchButton != null) searchButton.setVisibility(View.GONE);
3672 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003673 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003674 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003675 }
3676 }
3677
Cristina Stancu476493b2013-08-07 17:20:14 +01003678 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003679 final View searchButtonContainer = findViewById(R.id.search_button_container);
3680 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003681 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003682 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003683 }
3684
Cristina Stancu476493b2013-08-07 17:20:14 +01003685 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003686 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003687 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003688
Winson Chungc51db6a2011-10-05 11:44:49 -07003689 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003690 final SearchManager searchManager =
3691 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3692 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3693
3694 ComponentName activityName = null;
3695 if (globalSearchActivity != null) {
3696 // Check if the global search activity handles voice search
3697 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3698 intent.setPackage(globalSearchActivity.getPackageName());
3699 activityName = intent.resolveActivity(getPackageManager());
3700 }
3701
3702 if (activityName == null) {
3703 // Fallback: check if an activity other than the global search activity
3704 // resolves this
3705 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3706 activityName = intent.resolveActivity(getPackageManager());
3707 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003708 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003709 int coi = getCurrentOrientationIndexForGlobalIcons();
3710 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003711 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3712 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3713 if (sVoiceSearchIcon[coi] == null) {
3714 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3715 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3716 TOOLBAR_ICON_METADATA_NAME);
3717 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003718 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003719 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003720 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003721 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003722 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003723 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003724 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003725 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003726 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003727 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003728 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003729 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003730
Cristina Stancu476493b2013-08-07 17:20:14 +01003731 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003732 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3733 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003734 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003735 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003736 }
3737
Winson Chung5841efa2013-09-30 18:06:44 -07003738 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003739 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3740 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003741 boolean visible = !forceDisableVoiceButtonProxy &&
3742 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003743 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003744 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003745 }
3746 }
Winson Chung5841efa2013-09-30 18:06:44 -07003747
3748 /**
3749 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3750 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3751 */
3752 public void disableVoiceButtonProxy(boolean disabled) {
3753 updateVoiceButtonProxyVisible(disabled);
3754 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003755 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003756 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003757 */
3758 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003759 if (!DISABLE_MARKET_BUTTON) {
3760 final View marketButton = findViewById(R.id.market_button);
3761 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3762 // Find the app market activity by resolving an intent.
3763 // (If multiple app markets are installed, it will return the ResolverActivity.)
3764 ComponentName activityName = intent.resolveActivity(getPackageManager());
3765 if (activityName != null) {
3766 int coi = getCurrentOrientationIndexForGlobalIcons();
3767 mAppMarketIntent = intent;
3768 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3769 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3770 TOOLBAR_ICON_METADATA_NAME);
3771 marketButton.setVisibility(View.VISIBLE);
3772 } else {
3773 // We should hide and disable the view so that we don't try and restore the visibility
3774 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3775 marketButton.setVisibility(View.GONE);
3776 marketButton.setEnabled(false);
3777 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003778 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003779 }
3780
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003781 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003782 if (!DISABLE_MARKET_BUTTON) {
3783 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3784 Resources r = getResources();
3785 Drawable marketIconDrawable = d.newDrawable(r);
3786 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3787 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3788 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003789
Winson Chungd64a6662013-09-30 11:06:59 -07003790 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3791 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003792 }
3793
Michael Jurkad7c28052012-04-27 15:43:36 -07003794 @Override
3795 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003796 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003797 final List<CharSequence> text = event.getText();
3798 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003799 // Populate event with a fake title based on the current state.
3800 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003801 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003802 } else {
3803 text.add(getString(R.string.all_apps_home_button_label));
3804 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003805 return result;
3806 }
3807
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003808 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003809 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003810 */
3811 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3812 @Override
3813 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003814 closeSystemDialogs();
3815 }
3816 }
3817
3818 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003819 * Receives notifications whenever the appwidgets are reset.
3820 */
3821 private class AppWidgetResetObserver extends ContentObserver {
3822 public AppWidgetResetObserver() {
3823 super(new Handler());
3824 }
3825
3826 @Override
3827 public void onChange(boolean selfChange) {
3828 onAppWidgetReset();
3829 }
3830 }
3831
3832 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003833 * If the activity is currently paused, signal that we need to run the passed Runnable
3834 * in onResume.
3835 *
3836 * This needs to be called from incoming places where resources might have been loaded
3837 * while we are paused. That is becaues the Configuration might be wrong
3838 * when we're not running, and if it comes back to what it was when we
3839 * were paused, we are not restarted.
3840 *
3841 * Implementation of the method from LauncherModel.Callbacks.
3842 *
3843 * @return true if we are currently paused. The caller might be able to
3844 * skip some work in that case since we will come back again.
3845 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003846 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 if (mPaused) {
3848 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003849 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003850 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003851 }
3852 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003853 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003854 return true;
3855 } else {
3856 return false;
3857 }
3858 }
3859
Michael Jurkac402cd92013-05-20 15:49:32 +02003860 private boolean waitUntilResume(Runnable run) {
3861 return waitUntilResume(run, false);
3862 }
3863
Michael Jurka1e2f4652013-07-08 18:03:46 -07003864 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003865 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003866 }
3867
Michael Jurka7607c2f2013-04-03 14:33:19 -07003868 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003869 * If the activity is currently paused, signal that we need to re-run the loader
3870 * in onResume.
3871 *
3872 * This needs to be called from incoming places where resources might have been loaded
3873 * while we are paused. That is becaues the Configuration might be wrong
3874 * when we're not running, and if it comes back to what it was when we
3875 * were paused, we are not restarted.
3876 *
3877 * Implementation of the method from LauncherModel.Callbacks.
3878 *
3879 * @return true if we are currently paused. The caller might be able to
3880 * skip some work in that case since we will come back again.
3881 */
3882 public boolean setLoadOnResume() {
3883 if (mPaused) {
3884 Log.i(TAG, "setLoadOnResume");
3885 mOnResumeNeedsLoad = true;
3886 return true;
3887 } else {
3888 return false;
3889 }
3890 }
3891
3892 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003893 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003894 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003896 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003897 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003898 } else {
3899 return SCREEN_COUNT / 2;
3900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003902
Joe Onorato9c1289c2009-08-17 11:03:03 -04003903 /**
3904 * Refreshes the shortcuts shown on the workspace.
3905 *
3906 * Implementation of the method from LauncherModel.Callbacks.
3907 */
3908 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003909 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003910
Michael Jurka7607c2f2013-04-03 14:33:19 -07003911 // If we're starting binding all over again, clear any bind calls we'd postponed in
3912 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3913 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003914 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003915
Winson Chungd64d1762013-08-20 14:37:16 -07003916 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003917 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003918 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003919
Michael Jurka05bf644e2011-11-30 20:28:53 -08003920 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003921 if (mHotseat != null) {
3922 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003923 }
3924 }
3925
Adam Cohendcd297f2013-06-18 13:13:40 -07003926 @Override
3927 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003928 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003929
Adam Cohen5084cba2013-09-03 12:01:16 -07003930 // If there are no screens, we need to have an empty screen
3931 if (orderedScreenIds.size() == 0) {
3932 mWorkspace.addExtraEmptyScreen();
3933 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003934
3935 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003936 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003937 if (hasCustomContentToLeft()) {
3938 mWorkspace.createCustomContentContainer();
3939 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003940 }
Winson Chung64359a52013-07-08 17:17:08 -07003941 }
3942
3943 @Override
3944 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003945 // Log to disk
3946 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3947 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3948 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003949 int count = orderedScreenIds.size();
3950 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003951 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003952 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003953 }
3954
Adam Cohen39a06042013-07-19 14:30:12 -07003955 private boolean shouldShowWeightWatcher() {
3956 String spKey = LauncherAppState.getSharedPreferencesKey();
3957 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003958 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003959
3960 return show;
3961 }
3962
3963 private void toggleShowWeightWatcher() {
3964 String spKey = LauncherAppState.getSharedPreferencesKey();
3965 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3966 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3967
3968 show = !show;
3969
3970 SharedPreferences.Editor editor = sp.edit();
3971 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3972 editor.commit();
3973
3974 if (mWeightWatcher != null) {
3975 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3976 }
3977 }
3978
Winson Chungd64d1762013-08-20 14:37:16 -07003979 public void bindAppsAdded(final ArrayList<Long> newScreens,
3980 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003981 final ArrayList<ItemInfo> addAnimated,
3982 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003983 Runnable r = new Runnable() {
3984 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003985 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003986 }
3987 };
3988 if (waitUntilResume(r)) {
3989 return;
3990 }
3991
Winson Chungd64d1762013-08-20 14:37:16 -07003992 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003993 if (newScreens != null) {
3994 bindAddScreens(newScreens);
3995 }
Winson Chungd64d1762013-08-20 14:37:16 -07003996
3997 // We add the items without animation on non-visible pages, and with
3998 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003999 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004000 bindItems(addNotAnimated, 0,
4001 addNotAnimated.size(), false);
4002 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004003 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004004 bindItems(addAnimated, 0,
4005 addAnimated.size(), true);
4006 }
Winson Chungc58497e2013-09-03 17:48:37 -07004007
Winson Chung87412982013-10-03 18:34:14 -07004008 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004009 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004010
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004011 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004012 addedApps != null && mAppsCustomizeContent != null) {
4013 mAppsCustomizeContent.addApps(addedApps);
4014 }
Winson Chungd64d1762013-08-20 14:37:16 -07004015 }
4016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004017 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004018 * Bind the items start-end from the list.
4019 *
4020 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004021 */
Winson Chung64359a52013-07-08 17:17:08 -07004022 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4023 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004024 Runnable r = new Runnable() {
4025 public void run() {
4026 bindItems(shortcuts, start, end, forceAnimateIcons);
4027 }
4028 };
4029 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004030 return;
4031 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004032
Winson Chungf0c6ae02012-03-21 16:10:31 -07004033 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004034 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4035 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004036 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004037 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004038 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004039 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004041
4042 // Short circuit if we are loading dock items for a configuration which has no dock
4043 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4044 mHotseat == null) {
4045 continue;
4046 }
4047
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 switch (item.itemType) {
4049 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4050 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004051 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004052 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004053
Winson Chung64359a52013-07-08 17:17:08 -07004054 /*
4055 * TODO: FIX collision case
4056 */
Winson Chungce376632013-07-11 16:12:41 -07004057 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4058 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4059 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004060 View v = cl.getChildAt(item.cellX, item.cellY);
4061 Object tag = v.getTag();
4062 String desc = "Collision while binding workspace item: " + item
4063 + ". Collides with " + tag;
4064 if (LauncherAppState.isDogfoodBuild()) {
4065 throw (new RuntimeException(desc));
4066 } else {
4067 Log.d(TAG, desc);
4068 }
Winson Chungce376632013-07-11 16:12:41 -07004069 }
Winson Chung64359a52013-07-08 17:17:08 -07004070 }
4071
Adam Cohendcd297f2013-06-18 13:13:40 -07004072 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4073 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004074 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004075 // Animate all the applications up now
4076 shortcut.setAlpha(0f);
4077 shortcut.setScaleX(0f);
4078 shortcut.setScaleY(0f);
4079 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004080 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004081 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004083 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004084 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004085 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004086 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004087 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4088 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004090 default:
4091 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004093 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004094
Winson Chung997a9232013-07-24 15:33:46 -07004095 if (animateIcons) {
4096 // Animate to the correct page
4097 if (newShortcutsScreenId > -1) {
4098 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004099 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004100 final Runnable startBounceAnimRunnable = new Runnable() {
4101 public void run() {
4102 anim.playTogether(bounceAnims);
4103 anim.start();
4104 }
4105 };
Winson Chung997a9232013-07-24 15:33:46 -07004106 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004107 // We post the animation slightly delayed to prevent slowdowns
4108 // when we are loading right after we return to launcher.
4109 mWorkspace.postDelayed(new Runnable() {
4110 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004111 if (mWorkspace != null) {
4112 mWorkspace.snapToPage(newScreenIndex);
4113 mWorkspace.postDelayed(startBounceAnimRunnable,
4114 NEW_APPS_ANIMATION_DELAY);
4115 }
Winson Chung94d67682013-09-25 16:29:40 -07004116 }
4117 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004118 } else {
4119 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004120 }
4121 }
Winson Chung64359a52013-07-08 17:17:08 -07004122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004124 }
4125
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 /**
4127 * Implementation of the method from LauncherModel.Callbacks.
4128 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004129 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004130 Runnable r = new Runnable() {
4131 public void run() {
4132 bindFolders(folders);
4133 }
4134 };
4135 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004136 return;
4137 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004138 sFolders.clear();
4139 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004140 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141
4142 /**
4143 * Add the views for a widget to the workspace.
4144 *
4145 * Implementation of the method from LauncherModel.Callbacks.
4146 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004147 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004148 Runnable r = new Runnable() {
4149 public void run() {
4150 bindAppWidget(item);
4151 }
4152 };
4153 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004154 return;
4155 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004156
Daniel Sandler843e8602010-06-07 14:59:01 -04004157 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4158 if (DEBUG_WIDGETS) {
4159 Log.d(TAG, "bindAppWidget: " + item);
4160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161 final Workspace workspace = mWorkspace;
4162
4163 final int appWidgetId = item.appWidgetId;
4164 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004165 if (DEBUG_WIDGETS) {
4166 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4167 }
4168
Joe Onorato9c1289c2009-08-17 11:03:03 -04004169 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4170
Joe Onorato9c1289c2009-08-17 11:03:03 -04004171 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004172 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004173
Adam Cohendcd297f2013-06-18 13:13:40 -07004174 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004176 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4177
Joe Onorato9c1289c2009-08-17 11:03:03 -04004178 workspace.requestLayout();
4179
Daniel Sandler843e8602010-06-07 14:59:01 -04004180 if (DEBUG_WIDGETS) {
4181 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4182 + (SystemClock.uptimeMillis()-start) + "ms");
4183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 }
4185
Adam Cohen1462de32012-07-24 22:34:36 -07004186 public void onPageBoundSynchronously(int page) {
4187 mSynchronouslyBoundPages.add(page);
4188 }
4189
Joe Onorato9c1289c2009-08-17 11:03:03 -04004190 /**
4191 * Callback saying that there aren't any more items to bind.
4192 *
4193 * Implementation of the method from LauncherModel.Callbacks.
4194 */
Adam Cohene25af792013-06-06 23:08:25 -07004195 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004196 Runnable r = new Runnable() {
4197 public void run() {
4198 finishBindingItems(upgradePath);
4199 }
4200 };
4201 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004202 return;
4203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004204 if (mSavedState != null) {
4205 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004206 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004207 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 mSavedState = null;
4209 }
4210
Adam Cohen1462de32012-07-24 22:34:36 -07004211 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004212
Winson Chungc51db6a2011-10-05 11:44:49 -07004213 // Update the market app icon as necessary (the other icons will be managed in response to
4214 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004215 if (!DISABLE_MARKET_BUTTON) {
4216 updateAppMarketIcon();
4217 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004218
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004219 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004220
4221 // If we received the result of any pending adds while the loader was running (e.g. the
4222 // widget configuration forced an orientation change), process them now.
4223 if (sPendingAddItem != null) {
4224 final long screenId = completeAdd(sPendingAddItem);
4225
4226 // TODO: this moves the user to the page where the pending item was added. Ideally,
4227 // the screen would be guaranteed to exist after bind, and the page would be set through
4228 // the workspace restore process.
4229 mWorkspace.post(new Runnable() {
4230 @Override
4231 public void run() {
4232 mWorkspace.snapToScreenId(screenId);
4233 }
4234 });
4235 sPendingAddItem = null;
4236 }
4237
Adam Cohene25af792013-06-06 23:08:25 -07004238 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004239 mWorkspace.getUniqueComponents(true, null);
4240 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004241 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004242 }
4243
Winson Chunga0b7e862013-09-05 16:03:15 -07004244 public boolean isAllAppsButtonRank(int rank) {
4245 if (mHotseat != null) {
4246 return mHotseat.isAllAppsButtonRank(rank);
4247 }
4248 return false;
4249 }
4250
Winson Chunga2413752012-04-03 14:22:34 -07004251 private boolean canRunNewAppsAnimation() {
4252 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4253 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4254 }
4255
Winson Chungc9168342013-06-26 14:54:55 -07004256 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4257 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4258 PropertyValuesHolder.ofFloat("alpha", 1f),
4259 PropertyValuesHolder.ofFloat("scaleX", 1f),
4260 PropertyValuesHolder.ofFloat("scaleY", 1f));
4261 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4262 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4263 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4264 return bounceAnim;
4265 }
4266
Adam Cohen27772732013-11-11 14:00:56 +00004267 public boolean useVerticalBarLayout() {
4268 return LauncherAppState.getInstance().getDynamicGrid().
4269 getDeviceProfile().isVerticalBarLayout();
4270 }
4271
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004272 protected Rect getSearchBarBounds() {
4273 return LauncherAppState.getInstance().getDynamicGrid().
4274 getDeviceProfile().getSearchBarBounds();
4275 }
4276
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004277 @Override
4278 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004279 boolean searchVisible = updateGlobalSearchIcon();
4280 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004281 if (mSearchDropTargetBar != null) {
4282 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4283 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004284 }
4285
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004286 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 * Add the icons for all apps.
4288 *
4289 * Implementation of the method from LauncherModel.Callbacks.
4290 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004291 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004292 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004293 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4294 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4295 getHotseat().addAllAppsFolder(mIconCache, apps,
4296 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4297 }
4298 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004299 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004300 if (mAppsCustomizeContent != null) {
4301 mAppsCustomizeContent.onPackagesUpdated(
4302 LauncherModel.getSortedWidgetsAndShortcuts(this));
4303 }
Winson Chungc58497e2013-09-03 17:48:37 -07004304 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004305 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004306 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004307 mAppsCustomizeContent.onPackagesUpdated(
4308 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004309 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 }
4312
4313 /**
4314 * A package was updated.
4315 *
4316 * Implementation of the method from LauncherModel.Callbacks.
4317 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004318 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004319 Runnable r = new Runnable() {
4320 public void run() {
4321 bindAppsUpdated(apps);
4322 }
4323 };
4324 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004325 return;
4326 }
4327
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004328 if (mWorkspace != null) {
4329 mWorkspace.updateShortcuts(apps);
4330 }
Winson Chungc58497e2013-09-03 17:48:37 -07004331
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004332 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004333 mAppsCustomizeContent != null) {
4334 mAppsCustomizeContent.updateApps(apps);
4335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004336 }
4337
4338 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004339 * Update the state of a package, typically related to install state.
4340 *
4341 * Implementation of the method from LauncherModel.Callbacks.
4342 */
4343 public void updatePackageState(String pkgName, int state) {
4344 if (mWorkspace != null) {
4345 mWorkspace.updatePackageState(pkgName, state);
4346 }
4347 }
4348
4349 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004350 * A package was uninstalled. We take both the super set of packageNames
4351 * in addition to specific applications to remove, the reason being that
4352 * this can be called when a package is updated as well. In that scenario,
4353 * we only remove specific components from the workspace, where as
4354 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004355 *
4356 * Implementation of the method from LauncherModel.Callbacks.
4357 */
Winson Chung83892cc2013-05-01 16:53:33 -07004358 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004359 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004360 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004361 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004362 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004363 }
Winson Chungd64d1762013-08-20 14:37:16 -07004364 };
4365 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004366 return;
4367 }
4368
Winson Chungdf95eb12013-10-16 14:57:07 -07004369 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004370 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004371 }
4372 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004373 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004374 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004375
Winson Chunga1820962011-10-03 16:31:06 -07004376 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004377 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004378
Winson Chungdf95eb12013-10-16 14:57:07 -07004379 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004380 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004381 mAppsCustomizeContent != null) {
4382 mAppsCustomizeContent.removeApps(appInfos);
4383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004384 }
Joe Onoratobe386092009-11-17 17:32:16 -08004385
4386 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004387 * A number of packages were updated.
4388 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004389 private ArrayList<Object> mWidgetsAndShortcuts;
4390 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004391 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004392 bindPackagesUpdated(mWidgetsAndShortcuts);
4393 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004394 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004395 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004396 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4397 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4398 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004399 return;
4400 }
4401
Winson Chung64359a52013-07-08 17:17:08 -07004402 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004403 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004404 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004405 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004406 }
4407
Winson Chung400438b2011-01-16 17:53:48 -08004408 private int mapConfigurationOriActivityInfoOri(int configOri) {
4409 final Display d = getWindowManager().getDefaultDisplay();
4410 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4411 switch (d.getRotation()) {
4412 case Surface.ROTATION_0:
4413 case Surface.ROTATION_180:
4414 // We are currently in the same basic orientation as the natural orientation
4415 naturalOri = configOri;
4416 break;
4417 case Surface.ROTATION_90:
4418 case Surface.ROTATION_270:
4419 // We are currently in the other basic orientation to the natural orientation
4420 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4421 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4422 break;
4423 }
4424
4425 int[] oriMap = {
4426 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4427 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4428 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4429 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4430 };
4431 // Since the map starts at portrait, we need to offset if this device's natural orientation
4432 // is landscape.
4433 int indexOffset = 0;
4434 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4435 indexOffset = 1;
4436 }
4437 return oriMap[(d.getRotation() + indexOffset) % 4];
4438 }
Adam Cohen446e9402011-09-15 18:21:21 -07004439
Winson Chung4b919f82012-05-01 10:44:08 -07004440 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004441 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004442 getResources().getBoolean(R.bool.allow_rotation);
4443 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004444 }
Winson Chung4b919f82012-05-01 10:44:08 -07004445 public void lockScreenOrientation() {
4446 if (isRotationEnabled()) {
4447 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4448 .getConfiguration().orientation));
4449 }
4450 }
4451 public void unlockScreenOrientation(boolean immediate) {
4452 if (isRotationEnabled()) {
4453 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004454 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004455 } else {
4456 mHandler.postDelayed(new Runnable() {
4457 public void run() {
4458 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4459 }
4460 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004461 }
Winson Chung4b919f82012-05-01 10:44:08 -07004462 }
Winson Chung400438b2011-01-16 17:53:48 -08004463 }
4464
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004465 /**
4466 * Called when the SearchBar hint should be changed.
4467 *
4468 * @param hint the hint to be displayed in the search bar.
4469 */
4470 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004471 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004472 }
4473
Winson Chunge43a1e72014-01-15 10:33:02 -08004474 protected boolean isLauncherPreinstalled() {
4475 PackageManager pm = getPackageManager();
4476 try {
4477 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4478 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4479 return true;
4480 } else {
4481 return false;
4482 }
4483 } catch (NameNotFoundException e) {
4484 e.printStackTrace();
4485 return false;
4486 }
4487 }
4488
Adam Cohenea90f832014-05-21 15:03:34 -07004489 /**
4490 * This method indicates whether or not we should suggest default wallpaper dimensions
4491 * when our wallpaper cropper was not yet used to set a wallpaper.
4492 */
4493 protected boolean overrideWallpaperDimensions() {
4494 return true;
4495 }
4496
Adam Cohen5eed5d82014-05-19 13:12:13 -07004497 protected boolean shouldClingFocusHotseatApp() {
4498 return false;
4499 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004500 protected String getFirstRunClingSearchBarHint() {
4501 return "";
4502 }
4503 protected String getFirstRunCustomContentHint() {
4504 return "";
4505 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004506 protected int getFirstRunFocusedHotseatAppDrawableId() {
4507 return -1;
4508 }
4509 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4510 return null;
4511 }
4512 protected int getFirstRunFocusedHotseatAppRank() {
4513 return -1;
4514 }
4515 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4516 return "";
4517 }
4518 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4519 return "";
4520 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004521
Winson Chungaf40f202013-09-18 18:26:31 -07004522 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004523 mLauncherClings.dismissFirstRunCling(v);
4524 }
4525 public void dismissMigrationClingCopyApps(View v) {
4526 mLauncherClings.dismissMigrationClingCopyApps(v);
4527 }
4528 public void dismissMigrationClingUseDefault(View v) {
4529 mLauncherClings.dismissMigrationClingUseDefault(v);
4530 }
4531 public void dismissMigrationWorkspaceCling(View v) {
4532 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004533 }
Winson Chung82f55532011-08-09 14:14:23 -07004534 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004535 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004536 }
4537 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004538 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004539 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004540 public void markFolderClingDismissedIfNecessary() {
4541 mLauncherClings.markFolderClingDismissedIfNecessary();
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543
4544 /**
4545 * To be overridden by subclasses to indicate that there is an activity to launch
4546 * before showing the standard launcher experience.
4547 */
4548 protected boolean hasFirstRunActivity() {
4549 return false;
4550 }
4551
4552 /**
4553 * To be overridden by subclasses to launch any first run activity
4554 */
4555 protected Intent getFirstRunActivity() {
4556 return null;
4557 }
4558
Winson Chunga6945242014-01-08 14:04:34 -08004559 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004560 return !ActivityManager.isRunningInTestHarness() &&
4561 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004562 }
4563
Adam Cohen4a9423d2014-03-28 14:22:31 -07004564 protected boolean hasRunFirstRunActivity() {
4565 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4566 }
4567
Adam Cohen432609a2014-03-13 17:03:22 -07004568 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004569 if (shouldRunFirstRunActivity() &&
4570 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004571 Intent firstRunIntent = getFirstRunActivity();
4572 if (firstRunIntent != null) {
4573 startActivity(firstRunIntent);
4574 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004575 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004576 }
4577 }
Adam Cohen432609a2014-03-13 17:03:22 -07004578 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004579 }
4580
4581 private void markFirstRunActivityShown() {
4582 SharedPreferences.Editor editor = mSharedPrefs.edit();
4583 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4584 editor.apply();
4585 }
4586
Adam Cohen432609a2014-03-13 17:03:22 -07004587 /**
4588 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4589 * screen that must be displayed and dismissed.
4590 */
4591 protected boolean hasDismissableIntroScreen() {
4592 return false;
4593 }
4594
4595 /**
4596 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4597 */
4598 protected View getIntroScreen() {
4599 return null;
4600 }
4601
4602 /**
4603 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4604 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4605 */
4606 private boolean shouldShowIntroScreen() {
4607 return hasDismissableIntroScreen() &&
4608 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4609 }
4610
4611 protected void showIntroScreen() {
4612 View introScreen = getIntroScreen();
4613 changeWallpaperVisiblity(false);
4614 if (introScreen != null) {
4615 mDragLayer.showOverlayView(introScreen);
4616 }
4617 }
4618
4619 public void dismissIntroScreen() {
4620 markIntroScreenDismissed();
4621 if (showFirstRunActivity()) {
4622 // We delay hiding the intro view until the first run activity is showing. This
4623 // avoids a blip.
4624 mWorkspace.postDelayed(new Runnable() {
4625 @Override
4626 public void run() {
4627 mDragLayer.dismissOverlayView();
4628 }
4629 }, ACTIVITY_START_DELAY);
4630 } else {
4631 mDragLayer.dismissOverlayView();
4632 }
4633 changeWallpaperVisiblity(true);
4634 }
4635
4636 private void markIntroScreenDismissed() {
4637 SharedPreferences.Editor editor = mSharedPrefs.edit();
4638 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4639 editor.apply();
4640 }
4641
Winson Chunga6945242014-01-08 14:04:34 -08004642 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004643 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4644 if (mHotseat != null) mHotseat.setAlpha(1f);
4645 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4646 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004647 }
4648
4649 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004650 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4651 if (mHotseat != null) mHotseat.setAlpha(0f);
4652 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4653 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004654 }
4655
Mathew Inwood72fbec12013-11-19 15:45:07 +00004656 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004657 // Called from search suggestion, not supported in other profiles.
4658 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4659 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4660 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4661 myUser);
4662 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004663 return null;
4664 }
Kenny Guyed131872014-04-30 03:02:21 +01004665 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004666 }
4667
4668 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4669 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004670 // Called from search suggestion, not supported in other profiles.
4671 return createShortcutDragInfo(shortcutIntent, caption, icon,
4672 UserHandleCompat.myUserHandle());
4673 }
4674
4675 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4676 Bitmap icon, UserHandleCompat user) {
4677 return new ShortcutInfo(shortcutIntent, caption, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 }
4679
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004680 protected void moveWorkspaceToDefaultScreen() {
4681 mWorkspace.moveToDefaultScreen(false);
4682 }
4683
Mathew Inwood72fbec12013-11-19 15:45:07 +00004684 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4685 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004686 mWorkspace.onExternalDragStartedWithItem(dragView);
4687 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004688 }
4689
Anjali Koppalf5d29132014-02-28 13:33:27 -08004690 @Override
4691 public void onPageSwitch(View newPage, int newPageIndex) {
4692 }
4693
Winson Chung80baf5a2010-08-09 16:03:15 -07004694 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004695 * Prints out out state for debugging.
4696 */
4697 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004698 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004699 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004700 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4701 Log.d(TAG, "mRestoring=" + mRestoring);
4702 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4703 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004704 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004705 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004706
Winson Chung785d2eb2011-04-14 16:08:02 -07004707 if (mAppsCustomizeContent != null) {
4708 mAppsCustomizeContent.dumpState();
4709 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004710 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004711 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004712
4713 @Override
4714 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4715 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004716 synchronized (sDumpLogs) {
4717 writer.println(" ");
4718 writer.println("Debug logs: ");
4719 for (int i = 0; i < sDumpLogs.size(); i++) {
4720 writer.println(" " + sDumpLogs.get(i));
4721 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004722 }
4723 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004724
4725 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004726 if (DEBUG_DUMP_LOG) {
4727 synchronized (sDumpLogs) {
4728 Log.d(TAG, "");
4729 Log.d(TAG, "*********************");
4730 Log.d(TAG, "Launcher debug logs: ");
4731 for (int i = 0; i < sDumpLogs.size(); i++) {
4732 Log.d(TAG, " " + sDumpLogs.get(i));
4733 }
4734 Log.d(TAG, "*********************");
4735 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004736 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004737 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004738 }
4739
4740 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004741 addDumpLog(tag, log, null, debugLog);
4742 }
4743
4744 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004746 if (e != null) {
4747 Log.d(tag, log, e);
4748 } else {
4749 Log.d(tag, log);
4750 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004751 }
Winson Chungede41292013-09-19 16:27:36 -07004752 if (DEBUG_DUMP_LOG) {
4753 sDateStamp.setTime(System.currentTimeMillis());
4754 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004755 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4756 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004757 }
Winson Chungede41292013-09-19 16:27:36 -07004758 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004759 }
4760
Winson Chungede41292013-09-19 16:27:36 -07004761 public void dumpLogsToLocalData() {
4762 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004763 new AsyncTask<Void, Void, Void>() {
4764 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004765 boolean success = false;
4766 sDateStamp.setTime(sRunStart);
4767 String FILENAME = sDateStamp.getMonth() + "-"
4768 + sDateStamp.getDay() + "_"
4769 + sDateStamp.getHours() + "-"
4770 + sDateStamp.getMinutes() + "_"
4771 + sDateStamp.getSeconds() + ".txt";
4772
4773 FileOutputStream fos = null;
4774 File outFile = null;
4775 try {
4776 outFile = new File(getFilesDir(), FILENAME);
4777 outFile.createNewFile();
4778 fos = new FileOutputStream(outFile);
4779 } catch (Exception e) {
4780 e.printStackTrace();
4781 }
4782 if (fos != null) {
4783 PrintWriter writer = new PrintWriter(fos);
4784
4785 writer.println(" ");
4786 writer.println("Debug logs: ");
4787 synchronized (sDumpLogs) {
4788 for (int i = 0; i < sDumpLogs.size(); i++) {
4789 writer.println(" " + sDumpLogs.get(i));
4790 }
4791 }
4792 writer.close();
4793 }
4794 try {
4795 if (fos != null) {
4796 fos.close();
4797 success = true;
4798 }
4799 } catch (IOException e) {
4800 e.printStackTrace();
4801 }
Michael Jurka43467462013-11-14 12:03:58 +01004802 return null;
Winson Chungede41292013-09-19 16:27:36 -07004803 }
Michael Jurka43467462013-11-14 12:03:58 +01004804 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004805 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004807}
Michael Jurka2763be32011-02-24 11:19:57 -08004808
Michael Jurkaabded662011-03-04 12:06:57 -08004809interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004810 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004811 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004812 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004813 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004814 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004815}
Dan Sandlerd5024042014-01-09 15:01:33 -05004816
4817interface DebugIntents {
4818 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4819 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004820}