blob: 0ab665d748165b63e5b2b61e34f570c47efca51a [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 Cohenf358a4b2013-07-23 16:47:31 -070085import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080086import android.view.View.OnLongClickListener;
Sunny Goyale755d462014-07-22 13:48:29 -070087import android.view.ViewAnimationUtils;
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;
Sunny Goyal651077b2014-06-30 14:15:31 -070091import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070094import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080095import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Advanceable;
98import android.widget.FrameLayout;
99import android.widget.ImageView;
100import android.widget.TextView;
101import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700102
Sunny Goyal651077b2014-06-30 14:15:31 -0700103import com.android.launcher3.DropTarget.DragObject;
104import com.android.launcher3.PagedView.PageSwitchListener;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700107import com.android.launcher3.compat.PackageInstallerCompat;
108import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100109import com.android.launcher3.compat.UserHandleCompat;
110import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
Winson Chunga6945242014-01-08 14:04:34 -0800131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800138 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400142 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Romain Guy98d01652009-06-30 16:21:04 -0700168 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
171 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800173 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung2672ff92012-05-04 16:22:30 -0700175 // The Intent extra that defines whether to ignore the launch animation
176 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400177 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
180 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700181 // Type: int
182 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
185 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: boolean
192 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
193 // Type: long
194 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Adam Cohen3ed316a2014-07-23 18:21:20 -0700209 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
210 static final String ACTION_FIRST_LOAD_COMPLETE =
211 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100213 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700214 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100215 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700216 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100217 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700218
Adam Cohen39a06042013-07-19 14:30:12 -0700219 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700220 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700221
Winson Chunga6945242014-01-08 14:04:34 -0800222 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
223
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700224 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700225 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700226 private State mState = State.WORKSPACE;
227 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700228
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700229 private boolean mIsSafeModeEnabled;
230
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700232 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
233 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700234 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800237 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000239 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
240 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
241
Winson Chunga2413752012-04-03 14:22:34 -0700242 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700243 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
244 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700245 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragLayer mDragLayer;
257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800259 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private AppWidgetManager mAppWidgetManager;
262 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Michael Jurkac9d95c52011-08-29 14:03:34 -0700264 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800266 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700267
Michael Jurka0280c3b2010-09-17 15:00:07 -0700268 private int[] mTmpAddItemCellCoordinates = new int[2];
269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private FolderInfo mFolderInfo;
271
Winson Chung3d503fb2011-07-13 17:25:49 -0700272 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800273 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700274
Michael Jurka838a4ca2011-02-07 13:33:06 -0800275 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700276
Winson Chungc51db6a2011-10-05 11:44:49 -0700277 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700278 private AppsCustomizeTabHost mAppsCustomizeTabHost;
279 private AppsCustomizePagedView mAppsCustomizeContent;
280 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800281 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700284 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
285 // scroll issues (because the workspace may not have been measured yet) and extra work.
286 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
287 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288
289 private SpannableStringBuilder mDefaultKeySsb = null;
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 private boolean mWorkspaceLoading = true;
292
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800293 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 private boolean mRestoring;
295 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700296 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297
Michael Jurka1e2f4652013-07-08 18:03:46 -0700298 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700299 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 private Bundle mSavedInstanceState;
302
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800304 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800305 private boolean mUserPresent = true;
306 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700307 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700310 private static LocaleConfiguration sLocaleConfiguration = null;
311
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700312 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
Adam Cohen61f560d2013-09-30 15:58:20 -0700314 private View.OnTouchListener mHapticFeedbackTouchListener;
315
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 // Related to the auto-advancing of widgets
317 private final int ADVANCE_MSG = 1;
318 private final int mAdvanceInterval = 20000;
319 private final int mAdvanceStagger = 250;
320 private long mAutoAdvanceSentTime;
321 private long mAutoAdvanceTimeLeft = -1;
322 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
323 new HashMap<View, AppWidgetProviderInfo>();
324
Winson Chung400438b2011-01-16 17:53:48 -0800325 // Determines how long to wait after a rotation before restoring the screen orientation to
326 // match the sensor state.
327 private final int mRestoreScreenOrientationDelay = 500;
328
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700330 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
331 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
332 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800333
Winson Chungd64a6662013-09-30 11:06:59 -0700334 private Intent mAppMarketIntent = null;
335 private static final boolean DISABLE_MARKET_BUTTON = true;
336
Craig Mautner360310b2012-10-26 15:13:08 -0700337 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700338
Adam Cohen1462de32012-07-24 22:34:36 -0700339 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700340 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700341
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700342 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700343 static Date sDateStamp = new Date();
344 static DateFormat sDateFormat =
345 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
346 static long sRunStart = System.currentTimeMillis();
347 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700348
Winson Chung46353de2012-02-16 14:05:10 -0800349 // We only want to get the SharedPreferences once since it does an FS stat each time we get
350 // it from the context.
351 private SharedPreferences mSharedPrefs;
352
Adam Cohene25af792013-06-06 23:08:25 -0700353 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
354
Winson Chungf0c6ae02012-03-21 16:10:31 -0700355 // Holds the page that we need to animate to, and the icon views that we need to animate up
356 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700357 private ImageView mFolderIconImageView;
358 private Bitmap mFolderIconBitmap;
359 private Canvas mFolderIconCanvas;
360 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700361
Michael Jurkaddd62e92011-02-16 17:49:14 -0800362 private BubbleTextView mWaitingForResume;
363
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 private Runnable mBuildLayersRunnable = new Runnable() {
365 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700366 if (mWorkspace != null) {
367 mWorkspace.buildPageHardwareLayers();
368 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700369 }
370 };
371
Adam Cohendb364c32014-05-20 14:23:40 -0700372 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373
Michael Jurka7267fa52013-09-26 15:29:57 -0700374 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800375
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 private static class PendingAddArguments {
377 int requestCode;
378 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700379 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700380 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381 int cellX;
382 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700383 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 }
385
Daniel Sandlerff02d492013-08-05 02:12:05 -0400386 private Stats mStats;
387
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800388 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800389 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700390 }
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 @Override
393 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700394 if (DEBUG_STRICT_MODE) {
395 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
396 .detectDiskReads()
397 .detectDiskWrites()
398 .detectNetwork() // or .detectAll() for all detectable problems
399 .penaltyLog()
400 .build());
401 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
402 .detectLeakedSqlLiteObjects()
403 .detectLeakedClosableObjects()
404 .penaltyLog()
405 .penaltyDeath()
406 .build());
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700413 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 // Determine the dynamic grid properties
415 Point smallestSize = new Point();
416 Point largestSize = new Point();
417 Point realSize = new Point();
418 Display display = getWindowManager().getDefaultDisplay();
419 display.getCurrentSizeRange(smallestSize, largestSize);
420 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700421 DisplayMetrics dm = new DisplayMetrics();
422 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700423
Winson Chung5f8afe62013-08-12 16:19:28 -0700424 // Lazy-initialize the dynamic grid
425 DeviceProfile grid = app.initDynamicGrid(this,
426 Math.min(smallestSize.x, smallestSize.y),
427 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700428 realSize.x, realSize.y,
429 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700430
431 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400432 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700433 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700434 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800435 mModel = app.setLauncher(this);
436 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700437 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400438 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800439 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700444 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
Adam Cohen53805212013-10-01 10:39:23 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100464 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800466 registerContentObservers();
467
Joe Onorato7c312c12009-08-13 21:36:53 -0700468 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 mSavedState = savedInstanceState;
471 restoreState(mSavedState);
472
473 if (PROFILE_STARTUP) {
474 android.os.Debug.stopMethodTracing();
475 }
476
477 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700478 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700479 // If the user leaves launcher, then we should just load items asynchronously when
480 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500481 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 } else {
483 // We only load the page synchronously if the user rotates (or triggers a
484 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800485 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 }
488
489 // For handling default keys
490 mDefaultKeySsb = new SpannableStringBuilder();
491 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800492
493 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
494 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800495
Adam Cohenf9426d52012-06-04 17:26:21 -0700496 updateGlobalIcons();
497
498 // On large interfaces, we want the screen to auto-rotate based on the current orientation
499 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700500
Adam Cohen432609a2014-03-13 17:03:22 -0700501 if (shouldShowIntroScreen()) {
502 showIntroScreen();
503 } else {
504 showFirstRunActivity();
505 }
506
Winson Chunge43a1e72014-01-15 10:33:02 -0800507 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
508 // on the device, then we always show the first run cling experience (or if there is no
509 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800510 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
511 if (mModel.canMigrateFromOldLauncherDb(this)) {
512 mLauncherClings.showMigrationCling();
513 } else {
514 mLauncherClings.showFirstRunCling();
515 }
Winson Chunga6945242014-01-08 14:04:34 -0800516 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800517 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800518 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700519 }
520
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700521 @Override
522 public void onLauncherProviderChange() { }
523
Winson Chung98ca0f72013-07-29 12:58:51 -0700524 /** To be overriden by subclasses to hint to Launcher that we have custom content */
525 protected boolean hasCustomContentToLeft() {
526 return false;
527 }
528
Dave Hawkeya8881582013-09-17 15:55:33 -0600529 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500530 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 * {@link #addToCustomContentPage}. This will only be invoked if
532 * {@link #hasCustomContentToLeft()} is {@code true}.
533 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500534 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 }
536
537 /**
538 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
539 * ensure the custom content page is added or removed if necessary.
540 */
541 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600542 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 // Not bound yet, wait for bindScreens to be called.
544 return;
545 }
546
547 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
548 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500549 mWorkspace.createCustomContentContainer();
550 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
552 mWorkspace.removeCustomContentPage();
553 }
554 }
555
Adam Cohenf9426d52012-06-04 17:26:21 -0700556 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700557 boolean searchVisible = false;
558 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800559 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700560 int coi = getCurrentOrientationIndexForGlobalIcons();
561 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
562 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700563 if (!DISABLE_MARKET_BUTTON) {
564 updateAppMarketIcon();
565 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700566 searchVisible = updateGlobalSearchIcon();
567 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700568 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700569 if (sGlobalSearchIcon[coi] != null) {
570 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700571 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700572 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700573 if (sVoiceSearchIcon[coi] != null) {
574 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700575 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700576 }
Winson Chungd64a6662013-09-30 11:06:59 -0700577 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700578 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800579 }
Winson Chungadf0c182012-08-23 14:59:07 -0700580 if (mSearchDropTargetBar != null) {
581 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
Romain Guycbb89e42009-06-08 15:52:54 -0700584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 if (sLocaleConfiguration == null) {
587 new AsyncTask<Void, Void, LocaleConfiguration>() {
588 @Override
589 protected LocaleConfiguration doInBackground(Void... unused) {
590 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
591 readConfiguration(Launcher.this, localeConfiguration);
592 return localeConfiguration;
593 }
594
595 @Override
596 protected void onPostExecute(LocaleConfiguration result) {
597 sLocaleConfiguration = result;
598 checkForLocaleChange(); // recursive, but now with a locale configuration
599 }
600 }.execute();
601 return;
602 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700603
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final Configuration configuration = getResources().getConfiguration();
605
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final String locale = configuration.locale.toString();
608
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final int mcc = configuration.mcc;
611
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final int mnc = configuration.mnc;
614
Romain Guy84f296c2009-11-04 15:00:44 -0800615 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616
Romain Guy84f296c2009-11-04 15:00:44 -0800617 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 sLocaleConfiguration.locale = locale;
619 sLocaleConfiguration.mcc = mcc;
620 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700621
Joe Onorato0589f0f2010-02-08 13:44:00 -0800622 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623
624 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100625 new AsyncTask<Void, Void, Void>() {
626 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100628 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700629 }
Michael Jurka43467462013-11-14 12:03:58 +0100630 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700631 }
632 }
633
634 private static class LocaleConfiguration {
635 public String locale;
636 public int mcc = -1;
637 public int mnc = -1;
638 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700639
Romain Guy98d01652009-06-30 16:21:04 -0700640 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
641 DataInputStream in = null;
642 try {
643 in = new DataInputStream(context.openFileInput(PREFERENCES));
644 configuration.locale = in.readUTF();
645 configuration.mcc = in.readInt();
646 configuration.mnc = in.readInt();
647 } catch (FileNotFoundException e) {
648 // Ignore
649 } catch (IOException e) {
650 // Ignore
651 } finally {
652 if (in != null) {
653 try {
654 in.close();
655 } catch (IOException e) {
656 // Ignore
657 }
658 }
659 }
660 }
661
662 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
663 DataOutputStream out = null;
664 try {
665 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
666 out.writeUTF(configuration.locale);
667 out.writeInt(configuration.mcc);
668 out.writeInt(configuration.mnc);
669 out.flush();
670 } catch (FileNotFoundException e) {
671 // Ignore
672 } catch (IOException e) {
673 //noinspection ResultOfMethodCallIgnored
674 context.getFileStreamPath(PREFERENCES).delete();
675 } finally {
676 if (out != null) {
677 try {
678 out.close();
679 } catch (IOException e) {
680 // Ignore
681 }
682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684 }
685
Anton Hanssona2f665f2013-09-26 12:18:32 +0100686 public Stats getStats() {
687 return mStats;
688 }
689
Bjorn Bringertc459e522013-06-07 19:36:01 +0100690 public LayoutInflater getInflater() {
691 return mInflater;
692 }
693
Winson Chung36a62fe2012-05-06 18:04:42 -0700694 boolean isDraggingEnabled() {
695 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
696 // that is subsequently removed from the workspace in startBinding().
697 return !mModel.isLoadingWorkspace();
698 }
699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 static int getScreen() {
701 synchronized (sLock) {
702 return sScreen;
703 }
704 }
705
706 static void setScreen(int screen) {
707 synchronized (sLock) {
708 sScreen = screen;
709 }
710 }
711
Winson Chung557d6ed2011-07-08 15:34:52 -0700712 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000713 * Copied from View -- the View version of the method isn't called
714 * anywhere else in our process and only exists for API level 17+,
715 * so it's ok to keep our own version with no API requirement.
716 */
717 public static int generateViewId() {
718 for (;;) {
719 final int result = sNextGeneratedId.get();
720 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
721 int newValue = result + 1;
722 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
723 if (sNextGeneratedId.compareAndSet(result, newValue)) {
724 return result;
725 }
726 }
727 }
728
729 public int getViewIdForItem(ItemInfo info) {
730 // This cast is safe given the > 2B range for int.
731 int itemId = (int) info.id;
732 if (mItemIdToViewId.containsKey(itemId)) {
733 return mItemIdToViewId.get(itemId);
734 }
735 int viewId = generateViewId();
736 mItemIdToViewId.put(itemId, viewId);
737 return viewId;
738 }
739
740 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700741 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
742 * a configuration step, this allows the proper animations to run after other transitions.
743 */
Adam Cohendb364c32014-05-20 14:23:40 -0700744 private long completeAdd(PendingAddArguments args) {
745
746 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700749 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700750 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700753 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700754 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 }
Michael Jurka27614d22012-04-02 06:40:22 -0700756 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
757 // if you turned the screen off and then back while in All Apps, Launcher would not
758 // return to the workspace. Clearing mAddInfo.container here fixes this issue
759 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700760 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 protected void onActivityResult(
765 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700766 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700767 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700768 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800769 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700770
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 Runnable exitSpringLoaded = new Runnable() {
772 @Override
773 public void run() {
774 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
775 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
776 }
777 };
778
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
782 if (resultCode == RESULT_CANCELED) {
783 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700784 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
788 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 }
790 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200791 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
792 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
793 mWorkspace.exitOverviewMode(false);
794 }
795 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200797
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
799 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700800
Adam Cohendb364c32014-05-20 14:23:40 -0700801 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800802 // We have special handling for widgets
803 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800804 final int appWidgetId;
805 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
806 : -1;
807 if (widgetId < 0) {
808 appWidgetId = pendingAddWidgetId;
809 } else {
810 appWidgetId = widgetId;
811 }
812
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800814 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700815 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
816 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700817 result = RESULT_CANCELED;
818 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 @Override
821 public void run() {
822 exitSpringLoadedDragModeDelayed(false, 0, null);
823 }
824 };
Adam Cohendb364c32014-05-20 14:23:40 -0700825 if (workspaceLocked) {
826 // No need to remove the empty screen if we're mid-binding, as the
827 // the bind will not add the empty screen.
828 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
829 } else {
830 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
831 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
832 }
Winson Chung5aaab772012-04-27 15:24:02 -0700833 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700834 if (!workspaceLocked) {
835 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
836 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
837
838 dropLayout.setDropPending(true);
839 final Runnable onComplete = new Runnable() {
840 @Override
841 public void run() {
842 completeTwoStageWidgetDrop(resultCode, appWidgetId);
843 dropLayout.setDropPending(false);
844 }
845 };
846 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 } else {
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
850 mPendingAddInfo);
851 sPendingAddItem = args;
852 }
Winson Chung5aaab772012-04-27 15:24:02 -0700853 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 return;
855 }
856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 // The pattern used here is that a user PICKs a specific application,
858 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
861 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700862 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700863 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
864 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800865 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700866 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800867 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohen00074722013-10-11 17:46:09 -0700872 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 }
878
Adam Cohendb364c32014-05-20 14:23:40 -0700879 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
880 appWidgetId, ItemInfo info) {
881 PendingAddArguments args = new PendingAddArguments();
882 args.requestCode = requestCode;
883 args.intent = data;
884 args.container = info.container;
885 args.screenId = info.screenId;
886 args.cellX = info.cellX;
887 args.cellY = info.cellY;
888 args.appWidgetId = appWidgetId;
889 return args;
890 }
891
892 /**
893 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
894 *
895 * @param screenId the screen id to check
896 * @return the new screen, or screenId if it exists
897 */
898 private long ensurePendingDropLayoutExists(long screenId) {
899 CellLayout dropLayout =
900 (CellLayout) mWorkspace.getScreenWithId(screenId);
901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700912 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700913 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 Runnable onCompleteRunnable = null;
915 int animationType = 0;
916
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 if (resultCode == RESULT_OK) {
919 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
920 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 mPendingAddWidgetInfo);
922 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 onCompleteRunnable = new Runnable() {
924 @Override
925 public void run() {
926 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700927 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700928 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
929 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
931 };
932 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800933 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 if (mDragLayer.getAnimatedView() != null) {
937 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
938 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
939 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 // The animated view may be null in the case of a rotation during widget configuration
942 onCompleteRunnable.run();
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700947 protected void onStop() {
948 super.onStop();
949 FirstFrameAnimatorHelper.setIsVisible(false);
950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
956 }
957
958 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200960 long startTime = 0;
961 if (DEBUG_RESUME_TIME) {
962 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400963 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700966
Winson Chung4a2afa32012-07-19 14:53:05 -0700967 // Restore the previous launcher state
968 if (mOnResumeState == State.WORKSPACE) {
969 showWorkspace(false);
970 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800971 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700972 }
973 mOnResumeState = State.NONE;
974
Craig Mautner360310b2012-10-26 15:13:08 -0700975 // Background was set to gradient in onPause(), restore to black if in all apps.
976 setWorkspaceBackground(mState == State.WORKSPACE);
977
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800978 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700979 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700980 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500981 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700983 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700985 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200986 // We might have postponed some bind calls until onResume (see waitUntilResume) --
987 // execute them here
988 long startTimeCallbacks = 0;
989 if (DEBUG_RESUME_TIME) {
990 startTimeCallbacks = System.currentTimeMillis();
991 }
992
993 if (mAppsCustomizeContent != null) {
994 mAppsCustomizeContent.setBulkBind(true);
995 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700996 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
997 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200998 }
999 if (mAppsCustomizeContent != null) {
1000 mAppsCustomizeContent.setBulkBind(false);
1001 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001003 if (DEBUG_RESUME_TIME) {
1004 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1005 (System.currentTimeMillis() - startTimeCallbacks));
1006 }
Michael Jurka447bf842013-05-15 14:52:15 +02001007 }
Michael Jurka54554252013-08-01 12:52:23 +02001008 if (mOnResumeCallbacks.size() > 0) {
1009 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1010 mOnResumeCallbacks.get(i).run();
1011 }
1012 mOnResumeCallbacks.clear();
1013 }
Winson Chunge4e50662012-01-23 14:45:13 -08001014
1015 // Reset the pressed state of icons that were locked in the press state while activities
1016 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001017 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001018 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001019 mWaitingForResume.setStayPressed(false);
1020 }
Winson Chunge4e50662012-01-23 14:45:13 -08001021 if (mAppsCustomizeContent != null) {
1022 // Resets the previous all apps icon press state
1023 mAppsCustomizeContent.resetDrawableState();
1024 }
Winson Chung82963d52013-10-09 11:20:57 -07001025
Adam Cohen06dff352012-06-01 17:17:08 -07001026 // It is possible that widgets can receive updates while launcher is not in the foreground.
1027 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1028 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1029 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001030 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001031
Winson Chung780fe592013-09-26 14:48:44 -07001032 // Process any items that were added while Launcher was away.
1033 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1034
Winson Chung5841efa2013-09-30 18:06:44 -07001035 // Update the voice search button proxy
1036 updateVoiceButtonProxyVisible(false);
1037
Adam Cohenf9426d52012-06-04 17:26:21 -07001038 // Again, as with the above scenario, it's possible that one or more of the global icons
1039 // were updated in the wrong orientation.
1040 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001041 if (DEBUG_RESUME_TIME) {
1042 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1043 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001044
1045 if (mWorkspace.getCustomContentCallbacks() != null) {
1046 // If we are resuming and the custom content is the current page, we call onShow().
1047 // It is also poassible that onShow will instead be called slightly after first layout
1048 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1049 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001050 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001051 }
1052 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001053 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001054 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001055
1056 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001057 }
1058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001060 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001061 // Ensure that items added to Launcher are queued until Launcher returns
1062 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001063 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001064
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001066 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001067 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001068 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001069
1070 // We call onHide() aggressively. The custom content callbacks should be able to
1071 // debounce excess onHide calls.
1072 if (mWorkspace.getCustomContentCallbacks() != null) {
1073 mWorkspace.getCustomContentCallbacks().onHide();
1074 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 }
Romain Guycbb89e42009-06-08 15:52:54 -07001076
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 QSBScroller mQsbScroller = new QSBScroller() {
1078 int scrollY = 0;
1079
1080 @Override
1081 public void setScrollY(int scroll) {
1082 scrollY = scroll;
1083
1084 if (mWorkspace.isOnOrMovingToCustomContent()) {
1085 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001086 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001087 }
1088 }
1089 };
1090
1091 public void resetQSBScroll() {
1092 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001093 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
1095
1096 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1098 // by a onResume or by scrolling otherwise.
1099 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001100
1101 // Custom content is completely hidden
1102 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001103
1104 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1105 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001106
1107 // Indicates whether the user is allowed to scroll away from the custom content.
1108 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
Jorim Jaggid017f882014-01-14 17:08:48 -08001111 protected boolean hasSettings() {
1112 return false;
1113 }
1114
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 public interface QSBScroller {
1116 public void setScrollY(int scrollY);
1117 }
1118
Winson Chung98ca0f72013-07-29 12:58:51 -07001119 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001120 CustomContentCallbacks callbacks, String description) {
1121 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001123 }
1124
Adam Cohenedb40762013-07-18 16:45:45 -07001125 // The custom content needs to offset its content to account for the QSB
1126 public int getTopOffsetForCustomContent() {
1127 return mWorkspace.getPaddingTop();
1128 }
1129
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001130 @Override
1131 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001132 // Flag the loader to stop early before switching
1133 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001134 if (mAppsCustomizeContent != null) {
1135 mAppsCustomizeContent.surrender();
1136 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001137 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001138 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001139
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001140 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001141 @Override
1142 public void onWindowFocusChanged(boolean hasFocus) {
1143 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001144 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001145 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 private boolean acceptFilter() {
1148 final InputMethodManager inputManager = (InputMethodManager)
1149 getSystemService(Context.INPUT_METHOD_SERVICE);
1150 return !inputManager.isFullscreenMode();
1151 }
1152
1153 @Override
1154 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001155 final int uniChar = event.getUnicodeChar();
1156 final boolean handled = super.onKeyDown(keyCode, event);
1157 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1158 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1160 keyCode, event);
1161 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001162 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001163 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001164 // showSearchDialog()
1165 // If there are multiple keystrokes before the search dialog takes focus,
1166 // onSearchRequested() will be called for every keystroke,
1167 // but it is idempotent, so it's fine.
1168 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
1171
Joe Onorato8a9625e2010-01-28 15:55:35 -08001172 // Eat the long press event so the keyboard doesn't come up.
1173 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1174 return true;
1175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 return handled;
1178 }
1179
Karl Rosaen138a0412009-04-23 19:00:21 -07001180 private String getTypedText() {
1181 return mDefaultKeySsb.toString();
1182 }
1183
1184 private void clearTypedText() {
1185 mDefaultKeySsb.clear();
1186 mDefaultKeySsb.clearSpans();
1187 Selection.setSelection(mDefaultKeySsb, 0);
1188 }
1189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001191 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1192 * State
1193 */
1194 private static State intToState(int stateOrdinal) {
1195 State state = State.WORKSPACE;
1196 final State[] stateValues = State.values();
1197 for (int i = 0; i < stateValues.length; i++) {
1198 if (stateValues[i].ordinal() == stateOrdinal) {
1199 state = stateValues[i];
1200 break;
1201 }
1202 }
1203 return state;
1204 }
1205
1206 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 * Restores the previous state, if it exists.
1208 *
1209 * @param savedState The previous state.
1210 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001211 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 private void restoreState(Bundle savedState) {
1213 if (savedState == null) {
1214 return;
1215 }
1216
Michael Jurka883f55b2010-10-21 15:47:14 -07001217 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001218 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001219 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001220 }
1221
Adam Cohen21cd0022013-10-09 18:57:02 -07001222 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1223 PagedView.INVALID_RESTORE_PAGE);
1224 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001225 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
Winson Chung3d503fb2011-07-13 17:25:49 -07001228 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001229 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1232 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001233 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001234 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1235 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001236 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1237 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1238 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001239 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001240 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 mRestoring = true;
1242 }
1243
1244 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1245 if (renameFolder) {
1246 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001247 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 mRestoring = true;
1249 }
Winson Chunga12a2502010-12-20 14:41:35 -08001250
Winson Chung785d2eb2011-04-14 16:08:02 -07001251 // Restore the AppsCustomize tab
1252 if (mAppsCustomizeTabHost != null) {
1253 String curTab = savedState.getString("apps_customize_currentTab");
1254 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001255 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001256 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001257 mAppsCustomizeContent.loadAssociatedPages(
1258 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001259 }
1260
Winson Chung5afbf7b2011-07-25 11:53:08 -07001261 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1262 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001264 mItemIdToViewId = (HashMap<Integer, Integer>)
1265 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 }
1267
1268 /**
1269 * Finds all the views we need and configure them properly.
1270 */
1271 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001272 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001273
Craig Mautner360310b2012-10-26 15:13:08 -07001274 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001275 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1276 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001277 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001278 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001279
John Spurlock77e1f472013-09-11 10:09:51 -04001280 mLauncherView.setSystemUiVisibility(
1281 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001282 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
Winson Chung4c98d922011-05-31 16:50:48 -07001284 // Setup the drag layer
1285 mDragLayer.setup(this, dragController);
1286
Winson Chung3d503fb2011-07-13 17:25:49 -07001287 // Setup the hotseat
1288 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1289 if (mHotseat != null) {
1290 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001291 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001292 }
1293
Jorim Jaggid017f882014-01-14 17:08:48 -08001294 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001295 View widgetButton = findViewById(R.id.widget_button);
1296 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001297 @Override
1298 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001299 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001300 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001301 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001302 }
1303 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001304 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1305
1306 View wallpaperButton = findViewById(R.id.wallpaper_button);
1307 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001308 @Override
1309 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001310 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001311 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001312 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001313 }
1314 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001315 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1316
1317 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001318 if (hasSettings()) {
1319 settingsButton.setOnClickListener(new OnClickListener() {
1320 @Override
1321 public void onClick(View arg0) {
1322 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001323 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001324 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001325 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001326 });
1327 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1328 } else {
1329 settingsButton.setVisibility(View.GONE);
1330 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1331 lp.gravity = Gravity.END | Gravity.TOP;
1332 widgetButton.requestLayout();
1333 }
1334
Adam Cohendbdff6b2013-09-18 19:09:15 -07001335 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001336
Winson Chung4c98d922011-05-31 16:50:48 -07001337 // Setup the workspace
1338 mWorkspace.setHapticFeedbackEnabled(false);
1339 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001340 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001341 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001342
Winson Chungf0ea4d32011-06-06 14:27:16 -07001343 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001344 mSearchDropTargetBar = (SearchDropTargetBar)
1345 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001346
Winson Chungf0ea4d32011-06-06 14:27:16 -07001347 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001349 mAppsCustomizeContent = (AppsCustomizePagedView)
1350 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1351 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001352
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001354 dragController.setDragScoller(mWorkspace);
1355 dragController.setScrollView(mDragLayer);
1356 dragController.setMoveTarget(mWorkspace);
1357 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001358 if (mSearchDropTargetBar != null) {
1359 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001360 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001361
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001362 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001363 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001364 mWeightWatcher = new WeightWatcher(this);
1365 mWeightWatcher.setAlpha(0.5f);
1366 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001367 new FrameLayout.LayoutParams(
1368 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001369 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001370 Gravity.BOTTOM)
1371 );
Adam Cohen39a06042013-07-19 14:30:12 -07001372
1373 boolean show = shouldShowWeightWatcher();
1374 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
1378 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001379 * Sets the all apps button. This method is called from {@link Hotseat}.
1380 */
1381 public void setAllAppsButton(View allAppsButton) {
1382 mAllAppsButton = allAppsButton;
1383 }
1384
1385 public View getAllAppsButton() {
1386 return mAllAppsButton;
1387 }
1388
1389 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 * Creates a view representing a shortcut.
1391 *
1392 * @param info The data structure describing the shortcut.
1393 *
1394 * @return A View inflated from R.layout.application.
1395 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001396 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001398 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400
1401 /**
1402 * Creates a view representing a shortcut inflated from the specified resource.
1403 *
1404 * @param layoutResId The id of the XML layout used to create the shortcut.
1405 * @param parent The group the shortcut belongs to.
1406 * @param info The data structure describing the shortcut.
1407 *
1408 * @return A View inflated from layoutResId.
1409 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001410 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001411 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001412 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 return favorite;
1415 }
1416
1417 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 * Add a shortcut to the workspace.
1419 *
1420 * @param data The intent describing the shortcut.
1421 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001423 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001424 int cellY) {
1425 int[] cellXY = mTmpAddItemCellCoordinates;
1426 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001427 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Michael Jurkad3ef3062010-11-23 16:23:58 -08001429 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001430
Adam Cohen558baaf2011-08-15 15:22:57 -07001431 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001432 if (info == null) {
1433 return;
1434 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001435 final View view = createShortcut(info);
1436
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437 // First we check if we already know the exact location where we want to add this item.
1438 if (cellX >= 0 && cellY >= 0) {
1439 cellXY[0] = cellX;
1440 cellXY[1] = cellY;
1441 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001442
1443 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001444 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001445 true, null,null)) {
1446 return;
1447 }
1448 DragObject dragObject = new DragObject();
1449 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001450 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1451 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001452 return;
1453 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001454 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001455 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001456 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001457 foundCellSpan = (result != null);
1458 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001459 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001460 }
1461
1462 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001463 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001464 return;
1465 }
1466
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468
1469 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001470 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001471 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473 }
1474
Adam Cohen2f093b62012-04-30 18:59:53 -07001475 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1476 int minHeight) {
1477 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001478 // We want to account for the extra amount of padding that we are adding to the widget
1479 // to ensure that it gets the full amount of space that it has requested
1480 int requiredWidth = minWidth + padding.left + padding.right;
1481 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001482 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001483 }
1484
Adam Cohen2f093b62012-04-30 18:59:53 -07001485 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1486 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001487 }
1488
Adam Cohen2f093b62012-04-30 18:59:53 -07001489 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1490 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001491 }
1492
Adam Cohen2f093b62012-04-30 18:59:53 -07001493 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1494 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001495 }
1496
Adam Cohen2f093b62012-04-30 18:59:53 -07001497 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1498 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001499 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001500 }
1501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001505 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001506 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001509 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1510 if (appWidgetInfo == null) {
1511 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1512 }
Romain Guycbb89e42009-06-08 15:52:54 -07001513
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001514 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001516
Adam Cohen2f093b62012-04-30 18:59:53 -07001517 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1518 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001521 // We have saved the position to which the widget was dragged-- this really only matters
1522 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001523 int[] cellXY = mTmpAddItemCellCoordinates;
1524 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001526 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001527 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1528 cellXY[0] = mPendingAddInfo.cellX;
1529 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001530 spanXY[0] = mPendingAddInfo.spanX;
1531 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001532 foundCellSpan = true;
1533 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001536 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1537 spanXY[1], cellXY, finalSpan);
1538 spanXY[0] = finalSpan[0];
1539 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001540 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001542 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001543 }
1544
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001546 if (appWidgetId != -1) {
1547 // Deleting an app widget ID is a void call but writes to disk before returning
1548 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001549 new AsyncTask<Void, Void, Void>() {
1550 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001551 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001552 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001553 }
Michael Jurka43467462013-11-14 12:03:58 +01001554 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001555 }
Winson Chung93eef082012-03-23 15:59:27 -07001556 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001557 return;
1558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001560 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001561 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1562 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 launcherInfo.spanX = spanXY[0];
1564 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001565 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1566 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570
1571 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 if (hostView == null) {
1573 // Perform actual inflation because we're live
1574 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1575 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1576 } else {
1577 // The AppWidgetHostView has already been inflated and instantiated
1578 launcherInfo.hostView = hostView;
1579 }
Romain Guycbb89e42009-06-08 15:52:54 -07001580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001582 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001583 launcherInfo.notifyWidgetSizeChanged(this);
1584
Adam Cohendcd297f2013-06-18 13:13:40 -07001585 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001586 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001587
1588 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001590 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1594 @Override
1595 public void onReceive(Context context, Intent intent) {
1596 final String action = intent.getAction();
1597 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1598 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001599 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001600 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001601
Winson Chungc100e8e2011-08-09 16:02:43 -07001602 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001603 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001604 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001605 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001606 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001607 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1608 mUserPresent = true;
1609 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001610 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1611 mModel.resetLoadedState(false, true);
1612 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1613 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1614 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1615 mModel.resetLoadedState(false, true);
1616 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1617 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1618 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001619 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1620 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1621 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 }
1623 }
1624 };
1625
1626 @Override
1627 public void onAttachedToWindow() {
1628 super.onAttachedToWindow();
1629
1630 // Listen for broadcasts related to user-presence
1631 final IntentFilter filter = new IntentFilter();
1632 filter.addAction(Intent.ACTION_SCREEN_OFF);
1633 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001634 // For handling managed profiles
1635 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1636 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001637 if (ENABLE_DEBUG_INTENTS) {
1638 filter.addAction(DebugIntents.DELETE_DATABASE);
1639 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1640 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001642 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001643 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001644 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 mVisible = true;
1646 }
1647
Adam Cohen0b395352014-06-09 22:54:36 +00001648 /**
1649 * Sets up transparent navigation and status bars in LMP.
1650 * This method is a no-op for other platform versions.
1651 */
1652 @TargetApi(19)
1653 private void setupTransparentSystemBarsForLmp() {
1654 // TODO(sansid): use the APIs directly when compiling against L sdk.
1655 // Currently we use reflection to access the flags and the API to set the transparency
1656 // on the System bars.
1657 if (Utilities.isLmp()) {
1658 try {
1659 getWindow().getAttributes().systemUiVisibility |=
1660 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1661 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1662 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1663 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1664 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1665 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1666 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1667 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1668
1669 Method setStatusBarColorMethod =
1670 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1671 Method setNavigationBarColorMethod =
1672 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1673 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1674 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1675 } catch (NoSuchFieldException e) {
1676 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1677 } catch (NoSuchMethodException ex) {
1678 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1679 } catch (IllegalAccessException e) {
1680 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1681 } catch (IllegalArgumentException e) {
1682 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1683 } catch (InvocationTargetException e) {
1684 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1685 } finally {}
1686 }
1687 }
1688
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 @Override
1690 public void onDetachedFromWindow() {
1691 super.onDetachedFromWindow();
1692 mVisible = false;
1693
Adam Cohend113e0c2010-11-11 10:48:05 -08001694 if (mAttached) {
1695 unregisterReceiver(mReceiver);
1696 mAttached = false;
1697 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 updateRunning();
1699 }
1700
1701 public void onWindowVisibilityChanged(int visibility) {
1702 mVisible = visibility == View.VISIBLE;
1703 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 // The following code used to be in onResume, but it turns out onResume is called when
1705 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1706 // is a more appropriate event to handle
1707 if (mVisible) {
1708 mAppsCustomizeTabHost.onWindowVisible();
1709 if (!mWorkspaceLoading) {
1710 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001711 // We want to let Launcher draw itself at least once before we force it to build
1712 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001713 // apps is nice and speedy.
1714 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001715 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001717 if (mStarted) return;
1718 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001719 // We delay the layer building a bit in order to give
1720 // other message processing a time to run. In particular
1721 // this avoids a delay in hiding the IME if it was
1722 // currently shown, because doing that may involve
1723 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001724 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001725 final ViewTreeObserver.OnDrawListener listener = this;
1726 mWorkspace.post(new Runnable() {
1727 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001728 if (mWorkspace != null &&
1729 mWorkspace.getViewTreeObserver() != null) {
1730 mWorkspace.getViewTreeObserver().
1731 removeOnDrawListener(listener);
1732 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001733 }
1734 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001735 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001736 }
1737 });
1738 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001739 // When Launcher comes back to foreground, a different Activity might be responsible for
1740 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001741 if (!DISABLE_MARKET_BUTTON) {
1742 updateAppMarketIcon();
1743 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 clearTypedText();
1745 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 }
1747
1748 private void sendAdvanceMessage(long delay) {
1749 mHandler.removeMessages(ADVANCE_MSG);
1750 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1751 mHandler.sendMessageDelayed(msg, delay);
1752 mAutoAdvanceSentTime = System.currentTimeMillis();
1753 }
1754
1755 private void updateRunning() {
1756 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1757 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1758 mAutoAdvanceRunning = autoAdvanceRunning;
1759 if (autoAdvanceRunning) {
1760 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1761 sendAdvanceMessage(delay);
1762 } else {
1763 if (!mWidgetsToAdvance.isEmpty()) {
1764 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1765 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1766 }
1767 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001768 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770 }
1771 }
1772
1773 private final Handler mHandler = new Handler() {
1774 @Override
1775 public void handleMessage(Message msg) {
1776 if (msg.what == ADVANCE_MSG) {
1777 int i = 0;
1778 for (View key: mWidgetsToAdvance.keySet()) {
1779 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1780 final int delay = mAdvanceStagger * i;
1781 if (v instanceof Advanceable) {
1782 postDelayed(new Runnable() {
1783 public void run() {
1784 ((Advanceable) v).advance();
1785 }
1786 }, delay);
1787 }
1788 i++;
1789 }
1790 sendAdvanceMessage(mAdvanceInterval);
1791 }
1792 }
1793 };
1794
1795 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001796 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1798 if (v instanceof Advanceable) {
1799 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001800 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 updateRunning();
1802 }
1803 }
1804
1805 void removeWidgetToAutoAdvance(View hostView) {
1806 if (mWidgetsToAdvance.containsKey(hostView)) {
1807 mWidgetsToAdvance.remove(hostView);
1808 updateRunning();
1809 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 }
1811
Joe Onorato9c1289c2009-08-17 11:03:03 -04001812 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001813 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 launcherInfo.hostView = null;
1815 }
1816
Winson Chung93eef082012-03-23 15:59:27 -07001817 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1818 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1819 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 }
1821
Winson Chunga6945242014-01-08 14:04:34 -08001822 public DragLayer getDragLayer() {
1823 return mDragLayer;
1824 }
1825
1826 public Workspace getWorkspace() {
1827 return mWorkspace;
1828 }
1829
1830 public Hotseat getHotseat() {
1831 return mHotseat;
1832 }
1833
Jorim Jaggid017f882014-01-14 17:08:48 -08001834 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001835 return mOverviewPanel;
1836 }
1837
1838 public SearchDropTargetBar getSearchBar() {
1839 return mSearchDropTargetBar;
1840 }
1841
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001842 public LauncherAppWidgetHost getAppWidgetHost() {
1843 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
Romain Guycbb89e42009-06-08 15:52:54 -07001845
Winson Chunga9abd0e2010-10-27 17:18:37 -07001846 public LauncherModel getModel() {
1847 return mModel;
1848 }
1849
Winson Chunga6945242014-01-08 14:04:34 -08001850 public LauncherClings getLauncherClings() {
1851 return mLauncherClings;
1852 }
1853
1854 protected SharedPreferences getSharedPrefs() {
1855 return mSharedPrefs;
1856 }
1857
Bjorn Bringertc459e522013-06-07 19:36:01 +01001858 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001859 getWindow().closeAllPanels();
1860
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001861 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001862 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 @Override
1866 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001867 long startTime = 0;
1868 if (DEBUG_RESUME_TIME) {
1869 startTime = System.currentTimeMillis();
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 super.onNewIntent(intent);
1872
1873 // Close the menu
1874 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001875 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001876 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001877
Adam Cohened307df2013-10-02 09:37:31 -07001878 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1879 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1880 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001881
Adam Cohen6fecd412013-10-02 17:41:50 -07001882 if (mWorkspace == null) {
1883 // Can be cases where mWorkspace is null, this prevents a NPE
1884 return;
1885 }
1886 Folder openFolder = mWorkspace.getOpenFolder();
1887 // In all these cases, only animate if we're already on home
1888 mWorkspace.exitWidgetResizeMode();
1889 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001890 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001892 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001893
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 closeFolder();
1895 exitSpringLoadedDragMode();
1896
1897 // If we are already on home, then just animate back to the workspace,
1898 // otherwise, just wait until onResume to set the state back to Workspace
1899 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001900 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 } else {
1902 mOnResumeState = State.WORKSPACE;
1903 }
1904
1905 final View v = getWindow().peekDecorView();
1906 if (v != null && v.getWindowToken() != null) {
1907 InputMethodManager imm = (InputMethodManager)getSystemService(
1908 INPUT_METHOD_SERVICE);
1909 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1910 }
1911
1912 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001913 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 mAppsCustomizeTabHost.reset();
1915 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001916
1917 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
Adam Cohened307df2013-10-02 09:37:31 -07001919
Michael Jurka447bf842013-05-15 14:52:15 +02001920 if (DEBUG_RESUME_TIME) {
1921 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
1924
Adam Coppa120b8e2013-11-12 16:26:04 +00001925 /**
1926 * Override point for subclasses to prevent movement to the default screen when the home
1927 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1928 */
1929 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1930 return true;
1931 }
1932
1933 /**
1934 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1935 */
1936 protected void onHomeIntent() {
1937 // Do nothing
1938 }
1939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001941 public void onRestoreInstanceState(Bundle state) {
1942 super.onRestoreInstanceState(state);
1943 for (int page: mSynchronouslyBoundPages) {
1944 mWorkspace.restoreInstanceStateForChild(page);
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
1948 @Override
1949 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001950 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001951 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1952 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001954 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Michael Jurka883f55b2010-10-21 15:47:14 -07001956 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001957 // We close any open folder since it will not be re-opened, and we need to make sure
1958 // this state is reflected.
1959 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mWaitingForResult) {
1963 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1966 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1968 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1969 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001970 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972
1973 if (mFolderInfo != null && mWaitingForResult) {
1974 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1975 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1976 }
Michael Jurka946ad472010-07-09 18:05:18 -07001977
Winson Chung785d2eb2011-04-14 16:08:02 -07001978 // Save the current AppsCustomize tab
1979 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001980 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1981 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001982 if (currentTabTag != null) {
1983 outState.putString("apps_customize_currentTab", currentTabTag);
1984 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001985 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1986 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001987 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001988 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
1991 @Override
1992 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Winson Chunge7a03942011-08-05 15:05:12 -07001995 // Remove all pending runnables
1996 mHandler.removeMessages(ADVANCE_MSG);
1997 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001998 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001999
Winson Chungcd2b0142011-06-08 16:02:26 -07002000 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002001 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07002002 mModel.stopLoader();
2003 app.setLauncher(null);
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002006 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002008 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002010 mAppWidgetHost = null;
2011
2012 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
2014 TextKeyListener.getInstance().release();
2015
Winson Chung81b52252012-08-27 15:34:29 -07002016 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2017 // to prevent leaking Launcher activities on orientation change.
2018 if (mModel != null) {
2019 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2020 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002021
2022 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002023 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002024
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002025 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002026 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002027 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002028 mWorkspace = null;
2029 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002030
Sunny Goyale755d462014-07-22 13:48:29 -07002031 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002032 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
Adam Cohena9cf38f2011-05-02 15:36:58 -07002035 public DragController getDragController() {
2036 return mDragController;
2037 }
2038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
2040 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002041 if (requestCode >= 0) {
2042 setWaitingForResult(true);
2043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 super.startActivityForResult(intent, requestCode);
2045 }
2046
Winson Chung70d72102011-08-12 11:24:35 -07002047 /**
2048 * Indicates that we want global search for this activity by setting the globalSearch
2049 * argument for {@link #startSearch} to true.
2050 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002052 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002054
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002055 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002056
Karl Rosaen138a0412009-04-23 19:00:21 -07002057 if (initialQuery == null) {
2058 // Use any text typed in the launcher as the initial query
2059 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 if (appSearchData == null) {
2062 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002063 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
Winson Chungadf0c182012-08-23 14:59:07 -07002065 Rect sourceBounds = new Rect();
2066 if (mSearchDropTargetBar != null) {
2067 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2068 }
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Bjorn Bringertc459e522013-06-07 19:36:01 +01002070 startSearch(initialQuery, selectInitialQuery,
2071 appSearchData, sourceBounds);
2072 }
2073
2074 public void startSearch(String initialQuery,
2075 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002076 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002078 }
2079
2080 /**
2081 * Starts the global search activity. This code is a copied from SearchManager
2082 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002083 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002084 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002085 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002086 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2087 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2088 if (globalSearchActivity == null) {
2089 Log.w(TAG, "No global search activity found.");
2090 return;
2091 }
2092 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2093 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2094 intent.setComponent(globalSearchActivity);
2095 // Make sure that we have a Bundle to put source in
2096 if (appSearchData == null) {
2097 appSearchData = new Bundle();
2098 } else {
2099 appSearchData = new Bundle(appSearchData);
2100 }
2101 // Set source to package name of app that starts global search, if not set already.
2102 if (!appSearchData.containsKey("source")) {
2103 appSearchData.putString("source", getPackageName());
2104 }
2105 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2106 if (!TextUtils.isEmpty(initialQuery)) {
2107 intent.putExtra(SearchManager.QUERY, initialQuery);
2108 }
2109 if (selectInitialQuery) {
2110 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2111 }
2112 intent.setSourceBounds(sourceBounds);
2113 try {
2114 startActivity(intent);
2115 } catch (ActivityNotFoundException ex) {
2116 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 }
2119
Yura4f93ec62014-02-04 14:15:21 +00002120 public boolean isOnCustomContent() {
2121 return mWorkspace.isOnOrMovingToCustomContent();
2122 }
2123
Winson Chung70d72102011-08-12 11:24:35 -07002124 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002125 public boolean onPrepareOptionsMenu(Menu menu) {
2126 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002127 if (!isOnCustomContent()) {
2128 // Close any open folders
2129 closeFolder();
2130 // Stop resizing any widgets
2131 mWorkspace.exitWidgetResizeMode();
2132 if (!mWorkspace.isInOverviewMode()) {
2133 // Show the overview mode
2134 showOverviewMode(true);
2135 } else {
2136 showWorkspace(true);
2137 }
Winson Chunge0298742014-01-17 12:03:00 -08002138 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002139 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002142 @Override
2143 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002144 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002145 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002147 }
2148
Joe Onorato9c1289c2009-08-17 11:03:03 -04002149 public boolean isWorkspaceLocked() {
2150 return mWorkspaceLoading || mWaitingForResult;
2151 }
2152
Adam Cohen517a7f52014-03-01 12:12:59 -08002153 public boolean isWorkspaceLoading() {
2154 return mWorkspaceLoading;
2155 }
2156
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002157 private void setWorkspaceLoading(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWorkspaceLoading = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 private void setWaitingForResult(boolean value) {
2166 boolean isLocked = isWorkspaceLocked();
2167 mWaitingForResult = value;
2168 if (isLocked != isWorkspaceLocked()) {
2169 onWorkspaceLockedChanged();
2170 }
2171 }
2172
2173 protected void onWorkspaceLockedChanged() { }
2174
Michael Jurka0280c3b2010-09-17 15:00:07 -07002175 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002176 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002177 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002178 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2179 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002180 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002181 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002182 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002183
Adam Cohenad4e15c2013-10-17 16:21:35 -07002184 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2185 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2186 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2187 }
2188
2189 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2190 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2191 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002192 if (appWidgetInfo.configure != null) {
2193 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002194 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002195
Bjorn Bringert7984c942009-12-09 15:38:25 +00002196 // Launch over to configure widget, if needed
2197 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002199 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002200 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002202 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002203 Runnable onComplete = new Runnable() {
2204 @Override
2205 public void run() {
2206 // Exit spring loaded mode if necessary after adding the widget
2207 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2208 null);
2209 }
2210 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002212 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002213 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 }
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002217 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002218 // Close any folders that may be open.
2219 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002220 mWorkspace.moveToCustomContentScreen(animate);
2221 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002222 /**
2223 * Process a shortcut drop.
2224 *
2225 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002227 * @param cell The cell it should be added to, optional
2228 * @param position The location on the screen where it was dropped, optional
2229 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002232 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002234 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236
2237 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.cellX = cell[0];
2239 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241
2242 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2243 createShortcutIntent.setComponent(componentName);
2244 processShortcut(createShortcutIntent);
2245 }
2246
Adam Cohenfbba09b2011-07-18 21:43:05 -07002247 /**
2248 * Process a widget drop.
2249 *
2250 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 * @param cell The cell it should be added to, optional
2253 * @param position The location on the screen where it was dropped, optional
2254 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002256 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002257 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002258 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002259 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002260 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002261 mPendingAddInfo.minSpanX = info.minSpanX;
2262 mPendingAddInfo.minSpanY = info.minSpanY;
2263
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.cellX = cell[0];
2266 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002268 if (span != null) {
2269 mPendingAddInfo.spanX = span[0];
2270 mPendingAddInfo.spanY = span[1];
2271 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272
Adam Cohened66b2b2012-01-23 17:28:51 -08002273 AppWidgetHostView hostView = info.boundWidget;
2274 int appWidgetId;
2275 if (hostView != null) {
2276 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002277 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002278 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002279 // In this case, we either need to start an activity to get permission to bind
2280 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002281 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002282 Bundle options = info.bindOptions;
2283
2284 boolean success = false;
2285 if (options != null) {
2286 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2287 info.componentName, options);
2288 } else {
2289 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2290 info.componentName);
2291 }
2292 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002296 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2297 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2298 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002299 // TODO: we need to make sure that this accounts for the options bundle.
2300 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002301 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2302 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002303 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 }
2305
Joe Onoratodeb98af2010-02-19 14:59:39 -08002306 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002307 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 }
2309
Winson Chung24ab2f12010-09-16 14:10:47 -07002310 void processWallpaper(Intent intent) {
2311 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2312 }
2313
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002316 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 folderInfo.title = getText(R.string.folder_name);
2318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002322 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323
2324 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 FolderIcon newFolder =
2326 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002329 // Force measure the new folder icon
2330 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2331 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002332 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 }
Romain Guycbb89e42009-06-08 15:52:54 -07002334
Joe Onorato9c1289c2009-08-17 11:03:03 -04002335 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002336 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002337 }
2338
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002339 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002340 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002341 }
2342
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002343 /**
2344 * Registers various content observers. The current implementation registers
2345 * only a favorites observer to keep track of the favorites applications.
2346 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002347 private void registerContentObservers() {
2348 ContentResolver resolver = getContentResolver();
2349 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2350 true, mWidgetObserver);
2351 }
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 @Override
2354 public boolean dispatchKeyEvent(KeyEvent event) {
2355 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2356 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002358 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002359 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002360 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002361 dumpState();
2362 return true;
2363 }
2364 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002365 }
2366 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2367 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002368 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 return true;
2370 }
2371 }
2372
2373 return super.dispatchKeyEvent(event);
2374 }
2375
Joe Onorato88ec0992009-11-19 13:16:06 -08002376 @Override
2377 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002378 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002379 if (mAppsCustomizeContent.getContentType() ==
2380 AppsCustomizePagedView.ContentType.Applications) {
2381 showWorkspace(true);
2382 } else {
2383 showOverviewMode(true);
2384 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002385 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002386 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002387 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002388 Folder openFolder = mWorkspace.getOpenFolder();
2389 if (openFolder.isEditingName()) {
2390 openFolder.dismissEditingName();
2391 } else {
2392 closeFolder();
2393 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002394 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002395 mWorkspace.exitWidgetResizeMode();
2396
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002397 // Back button is a no-op here, but give at least some feedback for the button press
2398 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002399 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002400 }
2401
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002403 * Re-listen when widgets are reset.
2404 */
2405 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002406 if (mAppWidgetHost != null) {
2407 mAppWidgetHost.startListening();
2408 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002409 }
2410
2411 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 * Launches the intent referred by the clicked shortcut.
2413 *
2414 * @param v The view representing the clicked shortcut.
2415 */
2416 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002417 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2418 // view has detached (it's possible for this to happen if the view is removed mid touch).
2419 if (v.getWindowToken() == null) {
2420 return;
2421 }
2422
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002423 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002424 return;
2425 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002426
Adam Cohen1697b792013-09-17 19:08:21 -07002427 if (v instanceof Workspace) {
2428 if (mWorkspace.isInOverviewMode()) {
2429 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002430 }
2431 return;
2432 }
2433
2434 if (v instanceof CellLayout) {
2435 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002436 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002437 }
2438 }
2439
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002441 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002442 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002444 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002445 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002446 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002448 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 }
2450 }
2451
Michael Jurka0e260592010-06-30 17:07:39 -07002452 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002453 return false;
2454 }
2455
Michael Jurkaaf442092010-06-10 17:01:57 -07002456 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002457 * Event handler for the search button
2458 *
2459 * @param v The view that was clicked.
2460 */
2461 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002462 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2463
Amith Yamasania135ba82011-08-09 17:42:01 -07002464 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002465 }
2466
2467 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002468 * Event handler for the voice button
2469 *
2470 * @param v The view that was clicked.
2471 */
2472 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002473 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002474
Bjorn Bringertc459e522013-06-07 19:36:01 +01002475 startVoice();
2476 }
2477
2478 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002479 try {
2480 final SearchManager searchManager =
2481 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2482 ComponentName activityName = searchManager.getGlobalSearchActivity();
2483 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002484 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002485 if (activityName != null) {
2486 intent.setPackage(activityName.getPackageName());
2487 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002488 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002489 } catch (ActivityNotFoundException e) {
2490 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002491 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002492 startActivitySafely(null, intent, "onClickVoiceButton");
2493 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002494 }
2495
2496 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002497 * Event handler for the "grid" button that appears on the home screen, which
2498 * enters all apps mode.
2499 *
2500 * @param v The view that was clicked.
2501 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 protected void onClickAllAppsButton(View v) {
2503 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2504 if (isAllAppsVisible()) {
2505 showWorkspace(true);
2506 } else {
2507 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2508 }
2509 }
2510
2511 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002512 * Event handler for a paged view icon click.
2513 * @param v The view that was clicked.
2514 * @param appInfo The {link AppInfo} of the view.
2515 */
2516 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2517 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2518 startActivitySafely(v, appInfo.intent, appInfo);
2519 getStats().recordLaunch(appInfo.intent);
2520 }
2521
2522 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 * Event handler for an app shortcut click.
2524 *
2525 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2526 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002527 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2529 Object tag = v.getTag();
2530 if (!(tag instanceof ShortcutInfo)) {
2531 throw new IllegalArgumentException("Input must be a Shortcut");
2532 }
2533
2534 // Open shortcut
2535 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2536 final Intent intent = shortcut.intent;
2537
2538 // Check for special shortcuts
2539 if (intent.getComponent() != null) {
2540 final String shortcutClass = intent.getComponent().getClassName();
2541
2542 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2543 MemoryDumpActivity.startDump(this);
2544 return;
2545 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2546 toggleShowWeightWatcher();
2547 return;
2548 }
2549 }
2550
Chris Wren40c5ed32014-06-24 18:24:23 -04002551 // Check for abandoned promise
2552 if (shortcut.isAbandoned() && v instanceof BubbleTextView) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002553 AlertDialog.Builder builder = new AlertDialog.Builder(this);
2554 builder.setTitle(R.string.abandoned_promises_title);
Chris Wren803a4be2014-07-01 16:52:49 -04002555 builder.setMessage(R.string.abandoned_promise_explanation);
Chris Wren40c5ed32014-06-24 18:24:23 -04002556 builder.setPositiveButton(R.string.abandoned_search,
2557 new DialogInterface.OnClickListener() {
2558 public void onClick(DialogInterface dialog, int id) {
2559 startAppShortcutActivity(v);
2560 }
2561 }
2562 );
Chris Wren40c5ed32014-06-24 18:24:23 -04002563 builder.setNeutralButton(R.string.abandoned_clean_this,
2564 new DialogInterface.OnClickListener() {
2565 public void onClick(DialogInterface dialog, int id) {
2566 final BubbleTextView bubble = (BubbleTextView) v;
2567 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2568 mWorkspace.removeAbandonedPromise(bubble, user);
2569 }
2570 });
2571 builder.create().show();
2572 return;
2573 }
2574
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 // Start activities
Chris Wren40c5ed32014-06-24 18:24:23 -04002576 startAppShortcutActivity(v);
2577 }
2578
2579 private void startAppShortcutActivity(View v) {
2580 Object tag = v.getTag();
2581 if (!(tag instanceof ShortcutInfo)) {
2582 throw new IllegalArgumentException("Input must be a Shortcut");
2583 }
2584 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2585 final Intent intent = shortcut.intent;
2586
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 int[] pos = new int[2];
2588 v.getLocationOnScreen(pos);
2589 intent.setSourceBounds(new Rect(pos[0], pos[1],
2590 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2591
2592 boolean success = startActivitySafely(v, intent, tag);
2593
2594 mStats.recordLaunch(intent, shortcut);
2595
2596 if (success && v instanceof BubbleTextView) {
2597 mWaitingForResume = (BubbleTextView) v;
2598 mWaitingForResume.setStayPressed(true);
2599 }
2600 }
2601
2602 /**
2603 * Event handler for a folder icon click.
2604 *
2605 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2606 */
2607 protected void onClickFolderIcon(View v) {
2608 if (LOGD) Log.d(TAG, "onClickFolder");
2609 if (!(v instanceof FolderIcon)){
2610 throw new IllegalArgumentException("Input must be a FolderIcon");
2611 }
2612
2613 FolderIcon folderIcon = (FolderIcon) v;
2614 final FolderInfo info = folderIcon.getFolderInfo();
2615 Folder openFolder = mWorkspace.getFolderForTag(info);
2616
2617 // If the folder info reports that the associated folder is open, then verify that
2618 // it is actually opened. There have been a few instances where this gets out of sync.
2619 if (info.opened && openFolder == null) {
2620 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2621 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2622 info.opened = false;
2623 }
2624
2625 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2626 // Close any open folder
2627 closeFolder();
2628 // Open the requested folder
2629 openFolder(folderIcon);
2630 } else {
2631 // Find the open folder...
2632 int folderScreen;
2633 if (openFolder != null) {
2634 folderScreen = mWorkspace.getPageForView(openFolder);
2635 // .. and close it
2636 closeFolder(openFolder);
2637 if (folderScreen != mWorkspace.getCurrentPage()) {
2638 // Close any folder open on the current screen
2639 closeFolder();
2640 // Pull the folder onto this screen
2641 openFolder(folderIcon);
2642 }
2643 }
2644 }
Michael Jurka5130e402011-10-13 04:55:35 -07002645 }
2646
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002647 /**
2648 * Event handler for the (Add) Widgets button that appears after a long press
2649 * on the home screen.
2650 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002651 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002653 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2654 }
2655
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 /**
2657 * Event handler for the wallpaper picker button that appears after a long press
2658 * on the home screen.
2659 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002660 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2662 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2663 pickWallpaper.setComponent(getWallpaperPickerComponent());
2664 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2665 }
2666
2667 /**
2668 * Event handler for a click on the settings button that appears after a long press
2669 * on the home screen.
2670 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002671 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2673 }
2674
Michael Jurka5130e402011-10-13 04:55:35 -07002675 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002676 // Provide the same haptic feedback that the system offers for virtual keys.
2677 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002678 }
2679
Adam Cohen61f560d2013-09-30 15:58:20 -07002680 public void performHapticFeedbackOnTouchDown(View v) {
2681 // Provide the same haptic feedback that the system offers for virtual keys.
2682 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2683 }
2684
2685 public View.OnTouchListener getHapticFeedbackTouchListener() {
2686 if (mHapticFeedbackTouchListener == null) {
2687 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2688 @Override
2689 public boolean onTouch(View v, MotionEvent event) {
2690 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2691 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2692 }
2693 return false;
2694 }
2695 };
2696 }
2697 return mHapticFeedbackTouchListener;
2698 }
2699
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002700 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002701 if (!DISABLE_MARKET_BUTTON) {
2702 if (mAppMarketIntent != null) {
2703 startActivitySafely(v, mAppMarketIntent, "app market");
2704 } else {
2705 Log.e(TAG, "Invalid app market intent.");
2706 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002707 }
2708 }
2709
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002710 public void onDragStarted(View view) {}
2711
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002712 /**
2713 * Called when the user stops interacting with the launcher.
2714 * This implies that the user is now on the homescreen and is not doing housekeeping.
2715 */
2716 protected void onInteractionEnd() {}
2717
2718 /**
2719 * Called when the user starts interacting with the launcher.
2720 * The possible interactions are:
2721 * - open all apps
2722 * - reorder an app shortcut, or a widget
2723 * - open the overview mode.
2724 * This is a good time to stop doing things that only make sense
2725 * when the user is on the homescreen and not doing housekeeping.
2726 */
2727 protected void onInteractionBegin() {}
2728
Kenny Guyf07af7b2014-07-31 11:39:16 +01002729 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002730 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002731 try {
2732 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2733 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2734 launcherApps.showAppDetailsForProfile(componentName, user);
2735 } catch (SecurityException e) {
2736 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2737 Log.e(TAG, "Launcher does not have permission to launch settings");
2738 } catch (ActivityNotFoundException e) {
2739 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2740 Log.e(TAG, "Unable to launch settings");
2741 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002742 }
2743
Michael Jurka1e2f4652013-07-08 18:03:46 -07002744 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002745 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2746 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002747 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002748 // System applications cannot be installed. For now, show a toast explaining that.
2749 // We may give them the option of disabling apps this way.
2750 int messageId = R.string.uninstall_system_app_text;
2751 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002752 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002753 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002754 String packageName = componentName.getPackageName();
2755 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002756 Intent intent = new Intent(
2757 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002758 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2759 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002760 if (user != null) {
2761 user.addToIntent(intent, Intent.EXTRA_USER);
2762 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002763 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002764 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002765 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002766 }
2767
Michael Jurka86a720e2012-05-09 11:23:23 -07002768 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002769 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002770 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002771 // Only launch using the new animation if the shortcut has not opted out (this is a
2772 // private contract between launcher and may be ignored in the future).
2773 boolean useLaunchAnimation = (v != null) &&
2774 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002775 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2776 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002777
Kenny Guy1317e2d2014-05-08 18:52:50 +01002778 UserHandleCompat user = null;
2779 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2780 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2781 user = userManager.getUserForSerialNumber(serialNumber);
2782 }
2783
2784 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002785 if (useLaunchAnimation) {
Adam Cohen4da294d2014-07-28 10:56:19 -07002786 ActivityOptions opts = Utilities.isLmp() ?
2787 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2788 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002789 optsBundle = opts.toBundle();
2790 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002791
2792 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2793 // Could be launching some bookkeeping activity
2794 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002795 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002796 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002797 launcherApps.startActivityForProfile(intent.getComponent(), user,
2798 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002799 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002800 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 } catch (SecurityException e) {
2802 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002803 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002804 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002805 "or use the exported attribute for this activity. "
2806 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002808 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002810
Michael Jurka86a720e2012-05-09 11:23:23 -07002811 boolean startActivitySafely(View v, Intent intent, Object tag) {
2812 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002813 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2814 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2815 return false;
2816 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002817 try {
2818 success = startActivity(v, intent, tag);
2819 } catch (ActivityNotFoundException e) {
2820 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2821 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2822 }
2823 return success;
2824 }
2825
Adam Cohen268c4752012-06-06 17:47:33 -07002826 /**
2827 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2828 * in the DragLayer in the exact absolute location of the original FolderIcon.
2829 */
2830 private void copyFolderIconToImage(FolderIcon fi) {
2831 final int width = fi.getMeasuredWidth();
2832 final int height = fi.getMeasuredHeight();
2833
2834 // Lazy load ImageView, Bitmap and Canvas
2835 if (mFolderIconImageView == null) {
2836 mFolderIconImageView = new ImageView(this);
2837 }
2838 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2839 mFolderIconBitmap.getHeight() != height) {
2840 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2841 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2842 }
2843
2844 DragLayer.LayoutParams lp;
2845 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2846 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2847 } else {
2848 lp = new DragLayer.LayoutParams(width, height);
2849 }
2850
Adam Cohen307fe232012-08-16 17:55:58 -07002851 // The layout from which the folder is being opened may be scaled, adjust the starting
2852 // view size by this scale factor.
2853 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002854 lp.customPosition = true;
2855 lp.x = mRectForFolderAnimation.left;
2856 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002857 lp.width = (int) (scale * width);
2858 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002859
2860 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2861 fi.draw(mFolderIconCanvas);
2862 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002863 if (fi.getFolder() != null) {
2864 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2865 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002866 }
Adam Cohen268c4752012-06-06 17:47:33 -07002867 // Just in case this image view is still in the drag layer from a previous animation,
2868 // we remove it and re-add it.
2869 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2870 mDragLayer.removeView(mFolderIconImageView);
2871 }
2872 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002873 if (fi.getFolder() != null) {
2874 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002875 }
Adam Cohen268c4752012-06-06 17:47:33 -07002876 }
2877
Adam Cohen2801caf2011-05-13 20:57:39 -07002878 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002879 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002880 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2881 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2882 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2883
Adam Cohenc51934b2011-07-26 21:07:43 -07002884 FolderInfo info = (FolderInfo) fi.getTag();
2885 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2886 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002887 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2888 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002889 }
2890
Adam Cohen268c4752012-06-06 17:47:33 -07002891 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2892 copyFolderIconToImage(fi);
2893 fi.setVisibility(View.INVISIBLE);
2894
Michael Jurka2ecf9952012-06-18 12:52:28 -07002895 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002896 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002897 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2898 oa.start();
2899 }
2900
Adam Cohen268c4752012-06-06 17:47:33 -07002901 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002902 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002903 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2904 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2905 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2906
Adam Cohen268c4752012-06-06 17:47:33 -07002907 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002908
Adam Cohen268c4752012-06-06 17:47:33 -07002909 // We remove and re-draw the FolderIcon in-case it has changed
2910 mDragLayer.removeView(mFolderIconImageView);
2911 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002912 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002913 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002914 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002915 oa.addListener(new AnimatorListenerAdapter() {
2916 @Override
2917 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002918 if (cl != null) {
2919 cl.clearFolderLeaveBehind();
2920 // Remove the ImageView copy of the FolderIcon and make the original visible.
2921 mDragLayer.removeView(mFolderIconImageView);
2922 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002923 }
2924 }
2925 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002926 oa.start();
2927 }
2928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002930 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 * is animated relative to the specified View. If the View is null, no animation
2932 * is played.
2933 *
2934 * @param folderInfo The FolderInfo describing the folder to open.
2935 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002936 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002937 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002938 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939
Adam Cohena9cf38f2011-05-02 15:36:58 -07002940 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002941
Adam Cohen4554ee12011-08-03 16:13:21 -07002942 // Just verify that the folder hasn't already been added to the DragLayer.
2943 // There was a one-off crash where the folder had a parent already.
2944 if (folder.getParent() == null) {
2945 mDragLayer.addView(folder);
2946 mDragController.addDropTarget((DropTarget) folder);
2947 } else {
2948 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2949 folder.getParent() + ").");
2950 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002951 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002952 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002953
2954 // Notify the accessibility manager that this folder "window" has appeared and occluded
2955 // the workspace items
2956 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2957 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002958 }
2959
2960 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002961 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002962 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002963 if (folder.isEditingName()) {
2964 folder.dismissEditingName();
2965 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002966 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002967
2968 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002969 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002970 }
2971 }
2972
2973 void closeFolder(Folder folder) {
2974 folder.getInfo().opened = false;
2975
2976 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2977 if (parent != null) {
2978 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2979 shrinkAndFadeInFolderIcon(fi);
2980 }
2981 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002982
2983 // Notify the accessibility manager that this folder "window" has disappeard and no
2984 // longer occludeds the workspace items
2985 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986 }
2987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002989 if (!isDraggingEnabled()) return false;
2990 if (isWorkspaceLocked()) return false;
2991 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992
Adam Cohen1697b792013-09-17 19:08:21 -07002993 if (v instanceof Workspace) {
2994 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002995 if (mWorkspace.enterOverviewMode()) {
2996 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2997 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2998 return true;
2999 } else {
3000 return false;
3001 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003002 } else {
3003 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003004 }
Adam Cohen1697b792013-09-17 19:08:21 -07003005 }
3006
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003007 CellLayout.CellInfo longClickCellInfo = null;
3008 View itemUnderLongClick = null;
3009 if (v.getTag() instanceof ItemInfo) {
3010 ItemInfo info = (ItemInfo) v.getTag();
3011 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3012 itemUnderLongClick = longClickCellInfo.cell;
3013 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
3015
Winson Chung3d503fb2011-07-13 17:25:49 -07003016 // The hotseat touch handling does not go through Workspace, and we always allow long press
3017 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003018 final boolean inHotseat = isHotseatLayout(v);
3019 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003020 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003021 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003022 // User long pressed on empty space
3023 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3024 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003025 if (mWorkspace.isInOverviewMode()) {
3026 mWorkspace.startReordering(v);
3027 } else {
3028 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003031 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3032 mHotseat.getOrderInHotseat(
3033 longClickCellInfo.cellX,
3034 longClickCellInfo.cellY));
3035 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003037 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039 }
3040 }
3041 return true;
3042 }
3043
Winson Chung3d503fb2011-07-13 17:25:49 -07003044 boolean isHotseatLayout(View layout) {
3045 return mHotseat != null && layout != null &&
3046 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3047 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003048
Winson Chung3d503fb2011-07-13 17:25:49 -07003049 /**
3050 * Returns the CellLayout of the specified container at the specified screen.
3051 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003052 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003053 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3054 if (mHotseat != null) {
3055 return mHotseat.getLayout();
3056 } else {
3057 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003058 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003059 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003060 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003061 }
3062 }
3063
Daniel Sandler843e8602010-06-07 14:59:01 -04003064 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003065 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003066 }
3067
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003068 /**
3069 * Helper method for the cameraZoomIn/cameraZoomOut animations
3070 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003071 * @param scaleFactor The scale factor used for the zoom
3072 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003073 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003074 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003075 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003076 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003077
Craig Mautner360310b2012-10-26 15:13:08 -07003078 private void setWorkspaceBackground(boolean workspace) {
3079 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003080 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003081 }
3082
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003083 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003084 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3085 int curflags = getWindow().getAttributes().flags
3086 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3087 if (wpflags != curflags) {
3088 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3089 }
Craig Mautner360310b2012-10-26 15:13:08 -07003090 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003091 }
3092
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003093 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3094 if (v instanceof LauncherTransitionable) {
3095 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3096 }
3097 }
3098
Michael Jurkabed61d22012-02-14 22:51:29 -08003099 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3100 if (v instanceof LauncherTransitionable) {
3101 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3102 }
Winson Chung70442722012-02-10 15:43:22 -08003103
3104 // Update the workspace transition step as well
3105 dispatchOnLauncherTransitionStep(v, 0f);
3106 }
3107
3108 private void dispatchOnLauncherTransitionStep(View v, float t) {
3109 if (v instanceof LauncherTransitionable) {
3110 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3111 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003112 }
3113
3114 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3115 if (v instanceof LauncherTransitionable) {
3116 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3117 }
Winson Chung70442722012-02-10 15:43:22 -08003118
3119 // Update the workspace transition step as well
3120 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003121 }
3122
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003123 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003124 * Things to test when changing the following seven functions.
3125 * - Home from workspace
3126 * - from center screen
3127 * - from other screens
3128 * - Home from all apps
3129 * - from center screen
3130 * - from other screens
3131 * - Back from all apps
3132 * - from center screen
3133 * - from other screens
3134 * - Launch app from workspace and quit
3135 * - with back
3136 * - with home
3137 * - Launch app from all apps and quit
3138 * - with back
3139 * - with home
3140 * - Go to a screen that's not the default, then all
3141 * apps, and launch and app, and go back
3142 * - with back
3143 * -with home
3144 * - On workspace, long press power and go back
3145 * - with back
3146 * - with home
3147 * - On all apps, long press power and go back
3148 * - with back
3149 * - with home
3150 * - On workspace, power off
3151 * - On all apps, power off
3152 * - Launch an app and turn off the screen while in that app
3153 * - Go back with home key
3154 * - Go back with back key TODO: make this not go to workspace
3155 * - From all apps
3156 * - From workspace
3157 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3158 * - From all apps
3159 * - From the center workspace
3160 * - From another workspace
3161 */
3162
3163 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003164 * Zoom the camera out from the workspace to reveal 'toView'.
3165 * Assumes that the view to show is anchored at either the very top or very bottom
3166 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003167 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003168 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003169 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3170 showAppsCustomizeHelper(animated, springLoaded, contentType);
3171 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003172
Winson Chungc58497e2013-09-03 17:48:37 -07003173 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3174 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003175 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003176 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003177 mStateAnimation.cancel();
3178 mStateAnimation = null;
3179 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003180
3181 boolean material = Utilities.isLmp();
3182
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003183 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003184
Winson Chungf0ea4d32011-06-06 14:27:16 -07003185 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3186 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003187 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3188
Winson Chungf0ea4d32011-06-06 14:27:16 -07003189 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003190 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003191 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003192 final int startDelay =
3193 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003194
Michael Jurkab3e22d92011-10-31 15:58:33 -07003195 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003196
Adam Cohen6c5891a2014-07-09 23:53:15 -07003197 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3198 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003199 Animator workspaceAnim =
Adam Cohen6c5891a2014-07-09 23:53:15 -07003200 mWorkspace.getChangeStateAnimation(workspaceState, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003201 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003202 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3203 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003204 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3205 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003206
3207 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003208 if (!material) {
3209 toView.setScaleX(scale);
3210 toView.setScaleY(scale);
3211 final LauncherViewPropertyAnimator scaleAnim =
3212 new LauncherViewPropertyAnimator(toView);
3213 scaleAnim.
3214 scaleX(1f).scaleY(1f).
3215 setDuration(duration).
3216 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003217
Adam Cohen6c5891a2014-07-09 23:53:15 -07003218 toView.setVisibility(View.VISIBLE);
3219 toView.setAlpha(0f);
3220 final ObjectAnimator alphaAnim = LauncherAnimUtils
3221 .ofFloat(toView, "alpha", 0f, 1f)
3222 .setDuration(fadeDuration);
3223 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
3224 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3225 @Override
3226 public void onAnimationUpdate(ValueAnimator animation) {
3227 if (animation == null) {
3228 throw new RuntimeException("animation is null");
3229 }
3230 float t = (Float) animation.getAnimatedValue();
3231 dispatchOnLauncherTransitionStep(fromView, t);
3232 dispatchOnLauncherTransitionStep(toView, t);
Michael Jurka059798a2012-09-04 09:20:16 -07003233 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003234 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003235
Adam Cohen6c5891a2014-07-09 23:53:15 -07003236 // toView should appear right at the end of the workspace shrink
3237 // animation
3238 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3239 mStateAnimation.play(scaleAnim).after(startDelay);
3240 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003241
Adam Cohen6c5891a2014-07-09 23:53:15 -07003242 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3243 @Override
3244 public void onAnimationStart(Animator animation) {
3245 // Prepare the position
3246 toView.setTranslationX(0.0f);
3247 toView.setTranslationY(0.0f);
3248 toView.setVisibility(View.VISIBLE);
3249 toView.bringToFront();
Winson Chungadf0c182012-08-23 14:59:07 -07003250 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003251 @Override
3252 public void onAnimationEnd(Animator animation) {
3253 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3254 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003255
Adam Cohen6c5891a2014-07-09 23:53:15 -07003256 // Hide the search bar
3257 if (mSearchDropTargetBar != null) {
3258 mSearchDropTargetBar.hideSearchBar(false);
3259 }
3260 }
3261 });
3262 } else {
3263 int width = toView.getMeasuredWidth();
3264 int height = toView.getMeasuredHeight();
3265 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3266
3267 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3268
3269 AppsCustomizePagedView content = (AppsCustomizePagedView)
3270 toView.findViewById(R.id.apps_customize_pane_content);
3271
3272 View page = content.getPageAt(content.getCurrentPage());
3273 View revealView = content;
3274
3275 float yDrift = height / 2f - 400;
3276
3277 LauncherViewPropertyAnimator panelAlphaAndDrift =
3278 new LauncherViewPropertyAnimator(revealView);
3279 revealView.setTranslationY(yDrift);
3280 revealView.setAlpha(0.3f);
3281 panelAlphaAndDrift.alpha(1)
3282 .translationY(0)
3283 .setDuration(revealDuration)
3284 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3285
3286 mStateAnimation.play(panelAlphaAndDrift);
3287
3288 if (page instanceof CellLayout) {
3289 CellLayout cellLayout = (CellLayout) page;
3290 cellLayout.enableHardwareLayer(true);
3291
3292 View iconsView = cellLayout.getShortcutsAndWidgets();
3293 iconsView.setAlpha(0f);
3294
3295 LauncherViewPropertyAnimator iconsAlpha =
3296 new LauncherViewPropertyAnimator(iconsView);
3297 iconsAlpha.alpha(1f)
3298 .setDuration(revealDuration - 100)
3299 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3300 mStateAnimation.play(iconsAlpha);
3301 }
3302
3303 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3304 pageIndicators.setAlpha(0f);
3305 final LauncherViewPropertyAnimator indicatorsAlpha =
3306 new LauncherViewPropertyAnimator(pageIndicators);
3307 indicatorsAlpha.alpha(1f);
3308 indicatorsAlpha.setDuration(revealDuration);
3309 mStateAnimation.play(indicatorsAlpha);
3310
3311 width = revealView.getMeasuredWidth();
3312
Nilesh Agrawal300c1502014-07-22 13:20:54 -07003313 Animator reveal =
Adam Cohen6c5891a2014-07-09 23:53:15 -07003314 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3315 height / 2 + 100, 0f, revealRadius);
3316 reveal.setDuration(revealDuration);
3317 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3318
3319 toView.setTranslationX(0);
3320 toView.setTranslationY(0);
3321 toView.setAlpha(1f);
3322 // toView should appear right at the end of the workspace shrink
3323 // animation
3324 mStateAnimation.play(reveal);
3325
3326 reveal.addListener(new AnimatorListenerAdapter() {
3327 @Override
3328 public void onAnimationStart(Animator animation) {
3329 // Prepare the position
3330 toView.bringToFront();
3331 toView.setVisibility(View.VISIBLE);
3332 }
3333 });
3334
3335 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3336 @Override
3337 public void onAnimationEnd(Animator animation) {
3338 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3339 dispatchOnLauncherTransitionEnd(toView, animated, false);
3340
3341 // Hide the search bar
3342 if (mSearchDropTargetBar != null) {
3343 mSearchDropTargetBar.hideSearchBar(false);
3344 }
3345 }
3346 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003347 }
Michael Jurka1899a362011-11-03 13:50:45 -07003348
3349 boolean delayAnim = false;
Adam Cohen6c5891a2014-07-09 23:53:15 -07003350 if (workspaceAnim != null) {
3351 mStateAnimation.play(workspaceAnim);
3352 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003353 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3354 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003355
3356 // If any of the objects being animated haven't been measured/laid out
3357 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003358 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003359 (mWorkspace.getMeasuredWidth() == 0) ||
3360 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003361 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003362 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003363
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003364 final AnimatorSet stateAnimation = mStateAnimation;
3365 final Runnable startAnimRunnable = new Runnable() {
3366 public void run() {
3367 // Check that mStateAnimation hasn't changed while
3368 // we waited for a layout/draw pass
3369 if (mStateAnimation != stateAnimation)
3370 return;
3371 setPivotsForZoom(toView, scale);
3372 dispatchOnLauncherTransitionStart(fromView, animated, false);
3373 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003374 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003375 }
3376 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003377 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003378 final ViewTreeObserver observer = toView.getViewTreeObserver();
3379 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3380 public void onGlobalLayout() {
3381 startAnimRunnable.run();
3382 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3383 }
3384 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003385 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003386 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003387 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003388 } else {
3389 toView.setTranslationX(0.0f);
3390 toView.setTranslationY(0.0f);
3391 toView.setScaleX(1.0f);
3392 toView.setScaleY(1.0f);
3393 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003394 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003395
Daniel Sandlere4f98912013-06-25 15:13:26 -04003396 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003397 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003398 if (mSearchDropTargetBar != null) {
3399 mSearchDropTargetBar.hideSearchBar(false);
3400 }
Michael Jurkaabded662011-03-04 12:06:57 -08003401 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003402 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003403 dispatchOnLauncherTransitionStart(fromView, animated, false);
3404 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003405 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003406 dispatchOnLauncherTransitionStart(toView, animated, false);
3407 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003408 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003409 }
3410
3411 /**
3412 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003414 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003415 */
Adam Cohened307df2013-10-02 09:37:31 -07003416 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003417 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003418
Michael Jurkab3e22d92011-10-31 15:58:33 -07003419 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003420 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421 mStateAnimation.cancel();
3422 mStateAnimation = null;
3423 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003424
3425 boolean material = Utilities.isLmp();
3426
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003427 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003428
Winson Chungf0ea4d32011-06-06 14:27:16 -07003429 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003430 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
3431 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3432
Winson Chungf0ea4d32011-06-06 14:27:16 -07003433 final float scaleFactor = (float)
3434 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3435 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003436 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003437 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003438 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003439 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3440 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003441 toState, animated, stagger, -1);
3442 } else if (toState == Workspace.State.SPRING_LOADED ||
3443 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003444 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003445 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003446 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003447
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003449 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003450 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003451 if (!material) {
3452 final LauncherViewPropertyAnimator scaleAnim =
3453 new LauncherViewPropertyAnimator(fromView);
3454 scaleAnim.
3455 scaleX(scaleFactor).scaleY(scaleFactor).
3456 setDuration(duration).
3457 setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurka159b4cc2012-01-17 03:00:35 -08003458
Adam Cohen6c5891a2014-07-09 23:53:15 -07003459 final ObjectAnimator alphaAnim = LauncherAnimUtils
3460 .ofFloat(fromView, "alpha", 1f, 0f)
3461 .setDuration(fadeOutDuration);
3462 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
3463 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3464 @Override
3465 public void onAnimationUpdate(ValueAnimator animation) {
3466 float t = 1f - (Float) animation.getAnimatedValue();
3467 dispatchOnLauncherTransitionStep(fromView, t);
3468 dispatchOnLauncherTransitionStep(toView, t);
3469 }
3470 });
3471
3472 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3473
3474 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3475 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3476 mAppsCustomizeContent.stopScrolling();
3477
3478 mStateAnimation.playTogether(scaleAnim, alphaAnim);
3479 } else {
3480 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3481
3482 int width = fromView.getMeasuredWidth();
3483 int height = fromView.getMeasuredHeight();
3484 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3485
3486 AppsCustomizePagedView content = (AppsCustomizePagedView)
3487 fromView.findViewById(R.id.apps_customize_pane_content);
3488
3489 final View page = content.getPageAt(content.getNextPage());
3490 View revealView = page;
3491
3492 float yDrift = height / 2f - 400;
3493
3494 LauncherViewPropertyAnimator panelAlphaAndDrift =
3495 new LauncherViewPropertyAnimator(revealView);
3496 revealView.setTranslationY(0);
3497 revealView.setAlpha(1);
3498 panelAlphaAndDrift.alpha(0)
3499 .translationY(yDrift)
3500 .setDuration(revealDuration)
3501 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3502
3503 mStateAnimation.play(panelAlphaAndDrift);
3504
3505 if (page instanceof CellLayout) {
3506 final CellLayout cellLayout = (CellLayout) page;
3507 cellLayout.enableHardwareLayer(true);
3508
3509 final View iconsView = cellLayout.getShortcutsAndWidgets();
3510
3511 LauncherViewPropertyAnimator iconsAlpha =
3512 new LauncherViewPropertyAnimator(iconsView);
3513 iconsAlpha.alpha(0f)
3514 .setDuration(revealDuration - 100)
3515 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3516
3517 mStateAnimation.play(iconsAlpha);
3518
3519 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3520 @Override
3521 public void onAnimationEnd(Animator animation) {
3522 cellLayout.setTranslationY(0);
3523 cellLayout.setAlpha(1f);
3524 iconsView.setAlpha(1f);
3525 }
3526 });
Winson Chung70442722012-02-10 15:43:22 -08003527 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003528
Adam Cohen6c5891a2014-07-09 23:53:15 -07003529 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3530 final LauncherViewPropertyAnimator indicatorsAlpha =
3531 new LauncherViewPropertyAnimator(pageIndicators);
3532 indicatorsAlpha.alpha(0f);
3533 indicatorsAlpha.setDuration(revealDuration);
3534 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3535 mStateAnimation.play(indicatorsAlpha);
Michael Jurkabed61d22012-02-14 22:51:29 -08003536
Adam Cohen6c5891a2014-07-09 23:53:15 -07003537 width = revealView.getMeasuredWidth();
3538
Nilesh Agrawal300c1502014-07-22 13:20:54 -07003539 Animator reveal =
Adam Cohen6c5891a2014-07-09 23:53:15 -07003540 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3541 height / 2 + 100, revealRadius, 0f);
3542 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3543 reveal.setDuration(revealDuration);
3544
3545 reveal.addListener(new AnimatorListenerAdapter() {
3546 @Override
3547 public void onAnimationEnd(Animator animation) {
3548 fromView.setVisibility(View.GONE);
3549 }
3550 });
3551
3552 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3553 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3554 mAppsCustomizeContent.stopScrolling();
3555
3556 mStateAnimation.play(reveal);
3557 }
3558 if (workspaceAnim != null) {
3559 mStateAnimation.play(workspaceAnim);
3560 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003561
3562 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003563 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003564 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003565 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003566 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3567 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003568 if (onCompleteRunnable != null) {
3569 onCompleteRunnable.run();
3570 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003571 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003572 }
3573 });
3574
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003575 dispatchOnLauncherTransitionStart(fromView, animated, true);
3576 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003577 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003578 } else {
3579 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003580 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003581 dispatchOnLauncherTransitionStart(fromView, animated, true);
3582 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003583 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003584 dispatchOnLauncherTransitionStart(toView, animated, true);
3585 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003586 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003587 }
3588
Michael Jurkae326f182011-11-21 14:05:46 -08003589 @Override
3590 public void onTrimMemory(int level) {
3591 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003592 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003593 mAppsCustomizeTabHost.onTrimMemory();
3594 }
3595 }
3596
Adam Cohened307df2013-10-02 09:37:31 -07003597 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003598 showWorkspace(animated, null);
3599 }
3600
Adam Cohened307df2013-10-02 09:37:31 -07003601 protected void showWorkspace() {
3602 showWorkspace(true);
3603 }
3604
Adam Cohened66b2b2012-01-23 17:28:51 -08003605 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003606 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003607 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003608 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003609 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003610
Winson Chungc7d2b602012-05-16 17:02:20 -07003611 // Show the search bar (only animate if we were showing the drop target bar in spring
3612 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003613 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003614 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003615 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003616
Michael Jurkab3e22d92011-10-31 15:58:33 -07003617 // Set focus to the AppsCustomize button
3618 if (mAllAppsButton != null) {
3619 mAllAppsButton.requestFocus();
3620 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003621 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003622
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003623 // Change the state *after* we've called all the transition code
3624 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003625
Winson Chung5fb63472011-02-02 17:03:37 -08003626 // Resume the auto-advance of widgets
3627 mUserPresent = true;
3628 updateRunning();
3629
alanv1d4fde62012-10-17 13:15:47 -07003630 // Send an accessibility event to announce the context change
3631 getWindow().getDecorView()
3632 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003633
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003634 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003635 }
3636
Adam Cohened307df2013-10-02 09:37:31 -07003637 void showOverviewMode(boolean animated) {
3638 mWorkspace.setVisibility(View.VISIBLE);
3639 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3640 mState = State.WORKSPACE;
3641 onWorkspaceShown(animated);
3642 }
3643
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003644 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003645 }
3646
Winson Chung82963d52013-10-09 11:20:57 -07003647 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3648 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003649 if (mState != State.WORKSPACE) return;
3650
Winson Chung82963d52013-10-09 11:20:57 -07003651 if (resetPageToZero) {
3652 mAppsCustomizeTabHost.reset();
3653 }
Winson Chungc58497e2013-09-03 17:48:37 -07003654 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003655 mAppsCustomizeTabHost.post(new Runnable() {
3656 @Override
3657 public void run() {
3658 // We post this in-case the all apps view isn't yet constructed.
3659 mAppsCustomizeTabHost.requestFocus();
3660 }
3661 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003662
Michael Jurkab3e22d92011-10-31 15:58:33 -07003663 // Change the state *after* we've called all the transition code
3664 mState = State.APPS_CUSTOMIZE;
3665
3666 // Pause the auto-advance of widgets until we are out of AllApps
3667 mUserPresent = false;
3668 updateRunning();
3669 closeFolder();
3670
3671 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003672 getWindow().getDecorView()
3673 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003674 }
3675
Adam Cohen7777d962011-08-18 18:58:38 -07003676 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003677 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003678 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003679 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003680 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003681 }
Adam Cohen7777d962011-08-18 18:58:38 -07003682
Adam Cohenad4e15c2013-10-17 16:21:35 -07003683 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003684 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003685 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3686
Winson Chunge7a03942011-08-05 15:05:12 -07003687 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003688 @Override
3689 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003690 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003691 // Before we show workspace, hide all apps again because
3692 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3693 // clean up our state transition functions
3694 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003695 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003696 } else {
3697 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003698 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003699 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003700 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003701 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003702
Michael Jurkad3ef3062010-11-23 16:23:58 -08003703 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003704 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003705 final boolean animated = true;
3706 final boolean springLoaded = true;
3707 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003708 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003709 }
3710 // Otherwise, we are not in spring loaded mode, so don't do anything.
3711 }
3712
Michael Jurkab3e22d92011-10-31 15:58:33 -07003713 void lockAllApps() {
3714 // TODO
3715 }
3716
3717 void unlockAllApps() {
3718 // TODO
3719 }
3720
Winson Chungf0ea4d32011-06-06 14:27:16 -07003721 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003722 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003723 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003724 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003725 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003726 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003727 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003728 int duration = 0;
3729 if (mSearchDropTargetBar != null) {
3730 duration = mSearchDropTargetBar.getTransitionInDuration();
3731 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003732 mHotseat.animate().alpha(1f).setDuration(duration);
3733 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003734 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003735 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003736 }
3737 }
3738 }
3739
3740 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003741 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003742 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003743 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003744 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003745 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003746 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003747 int duration = 0;
3748 if (mSearchDropTargetBar != null) {
3749 duration = mSearchDropTargetBar.getTransitionOutDuration();
3750 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003751 mHotseat.animate().alpha(0f).setDuration(duration);
3752 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003753 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003754 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003755 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003756 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003757 }
3758
Patrick Dubroy5f445422011-02-18 14:35:21 -08003759 /**
3760 * Add an item from all apps or customize onto the given workspace screen.
3761 * If layout is null, add to the current screen.
3762 */
3763 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003764 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003765 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003766 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003767 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003768
Winson Chungdff8ebb2011-09-08 17:25:31 -07003769 /** Maps the current orientation to an index for referencing orientation correct global icons */
3770 private int getCurrentOrientationIndexForGlobalIcons() {
3771 // default - 0, landscape - 1
3772 switch (getResources().getConfiguration().orientation) {
3773 case Configuration.ORIENTATION_LANDSCAPE:
3774 return 1;
3775 default:
3776 return 0;
3777 }
3778 }
3779
Michael Jurkaae65ee32012-04-30 11:45:54 -07003780 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003781 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003782 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003783 // Look for the toolbar icon specified in the activity meta-data
3784 Bundle metaData = packageManager.getActivityInfo(
3785 activityName, PackageManager.GET_META_DATA).metaData;
3786 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003787 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003788 if (iconResId != 0) {
3789 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003790 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003791 }
3792 }
3793 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003794 // This can happen if the activity defines an invalid drawable
3795 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3796 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003797 } catch (Resources.NotFoundException nfe) {
3798 // This can happen if the activity defines an invalid drawable
3799 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3800 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003801 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003802 return null;
3803 }
3804
3805 // if successful in getting icon, return it; otherwise, set button to use default drawable
3806 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003807 int buttonId, ComponentName activityName, int fallbackDrawableId,
3808 String toolbarResourceName) {
3809 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003810 Resources r = getResources();
3811 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3812 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003813
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003814 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003815 // If we were unable to find the icon via the meta-data, use a generic one
3816 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003817 toolbarIcon = r.getDrawable(fallbackDrawableId);
3818 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003819 if (button != null) {
3820 button.setCompoundDrawables(toolbarIcon, null, null, null);
3821 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003822 return null;
3823 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003824 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003825 if (button != null) {
3826 button.setCompoundDrawables(toolbarIcon, null, null, null);
3827 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003828 return toolbarIcon.getConstantState();
3829 }
3830 }
3831
3832 // if successful in getting icon, return it; otherwise, set button to use default drawable
3833 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003834 int buttonId, ComponentName activityName, int fallbackDrawableId,
3835 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003836 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003837 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003838
Michael Jurka19e0fc52011-07-22 18:00:21 -07003839 if (button != null) {
3840 // If we were unable to find the icon via the meta-data, use a
3841 // generic one
3842 if (toolbarIcon == null) {
3843 button.setImageResource(fallbackDrawableId);
3844 } else {
3845 button.setImageDrawable(toolbarIcon);
3846 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003847 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003848
3849 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3850
Michael Jurka0423dcf2010-10-05 14:56:18 -07003851 }
3852
Winson Chung1b884092012-06-08 17:13:02 -07003853 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003854 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003855 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003856 }
3857
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003858 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003859 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003860 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003861 }
3862
Winson Chungbb185bd2011-11-21 12:31:42 -08003863 private void invalidatePressedFocusedStates(View container, View button) {
3864 if (container instanceof HolographicLinearLayout) {
3865 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3866 layout.invalidatePressedFocusedStates();
3867 } else if (button instanceof HolographicImageView) {
3868 HolographicImageView view = (HolographicImageView) button;
3869 view.invalidatePressedFocusedStates();
3870 }
3871 }
3872
Cristina Stancu476493b2013-08-07 17:20:14 +01003873 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003874 if (mQsb == null) {
3875 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3876 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003877 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003878 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003879 }
3880
3881 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003882 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003883 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003884 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003885 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003886
Winson Chung1cad91e2011-05-25 17:41:01 -07003887 final SearchManager searchManager =
3888 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3889 ComponentName activityName = searchManager.getGlobalSearchActivity();
3890 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003891 int coi = getCurrentOrientationIndexForGlobalIcons();
3892 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003893 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3894 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3895 if (sGlobalSearchIcon[coi] == null) {
3896 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3897 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3898 TOOLBAR_ICON_METADATA_NAME);
3899 }
3900
Winson Chungc51db6a2011-10-05 11:44:49 -07003901 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3902 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003903 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003904 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003905 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003906 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003907 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3908 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003909 if (searchButton != null) searchButton.setVisibility(View.GONE);
3910 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003911 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003912 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003913 }
3914 }
3915
Cristina Stancu476493b2013-08-07 17:20:14 +01003916 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003917 final View searchButtonContainer = findViewById(R.id.search_button_container);
3918 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003919 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003920 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003921 }
3922
Cristina Stancu476493b2013-08-07 17:20:14 +01003923 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003924 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003925 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003926
Winson Chungc51db6a2011-10-05 11:44:49 -07003927 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003928 final SearchManager searchManager =
3929 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3930 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3931
3932 ComponentName activityName = null;
3933 if (globalSearchActivity != null) {
3934 // Check if the global search activity handles voice search
3935 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3936 intent.setPackage(globalSearchActivity.getPackageName());
3937 activityName = intent.resolveActivity(getPackageManager());
3938 }
3939
3940 if (activityName == null) {
3941 // Fallback: check if an activity other than the global search activity
3942 // resolves this
3943 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3944 activityName = intent.resolveActivity(getPackageManager());
3945 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003946 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003947 int coi = getCurrentOrientationIndexForGlobalIcons();
3948 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003949 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3950 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3951 if (sVoiceSearchIcon[coi] == null) {
3952 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3953 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3954 TOOLBAR_ICON_METADATA_NAME);
3955 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003956 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003957 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003958 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003959 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003960 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003961 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003962 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003963 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003964 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003965 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003966 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003967 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003968
Cristina Stancu476493b2013-08-07 17:20:14 +01003969 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003970 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3971 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003972 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003973 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003974 }
3975
Winson Chung5841efa2013-09-30 18:06:44 -07003976 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003977 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3978 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003979 boolean visible = !forceDisableVoiceButtonProxy &&
3980 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003981 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003982 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003983 }
3984 }
Winson Chung5841efa2013-09-30 18:06:44 -07003985
3986 /**
3987 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3988 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3989 */
3990 public void disableVoiceButtonProxy(boolean disabled) {
3991 updateVoiceButtonProxyVisible(disabled);
3992 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003993 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003994 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003995 */
3996 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003997 if (!DISABLE_MARKET_BUTTON) {
3998 final View marketButton = findViewById(R.id.market_button);
3999 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
4000 // Find the app market activity by resolving an intent.
4001 // (If multiple app markets are installed, it will return the ResolverActivity.)
4002 ComponentName activityName = intent.resolveActivity(getPackageManager());
4003 if (activityName != null) {
4004 int coi = getCurrentOrientationIndexForGlobalIcons();
4005 mAppMarketIntent = intent;
4006 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
4007 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
4008 TOOLBAR_ICON_METADATA_NAME);
4009 marketButton.setVisibility(View.VISIBLE);
4010 } else {
4011 // We should hide and disable the view so that we don't try and restore the visibility
4012 // of it when we swap between drag & normal states from IconDropTarget subclasses.
4013 marketButton.setVisibility(View.GONE);
4014 marketButton.setEnabled(false);
4015 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004016 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004017 }
4018
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004019 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07004020 if (!DISABLE_MARKET_BUTTON) {
4021 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
4022 Resources r = getResources();
4023 Drawable marketIconDrawable = d.newDrawable(r);
4024 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4025 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
4026 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07004027
Winson Chungd64a6662013-09-30 11:06:59 -07004028 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
4029 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004030 }
4031
Michael Jurkad7c28052012-04-27 15:43:36 -07004032 @Override
4033 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004034 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004035 final List<CharSequence> text = event.getText();
4036 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004037 // Populate event with a fake title based on the current state.
4038 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004039 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004040 } else {
4041 text.add(getString(R.string.all_apps_home_button_label));
4042 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004043 return result;
4044 }
4045
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004046 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004047 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004048 */
4049 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4050 @Override
4051 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004052 closeSystemDialogs();
4053 }
4054 }
4055
4056 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004057 * Receives notifications whenever the appwidgets are reset.
4058 */
4059 private class AppWidgetResetObserver extends ContentObserver {
4060 public AppWidgetResetObserver() {
4061 super(new Handler());
4062 }
4063
4064 @Override
4065 public void onChange(boolean selfChange) {
4066 onAppWidgetReset();
4067 }
4068 }
4069
4070 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004071 * If the activity is currently paused, signal that we need to run the passed Runnable
4072 * in onResume.
4073 *
4074 * This needs to be called from incoming places where resources might have been loaded
4075 * while we are paused. That is becaues the Configuration might be wrong
4076 * when we're not running, and if it comes back to what it was when we
4077 * were paused, we are not restarted.
4078 *
4079 * Implementation of the method from LauncherModel.Callbacks.
4080 *
4081 * @return true if we are currently paused. The caller might be able to
4082 * skip some work in that case since we will come back again.
4083 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004084 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 if (mPaused) {
4086 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004087 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004088 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004089 }
4090 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004091 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004092 return true;
4093 } else {
4094 return false;
4095 }
4096 }
4097
Michael Jurkac402cd92013-05-20 15:49:32 +02004098 private boolean waitUntilResume(Runnable run) {
4099 return waitUntilResume(run, false);
4100 }
4101
Michael Jurka1e2f4652013-07-08 18:03:46 -07004102 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004103 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004104 }
4105
Michael Jurka7607c2f2013-04-03 14:33:19 -07004106 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004107 * If the activity is currently paused, signal that we need to re-run the loader
4108 * in onResume.
4109 *
4110 * This needs to be called from incoming places where resources might have been loaded
4111 * while we are paused. That is becaues the Configuration might be wrong
4112 * when we're not running, and if it comes back to what it was when we
4113 * were paused, we are not restarted.
4114 *
4115 * Implementation of the method from LauncherModel.Callbacks.
4116 *
4117 * @return true if we are currently paused. The caller might be able to
4118 * skip some work in that case since we will come back again.
4119 */
4120 public boolean setLoadOnResume() {
4121 if (mPaused) {
4122 Log.i(TAG, "setLoadOnResume");
4123 mOnResumeNeedsLoad = true;
4124 return true;
4125 } else {
4126 return false;
4127 }
4128 }
4129
4130 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004132 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004134 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004135 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004136 } else {
4137 return SCREEN_COUNT / 2;
4138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004140
Joe Onorato9c1289c2009-08-17 11:03:03 -04004141 /**
4142 * Refreshes the shortcuts shown on the workspace.
4143 *
4144 * Implementation of the method from LauncherModel.Callbacks.
4145 */
4146 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004147 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004148
Michael Jurka7607c2f2013-04-03 14:33:19 -07004149 // If we're starting binding all over again, clear any bind calls we'd postponed in
4150 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4151 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004152 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004153
Winson Chungd64d1762013-08-20 14:37:16 -07004154 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004155 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004156 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004157
Michael Jurka05bf6442011-11-30 20:28:53 -08004158 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004159 if (mHotseat != null) {
4160 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004161 }
4162 }
4163
Adam Cohendcd297f2013-06-18 13:13:40 -07004164 @Override
4165 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004166 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004167
Adam Cohen5084cba2013-09-03 12:01:16 -07004168 // If there are no screens, we need to have an empty screen
4169 if (orderedScreenIds.size() == 0) {
4170 mWorkspace.addExtraEmptyScreen();
4171 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004172
4173 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004174 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004175 if (hasCustomContentToLeft()) {
4176 mWorkspace.createCustomContentContainer();
4177 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004178 }
Winson Chung64359a52013-07-08 17:17:08 -07004179 }
4180
4181 @Override
4182 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004183 // Log to disk
4184 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4185 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4186 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004187 int count = orderedScreenIds.size();
4188 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004189 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004190 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004191 }
4192
Adam Cohen39a06042013-07-19 14:30:12 -07004193 private boolean shouldShowWeightWatcher() {
4194 String spKey = LauncherAppState.getSharedPreferencesKey();
4195 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004196 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004197
4198 return show;
4199 }
4200
4201 private void toggleShowWeightWatcher() {
4202 String spKey = LauncherAppState.getSharedPreferencesKey();
4203 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4204 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4205
4206 show = !show;
4207
4208 SharedPreferences.Editor editor = sp.edit();
4209 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4210 editor.commit();
4211
4212 if (mWeightWatcher != null) {
4213 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4214 }
4215 }
4216
Winson Chungd64d1762013-08-20 14:37:16 -07004217 public void bindAppsAdded(final ArrayList<Long> newScreens,
4218 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004219 final ArrayList<ItemInfo> addAnimated,
4220 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004221 Runnable r = new Runnable() {
4222 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004223 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004224 }
4225 };
4226 if (waitUntilResume(r)) {
4227 return;
4228 }
4229
Winson Chungd64d1762013-08-20 14:37:16 -07004230 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004231 if (newScreens != null) {
4232 bindAddScreens(newScreens);
4233 }
Winson Chungd64d1762013-08-20 14:37:16 -07004234
4235 // We add the items without animation on non-visible pages, and with
4236 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004237 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004238 bindItems(addNotAnimated, 0,
4239 addNotAnimated.size(), false);
4240 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004241 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004242 bindItems(addAnimated, 0,
4243 addAnimated.size(), true);
4244 }
Winson Chungc58497e2013-09-03 17:48:37 -07004245
Winson Chung87412982013-10-03 18:34:14 -07004246 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004247 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004248
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004249 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004250 addedApps != null && mAppsCustomizeContent != null) {
4251 mAppsCustomizeContent.addApps(addedApps);
4252 }
Winson Chungd64d1762013-08-20 14:37:16 -07004253 }
4254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004255 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004256 * Bind the items start-end from the list.
4257 *
4258 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004259 */
Winson Chung64359a52013-07-08 17:17:08 -07004260 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4261 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004262 Runnable r = new Runnable() {
4263 public void run() {
4264 bindItems(shortcuts, start, end, forceAnimateIcons);
4265 }
4266 };
4267 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004268 return;
4269 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004270
Winson Chungf0c6ae02012-03-21 16:10:31 -07004271 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004272 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4273 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004274 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004275 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004276 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004277 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004278 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004279
4280 // Short circuit if we are loading dock items for a configuration which has no dock
4281 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4282 mHotseat == null) {
4283 continue;
4284 }
4285
Joe Onorato9c1289c2009-08-17 11:03:03 -04004286 switch (item.itemType) {
4287 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4288 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004289 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004290 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004291
Winson Chung64359a52013-07-08 17:17:08 -07004292 /*
4293 * TODO: FIX collision case
4294 */
Winson Chungce376632013-07-11 16:12:41 -07004295 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4296 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4297 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004298 View v = cl.getChildAt(item.cellX, item.cellY);
4299 Object tag = v.getTag();
4300 String desc = "Collision while binding workspace item: " + item
4301 + ". Collides with " + tag;
4302 if (LauncherAppState.isDogfoodBuild()) {
4303 throw (new RuntimeException(desc));
4304 } else {
4305 Log.d(TAG, desc);
4306 }
Winson Chungce376632013-07-11 16:12:41 -07004307 }
Winson Chung64359a52013-07-08 17:17:08 -07004308 }
4309
Adam Cohendcd297f2013-06-18 13:13:40 -07004310 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4311 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004312 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004313 // Animate all the applications up now
4314 shortcut.setAlpha(0f);
4315 shortcut.setScaleX(0f);
4316 shortcut.setScaleY(0f);
4317 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004318 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004319 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004321 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004322 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004323 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004324 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004325 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4326 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004327 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004328 default:
4329 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004331 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004332
Winson Chung997a9232013-07-24 15:33:46 -07004333 if (animateIcons) {
4334 // Animate to the correct page
4335 if (newShortcutsScreenId > -1) {
4336 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004337 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004338 final Runnable startBounceAnimRunnable = new Runnable() {
4339 public void run() {
4340 anim.playTogether(bounceAnims);
4341 anim.start();
4342 }
4343 };
Winson Chung997a9232013-07-24 15:33:46 -07004344 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004345 // We post the animation slightly delayed to prevent slowdowns
4346 // when we are loading right after we return to launcher.
4347 mWorkspace.postDelayed(new Runnable() {
4348 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004349 if (mWorkspace != null) {
4350 mWorkspace.snapToPage(newScreenIndex);
4351 mWorkspace.postDelayed(startBounceAnimRunnable,
4352 NEW_APPS_ANIMATION_DELAY);
4353 }
Winson Chung94d67682013-09-25 16:29:40 -07004354 }
4355 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004356 } else {
4357 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004358 }
4359 }
Winson Chung64359a52013-07-08 17:17:08 -07004360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004361 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004362 }
4363
Joe Onorato9c1289c2009-08-17 11:03:03 -04004364 /**
4365 * Implementation of the method from LauncherModel.Callbacks.
4366 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004367 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004368 Runnable r = new Runnable() {
4369 public void run() {
4370 bindFolders(folders);
4371 }
4372 };
4373 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004374 return;
4375 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004376 sFolders.clear();
4377 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004379
4380 /**
4381 * Add the views for a widget to the workspace.
4382 *
4383 * Implementation of the method from LauncherModel.Callbacks.
4384 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004385 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004386 Runnable r = new Runnable() {
4387 public void run() {
4388 bindAppWidget(item);
4389 }
4390 };
4391 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004392 return;
4393 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004394
Daniel Sandler843e8602010-06-07 14:59:01 -04004395 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4396 if (DEBUG_WIDGETS) {
4397 Log.d(TAG, "bindAppWidget: " + item);
4398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004399 final Workspace workspace = mWorkspace;
4400
Sunny Goyal651077b2014-06-30 14:15:31 -07004401 final AppWidgetProviderInfo appWidgetInfo;
4402 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4403 final int appWidgetId = item.appWidgetId;
4404 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4405 if (DEBUG_WIDGETS) {
4406 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4407 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004408
Sunny Goyal651077b2014-06-30 14:15:31 -07004409 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4410 } else {
4411 appWidgetInfo = null;
4412 item.hostView = new LauncherAppWidgetHostView(this, false);
4413 item.hostView.updateAppWidget(null);
4414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004415
Joe Onorato9c1289c2009-08-17 11:03:03 -04004416 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004417 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004418
Adam Cohendcd297f2013-06-18 13:13:40 -07004419 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004420 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004421 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4422
Joe Onorato9c1289c2009-08-17 11:03:03 -04004423 workspace.requestLayout();
4424
Daniel Sandler843e8602010-06-07 14:59:01 -04004425 if (DEBUG_WIDGETS) {
4426 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4427 + (SystemClock.uptimeMillis()-start) + "ms");
4428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004429 }
4430
Adam Cohen1462de32012-07-24 22:34:36 -07004431 public void onPageBoundSynchronously(int page) {
4432 mSynchronouslyBoundPages.add(page);
4433 }
4434
Joe Onorato9c1289c2009-08-17 11:03:03 -04004435 /**
4436 * Callback saying that there aren't any more items to bind.
4437 *
4438 * Implementation of the method from LauncherModel.Callbacks.
4439 */
Adam Cohene25af792013-06-06 23:08:25 -07004440 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004441 Runnable r = new Runnable() {
4442 public void run() {
4443 finishBindingItems(upgradePath);
4444 }
4445 };
4446 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004447 return;
4448 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004449 if (mSavedState != null) {
4450 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004451 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004452 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004453 mSavedState = null;
4454 }
4455
Adam Cohen1462de32012-07-24 22:34:36 -07004456 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004457
Winson Chungc51db6a2011-10-05 11:44:49 -07004458 // Update the market app icon as necessary (the other icons will be managed in response to
4459 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004460 if (!DISABLE_MARKET_BUTTON) {
4461 updateAppMarketIcon();
4462 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004463
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004464 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004465 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004466
4467 // If we received the result of any pending adds while the loader was running (e.g. the
4468 // widget configuration forced an orientation change), process them now.
4469 if (sPendingAddItem != null) {
4470 final long screenId = completeAdd(sPendingAddItem);
4471
4472 // TODO: this moves the user to the page where the pending item was added. Ideally,
4473 // the screen would be guaranteed to exist after bind, and the page would be set through
4474 // the workspace restore process.
4475 mWorkspace.post(new Runnable() {
4476 @Override
4477 public void run() {
4478 mWorkspace.snapToScreenId(screenId);
4479 }
4480 });
4481 sPendingAddItem = null;
4482 }
4483
Adam Cohene25af792013-06-06 23:08:25 -07004484 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004485 mWorkspace.getUniqueComponents(true, null);
4486 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004487 }
Sunny Goyale755d462014-07-22 13:48:29 -07004488 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004489 }
4490
Adam Cohen3ed316a2014-07-23 18:21:20 -07004491 private void sendLoadingCompleteBroadcastIfNecessary() {
4492 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4493 String permission =
4494 getResources().getString(R.string.receive_first_load_broadcast_permission);
4495 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4496 sendBroadcast(intent, permission);
4497 SharedPreferences.Editor editor = mSharedPrefs.edit();
4498 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4499 editor.apply();
4500 }
4501 }
4502
Winson Chunga0b7e862013-09-05 16:03:15 -07004503 public boolean isAllAppsButtonRank(int rank) {
4504 if (mHotseat != null) {
4505 return mHotseat.isAllAppsButtonRank(rank);
4506 }
4507 return false;
4508 }
4509
Winson Chunga2413752012-04-03 14:22:34 -07004510 private boolean canRunNewAppsAnimation() {
4511 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4512 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4513 }
4514
Winson Chungc9168342013-06-26 14:54:55 -07004515 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4516 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4517 PropertyValuesHolder.ofFloat("alpha", 1f),
4518 PropertyValuesHolder.ofFloat("scaleX", 1f),
4519 PropertyValuesHolder.ofFloat("scaleY", 1f));
4520 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4521 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4522 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4523 return bounceAnim;
4524 }
4525
Adam Cohen27772732013-11-11 14:00:56 +00004526 public boolean useVerticalBarLayout() {
4527 return LauncherAppState.getInstance().getDynamicGrid().
4528 getDeviceProfile().isVerticalBarLayout();
4529 }
4530
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004531 protected Rect getSearchBarBounds() {
4532 return LauncherAppState.getInstance().getDynamicGrid().
4533 getDeviceProfile().getSearchBarBounds();
4534 }
4535
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004536 @Override
4537 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004538 boolean searchVisible = updateGlobalSearchIcon();
4539 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004540 if (mSearchDropTargetBar != null) {
4541 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4542 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004543 }
4544
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004545 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004546 * Add the icons for all apps.
4547 *
4548 * Implementation of the method from LauncherModel.Callbacks.
4549 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004550 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004551 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004552 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4553 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4554 getHotseat().addAllAppsFolder(mIconCache, apps,
4555 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4556 }
4557 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004558 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004559 if (mAppsCustomizeContent != null) {
4560 mAppsCustomizeContent.onPackagesUpdated(
4561 LauncherModel.getSortedWidgetsAndShortcuts(this));
4562 }
Winson Chungc58497e2013-09-03 17:48:37 -07004563 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004564 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004565 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004566 mAppsCustomizeContent.onPackagesUpdated(
4567 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004568 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004569 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004570 }
4571
4572 /**
4573 * A package was updated.
4574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004577 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004578 Runnable r = new Runnable() {
4579 public void run() {
4580 bindAppsUpdated(apps);
4581 }
4582 };
4583 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004584 return;
4585 }
4586
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004587 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004588 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004589 }
Winson Chungc58497e2013-09-03 17:48:37 -07004590
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004591 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004592 mAppsCustomizeContent != null) {
4593 mAppsCustomizeContent.updateApps(apps);
4594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004595 }
4596
4597 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004598 * Update the state of a package, typically related to install state.
4599 *
4600 * Implementation of the method from LauncherModel.Callbacks.
4601 */
Sunny Goyale755d462014-07-22 13:48:29 -07004602 @Override
4603 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004604 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004605 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004606 }
4607 }
4608
4609 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004610 * A package was uninstalled. We take both the super set of packageNames
4611 * in addition to specific applications to remove, the reason being that
4612 * this can be called when a package is updated as well. In that scenario,
4613 * we only remove specific components from the workspace, where as
4614 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004615 *
4616 * Implementation of the method from LauncherModel.Callbacks.
4617 */
Winson Chung83892cc2013-05-01 16:53:33 -07004618 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004619 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004620 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004621 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004622 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004623 }
Winson Chungd64d1762013-08-20 14:37:16 -07004624 };
4625 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004626 return;
4627 }
4628
Winson Chungdf95eb12013-10-16 14:57:07 -07004629 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004630 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004631 }
4632 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004633 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004634 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004635
Winson Chunga1820962011-10-03 16:31:06 -07004636 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004637 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004638
Winson Chungdf95eb12013-10-16 14:57:07 -07004639 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004640 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004641 mAppsCustomizeContent != null) {
4642 mAppsCustomizeContent.removeApps(appInfos);
4643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004644 }
Joe Onoratobe386092009-11-17 17:32:16 -08004645
4646 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004647 * A number of packages were updated.
4648 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004649 private ArrayList<Object> mWidgetsAndShortcuts;
4650 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004651 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004652 bindPackagesUpdated(mWidgetsAndShortcuts);
4653 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004654 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004655 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004656 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4657 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4658 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004659 return;
4660 }
4661
Winson Chung64359a52013-07-08 17:17:08 -07004662 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004663 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004664 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004665 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004666 }
4667
Winson Chung400438b2011-01-16 17:53:48 -08004668 private int mapConfigurationOriActivityInfoOri(int configOri) {
4669 final Display d = getWindowManager().getDefaultDisplay();
4670 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4671 switch (d.getRotation()) {
4672 case Surface.ROTATION_0:
4673 case Surface.ROTATION_180:
4674 // We are currently in the same basic orientation as the natural orientation
4675 naturalOri = configOri;
4676 break;
4677 case Surface.ROTATION_90:
4678 case Surface.ROTATION_270:
4679 // We are currently in the other basic orientation to the natural orientation
4680 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4681 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4682 break;
4683 }
4684
4685 int[] oriMap = {
4686 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4687 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4688 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4689 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4690 };
4691 // Since the map starts at portrait, we need to offset if this device's natural orientation
4692 // is landscape.
4693 int indexOffset = 0;
4694 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4695 indexOffset = 1;
4696 }
4697 return oriMap[(d.getRotation() + indexOffset) % 4];
4698 }
Adam Cohen446e9402011-09-15 18:21:21 -07004699
Winson Chung4b919f82012-05-01 10:44:08 -07004700 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004701 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004702 getResources().getBoolean(R.bool.allow_rotation);
4703 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004704 }
Winson Chung4b919f82012-05-01 10:44:08 -07004705 public void lockScreenOrientation() {
4706 if (isRotationEnabled()) {
4707 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4708 .getConfiguration().orientation));
4709 }
4710 }
4711 public void unlockScreenOrientation(boolean immediate) {
4712 if (isRotationEnabled()) {
4713 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004714 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004715 } else {
4716 mHandler.postDelayed(new Runnable() {
4717 public void run() {
4718 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4719 }
4720 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004721 }
Winson Chung4b919f82012-05-01 10:44:08 -07004722 }
Winson Chung400438b2011-01-16 17:53:48 -08004723 }
4724
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004725 /**
4726 * Called when the SearchBar hint should be changed.
4727 *
4728 * @param hint the hint to be displayed in the search bar.
4729 */
4730 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004731 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004732 }
4733
Winson Chunge43a1e72014-01-15 10:33:02 -08004734 protected boolean isLauncherPreinstalled() {
4735 PackageManager pm = getPackageManager();
4736 try {
4737 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4738 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4739 return true;
4740 } else {
4741 return false;
4742 }
4743 } catch (NameNotFoundException e) {
4744 e.printStackTrace();
4745 return false;
4746 }
4747 }
4748
Adam Cohenea90f832014-05-21 15:03:34 -07004749 /**
4750 * This method indicates whether or not we should suggest default wallpaper dimensions
4751 * when our wallpaper cropper was not yet used to set a wallpaper.
4752 */
4753 protected boolean overrideWallpaperDimensions() {
4754 return true;
4755 }
4756
Adam Cohen5eed5d82014-05-19 13:12:13 -07004757 protected boolean shouldClingFocusHotseatApp() {
4758 return false;
4759 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004760 protected String getFirstRunClingSearchBarHint() {
4761 return "";
4762 }
4763 protected String getFirstRunCustomContentHint() {
4764 return "";
4765 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004766 protected int getFirstRunFocusedHotseatAppDrawableId() {
4767 return -1;
4768 }
4769 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4770 return null;
4771 }
4772 protected int getFirstRunFocusedHotseatAppRank() {
4773 return -1;
4774 }
4775 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4776 return "";
4777 }
4778 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4779 return "";
4780 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004781
Winson Chungaf40f202013-09-18 18:26:31 -07004782 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004783 mLauncherClings.dismissFirstRunCling(v);
4784 }
4785 public void dismissMigrationClingCopyApps(View v) {
4786 mLauncherClings.dismissMigrationClingCopyApps(v);
4787 }
4788 public void dismissMigrationClingUseDefault(View v) {
4789 mLauncherClings.dismissMigrationClingUseDefault(v);
4790 }
4791 public void dismissMigrationWorkspaceCling(View v) {
4792 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004793 }
Winson Chung82f55532011-08-09 14:14:23 -07004794 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004795 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004796 }
4797 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004798 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004799 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004800 public void markFolderClingDismissedIfNecessary() {
4801 mLauncherClings.markFolderClingDismissedIfNecessary();
4802 }
Adam Cohen432609a2014-03-13 17:03:22 -07004803
4804 /**
4805 * To be overridden by subclasses to indicate that there is an activity to launch
4806 * before showing the standard launcher experience.
4807 */
4808 protected boolean hasFirstRunActivity() {
4809 return false;
4810 }
4811
4812 /**
4813 * To be overridden by subclasses to launch any first run activity
4814 */
4815 protected Intent getFirstRunActivity() {
4816 return null;
4817 }
4818
Winson Chunga6945242014-01-08 14:04:34 -08004819 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004820 return !ActivityManager.isRunningInTestHarness() &&
4821 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004822 }
4823
Adam Cohen4a9423d2014-03-28 14:22:31 -07004824 protected boolean hasRunFirstRunActivity() {
4825 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4826 }
4827
Adam Cohen432609a2014-03-13 17:03:22 -07004828 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004829 if (shouldRunFirstRunActivity() &&
4830 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004831 Intent firstRunIntent = getFirstRunActivity();
4832 if (firstRunIntent != null) {
4833 startActivity(firstRunIntent);
4834 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004835 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004836 }
4837 }
Adam Cohen432609a2014-03-13 17:03:22 -07004838 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004839 }
4840
4841 private void markFirstRunActivityShown() {
4842 SharedPreferences.Editor editor = mSharedPrefs.edit();
4843 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4844 editor.apply();
4845 }
4846
Adam Cohen432609a2014-03-13 17:03:22 -07004847 /**
4848 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4849 * screen that must be displayed and dismissed.
4850 */
4851 protected boolean hasDismissableIntroScreen() {
4852 return false;
4853 }
4854
4855 /**
4856 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4857 */
4858 protected View getIntroScreen() {
4859 return null;
4860 }
4861
4862 /**
4863 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4864 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4865 */
4866 private boolean shouldShowIntroScreen() {
4867 return hasDismissableIntroScreen() &&
4868 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4869 }
4870
4871 protected void showIntroScreen() {
4872 View introScreen = getIntroScreen();
4873 changeWallpaperVisiblity(false);
4874 if (introScreen != null) {
4875 mDragLayer.showOverlayView(introScreen);
4876 }
4877 }
4878
4879 public void dismissIntroScreen() {
4880 markIntroScreenDismissed();
4881 if (showFirstRunActivity()) {
4882 // We delay hiding the intro view until the first run activity is showing. This
4883 // avoids a blip.
4884 mWorkspace.postDelayed(new Runnable() {
4885 @Override
4886 public void run() {
4887 mDragLayer.dismissOverlayView();
4888 }
4889 }, ACTIVITY_START_DELAY);
4890 } else {
4891 mDragLayer.dismissOverlayView();
4892 }
4893 changeWallpaperVisiblity(true);
4894 }
4895
4896 private void markIntroScreenDismissed() {
4897 SharedPreferences.Editor editor = mSharedPrefs.edit();
4898 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4899 editor.apply();
4900 }
4901
Winson Chunga6945242014-01-08 14:04:34 -08004902 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004903 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4904 if (mHotseat != null) mHotseat.setAlpha(1f);
4905 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4906 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004907 }
4908
4909 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004910 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4911 if (mHotseat != null) mHotseat.setAlpha(0f);
4912 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4913 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004914 }
4915
Mathew Inwood72fbec12013-11-19 15:45:07 +00004916 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004917 // Called from search suggestion, not supported in other profiles.
4918 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4919 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4920 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4921 myUser);
4922 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004923 return null;
4924 }
Kenny Guyed131872014-04-30 03:02:21 +01004925 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004926 }
4927
4928 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4929 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004930 // Called from search suggestion, not supported in other profiles.
4931 return createShortcutDragInfo(shortcutIntent, caption, icon,
4932 UserHandleCompat.myUserHandle());
4933 }
4934
4935 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4936 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004937 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004938 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004939 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004940 }
4941
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004942 protected void moveWorkspaceToDefaultScreen() {
4943 mWorkspace.moveToDefaultScreen(false);
4944 }
4945
Mathew Inwood72fbec12013-11-19 15:45:07 +00004946 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4947 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004948 mWorkspace.onExternalDragStartedWithItem(dragView);
4949 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004950 }
4951
Anjali Koppalf5d29132014-02-28 13:33:27 -08004952 @Override
4953 public void onPageSwitch(View newPage, int newPageIndex) {
4954 }
4955
Winson Chung80baf5a2010-08-09 16:03:15 -07004956 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004957 * Prints out out state for debugging.
4958 */
4959 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004960 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004961 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004962 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4963 Log.d(TAG, "mRestoring=" + mRestoring);
4964 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4965 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004966 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004967 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004968
Winson Chung785d2eb2011-04-14 16:08:02 -07004969 if (mAppsCustomizeContent != null) {
4970 mAppsCustomizeContent.dumpState();
4971 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004972 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004973 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004974
4975 @Override
4976 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4977 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004978 synchronized (sDumpLogs) {
4979 writer.println(" ");
4980 writer.println("Debug logs: ");
4981 for (int i = 0; i < sDumpLogs.size(); i++) {
4982 writer.println(" " + sDumpLogs.get(i));
4983 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004984 }
4985 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004986
4987 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004988 if (DEBUG_DUMP_LOG) {
4989 synchronized (sDumpLogs) {
4990 Log.d(TAG, "");
4991 Log.d(TAG, "*********************");
4992 Log.d(TAG, "Launcher debug logs: ");
4993 for (int i = 0; i < sDumpLogs.size(); i++) {
4994 Log.d(TAG, " " + sDumpLogs.get(i));
4995 }
4996 Log.d(TAG, "*********************");
4997 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004998 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004999 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005000 }
5001
5002 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005003 addDumpLog(tag, log, null, debugLog);
5004 }
5005
5006 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005007 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005008 if (e != null) {
5009 Log.d(tag, log, e);
5010 } else {
5011 Log.d(tag, log);
5012 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005013 }
Winson Chungede41292013-09-19 16:27:36 -07005014 if (DEBUG_DUMP_LOG) {
5015 sDateStamp.setTime(System.currentTimeMillis());
5016 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005017 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5018 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005019 }
Winson Chungede41292013-09-19 16:27:36 -07005020 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005021 }
5022
Winson Chungede41292013-09-19 16:27:36 -07005023 public void dumpLogsToLocalData() {
5024 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005025 new AsyncTask<Void, Void, Void>() {
5026 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005027 boolean success = false;
5028 sDateStamp.setTime(sRunStart);
5029 String FILENAME = sDateStamp.getMonth() + "-"
5030 + sDateStamp.getDay() + "_"
5031 + sDateStamp.getHours() + "-"
5032 + sDateStamp.getMinutes() + "_"
5033 + sDateStamp.getSeconds() + ".txt";
5034
5035 FileOutputStream fos = null;
5036 File outFile = null;
5037 try {
5038 outFile = new File(getFilesDir(), FILENAME);
5039 outFile.createNewFile();
5040 fos = new FileOutputStream(outFile);
5041 } catch (Exception e) {
5042 e.printStackTrace();
5043 }
5044 if (fos != null) {
5045 PrintWriter writer = new PrintWriter(fos);
5046
5047 writer.println(" ");
5048 writer.println("Debug logs: ");
5049 synchronized (sDumpLogs) {
5050 for (int i = 0; i < sDumpLogs.size(); i++) {
5051 writer.println(" " + sDumpLogs.get(i));
5052 }
5053 }
5054 writer.close();
5055 }
5056 try {
5057 if (fos != null) {
5058 fos.close();
5059 success = true;
5060 }
5061 } catch (IOException e) {
5062 e.printStackTrace();
5063 }
Michael Jurka43467462013-11-14 12:03:58 +01005064 return null;
Winson Chungede41292013-09-19 16:27:36 -07005065 }
Michael Jurka43467462013-11-14 12:03:58 +01005066 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005067 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005069}
Michael Jurka2763be32011-02-24 11:19:57 -08005070
Michael Jurkaabded662011-03-04 12:06:57 -08005071interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005072 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005073 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005074 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005075 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005076 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005077}
Dan Sandlerd5024042014-01-09 15:01:33 -05005078
5079interface DebugIntents {
5080 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5081 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005082}