blob: 0b079682664506282b59e6c2c957f192e55d6f11 [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;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohen0b395352014-06-09 22:54:36 +000085import android.view.Window;
Adam Cohenf358a4b2013-07-23 16:47:31 -070086import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080087import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080090import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070093import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400105import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800106import com.android.launcher3.PagedView.PageSwitchListener;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.DataInputStream;
108import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700110import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000115import java.lang.reflect.Field;
116import java.lang.reflect.InvocationTargetException;
117import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700123import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000124import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
Winson Chunga6945242014-01-08 14:04:34 -0800126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800133 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400137 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
151
Mathew Inwood876a8462013-06-14 14:12:41 +0100152 /**
153 * IntentStarter uses request codes starting with this. This must be greater than all activity
154 * request codes used internally.
155 */
156 protected static final int REQUEST_LAST = 100;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
159
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 static final int SCREEN_COUNT = 5;
161 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Romain Guy98d01652009-06-30 16:21:04 -0700163 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 // To turn on these properties, type
165 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
166 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800168 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Winson Chung2672ff92012-05-04 16:22:30 -0700170 // The Intent extra that defines whether to ignore the launch animation
171 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400172 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
175 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700176 // Type: int
177 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
180 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: boolean
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
188 // Type: long
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: int[]
199 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohenb54a5982014-01-08 15:21:04 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100205 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700206 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100207 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700208 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100209 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700210
Adam Cohen39a06042013-07-19 14:30:12 -0700211 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700212 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700213
Winson Chunga6945242014-01-08 14:04:34 -0800214 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
215
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700217 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700218 private State mState = State.WORKSPACE;
219 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700222 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
223 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700224 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800227 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000229 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
230 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
231
Winson Chunga2413752012-04-03 14:22:34 -0700232 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700233 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
234 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700235 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700236
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800237 private final BroadcastReceiver mCloseSystemDialogsReceiver
238 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800239 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private LayoutInflater mInflater;
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700244 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800245 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800246 private DragLayer mDragLayer;
247 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700248 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800249 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700251 private AppWidgetManager mAppWidgetManager;
252 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Michael Jurkac9d95c52011-08-29 14:03:34 -0700254 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800256 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257
Michael Jurka0280c3b2010-09-17 15:00:07 -0700258 private int[] mTmpAddItemCellCoordinates = new int[2];
259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private FolderInfo mFolderInfo;
261
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266
Winson Chungc51db6a2011-10-05 11:44:49 -0700267 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700268 private AppsCustomizeTabHost mAppsCustomizeTabHost;
269 private AppsCustomizePagedView mAppsCustomizeContent;
270 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800271 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700274 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
275 // scroll issues (because the workspace may not have been measured yet) and extra work.
276 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
277 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
279 private SpannableStringBuilder mDefaultKeySsb = null;
280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private boolean mWorkspaceLoading = true;
282
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800283 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private boolean mRestoring;
285 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700286 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
Michael Jurka1e2f4652013-07-08 18:03:46 -0700288 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700289 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 private Bundle mSavedInstanceState;
292
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mUserPresent = true;
296 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700297 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700300 private static LocaleConfiguration sLocaleConfiguration = null;
301
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700302 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
Adam Cohen61f560d2013-09-30 15:58:20 -0700304 private View.OnTouchListener mHapticFeedbackTouchListener;
305
Adam Cohended9f8d2010-11-03 13:25:16 -0700306 // Related to the auto-advancing of widgets
307 private final int ADVANCE_MSG = 1;
308 private final int mAdvanceInterval = 20000;
309 private final int mAdvanceStagger = 250;
310 private long mAutoAdvanceSentTime;
311 private long mAutoAdvanceTimeLeft = -1;
312 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
313 new HashMap<View, AppWidgetProviderInfo>();
314
Winson Chung400438b2011-01-16 17:53:48 -0800315 // Determines how long to wait after a rotation before restoring the screen orientation to
316 // match the sensor state.
317 private final int mRestoreScreenOrientationDelay = 500;
318
Michael Jurka4ef207b2010-11-29 17:05:45 -0800319 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700320 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
321 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
322 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800323
Winson Chungd64a6662013-09-30 11:06:59 -0700324 private Intent mAppMarketIntent = null;
325 private static final boolean DISABLE_MARKET_BUTTON = true;
326
Craig Mautner360310b2012-10-26 15:13:08 -0700327 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Adam Cohene25af792013-06-06 23:08:25 -0700343 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
344
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 // Holds the page that we need to animate to, and the icon views that we need to animate up
346 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private ImageView mFolderIconImageView;
348 private Bitmap mFolderIconBitmap;
349 private Canvas mFolderIconCanvas;
350 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 private BubbleTextView mWaitingForResume;
353
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 private Runnable mBuildLayersRunnable = new Runnable() {
355 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700356 if (mWorkspace != null) {
357 mWorkspace.buildPageHardwareLayers();
358 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 }
360 };
361
Adam Cohendb364c32014-05-20 14:23:40 -0700362 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363
Michael Jurka7267fa52013-09-26 15:29:57 -0700364 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800365
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 private static class PendingAddArguments {
367 int requestCode;
368 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700369 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700370 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 int cellX;
372 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700373 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374 }
375
Daniel Sandlerff02d492013-08-05 02:12:05 -0400376 private Stats mStats;
377
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800378 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800379 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700380 }
381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400402 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700403 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700404 // Determine the dynamic grid properties
405 Point smallestSize = new Point();
406 Point largestSize = new Point();
407 Point realSize = new Point();
408 Display display = getWindowManager().getDefaultDisplay();
409 display.getCurrentSizeRange(smallestSize, largestSize);
410 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700411 DisplayMetrics dm = new DisplayMetrics();
412 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 // Lazy-initialize the dynamic grid
415 DeviceProfile grid = app.initDynamicGrid(this,
416 Math.min(smallestSize.x, smallestSize.y),
417 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700418 realSize.x, realSize.y,
419 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700420
421 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400422 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700423 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800424 mModel = app.setLauncher(this);
425 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700426 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400427 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800428 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Daniel Sandlerff02d492013-08-05 02:12:05 -0400431 mStats = new Stats(this);
432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700434
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700435 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
436 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700438 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
439 // this also ensures that any synchronous binding below doesn't re-trigger another
440 // LauncherModel load.
441 mPaused = false;
442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200444 android.os.Debug.startMethodTracing(
445 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 }
447
Adam Cohen53805212013-10-01 10:39:23 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100453 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800455 registerContentObservers();
456
Joe Onorato7c312c12009-08-13 21:36:53 -0700457 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 mSavedState = savedInstanceState;
460 restoreState(mSavedState);
461
462 if (PROFILE_STARTUP) {
463 android.os.Debug.stopMethodTracing();
464 }
465
466 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700467 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 // If the user leaves launcher, then we should just load items asynchronously when
469 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500470 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 } else {
472 // We only load the page synchronously if the user rotates (or triggers a
473 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800474 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 }
477
478 // For handling default keys
479 mDefaultKeySsb = new SpannableStringBuilder();
480 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800481
482 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
483 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800484
Adam Cohenf9426d52012-06-04 17:26:21 -0700485 updateGlobalIcons();
486
487 // On large interfaces, we want the screen to auto-rotate based on the current orientation
488 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700489
Adam Cohen432609a2014-03-13 17:03:22 -0700490 if (shouldShowIntroScreen()) {
491 showIntroScreen();
492 } else {
493 showFirstRunActivity();
494 }
495
Winson Chunge43a1e72014-01-15 10:33:02 -0800496 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
497 // on the device, then we always show the first run cling experience (or if there is no
498 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800499 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
500 if (mModel.canMigrateFromOldLauncherDb(this)) {
501 mLauncherClings.showMigrationCling();
502 } else {
503 mLauncherClings.showFirstRunCling();
504 }
Winson Chunga6945242014-01-08 14:04:34 -0800505 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800506 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800507 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700508 }
509
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700510 @Override
511 public void onLauncherProviderChange() { }
512
Winson Chung98ca0f72013-07-29 12:58:51 -0700513 /** To be overriden by subclasses to hint to Launcher that we have custom content */
514 protected boolean hasCustomContentToLeft() {
515 return false;
516 }
517
Dave Hawkeya8881582013-09-17 15:55:33 -0600518 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500519 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600520 * {@link #addToCustomContentPage}. This will only be invoked if
521 * {@link #hasCustomContentToLeft()} is {@code true}.
522 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 }
525
526 /**
527 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
528 * ensure the custom content page is added or removed if necessary.
529 */
530 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600531 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600532 // Not bound yet, wait for bindScreens to be called.
533 return;
534 }
535
536 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
537 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500538 mWorkspace.createCustomContentContainer();
539 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600540 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
541 mWorkspace.removeCustomContentPage();
542 }
543 }
544
Adam Cohenf9426d52012-06-04 17:26:21 -0700545 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700546 boolean searchVisible = false;
547 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800548 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700549 int coi = getCurrentOrientationIndexForGlobalIcons();
550 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
551 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700552 if (!DISABLE_MARKET_BUTTON) {
553 updateAppMarketIcon();
554 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700555 searchVisible = updateGlobalSearchIcon();
556 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700557 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700558 if (sGlobalSearchIcon[coi] != null) {
559 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700560 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sVoiceSearchIcon[coi] != null) {
563 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungd64a6662013-09-30 11:06:59 -0700566 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700567 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800568 }
Winson Chungadf0c182012-08-23 14:59:07 -0700569 if (mSearchDropTargetBar != null) {
570 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
571 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 }
Romain Guycbb89e42009-06-08 15:52:54 -0700573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700575 if (sLocaleConfiguration == null) {
576 new AsyncTask<Void, Void, LocaleConfiguration>() {
577 @Override
578 protected LocaleConfiguration doInBackground(Void... unused) {
579 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
580 readConfiguration(Launcher.this, localeConfiguration);
581 return localeConfiguration;
582 }
583
584 @Override
585 protected void onPostExecute(LocaleConfiguration result) {
586 sLocaleConfiguration = result;
587 checkForLocaleChange(); // recursive, but now with a locale configuration
588 }
589 }.execute();
590 return;
591 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 final Configuration configuration = getResources().getConfiguration();
594
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 final String locale = configuration.locale.toString();
597
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final int mcc = configuration.mcc;
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final int mnc = configuration.mnc;
603
Romain Guy84f296c2009-11-04 15:00:44 -0800604 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605
Romain Guy84f296c2009-11-04 15:00:44 -0800606 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 sLocaleConfiguration.locale = locale;
608 sLocaleConfiguration.mcc = mcc;
609 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700610
Joe Onorato0589f0f2010-02-08 13:44:00 -0800611 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612
613 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100614 new AsyncTask<Void, Void, Void>() {
615 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100617 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 }
Michael Jurka43467462013-11-14 12:03:58 +0100619 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700620 }
621 }
622
623 private static class LocaleConfiguration {
624 public String locale;
625 public int mcc = -1;
626 public int mnc = -1;
627 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700628
Romain Guy98d01652009-06-30 16:21:04 -0700629 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
630 DataInputStream in = null;
631 try {
632 in = new DataInputStream(context.openFileInput(PREFERENCES));
633 configuration.locale = in.readUTF();
634 configuration.mcc = in.readInt();
635 configuration.mnc = in.readInt();
636 } catch (FileNotFoundException e) {
637 // Ignore
638 } catch (IOException e) {
639 // Ignore
640 } finally {
641 if (in != null) {
642 try {
643 in.close();
644 } catch (IOException e) {
645 // Ignore
646 }
647 }
648 }
649 }
650
651 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
652 DataOutputStream out = null;
653 try {
654 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
655 out.writeUTF(configuration.locale);
656 out.writeInt(configuration.mcc);
657 out.writeInt(configuration.mnc);
658 out.flush();
659 } catch (FileNotFoundException e) {
660 // Ignore
661 } catch (IOException e) {
662 //noinspection ResultOfMethodCallIgnored
663 context.getFileStreamPath(PREFERENCES).delete();
664 } finally {
665 if (out != null) {
666 try {
667 out.close();
668 } catch (IOException e) {
669 // Ignore
670 }
671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
673 }
674
Anton Hanssona2f665f2013-09-26 12:18:32 +0100675 public Stats getStats() {
676 return mStats;
677 }
678
Bjorn Bringertc459e522013-06-07 19:36:01 +0100679 public LayoutInflater getInflater() {
680 return mInflater;
681 }
682
Winson Chung36a62fe2012-05-06 18:04:42 -0700683 boolean isDraggingEnabled() {
684 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
685 // that is subsequently removed from the workspace in startBinding().
686 return !mModel.isLoadingWorkspace();
687 }
688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 static int getScreen() {
690 synchronized (sLock) {
691 return sScreen;
692 }
693 }
694
695 static void setScreen(int screen) {
696 synchronized (sLock) {
697 sScreen = screen;
698 }
699 }
700
Winson Chung557d6ed2011-07-08 15:34:52 -0700701 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000702 * Copied from View -- the View version of the method isn't called
703 * anywhere else in our process and only exists for API level 17+,
704 * so it's ok to keep our own version with no API requirement.
705 */
706 public static int generateViewId() {
707 for (;;) {
708 final int result = sNextGeneratedId.get();
709 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
710 int newValue = result + 1;
711 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
712 if (sNextGeneratedId.compareAndSet(result, newValue)) {
713 return result;
714 }
715 }
716 }
717
718 public int getViewIdForItem(ItemInfo info) {
719 // This cast is safe given the > 2B range for int.
720 int itemId = (int) info.id;
721 if (mItemIdToViewId.containsKey(itemId)) {
722 return mItemIdToViewId.get(itemId);
723 }
724 int viewId = generateViewId();
725 mItemIdToViewId.put(itemId, viewId);
726 return viewId;
727 }
728
729 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700730 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
731 * a configuration step, this allows the proper animations to run after other transitions.
732 */
Adam Cohendb364c32014-05-20 14:23:40 -0700733 private long completeAdd(PendingAddArguments args) {
734
735 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800737 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700738 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700740 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700742 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 }
Michael Jurka27614d22012-04-02 06:40:22 -0700745 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
746 // if you turned the screen off and then back while in All Apps, Launcher would not
747 // return to the workspace. Clearing mAddInfo.container here fixes this issue
748 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700749 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800750 }
751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 protected void onActivityResult(
754 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700755 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700756 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800758 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700759
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 Runnable exitSpringLoaded = new Runnable() {
761 @Override
762 public void run() {
763 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
764 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
765 }
766 };
767
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
771 if (resultCode == RESULT_CANCELED) {
772 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700773 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
777 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700778 }
779 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200780 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
781 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
782 mWorkspace.exitOverviewMode(false);
783 }
784 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200786
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
788 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700789
Adam Cohendb364c32014-05-20 14:23:40 -0700790 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 // We have special handling for widgets
792 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800793 final int appWidgetId;
794 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
795 : -1;
796 if (widgetId < 0) {
797 appWidgetId = pendingAddWidgetId;
798 } else {
799 appWidgetId = widgetId;
800 }
801
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800803 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
805 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 result = RESULT_CANCELED;
807 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700808 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700809 @Override
810 public void run() {
811 exitSpringLoadedDragModeDelayed(false, 0, null);
812 }
813 };
Adam Cohendb364c32014-05-20 14:23:40 -0700814 if (workspaceLocked) {
815 // No need to remove the empty screen if we're mid-binding, as the
816 // the bind will not add the empty screen.
817 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
818 } else {
819 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
820 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
821 }
Winson Chung5aaab772012-04-27 15:24:02 -0700822 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700823 if (!workspaceLocked) {
824 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
825 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
826
827 dropLayout.setDropPending(true);
828 final Runnable onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(resultCode, appWidgetId);
832 dropLayout.setDropPending(false);
833 }
834 };
835 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
837 } else {
838 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
839 mPendingAddInfo);
840 sPendingAddItem = args;
841 }
Winson Chung5aaab772012-04-27 15:24:02 -0700842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 return;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // The pattern used here is that a user PICKs a specific application,
847 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
850 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700851 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
853 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700858 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700862 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
Adam Cohendb364c32014-05-20 14:23:40 -0700868 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
869 appWidgetId, ItemInfo info) {
870 PendingAddArguments args = new PendingAddArguments();
871 args.requestCode = requestCode;
872 args.intent = data;
873 args.container = info.container;
874 args.screenId = info.screenId;
875 args.cellX = info.cellX;
876 args.cellY = info.cellY;
877 args.appWidgetId = appWidgetId;
878 return args;
879 }
880
881 /**
882 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
883 *
884 * @param screenId the screen id to check
885 * @return the new screen, or screenId if it exists
886 */
887 private long ensurePendingDropLayoutExists(long screenId) {
888 CellLayout dropLayout =
889 (CellLayout) mWorkspace.getScreenWithId(screenId);
890 if (dropLayout == null) {
891 // it's possible that the add screen was removed because it was
892 // empty and a re-bind occurred
893 mWorkspace.addExtraEmptyScreen();
894 return mWorkspace.commitExtraEmptyScreen();
895 } else {
896 return screenId;
897 }
898 }
899
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700901 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700902 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 Runnable onCompleteRunnable = null;
904 int animationType = 0;
905
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700906 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 if (resultCode == RESULT_OK) {
908 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
909 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 mPendingAddWidgetInfo);
911 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 onCompleteRunnable = new Runnable() {
913 @Override
914 public void run() {
915 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700916 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700917 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
918 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 }
920 };
921 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800922 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 if (mDragLayer.getAnimatedView() != null) {
926 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
927 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
928 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700929 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 // The animated view may be null in the case of a rotation during widget configuration
931 onCompleteRunnable.run();
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700936 protected void onStop() {
937 super.onStop();
938 FirstFrameAnimatorHelper.setIsVisible(false);
939 }
940
941 @Override
942 protected void onStart() {
943 super.onStart();
944 FirstFrameAnimatorHelper.setIsVisible(true);
945 }
946
947 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200949 long startTime = 0;
950 if (DEBUG_RESUME_TIME) {
951 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400952 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700955
Winson Chung4a2afa32012-07-19 14:53:05 -0700956 // Restore the previous launcher state
957 if (mOnResumeState == State.WORKSPACE) {
958 showWorkspace(false);
959 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800960 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700961 }
962 mOnResumeState = State.NONE;
963
Craig Mautner360310b2012-10-26 15:13:08 -0700964 // Background was set to gradient in onPause(), restore to black if in all apps.
965 setWorkspaceBackground(mState == State.WORKSPACE);
966
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800967 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700968 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700969 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500970 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400971 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700972 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700974 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200975 // We might have postponed some bind calls until onResume (see waitUntilResume) --
976 // execute them here
977 long startTimeCallbacks = 0;
978 if (DEBUG_RESUME_TIME) {
979 startTimeCallbacks = System.currentTimeMillis();
980 }
981
982 if (mAppsCustomizeContent != null) {
983 mAppsCustomizeContent.setBulkBind(true);
984 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700985 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
986 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200987 }
988 if (mAppsCustomizeContent != null) {
989 mAppsCustomizeContent.setBulkBind(false);
990 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700991 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200992 if (DEBUG_RESUME_TIME) {
993 Log.d(TAG, "Time spent processing callbacks in onResume: " +
994 (System.currentTimeMillis() - startTimeCallbacks));
995 }
Michael Jurka447bf842013-05-15 14:52:15 +0200996 }
Michael Jurka54554252013-08-01 12:52:23 +0200997 if (mOnResumeCallbacks.size() > 0) {
998 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
999 mOnResumeCallbacks.get(i).run();
1000 }
1001 mOnResumeCallbacks.clear();
1002 }
Winson Chunge4e50662012-01-23 14:45:13 -08001003
1004 // Reset the pressed state of icons that were locked in the press state while activities
1005 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001006 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001007 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001008 mWaitingForResume.setStayPressed(false);
1009 }
Winson Chunge4e50662012-01-23 14:45:13 -08001010 if (mAppsCustomizeContent != null) {
1011 // Resets the previous all apps icon press state
1012 mAppsCustomizeContent.resetDrawableState();
1013 }
Winson Chung82963d52013-10-09 11:20:57 -07001014
Adam Cohen06dff352012-06-01 17:17:08 -07001015 // It is possible that widgets can receive updates while launcher is not in the foreground.
1016 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1017 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1018 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001019 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001020
Winson Chung780fe592013-09-26 14:48:44 -07001021 // Process any items that were added while Launcher was away.
1022 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1023
Winson Chung5841efa2013-09-30 18:06:44 -07001024 // Update the voice search button proxy
1025 updateVoiceButtonProxyVisible(false);
1026
Adam Cohenf9426d52012-06-04 17:26:21 -07001027 // Again, as with the above scenario, it's possible that one or more of the global icons
1028 // were updated in the wrong orientation.
1029 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001030 if (DEBUG_RESUME_TIME) {
1031 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1032 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001033
1034 if (mWorkspace.getCustomContentCallbacks() != null) {
1035 // If we are resuming and the custom content is the current page, we call onShow().
1036 // It is also poassible that onShow will instead be called slightly after first layout
1037 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1038 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001039 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001040 }
1041 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001042 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001043 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001044 }
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001048 // Ensure that items added to Launcher are queued until Launcher returns
1049 InstallShortcutReceiver.enableInstallQueue();
1050
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001052 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001053 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001054 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001055
1056 // We call onHide() aggressively. The custom content callbacks should be able to
1057 // debounce excess onHide calls.
1058 if (mWorkspace.getCustomContentCallbacks() != null) {
1059 mWorkspace.getCustomContentCallbacks().onHide();
1060 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 }
Romain Guycbb89e42009-06-08 15:52:54 -07001062
Adam Cohenbffe7452013-07-22 18:21:45 -07001063 QSBScroller mQsbScroller = new QSBScroller() {
1064 int scrollY = 0;
1065
1066 @Override
1067 public void setScrollY(int scroll) {
1068 scrollY = scroll;
1069
1070 if (mWorkspace.isOnOrMovingToCustomContent()) {
1071 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001072 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001073 }
1074 }
1075 };
1076
1077 public void resetQSBScroll() {
1078 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001079 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 }
1081
1082 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001083 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1084 // by a onResume or by scrolling otherwise.
1085 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001086
1087 // Custom content is completely hidden
1088 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001089
1090 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1091 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
Jorim Jaggid017f882014-01-14 17:08:48 -08001094 protected boolean hasSettings() {
1095 return false;
1096 }
1097
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 public interface QSBScroller {
1099 public void setScrollY(int scrollY);
1100 }
1101
Winson Chung98ca0f72013-07-29 12:58:51 -07001102 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001103 CustomContentCallbacks callbacks, String description) {
1104 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001106 }
1107
Adam Cohenedb40762013-07-18 16:45:45 -07001108 // The custom content needs to offset its content to account for the QSB
1109 public int getTopOffsetForCustomContent() {
1110 return mWorkspace.getPaddingTop();
1111 }
1112
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001113 @Override
1114 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 // Flag the loader to stop early before switching
1116 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 if (mAppsCustomizeContent != null) {
1118 mAppsCustomizeContent.surrender();
1119 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001120 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001121 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001122
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001123 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001124 @Override
1125 public void onWindowFocusChanged(boolean hasFocus) {
1126 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001127 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001128 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 private boolean acceptFilter() {
1131 final InputMethodManager inputManager = (InputMethodManager)
1132 getSystemService(Context.INPUT_METHOD_SERVICE);
1133 return !inputManager.isFullscreenMode();
1134 }
1135
1136 @Override
1137 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001138 final int uniChar = event.getUnicodeChar();
1139 final boolean handled = super.onKeyDown(keyCode, event);
1140 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1141 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1143 keyCode, event);
1144 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001145 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001146 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001147 // showSearchDialog()
1148 // If there are multiple keystrokes before the search dialog takes focus,
1149 // onSearchRequested() will be called for every keystroke,
1150 // but it is idempotent, so it's fine.
1151 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153 }
1154
Joe Onorato8a9625e2010-01-28 15:55:35 -08001155 // Eat the long press event so the keyboard doesn't come up.
1156 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1157 return true;
1158 }
1159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 return handled;
1161 }
1162
Karl Rosaen138a0412009-04-23 19:00:21 -07001163 private String getTypedText() {
1164 return mDefaultKeySsb.toString();
1165 }
1166
1167 private void clearTypedText() {
1168 mDefaultKeySsb.clear();
1169 mDefaultKeySsb.clearSpans();
1170 Selection.setSelection(mDefaultKeySsb, 0);
1171 }
1172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001174 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1175 * State
1176 */
1177 private static State intToState(int stateOrdinal) {
1178 State state = State.WORKSPACE;
1179 final State[] stateValues = State.values();
1180 for (int i = 0; i < stateValues.length; i++) {
1181 if (stateValues[i].ordinal() == stateOrdinal) {
1182 state = stateValues[i];
1183 break;
1184 }
1185 }
1186 return state;
1187 }
1188
1189 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 * Restores the previous state, if it exists.
1191 *
1192 * @param savedState The previous state.
1193 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001194 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 private void restoreState(Bundle savedState) {
1196 if (savedState == null) {
1197 return;
1198 }
1199
Michael Jurka883f55b2010-10-21 15:47:14 -07001200 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001201 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001202 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001203 }
1204
Adam Cohen21cd0022013-10-09 18:57:02 -07001205 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1206 PagedView.INVALID_RESTORE_PAGE);
1207 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001208 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
1210
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213
Winson Chung3d503fb2011-07-13 17:25:49 -07001214 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1215 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001216 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001217 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1218 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001219 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1220 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1221 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001222 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001223 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 mRestoring = true;
1225 }
1226
1227 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1228 if (renameFolder) {
1229 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001230 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 mRestoring = true;
1232 }
Winson Chunga12a2502010-12-20 14:41:35 -08001233
Winson Chung785d2eb2011-04-14 16:08:02 -07001234 // Restore the AppsCustomize tab
1235 if (mAppsCustomizeTabHost != null) {
1236 String curTab = savedState.getString("apps_customize_currentTab");
1237 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001238 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001239 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001240 mAppsCustomizeContent.loadAssociatedPages(
1241 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001242 }
1243
Winson Chung5afbf7b2011-07-25 11:53:08 -07001244 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1245 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001246 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001247 mItemIdToViewId = (HashMap<Integer, Integer>)
1248 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
1251 /**
1252 * Finds all the views we need and configure them properly.
1253 */
1254 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001255 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001256
Craig Mautner360310b2012-10-26 15:13:08 -07001257 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001258 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1259 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001260 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001261 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001262
John Spurlock77e1f472013-09-11 10:09:51 -04001263 mLauncherView.setSystemUiVisibility(
1264 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001265 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
Winson Chung4c98d922011-05-31 16:50:48 -07001267 // Setup the drag layer
1268 mDragLayer.setup(this, dragController);
1269
Winson Chung3d503fb2011-07-13 17:25:49 -07001270 // Setup the hotseat
1271 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1272 if (mHotseat != null) {
1273 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001274 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 }
1276
Jorim Jaggid017f882014-01-14 17:08:48 -08001277 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001278 View widgetButton = findViewById(R.id.widget_button);
1279 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001280 @Override
1281 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001282 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001283 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001284 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001285 }
1286 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001287 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1288
1289 View wallpaperButton = findViewById(R.id.wallpaper_button);
1290 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001291 @Override
1292 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001293 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001294 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001295 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001296 }
1297 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001298 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1299
1300 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001301 if (hasSettings()) {
1302 settingsButton.setOnClickListener(new OnClickListener() {
1303 @Override
1304 public void onClick(View arg0) {
1305 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001306 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001307 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001308 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001309 });
1310 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1311 } else {
1312 settingsButton.setVisibility(View.GONE);
1313 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1314 lp.gravity = Gravity.END | Gravity.TOP;
1315 widgetButton.requestLayout();
1316 }
1317
Adam Cohendbdff6b2013-09-18 19:09:15 -07001318 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001319
Winson Chung4c98d922011-05-31 16:50:48 -07001320 // Setup the workspace
1321 mWorkspace.setHapticFeedbackEnabled(false);
1322 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001323 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001324 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001325
Winson Chungf0ea4d32011-06-06 14:27:16 -07001326 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001327 mSearchDropTargetBar = (SearchDropTargetBar)
1328 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001329
Winson Chungf0ea4d32011-06-06 14:27:16 -07001330 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001331 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001332 mAppsCustomizeContent = (AppsCustomizePagedView)
1333 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1334 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001335
Winson Chung3d503fb2011-07-13 17:25:49 -07001336 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001337 dragController.setDragScoller(mWorkspace);
1338 dragController.setScrollView(mDragLayer);
1339 dragController.setMoveTarget(mWorkspace);
1340 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001341 if (mSearchDropTargetBar != null) {
1342 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001343 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001344
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001345 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001346 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001347 mWeightWatcher = new WeightWatcher(this);
1348 mWeightWatcher.setAlpha(0.5f);
1349 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001350 new FrameLayout.LayoutParams(
1351 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001352 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001353 Gravity.BOTTOM)
1354 );
Adam Cohen39a06042013-07-19 14:30:12 -07001355
1356 boolean show = shouldShowWeightWatcher();
1357 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001358 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
1361 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001362 * Sets the all apps button. This method is called from {@link Hotseat}.
1363 */
1364 public void setAllAppsButton(View allAppsButton) {
1365 mAllAppsButton = allAppsButton;
1366 }
1367
1368 public View getAllAppsButton() {
1369 return mAllAppsButton;
1370 }
1371
1372 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 * Creates a view representing a shortcut.
1374 *
1375 * @param info The data structure describing the shortcut.
1376 *
1377 * @return A View inflated from R.layout.application.
1378 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001379 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001381 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
1383
1384 /**
1385 * Creates a view representing a shortcut inflated from the specified resource.
1386 *
1387 * @param layoutResId The id of the XML layout used to create the shortcut.
1388 * @param parent The group the shortcut belongs to.
1389 * @param info The data structure describing the shortcut.
1390 *
1391 * @return A View inflated from layoutResId.
1392 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001393 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001394 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1395 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 return favorite;
1398 }
1399
1400 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 * Add a shortcut to the workspace.
1402 *
1403 * @param data The intent describing the shortcut.
1404 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001406 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001407 int cellY) {
1408 int[] cellXY = mTmpAddItemCellCoordinates;
1409 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001410 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Michael Jurkad3ef3062010-11-23 16:23:58 -08001412 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001413
Adam Cohen558baaf2011-08-15 15:22:57 -07001414 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001415 if (info == null) {
1416 return;
1417 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001418 final View view = createShortcut(info);
1419
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420 // First we check if we already know the exact location where we want to add this item.
1421 if (cellX >= 0 && cellY >= 0) {
1422 cellXY[0] = cellX;
1423 cellXY[1] = cellY;
1424 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001425
1426 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001427 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001428 true, null,null)) {
1429 return;
1430 }
1431 DragObject dragObject = new DragObject();
1432 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001433 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1434 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001435 return;
1436 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001438 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001439 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001440 foundCellSpan = (result != null);
1441 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001442 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001443 }
1444
1445 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001446 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001447 return;
1448 }
1449
Adam Cohendcd297f2013-06-18 13:13:40 -07001450 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
1452 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001453 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001454 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456 }
1457
Adam Cohen2f093b62012-04-30 18:59:53 -07001458 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1459 int minHeight) {
1460 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001461 // We want to account for the extra amount of padding that we are adding to the widget
1462 // to ensure that it gets the full amount of space that it has requested
1463 int requiredWidth = minWidth + padding.left + padding.right;
1464 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001465 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1469 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001470 }
1471
Adam Cohen2f093b62012-04-30 18:59:53 -07001472 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1473 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001474 }
1475
Adam Cohen2f093b62012-04-30 18:59:53 -07001476 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1477 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001478 }
1479
Adam Cohen2f093b62012-04-30 18:59:53 -07001480 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1481 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001482 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001483 }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001486 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001488 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001489 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001491 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001492 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1493 if (appWidgetInfo == null) {
1494 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1495 }
Romain Guycbb89e42009-06-08 15:52:54 -07001496
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001497 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001498 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001499
Adam Cohen2f093b62012-04-30 18:59:53 -07001500 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1501 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001504 // We have saved the position to which the widget was dragged-- this really only matters
1505 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 int[] cellXY = mTmpAddItemCellCoordinates;
1507 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001508 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1511 cellXY[0] = mPendingAddInfo.cellX;
1512 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001513 spanXY[0] = mPendingAddInfo.spanX;
1514 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001515 foundCellSpan = true;
1516 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001517 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001518 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001519 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1520 spanXY[1], cellXY, finalSpan);
1521 spanXY[0] = finalSpan[0];
1522 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001523 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001526 }
1527
Michael Jurka0280c3b2010-09-17 15:00:07 -07001528 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001529 if (appWidgetId != -1) {
1530 // Deleting an app widget ID is a void call but writes to disk before returning
1531 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001532 new AsyncTask<Void, Void, Void>() {
1533 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001534 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001535 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001536 }
Michael Jurka43467462013-11-14 12:03:58 +01001537 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001538 }
Winson Chung93eef082012-03-23 15:59:27 -07001539 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001540 return;
1541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001544 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1545 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001546 launcherInfo.spanX = spanXY[0];
1547 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001548 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1549 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001552 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 if (hostView == null) {
1556 // Perform actual inflation because we're live
1557 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1558 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1559 } else {
1560 // The AppWidgetHostView has already been inflated and instantiated
1561 launcherInfo.hostView = hostView;
1562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001565 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001566 launcherInfo.notifyWidgetSizeChanged(this);
1567
Adam Cohendcd297f2013-06-18 13:13:40 -07001568 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001569 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001570
1571 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001573 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Adam Cohended9f8d2010-11-03 13:25:16 -07001576 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1577 @Override
1578 public void onReceive(Context context, Intent intent) {
1579 final String action = intent.getAction();
1580 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1581 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001582 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001584
Winson Chungc100e8e2011-08-09 16:02:43 -07001585 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001586 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001587 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001588 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001589 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1591 mUserPresent = true;
1592 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001593 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1594 mModel.resetLoadedState(false, true);
1595 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1596 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1597 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1598 mModel.resetLoadedState(false, true);
1599 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1600 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1601 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001602 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1603 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1604 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001605 }
1606 }
1607 };
1608
1609 @Override
1610 public void onAttachedToWindow() {
1611 super.onAttachedToWindow();
1612
1613 // Listen for broadcasts related to user-presence
1614 final IntentFilter filter = new IntentFilter();
1615 filter.addAction(Intent.ACTION_SCREEN_OFF);
1616 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001617 // For handling managed profiles
1618 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1619 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001620 if (ENABLE_DEBUG_INTENTS) {
1621 filter.addAction(DebugIntents.DELETE_DATABASE);
1622 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1623 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001624 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001625 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001626 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001627 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 mVisible = true;
1629 }
1630
Adam Cohen0b395352014-06-09 22:54:36 +00001631 /**
1632 * Sets up transparent navigation and status bars in LMP.
1633 * This method is a no-op for other platform versions.
1634 */
1635 @TargetApi(19)
1636 private void setupTransparentSystemBarsForLmp() {
1637 // TODO(sansid): use the APIs directly when compiling against L sdk.
1638 // Currently we use reflection to access the flags and the API to set the transparency
1639 // on the System bars.
1640 if (Utilities.isLmp()) {
1641 try {
1642 getWindow().getAttributes().systemUiVisibility |=
1643 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1644 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1645 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1646 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1647 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1648 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1649 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1650 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1651
1652 Method setStatusBarColorMethod =
1653 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1654 Method setNavigationBarColorMethod =
1655 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1656 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1657 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1658 } catch (NoSuchFieldException e) {
1659 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1660 } catch (NoSuchMethodException ex) {
1661 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1662 } catch (IllegalAccessException e) {
1663 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1664 } catch (IllegalArgumentException e) {
1665 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1666 } catch (InvocationTargetException e) {
1667 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1668 } finally {}
1669 }
1670 }
1671
Adam Cohended9f8d2010-11-03 13:25:16 -07001672 @Override
1673 public void onDetachedFromWindow() {
1674 super.onDetachedFromWindow();
1675 mVisible = false;
1676
Adam Cohend113e0c2010-11-11 10:48:05 -08001677 if (mAttached) {
1678 unregisterReceiver(mReceiver);
1679 mAttached = false;
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 updateRunning();
1682 }
1683
1684 public void onWindowVisibilityChanged(int visibility) {
1685 mVisible = visibility == View.VISIBLE;
1686 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001687 // The following code used to be in onResume, but it turns out onResume is called when
1688 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1689 // is a more appropriate event to handle
1690 if (mVisible) {
1691 mAppsCustomizeTabHost.onWindowVisible();
1692 if (!mWorkspaceLoading) {
1693 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001694 // We want to let Launcher draw itself at least once before we force it to build
1695 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001696 // apps is nice and speedy.
1697 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001698 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001699 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001700 if (mStarted) return;
1701 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001702 // We delay the layer building a bit in order to give
1703 // other message processing a time to run. In particular
1704 // this avoids a delay in hiding the IME if it was
1705 // currently shown, because doing that may involve
1706 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001707 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001708 final ViewTreeObserver.OnDrawListener listener = this;
1709 mWorkspace.post(new Runnable() {
1710 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001711 if (mWorkspace != null &&
1712 mWorkspace.getViewTreeObserver() != null) {
1713 mWorkspace.getViewTreeObserver().
1714 removeOnDrawListener(listener);
1715 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001716 }
1717 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001718 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001719 }
1720 });
1721 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001722 // When Launcher comes back to foreground, a different Activity might be responsible for
1723 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001724 if (!DISABLE_MARKET_BUTTON) {
1725 updateAppMarketIcon();
1726 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001727 clearTypedText();
1728 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 }
1730
1731 private void sendAdvanceMessage(long delay) {
1732 mHandler.removeMessages(ADVANCE_MSG);
1733 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1734 mHandler.sendMessageDelayed(msg, delay);
1735 mAutoAdvanceSentTime = System.currentTimeMillis();
1736 }
1737
1738 private void updateRunning() {
1739 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1740 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1741 mAutoAdvanceRunning = autoAdvanceRunning;
1742 if (autoAdvanceRunning) {
1743 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1744 sendAdvanceMessage(delay);
1745 } else {
1746 if (!mWidgetsToAdvance.isEmpty()) {
1747 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1748 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1749 }
1750 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001751 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
1753 }
1754 }
1755
1756 private final Handler mHandler = new Handler() {
1757 @Override
1758 public void handleMessage(Message msg) {
1759 if (msg.what == ADVANCE_MSG) {
1760 int i = 0;
1761 for (View key: mWidgetsToAdvance.keySet()) {
1762 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1763 final int delay = mAdvanceStagger * i;
1764 if (v instanceof Advanceable) {
1765 postDelayed(new Runnable() {
1766 public void run() {
1767 ((Advanceable) v).advance();
1768 }
1769 }, delay);
1770 }
1771 i++;
1772 }
1773 sendAdvanceMessage(mAdvanceInterval);
1774 }
1775 }
1776 };
1777
1778 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001779 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1781 if (v instanceof Advanceable) {
1782 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001783 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 updateRunning();
1785 }
1786 }
1787
1788 void removeWidgetToAutoAdvance(View hostView) {
1789 if (mWidgetsToAdvance.containsKey(hostView)) {
1790 mWidgetsToAdvance.remove(hostView);
1791 updateRunning();
1792 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 }
1794
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001797 launcherInfo.hostView = null;
1798 }
1799
Winson Chung93eef082012-03-23 15:59:27 -07001800 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1801 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1802 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 }
1804
Winson Chunga6945242014-01-08 14:04:34 -08001805 public DragLayer getDragLayer() {
1806 return mDragLayer;
1807 }
1808
1809 public Workspace getWorkspace() {
1810 return mWorkspace;
1811 }
1812
1813 public Hotseat getHotseat() {
1814 return mHotseat;
1815 }
1816
Jorim Jaggid017f882014-01-14 17:08:48 -08001817 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001818 return mOverviewPanel;
1819 }
1820
1821 public SearchDropTargetBar getSearchBar() {
1822 return mSearchDropTargetBar;
1823 }
1824
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001825 public LauncherAppWidgetHost getAppWidgetHost() {
1826 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Winson Chunga9abd0e2010-10-27 17:18:37 -07001829 public LauncherModel getModel() {
1830 return mModel;
1831 }
1832
Winson Chunga6945242014-01-08 14:04:34 -08001833 public LauncherClings getLauncherClings() {
1834 return mLauncherClings;
1835 }
1836
1837 protected SharedPreferences getSharedPrefs() {
1838 return mSharedPrefs;
1839 }
1840
Bjorn Bringertc459e522013-06-07 19:36:01 +01001841 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001842 getWindow().closeAllPanels();
1843
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001844 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001845 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 @Override
1849 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001850 long startTime = 0;
1851 if (DEBUG_RESUME_TIME) {
1852 startTime = System.currentTimeMillis();
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 super.onNewIntent(intent);
1855
1856 // Close the menu
1857 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001858 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001859 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001860
Adam Cohened307df2013-10-02 09:37:31 -07001861 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1862 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1863 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001864
Adam Cohen6fecd412013-10-02 17:41:50 -07001865 if (mWorkspace == null) {
1866 // Can be cases where mWorkspace is null, this prevents a NPE
1867 return;
1868 }
1869 Folder openFolder = mWorkspace.getOpenFolder();
1870 // In all these cases, only animate if we're already on home
1871 mWorkspace.exitWidgetResizeMode();
1872 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001873 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001874 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001875 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001876
Adam Cohen6fecd412013-10-02 17:41:50 -07001877 closeFolder();
1878 exitSpringLoadedDragMode();
1879
1880 // If we are already on home, then just animate back to the workspace,
1881 // otherwise, just wait until onResume to set the state back to Workspace
1882 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001883 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 } else {
1885 mOnResumeState = State.WORKSPACE;
1886 }
1887
1888 final View v = getWindow().peekDecorView();
1889 if (v != null && v.getWindowToken() != null) {
1890 InputMethodManager imm = (InputMethodManager)getSystemService(
1891 INPUT_METHOD_SERVICE);
1892 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1893 }
1894
1895 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001896 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001897 mAppsCustomizeTabHost.reset();
1898 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001899
1900 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
Adam Cohened307df2013-10-02 09:37:31 -07001902
Michael Jurka447bf842013-05-15 14:52:15 +02001903 if (DEBUG_RESUME_TIME) {
1904 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907
Adam Coppa120b8e2013-11-12 16:26:04 +00001908 /**
1909 * Override point for subclasses to prevent movement to the default screen when the home
1910 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1911 */
1912 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1913 return true;
1914 }
1915
1916 /**
1917 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1918 */
1919 protected void onHomeIntent() {
1920 // Do nothing
1921 }
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001924 public void onRestoreInstanceState(Bundle state) {
1925 super.onRestoreInstanceState(state);
1926 for (int page: mSynchronouslyBoundPages) {
1927 mWorkspace.restoreInstanceStateForChild(page);
1928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
1930
1931 @Override
1932 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001933 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001934 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1935 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001936 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001937 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938
Michael Jurka883f55b2010-10-21 15:47:14 -07001939 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001940 // We close any open folder since it will not be re-opened, and we need to make sure
1941 // this state is reflected.
1942 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943
Adam Cohendcd297f2013-06-18 13:13:40 -07001944 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 mWaitingForResult) {
1946 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001948 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1949 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001950 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1951 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1952 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001953 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
1956 if (mFolderInfo != null && mWaitingForResult) {
1957 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1958 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1959 }
Michael Jurka946ad472010-07-09 18:05:18 -07001960
Winson Chung785d2eb2011-04-14 16:08:02 -07001961 // Save the current AppsCustomize tab
1962 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001963 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1964 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001965 if (currentTabTag != null) {
1966 outState.putString("apps_customize_currentTab", currentTabTag);
1967 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001968 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1969 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001970 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001971 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001977
Winson Chunge7a03942011-08-05 15:05:12 -07001978 // Remove all pending runnables
1979 mHandler.removeMessages(ADVANCE_MSG);
1980 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001981 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001982
Winson Chungcd2b0142011-06-08 16:02:26 -07001983 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001984 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001985 mModel.stopLoader();
1986 app.setLauncher(null);
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001989 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001991 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001993 mAppWidgetHost = null;
1994
1995 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
1997 TextKeyListener.getInstance().release();
1998
Winson Chung81b52252012-08-27 15:34:29 -07001999 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2000 // to prevent leaking Launcher activities on orientation change.
2001 if (mModel != null) {
2002 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2003 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002004
2005 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002006 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002007
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002008 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002009 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002010 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002011 mWorkspace = null;
2012 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002013
Michael Jurka2ecf9952012-06-18 12:52:28 -07002014 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
Adam Cohena9cf38f2011-05-02 15:36:58 -07002017 public DragController getDragController() {
2018 return mDragController;
2019 }
2020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 @Override
2022 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002023 if (requestCode >= 0) {
2024 setWaitingForResult(true);
2025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.startActivityForResult(intent, requestCode);
2027 }
2028
Winson Chung70d72102011-08-12 11:24:35 -07002029 /**
2030 * Indicates that we want global search for this activity by setting the globalSearch
2031 * argument for {@link #startSearch} to true.
2032 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002034 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002036
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002037 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Karl Rosaen138a0412009-04-23 19:00:21 -07002039 if (initialQuery == null) {
2040 // Use any text typed in the launcher as the initial query
2041 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 if (appSearchData == null) {
2044 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002045 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
Winson Chungadf0c182012-08-23 14:59:07 -07002047 Rect sourceBounds = new Rect();
2048 if (mSearchDropTargetBar != null) {
2049 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2050 }
Romain Guycbb89e42009-06-08 15:52:54 -07002051
Bjorn Bringertc459e522013-06-07 19:36:01 +01002052 startSearch(initialQuery, selectInitialQuery,
2053 appSearchData, sourceBounds);
2054 }
2055
2056 public void startSearch(String initialQuery,
2057 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002058 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002060 }
2061
2062 /**
2063 * Starts the global search activity. This code is a copied from SearchManager
2064 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002065 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002066 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002068 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2069 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2070 if (globalSearchActivity == null) {
2071 Log.w(TAG, "No global search activity found.");
2072 return;
2073 }
2074 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2075 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2076 intent.setComponent(globalSearchActivity);
2077 // Make sure that we have a Bundle to put source in
2078 if (appSearchData == null) {
2079 appSearchData = new Bundle();
2080 } else {
2081 appSearchData = new Bundle(appSearchData);
2082 }
2083 // Set source to package name of app that starts global search, if not set already.
2084 if (!appSearchData.containsKey("source")) {
2085 appSearchData.putString("source", getPackageName());
2086 }
2087 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2088 if (!TextUtils.isEmpty(initialQuery)) {
2089 intent.putExtra(SearchManager.QUERY, initialQuery);
2090 }
2091 if (selectInitialQuery) {
2092 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2093 }
2094 intent.setSourceBounds(sourceBounds);
2095 try {
2096 startActivity(intent);
2097 } catch (ActivityNotFoundException ex) {
2098 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
2101
Yura4f93ec62014-02-04 14:15:21 +00002102 public boolean isOnCustomContent() {
2103 return mWorkspace.isOnOrMovingToCustomContent();
2104 }
2105
Winson Chung70d72102011-08-12 11:24:35 -07002106 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002107 public boolean onPrepareOptionsMenu(Menu menu) {
2108 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002109 if (!isOnCustomContent()) {
2110 // Close any open folders
2111 closeFolder();
2112 // Stop resizing any widgets
2113 mWorkspace.exitWidgetResizeMode();
2114 if (!mWorkspace.isInOverviewMode()) {
2115 // Show the overview mode
2116 showOverviewMode(true);
2117 } else {
2118 showWorkspace(true);
2119 }
Winson Chunge0298742014-01-17 12:03:00 -08002120 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002121 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002124 @Override
2125 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002126 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002127 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002128 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002129 }
2130
Joe Onorato9c1289c2009-08-17 11:03:03 -04002131 public boolean isWorkspaceLocked() {
2132 return mWorkspaceLoading || mWaitingForResult;
2133 }
2134
Adam Cohen517a7f52014-03-01 12:12:59 -08002135 public boolean isWorkspaceLoading() {
2136 return mWorkspaceLoading;
2137 }
2138
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002139 private void setWorkspaceLoading(boolean value) {
2140 boolean isLocked = isWorkspaceLocked();
2141 mWorkspaceLoading = value;
2142 if (isLocked != isWorkspaceLocked()) {
2143 onWorkspaceLockedChanged();
2144 }
2145 }
2146
2147 private void setWaitingForResult(boolean value) {
2148 boolean isLocked = isWorkspaceLocked();
2149 mWaitingForResult = value;
2150 if (isLocked != isWorkspaceLocked()) {
2151 onWorkspaceLockedChanged();
2152 }
2153 }
2154
2155 protected void onWorkspaceLockedChanged() { }
2156
Michael Jurka0280c3b2010-09-17 15:00:07 -07002157 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002158 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002159 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2161 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002162 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002163 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002164 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002165
Adam Cohenad4e15c2013-10-17 16:21:35 -07002166 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2167 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2168 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2169 }
2170
2171 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2172 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2173 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002174 if (appWidgetInfo.configure != null) {
2175 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002176 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002177
Bjorn Bringert7984c942009-12-09 15:38:25 +00002178 // Launch over to configure widget, if needed
2179 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002180 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002181 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002182 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002184 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002185 Runnable onComplete = new Runnable() {
2186 @Override
2187 public void run() {
2188 // Exit spring loaded mode if necessary after adding the widget
2189 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2190 null);
2191 }
2192 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002194 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002195 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
2197 }
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002199 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002200 // Close any folders that may be open.
2201 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002202 mWorkspace.moveToCustomContentScreen(animate);
2203 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002204 /**
2205 * Process a shortcut drop.
2206 *
2207 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002208 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002209 * @param cell The cell it should be added to, optional
2210 * @param position The location on the screen where it was dropped, optional
2211 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002212 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002213 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002214 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002215 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002216 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002217 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002218
2219 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002220 mPendingAddInfo.cellX = cell[0];
2221 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002222 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002223
2224 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2225 createShortcutIntent.setComponent(componentName);
2226 processShortcut(createShortcutIntent);
2227 }
2228
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 /**
2230 * Process a widget drop.
2231 *
2232 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002233 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002234 * @param cell The cell it should be added to, optional
2235 * @param position The location on the screen where it was dropped, optional
2236 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002238 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002243 mPendingAddInfo.minSpanX = info.minSpanX;
2244 mPendingAddInfo.minSpanY = info.minSpanY;
2245
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.cellX = cell[0];
2248 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002250 if (span != null) {
2251 mPendingAddInfo.spanX = span[0];
2252 mPendingAddInfo.spanY = span[1];
2253 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254
Adam Cohened66b2b2012-01-23 17:28:51 -08002255 AppWidgetHostView hostView = info.boundWidget;
2256 int appWidgetId;
2257 if (hostView != null) {
2258 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002259 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002260 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002261 // In this case, we either need to start an activity to get permission to bind
2262 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002263 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002264 Bundle options = info.bindOptions;
2265
2266 boolean success = false;
2267 if (options != null) {
2268 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2269 info.componentName, options);
2270 } else {
2271 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2272 info.componentName);
2273 }
2274 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002275 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002276 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002277 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002278 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2279 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2280 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002281 // TODO: we need to make sure that this accounts for the options bundle.
2282 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002283 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2284 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002285 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286 }
2287
Joe Onoratodeb98af2010-02-19 14:59:39 -08002288 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002289 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
2291
Winson Chung24ab2f12010-09-16 14:10:47 -07002292 void processWallpaper(Intent intent) {
2293 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2294 }
2295
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002298 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 folderInfo.title = getText(R.string.folder_name);
2300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002304 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305
2306 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 FolderIcon newFolder =
2308 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002311 // Force measure the new folder icon
2312 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2313 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002314 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 }
Romain Guycbb89e42009-06-08 15:52:54 -07002316
Joe Onorato9c1289c2009-08-17 11:03:03 -04002317 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002318 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002319 }
2320
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002321 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002322 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002323 }
2324
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002325 /**
2326 * Registers various content observers. The current implementation registers
2327 * only a favorites observer to keep track of the favorites applications.
2328 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002329 private void registerContentObservers() {
2330 ContentResolver resolver = getContentResolver();
2331 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2332 true, mWidgetObserver);
2333 }
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 @Override
2336 public boolean dispatchKeyEvent(KeyEvent event) {
2337 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2338 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002340 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002341 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002342 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002343 dumpState();
2344 return true;
2345 }
2346 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002347 }
2348 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2349 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002350 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 return true;
2352 }
2353 }
2354
2355 return super.dispatchKeyEvent(event);
2356 }
2357
Joe Onorato88ec0992009-11-19 13:16:06 -08002358 @Override
2359 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002360 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002361 if (mAppsCustomizeContent.getContentType() ==
2362 AppsCustomizePagedView.ContentType.Applications) {
2363 showWorkspace(true);
2364 } else {
2365 showOverviewMode(true);
2366 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002367 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002368 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002369 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002370 Folder openFolder = mWorkspace.getOpenFolder();
2371 if (openFolder.isEditingName()) {
2372 openFolder.dismissEditingName();
2373 } else {
2374 closeFolder();
2375 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002376 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002377 mWorkspace.exitWidgetResizeMode();
2378
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002379 // Back button is a no-op here, but give at least some feedback for the button press
2380 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002381 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002382 }
2383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002385 * Re-listen when widgets are reset.
2386 */
2387 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002388 if (mAppWidgetHost != null) {
2389 mAppWidgetHost.startListening();
2390 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002391 }
2392
2393 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 * Launches the intent referred by the clicked shortcut.
2395 *
2396 * @param v The view representing the clicked shortcut.
2397 */
2398 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002399 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2400 // view has detached (it's possible for this to happen if the view is removed mid touch).
2401 if (v.getWindowToken() == null) {
2402 return;
2403 }
2404
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002405 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002406 return;
2407 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002408
Adam Cohen1697b792013-09-17 19:08:21 -07002409 if (v instanceof Workspace) {
2410 if (mWorkspace.isInOverviewMode()) {
2411 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002412 }
2413 return;
2414 }
2415
2416 if (v instanceof CellLayout) {
2417 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002418 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002419 }
2420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002423 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002424 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002426 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002427 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002428 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002429 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002430 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 }
2432 }
2433
Michael Jurka0e260592010-06-30 17:07:39 -07002434 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002435 return false;
2436 }
2437
Michael Jurkaaf442092010-06-10 17:01:57 -07002438 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002439 * Event handler for the search button
2440 *
2441 * @param v The view that was clicked.
2442 */
2443 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002444 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2445
Amith Yamasania135ba82011-08-09 17:42:01 -07002446 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002447 }
2448
2449 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002450 * Event handler for the voice button
2451 *
2452 * @param v The view that was clicked.
2453 */
2454 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002455 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002456
Bjorn Bringertc459e522013-06-07 19:36:01 +01002457 startVoice();
2458 }
2459
2460 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002461 try {
2462 final SearchManager searchManager =
2463 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2464 ComponentName activityName = searchManager.getGlobalSearchActivity();
2465 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002466 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002467 if (activityName != null) {
2468 intent.setPackage(activityName.getPackageName());
2469 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002470 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002471 } catch (ActivityNotFoundException e) {
2472 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002473 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002474 startActivitySafely(null, intent, "onClickVoiceButton");
2475 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002476 }
2477
2478 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002479 * Event handler for the "grid" button that appears on the home screen, which
2480 * enters all apps mode.
2481 *
2482 * @param v The view that was clicked.
2483 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002484 protected void onClickAllAppsButton(View v) {
2485 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2486 if (isAllAppsVisible()) {
2487 showWorkspace(true);
2488 } else {
2489 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2490 }
2491 }
2492
2493 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002494 * Event handler for a paged view icon click.
2495 * @param v The view that was clicked.
2496 * @param appInfo The {link AppInfo} of the view.
2497 */
2498 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2499 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2500 startActivitySafely(v, appInfo.intent, appInfo);
2501 getStats().recordLaunch(appInfo.intent);
2502 }
2503
2504 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 * Event handler for an app shortcut click.
2506 *
2507 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2508 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002509 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2511 Object tag = v.getTag();
2512 if (!(tag instanceof ShortcutInfo)) {
2513 throw new IllegalArgumentException("Input must be a Shortcut");
2514 }
2515
2516 // Open shortcut
2517 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2518 final Intent intent = shortcut.intent;
2519
2520 // Check for special shortcuts
2521 if (intent.getComponent() != null) {
2522 final String shortcutClass = intent.getComponent().getClassName();
2523
2524 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2525 MemoryDumpActivity.startDump(this);
2526 return;
2527 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2528 toggleShowWeightWatcher();
2529 return;
2530 }
2531 }
2532
Chris Wren40c5ed32014-06-24 18:24:23 -04002533 // Check for abandoned promise
2534 if (shortcut.isAbandoned() && v instanceof BubbleTextView) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002535 AlertDialog.Builder builder = new AlertDialog.Builder(this);
2536 builder.setTitle(R.string.abandoned_promises_title);
Chris Wren803a4be2014-07-01 16:52:49 -04002537 builder.setMessage(R.string.abandoned_promise_explanation);
Chris Wren40c5ed32014-06-24 18:24:23 -04002538 builder.setPositiveButton(R.string.abandoned_search,
2539 new DialogInterface.OnClickListener() {
2540 public void onClick(DialogInterface dialog, int id) {
2541 startAppShortcutActivity(v);
2542 }
2543 }
2544 );
Chris Wren40c5ed32014-06-24 18:24:23 -04002545 builder.setNeutralButton(R.string.abandoned_clean_this,
2546 new DialogInterface.OnClickListener() {
2547 public void onClick(DialogInterface dialog, int id) {
2548 final BubbleTextView bubble = (BubbleTextView) v;
2549 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2550 mWorkspace.removeAbandonedPromise(bubble, user);
2551 }
2552 });
2553 builder.create().show();
2554 return;
2555 }
2556
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002557 // Start activities
Chris Wren40c5ed32014-06-24 18:24:23 -04002558 startAppShortcutActivity(v);
2559 }
2560
2561 private void startAppShortcutActivity(View v) {
2562 Object tag = v.getTag();
2563 if (!(tag instanceof ShortcutInfo)) {
2564 throw new IllegalArgumentException("Input must be a Shortcut");
2565 }
2566 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2567 final Intent intent = shortcut.intent;
2568
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002569 int[] pos = new int[2];
2570 v.getLocationOnScreen(pos);
2571 intent.setSourceBounds(new Rect(pos[0], pos[1],
2572 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2573
2574 boolean success = startActivitySafely(v, intent, tag);
2575
2576 mStats.recordLaunch(intent, shortcut);
2577
2578 if (success && v instanceof BubbleTextView) {
2579 mWaitingForResume = (BubbleTextView) v;
2580 mWaitingForResume.setStayPressed(true);
2581 }
2582 }
2583
2584 /**
2585 * Event handler for a folder icon click.
2586 *
2587 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2588 */
2589 protected void onClickFolderIcon(View v) {
2590 if (LOGD) Log.d(TAG, "onClickFolder");
2591 if (!(v instanceof FolderIcon)){
2592 throw new IllegalArgumentException("Input must be a FolderIcon");
2593 }
2594
2595 FolderIcon folderIcon = (FolderIcon) v;
2596 final FolderInfo info = folderIcon.getFolderInfo();
2597 Folder openFolder = mWorkspace.getFolderForTag(info);
2598
2599 // If the folder info reports that the associated folder is open, then verify that
2600 // it is actually opened. There have been a few instances where this gets out of sync.
2601 if (info.opened && openFolder == null) {
2602 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2603 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2604 info.opened = false;
2605 }
2606
2607 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2608 // Close any open folder
2609 closeFolder();
2610 // Open the requested folder
2611 openFolder(folderIcon);
2612 } else {
2613 // Find the open folder...
2614 int folderScreen;
2615 if (openFolder != null) {
2616 folderScreen = mWorkspace.getPageForView(openFolder);
2617 // .. and close it
2618 closeFolder(openFolder);
2619 if (folderScreen != mWorkspace.getCurrentPage()) {
2620 // Close any folder open on the current screen
2621 closeFolder();
2622 // Pull the folder onto this screen
2623 openFolder(folderIcon);
2624 }
2625 }
2626 }
Michael Jurka5130e402011-10-13 04:55:35 -07002627 }
2628
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002629 /**
2630 * Event handler for the (Add) Widgets button that appears after a long press
2631 * on the home screen.
2632 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002633 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002635 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2636 }
2637
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002638 /**
2639 * Event handler for the wallpaper picker button that appears after a long press
2640 * on the home screen.
2641 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002642 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2644 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2645 pickWallpaper.setComponent(getWallpaperPickerComponent());
2646 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2647 }
2648
2649 /**
2650 * Event handler for a click on the settings button that appears after a long press
2651 * on the home screen.
2652 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002653 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2655 }
2656
Michael Jurka5130e402011-10-13 04:55:35 -07002657 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002658 // Provide the same haptic feedback that the system offers for virtual keys.
2659 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002660 }
2661
Adam Cohen61f560d2013-09-30 15:58:20 -07002662 public void performHapticFeedbackOnTouchDown(View v) {
2663 // Provide the same haptic feedback that the system offers for virtual keys.
2664 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2665 }
2666
2667 public View.OnTouchListener getHapticFeedbackTouchListener() {
2668 if (mHapticFeedbackTouchListener == null) {
2669 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2670 @Override
2671 public boolean onTouch(View v, MotionEvent event) {
2672 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2673 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2674 }
2675 return false;
2676 }
2677 };
2678 }
2679 return mHapticFeedbackTouchListener;
2680 }
2681
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002682 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002683 if (!DISABLE_MARKET_BUTTON) {
2684 if (mAppMarketIntent != null) {
2685 startActivitySafely(v, mAppMarketIntent, "app market");
2686 } else {
2687 Log.e(TAG, "Invalid app market intent.");
2688 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002689 }
2690 }
2691
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002692 public void onDragStarted(View view) {}
2693
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002694 /**
2695 * Called when the user stops interacting with the launcher.
2696 * This implies that the user is now on the homescreen and is not doing housekeeping.
2697 */
2698 protected void onInteractionEnd() {}
2699
2700 /**
2701 * Called when the user starts interacting with the launcher.
2702 * The possible interactions are:
2703 * - open all apps
2704 * - reorder an app shortcut, or a widget
2705 * - open the overview mode.
2706 * This is a good time to stop doing things that only make sense
2707 * when the user is on the homescreen and not doing housekeeping.
2708 */
2709 protected void onInteractionBegin() {}
2710
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002711 void startApplicationDetailsActivity(ComponentName componentName) {
2712 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002713 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2714 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002715 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2716 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002717 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002718 }
2719
Michael Jurka1e2f4652013-07-08 18:03:46 -07002720 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002721 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2722 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002723 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002724 // System applications cannot be installed. For now, show a toast explaining that.
2725 // We may give them the option of disabling apps this way.
2726 int messageId = R.string.uninstall_system_app_text;
2727 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002728 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002729 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002730 String packageName = componentName.getPackageName();
2731 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002732 Intent intent = new Intent(
2733 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002734 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2735 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002736 if (user != null) {
2737 user.addToIntent(intent, Intent.EXTRA_USER);
2738 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002739 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002740 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002741 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002742 }
2743
Michael Jurka86a720e2012-05-09 11:23:23 -07002744 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002745 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002747 // Only launch using the new animation if the shortcut has not opted out (this is a
2748 // private contract between launcher and may be ignored in the future).
2749 boolean useLaunchAnimation = (v != null) &&
2750 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002751 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2752 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002753
Kenny Guy1317e2d2014-05-08 18:52:50 +01002754 UserHandleCompat user = null;
2755 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2756 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2757 user = userManager.getUserForSerialNumber(serialNumber);
2758 }
2759
2760 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002761 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002762 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2763 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002764 optsBundle = opts.toBundle();
2765 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002766 if (useLaunchAnimation && Utilities.isLmp()) {
2767 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2768 optsBundle = opts.toBundle();
2769 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002770
2771 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2772 // Could be launching some bookkeeping activity
2773 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002774 } else {
Kenny Guyc2bd8102014-06-30 12:30:31 +01002775 launcherApps.startActivityForProfile(intent.getComponent(), user,
2776 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002777 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002778 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 } catch (SecurityException e) {
2780 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002781 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002783 "or use the exported attribute for this activity. "
2784 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002786 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002788
Michael Jurka86a720e2012-05-09 11:23:23 -07002789 boolean startActivitySafely(View v, Intent intent, Object tag) {
2790 boolean success = false;
2791 try {
2792 success = startActivity(v, intent, tag);
2793 } catch (ActivityNotFoundException e) {
2794 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2795 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2796 }
2797 return success;
2798 }
2799
Adam Cohen268c4752012-06-06 17:47:33 -07002800 /**
2801 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2802 * in the DragLayer in the exact absolute location of the original FolderIcon.
2803 */
2804 private void copyFolderIconToImage(FolderIcon fi) {
2805 final int width = fi.getMeasuredWidth();
2806 final int height = fi.getMeasuredHeight();
2807
2808 // Lazy load ImageView, Bitmap and Canvas
2809 if (mFolderIconImageView == null) {
2810 mFolderIconImageView = new ImageView(this);
2811 }
2812 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2813 mFolderIconBitmap.getHeight() != height) {
2814 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2815 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2816 }
2817
2818 DragLayer.LayoutParams lp;
2819 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2820 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2821 } else {
2822 lp = new DragLayer.LayoutParams(width, height);
2823 }
2824
Adam Cohen307fe232012-08-16 17:55:58 -07002825 // The layout from which the folder is being opened may be scaled, adjust the starting
2826 // view size by this scale factor.
2827 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002828 lp.customPosition = true;
2829 lp.x = mRectForFolderAnimation.left;
2830 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002831 lp.width = (int) (scale * width);
2832 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002833
2834 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2835 fi.draw(mFolderIconCanvas);
2836 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002837 if (fi.getFolder() != null) {
2838 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2839 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002840 }
Adam Cohen268c4752012-06-06 17:47:33 -07002841 // Just in case this image view is still in the drag layer from a previous animation,
2842 // we remove it and re-add it.
2843 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2844 mDragLayer.removeView(mFolderIconImageView);
2845 }
2846 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002847 if (fi.getFolder() != null) {
2848 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002849 }
Adam Cohen268c4752012-06-06 17:47:33 -07002850 }
2851
Adam Cohen2801caf2011-05-13 20:57:39 -07002852 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002853 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002854 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2855 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2856 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2857
Adam Cohenc51934b2011-07-26 21:07:43 -07002858 FolderInfo info = (FolderInfo) fi.getTag();
2859 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2860 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002861 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2862 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002863 }
2864
Adam Cohen268c4752012-06-06 17:47:33 -07002865 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2866 copyFolderIconToImage(fi);
2867 fi.setVisibility(View.INVISIBLE);
2868
Michael Jurka2ecf9952012-06-18 12:52:28 -07002869 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002870 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002871 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2872 oa.start();
2873 }
2874
Adam Cohen268c4752012-06-06 17:47:33 -07002875 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002876 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002877 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2878 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2879 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2880
Adam Cohen268c4752012-06-06 17:47:33 -07002881 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002882
Adam Cohen268c4752012-06-06 17:47:33 -07002883 // We remove and re-draw the FolderIcon in-case it has changed
2884 mDragLayer.removeView(mFolderIconImageView);
2885 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002886 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002887 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002888 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002889 oa.addListener(new AnimatorListenerAdapter() {
2890 @Override
2891 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002892 if (cl != null) {
2893 cl.clearFolderLeaveBehind();
2894 // Remove the ImageView copy of the FolderIcon and make the original visible.
2895 mDragLayer.removeView(mFolderIconImageView);
2896 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002897 }
2898 }
2899 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002900 oa.start();
2901 }
2902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002904 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 * is animated relative to the specified View. If the View is null, no animation
2906 * is played.
2907 *
2908 * @param folderInfo The FolderInfo describing the folder to open.
2909 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002910 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002911 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002912 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913
Adam Cohena9cf38f2011-05-02 15:36:58 -07002914 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915
Adam Cohen4554ee12011-08-03 16:13:21 -07002916 // Just verify that the folder hasn't already been added to the DragLayer.
2917 // There was a one-off crash where the folder had a parent already.
2918 if (folder.getParent() == null) {
2919 mDragLayer.addView(folder);
2920 mDragController.addDropTarget((DropTarget) folder);
2921 } else {
2922 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2923 folder.getParent() + ").");
2924 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002925 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002926 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002927
2928 // Notify the accessibility manager that this folder "window" has appeared and occluded
2929 // the workspace items
2930 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2931 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002932 }
2933
2934 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002935 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002936 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002937 if (folder.isEditingName()) {
2938 folder.dismissEditingName();
2939 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002940 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002941
2942 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002943 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002944 }
2945 }
2946
2947 void closeFolder(Folder folder) {
2948 folder.getInfo().opened = false;
2949
2950 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2951 if (parent != null) {
2952 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2953 shrinkAndFadeInFolderIcon(fi);
2954 }
2955 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002956
2957 // Notify the accessibility manager that this folder "window" has disappeard and no
2958 // longer occludeds the workspace items
2959 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 }
2961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002963 if (!isDraggingEnabled()) return false;
2964 if (isWorkspaceLocked()) return false;
2965 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966
Adam Cohen1697b792013-09-17 19:08:21 -07002967 if (v instanceof Workspace) {
2968 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002969 if (mWorkspace.enterOverviewMode()) {
2970 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2971 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2972 return true;
2973 } else {
2974 return false;
2975 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002976 } else {
2977 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002978 }
Adam Cohen1697b792013-09-17 19:08:21 -07002979 }
2980
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002981 CellLayout.CellInfo longClickCellInfo = null;
2982 View itemUnderLongClick = null;
2983 if (v.getTag() instanceof ItemInfo) {
2984 ItemInfo info = (ItemInfo) v.getTag();
2985 longClickCellInfo = new CellLayout.CellInfo(v, info);;
2986 itemUnderLongClick = longClickCellInfo.cell;
2987 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 }
2989
Winson Chung3d503fb2011-07-13 17:25:49 -07002990 // The hotseat touch handling does not go through Workspace, and we always allow long press
2991 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05002992 final boolean inHotseat = isHotseatLayout(v);
2993 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002994 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002995 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002996 // User long pressed on empty space
2997 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2998 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07002999 if (mWorkspace.isInOverviewMode()) {
3000 mWorkspace.startReordering(v);
3001 } else {
3002 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003005 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3006 mHotseat.getOrderInHotseat(
3007 longClickCellInfo.cellX,
3008 longClickCellInfo.cellY));
3009 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003011 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
3013 }
3014 }
3015 return true;
3016 }
3017
Winson Chung3d503fb2011-07-13 17:25:49 -07003018 boolean isHotseatLayout(View layout) {
3019 return mHotseat != null && layout != null &&
3020 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3021 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003022
Winson Chung3d503fb2011-07-13 17:25:49 -07003023 /**
3024 * Returns the CellLayout of the specified container at the specified screen.
3025 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003026 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003027 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3028 if (mHotseat != null) {
3029 return mHotseat.getLayout();
3030 } else {
3031 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003032 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003033 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003034 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003035 }
3036 }
3037
Daniel Sandler843e8602010-06-07 14:59:01 -04003038 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003039 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003040 }
3041
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003042 /**
3043 * Helper method for the cameraZoomIn/cameraZoomOut animations
3044 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003045 * @param scaleFactor The scale factor used for the zoom
3046 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003047 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003048 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003049 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003050 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003051
Craig Mautner360310b2012-10-26 15:13:08 -07003052 private void setWorkspaceBackground(boolean workspace) {
3053 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003054 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003055 }
3056
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003057 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003058 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3059 int curflags = getWindow().getAttributes().flags
3060 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3061 if (wpflags != curflags) {
3062 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3063 }
Craig Mautner360310b2012-10-26 15:13:08 -07003064 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003065 }
3066
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003067 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3068 if (v instanceof LauncherTransitionable) {
3069 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3070 }
3071 }
3072
Michael Jurkabed61d22012-02-14 22:51:29 -08003073 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3074 if (v instanceof LauncherTransitionable) {
3075 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3076 }
Winson Chung70442722012-02-10 15:43:22 -08003077
3078 // Update the workspace transition step as well
3079 dispatchOnLauncherTransitionStep(v, 0f);
3080 }
3081
3082 private void dispatchOnLauncherTransitionStep(View v, float t) {
3083 if (v instanceof LauncherTransitionable) {
3084 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3085 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003086 }
3087
3088 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3089 if (v instanceof LauncherTransitionable) {
3090 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3091 }
Winson Chung70442722012-02-10 15:43:22 -08003092
3093 // Update the workspace transition step as well
3094 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003095 }
3096
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003097 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003098 * Things to test when changing the following seven functions.
3099 * - Home from workspace
3100 * - from center screen
3101 * - from other screens
3102 * - Home from all apps
3103 * - from center screen
3104 * - from other screens
3105 * - Back from all apps
3106 * - from center screen
3107 * - from other screens
3108 * - Launch app from workspace and quit
3109 * - with back
3110 * - with home
3111 * - Launch app from all apps and quit
3112 * - with back
3113 * - with home
3114 * - Go to a screen that's not the default, then all
3115 * apps, and launch and app, and go back
3116 * - with back
3117 * -with home
3118 * - On workspace, long press power and go back
3119 * - with back
3120 * - with home
3121 * - On all apps, long press power and go back
3122 * - with back
3123 * - with home
3124 * - On workspace, power off
3125 * - On all apps, power off
3126 * - Launch an app and turn off the screen while in that app
3127 * - Go back with home key
3128 * - Go back with back key TODO: make this not go to workspace
3129 * - From all apps
3130 * - From workspace
3131 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3132 * - From all apps
3133 * - From the center workspace
3134 * - From another workspace
3135 */
3136
3137 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003138 * Zoom the camera out from the workspace to reveal 'toView'.
3139 * Assumes that the view to show is anchored at either the very top or very bottom
3140 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003141 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003142 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003143 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3144 showAppsCustomizeHelper(animated, springLoaded, contentType);
3145 }
3146 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3147 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003148 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003149 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003150 mStateAnimation.cancel();
3151 mStateAnimation = null;
3152 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003153 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003154
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3156 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3157 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003158 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003159 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003160 final int startDelay =
3161 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003162
Michael Jurkab3e22d92011-10-31 15:58:33 -07003163 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003164
Michael Jurkad74c9842011-07-10 12:44:21 -07003165 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003166 Animator workspaceAnim =
3167 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003168 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003169 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3170 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003171 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3172 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003173
3174 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003175 toView.setScaleX(scale);
3176 toView.setScaleY(scale);
3177 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3178 scaleAnim.
3179 scaleX(1f).scaleY(1f).
3180 setDuration(duration).
3181 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003182
Winson Chungf0ea4d32011-06-06 14:27:16 -07003183 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003184 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003185 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003186 .ofFloat(toView, "alpha", 0f, 1f)
3187 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003188 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003189 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3190 @Override
3191 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003192 if (animation == null) {
3193 throw new RuntimeException("animation is null");
3194 }
Winson Chung70442722012-02-10 15:43:22 -08003195 float t = (Float) animation.getAnimatedValue();
3196 dispatchOnLauncherTransitionStep(fromView, t);
3197 dispatchOnLauncherTransitionStep(toView, t);
3198 }
3199 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003200
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003201 // toView should appear right at the end of the workspace shrink
3202 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003203 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003204 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003205 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003206
3207 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003208 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003209 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003210 // Prepare the position
3211 toView.setTranslationX(0.0f);
3212 toView.setTranslationY(0.0f);
3213 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003214 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003215 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003216 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003217 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003218 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3219 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003220
Winson Chungc7d2b602012-05-16 17:02:20 -07003221 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003222 if (mSearchDropTargetBar != null) {
3223 mSearchDropTargetBar.hideSearchBar(false);
3224 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003225 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003226 });
3227
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003228 if (workspaceAnim != null) {
3229 mStateAnimation.play(workspaceAnim);
3230 }
Michael Jurka1899a362011-11-03 13:50:45 -07003231
3232 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003233
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003234 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3235 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003236
3237 // If any of the objects being animated haven't been measured/laid out
3238 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003239 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003240 (mWorkspace.getMeasuredWidth() == 0) ||
3241 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003242 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003243 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003244
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003245 final AnimatorSet stateAnimation = mStateAnimation;
3246 final Runnable startAnimRunnable = new Runnable() {
3247 public void run() {
3248 // Check that mStateAnimation hasn't changed while
3249 // we waited for a layout/draw pass
3250 if (mStateAnimation != stateAnimation)
3251 return;
3252 setPivotsForZoom(toView, scale);
3253 dispatchOnLauncherTransitionStart(fromView, animated, false);
3254 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003255 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003256 }
3257 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003258 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003259 final ViewTreeObserver observer = toView.getViewTreeObserver();
3260 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3261 public void onGlobalLayout() {
3262 startAnimRunnable.run();
3263 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3264 }
3265 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003266 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003267 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003268 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003269 } else {
3270 toView.setTranslationX(0.0f);
3271 toView.setTranslationY(0.0f);
3272 toView.setScaleX(1.0f);
3273 toView.setScaleY(1.0f);
3274 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003275 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003276
Daniel Sandlere4f98912013-06-25 15:13:26 -04003277 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003278 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003279 if (mSearchDropTargetBar != null) {
3280 mSearchDropTargetBar.hideSearchBar(false);
3281 }
Michael Jurkaabded662011-03-04 12:06:57 -08003282 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003283 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003284 dispatchOnLauncherTransitionStart(fromView, animated, false);
3285 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003286 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003287 dispatchOnLauncherTransitionStart(toView, animated, false);
3288 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003289 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003290 }
3291
3292 /**
3293 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003294 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003295 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003296 */
Adam Cohened307df2013-10-02 09:37:31 -07003297 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003298 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003299
Michael Jurkab3e22d92011-10-31 15:58:33 -07003300 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003301 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003302 mStateAnimation.cancel();
3303 mStateAnimation = null;
3304 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003305 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003306
Winson Chungf0ea4d32011-06-06 14:27:16 -07003307 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003308 final int fadeOutDuration =
3309 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003310 final float scaleFactor = (float)
3311 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3312 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003313 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003314 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003315 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003316 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3317 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003318 toState, animated, stagger, -1);
3319 } else if (toState == Workspace.State.SPRING_LOADED ||
3320 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003321 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003322 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003323 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003324
Michael Jurkab3e22d92011-10-31 15:58:33 -07003325 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003326 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003327 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003328 final LauncherViewPropertyAnimator scaleAnim =
3329 new LauncherViewPropertyAnimator(fromView);
3330 scaleAnim.
3331 scaleX(scaleFactor).scaleY(scaleFactor).
3332 setDuration(duration).
3333 setInterpolator(new Workspace.ZoomInInterpolator());
3334
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003335 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003336 .ofFloat(fromView, "alpha", 1f, 0f)
3337 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003338 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003339 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3340 @Override
3341 public void onAnimationUpdate(ValueAnimator animation) {
3342 float t = 1f - (Float) animation.getAnimatedValue();
3343 dispatchOnLauncherTransitionStep(fromView, t);
3344 dispatchOnLauncherTransitionStep(toView, t);
3345 }
3346 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003347
Michael Jurka2ecf9952012-06-18 12:52:28 -07003348 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003349
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003350 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3351 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003352 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003353
3354 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003355 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003356 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003357 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003358 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3359 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003360 if (onCompleteRunnable != null) {
3361 onCompleteRunnable.run();
3362 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003363 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003364 }
3365 });
3366
Winson Chungf0ea4d32011-06-06 14:27:16 -07003367 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003368 if (workspaceAnim != null) {
3369 mStateAnimation.play(workspaceAnim);
3370 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003371 dispatchOnLauncherTransitionStart(fromView, animated, true);
3372 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003373 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003374 } else {
3375 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003376 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003377 dispatchOnLauncherTransitionStart(fromView, animated, true);
3378 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003379 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003380 dispatchOnLauncherTransitionStart(toView, animated, true);
3381 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003382 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003383 }
3384
Michael Jurkae326f182011-11-21 14:05:46 -08003385 @Override
3386 public void onTrimMemory(int level) {
3387 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003388 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003389 mAppsCustomizeTabHost.onTrimMemory();
3390 }
3391 }
3392
Adam Cohened307df2013-10-02 09:37:31 -07003393 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003394 showWorkspace(animated, null);
3395 }
3396
Adam Cohened307df2013-10-02 09:37:31 -07003397 protected void showWorkspace() {
3398 showWorkspace(true);
3399 }
3400
Adam Cohened66b2b2012-01-23 17:28:51 -08003401 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003402 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003403 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003405 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406
Winson Chungc7d2b602012-05-16 17:02:20 -07003407 // Show the search bar (only animate if we were showing the drop target bar in spring
3408 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003409 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003410 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003411 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003412
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413 // Set focus to the AppsCustomize button
3414 if (mAllAppsButton != null) {
3415 mAllAppsButton.requestFocus();
3416 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003417 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003418
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003419 // Change the state *after* we've called all the transition code
3420 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003421
Winson Chung5fb63472011-02-02 17:03:37 -08003422 // Resume the auto-advance of widgets
3423 mUserPresent = true;
3424 updateRunning();
3425
alanv1d4fde62012-10-17 13:15:47 -07003426 // Send an accessibility event to announce the context change
3427 getWindow().getDecorView()
3428 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003429
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003430 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003431 }
3432
Adam Cohened307df2013-10-02 09:37:31 -07003433 void showOverviewMode(boolean animated) {
3434 mWorkspace.setVisibility(View.VISIBLE);
3435 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3436 mState = State.WORKSPACE;
3437 onWorkspaceShown(animated);
3438 }
3439
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003440 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003441 }
3442
Winson Chung82963d52013-10-09 11:20:57 -07003443 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3444 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003445 if (mState != State.WORKSPACE) return;
3446
Winson Chung82963d52013-10-09 11:20:57 -07003447 if (resetPageToZero) {
3448 mAppsCustomizeTabHost.reset();
3449 }
Winson Chungc58497e2013-09-03 17:48:37 -07003450 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003451 mAppsCustomizeTabHost.post(new Runnable() {
3452 @Override
3453 public void run() {
3454 // We post this in-case the all apps view isn't yet constructed.
3455 mAppsCustomizeTabHost.requestFocus();
3456 }
3457 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459 // Change the state *after* we've called all the transition code
3460 mState = State.APPS_CUSTOMIZE;
3461
3462 // Pause the auto-advance of widgets until we are out of AllApps
3463 mUserPresent = false;
3464 updateRunning();
3465 closeFolder();
3466
3467 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003468 getWindow().getDecorView()
3469 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470 }
3471
Adam Cohen7777d962011-08-18 18:58:38 -07003472 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003473 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003474 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003475 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003476 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003477 }
Adam Cohen7777d962011-08-18 18:58:38 -07003478
Adam Cohenad4e15c2013-10-17 16:21:35 -07003479 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003480 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003481 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3482
Winson Chunge7a03942011-08-05 15:05:12 -07003483 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003484 @Override
3485 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003486 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003487 // Before we show workspace, hide all apps again because
3488 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3489 // clean up our state transition functions
3490 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003491 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003492 } else {
3493 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003494 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003496 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003497 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003498
Michael Jurkad3ef3062010-11-23 16:23:58 -08003499 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003500 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003501 final boolean animated = true;
3502 final boolean springLoaded = true;
3503 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003504 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003505 }
3506 // Otherwise, we are not in spring loaded mode, so don't do anything.
3507 }
3508
Michael Jurkab3e22d92011-10-31 15:58:33 -07003509 void lockAllApps() {
3510 // TODO
3511 }
3512
3513 void unlockAllApps() {
3514 // TODO
3515 }
3516
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003518 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003519 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003520 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003521 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003523 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003524 int duration = 0;
3525 if (mSearchDropTargetBar != null) {
3526 duration = mSearchDropTargetBar.getTransitionInDuration();
3527 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003528 mHotseat.animate().alpha(1f).setDuration(duration);
3529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003530 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003531 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003532 }
3533 }
3534 }
3535
3536 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003537 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003538 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003539 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003540 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003541 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003542 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003543 int duration = 0;
3544 if (mSearchDropTargetBar != null) {
3545 duration = mSearchDropTargetBar.getTransitionOutDuration();
3546 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003547 mHotseat.animate().alpha(0f).setDuration(duration);
3548 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003549 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003550 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003551 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003552 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003553 }
3554
Patrick Dubroy5f445422011-02-18 14:35:21 -08003555 /**
3556 * Add an item from all apps or customize onto the given workspace screen.
3557 * If layout is null, add to the current screen.
3558 */
3559 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003560 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003561 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003562 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003563 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003564
Winson Chungdff8ebb2011-09-08 17:25:31 -07003565 /** Maps the current orientation to an index for referencing orientation correct global icons */
3566 private int getCurrentOrientationIndexForGlobalIcons() {
3567 // default - 0, landscape - 1
3568 switch (getResources().getConfiguration().orientation) {
3569 case Configuration.ORIENTATION_LANDSCAPE:
3570 return 1;
3571 default:
3572 return 0;
3573 }
3574 }
3575
Michael Jurkaae65ee32012-04-30 11:45:54 -07003576 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003577 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003578 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003579 // Look for the toolbar icon specified in the activity meta-data
3580 Bundle metaData = packageManager.getActivityInfo(
3581 activityName, PackageManager.GET_META_DATA).metaData;
3582 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003583 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003584 if (iconResId != 0) {
3585 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003586 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003587 }
3588 }
3589 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003590 // This can happen if the activity defines an invalid drawable
3591 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3592 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003593 } catch (Resources.NotFoundException nfe) {
3594 // This can happen if the activity defines an invalid drawable
3595 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3596 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003597 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003598 return null;
3599 }
3600
3601 // if successful in getting icon, return it; otherwise, set button to use default drawable
3602 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003603 int buttonId, ComponentName activityName, int fallbackDrawableId,
3604 String toolbarResourceName) {
3605 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003606 Resources r = getResources();
3607 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3608 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003609
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003610 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003611 // If we were unable to find the icon via the meta-data, use a generic one
3612 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003613 toolbarIcon = r.getDrawable(fallbackDrawableId);
3614 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003615 if (button != null) {
3616 button.setCompoundDrawables(toolbarIcon, null, null, null);
3617 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003618 return null;
3619 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003620 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003621 if (button != null) {
3622 button.setCompoundDrawables(toolbarIcon, null, null, null);
3623 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003624 return toolbarIcon.getConstantState();
3625 }
3626 }
3627
3628 // if successful in getting icon, return it; otherwise, set button to use default drawable
3629 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003630 int buttonId, ComponentName activityName, int fallbackDrawableId,
3631 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003632 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003633 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003634
Michael Jurka19e0fc52011-07-22 18:00:21 -07003635 if (button != null) {
3636 // If we were unable to find the icon via the meta-data, use a
3637 // generic one
3638 if (toolbarIcon == null) {
3639 button.setImageResource(fallbackDrawableId);
3640 } else {
3641 button.setImageDrawable(toolbarIcon);
3642 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003643 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003644
3645 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3646
Michael Jurka0423dcf2010-10-05 14:56:18 -07003647 }
3648
Winson Chung1b884092012-06-08 17:13:02 -07003649 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003650 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003651 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003652 }
3653
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003654 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003655 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003656 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003657 }
3658
Winson Chungbb185bd2011-11-21 12:31:42 -08003659 private void invalidatePressedFocusedStates(View container, View button) {
3660 if (container instanceof HolographicLinearLayout) {
3661 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3662 layout.invalidatePressedFocusedStates();
3663 } else if (button instanceof HolographicImageView) {
3664 HolographicImageView view = (HolographicImageView) button;
3665 view.invalidatePressedFocusedStates();
3666 }
3667 }
3668
Cristina Stancu476493b2013-08-07 17:20:14 +01003669 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003670 if (mQsb == null) {
3671 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3672 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003673 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003674 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003675 }
3676
3677 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003678 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003679 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003680 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003681 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003682
Winson Chung1cad91e2011-05-25 17:41:01 -07003683 final SearchManager searchManager =
3684 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3685 ComponentName activityName = searchManager.getGlobalSearchActivity();
3686 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003687 int coi = getCurrentOrientationIndexForGlobalIcons();
3688 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003689 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3690 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3691 if (sGlobalSearchIcon[coi] == null) {
3692 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3693 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3694 TOOLBAR_ICON_METADATA_NAME);
3695 }
3696
Winson Chungc51db6a2011-10-05 11:44:49 -07003697 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3698 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003699 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003700 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003701 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003702 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003703 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3704 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003705 if (searchButton != null) searchButton.setVisibility(View.GONE);
3706 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003707 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003708 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003709 }
3710 }
3711
Cristina Stancu476493b2013-08-07 17:20:14 +01003712 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003713 final View searchButtonContainer = findViewById(R.id.search_button_container);
3714 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003715 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003716 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003717 }
3718
Cristina Stancu476493b2013-08-07 17:20:14 +01003719 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003720 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003721 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003722
Winson Chungc51db6a2011-10-05 11:44:49 -07003723 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003724 final SearchManager searchManager =
3725 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3726 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3727
3728 ComponentName activityName = null;
3729 if (globalSearchActivity != null) {
3730 // Check if the global search activity handles voice search
3731 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3732 intent.setPackage(globalSearchActivity.getPackageName());
3733 activityName = intent.resolveActivity(getPackageManager());
3734 }
3735
3736 if (activityName == null) {
3737 // Fallback: check if an activity other than the global search activity
3738 // resolves this
3739 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3740 activityName = intent.resolveActivity(getPackageManager());
3741 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003742 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003743 int coi = getCurrentOrientationIndexForGlobalIcons();
3744 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003745 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3746 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3747 if (sVoiceSearchIcon[coi] == null) {
3748 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3749 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3750 TOOLBAR_ICON_METADATA_NAME);
3751 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003752 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003753 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003754 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003755 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003756 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003757 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003758 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003759 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003760 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003761 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003762 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003763 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003764
Cristina Stancu476493b2013-08-07 17:20:14 +01003765 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003766 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3767 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003768 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003769 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003770 }
3771
Winson Chung5841efa2013-09-30 18:06:44 -07003772 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003773 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3774 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003775 boolean visible = !forceDisableVoiceButtonProxy &&
3776 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003777 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003778 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003779 }
3780 }
Winson Chung5841efa2013-09-30 18:06:44 -07003781
3782 /**
3783 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3784 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3785 */
3786 public void disableVoiceButtonProxy(boolean disabled) {
3787 updateVoiceButtonProxyVisible(disabled);
3788 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003789 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003790 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003791 */
3792 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003793 if (!DISABLE_MARKET_BUTTON) {
3794 final View marketButton = findViewById(R.id.market_button);
3795 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3796 // Find the app market activity by resolving an intent.
3797 // (If multiple app markets are installed, it will return the ResolverActivity.)
3798 ComponentName activityName = intent.resolveActivity(getPackageManager());
3799 if (activityName != null) {
3800 int coi = getCurrentOrientationIndexForGlobalIcons();
3801 mAppMarketIntent = intent;
3802 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3803 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3804 TOOLBAR_ICON_METADATA_NAME);
3805 marketButton.setVisibility(View.VISIBLE);
3806 } else {
3807 // We should hide and disable the view so that we don't try and restore the visibility
3808 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3809 marketButton.setVisibility(View.GONE);
3810 marketButton.setEnabled(false);
3811 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003812 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003813 }
3814
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003815 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003816 if (!DISABLE_MARKET_BUTTON) {
3817 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3818 Resources r = getResources();
3819 Drawable marketIconDrawable = d.newDrawable(r);
3820 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3821 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3822 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003823
Winson Chungd64a6662013-09-30 11:06:59 -07003824 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3825 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003826 }
3827
Michael Jurkad7c28052012-04-27 15:43:36 -07003828 @Override
3829 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003830 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003831 final List<CharSequence> text = event.getText();
3832 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003833 // Populate event with a fake title based on the current state.
3834 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003835 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003836 } else {
3837 text.add(getString(R.string.all_apps_home_button_label));
3838 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003839 return result;
3840 }
3841
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003842 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003843 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003844 */
3845 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3846 @Override
3847 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003848 closeSystemDialogs();
3849 }
3850 }
3851
3852 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003853 * Receives notifications whenever the appwidgets are reset.
3854 */
3855 private class AppWidgetResetObserver extends ContentObserver {
3856 public AppWidgetResetObserver() {
3857 super(new Handler());
3858 }
3859
3860 @Override
3861 public void onChange(boolean selfChange) {
3862 onAppWidgetReset();
3863 }
3864 }
3865
3866 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003867 * If the activity is currently paused, signal that we need to run the passed Runnable
3868 * in onResume.
3869 *
3870 * This needs to be called from incoming places where resources might have been loaded
3871 * while we are paused. That is becaues the Configuration might be wrong
3872 * when we're not running, and if it comes back to what it was when we
3873 * were paused, we are not restarted.
3874 *
3875 * Implementation of the method from LauncherModel.Callbacks.
3876 *
3877 * @return true if we are currently paused. The caller might be able to
3878 * skip some work in that case since we will come back again.
3879 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003880 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003881 if (mPaused) {
3882 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003883 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003884 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003885 }
3886 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003887 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003888 return true;
3889 } else {
3890 return false;
3891 }
3892 }
3893
Michael Jurkac402cd92013-05-20 15:49:32 +02003894 private boolean waitUntilResume(Runnable run) {
3895 return waitUntilResume(run, false);
3896 }
3897
Michael Jurka1e2f4652013-07-08 18:03:46 -07003898 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003899 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003900 }
3901
Michael Jurka7607c2f2013-04-03 14:33:19 -07003902 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003903 * If the activity is currently paused, signal that we need to re-run the loader
3904 * in onResume.
3905 *
3906 * This needs to be called from incoming places where resources might have been loaded
3907 * while we are paused. That is becaues the Configuration might be wrong
3908 * when we're not running, and if it comes back to what it was when we
3909 * were paused, we are not restarted.
3910 *
3911 * Implementation of the method from LauncherModel.Callbacks.
3912 *
3913 * @return true if we are currently paused. The caller might be able to
3914 * skip some work in that case since we will come back again.
3915 */
3916 public boolean setLoadOnResume() {
3917 if (mPaused) {
3918 Log.i(TAG, "setLoadOnResume");
3919 mOnResumeNeedsLoad = true;
3920 return true;
3921 } else {
3922 return false;
3923 }
3924 }
3925
3926 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003928 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003930 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003931 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003932 } else {
3933 return SCREEN_COUNT / 2;
3934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003936
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937 /**
3938 * Refreshes the shortcuts shown on the workspace.
3939 *
3940 * Implementation of the method from LauncherModel.Callbacks.
3941 */
3942 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003943 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003944
Michael Jurka7607c2f2013-04-03 14:33:19 -07003945 // If we're starting binding all over again, clear any bind calls we'd postponed in
3946 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3947 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003948 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003949
Winson Chungd64d1762013-08-20 14:37:16 -07003950 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003951 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003952 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003953
Michael Jurka05bf6442011-11-30 20:28:53 -08003954 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003955 if (mHotseat != null) {
3956 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003957 }
3958 }
3959
Adam Cohendcd297f2013-06-18 13:13:40 -07003960 @Override
3961 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003962 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003963
Adam Cohen5084cba2013-09-03 12:01:16 -07003964 // If there are no screens, we need to have an empty screen
3965 if (orderedScreenIds.size() == 0) {
3966 mWorkspace.addExtraEmptyScreen();
3967 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003968
3969 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003970 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003971 if (hasCustomContentToLeft()) {
3972 mWorkspace.createCustomContentContainer();
3973 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003974 }
Winson Chung64359a52013-07-08 17:17:08 -07003975 }
3976
3977 @Override
3978 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003979 // Log to disk
3980 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3981 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3982 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003983 int count = orderedScreenIds.size();
3984 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003985 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003986 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003987 }
3988
Adam Cohen39a06042013-07-19 14:30:12 -07003989 private boolean shouldShowWeightWatcher() {
3990 String spKey = LauncherAppState.getSharedPreferencesKey();
3991 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003992 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003993
3994 return show;
3995 }
3996
3997 private void toggleShowWeightWatcher() {
3998 String spKey = LauncherAppState.getSharedPreferencesKey();
3999 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4000 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4001
4002 show = !show;
4003
4004 SharedPreferences.Editor editor = sp.edit();
4005 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4006 editor.commit();
4007
4008 if (mWeightWatcher != null) {
4009 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4010 }
4011 }
4012
Winson Chungd64d1762013-08-20 14:37:16 -07004013 public void bindAppsAdded(final ArrayList<Long> newScreens,
4014 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004015 final ArrayList<ItemInfo> addAnimated,
4016 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004017 Runnable r = new Runnable() {
4018 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004019 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004020 }
4021 };
4022 if (waitUntilResume(r)) {
4023 return;
4024 }
4025
Winson Chungd64d1762013-08-20 14:37:16 -07004026 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004027 if (newScreens != null) {
4028 bindAddScreens(newScreens);
4029 }
Winson Chungd64d1762013-08-20 14:37:16 -07004030
4031 // We add the items without animation on non-visible pages, and with
4032 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004033 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004034 bindItems(addNotAnimated, 0,
4035 addNotAnimated.size(), false);
4036 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004037 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004038 bindItems(addAnimated, 0,
4039 addAnimated.size(), true);
4040 }
Winson Chungc58497e2013-09-03 17:48:37 -07004041
Winson Chung87412982013-10-03 18:34:14 -07004042 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004043 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004044
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004045 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004046 addedApps != null && mAppsCustomizeContent != null) {
4047 mAppsCustomizeContent.addApps(addedApps);
4048 }
Winson Chungd64d1762013-08-20 14:37:16 -07004049 }
4050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004051 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 * Bind the items start-end from the list.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004055 */
Winson Chung64359a52013-07-08 17:17:08 -07004056 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4057 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004058 Runnable r = new Runnable() {
4059 public void run() {
4060 bindItems(shortcuts, start, end, forceAnimateIcons);
4061 }
4062 };
4063 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004064 return;
4065 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004066
Winson Chungf0c6ae02012-03-21 16:10:31 -07004067 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004068 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4069 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004070 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004071 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004072 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004073 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004074 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004075
4076 // Short circuit if we are loading dock items for a configuration which has no dock
4077 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4078 mHotseat == null) {
4079 continue;
4080 }
4081
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 switch (item.itemType) {
4083 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4084 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004085 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004086 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004087
Winson Chung64359a52013-07-08 17:17:08 -07004088 /*
4089 * TODO: FIX collision case
4090 */
Winson Chungce376632013-07-11 16:12:41 -07004091 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4092 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4093 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004094 View v = cl.getChildAt(item.cellX, item.cellY);
4095 Object tag = v.getTag();
4096 String desc = "Collision while binding workspace item: " + item
4097 + ". Collides with " + tag;
4098 if (LauncherAppState.isDogfoodBuild()) {
4099 throw (new RuntimeException(desc));
4100 } else {
4101 Log.d(TAG, desc);
4102 }
Winson Chungce376632013-07-11 16:12:41 -07004103 }
Winson Chung64359a52013-07-08 17:17:08 -07004104 }
4105
Adam Cohendcd297f2013-06-18 13:13:40 -07004106 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4107 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004108 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004109 // Animate all the applications up now
4110 shortcut.setAlpha(0f);
4111 shortcut.setScaleX(0f);
4112 shortcut.setScaleY(0f);
4113 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004114 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004117 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004118 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004119 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004120 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004121 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4122 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004124 default:
4125 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004127 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004128
Winson Chung997a9232013-07-24 15:33:46 -07004129 if (animateIcons) {
4130 // Animate to the correct page
4131 if (newShortcutsScreenId > -1) {
4132 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004133 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004134 final Runnable startBounceAnimRunnable = new Runnable() {
4135 public void run() {
4136 anim.playTogether(bounceAnims);
4137 anim.start();
4138 }
4139 };
Winson Chung997a9232013-07-24 15:33:46 -07004140 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004141 // We post the animation slightly delayed to prevent slowdowns
4142 // when we are loading right after we return to launcher.
4143 mWorkspace.postDelayed(new Runnable() {
4144 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004145 if (mWorkspace != null) {
4146 mWorkspace.snapToPage(newScreenIndex);
4147 mWorkspace.postDelayed(startBounceAnimRunnable,
4148 NEW_APPS_ANIMATION_DELAY);
4149 }
Winson Chung94d67682013-09-25 16:29:40 -07004150 }
4151 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004152 } else {
4153 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004154 }
4155 }
Winson Chung64359a52013-07-08 17:17:08 -07004156 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004158 }
4159
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 /**
4161 * Implementation of the method from LauncherModel.Callbacks.
4162 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004163 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004164 Runnable r = new Runnable() {
4165 public void run() {
4166 bindFolders(folders);
4167 }
4168 };
4169 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004170 return;
4171 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004172 sFolders.clear();
4173 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175
4176 /**
4177 * Add the views for a widget to the workspace.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004181 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004182 Runnable r = new Runnable() {
4183 public void run() {
4184 bindAppWidget(item);
4185 }
4186 };
4187 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004188 return;
4189 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004190
Daniel Sandler843e8602010-06-07 14:59:01 -04004191 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4192 if (DEBUG_WIDGETS) {
4193 Log.d(TAG, "bindAppWidget: " + item);
4194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004195 final Workspace workspace = mWorkspace;
4196
4197 final int appWidgetId = item.appWidgetId;
4198 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004199 if (DEBUG_WIDGETS) {
4200 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4201 }
4202
Joe Onorato9c1289c2009-08-17 11:03:03 -04004203 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4204
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004206 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004207
Adam Cohendcd297f2013-06-18 13:13:40 -07004208 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004209 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004210 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4211
Joe Onorato9c1289c2009-08-17 11:03:03 -04004212 workspace.requestLayout();
4213
Daniel Sandler843e8602010-06-07 14:59:01 -04004214 if (DEBUG_WIDGETS) {
4215 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4216 + (SystemClock.uptimeMillis()-start) + "ms");
4217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218 }
4219
Adam Cohen1462de32012-07-24 22:34:36 -07004220 public void onPageBoundSynchronously(int page) {
4221 mSynchronouslyBoundPages.add(page);
4222 }
4223
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 /**
4225 * Callback saying that there aren't any more items to bind.
4226 *
4227 * Implementation of the method from LauncherModel.Callbacks.
4228 */
Adam Cohene25af792013-06-06 23:08:25 -07004229 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004230 Runnable r = new Runnable() {
4231 public void run() {
4232 finishBindingItems(upgradePath);
4233 }
4234 };
4235 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004236 return;
4237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 if (mSavedState != null) {
4239 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004240 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004242 mSavedState = null;
4243 }
4244
Adam Cohen1462de32012-07-24 22:34:36 -07004245 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246
Winson Chungc51db6a2011-10-05 11:44:49 -07004247 // Update the market app icon as necessary (the other icons will be managed in response to
4248 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004249 if (!DISABLE_MARKET_BUTTON) {
4250 updateAppMarketIcon();
4251 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004252
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004253 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004254
4255 // If we received the result of any pending adds while the loader was running (e.g. the
4256 // widget configuration forced an orientation change), process them now.
4257 if (sPendingAddItem != null) {
4258 final long screenId = completeAdd(sPendingAddItem);
4259
4260 // TODO: this moves the user to the page where the pending item was added. Ideally,
4261 // the screen would be guaranteed to exist after bind, and the page would be set through
4262 // the workspace restore process.
4263 mWorkspace.post(new Runnable() {
4264 @Override
4265 public void run() {
4266 mWorkspace.snapToScreenId(screenId);
4267 }
4268 });
4269 sPendingAddItem = null;
4270 }
4271
Adam Cohene25af792013-06-06 23:08:25 -07004272 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004273 mWorkspace.getUniqueComponents(true, null);
4274 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004275 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004276 }
4277
Winson Chunga0b7e862013-09-05 16:03:15 -07004278 public boolean isAllAppsButtonRank(int rank) {
4279 if (mHotseat != null) {
4280 return mHotseat.isAllAppsButtonRank(rank);
4281 }
4282 return false;
4283 }
4284
Winson Chunga2413752012-04-03 14:22:34 -07004285 private boolean canRunNewAppsAnimation() {
4286 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4287 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4288 }
4289
Winson Chungc9168342013-06-26 14:54:55 -07004290 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4291 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4292 PropertyValuesHolder.ofFloat("alpha", 1f),
4293 PropertyValuesHolder.ofFloat("scaleX", 1f),
4294 PropertyValuesHolder.ofFloat("scaleY", 1f));
4295 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4296 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4297 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4298 return bounceAnim;
4299 }
4300
Adam Cohen27772732013-11-11 14:00:56 +00004301 public boolean useVerticalBarLayout() {
4302 return LauncherAppState.getInstance().getDynamicGrid().
4303 getDeviceProfile().isVerticalBarLayout();
4304 }
4305
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004306 protected Rect getSearchBarBounds() {
4307 return LauncherAppState.getInstance().getDynamicGrid().
4308 getDeviceProfile().getSearchBarBounds();
4309 }
4310
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004311 @Override
4312 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004313 boolean searchVisible = updateGlobalSearchIcon();
4314 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004315 if (mSearchDropTargetBar != null) {
4316 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4317 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004318 }
4319
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004320 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004321 * Add the icons for all apps.
4322 *
4323 * Implementation of the method from LauncherModel.Callbacks.
4324 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004325 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004326 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004327 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4328 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4329 getHotseat().addAllAppsFolder(mIconCache, apps,
4330 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4331 }
4332 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004333 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004334 if (mAppsCustomizeContent != null) {
4335 mAppsCustomizeContent.onPackagesUpdated(
4336 LauncherModel.getSortedWidgetsAndShortcuts(this));
4337 }
Winson Chungc58497e2013-09-03 17:48:37 -07004338 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004339 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004340 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004341 mAppsCustomizeContent.onPackagesUpdated(
4342 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004343 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004344 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004345 }
4346
4347 /**
4348 * A package was updated.
4349 *
4350 * Implementation of the method from LauncherModel.Callbacks.
4351 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004352 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004353 Runnable r = new Runnable() {
4354 public void run() {
4355 bindAppsUpdated(apps);
4356 }
4357 };
4358 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004359 return;
4360 }
4361
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004362 if (mWorkspace != null) {
4363 mWorkspace.updateShortcuts(apps);
4364 }
Winson Chungc58497e2013-09-03 17:48:37 -07004365
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004366 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004367 mAppsCustomizeContent != null) {
4368 mAppsCustomizeContent.updateApps(apps);
4369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370 }
4371
4372 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004373 * Update the state of a package, typically related to install state.
4374 *
4375 * Implementation of the method from LauncherModel.Callbacks.
4376 */
4377 public void updatePackageState(String pkgName, int state) {
4378 if (mWorkspace != null) {
4379 mWorkspace.updatePackageState(pkgName, state);
4380 }
4381 }
4382
4383 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004384 * A package was uninstalled. We take both the super set of packageNames
4385 * in addition to specific applications to remove, the reason being that
4386 * this can be called when a package is updated as well. In that scenario,
4387 * we only remove specific components from the workspace, where as
4388 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004389 *
4390 * Implementation of the method from LauncherModel.Callbacks.
4391 */
Winson Chung83892cc2013-05-01 16:53:33 -07004392 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004393 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004394 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004395 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004396 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004397 }
Winson Chungd64d1762013-08-20 14:37:16 -07004398 };
4399 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004400 return;
4401 }
4402
Winson Chungdf95eb12013-10-16 14:57:07 -07004403 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004404 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004405 }
4406 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004407 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004408 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004409
Winson Chunga1820962011-10-03 16:31:06 -07004410 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004411 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004412
Winson Chungdf95eb12013-10-16 14:57:07 -07004413 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004414 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004415 mAppsCustomizeContent != null) {
4416 mAppsCustomizeContent.removeApps(appInfos);
4417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004418 }
Joe Onoratobe386092009-11-17 17:32:16 -08004419
4420 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004421 * A number of packages were updated.
4422 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004423 private ArrayList<Object> mWidgetsAndShortcuts;
4424 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004425 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004426 bindPackagesUpdated(mWidgetsAndShortcuts);
4427 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004428 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004429 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004430 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4431 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4432 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004433 return;
4434 }
4435
Winson Chung64359a52013-07-08 17:17:08 -07004436 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004437 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004438 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004439 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004440 }
4441
Winson Chung400438b2011-01-16 17:53:48 -08004442 private int mapConfigurationOriActivityInfoOri(int configOri) {
4443 final Display d = getWindowManager().getDefaultDisplay();
4444 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4445 switch (d.getRotation()) {
4446 case Surface.ROTATION_0:
4447 case Surface.ROTATION_180:
4448 // We are currently in the same basic orientation as the natural orientation
4449 naturalOri = configOri;
4450 break;
4451 case Surface.ROTATION_90:
4452 case Surface.ROTATION_270:
4453 // We are currently in the other basic orientation to the natural orientation
4454 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4455 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4456 break;
4457 }
4458
4459 int[] oriMap = {
4460 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4461 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4462 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4463 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4464 };
4465 // Since the map starts at portrait, we need to offset if this device's natural orientation
4466 // is landscape.
4467 int indexOffset = 0;
4468 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4469 indexOffset = 1;
4470 }
4471 return oriMap[(d.getRotation() + indexOffset) % 4];
4472 }
Adam Cohen446e9402011-09-15 18:21:21 -07004473
Winson Chung4b919f82012-05-01 10:44:08 -07004474 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004475 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004476 getResources().getBoolean(R.bool.allow_rotation);
4477 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004478 }
Winson Chung4b919f82012-05-01 10:44:08 -07004479 public void lockScreenOrientation() {
4480 if (isRotationEnabled()) {
4481 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4482 .getConfiguration().orientation));
4483 }
4484 }
4485 public void unlockScreenOrientation(boolean immediate) {
4486 if (isRotationEnabled()) {
4487 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004488 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004489 } else {
4490 mHandler.postDelayed(new Runnable() {
4491 public void run() {
4492 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4493 }
4494 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004495 }
Winson Chung4b919f82012-05-01 10:44:08 -07004496 }
Winson Chung400438b2011-01-16 17:53:48 -08004497 }
4498
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004499 /**
4500 * Called when the SearchBar hint should be changed.
4501 *
4502 * @param hint the hint to be displayed in the search bar.
4503 */
4504 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004505 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004506 }
4507
Winson Chunge43a1e72014-01-15 10:33:02 -08004508 protected boolean isLauncherPreinstalled() {
4509 PackageManager pm = getPackageManager();
4510 try {
4511 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4512 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4513 return true;
4514 } else {
4515 return false;
4516 }
4517 } catch (NameNotFoundException e) {
4518 e.printStackTrace();
4519 return false;
4520 }
4521 }
4522
Adam Cohenea90f832014-05-21 15:03:34 -07004523 /**
4524 * This method indicates whether or not we should suggest default wallpaper dimensions
4525 * when our wallpaper cropper was not yet used to set a wallpaper.
4526 */
4527 protected boolean overrideWallpaperDimensions() {
4528 return true;
4529 }
4530
Adam Cohen5eed5d82014-05-19 13:12:13 -07004531 protected boolean shouldClingFocusHotseatApp() {
4532 return false;
4533 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004534 protected String getFirstRunClingSearchBarHint() {
4535 return "";
4536 }
4537 protected String getFirstRunCustomContentHint() {
4538 return "";
4539 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004540 protected int getFirstRunFocusedHotseatAppDrawableId() {
4541 return -1;
4542 }
4543 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4544 return null;
4545 }
4546 protected int getFirstRunFocusedHotseatAppRank() {
4547 return -1;
4548 }
4549 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4550 return "";
4551 }
4552 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4553 return "";
4554 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004555
Winson Chungaf40f202013-09-18 18:26:31 -07004556 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004557 mLauncherClings.dismissFirstRunCling(v);
4558 }
4559 public void dismissMigrationClingCopyApps(View v) {
4560 mLauncherClings.dismissMigrationClingCopyApps(v);
4561 }
4562 public void dismissMigrationClingUseDefault(View v) {
4563 mLauncherClings.dismissMigrationClingUseDefault(v);
4564 }
4565 public void dismissMigrationWorkspaceCling(View v) {
4566 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004567 }
Winson Chung82f55532011-08-09 14:14:23 -07004568 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004569 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004570 }
4571 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004572 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004573 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004574 public void markFolderClingDismissedIfNecessary() {
4575 mLauncherClings.markFolderClingDismissedIfNecessary();
4576 }
Adam Cohen432609a2014-03-13 17:03:22 -07004577
4578 /**
4579 * To be overridden by subclasses to indicate that there is an activity to launch
4580 * before showing the standard launcher experience.
4581 */
4582 protected boolean hasFirstRunActivity() {
4583 return false;
4584 }
4585
4586 /**
4587 * To be overridden by subclasses to launch any first run activity
4588 */
4589 protected Intent getFirstRunActivity() {
4590 return null;
4591 }
4592
Winson Chunga6945242014-01-08 14:04:34 -08004593 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004594 return !ActivityManager.isRunningInTestHarness() &&
4595 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004596 }
4597
Adam Cohen4a9423d2014-03-28 14:22:31 -07004598 protected boolean hasRunFirstRunActivity() {
4599 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4600 }
4601
Adam Cohen432609a2014-03-13 17:03:22 -07004602 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004603 if (shouldRunFirstRunActivity() &&
4604 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004605 Intent firstRunIntent = getFirstRunActivity();
4606 if (firstRunIntent != null) {
4607 startActivity(firstRunIntent);
4608 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004609 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004610 }
4611 }
Adam Cohen432609a2014-03-13 17:03:22 -07004612 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004613 }
4614
4615 private void markFirstRunActivityShown() {
4616 SharedPreferences.Editor editor = mSharedPrefs.edit();
4617 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4618 editor.apply();
4619 }
4620
Adam Cohen432609a2014-03-13 17:03:22 -07004621 /**
4622 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4623 * screen that must be displayed and dismissed.
4624 */
4625 protected boolean hasDismissableIntroScreen() {
4626 return false;
4627 }
4628
4629 /**
4630 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4631 */
4632 protected View getIntroScreen() {
4633 return null;
4634 }
4635
4636 /**
4637 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4638 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4639 */
4640 private boolean shouldShowIntroScreen() {
4641 return hasDismissableIntroScreen() &&
4642 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4643 }
4644
4645 protected void showIntroScreen() {
4646 View introScreen = getIntroScreen();
4647 changeWallpaperVisiblity(false);
4648 if (introScreen != null) {
4649 mDragLayer.showOverlayView(introScreen);
4650 }
4651 }
4652
4653 public void dismissIntroScreen() {
4654 markIntroScreenDismissed();
4655 if (showFirstRunActivity()) {
4656 // We delay hiding the intro view until the first run activity is showing. This
4657 // avoids a blip.
4658 mWorkspace.postDelayed(new Runnable() {
4659 @Override
4660 public void run() {
4661 mDragLayer.dismissOverlayView();
4662 }
4663 }, ACTIVITY_START_DELAY);
4664 } else {
4665 mDragLayer.dismissOverlayView();
4666 }
4667 changeWallpaperVisiblity(true);
4668 }
4669
4670 private void markIntroScreenDismissed() {
4671 SharedPreferences.Editor editor = mSharedPrefs.edit();
4672 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4673 editor.apply();
4674 }
4675
Winson Chunga6945242014-01-08 14:04:34 -08004676 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004677 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4678 if (mHotseat != null) mHotseat.setAlpha(1f);
4679 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4680 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004681 }
4682
4683 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004684 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4685 if (mHotseat != null) mHotseat.setAlpha(0f);
4686 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4687 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004688 }
4689
Mathew Inwood72fbec12013-11-19 15:45:07 +00004690 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004691 // Called from search suggestion, not supported in other profiles.
4692 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4693 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4694 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4695 myUser);
4696 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004697 return null;
4698 }
Kenny Guyed131872014-04-30 03:02:21 +01004699 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004700 }
4701
4702 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4703 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004704 // Called from search suggestion, not supported in other profiles.
4705 return createShortcutDragInfo(shortcutIntent, caption, icon,
4706 UserHandleCompat.myUserHandle());
4707 }
4708
4709 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4710 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004711 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
4712 String contentDescription = userManager.getBadgedLabelForUser(caption.toString(), user);
4713 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004714 }
4715
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004716 protected void moveWorkspaceToDefaultScreen() {
4717 mWorkspace.moveToDefaultScreen(false);
4718 }
4719
Mathew Inwood72fbec12013-11-19 15:45:07 +00004720 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4721 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004722 mWorkspace.onExternalDragStartedWithItem(dragView);
4723 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004724 }
4725
Anjali Koppalf5d29132014-02-28 13:33:27 -08004726 @Override
4727 public void onPageSwitch(View newPage, int newPageIndex) {
4728 }
4729
Winson Chung80baf5a2010-08-09 16:03:15 -07004730 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004731 * Prints out out state for debugging.
4732 */
4733 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004734 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004735 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004736 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4737 Log.d(TAG, "mRestoring=" + mRestoring);
4738 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4739 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004740 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004741 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004742
Winson Chung785d2eb2011-04-14 16:08:02 -07004743 if (mAppsCustomizeContent != null) {
4744 mAppsCustomizeContent.dumpState();
4745 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004746 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004747 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004748
4749 @Override
4750 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4751 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004752 synchronized (sDumpLogs) {
4753 writer.println(" ");
4754 writer.println("Debug logs: ");
4755 for (int i = 0; i < sDumpLogs.size(); i++) {
4756 writer.println(" " + sDumpLogs.get(i));
4757 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004758 }
4759 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004760
4761 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004762 if (DEBUG_DUMP_LOG) {
4763 synchronized (sDumpLogs) {
4764 Log.d(TAG, "");
4765 Log.d(TAG, "*********************");
4766 Log.d(TAG, "Launcher debug logs: ");
4767 for (int i = 0; i < sDumpLogs.size(); i++) {
4768 Log.d(TAG, " " + sDumpLogs.get(i));
4769 }
4770 Log.d(TAG, "*********************");
4771 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004772 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004773 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004774 }
4775
4776 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004777 addDumpLog(tag, log, null, debugLog);
4778 }
4779
4780 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004781 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004782 if (e != null) {
4783 Log.d(tag, log, e);
4784 } else {
4785 Log.d(tag, log);
4786 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004787 }
Winson Chungede41292013-09-19 16:27:36 -07004788 if (DEBUG_DUMP_LOG) {
4789 sDateStamp.setTime(System.currentTimeMillis());
4790 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004791 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4792 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004793 }
Winson Chungede41292013-09-19 16:27:36 -07004794 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004795 }
4796
Winson Chungede41292013-09-19 16:27:36 -07004797 public void dumpLogsToLocalData() {
4798 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004799 new AsyncTask<Void, Void, Void>() {
4800 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004801 boolean success = false;
4802 sDateStamp.setTime(sRunStart);
4803 String FILENAME = sDateStamp.getMonth() + "-"
4804 + sDateStamp.getDay() + "_"
4805 + sDateStamp.getHours() + "-"
4806 + sDateStamp.getMinutes() + "_"
4807 + sDateStamp.getSeconds() + ".txt";
4808
4809 FileOutputStream fos = null;
4810 File outFile = null;
4811 try {
4812 outFile = new File(getFilesDir(), FILENAME);
4813 outFile.createNewFile();
4814 fos = new FileOutputStream(outFile);
4815 } catch (Exception e) {
4816 e.printStackTrace();
4817 }
4818 if (fos != null) {
4819 PrintWriter writer = new PrintWriter(fos);
4820
4821 writer.println(" ");
4822 writer.println("Debug logs: ");
4823 synchronized (sDumpLogs) {
4824 for (int i = 0; i < sDumpLogs.size(); i++) {
4825 writer.println(" " + sDumpLogs.get(i));
4826 }
4827 }
4828 writer.close();
4829 }
4830 try {
4831 if (fos != null) {
4832 fos.close();
4833 success = true;
4834 }
4835 } catch (IOException e) {
4836 e.printStackTrace();
4837 }
Michael Jurka43467462013-11-14 12:03:58 +01004838 return null;
Winson Chungede41292013-09-19 16:27:36 -07004839 }
Michael Jurka43467462013-11-14 12:03:58 +01004840 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004841 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004843}
Michael Jurka2763be32011-02-24 11:19:57 -08004844
Michael Jurkaabded662011-03-04 12:06:57 -08004845interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004846 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004847 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004848 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004849 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004850 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004851}
Dan Sandlerd5024042014-01-09 15:01:33 -05004852
4853interface DebugIntents {
4854 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4855 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004856}