blob: 60efcea6d43840d10ecad479693aac9e091e97cf [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 Sandler6053b802013-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
Winson Chung4a2afa32012-07-19 14:53:05 -0700289 // Keep track of whether the user has left launcher
290 private static boolean sPausedFromUserAction = false;
291
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private Bundle mSavedInstanceState;
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mUserPresent = true;
297 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700298 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static LocaleConfiguration sLocaleConfiguration = null;
302
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700303 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
Adam Cohen61f560d2013-09-30 15:58:20 -0700305 private View.OnTouchListener mHapticFeedbackTouchListener;
306
Adam Cohended9f8d2010-11-03 13:25:16 -0700307 // Related to the auto-advancing of widgets
308 private final int ADVANCE_MSG = 1;
309 private final int mAdvanceInterval = 20000;
310 private final int mAdvanceStagger = 250;
311 private long mAutoAdvanceSentTime;
312 private long mAutoAdvanceTimeLeft = -1;
313 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
314 new HashMap<View, AppWidgetProviderInfo>();
315
Winson Chung400438b2011-01-16 17:53:48 -0800316 // Determines how long to wait after a rotation before restoring the screen orientation to
317 // match the sensor state.
318 private final int mRestoreScreenOrientationDelay = 500;
319
Michael Jurka4ef207b2010-11-29 17:05:45 -0800320 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700321 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
322 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
323 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800324
Winson Chungd64a6662013-09-30 11:06:59 -0700325 private Intent mAppMarketIntent = null;
326 private static final boolean DISABLE_MARKET_BUTTON = true;
327
Craig Mautner360310b2012-10-26 15:13:08 -0700328 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Adam Cohene25af792013-06-06 23:08:25 -0700344 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
345
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 // Holds the page that we need to animate to, and the icon views that we need to animate up
347 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700348 private ImageView mFolderIconImageView;
349 private Bitmap mFolderIconBitmap;
350 private Canvas mFolderIconCanvas;
351 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700352
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 private Runnable mBuildLayersRunnable = new Runnable() {
356 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700357 if (mWorkspace != null) {
358 mWorkspace.buildPageHardwareLayers();
359 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 }
361 };
362
Adam Cohendb364c32014-05-20 14:23:40 -0700363 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364
Michael Jurka7267fa52013-09-26 15:29:57 -0700365 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800366
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 private static class PendingAddArguments {
368 int requestCode;
369 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700370 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700371 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 int cellX;
373 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700374 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 }
376
Daniel Sandlerff02d492013-08-05 02:12:05 -0400377 private Stats mStats;
378
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800379 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800380 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700381 }
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400402 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400403 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700404 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 // Determine the dynamic grid properties
406 Point smallestSize = new Point();
407 Point largestSize = new Point();
408 Point realSize = new Point();
409 Display display = getWindowManager().getDefaultDisplay();
410 display.getCurrentSizeRange(smallestSize, largestSize);
411 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700412 DisplayMetrics dm = new DisplayMetrics();
413 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 // Lazy-initialize the dynamic grid
416 DeviceProfile grid = app.initDynamicGrid(this,
417 Math.min(smallestSize.x, smallestSize.y),
418 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700419 realSize.x, realSize.y,
420 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700421
422 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400423 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700424 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800425 mModel = app.setLauncher(this);
426 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700427 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400428 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800429 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Daniel Sandlerff02d492013-08-05 02:12:05 -0400432 mStats = new Stats(this);
433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700435
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
437 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700439 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
440 // this also ensures that any synchronous binding below doesn't re-trigger another
441 // LauncherModel load.
442 mPaused = false;
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200445 android.os.Debug.startMethodTracing(
446 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448
Adam Cohen53805212013-10-01 10:39:23 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100454 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800456 registerContentObservers();
457
Joe Onorato7c312c12009-08-13 21:36:53 -0700458 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 mSavedState = savedInstanceState;
461 restoreState(mSavedState);
462
463 if (PROFILE_STARTUP) {
464 android.os.Debug.stopMethodTracing();
465 }
466
467 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700468 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 // If the user leaves launcher, then we should just load items asynchronously when
470 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500471 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 } else {
473 // We only load the page synchronously if the user rotates (or triggers a
474 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800475 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478
479 // For handling default keys
480 mDefaultKeySsb = new SpannableStringBuilder();
481 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800482
483 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
484 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800485
Adam Cohenf9426d52012-06-04 17:26:21 -0700486 updateGlobalIcons();
487
488 // On large interfaces, we want the screen to auto-rotate based on the current orientation
489 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen432609a2014-03-13 17:03:22 -0700491 if (shouldShowIntroScreen()) {
492 showIntroScreen();
493 } else {
494 showFirstRunActivity();
495 }
496
Winson Chunge43a1e72014-01-15 10:33:02 -0800497 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
498 // on the device, then we always show the first run cling experience (or if there is no
499 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800500 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
501 if (mModel.canMigrateFromOldLauncherDb(this)) {
502 mLauncherClings.showMigrationCling();
503 } else {
504 mLauncherClings.showFirstRunCling();
505 }
Winson Chunga6945242014-01-08 14:04:34 -0800506 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800507 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800508 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700509 }
510
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700511 @Override
512 public void onLauncherProviderChange() { }
513
Winson Chung4a2afa32012-07-19 14:53:05 -0700514 protected void onUserLeaveHint() {
515 super.onUserLeaveHint();
516 sPausedFromUserAction = true;
517 }
518
Winson Chung98ca0f72013-07-29 12:58:51 -0700519 /** To be overriden by subclasses to hint to Launcher that we have custom content */
520 protected boolean hasCustomContentToLeft() {
521 return false;
522 }
523
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500525 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600526 * {@link #addToCustomContentPage}. This will only be invoked if
527 * {@link #hasCustomContentToLeft()} is {@code true}.
528 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500529 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600530 }
531
532 /**
533 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
534 * ensure the custom content page is added or removed if necessary.
535 */
536 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600537 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 // Not bound yet, wait for bindScreens to be called.
539 return;
540 }
541
542 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
543 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500544 mWorkspace.createCustomContentContainer();
545 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600546 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
547 mWorkspace.removeCustomContentPage();
548 }
549 }
550
Adam Cohenf9426d52012-06-04 17:26:21 -0700551 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700552 boolean searchVisible = false;
553 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800554 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700555 int coi = getCurrentOrientationIndexForGlobalIcons();
556 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
557 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700558 if (!DISABLE_MARKET_BUTTON) {
559 updateAppMarketIcon();
560 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700561 searchVisible = updateGlobalSearchIcon();
562 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700563 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700564 if (sGlobalSearchIcon[coi] != null) {
565 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700566 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700567 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700568 if (sVoiceSearchIcon[coi] != null) {
569 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700570 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 }
Winson Chungd64a6662013-09-30 11:06:59 -0700572 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700573 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800574 }
Winson Chungadf0c182012-08-23 14:59:07 -0700575 if (mSearchDropTargetBar != null) {
576 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
Romain Guycbb89e42009-06-08 15:52:54 -0700579
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 if (sLocaleConfiguration == null) {
582 new AsyncTask<Void, Void, LocaleConfiguration>() {
583 @Override
584 protected LocaleConfiguration doInBackground(Void... unused) {
585 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
586 readConfiguration(Launcher.this, localeConfiguration);
587 return localeConfiguration;
588 }
589
590 @Override
591 protected void onPostExecute(LocaleConfiguration result) {
592 sLocaleConfiguration = result;
593 checkForLocaleChange(); // recursive, but now with a locale configuration
594 }
595 }.execute();
596 return;
597 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final Configuration configuration = getResources().getConfiguration();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final String locale = configuration.locale.toString();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mcc = configuration.mcc;
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mnc = configuration.mnc;
609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611
Romain Guy84f296c2009-11-04 15:00:44 -0800612 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 sLocaleConfiguration.locale = locale;
614 sLocaleConfiguration.mcc = mcc;
615 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700616
Joe Onorato0589f0f2010-02-08 13:44:00 -0800617 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618
619 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100620 new AsyncTask<Void, Void, Void>() {
621 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100623 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 }
Michael Jurka43467462013-11-14 12:03:58 +0100625 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700626 }
627 }
628
629 private static class LocaleConfiguration {
630 public String locale;
631 public int mcc = -1;
632 public int mnc = -1;
633 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700634
Romain Guy98d01652009-06-30 16:21:04 -0700635 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
636 DataInputStream in = null;
637 try {
638 in = new DataInputStream(context.openFileInput(PREFERENCES));
639 configuration.locale = in.readUTF();
640 configuration.mcc = in.readInt();
641 configuration.mnc = in.readInt();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 // Ignore
646 } finally {
647 if (in != null) {
648 try {
649 in.close();
650 } catch (IOException e) {
651 // Ignore
652 }
653 }
654 }
655 }
656
657 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
658 DataOutputStream out = null;
659 try {
660 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
661 out.writeUTF(configuration.locale);
662 out.writeInt(configuration.mcc);
663 out.writeInt(configuration.mnc);
664 out.flush();
665 } catch (FileNotFoundException e) {
666 // Ignore
667 } catch (IOException e) {
668 //noinspection ResultOfMethodCallIgnored
669 context.getFileStreamPath(PREFERENCES).delete();
670 } finally {
671 if (out != null) {
672 try {
673 out.close();
674 } catch (IOException e) {
675 // Ignore
676 }
677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
679 }
680
Anton Hanssona2f665f2013-09-26 12:18:32 +0100681 public Stats getStats() {
682 return mStats;
683 }
684
Bjorn Bringertc459e522013-06-07 19:36:01 +0100685 public LayoutInflater getInflater() {
686 return mInflater;
687 }
688
Winson Chung36a62fe2012-05-06 18:04:42 -0700689 boolean isDraggingEnabled() {
690 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
691 // that is subsequently removed from the workspace in startBinding().
692 return !mModel.isLoadingWorkspace();
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 static int getScreen() {
696 synchronized (sLock) {
697 return sScreen;
698 }
699 }
700
701 static void setScreen(int screen) {
702 synchronized (sLock) {
703 sScreen = screen;
704 }
705 }
706
Winson Chung557d6ed2011-07-08 15:34:52 -0700707 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000708 * Copied from View -- the View version of the method isn't called
709 * anywhere else in our process and only exists for API level 17+,
710 * so it's ok to keep our own version with no API requirement.
711 */
712 public static int generateViewId() {
713 for (;;) {
714 final int result = sNextGeneratedId.get();
715 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
716 int newValue = result + 1;
717 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
718 if (sNextGeneratedId.compareAndSet(result, newValue)) {
719 return result;
720 }
721 }
722 }
723
724 public int getViewIdForItem(ItemInfo info) {
725 // This cast is safe given the > 2B range for int.
726 int itemId = (int) info.id;
727 if (mItemIdToViewId.containsKey(itemId)) {
728 return mItemIdToViewId.get(itemId);
729 }
730 int viewId = generateViewId();
731 mItemIdToViewId.put(itemId, viewId);
732 return viewId;
733 }
734
735 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700736 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
737 * a configuration step, this allows the proper animations to run after other transitions.
738 */
Adam Cohendb364c32014-05-20 14:23:40 -0700739 private long completeAdd(PendingAddArguments args) {
740
741 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700744 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700745 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700748 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700749 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800750 }
Michael Jurka27614d22012-04-02 06:40:22 -0700751 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
752 // if you turned the screen off and then back while in All Apps, Launcher would not
753 // return to the workspace. Clearing mAddInfo.container here fixes this issue
754 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700755 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800756 }
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 protected void onActivityResult(
760 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700761 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700762 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700763 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800764 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700765
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 Runnable exitSpringLoaded = new Runnable() {
767 @Override
768 public void run() {
769 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
770 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
771 }
772 };
773
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
777 if (resultCode == RESULT_CANCELED) {
778 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700779 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
783 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 }
785 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200786 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
787 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
788 mWorkspace.exitOverviewMode(false);
789 }
790 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200792
Adam Cohened66b2b2012-01-23 17:28:51 -0800793 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
794 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700795
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800797 // We have special handling for widgets
798 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800799 final int appWidgetId;
800 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
801 : -1;
802 if (widgetId < 0) {
803 appWidgetId = pendingAddWidgetId;
804 } else {
805 appWidgetId = widgetId;
806 }
807
Adam Cohenad4e15c2013-10-17 16:21:35 -0700808 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800809 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
811 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700812 result = RESULT_CANCELED;
813 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 @Override
816 public void run() {
817 exitSpringLoadedDragModeDelayed(false, 0, null);
818 }
819 };
Adam Cohendb364c32014-05-20 14:23:40 -0700820 if (workspaceLocked) {
821 // No need to remove the empty screen if we're mid-binding, as the
822 // the bind will not add the empty screen.
823 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
824 } else {
825 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
826 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
827 }
Winson Chung5aaab772012-04-27 15:24:02 -0700828 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700829 if (!workspaceLocked) {
830 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
831 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
832
833 dropLayout.setDropPending(true);
834 final Runnable onComplete = new Runnable() {
835 @Override
836 public void run() {
837 completeTwoStageWidgetDrop(resultCode, appWidgetId);
838 dropLayout.setDropPending(false);
839 }
840 };
841 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
842 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
843 } else {
844 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
845 mPendingAddInfo);
846 sPendingAddItem = args;
847 }
Winson Chung5aaab772012-04-27 15:24:02 -0700848 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 return;
850 }
851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 // The pattern used here is that a user PICKs a specific application,
853 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
856 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700857 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700858 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
859 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700861 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800862 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700864 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
865 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 }
Adam Cohen00074722013-10-11 17:46:09 -0700867 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700868 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 }
873
Adam Cohendb364c32014-05-20 14:23:40 -0700874 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
875 appWidgetId, ItemInfo info) {
876 PendingAddArguments args = new PendingAddArguments();
877 args.requestCode = requestCode;
878 args.intent = data;
879 args.container = info.container;
880 args.screenId = info.screenId;
881 args.cellX = info.cellX;
882 args.cellY = info.cellY;
883 args.appWidgetId = appWidgetId;
884 return args;
885 }
886
887 /**
888 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
889 *
890 * @param screenId the screen id to check
891 * @return the new screen, or screenId if it exists
892 */
893 private long ensurePendingDropLayoutExists(long screenId) {
894 CellLayout dropLayout =
895 (CellLayout) mWorkspace.getScreenWithId(screenId);
896 if (dropLayout == null) {
897 // it's possible that the add screen was removed because it was
898 // empty and a re-bind occurred
899 mWorkspace.addExtraEmptyScreen();
900 return mWorkspace.commitExtraEmptyScreen();
901 } else {
902 return screenId;
903 }
904 }
905
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700907 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700908 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 Runnable onCompleteRunnable = null;
910 int animationType = 0;
911
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700912 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 if (resultCode == RESULT_OK) {
914 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
915 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 mPendingAddWidgetInfo);
917 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 onCompleteRunnable = new Runnable() {
919 @Override
920 public void run() {
921 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700923 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
924 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 }
926 };
927 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800928 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 if (mDragLayer.getAnimatedView() != null) {
932 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
933 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
934 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 // The animated view may be null in the case of a rotation during widget configuration
937 onCompleteRunnable.run();
938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 }
940
941 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700942 protected void onStop() {
943 super.onStop();
944 FirstFrameAnimatorHelper.setIsVisible(false);
945 }
946
947 @Override
948 protected void onStart() {
949 super.onStart();
950 FirstFrameAnimatorHelper.setIsVisible(true);
951 }
952
953 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200955 long startTime = 0;
956 if (DEBUG_RESUME_TIME) {
957 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400958 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700961
Winson Chung4a2afa32012-07-19 14:53:05 -0700962 // Restore the previous launcher state
963 if (mOnResumeState == State.WORKSPACE) {
964 showWorkspace(false);
965 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800966 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700967 }
968 mOnResumeState = State.NONE;
969
Craig Mautner360310b2012-10-26 15:13:08 -0700970 // Background was set to gradient in onPause(), restore to black if in all apps.
971 setWorkspaceBackground(mState == State.WORKSPACE);
972
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800973 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700976 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500977 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700979 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700981 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200982 // We might have postponed some bind calls until onResume (see waitUntilResume) --
983 // execute them here
984 long startTimeCallbacks = 0;
985 if (DEBUG_RESUME_TIME) {
986 startTimeCallbacks = System.currentTimeMillis();
987 }
988
989 if (mAppsCustomizeContent != null) {
990 mAppsCustomizeContent.setBulkBind(true);
991 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700992 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
993 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200994 }
995 if (mAppsCustomizeContent != null) {
996 mAppsCustomizeContent.setBulkBind(false);
997 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700998 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 if (DEBUG_RESUME_TIME) {
1000 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1001 (System.currentTimeMillis() - startTimeCallbacks));
1002 }
Michael Jurka447bf842013-05-15 14:52:15 +02001003 }
Michael Jurka54554252013-08-01 12:52:23 +02001004 if (mOnResumeCallbacks.size() > 0) {
1005 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1006 mOnResumeCallbacks.get(i).run();
1007 }
1008 mOnResumeCallbacks.clear();
1009 }
Winson Chunge4e50662012-01-23 14:45:13 -08001010
1011 // Reset the pressed state of icons that were locked in the press state while activities
1012 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001013 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001014 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001015 mWaitingForResume.setStayPressed(false);
1016 }
Winson Chunge4e50662012-01-23 14:45:13 -08001017 if (mAppsCustomizeContent != null) {
1018 // Resets the previous all apps icon press state
1019 mAppsCustomizeContent.resetDrawableState();
1020 }
Winson Chung82963d52013-10-09 11:20:57 -07001021
Adam Cohen06dff352012-06-01 17:17:08 -07001022 // It is possible that widgets can receive updates while launcher is not in the foreground.
1023 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1024 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1025 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001026 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001027
Winson Chung780fe592013-09-26 14:48:44 -07001028 // Process any items that were added while Launcher was away.
1029 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1030
Winson Chung5841efa2013-09-30 18:06:44 -07001031 // Update the voice search button proxy
1032 updateVoiceButtonProxyVisible(false);
1033
Adam Cohenf9426d52012-06-04 17:26:21 -07001034 // Again, as with the above scenario, it's possible that one or more of the global icons
1035 // were updated in the wrong orientation.
1036 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1039 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001040
1041 if (mWorkspace.getCustomContentCallbacks() != null) {
1042 // If we are resuming and the custom content is the current page, we call onShow().
1043 // It is also poassible that onShow will instead be called slightly after first layout
1044 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001046 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001047 }
1048 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001049 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001050 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001055 // Ensure that items added to Launcher are queued until Launcher returns
1056 InstallShortcutReceiver.enableInstallQueue();
1057
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001059 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001060 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001061 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001062
1063 // We call onHide() aggressively. The custom content callbacks should be able to
1064 // debounce excess onHide calls.
1065 if (mWorkspace.getCustomContentCallbacks() != null) {
1066 mWorkspace.getCustomContentCallbacks().onHide();
1067 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 QSBScroller mQsbScroller = new QSBScroller() {
1071 int scrollY = 0;
1072
1073 @Override
1074 public void setScrollY(int scroll) {
1075 scrollY = scroll;
1076
1077 if (mWorkspace.isOnOrMovingToCustomContent()) {
1078 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001079 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 }
1081 }
1082 };
1083
1084 public void resetQSBScroll() {
1085 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001086 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001087 }
1088
1089 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001090 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1091 // by a onResume or by scrolling otherwise.
1092 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001093
1094 // Custom content is completely hidden
1095 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001096
1097 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1098 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
1100
Jorim Jaggid017f882014-01-14 17:08:48 -08001101 protected boolean hasSettings() {
1102 return false;
1103 }
1104
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 public interface QSBScroller {
1106 public void setScrollY(int scrollY);
1107 }
1108
Winson Chung98ca0f72013-07-29 12:58:51 -07001109 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001110 CustomContentCallbacks callbacks, String description) {
1111 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001113 }
1114
Adam Cohenedb40762013-07-18 16:45:45 -07001115 // The custom content needs to offset its content to account for the QSB
1116 public int getTopOffsetForCustomContent() {
1117 return mWorkspace.getPaddingTop();
1118 }
1119
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001120 @Override
1121 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 // Flag the loader to stop early before switching
1123 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001124 if (mAppsCustomizeContent != null) {
1125 mAppsCustomizeContent.surrender();
1126 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001127 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001128 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001130 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001131 @Override
1132 public void onWindowFocusChanged(boolean hasFocus) {
1133 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001134 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001135 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 private boolean acceptFilter() {
1138 final InputMethodManager inputManager = (InputMethodManager)
1139 getSystemService(Context.INPUT_METHOD_SERVICE);
1140 return !inputManager.isFullscreenMode();
1141 }
1142
1143 @Override
1144 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001145 final int uniChar = event.getUnicodeChar();
1146 final boolean handled = super.onKeyDown(keyCode, event);
1147 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1148 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1150 keyCode, event);
1151 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001152 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001153 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 // showSearchDialog()
1155 // If there are multiple keystrokes before the search dialog takes focus,
1156 // onSearchRequested() will be called for every keystroke,
1157 // but it is idempotent, so it's fine.
1158 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160 }
1161
Joe Onorato8a9625e2010-01-28 15:55:35 -08001162 // Eat the long press event so the keyboard doesn't come up.
1163 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1164 return true;
1165 }
1166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 return handled;
1168 }
1169
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 private String getTypedText() {
1171 return mDefaultKeySsb.toString();
1172 }
1173
1174 private void clearTypedText() {
1175 mDefaultKeySsb.clear();
1176 mDefaultKeySsb.clearSpans();
1177 Selection.setSelection(mDefaultKeySsb, 0);
1178 }
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001181 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1182 * State
1183 */
1184 private static State intToState(int stateOrdinal) {
1185 State state = State.WORKSPACE;
1186 final State[] stateValues = State.values();
1187 for (int i = 0; i < stateValues.length; i++) {
1188 if (stateValues[i].ordinal() == stateOrdinal) {
1189 state = stateValues[i];
1190 break;
1191 }
1192 }
1193 return state;
1194 }
1195
1196 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 * Restores the previous state, if it exists.
1198 *
1199 * @param savedState The previous state.
1200 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001201 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 private void restoreState(Bundle savedState) {
1203 if (savedState == null) {
1204 return;
1205 }
1206
Michael Jurka883f55b2010-10-21 15:47:14 -07001207 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001208 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001209 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001210 }
1211
Adam Cohen21cd0022013-10-09 18:57:02 -07001212 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1213 PagedView.INVALID_RESTORE_PAGE);
1214 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001215 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
Winson Chung3d503fb2011-07-13 17:25:49 -07001218 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001219 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1222 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001223 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1225 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001226 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1227 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1228 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001229 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001230 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 mRestoring = true;
1232 }
1233
1234 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1235 if (renameFolder) {
1236 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001237 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 mRestoring = true;
1239 }
Winson Chunga12a2502010-12-20 14:41:35 -08001240
Winson Chung785d2eb2011-04-14 16:08:02 -07001241 // Restore the AppsCustomize tab
1242 if (mAppsCustomizeTabHost != null) {
1243 String curTab = savedState.getString("apps_customize_currentTab");
1244 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001245 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001246 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001247 mAppsCustomizeContent.loadAssociatedPages(
1248 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001249 }
1250
Winson Chung5afbf7b2011-07-25 11:53:08 -07001251 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1252 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001253 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001254 mItemIdToViewId = (HashMap<Integer, Integer>)
1255 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
1258 /**
1259 * Finds all the views we need and configure them properly.
1260 */
1261 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001262 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001263
Craig Mautner360310b2012-10-26 15:13:08 -07001264 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001265 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1266 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001267 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001268 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001269
John Spurlock77e1f472013-09-11 10:09:51 -04001270 mLauncherView.setSystemUiVisibility(
1271 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001272 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Winson Chung4c98d922011-05-31 16:50:48 -07001274 // Setup the drag layer
1275 mDragLayer.setup(this, dragController);
1276
Winson Chung3d503fb2011-07-13 17:25:49 -07001277 // Setup the hotseat
1278 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1279 if (mHotseat != null) {
1280 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001281 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 }
1283
Jorim Jaggid017f882014-01-14 17:08:48 -08001284 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001285 View widgetButton = findViewById(R.id.widget_button);
1286 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001287 @Override
1288 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001289 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001290 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001291 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001292 }
1293 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001294 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen71b04732014-06-11 10:36:14 -07001295 if (Utilities.isLmp()) {
1296 ((TextView) widgetButton).setCompoundDrawablesWithIntrinsicBounds(0,
1297 R.drawable.widget_button_l, 0, 0);
1298 }
Adam Cohen61f560d2013-09-30 15:58:20 -07001299
1300 View wallpaperButton = findViewById(R.id.wallpaper_button);
1301 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001302 @Override
1303 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001304 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001305 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001306 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001307 }
1308 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001309 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1310
Adam Cohen71b04732014-06-11 10:36:14 -07001311 if (Utilities.isLmp()) {
1312 ((TextView) wallpaperButton).setCompoundDrawablesWithIntrinsicBounds(0,
1313 R.drawable.wallpaper_button_l, 0, 0);
1314 }
1315
Adam Cohen61f560d2013-09-30 15:58:20 -07001316 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001317 if (hasSettings()) {
1318 settingsButton.setOnClickListener(new OnClickListener() {
1319 @Override
1320 public void onClick(View arg0) {
1321 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001322 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001323 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001324 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001325 });
1326 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen71b04732014-06-11 10:36:14 -07001327 if (Utilities.isLmp()) {
1328 ((TextView) settingsButton).setCompoundDrawablesWithIntrinsicBounds(0,
1329 R.drawable.setting_button_l, 0, 0);
1330 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001331 } else {
1332 settingsButton.setVisibility(View.GONE);
1333 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1334 lp.gravity = Gravity.END | Gravity.TOP;
1335 widgetButton.requestLayout();
1336 }
1337
Adam Cohendbdff6b2013-09-18 19:09:15 -07001338 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001339
Winson Chung4c98d922011-05-31 16:50:48 -07001340 // Setup the workspace
1341 mWorkspace.setHapticFeedbackEnabled(false);
1342 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001343 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001344 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001345
Winson Chungf0ea4d32011-06-06 14:27:16 -07001346 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001347 mSearchDropTargetBar = (SearchDropTargetBar)
1348 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001349
Winson Chungf0ea4d32011-06-06 14:27:16 -07001350 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001351 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001352 mAppsCustomizeContent = (AppsCustomizePagedView)
1353 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1354 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001355
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001357 dragController.setDragScoller(mWorkspace);
1358 dragController.setScrollView(mDragLayer);
1359 dragController.setMoveTarget(mWorkspace);
1360 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001361 if (mSearchDropTargetBar != null) {
1362 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001363 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001364
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001365 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001366 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001367 mWeightWatcher = new WeightWatcher(this);
1368 mWeightWatcher.setAlpha(0.5f);
1369 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001370 new FrameLayout.LayoutParams(
1371 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001372 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001373 Gravity.BOTTOM)
1374 );
Adam Cohen39a06042013-07-19 14:30:12 -07001375
1376 boolean show = shouldShowWeightWatcher();
1377 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380
1381 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001382 * Sets the all apps button. This method is called from {@link Hotseat}.
1383 */
1384 public void setAllAppsButton(View allAppsButton) {
1385 mAllAppsButton = allAppsButton;
1386 }
1387
1388 public View getAllAppsButton() {
1389 return mAllAppsButton;
1390 }
1391
1392 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 * Creates a view representing a shortcut.
1394 *
1395 * @param info The data structure describing the shortcut.
1396 *
1397 * @return A View inflated from R.layout.application.
1398 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001399 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001401 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
1404 /**
1405 * Creates a view representing a shortcut inflated from the specified resource.
1406 *
1407 * @param layoutResId The id of the XML layout used to create the shortcut.
1408 * @param parent The group the shortcut belongs to.
1409 * @param info The data structure describing the shortcut.
1410 *
1411 * @return A View inflated from layoutResId.
1412 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001413 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001414 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1415 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 return favorite;
1418 }
1419
1420 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 * Add a shortcut to the workspace.
1422 *
1423 * @param data The intent describing the shortcut.
1424 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001426 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001427 int cellY) {
1428 int[] cellXY = mTmpAddItemCellCoordinates;
1429 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001430 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001431
Michael Jurkad3ef3062010-11-23 16:23:58 -08001432 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001433
Adam Cohen558baaf2011-08-15 15:22:57 -07001434 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001435 if (info == null) {
1436 return;
1437 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001438 final View view = createShortcut(info);
1439
Adam Cohenfbba09b2011-07-18 21:43:05 -07001440 // First we check if we already know the exact location where we want to add this item.
1441 if (cellX >= 0 && cellY >= 0) {
1442 cellXY[0] = cellX;
1443 cellXY[1] = cellY;
1444 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001445
1446 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001447 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001448 true, null,null)) {
1449 return;
1450 }
1451 DragObject dragObject = new DragObject();
1452 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001453 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1454 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001455 return;
1456 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001458 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001459 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001460 foundCellSpan = (result != null);
1461 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001463 }
1464
1465 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001466 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001467 return;
1468 }
1469
Adam Cohendcd297f2013-06-18 13:13:40 -07001470 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
1472 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001473 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001474 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476 }
1477
Adam Cohen2f093b62012-04-30 18:59:53 -07001478 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1479 int minHeight) {
1480 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001481 // We want to account for the extra amount of padding that we are adding to the widget
1482 // to ensure that it gets the full amount of space that it has requested
1483 int requiredWidth = minWidth + padding.left + padding.right;
1484 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001485 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001486 }
1487
Adam Cohen2f093b62012-04-30 18:59:53 -07001488 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1489 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001490 }
1491
Adam Cohen2f093b62012-04-30 18:59:53 -07001492 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1493 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001494 }
1495
Adam Cohen2f093b62012-04-30 18:59:53 -07001496 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1497 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001498 }
1499
Adam Cohen2f093b62012-04-30 18:59:53 -07001500 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1501 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001502 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001503 }
1504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001506 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001508 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001509 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001511 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001512 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1513 if (appWidgetInfo == null) {
1514 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1515 }
Romain Guycbb89e42009-06-08 15:52:54 -07001516
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001517 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001519
Adam Cohen2f093b62012-04-30 18:59:53 -07001520 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1521 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001524 // We have saved the position to which the widget was dragged-- this really only matters
1525 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001526 int[] cellXY = mTmpAddItemCellCoordinates;
1527 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001528 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1531 cellXY[0] = mPendingAddInfo.cellX;
1532 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001533 spanXY[0] = mPendingAddInfo.spanX;
1534 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 foundCellSpan = true;
1536 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001539 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1540 spanXY[1], cellXY, finalSpan);
1541 spanXY[0] = finalSpan[0];
1542 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001544 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001545 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001546 }
1547
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001549 if (appWidgetId != -1) {
1550 // Deleting an app widget ID is a void call but writes to disk before returning
1551 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001552 new AsyncTask<Void, Void, Void>() {
1553 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001554 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001555 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001556 }
Michael Jurka43467462013-11-14 12:03:58 +01001557 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001558 }
Winson Chung93eef082012-03-23 15:59:27 -07001559 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001560 return;
1561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001563 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001564 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1565 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 launcherInfo.spanX = spanXY[0];
1567 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001568 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1569 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001572 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573
1574 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 if (hostView == null) {
1576 // Perform actual inflation because we're live
1577 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1578 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1579 } else {
1580 // The AppWidgetHostView has already been inflated and instantiated
1581 launcherInfo.hostView = hostView;
1582 }
Romain Guycbb89e42009-06-08 15:52:54 -07001583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001585 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001586 launcherInfo.notifyWidgetSizeChanged(this);
1587
Adam Cohendcd297f2013-06-18 13:13:40 -07001588 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001589 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001590
1591 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001593 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
Romain Guycbb89e42009-06-08 15:52:54 -07001595
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1597 @Override
1598 public void onReceive(Context context, Intent intent) {
1599 final String action = intent.getAction();
1600 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1601 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001602 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001604
Winson Chungc100e8e2011-08-09 16:02:43 -07001605 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001606 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001607 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001608 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1611 mUserPresent = true;
1612 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001613 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1614 mModel.resetLoadedState(false, true);
1615 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1616 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1617 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1618 mModel.resetLoadedState(false, true);
1619 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1620 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1621 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001622 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1623 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1624 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 }
1626 }
1627 };
1628
1629 @Override
1630 public void onAttachedToWindow() {
1631 super.onAttachedToWindow();
1632
1633 // Listen for broadcasts related to user-presence
1634 final IntentFilter filter = new IntentFilter();
1635 filter.addAction(Intent.ACTION_SCREEN_OFF);
1636 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001637 // For handling managed profiles
1638 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1639 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001640 if (ENABLE_DEBUG_INTENTS) {
1641 filter.addAction(DebugIntents.DELETE_DATABASE);
1642 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1643 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001645 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001646 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001647 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 mVisible = true;
1649 }
1650
Adam Cohen0b395352014-06-09 22:54:36 +00001651 /**
1652 * Sets up transparent navigation and status bars in LMP.
1653 * This method is a no-op for other platform versions.
1654 */
1655 @TargetApi(19)
1656 private void setupTransparentSystemBarsForLmp() {
1657 // TODO(sansid): use the APIs directly when compiling against L sdk.
1658 // Currently we use reflection to access the flags and the API to set the transparency
1659 // on the System bars.
1660 if (Utilities.isLmp()) {
1661 try {
1662 getWindow().getAttributes().systemUiVisibility |=
1663 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1664 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1665 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1666 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1667 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1668 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1669 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1670 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1671
1672 Method setStatusBarColorMethod =
1673 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1674 Method setNavigationBarColorMethod =
1675 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1676 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1677 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1678 } catch (NoSuchFieldException e) {
1679 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1680 } catch (NoSuchMethodException ex) {
1681 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1682 } catch (IllegalAccessException e) {
1683 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1684 } catch (IllegalArgumentException e) {
1685 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1686 } catch (InvocationTargetException e) {
1687 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1688 } finally {}
1689 }
1690 }
1691
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 @Override
1693 public void onDetachedFromWindow() {
1694 super.onDetachedFromWindow();
1695 mVisible = false;
1696
Adam Cohend113e0c2010-11-11 10:48:05 -08001697 if (mAttached) {
1698 unregisterReceiver(mReceiver);
1699 mAttached = false;
1700 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 updateRunning();
1702 }
1703
1704 public void onWindowVisibilityChanged(int visibility) {
1705 mVisible = visibility == View.VISIBLE;
1706 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001707 // The following code used to be in onResume, but it turns out onResume is called when
1708 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1709 // is a more appropriate event to handle
1710 if (mVisible) {
1711 mAppsCustomizeTabHost.onWindowVisible();
1712 if (!mWorkspaceLoading) {
1713 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001714 // We want to let Launcher draw itself at least once before we force it to build
1715 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 // apps is nice and speedy.
1717 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001718 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001719 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001720 if (mStarted) return;
1721 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001722 // We delay the layer building a bit in order to give
1723 // other message processing a time to run. In particular
1724 // this avoids a delay in hiding the IME if it was
1725 // currently shown, because doing that may involve
1726 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001727 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001728 final ViewTreeObserver.OnDrawListener listener = this;
1729 mWorkspace.post(new Runnable() {
1730 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001731 if (mWorkspace != null &&
1732 mWorkspace.getViewTreeObserver() != null) {
1733 mWorkspace.getViewTreeObserver().
1734 removeOnDrawListener(listener);
1735 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001736 }
1737 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001738 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001739 }
1740 });
1741 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001742 // When Launcher comes back to foreground, a different Activity might be responsible for
1743 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001744 if (!DISABLE_MARKET_BUTTON) {
1745 updateAppMarketIcon();
1746 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001747 clearTypedText();
1748 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 }
1750
1751 private void sendAdvanceMessage(long delay) {
1752 mHandler.removeMessages(ADVANCE_MSG);
1753 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1754 mHandler.sendMessageDelayed(msg, delay);
1755 mAutoAdvanceSentTime = System.currentTimeMillis();
1756 }
1757
1758 private void updateRunning() {
1759 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1760 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1761 mAutoAdvanceRunning = autoAdvanceRunning;
1762 if (autoAdvanceRunning) {
1763 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1764 sendAdvanceMessage(delay);
1765 } else {
1766 if (!mWidgetsToAdvance.isEmpty()) {
1767 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1768 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1769 }
1770 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001771 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
1773 }
1774 }
1775
1776 private final Handler mHandler = new Handler() {
1777 @Override
1778 public void handleMessage(Message msg) {
1779 if (msg.what == ADVANCE_MSG) {
1780 int i = 0;
1781 for (View key: mWidgetsToAdvance.keySet()) {
1782 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1783 final int delay = mAdvanceStagger * i;
1784 if (v instanceof Advanceable) {
1785 postDelayed(new Runnable() {
1786 public void run() {
1787 ((Advanceable) v).advance();
1788 }
1789 }, delay);
1790 }
1791 i++;
1792 }
1793 sendAdvanceMessage(mAdvanceInterval);
1794 }
1795 }
1796 };
1797
1798 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001799 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1801 if (v instanceof Advanceable) {
1802 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001803 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 updateRunning();
1805 }
1806 }
1807
1808 void removeWidgetToAutoAdvance(View hostView) {
1809 if (mWidgetsToAdvance.containsKey(hostView)) {
1810 mWidgetsToAdvance.remove(hostView);
1811 updateRunning();
1812 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 }
1814
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 launcherInfo.hostView = null;
1818 }
1819
Winson Chung93eef082012-03-23 15:59:27 -07001820 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1821 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1822 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 }
1824
Winson Chunga6945242014-01-08 14:04:34 -08001825 public DragLayer getDragLayer() {
1826 return mDragLayer;
1827 }
1828
1829 public Workspace getWorkspace() {
1830 return mWorkspace;
1831 }
1832
1833 public Hotseat getHotseat() {
1834 return mHotseat;
1835 }
1836
Jorim Jaggid017f882014-01-14 17:08:48 -08001837 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001838 return mOverviewPanel;
1839 }
1840
1841 public SearchDropTargetBar getSearchBar() {
1842 return mSearchDropTargetBar;
1843 }
1844
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001845 public LauncherAppWidgetHost getAppWidgetHost() {
1846 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
Romain Guycbb89e42009-06-08 15:52:54 -07001848
Winson Chunga9abd0e2010-10-27 17:18:37 -07001849 public LauncherModel getModel() {
1850 return mModel;
1851 }
1852
Winson Chunga6945242014-01-08 14:04:34 -08001853 public LauncherClings getLauncherClings() {
1854 return mLauncherClings;
1855 }
1856
1857 protected SharedPreferences getSharedPrefs() {
1858 return mSharedPrefs;
1859 }
1860
Bjorn Bringertc459e522013-06-07 19:36:01 +01001861 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001862 getWindow().closeAllPanels();
1863
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001864 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001865 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 }
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 @Override
1869 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001870 long startTime = 0;
1871 if (DEBUG_RESUME_TIME) {
1872 startTime = System.currentTimeMillis();
1873 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 super.onNewIntent(intent);
1875
1876 // Close the menu
1877 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001878 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001879 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001880
Adam Cohened307df2013-10-02 09:37:31 -07001881 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1882 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1883 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001884
Adam Cohen6fecd412013-10-02 17:41:50 -07001885 if (mWorkspace == null) {
1886 // Can be cases where mWorkspace is null, this prevents a NPE
1887 return;
1888 }
1889 Folder openFolder = mWorkspace.getOpenFolder();
1890 // In all these cases, only animate if we're already on home
1891 mWorkspace.exitWidgetResizeMode();
1892 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001893 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001895 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001896
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 closeFolder();
1898 exitSpringLoadedDragMode();
1899
1900 // If we are already on home, then just animate back to the workspace,
1901 // otherwise, just wait until onResume to set the state back to Workspace
1902 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001903 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001904 } else {
1905 mOnResumeState = State.WORKSPACE;
1906 }
1907
1908 final View v = getWindow().peekDecorView();
1909 if (v != null && v.getWindowToken() != null) {
1910 InputMethodManager imm = (InputMethodManager)getSystemService(
1911 INPUT_METHOD_SERVICE);
1912 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1913 }
1914
1915 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001916 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 mAppsCustomizeTabHost.reset();
1918 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001919
1920 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Adam Cohened307df2013-10-02 09:37:31 -07001922
Michael Jurka447bf842013-05-15 14:52:15 +02001923 if (DEBUG_RESUME_TIME) {
1924 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
1927
Adam Coppa120b8e2013-11-12 16:26:04 +00001928 /**
1929 * Override point for subclasses to prevent movement to the default screen when the home
1930 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1931 */
1932 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1933 return true;
1934 }
1935
1936 /**
1937 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1938 */
1939 protected void onHomeIntent() {
1940 // Do nothing
1941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001944 public void onRestoreInstanceState(Bundle state) {
1945 super.onRestoreInstanceState(state);
1946 for (int page: mSynchronouslyBoundPages) {
1947 mWorkspace.restoreInstanceStateForChild(page);
1948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950
1951 @Override
1952 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001954 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1955 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001956 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001957 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Michael Jurka883f55b2010-10-21 15:47:14 -07001959 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001960 // We close any open folder since it will not be re-opened, and we need to make sure
1961 // this state is reflected.
1962 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 mWaitingForResult) {
1966 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001967 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1969 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001970 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1971 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1972 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001973 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
1976 if (mFolderInfo != null && mWaitingForResult) {
1977 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1978 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1979 }
Michael Jurka946ad472010-07-09 18:05:18 -07001980
Winson Chung785d2eb2011-04-14 16:08:02 -07001981 // Save the current AppsCustomize tab
1982 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001983 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1984 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001985 if (currentTabTag != null) {
1986 outState.putString("apps_customize_currentTab", currentTabTag);
1987 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001988 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1989 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001990 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001991 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
1993
1994 @Override
1995 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Winson Chunge7a03942011-08-05 15:05:12 -07001998 // Remove all pending runnables
1999 mHandler.removeMessages(ADVANCE_MSG);
2000 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002001 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002002
Winson Chungcd2b0142011-06-08 16:02:26 -07002003 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002004 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07002005 mModel.stopLoader();
2006 app.setLauncher(null);
2007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002009 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002011 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002013 mAppWidgetHost = null;
2014
2015 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
2017 TextKeyListener.getInstance().release();
2018
Winson Chung81b52252012-08-27 15:34:29 -07002019 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2020 // to prevent leaking Launcher activities on orientation change.
2021 if (mModel != null) {
2022 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2023 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002024
2025 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002026 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002027
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002028 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002029 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002030 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002031 mWorkspace = null;
2032 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002033
Michael Jurka2ecf9952012-06-18 12:52:28 -07002034 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
Adam Cohena9cf38f2011-05-02 15:36:58 -07002037 public DragController getDragController() {
2038 return mDragController;
2039 }
2040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 @Override
2042 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002043 if (requestCode >= 0) {
2044 setWaitingForResult(true);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 super.startActivityForResult(intent, requestCode);
2047 }
2048
Winson Chung70d72102011-08-12 11:24:35 -07002049 /**
2050 * Indicates that we want global search for this activity by setting the globalSearch
2051 * argument for {@link #startSearch} to true.
2052 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002054 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002056
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002057 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002058
Karl Rosaen138a0412009-04-23 19:00:21 -07002059 if (initialQuery == null) {
2060 // Use any text typed in the launcher as the initial query
2061 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 if (appSearchData == null) {
2064 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002065 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
Winson Chungadf0c182012-08-23 14:59:07 -07002067 Rect sourceBounds = new Rect();
2068 if (mSearchDropTargetBar != null) {
2069 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2070 }
Romain Guycbb89e42009-06-08 15:52:54 -07002071
Bjorn Bringertc459e522013-06-07 19:36:01 +01002072 startSearch(initialQuery, selectInitialQuery,
2073 appSearchData, sourceBounds);
2074 }
2075
2076 public void startSearch(String initialQuery,
2077 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002078 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002080 }
2081
2082 /**
2083 * Starts the global search activity. This code is a copied from SearchManager
2084 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002086 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002087 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002088 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2089 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2090 if (globalSearchActivity == null) {
2091 Log.w(TAG, "No global search activity found.");
2092 return;
2093 }
2094 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2095 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2096 intent.setComponent(globalSearchActivity);
2097 // Make sure that we have a Bundle to put source in
2098 if (appSearchData == null) {
2099 appSearchData = new Bundle();
2100 } else {
2101 appSearchData = new Bundle(appSearchData);
2102 }
2103 // Set source to package name of app that starts global search, if not set already.
2104 if (!appSearchData.containsKey("source")) {
2105 appSearchData.putString("source", getPackageName());
2106 }
2107 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2108 if (!TextUtils.isEmpty(initialQuery)) {
2109 intent.putExtra(SearchManager.QUERY, initialQuery);
2110 }
2111 if (selectInitialQuery) {
2112 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2113 }
2114 intent.setSourceBounds(sourceBounds);
2115 try {
2116 startActivity(intent);
2117 } catch (ActivityNotFoundException ex) {
2118 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 }
2121
Yura4f93ec62014-02-04 14:15:21 +00002122 public boolean isOnCustomContent() {
2123 return mWorkspace.isOnOrMovingToCustomContent();
2124 }
2125
Winson Chung70d72102011-08-12 11:24:35 -07002126 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002127 public boolean onPrepareOptionsMenu(Menu menu) {
2128 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002129 if (!isOnCustomContent()) {
2130 // Close any open folders
2131 closeFolder();
2132 // Stop resizing any widgets
2133 mWorkspace.exitWidgetResizeMode();
2134 if (!mWorkspace.isInOverviewMode()) {
2135 // Show the overview mode
2136 showOverviewMode(true);
2137 } else {
2138 showWorkspace(true);
2139 }
Winson Chunge0298742014-01-17 12:03:00 -08002140 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002141 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002144 @Override
2145 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002146 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002147 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002149 }
2150
Joe Onorato9c1289c2009-08-17 11:03:03 -04002151 public boolean isWorkspaceLocked() {
2152 return mWorkspaceLoading || mWaitingForResult;
2153 }
2154
Adam Cohen517a7f52014-03-01 12:12:59 -08002155 public boolean isWorkspaceLoading() {
2156 return mWorkspaceLoading;
2157 }
2158
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002159 private void setWorkspaceLoading(boolean value) {
2160 boolean isLocked = isWorkspaceLocked();
2161 mWorkspaceLoading = value;
2162 if (isLocked != isWorkspaceLocked()) {
2163 onWorkspaceLockedChanged();
2164 }
2165 }
2166
2167 private void setWaitingForResult(boolean value) {
2168 boolean isLocked = isWorkspaceLocked();
2169 mWaitingForResult = value;
2170 if (isLocked != isWorkspaceLocked()) {
2171 onWorkspaceLockedChanged();
2172 }
2173 }
2174
2175 protected void onWorkspaceLockedChanged() { }
2176
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002180 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2181 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002182 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002183 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002185
Adam Cohenad4e15c2013-10-17 16:21:35 -07002186 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2187 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2188 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2189 }
2190
2191 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2192 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2193 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002194 if (appWidgetInfo.configure != null) {
2195 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002196 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002197
Bjorn Bringert7984c942009-12-09 15:38:25 +00002198 // Launch over to configure widget, if needed
2199 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002200 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002201 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002202 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002204 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002205 Runnable onComplete = new Runnable() {
2206 @Override
2207 public void run() {
2208 // Exit spring loaded mode if necessary after adding the widget
2209 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2210 null);
2211 }
2212 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002213 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002214 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002215 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
Romain Guycbb89e42009-06-08 15:52:54 -07002218
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002219 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002220 // Close any folders that may be open.
2221 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002222 mWorkspace.moveToCustomContentScreen(animate);
2223 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002224 /**
2225 * Process a shortcut drop.
2226 *
2227 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002228 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 * @param cell The cell it should be added to, optional
2230 * @param position The location on the screen where it was dropped, optional
2231 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002232 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238
2239 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.cellX = cell[0];
2241 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002243
2244 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2245 createShortcutIntent.setComponent(componentName);
2246 processShortcut(createShortcutIntent);
2247 }
2248
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 /**
2250 * Process a widget drop.
2251 *
2252 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 * @param cell The cell it should be added to, optional
2255 * @param position The location on the screen where it was dropped, optional
2256 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002257 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002258 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002261 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002263 mPendingAddInfo.minSpanX = info.minSpanX;
2264 mPendingAddInfo.minSpanY = info.minSpanY;
2265
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 mPendingAddInfo.cellX = cell[0];
2268 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002269 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002270 if (span != null) {
2271 mPendingAddInfo.spanX = span[0];
2272 mPendingAddInfo.spanY = span[1];
2273 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002274
Adam Cohened66b2b2012-01-23 17:28:51 -08002275 AppWidgetHostView hostView = info.boundWidget;
2276 int appWidgetId;
2277 if (hostView != null) {
2278 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002279 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002280 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002281 // In this case, we either need to start an activity to get permission to bind
2282 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002283 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002284 Bundle options = info.bindOptions;
2285
2286 boolean success = false;
2287 if (options != null) {
2288 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2289 info.componentName, options);
2290 } else {
2291 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2292 info.componentName);
2293 }
2294 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002298 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2299 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2300 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 // TODO: we need to make sure that this accounts for the options bundle.
2302 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2304 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002305 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 }
2307
Joe Onoratodeb98af2010-02-19 14:59:39 -08002308 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002309 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 }
2311
Winson Chung24ab2f12010-09-16 14:10:47 -07002312 void processWallpaper(Intent intent) {
2313 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2314 }
2315
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002318 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 folderInfo.title = getText(R.string.folder_name);
2320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002324 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325
2326 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 FolderIcon newFolder =
2328 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002331 // Force measure the new folder icon
2332 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2333 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002334 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
Romain Guycbb89e42009-06-08 15:52:54 -07002336
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002338 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002339 }
2340
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002341 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002342 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002343 }
2344
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002345 /**
2346 * Registers various content observers. The current implementation registers
2347 * only a favorites observer to keep track of the favorites applications.
2348 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002349 private void registerContentObservers() {
2350 ContentResolver resolver = getContentResolver();
2351 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2352 true, mWidgetObserver);
2353 }
2354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 @Override
2356 public boolean dispatchKeyEvent(KeyEvent event) {
2357 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2358 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002360 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002361 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002362 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002363 dumpState();
2364 return true;
2365 }
2366 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002367 }
2368 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2369 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002370 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 return true;
2372 }
2373 }
2374
2375 return super.dispatchKeyEvent(event);
2376 }
2377
Joe Onorato88ec0992009-11-19 13:16:06 -08002378 @Override
2379 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002380 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002381 if (mAppsCustomizeContent.getContentType() ==
2382 AppsCustomizePagedView.ContentType.Applications) {
2383 showWorkspace(true);
2384 } else {
2385 showOverviewMode(true);
2386 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002387 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002388 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002389 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002390 Folder openFolder = mWorkspace.getOpenFolder();
2391 if (openFolder.isEditingName()) {
2392 openFolder.dismissEditingName();
2393 } else {
2394 closeFolder();
2395 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002396 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002397 mWorkspace.exitWidgetResizeMode();
2398
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002399 // Back button is a no-op here, but give at least some feedback for the button press
2400 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002401 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002402 }
2403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002405 * Re-listen when widgets are reset.
2406 */
2407 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002408 if (mAppWidgetHost != null) {
2409 mAppWidgetHost.startListening();
2410 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002411 }
2412
2413 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 * Launches the intent referred by the clicked shortcut.
2415 *
2416 * @param v The view representing the clicked shortcut.
2417 */
2418 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002419 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2420 // view has detached (it's possible for this to happen if the view is removed mid touch).
2421 if (v.getWindowToken() == null) {
2422 return;
2423 }
2424
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002425 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002426 return;
2427 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002428
Adam Cohen1697b792013-09-17 19:08:21 -07002429 if (v instanceof Workspace) {
2430 if (mWorkspace.isInOverviewMode()) {
2431 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002432 }
2433 return;
2434 }
2435
2436 if (v instanceof CellLayout) {
2437 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002438 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 }
2440 }
2441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002443 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002444 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002446 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002447 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002448 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002449 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002450 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 }
2452 }
2453
Michael Jurka0e260592010-06-30 17:07:39 -07002454 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002455 return false;
2456 }
2457
Michael Jurkaaf442092010-06-10 17:01:57 -07002458 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002459 * Event handler for the search button
2460 *
2461 * @param v The view that was clicked.
2462 */
2463 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002464 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2465
Amith Yamasania135ba82011-08-09 17:42:01 -07002466 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002467 }
2468
2469 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002470 * Event handler for the voice button
2471 *
2472 * @param v The view that was clicked.
2473 */
2474 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002475 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002476
Bjorn Bringertc459e522013-06-07 19:36:01 +01002477 startVoice();
2478 }
2479
2480 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002481 try {
2482 final SearchManager searchManager =
2483 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2484 ComponentName activityName = searchManager.getGlobalSearchActivity();
2485 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002486 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002487 if (activityName != null) {
2488 intent.setPackage(activityName.getPackageName());
2489 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002490 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002491 } catch (ActivityNotFoundException e) {
2492 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002493 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002494 startActivitySafely(null, intent, "onClickVoiceButton");
2495 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002496 }
2497
2498 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002499 * Event handler for the "grid" button that appears on the home screen, which
2500 * enters all apps mode.
2501 *
2502 * @param v The view that was clicked.
2503 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002504 protected void onClickAllAppsButton(View v) {
2505 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2506 if (isAllAppsVisible()) {
2507 showWorkspace(true);
2508 } else {
2509 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2510 }
2511 }
2512
2513 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002514 * Event handler for a paged view icon click.
2515 * @param v The view that was clicked.
2516 * @param appInfo The {link AppInfo} of the view.
2517 */
2518 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2519 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2520 startActivitySafely(v, appInfo.intent, appInfo);
2521 getStats().recordLaunch(appInfo.intent);
2522 }
2523
2524 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 * Event handler for an app shortcut click.
2526 *
2527 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2528 */
2529 protected void onClickAppShortcut(View v) {
2530 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2531 Object tag = v.getTag();
2532 if (!(tag instanceof ShortcutInfo)) {
2533 throw new IllegalArgumentException("Input must be a Shortcut");
2534 }
2535
2536 // Open shortcut
2537 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2538 final Intent intent = shortcut.intent;
2539
2540 // Check for special shortcuts
2541 if (intent.getComponent() != null) {
2542 final String shortcutClass = intent.getComponent().getClassName();
2543
2544 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2545 MemoryDumpActivity.startDump(this);
2546 return;
2547 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2548 toggleShowWeightWatcher();
2549 return;
2550 }
2551 }
2552
2553 // Start activities
2554 int[] pos = new int[2];
2555 v.getLocationOnScreen(pos);
2556 intent.setSourceBounds(new Rect(pos[0], pos[1],
2557 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2558
2559 boolean success = startActivitySafely(v, intent, tag);
2560
2561 mStats.recordLaunch(intent, shortcut);
2562
2563 if (success && v instanceof BubbleTextView) {
2564 mWaitingForResume = (BubbleTextView) v;
2565 mWaitingForResume.setStayPressed(true);
2566 }
2567 }
2568
2569 /**
2570 * Event handler for a folder icon click.
2571 *
2572 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2573 */
2574 protected void onClickFolderIcon(View v) {
2575 if (LOGD) Log.d(TAG, "onClickFolder");
2576 if (!(v instanceof FolderIcon)){
2577 throw new IllegalArgumentException("Input must be a FolderIcon");
2578 }
2579
2580 FolderIcon folderIcon = (FolderIcon) v;
2581 final FolderInfo info = folderIcon.getFolderInfo();
2582 Folder openFolder = mWorkspace.getFolderForTag(info);
2583
2584 // If the folder info reports that the associated folder is open, then verify that
2585 // it is actually opened. There have been a few instances where this gets out of sync.
2586 if (info.opened && openFolder == null) {
2587 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2588 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2589 info.opened = false;
2590 }
2591
2592 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2593 // Close any open folder
2594 closeFolder();
2595 // Open the requested folder
2596 openFolder(folderIcon);
2597 } else {
2598 // Find the open folder...
2599 int folderScreen;
2600 if (openFolder != null) {
2601 folderScreen = mWorkspace.getPageForView(openFolder);
2602 // .. and close it
2603 closeFolder(openFolder);
2604 if (folderScreen != mWorkspace.getCurrentPage()) {
2605 // Close any folder open on the current screen
2606 closeFolder();
2607 // Pull the folder onto this screen
2608 openFolder(folderIcon);
2609 }
2610 }
2611 }
Michael Jurka5130e402011-10-13 04:55:35 -07002612 }
2613
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002614 /**
2615 * Event handler for the (Add) Widgets button that appears after a long press
2616 * on the home screen.
2617 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002618 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002620 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2621 }
2622
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 /**
2624 * Event handler for the wallpaper picker button that appears after a long press
2625 * on the home screen.
2626 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002627 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002628 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2629 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2630 pickWallpaper.setComponent(getWallpaperPickerComponent());
2631 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2632 }
2633
2634 /**
2635 * Event handler for a click on the settings button that appears after a long press
2636 * on the home screen.
2637 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002638 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2640 }
2641
Michael Jurka5130e402011-10-13 04:55:35 -07002642 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002643 // Provide the same haptic feedback that the system offers for virtual keys.
2644 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002645 }
2646
Adam Cohen61f560d2013-09-30 15:58:20 -07002647 public void performHapticFeedbackOnTouchDown(View v) {
2648 // Provide the same haptic feedback that the system offers for virtual keys.
2649 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2650 }
2651
2652 public View.OnTouchListener getHapticFeedbackTouchListener() {
2653 if (mHapticFeedbackTouchListener == null) {
2654 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2655 @Override
2656 public boolean onTouch(View v, MotionEvent event) {
2657 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2658 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2659 }
2660 return false;
2661 }
2662 };
2663 }
2664 return mHapticFeedbackTouchListener;
2665 }
2666
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002667 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002668 if (!DISABLE_MARKET_BUTTON) {
2669 if (mAppMarketIntent != null) {
2670 startActivitySafely(v, mAppMarketIntent, "app market");
2671 } else {
2672 Log.e(TAG, "Invalid app market intent.");
2673 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002674 }
2675 }
2676
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002677 public void onDragStarted(View view) {}
2678
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002679 /**
2680 * Called when the user stops interacting with the launcher.
2681 * This implies that the user is now on the homescreen and is not doing housekeeping.
2682 */
2683 protected void onInteractionEnd() {}
2684
2685 /**
2686 * Called when the user starts interacting with the launcher.
2687 * The possible interactions are:
2688 * - open all apps
2689 * - reorder an app shortcut, or a widget
2690 * - open the overview mode.
2691 * This is a good time to stop doing things that only make sense
2692 * when the user is on the homescreen and not doing housekeeping.
2693 */
2694 protected void onInteractionBegin() {}
2695
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002696 void startApplicationDetailsActivity(ComponentName componentName) {
2697 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002698 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2699 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002700 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2701 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002702 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002703 }
2704
Michael Jurka1e2f4652013-07-08 18:03:46 -07002705 // returns true if the activity was started
2706 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002707 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002708 // System applications cannot be installed. For now, show a toast explaining that.
2709 // We may give them the option of disabling apps this way.
2710 int messageId = R.string.uninstall_system_app_text;
2711 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002712 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002713 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002714 String packageName = componentName.getPackageName();
2715 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002716 Intent intent = new Intent(
2717 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002718 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2719 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002720 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002721 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002722 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002723 }
2724
Michael Jurka86a720e2012-05-09 11:23:23 -07002725 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002726 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002728 // Only launch using the new animation if the shortcut has not opted out (this is a
2729 // private contract between launcher and may be ignored in the future).
2730 boolean useLaunchAnimation = (v != null) &&
2731 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002732 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2733 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002734
Kenny Guy1317e2d2014-05-08 18:52:50 +01002735 UserHandleCompat user = null;
2736 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2737 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2738 user = userManager.getUserForSerialNumber(serialNumber);
2739 }
2740
2741 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002742 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002743 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2744 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002745 optsBundle = opts.toBundle();
2746 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002747 if (useLaunchAnimation && Utilities.isLmp()) {
2748 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2749 optsBundle = opts.toBundle();
2750 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002751
2752 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2753 // Could be launching some bookkeeping activity
2754 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002755 } else {
Kenny Guy1317e2d2014-05-08 18:52:50 +01002756 launcherApps.startActivityForProfile(intent.getComponent(),
2757 intent.getSourceBounds(), optsBundle, user);
Winson Chungc7450e32012-04-17 17:34:08 -07002758 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002759 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 } catch (SecurityException e) {
2761 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002762 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002764 "or use the exported attribute for this activity. "
2765 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002767 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002768 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002769
Michael Jurka86a720e2012-05-09 11:23:23 -07002770 boolean startActivitySafely(View v, Intent intent, Object tag) {
2771 boolean success = false;
2772 try {
2773 success = startActivity(v, intent, tag);
2774 } catch (ActivityNotFoundException e) {
2775 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2776 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2777 }
2778 return success;
2779 }
2780
Adam Cohen268c4752012-06-06 17:47:33 -07002781 /**
2782 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2783 * in the DragLayer in the exact absolute location of the original FolderIcon.
2784 */
2785 private void copyFolderIconToImage(FolderIcon fi) {
2786 final int width = fi.getMeasuredWidth();
2787 final int height = fi.getMeasuredHeight();
2788
2789 // Lazy load ImageView, Bitmap and Canvas
2790 if (mFolderIconImageView == null) {
2791 mFolderIconImageView = new ImageView(this);
2792 }
2793 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2794 mFolderIconBitmap.getHeight() != height) {
2795 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2796 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2797 }
2798
2799 DragLayer.LayoutParams lp;
2800 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2801 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2802 } else {
2803 lp = new DragLayer.LayoutParams(width, height);
2804 }
2805
Adam Cohen307fe232012-08-16 17:55:58 -07002806 // The layout from which the folder is being opened may be scaled, adjust the starting
2807 // view size by this scale factor.
2808 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002809 lp.customPosition = true;
2810 lp.x = mRectForFolderAnimation.left;
2811 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002812 lp.width = (int) (scale * width);
2813 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002814
2815 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2816 fi.draw(mFolderIconCanvas);
2817 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002818 if (fi.getFolder() != null) {
2819 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2820 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002821 }
Adam Cohen268c4752012-06-06 17:47:33 -07002822 // Just in case this image view is still in the drag layer from a previous animation,
2823 // we remove it and re-add it.
2824 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2825 mDragLayer.removeView(mFolderIconImageView);
2826 }
2827 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002828 if (fi.getFolder() != null) {
2829 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002830 }
Adam Cohen268c4752012-06-06 17:47:33 -07002831 }
2832
Adam Cohen2801caf2011-05-13 20:57:39 -07002833 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002834 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002835 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2836 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2837 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2838
Adam Cohenc51934b2011-07-26 21:07:43 -07002839 FolderInfo info = (FolderInfo) fi.getTag();
2840 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2841 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002842 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2843 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002844 }
2845
Adam Cohen268c4752012-06-06 17:47:33 -07002846 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2847 copyFolderIconToImage(fi);
2848 fi.setVisibility(View.INVISIBLE);
2849
Michael Jurka2ecf9952012-06-18 12:52:28 -07002850 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002851 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002852 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2853 oa.start();
2854 }
2855
Adam Cohen268c4752012-06-06 17:47:33 -07002856 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002857 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002858 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2859 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2860 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2861
Adam Cohen268c4752012-06-06 17:47:33 -07002862 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002863
Adam Cohen268c4752012-06-06 17:47:33 -07002864 // We remove and re-draw the FolderIcon in-case it has changed
2865 mDragLayer.removeView(mFolderIconImageView);
2866 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002867 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002868 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002869 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002870 oa.addListener(new AnimatorListenerAdapter() {
2871 @Override
2872 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002873 if (cl != null) {
2874 cl.clearFolderLeaveBehind();
2875 // Remove the ImageView copy of the FolderIcon and make the original visible.
2876 mDragLayer.removeView(mFolderIconImageView);
2877 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002878 }
2879 }
2880 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002881 oa.start();
2882 }
2883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002885 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 * is animated relative to the specified View. If the View is null, no animation
2887 * is played.
2888 *
2889 * @param folderInfo The FolderInfo describing the folder to open.
2890 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002891 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002892 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002893 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002894
Adam Cohena9cf38f2011-05-02 15:36:58 -07002895 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896
Adam Cohen4554ee12011-08-03 16:13:21 -07002897 // Just verify that the folder hasn't already been added to the DragLayer.
2898 // There was a one-off crash where the folder had a parent already.
2899 if (folder.getParent() == null) {
2900 mDragLayer.addView(folder);
2901 mDragController.addDropTarget((DropTarget) folder);
2902 } else {
2903 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2904 folder.getParent() + ").");
2905 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002906 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002907 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002908
2909 // Notify the accessibility manager that this folder "window" has appeared and occluded
2910 // the workspace items
2911 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2912 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002913 }
2914
2915 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002916 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002917 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002918 if (folder.isEditingName()) {
2919 folder.dismissEditingName();
2920 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002921 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002922
2923 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002924 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002925 }
2926 }
2927
2928 void closeFolder(Folder folder) {
2929 folder.getInfo().opened = false;
2930
2931 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2932 if (parent != null) {
2933 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2934 shrinkAndFadeInFolderIcon(fi);
2935 }
2936 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002937
2938 // Notify the accessibility manager that this folder "window" has disappeard and no
2939 // longer occludeds the workspace items
2940 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941 }
2942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002943 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002944 if (!isDraggingEnabled()) return false;
2945 if (isWorkspaceLocked()) return false;
2946 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947
Adam Cohen1697b792013-09-17 19:08:21 -07002948 if (v instanceof Workspace) {
2949 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002950 if (mWorkspace.enterOverviewMode()) {
2951 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2952 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2953 return true;
2954 } else {
2955 return false;
2956 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002957 } else {
2958 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002959 }
Adam Cohen1697b792013-09-17 19:08:21 -07002960 }
2961
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002962 CellLayout.CellInfo longClickCellInfo = null;
2963 View itemUnderLongClick = null;
2964 if (v.getTag() instanceof ItemInfo) {
2965 ItemInfo info = (ItemInfo) v.getTag();
2966 longClickCellInfo = new CellLayout.CellInfo(v, info);;
2967 itemUnderLongClick = longClickCellInfo.cell;
2968 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 }
2970
Winson Chung3d503fb2011-07-13 17:25:49 -07002971 // The hotseat touch handling does not go through Workspace, and we always allow long press
2972 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05002973 final boolean inHotseat = isHotseatLayout(v);
2974 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002975 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002976 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002977 // User long pressed on empty space
2978 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2979 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07002980 if (mWorkspace.isInOverviewMode()) {
2981 mWorkspace.startReordering(v);
2982 } else {
2983 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002986 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2987 mHotseat.getOrderInHotseat(
2988 longClickCellInfo.cellX,
2989 longClickCellInfo.cellY));
2990 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002992 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 }
2994 }
2995 }
2996 return true;
2997 }
2998
Winson Chung3d503fb2011-07-13 17:25:49 -07002999 boolean isHotseatLayout(View layout) {
3000 return mHotseat != null && layout != null &&
3001 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3002 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003003
Winson Chung3d503fb2011-07-13 17:25:49 -07003004 /**
3005 * Returns the CellLayout of the specified container at the specified screen.
3006 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003007 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003008 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3009 if (mHotseat != null) {
3010 return mHotseat.getLayout();
3011 } else {
3012 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003013 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003014 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003015 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003016 }
3017 }
3018
Daniel Sandler843e8602010-06-07 14:59:01 -04003019 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003020 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003021 }
3022
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003023 /**
3024 * Helper method for the cameraZoomIn/cameraZoomOut animations
3025 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003026 * @param scaleFactor The scale factor used for the zoom
3027 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003028 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003029 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003030 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003031 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003032
Craig Mautner360310b2012-10-26 15:13:08 -07003033 private void setWorkspaceBackground(boolean workspace) {
3034 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003035 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003036 }
3037
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003038 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003039 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3040 int curflags = getWindow().getAttributes().flags
3041 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3042 if (wpflags != curflags) {
3043 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3044 }
Craig Mautner360310b2012-10-26 15:13:08 -07003045 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003046 }
3047
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003048 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3049 if (v instanceof LauncherTransitionable) {
3050 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3051 }
3052 }
3053
Michael Jurkabed61d22012-02-14 22:51:29 -08003054 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3055 if (v instanceof LauncherTransitionable) {
3056 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3057 }
Winson Chung70442722012-02-10 15:43:22 -08003058
3059 // Update the workspace transition step as well
3060 dispatchOnLauncherTransitionStep(v, 0f);
3061 }
3062
3063 private void dispatchOnLauncherTransitionStep(View v, float t) {
3064 if (v instanceof LauncherTransitionable) {
3065 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3066 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003067 }
3068
3069 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3070 if (v instanceof LauncherTransitionable) {
3071 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3072 }
Winson Chung70442722012-02-10 15:43:22 -08003073
3074 // Update the workspace transition step as well
3075 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003076 }
3077
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003078 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003079 * Things to test when changing the following seven functions.
3080 * - Home from workspace
3081 * - from center screen
3082 * - from other screens
3083 * - Home from all apps
3084 * - from center screen
3085 * - from other screens
3086 * - Back from all apps
3087 * - from center screen
3088 * - from other screens
3089 * - Launch app from workspace and quit
3090 * - with back
3091 * - with home
3092 * - Launch app from all apps and quit
3093 * - with back
3094 * - with home
3095 * - Go to a screen that's not the default, then all
3096 * apps, and launch and app, and go back
3097 * - with back
3098 * -with home
3099 * - On workspace, long press power and go back
3100 * - with back
3101 * - with home
3102 * - On all apps, long press power and go back
3103 * - with back
3104 * - with home
3105 * - On workspace, power off
3106 * - On all apps, power off
3107 * - Launch an app and turn off the screen while in that app
3108 * - Go back with home key
3109 * - Go back with back key TODO: make this not go to workspace
3110 * - From all apps
3111 * - From workspace
3112 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3113 * - From all apps
3114 * - From the center workspace
3115 * - From another workspace
3116 */
3117
3118 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003119 * Zoom the camera out from the workspace to reveal 'toView'.
3120 * Assumes that the view to show is anchored at either the very top or very bottom
3121 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003122 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003123 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003124 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3125 showAppsCustomizeHelper(animated, springLoaded, contentType);
3126 }
3127 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3128 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003129 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003130 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003131 mStateAnimation.cancel();
3132 mStateAnimation = null;
3133 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003134 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003135
Winson Chungf0ea4d32011-06-06 14:27:16 -07003136 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3137 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3138 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003139 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003140 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003141 final int startDelay =
3142 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003143
Michael Jurkab3e22d92011-10-31 15:58:33 -07003144 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003145
Michael Jurkad74c9842011-07-10 12:44:21 -07003146 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003147 Animator workspaceAnim =
3148 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003149 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003150 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3151 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003152 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3153 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003154
3155 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003156 toView.setScaleX(scale);
3157 toView.setScaleY(scale);
3158 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3159 scaleAnim.
3160 scaleX(1f).scaleY(1f).
3161 setDuration(duration).
3162 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003163
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003165 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003166 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003167 .ofFloat(toView, "alpha", 0f, 1f)
3168 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003169 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003170 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3171 @Override
3172 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003173 if (animation == null) {
3174 throw new RuntimeException("animation is null");
3175 }
Winson Chung70442722012-02-10 15:43:22 -08003176 float t = (Float) animation.getAnimatedValue();
3177 dispatchOnLauncherTransitionStep(fromView, t);
3178 dispatchOnLauncherTransitionStep(toView, t);
3179 }
3180 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003181
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003182 // toView should appear right at the end of the workspace shrink
3183 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003184 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003185 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003186 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003187
3188 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003189 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003190 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003191 // Prepare the position
3192 toView.setTranslationX(0.0f);
3193 toView.setTranslationY(0.0f);
3194 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003195 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003196 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003197 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003198 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003199 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3200 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003201
Winson Chungc7d2b602012-05-16 17:02:20 -07003202 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003203 if (mSearchDropTargetBar != null) {
3204 mSearchDropTargetBar.hideSearchBar(false);
3205 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003206 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003207 });
3208
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003209 if (workspaceAnim != null) {
3210 mStateAnimation.play(workspaceAnim);
3211 }
Michael Jurka1899a362011-11-03 13:50:45 -07003212
3213 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003214
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003215 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3216 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003217
3218 // If any of the objects being animated haven't been measured/laid out
3219 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003220 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003221 (mWorkspace.getMeasuredWidth() == 0) ||
3222 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003223 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003224 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003225
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003226 final AnimatorSet stateAnimation = mStateAnimation;
3227 final Runnable startAnimRunnable = new Runnable() {
3228 public void run() {
3229 // Check that mStateAnimation hasn't changed while
3230 // we waited for a layout/draw pass
3231 if (mStateAnimation != stateAnimation)
3232 return;
3233 setPivotsForZoom(toView, scale);
3234 dispatchOnLauncherTransitionStart(fromView, animated, false);
3235 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003236 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003237 }
3238 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003239 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003240 final ViewTreeObserver observer = toView.getViewTreeObserver();
3241 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3242 public void onGlobalLayout() {
3243 startAnimRunnable.run();
3244 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3245 }
3246 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003247 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003248 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003249 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003250 } else {
3251 toView.setTranslationX(0.0f);
3252 toView.setTranslationY(0.0f);
3253 toView.setScaleX(1.0f);
3254 toView.setScaleY(1.0f);
3255 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003256 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003257
Daniel Sandlere4f98912013-06-25 15:13:26 -04003258 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003259 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003260 if (mSearchDropTargetBar != null) {
3261 mSearchDropTargetBar.hideSearchBar(false);
3262 }
Michael Jurkaabded662011-03-04 12:06:57 -08003263 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003264 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003265 dispatchOnLauncherTransitionStart(fromView, animated, false);
3266 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003267 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003268 dispatchOnLauncherTransitionStart(toView, animated, false);
3269 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003270 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003271 }
3272
3273 /**
3274 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003276 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003277 */
Adam Cohened307df2013-10-02 09:37:31 -07003278 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003279 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003280
Michael Jurkab3e22d92011-10-31 15:58:33 -07003281 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003282 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003283 mStateAnimation.cancel();
3284 mStateAnimation = null;
3285 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003286 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003287
Winson Chungf0ea4d32011-06-06 14:27:16 -07003288 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003289 final int fadeOutDuration =
3290 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003291 final float scaleFactor = (float)
3292 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3293 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003294 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003295 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003296 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003297 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3298 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003299 toState, animated, stagger, -1);
3300 } else if (toState == Workspace.State.SPRING_LOADED ||
3301 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003302 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003303 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003304 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003305
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003307 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003308 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003309 final LauncherViewPropertyAnimator scaleAnim =
3310 new LauncherViewPropertyAnimator(fromView);
3311 scaleAnim.
3312 scaleX(scaleFactor).scaleY(scaleFactor).
3313 setDuration(duration).
3314 setInterpolator(new Workspace.ZoomInInterpolator());
3315
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003316 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003317 .ofFloat(fromView, "alpha", 1f, 0f)
3318 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003319 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003320 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3321 @Override
3322 public void onAnimationUpdate(ValueAnimator animation) {
3323 float t = 1f - (Float) animation.getAnimatedValue();
3324 dispatchOnLauncherTransitionStep(fromView, t);
3325 dispatchOnLauncherTransitionStep(toView, t);
3326 }
3327 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003328
Michael Jurka2ecf9952012-06-18 12:52:28 -07003329 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003330
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003331 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3332 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003333 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003334
3335 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003336 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003337 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003338 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003339 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3340 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003341 if (onCompleteRunnable != null) {
3342 onCompleteRunnable.run();
3343 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003344 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003345 }
3346 });
3347
Winson Chungf0ea4d32011-06-06 14:27:16 -07003348 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003349 if (workspaceAnim != null) {
3350 mStateAnimation.play(workspaceAnim);
3351 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003352 dispatchOnLauncherTransitionStart(fromView, animated, true);
3353 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003354 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003355 } else {
3356 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003357 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003358 dispatchOnLauncherTransitionStart(fromView, animated, true);
3359 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003360 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003361 dispatchOnLauncherTransitionStart(toView, animated, true);
3362 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003363 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003364 }
3365
Michael Jurkae326f182011-11-21 14:05:46 -08003366 @Override
3367 public void onTrimMemory(int level) {
3368 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003369 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003370 mAppsCustomizeTabHost.onTrimMemory();
3371 }
3372 }
3373
Adam Cohened307df2013-10-02 09:37:31 -07003374 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003375 showWorkspace(animated, null);
3376 }
3377
Adam Cohened307df2013-10-02 09:37:31 -07003378 protected void showWorkspace() {
3379 showWorkspace(true);
3380 }
3381
Adam Cohened66b2b2012-01-23 17:28:51 -08003382 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003383 if (mWorkspace.isInOverviewMode()) {
3384 mWorkspace.exitOverviewMode(animated);
3385 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003386 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003387 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003388 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003389 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003390
Winson Chungc7d2b602012-05-16 17:02:20 -07003391 // Show the search bar (only animate if we were showing the drop target bar in spring
3392 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003393 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003394 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003395 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003396
Michael Jurkab3e22d92011-10-31 15:58:33 -07003397 // Set focus to the AppsCustomize button
3398 if (mAllAppsButton != null) {
3399 mAllAppsButton.requestFocus();
3400 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003401 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003402
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003403 // Change the state *after* we've called all the transition code
3404 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003405
Winson Chung5fb63472011-02-02 17:03:37 -08003406 // Resume the auto-advance of widgets
3407 mUserPresent = true;
3408 updateRunning();
3409
alanv1d4fde62012-10-17 13:15:47 -07003410 // Send an accessibility event to announce the context change
3411 getWindow().getDecorView()
3412 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003413
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003414 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003415 }
3416
Adam Cohened307df2013-10-02 09:37:31 -07003417 void showOverviewMode(boolean animated) {
3418 mWorkspace.setVisibility(View.VISIBLE);
3419 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3420 mState = State.WORKSPACE;
3421 onWorkspaceShown(animated);
3422 }
3423
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003424 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003425 }
3426
Winson Chung82963d52013-10-09 11:20:57 -07003427 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3428 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429 if (mState != State.WORKSPACE) return;
3430
Winson Chung82963d52013-10-09 11:20:57 -07003431 if (resetPageToZero) {
3432 mAppsCustomizeTabHost.reset();
3433 }
Winson Chungc58497e2013-09-03 17:48:37 -07003434 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003435 mAppsCustomizeTabHost.requestFocus();
3436
Michael Jurkab3e22d92011-10-31 15:58:33 -07003437 // Change the state *after* we've called all the transition code
3438 mState = State.APPS_CUSTOMIZE;
3439
3440 // Pause the auto-advance of widgets until we are out of AllApps
3441 mUserPresent = false;
3442 updateRunning();
3443 closeFolder();
3444
3445 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003446 getWindow().getDecorView()
3447 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 }
3449
Adam Cohen7777d962011-08-18 18:58:38 -07003450 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003451 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003452 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003453 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003454 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003455 }
Adam Cohen7777d962011-08-18 18:58:38 -07003456
Adam Cohenad4e15c2013-10-17 16:21:35 -07003457 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003458 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003459 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3460
Winson Chunge7a03942011-08-05 15:05:12 -07003461 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003462 @Override
3463 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003464 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003465 // Before we show workspace, hide all apps again because
3466 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3467 // clean up our state transition functions
3468 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003469 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003470 } else {
3471 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003472 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003473 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003474 }, delay);
3475
Winson Chung557d6ed2011-07-08 15:34:52 -07003476 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003477
Michael Jurkad3ef3062010-11-23 16:23:58 -08003478 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003479 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003480 final boolean animated = true;
3481 final boolean springLoaded = true;
3482 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003483 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003484 }
3485 // Otherwise, we are not in spring loaded mode, so don't do anything.
3486 }
3487
Michael Jurkab3e22d92011-10-31 15:58:33 -07003488 void lockAllApps() {
3489 // TODO
3490 }
3491
3492 void unlockAllApps() {
3493 // TODO
3494 }
3495
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003497 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003498 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003499 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003500 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003501 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003502 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003503 int duration = 0;
3504 if (mSearchDropTargetBar != null) {
3505 duration = mSearchDropTargetBar.getTransitionInDuration();
3506 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003507 mHotseat.animate().alpha(1f).setDuration(duration);
3508 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003509 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003510 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003511 }
3512 }
3513 }
3514
3515 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003516 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003518 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003519 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003520 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003521 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003522 int duration = 0;
3523 if (mSearchDropTargetBar != null) {
3524 duration = mSearchDropTargetBar.getTransitionOutDuration();
3525 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003526 mHotseat.animate().alpha(0f).setDuration(duration);
3527 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003528 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003529 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003530 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003531 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003532 }
3533
Patrick Dubroy5f445422011-02-18 14:35:21 -08003534 /**
3535 * Add an item from all apps or customize onto the given workspace screen.
3536 * If layout is null, add to the current screen.
3537 */
3538 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003539 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003540 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003541 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003542 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003543
Winson Chungdff8ebb2011-09-08 17:25:31 -07003544 /** Maps the current orientation to an index for referencing orientation correct global icons */
3545 private int getCurrentOrientationIndexForGlobalIcons() {
3546 // default - 0, landscape - 1
3547 switch (getResources().getConfiguration().orientation) {
3548 case Configuration.ORIENTATION_LANDSCAPE:
3549 return 1;
3550 default:
3551 return 0;
3552 }
3553 }
3554
Michael Jurkaae65ee32012-04-30 11:45:54 -07003555 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003556 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003557 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003558 // Look for the toolbar icon specified in the activity meta-data
3559 Bundle metaData = packageManager.getActivityInfo(
3560 activityName, PackageManager.GET_META_DATA).metaData;
3561 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003562 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003563 if (iconResId != 0) {
3564 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003565 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003566 }
3567 }
3568 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003569 // This can happen if the activity defines an invalid drawable
3570 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3571 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003572 } catch (Resources.NotFoundException nfe) {
3573 // This can happen if the activity defines an invalid drawable
3574 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3575 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003576 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003577 return null;
3578 }
3579
3580 // if successful in getting icon, return it; otherwise, set button to use default drawable
3581 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003582 int buttonId, ComponentName activityName, int fallbackDrawableId,
3583 String toolbarResourceName) {
3584 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003585 Resources r = getResources();
3586 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3587 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003588
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003589 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003590 // If we were unable to find the icon via the meta-data, use a generic one
3591 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003592 toolbarIcon = r.getDrawable(fallbackDrawableId);
3593 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003594 if (button != null) {
3595 button.setCompoundDrawables(toolbarIcon, null, null, null);
3596 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003597 return null;
3598 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003599 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003600 if (button != null) {
3601 button.setCompoundDrawables(toolbarIcon, null, null, null);
3602 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003603 return toolbarIcon.getConstantState();
3604 }
3605 }
3606
3607 // if successful in getting icon, return it; otherwise, set button to use default drawable
3608 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003609 int buttonId, ComponentName activityName, int fallbackDrawableId,
3610 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003611 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003612 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003613
Michael Jurka19e0fc52011-07-22 18:00:21 -07003614 if (button != null) {
3615 // If we were unable to find the icon via the meta-data, use a
3616 // generic one
3617 if (toolbarIcon == null) {
3618 button.setImageResource(fallbackDrawableId);
3619 } else {
3620 button.setImageDrawable(toolbarIcon);
3621 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003622 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003623
3624 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3625
Michael Jurka0423dcf2010-10-05 14:56:18 -07003626 }
3627
Winson Chung1b884092012-06-08 17:13:02 -07003628 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003629 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003630 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003631 }
3632
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003633 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003634 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003635 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003636 }
3637
Winson Chungbb185bd2011-11-21 12:31:42 -08003638 private void invalidatePressedFocusedStates(View container, View button) {
3639 if (container instanceof HolographicLinearLayout) {
3640 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3641 layout.invalidatePressedFocusedStates();
3642 } else if (button instanceof HolographicImageView) {
3643 HolographicImageView view = (HolographicImageView) button;
3644 view.invalidatePressedFocusedStates();
3645 }
3646 }
3647
Cristina Stancu476493b2013-08-07 17:20:14 +01003648 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003649 if (mQsb == null) {
3650 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3651 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003652 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003653 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003654 }
3655
3656 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003657 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003658 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003659 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003660 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003661
Winson Chung1cad91e2011-05-25 17:41:01 -07003662 final SearchManager searchManager =
3663 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3664 ComponentName activityName = searchManager.getGlobalSearchActivity();
3665 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003666 int coi = getCurrentOrientationIndexForGlobalIcons();
3667 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003668 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3669 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3670 if (sGlobalSearchIcon[coi] == null) {
3671 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3672 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3673 TOOLBAR_ICON_METADATA_NAME);
3674 }
3675
Winson Chungc51db6a2011-10-05 11:44:49 -07003676 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3677 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003678 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003679 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003680 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003681 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003682 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3683 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003684 if (searchButton != null) searchButton.setVisibility(View.GONE);
3685 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003686 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003687 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003688 }
3689 }
3690
Cristina Stancu476493b2013-08-07 17:20:14 +01003691 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003692 final View searchButtonContainer = findViewById(R.id.search_button_container);
3693 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003694 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003695 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003696 }
3697
Cristina Stancu476493b2013-08-07 17:20:14 +01003698 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003699 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003700 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003701
Winson Chungc51db6a2011-10-05 11:44:49 -07003702 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003703 final SearchManager searchManager =
3704 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3705 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3706
3707 ComponentName activityName = null;
3708 if (globalSearchActivity != null) {
3709 // Check if the global search activity handles voice search
3710 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3711 intent.setPackage(globalSearchActivity.getPackageName());
3712 activityName = intent.resolveActivity(getPackageManager());
3713 }
3714
3715 if (activityName == null) {
3716 // Fallback: check if an activity other than the global search activity
3717 // resolves this
3718 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3719 activityName = intent.resolveActivity(getPackageManager());
3720 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003721 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003722 int coi = getCurrentOrientationIndexForGlobalIcons();
3723 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003724 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3725 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3726 if (sVoiceSearchIcon[coi] == null) {
3727 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3728 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3729 TOOLBAR_ICON_METADATA_NAME);
3730 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003731 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003732 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003733 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003734 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003735 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003736 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003737 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003738 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003739 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003740 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003741 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003742 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003743
Cristina Stancu476493b2013-08-07 17:20:14 +01003744 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003745 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3746 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003747 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003748 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003749 }
3750
Winson Chung5841efa2013-09-30 18:06:44 -07003751 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003752 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3753 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003754 boolean visible = !forceDisableVoiceButtonProxy &&
3755 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003756 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003757 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003758 }
3759 }
Winson Chung5841efa2013-09-30 18:06:44 -07003760
3761 /**
3762 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3763 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3764 */
3765 public void disableVoiceButtonProxy(boolean disabled) {
3766 updateVoiceButtonProxyVisible(disabled);
3767 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003768 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003769 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003770 */
3771 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003772 if (!DISABLE_MARKET_BUTTON) {
3773 final View marketButton = findViewById(R.id.market_button);
3774 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3775 // Find the app market activity by resolving an intent.
3776 // (If multiple app markets are installed, it will return the ResolverActivity.)
3777 ComponentName activityName = intent.resolveActivity(getPackageManager());
3778 if (activityName != null) {
3779 int coi = getCurrentOrientationIndexForGlobalIcons();
3780 mAppMarketIntent = intent;
3781 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3782 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3783 TOOLBAR_ICON_METADATA_NAME);
3784 marketButton.setVisibility(View.VISIBLE);
3785 } else {
3786 // We should hide and disable the view so that we don't try and restore the visibility
3787 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3788 marketButton.setVisibility(View.GONE);
3789 marketButton.setEnabled(false);
3790 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003791 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003792 }
3793
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003794 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003795 if (!DISABLE_MARKET_BUTTON) {
3796 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3797 Resources r = getResources();
3798 Drawable marketIconDrawable = d.newDrawable(r);
3799 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3800 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3801 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003802
Winson Chungd64a6662013-09-30 11:06:59 -07003803 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3804 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003805 }
3806
Michael Jurkad7c28052012-04-27 15:43:36 -07003807 @Override
3808 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003809 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003810 final List<CharSequence> text = event.getText();
3811 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003812 // Populate event with a fake title based on the current state.
3813 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003814 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003815 } else {
3816 text.add(getString(R.string.all_apps_home_button_label));
3817 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003818 return result;
3819 }
3820
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003821 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003822 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003823 */
3824 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3825 @Override
3826 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003827 closeSystemDialogs();
3828 }
3829 }
3830
3831 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003832 * Receives notifications whenever the appwidgets are reset.
3833 */
3834 private class AppWidgetResetObserver extends ContentObserver {
3835 public AppWidgetResetObserver() {
3836 super(new Handler());
3837 }
3838
3839 @Override
3840 public void onChange(boolean selfChange) {
3841 onAppWidgetReset();
3842 }
3843 }
3844
3845 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 * If the activity is currently paused, signal that we need to run the passed Runnable
3847 * in onResume.
3848 *
3849 * This needs to be called from incoming places where resources might have been loaded
3850 * while we are paused. That is becaues the Configuration might be wrong
3851 * when we're not running, and if it comes back to what it was when we
3852 * were paused, we are not restarted.
3853 *
3854 * Implementation of the method from LauncherModel.Callbacks.
3855 *
3856 * @return true if we are currently paused. The caller might be able to
3857 * skip some work in that case since we will come back again.
3858 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003859 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003860 if (mPaused) {
3861 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003862 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003863 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003864 }
3865 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003866 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003867 return true;
3868 } else {
3869 return false;
3870 }
3871 }
3872
Michael Jurkac402cd92013-05-20 15:49:32 +02003873 private boolean waitUntilResume(Runnable run) {
3874 return waitUntilResume(run, false);
3875 }
3876
Michael Jurka1e2f4652013-07-08 18:03:46 -07003877 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003878 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003879 }
3880
Michael Jurka7607c2f2013-04-03 14:33:19 -07003881 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003882 * If the activity is currently paused, signal that we need to re-run the loader
3883 * in onResume.
3884 *
3885 * This needs to be called from incoming places where resources might have been loaded
3886 * while we are paused. That is becaues the Configuration might be wrong
3887 * when we're not running, and if it comes back to what it was when we
3888 * were paused, we are not restarted.
3889 *
3890 * Implementation of the method from LauncherModel.Callbacks.
3891 *
3892 * @return true if we are currently paused. The caller might be able to
3893 * skip some work in that case since we will come back again.
3894 */
3895 public boolean setLoadOnResume() {
3896 if (mPaused) {
3897 Log.i(TAG, "setLoadOnResume");
3898 mOnResumeNeedsLoad = true;
3899 return true;
3900 } else {
3901 return false;
3902 }
3903 }
3904
3905 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003906 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003907 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003909 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003910 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003911 } else {
3912 return SCREEN_COUNT / 2;
3913 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003915
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916 /**
3917 * Refreshes the shortcuts shown on the workspace.
3918 *
3919 * Implementation of the method from LauncherModel.Callbacks.
3920 */
3921 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003922 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003923
Michael Jurka7607c2f2013-04-03 14:33:19 -07003924 // If we're starting binding all over again, clear any bind calls we'd postponed in
3925 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3926 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003927 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003928
Winson Chungd64d1762013-08-20 14:37:16 -07003929 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003930 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003931 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003932
Michael Jurka05bf6442011-11-30 20:28:53 -08003933 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003934 if (mHotseat != null) {
3935 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003936 }
3937 }
3938
Adam Cohendcd297f2013-06-18 13:13:40 -07003939 @Override
3940 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003941 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003942
Adam Cohen5084cba2013-09-03 12:01:16 -07003943 // If there are no screens, we need to have an empty screen
3944 if (orderedScreenIds.size() == 0) {
3945 mWorkspace.addExtraEmptyScreen();
3946 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003947
3948 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003949 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003950 if (hasCustomContentToLeft()) {
3951 mWorkspace.createCustomContentContainer();
3952 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003953 }
Winson Chung64359a52013-07-08 17:17:08 -07003954 }
3955
3956 @Override
3957 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003958 // Log to disk
3959 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3960 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3961 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003962 int count = orderedScreenIds.size();
3963 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003964 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003965 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003966 }
3967
Adam Cohen39a06042013-07-19 14:30:12 -07003968 private boolean shouldShowWeightWatcher() {
3969 String spKey = LauncherAppState.getSharedPreferencesKey();
3970 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003971 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003972
3973 return show;
3974 }
3975
3976 private void toggleShowWeightWatcher() {
3977 String spKey = LauncherAppState.getSharedPreferencesKey();
3978 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3979 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3980
3981 show = !show;
3982
3983 SharedPreferences.Editor editor = sp.edit();
3984 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3985 editor.commit();
3986
3987 if (mWeightWatcher != null) {
3988 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3989 }
3990 }
3991
Winson Chungd64d1762013-08-20 14:37:16 -07003992 public void bindAppsAdded(final ArrayList<Long> newScreens,
3993 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003994 final ArrayList<ItemInfo> addAnimated,
3995 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003996 Runnable r = new Runnable() {
3997 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003998 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003999 }
4000 };
4001 if (waitUntilResume(r)) {
4002 return;
4003 }
4004
Winson Chungd64d1762013-08-20 14:37:16 -07004005 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004006 if (newScreens != null) {
4007 bindAddScreens(newScreens);
4008 }
Winson Chungd64d1762013-08-20 14:37:16 -07004009
4010 // We add the items without animation on non-visible pages, and with
4011 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004012 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004013 bindItems(addNotAnimated, 0,
4014 addNotAnimated.size(), false);
4015 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004016 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004017 bindItems(addAnimated, 0,
4018 addAnimated.size(), true);
4019 }
Winson Chungc58497e2013-09-03 17:48:37 -07004020
Winson Chung87412982013-10-03 18:34:14 -07004021 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004022 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004023
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004024 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004025 addedApps != null && mAppsCustomizeContent != null) {
4026 mAppsCustomizeContent.addApps(addedApps);
4027 }
Winson Chungd64d1762013-08-20 14:37:16 -07004028 }
4029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004030 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004031 * Bind the items start-end from the list.
4032 *
4033 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004034 */
Winson Chung64359a52013-07-08 17:17:08 -07004035 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4036 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004037 Runnable r = new Runnable() {
4038 public void run() {
4039 bindItems(shortcuts, start, end, forceAnimateIcons);
4040 }
4041 };
4042 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004043 return;
4044 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004045
Winson Chungf0c6ae02012-03-21 16:10:31 -07004046 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004047 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4048 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004049 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004050 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004051 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004052 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004054
4055 // Short circuit if we are loading dock items for a configuration which has no dock
4056 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4057 mHotseat == null) {
4058 continue;
4059 }
4060
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 switch (item.itemType) {
4062 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4063 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004064 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004065 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004066
Winson Chung64359a52013-07-08 17:17:08 -07004067 /*
4068 * TODO: FIX collision case
4069 */
Winson Chungce376632013-07-11 16:12:41 -07004070 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4071 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4072 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004073 View v = cl.getChildAt(item.cellX, item.cellY);
4074 Object tag = v.getTag();
4075 String desc = "Collision while binding workspace item: " + item
4076 + ". Collides with " + tag;
4077 if (LauncherAppState.isDogfoodBuild()) {
4078 throw (new RuntimeException(desc));
4079 } else {
4080 Log.d(TAG, desc);
4081 }
Winson Chungce376632013-07-11 16:12:41 -07004082 }
Winson Chung64359a52013-07-08 17:17:08 -07004083 }
4084
Adam Cohendcd297f2013-06-18 13:13:40 -07004085 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4086 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004087 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004088 // Animate all the applications up now
4089 shortcut.setAlpha(0f);
4090 shortcut.setScaleX(0f);
4091 shortcut.setScaleY(0f);
4092 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004093 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004096 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004097 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004098 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004099 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004100 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4101 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004103 default:
4104 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004106 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004107
Winson Chung997a9232013-07-24 15:33:46 -07004108 if (animateIcons) {
4109 // Animate to the correct page
4110 if (newShortcutsScreenId > -1) {
4111 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004112 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004113 final Runnable startBounceAnimRunnable = new Runnable() {
4114 public void run() {
4115 anim.playTogether(bounceAnims);
4116 anim.start();
4117 }
4118 };
Winson Chung997a9232013-07-24 15:33:46 -07004119 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004120 // We post the animation slightly delayed to prevent slowdowns
4121 // when we are loading right after we return to launcher.
4122 mWorkspace.postDelayed(new Runnable() {
4123 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004124 if (mWorkspace != null) {
4125 mWorkspace.snapToPage(newScreenIndex);
4126 mWorkspace.postDelayed(startBounceAnimRunnable,
4127 NEW_APPS_ANIMATION_DELAY);
4128 }
Winson Chung94d67682013-09-25 16:29:40 -07004129 }
4130 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004131 } else {
4132 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004133 }
4134 }
Winson Chung64359a52013-07-08 17:17:08 -07004135 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004136 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004137 }
4138
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 /**
4140 * Implementation of the method from LauncherModel.Callbacks.
4141 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004142 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004143 Runnable r = new Runnable() {
4144 public void run() {
4145 bindFolders(folders);
4146 }
4147 };
4148 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004149 return;
4150 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004151 sFolders.clear();
4152 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004153 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004154
4155 /**
4156 * Add the views for a widget to the workspace.
4157 *
4158 * Implementation of the method from LauncherModel.Callbacks.
4159 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004160 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004161 Runnable r = new Runnable() {
4162 public void run() {
4163 bindAppWidget(item);
4164 }
4165 };
4166 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004167 return;
4168 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004169
Daniel Sandler843e8602010-06-07 14:59:01 -04004170 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4171 if (DEBUG_WIDGETS) {
4172 Log.d(TAG, "bindAppWidget: " + item);
4173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004174 final Workspace workspace = mWorkspace;
4175
4176 final int appWidgetId = item.appWidgetId;
4177 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004178 if (DEBUG_WIDGETS) {
4179 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4180 }
4181
Joe Onorato9c1289c2009-08-17 11:03:03 -04004182 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4183
Joe Onorato9c1289c2009-08-17 11:03:03 -04004184 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004185 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186
Adam Cohendcd297f2013-06-18 13:13:40 -07004187 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004189 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4190
Joe Onorato9c1289c2009-08-17 11:03:03 -04004191 workspace.requestLayout();
4192
Daniel Sandler843e8602010-06-07 14:59:01 -04004193 if (DEBUG_WIDGETS) {
4194 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4195 + (SystemClock.uptimeMillis()-start) + "ms");
4196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004197 }
4198
Adam Cohen1462de32012-07-24 22:34:36 -07004199 public void onPageBoundSynchronously(int page) {
4200 mSynchronouslyBoundPages.add(page);
4201 }
4202
Joe Onorato9c1289c2009-08-17 11:03:03 -04004203 /**
4204 * Callback saying that there aren't any more items to bind.
4205 *
4206 * Implementation of the method from LauncherModel.Callbacks.
4207 */
Adam Cohene25af792013-06-06 23:08:25 -07004208 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004209 Runnable r = new Runnable() {
4210 public void run() {
4211 finishBindingItems(upgradePath);
4212 }
4213 };
4214 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004215 return;
4216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004217 if (mSavedState != null) {
4218 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004219 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004221 mSavedState = null;
4222 }
4223
Adam Cohen1462de32012-07-24 22:34:36 -07004224 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004225
Winson Chungc51db6a2011-10-05 11:44:49 -07004226 // Update the market app icon as necessary (the other icons will be managed in response to
4227 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004228 if (!DISABLE_MARKET_BUTTON) {
4229 updateAppMarketIcon();
4230 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004231
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004232 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004233
4234 // If we received the result of any pending adds while the loader was running (e.g. the
4235 // widget configuration forced an orientation change), process them now.
4236 if (sPendingAddItem != null) {
4237 final long screenId = completeAdd(sPendingAddItem);
4238
4239 // TODO: this moves the user to the page where the pending item was added. Ideally,
4240 // the screen would be guaranteed to exist after bind, and the page would be set through
4241 // the workspace restore process.
4242 mWorkspace.post(new Runnable() {
4243 @Override
4244 public void run() {
4245 mWorkspace.snapToScreenId(screenId);
4246 }
4247 });
4248 sPendingAddItem = null;
4249 }
4250
Adam Cohene25af792013-06-06 23:08:25 -07004251 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004252 mWorkspace.getUniqueComponents(true, null);
4253 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004254 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004255 }
4256
Winson Chunga0b7e862013-09-05 16:03:15 -07004257 public boolean isAllAppsButtonRank(int rank) {
4258 if (mHotseat != null) {
4259 return mHotseat.isAllAppsButtonRank(rank);
4260 }
4261 return false;
4262 }
4263
Winson Chunga2413752012-04-03 14:22:34 -07004264 private boolean canRunNewAppsAnimation() {
4265 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4266 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4267 }
4268
Winson Chungc9168342013-06-26 14:54:55 -07004269 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4270 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4271 PropertyValuesHolder.ofFloat("alpha", 1f),
4272 PropertyValuesHolder.ofFloat("scaleX", 1f),
4273 PropertyValuesHolder.ofFloat("scaleY", 1f));
4274 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4275 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4276 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4277 return bounceAnim;
4278 }
4279
Adam Cohen27772732013-11-11 14:00:56 +00004280 public boolean useVerticalBarLayout() {
4281 return LauncherAppState.getInstance().getDynamicGrid().
4282 getDeviceProfile().isVerticalBarLayout();
4283 }
4284
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004285 protected Rect getSearchBarBounds() {
4286 return LauncherAppState.getInstance().getDynamicGrid().
4287 getDeviceProfile().getSearchBarBounds();
4288 }
4289
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004290 @Override
4291 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004292 boolean searchVisible = updateGlobalSearchIcon();
4293 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004294 if (mSearchDropTargetBar != null) {
4295 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4296 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004297 }
4298
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004299 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004300 * Add the icons for all apps.
4301 *
4302 * Implementation of the method from LauncherModel.Callbacks.
4303 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004304 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004305 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004306 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4307 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4308 getHotseat().addAllAppsFolder(mIconCache, apps,
4309 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4310 }
4311 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004312 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004313 if (mAppsCustomizeContent != null) {
4314 mAppsCustomizeContent.onPackagesUpdated(
4315 LauncherModel.getSortedWidgetsAndShortcuts(this));
4316 }
Winson Chungc58497e2013-09-03 17:48:37 -07004317 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004318 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004319 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004320 mAppsCustomizeContent.onPackagesUpdated(
4321 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004322 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004324 }
4325
4326 /**
4327 * A package was updated.
4328 *
4329 * Implementation of the method from LauncherModel.Callbacks.
4330 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004331 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004332 Runnable r = new Runnable() {
4333 public void run() {
4334 bindAppsUpdated(apps);
4335 }
4336 };
4337 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004338 return;
4339 }
4340
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004341 if (mWorkspace != null) {
4342 mWorkspace.updateShortcuts(apps);
4343 }
Winson Chungc58497e2013-09-03 17:48:37 -07004344
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004345 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004346 mAppsCustomizeContent != null) {
4347 mAppsCustomizeContent.updateApps(apps);
4348 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004349 }
4350
4351 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004352 * Update the state of a package, typically related to install state.
4353 *
4354 * Implementation of the method from LauncherModel.Callbacks.
4355 */
4356 public void updatePackageState(String pkgName, int state) {
4357 if (mWorkspace != null) {
4358 mWorkspace.updatePackageState(pkgName, state);
4359 }
4360 }
4361
4362 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004363 * A package was uninstalled. We take both the super set of packageNames
4364 * in addition to specific applications to remove, the reason being that
4365 * this can be called when a package is updated as well. In that scenario,
4366 * we only remove specific components from the workspace, where as
4367 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004368 *
4369 * Implementation of the method from LauncherModel.Callbacks.
4370 */
Winson Chung83892cc2013-05-01 16:53:33 -07004371 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004372 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004373 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004374 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004375 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004376 }
Winson Chungd64d1762013-08-20 14:37:16 -07004377 };
4378 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004379 return;
4380 }
4381
Winson Chungdf95eb12013-10-16 14:57:07 -07004382 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004383 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004384 }
4385 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004386 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004387 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004388
Winson Chunga1820962011-10-03 16:31:06 -07004389 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004390 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004391
Winson Chungdf95eb12013-10-16 14:57:07 -07004392 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004393 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004394 mAppsCustomizeContent != null) {
4395 mAppsCustomizeContent.removeApps(appInfos);
4396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004397 }
Joe Onoratobe386092009-11-17 17:32:16 -08004398
4399 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004400 * A number of packages were updated.
4401 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004402 private ArrayList<Object> mWidgetsAndShortcuts;
4403 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004404 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004405 bindPackagesUpdated(mWidgetsAndShortcuts);
4406 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004407 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004408 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004409 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4410 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4411 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004412 return;
4413 }
4414
Winson Chung64359a52013-07-08 17:17:08 -07004415 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004416 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004417 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004418 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004419 }
4420
Winson Chung400438b2011-01-16 17:53:48 -08004421 private int mapConfigurationOriActivityInfoOri(int configOri) {
4422 final Display d = getWindowManager().getDefaultDisplay();
4423 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4424 switch (d.getRotation()) {
4425 case Surface.ROTATION_0:
4426 case Surface.ROTATION_180:
4427 // We are currently in the same basic orientation as the natural orientation
4428 naturalOri = configOri;
4429 break;
4430 case Surface.ROTATION_90:
4431 case Surface.ROTATION_270:
4432 // We are currently in the other basic orientation to the natural orientation
4433 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4434 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4435 break;
4436 }
4437
4438 int[] oriMap = {
4439 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4440 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4441 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4442 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4443 };
4444 // Since the map starts at portrait, we need to offset if this device's natural orientation
4445 // is landscape.
4446 int indexOffset = 0;
4447 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4448 indexOffset = 1;
4449 }
4450 return oriMap[(d.getRotation() + indexOffset) % 4];
4451 }
Adam Cohen446e9402011-09-15 18:21:21 -07004452
Winson Chung4b919f82012-05-01 10:44:08 -07004453 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004454 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004455 getResources().getBoolean(R.bool.allow_rotation);
4456 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004457 }
Winson Chung4b919f82012-05-01 10:44:08 -07004458 public void lockScreenOrientation() {
4459 if (isRotationEnabled()) {
4460 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4461 .getConfiguration().orientation));
4462 }
4463 }
4464 public void unlockScreenOrientation(boolean immediate) {
4465 if (isRotationEnabled()) {
4466 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004467 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004468 } else {
4469 mHandler.postDelayed(new Runnable() {
4470 public void run() {
4471 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4472 }
4473 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004474 }
Winson Chung4b919f82012-05-01 10:44:08 -07004475 }
Winson Chung400438b2011-01-16 17:53:48 -08004476 }
4477
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004478 /**
4479 * Called when the SearchBar hint should be changed.
4480 *
4481 * @param hint the hint to be displayed in the search bar.
4482 */
4483 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004484 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004485 }
4486
Winson Chunge43a1e72014-01-15 10:33:02 -08004487 protected boolean isLauncherPreinstalled() {
4488 PackageManager pm = getPackageManager();
4489 try {
4490 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4491 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4492 return true;
4493 } else {
4494 return false;
4495 }
4496 } catch (NameNotFoundException e) {
4497 e.printStackTrace();
4498 return false;
4499 }
4500 }
4501
Adam Cohenea90f832014-05-21 15:03:34 -07004502 /**
4503 * This method indicates whether or not we should suggest default wallpaper dimensions
4504 * when our wallpaper cropper was not yet used to set a wallpaper.
4505 */
4506 protected boolean overrideWallpaperDimensions() {
4507 return true;
4508 }
4509
Adam Cohen5eed5d82014-05-19 13:12:13 -07004510 protected boolean shouldClingFocusHotseatApp() {
4511 return false;
4512 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004513 protected String getFirstRunClingSearchBarHint() {
4514 return "";
4515 }
4516 protected String getFirstRunCustomContentHint() {
4517 return "";
4518 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004519 protected int getFirstRunFocusedHotseatAppDrawableId() {
4520 return -1;
4521 }
4522 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4523 return null;
4524 }
4525 protected int getFirstRunFocusedHotseatAppRank() {
4526 return -1;
4527 }
4528 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4529 return "";
4530 }
4531 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4532 return "";
4533 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004534
Winson Chungaf40f202013-09-18 18:26:31 -07004535 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004536 mLauncherClings.dismissFirstRunCling(v);
4537 }
4538 public void dismissMigrationClingCopyApps(View v) {
4539 mLauncherClings.dismissMigrationClingCopyApps(v);
4540 }
4541 public void dismissMigrationClingUseDefault(View v) {
4542 mLauncherClings.dismissMigrationClingUseDefault(v);
4543 }
4544 public void dismissMigrationWorkspaceCling(View v) {
4545 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004546 }
Winson Chung82f55532011-08-09 14:14:23 -07004547 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004548 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004549 }
4550 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004551 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004552 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004553 public void markFolderClingDismissedIfNecessary() {
4554 mLauncherClings.markFolderClingDismissedIfNecessary();
4555 }
Adam Cohen432609a2014-03-13 17:03:22 -07004556
4557 /**
4558 * To be overridden by subclasses to indicate that there is an activity to launch
4559 * before showing the standard launcher experience.
4560 */
4561 protected boolean hasFirstRunActivity() {
4562 return false;
4563 }
4564
4565 /**
4566 * To be overridden by subclasses to launch any first run activity
4567 */
4568 protected Intent getFirstRunActivity() {
4569 return null;
4570 }
4571
Winson Chunga6945242014-01-08 14:04:34 -08004572 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004573 return !ActivityManager.isRunningInTestHarness() &&
4574 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004575 }
4576
Adam Cohen4a9423d2014-03-28 14:22:31 -07004577 protected boolean hasRunFirstRunActivity() {
4578 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4579 }
4580
Adam Cohen432609a2014-03-13 17:03:22 -07004581 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004582 if (shouldRunFirstRunActivity() &&
4583 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004584 Intent firstRunIntent = getFirstRunActivity();
4585 if (firstRunIntent != null) {
4586 startActivity(firstRunIntent);
4587 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004588 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004589 }
4590 }
Adam Cohen432609a2014-03-13 17:03:22 -07004591 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004592 }
4593
4594 private void markFirstRunActivityShown() {
4595 SharedPreferences.Editor editor = mSharedPrefs.edit();
4596 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4597 editor.apply();
4598 }
4599
Adam Cohen432609a2014-03-13 17:03:22 -07004600 /**
4601 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4602 * screen that must be displayed and dismissed.
4603 */
4604 protected boolean hasDismissableIntroScreen() {
4605 return false;
4606 }
4607
4608 /**
4609 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4610 */
4611 protected View getIntroScreen() {
4612 return null;
4613 }
4614
4615 /**
4616 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4617 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4618 */
4619 private boolean shouldShowIntroScreen() {
4620 return hasDismissableIntroScreen() &&
4621 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4622 }
4623
4624 protected void showIntroScreen() {
4625 View introScreen = getIntroScreen();
4626 changeWallpaperVisiblity(false);
4627 if (introScreen != null) {
4628 mDragLayer.showOverlayView(introScreen);
4629 }
4630 }
4631
4632 public void dismissIntroScreen() {
4633 markIntroScreenDismissed();
4634 if (showFirstRunActivity()) {
4635 // We delay hiding the intro view until the first run activity is showing. This
4636 // avoids a blip.
4637 mWorkspace.postDelayed(new Runnable() {
4638 @Override
4639 public void run() {
4640 mDragLayer.dismissOverlayView();
4641 }
4642 }, ACTIVITY_START_DELAY);
4643 } else {
4644 mDragLayer.dismissOverlayView();
4645 }
4646 changeWallpaperVisiblity(true);
4647 }
4648
4649 private void markIntroScreenDismissed() {
4650 SharedPreferences.Editor editor = mSharedPrefs.edit();
4651 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4652 editor.apply();
4653 }
4654
Winson Chunga6945242014-01-08 14:04:34 -08004655 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004656 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4657 if (mHotseat != null) mHotseat.setAlpha(1f);
4658 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4659 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004660 }
4661
4662 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004663 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4664 if (mHotseat != null) mHotseat.setAlpha(0f);
4665 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4666 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004667 }
4668
Mathew Inwood72fbec12013-11-19 15:45:07 +00004669 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004670 // Called from search suggestion, not supported in other profiles.
4671 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4672 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4673 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4674 myUser);
4675 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004676 return null;
4677 }
Kenny Guyed131872014-04-30 03:02:21 +01004678 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004679 }
4680
4681 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4682 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004683 // Called from search suggestion, not supported in other profiles.
4684 return createShortcutDragInfo(shortcutIntent, caption, icon,
4685 UserHandleCompat.myUserHandle());
4686 }
4687
4688 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4689 Bitmap icon, UserHandleCompat user) {
4690 return new ShortcutInfo(shortcutIntent, caption, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004691 }
4692
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004693 protected void moveWorkspaceToDefaultScreen() {
4694 mWorkspace.moveToDefaultScreen(false);
4695 }
4696
Mathew Inwood72fbec12013-11-19 15:45:07 +00004697 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4698 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004699 mWorkspace.onExternalDragStartedWithItem(dragView);
4700 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004701 }
4702
Anjali Koppalf5d29132014-02-28 13:33:27 -08004703 @Override
4704 public void onPageSwitch(View newPage, int newPageIndex) {
4705 }
4706
Winson Chung80baf5a2010-08-09 16:03:15 -07004707 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004708 * Prints out out state for debugging.
4709 */
4710 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004711 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004712 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004713 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4714 Log.d(TAG, "mRestoring=" + mRestoring);
4715 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4716 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004717 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004718 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004719
Winson Chung785d2eb2011-04-14 16:08:02 -07004720 if (mAppsCustomizeContent != null) {
4721 mAppsCustomizeContent.dumpState();
4722 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004723 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004724 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004725
4726 @Override
4727 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4728 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004729 synchronized (sDumpLogs) {
4730 writer.println(" ");
4731 writer.println("Debug logs: ");
4732 for (int i = 0; i < sDumpLogs.size(); i++) {
4733 writer.println(" " + sDumpLogs.get(i));
4734 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004735 }
4736 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004737
4738 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004739 if (DEBUG_DUMP_LOG) {
4740 synchronized (sDumpLogs) {
4741 Log.d(TAG, "");
4742 Log.d(TAG, "*********************");
4743 Log.d(TAG, "Launcher debug logs: ");
4744 for (int i = 0; i < sDumpLogs.size(); i++) {
4745 Log.d(TAG, " " + sDumpLogs.get(i));
4746 }
4747 Log.d(TAG, "*********************");
4748 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004749 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004750 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004751 }
4752
4753 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004754 addDumpLog(tag, log, null, debugLog);
4755 }
4756
4757 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004758 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004759 if (e != null) {
4760 Log.d(tag, log, e);
4761 } else {
4762 Log.d(tag, log);
4763 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004764 }
Winson Chungede41292013-09-19 16:27:36 -07004765 if (DEBUG_DUMP_LOG) {
4766 sDateStamp.setTime(System.currentTimeMillis());
4767 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004768 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4769 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004770 }
Winson Chungede41292013-09-19 16:27:36 -07004771 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004772 }
4773
Winson Chungede41292013-09-19 16:27:36 -07004774 public void dumpLogsToLocalData() {
4775 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004776 new AsyncTask<Void, Void, Void>() {
4777 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004778 boolean success = false;
4779 sDateStamp.setTime(sRunStart);
4780 String FILENAME = sDateStamp.getMonth() + "-"
4781 + sDateStamp.getDay() + "_"
4782 + sDateStamp.getHours() + "-"
4783 + sDateStamp.getMinutes() + "_"
4784 + sDateStamp.getSeconds() + ".txt";
4785
4786 FileOutputStream fos = null;
4787 File outFile = null;
4788 try {
4789 outFile = new File(getFilesDir(), FILENAME);
4790 outFile.createNewFile();
4791 fos = new FileOutputStream(outFile);
4792 } catch (Exception e) {
4793 e.printStackTrace();
4794 }
4795 if (fos != null) {
4796 PrintWriter writer = new PrintWriter(fos);
4797
4798 writer.println(" ");
4799 writer.println("Debug logs: ");
4800 synchronized (sDumpLogs) {
4801 for (int i = 0; i < sDumpLogs.size(); i++) {
4802 writer.println(" " + sDumpLogs.get(i));
4803 }
4804 }
4805 writer.close();
4806 }
4807 try {
4808 if (fos != null) {
4809 fos.close();
4810 success = true;
4811 }
4812 } catch (IOException e) {
4813 e.printStackTrace();
4814 }
Michael Jurka43467462013-11-14 12:03:58 +01004815 return null;
Winson Chungede41292013-09-19 16:27:36 -07004816 }
Michael Jurka43467462013-11-14 12:03:58 +01004817 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004818 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004820}
Michael Jurka2763be32011-02-24 11:19:57 -08004821
Michael Jurkaabded662011-03-04 12:06:57 -08004822interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004823 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004824 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004825 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004826 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004827 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004828}
Dan Sandlerd5024042014-01-09 15:01:33 -05004829
4830interface DebugIntents {
4831 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4832 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004833}