blob: 9bf536ec41706de5c236aa42dcb9823963b8b2cb [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Sunny Goyalf8763012014-09-23 16:22:10 -070076import android.view.ContextThemeWrapper;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Display;
78import android.view.Gravity;
79import android.view.HapticFeedbackConstants;
80import android.view.KeyEvent;
81import android.view.LayoutInflater;
82import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.MotionEvent;
84import android.view.Surface;
85import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070086import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080087import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070088import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
90import android.view.ViewTreeObserver;
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 Cohen9bfdb762014-07-21 17:44:06 -070094import android.view.animation.AccelerateInterpolator;
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;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700105import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100106import com.android.launcher3.compat.LauncherActivityInfoCompat;
107import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700108import com.android.launcher3.compat.PackageInstallerCompat;
109import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100110import com.android.launcher3.compat.UserHandleCompat;
111import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700112
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.DataInputStream;
114import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700120import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000121import java.lang.reflect.Field;
122import java.lang.reflect.InvocationTargetException;
123import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700126import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000130import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
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;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
167 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
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;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700246 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700247
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800248 private final BroadcastReceiver mCloseSystemDialogsReceiver
249 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800250 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private LayoutInflater mInflater;
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700255 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800256 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800257 private DragLayer mDragLayer;
258 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700259 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Sunny Goyalffe83f12014-08-14 17:39:34 -0700261 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 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];
Winson Chungd64a6662013-09-30 11:06:59 -0700332
Craig Mautner360310b2012-10-26 15:13:08 -0700333 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700334
Adam Cohen1462de32012-07-24 22:34:36 -0700335 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700336 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700337
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700339 static Date sDateStamp = new Date();
340 static DateFormat sDateFormat =
341 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
342 static long sRunStart = System.currentTimeMillis();
343 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700344
Winson Chung46353de2012-02-16 14:05:10 -0800345 // We only want to get the SharedPreferences once since it does an FS stat each time we get
346 // it from the context.
347 private SharedPreferences mSharedPrefs;
348
Adam Cohene25af792013-06-06 23:08:25 -0700349 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
350
Winson Chungf0c6ae02012-03-21 16:10:31 -0700351 // Holds the page that we need to animate to, and the icon views that we need to animate up
352 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700353 private ImageView mFolderIconImageView;
354 private Bitmap mFolderIconBitmap;
355 private Canvas mFolderIconCanvas;
356 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700357
Michael Jurkaddd62e92011-02-16 17:49:14 -0800358 private BubbleTextView mWaitingForResume;
359
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 private Runnable mBuildLayersRunnable = new Runnable() {
361 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700362 if (mWorkspace != null) {
363 mWorkspace.buildPageHardwareLayers();
364 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700365 }
366 };
367
Adam Cohendb364c32014-05-20 14:23:40 -0700368 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369
Michael Jurka7267fa52013-09-26 15:29:57 -0700370 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800371
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 private static class PendingAddArguments {
373 int requestCode;
374 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700375 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700376 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377 int cellX;
378 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700379 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 }
381
Daniel Sandlerff02d492013-08-05 02:12:05 -0400382 private Stats mStats;
383
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700384 FocusIndicatorView mFocusHandler;
385
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800386 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800387 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700388 }
389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 @Override
391 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700392 if (DEBUG_STRICT_MODE) {
393 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
394 .detectDiskReads()
395 .detectDiskWrites()
396 .detectNetwork() // or .detectAll() for all detectable problems
397 .penaltyLog()
398 .build());
399 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
400 .detectLeakedSqlLiteObjects()
401 .detectLeakedClosableObjects()
402 .penaltyLog()
403 .penaltyDeath()
404 .build());
405 }
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700411 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700412 // Determine the dynamic grid properties
413 Point smallestSize = new Point();
414 Point largestSize = new Point();
415 Point realSize = new Point();
416 Display display = getWindowManager().getDefaultDisplay();
417 display.getCurrentSizeRange(smallestSize, largestSize);
418 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700419 DisplayMetrics dm = new DisplayMetrics();
420 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Winson Chung5f8afe62013-08-12 16:19:28 -0700422 // Lazy-initialize the dynamic grid
423 DeviceProfile grid = app.initDynamicGrid(this,
424 Math.min(smallestSize.x, smallestSize.y),
425 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700426 realSize.x, realSize.y,
427 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700428
429 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400430 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700431 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700432 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800433 mModel = app.setLauncher(this);
434 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700435 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400436 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Daniel Sandlerff02d492013-08-05 02:12:05 -0400439 mStats = new Stats(this);
440
Sunny Goyalffe83f12014-08-14 17:39:34 -0700441 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
444 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700446 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
447 // this also ensures that any synchronous binding below doesn't re-trigger another
448 // LauncherModel load.
449 mPaused = false;
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200452 android.os.Debug.startMethodTracing(
453 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100460 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800462 registerContentObservers();
463
Joe Onorato7c312c12009-08-13 21:36:53 -0700464 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 mSavedState = savedInstanceState;
467 restoreState(mSavedState);
468
469 if (PROFILE_STARTUP) {
470 android.os.Debug.stopMethodTracing();
471 }
472
473 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700474 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 // If the user leaves launcher, then we should just load items asynchronously when
476 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500477 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 } else {
479 // We only load the page synchronously if the user rotates (or triggers a
480 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800481 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484
485 // For handling default keys
486 mDefaultKeySsb = new SpannableStringBuilder();
487 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800488
489 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
490 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800491
Adam Cohenf9426d52012-06-04 17:26:21 -0700492 updateGlobalIcons();
493
494 // On large interfaces, we want the screen to auto-rotate based on the current orientation
495 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700496
Adam Cohen432609a2014-03-13 17:03:22 -0700497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
Sunny Goyal88d60f12014-09-08 03:47:29 -0700501 showFirstRunClings();
Winson Chunga6945242014-01-08 14:04:34 -0800502 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700503 }
504
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700505 @Override
506 public void onLauncherProviderChange() { }
507
Winson Chung98ca0f72013-07-29 12:58:51 -0700508 /** To be overriden by subclasses to hint to Launcher that we have custom content */
509 protected boolean hasCustomContentToLeft() {
510 return false;
511 }
512
Dave Hawkeya8881582013-09-17 15:55:33 -0600513 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500514 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600515 * {@link #addToCustomContentPage}. This will only be invoked if
516 * {@link #hasCustomContentToLeft()} is {@code true}.
517 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500518 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600519 }
520
521 /**
522 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
523 * ensure the custom content page is added or removed if necessary.
524 */
525 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600526 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600527 // Not bound yet, wait for bindScreens to be called.
528 return;
529 }
530
531 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
532 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500533 mWorkspace.createCustomContentContainer();
534 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
536 mWorkspace.removeCustomContentPage();
537 }
538 }
539
Adam Cohenf9426d52012-06-04 17:26:21 -0700540 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700541 boolean searchVisible = false;
542 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800543 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700544 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700545 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700546 searchVisible = updateGlobalSearchIcon();
547 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700548 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700549 if (sGlobalSearchIcon[coi] != null) {
550 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700551 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700552 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 if (sVoiceSearchIcon[coi] != null) {
554 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700555 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700556 }
Winson Chungadf0c182012-08-23 14:59:07 -0700557 if (mSearchDropTargetBar != null) {
558 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
Romain Guycbb89e42009-06-08 15:52:54 -0700561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 if (sLocaleConfiguration == null) {
564 new AsyncTask<Void, Void, LocaleConfiguration>() {
565 @Override
566 protected LocaleConfiguration doInBackground(Void... unused) {
567 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
568 readConfiguration(Launcher.this, localeConfiguration);
569 return localeConfiguration;
570 }
571
572 @Override
573 protected void onPostExecute(LocaleConfiguration result) {
574 sLocaleConfiguration = result;
575 checkForLocaleChange(); // recursive, but now with a locale configuration
576 }
577 }.execute();
578 return;
579 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 final Configuration configuration = getResources().getConfiguration();
582
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final String locale = configuration.locale.toString();
585
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 final int mcc = configuration.mcc;
588
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final int mnc = configuration.mnc;
591
Romain Guy84f296c2009-11-04 15:00:44 -0800592 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593
Romain Guy84f296c2009-11-04 15:00:44 -0800594 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 sLocaleConfiguration.locale = locale;
596 sLocaleConfiguration.mcc = mcc;
597 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700598
Joe Onorato0589f0f2010-02-08 13:44:00 -0800599 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700600
601 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100602 new AsyncTask<Void, Void, Void>() {
603 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100605 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 }
Michael Jurka43467462013-11-14 12:03:58 +0100607 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700608 }
609 }
610
611 private static class LocaleConfiguration {
612 public String locale;
613 public int mcc = -1;
614 public int mnc = -1;
615 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700616
Romain Guy98d01652009-06-30 16:21:04 -0700617 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
618 DataInputStream in = null;
619 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100620 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFS));
Romain Guy98d01652009-06-30 16:21:04 -0700621 configuration.locale = in.readUTF();
622 configuration.mcc = in.readInt();
623 configuration.mnc = in.readInt();
624 } catch (FileNotFoundException e) {
625 // Ignore
626 } catch (IOException e) {
627 // Ignore
628 } finally {
629 if (in != null) {
630 try {
631 in.close();
632 } catch (IOException e) {
633 // Ignore
634 }
635 }
636 }
637 }
638
639 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
640 DataOutputStream out = null;
641 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100642 out = new DataOutputStream(context.openFileOutput(
643 LauncherFiles.LAUNCHER_PREFS, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700644 out.writeUTF(configuration.locale);
645 out.writeInt(configuration.mcc);
646 out.writeInt(configuration.mnc);
647 out.flush();
648 } catch (FileNotFoundException e) {
649 // Ignore
650 } catch (IOException e) {
651 //noinspection ResultOfMethodCallIgnored
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100652 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFS).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700653 } finally {
654 if (out != null) {
655 try {
656 out.close();
657 } catch (IOException e) {
658 // Ignore
659 }
660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
662 }
663
Anton Hanssona2f665f2013-09-26 12:18:32 +0100664 public Stats getStats() {
665 return mStats;
666 }
667
Bjorn Bringertc459e522013-06-07 19:36:01 +0100668 public LayoutInflater getInflater() {
669 return mInflater;
670 }
671
Winson Chung36a62fe2012-05-06 18:04:42 -0700672 boolean isDraggingEnabled() {
673 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
674 // that is subsequently removed from the workspace in startBinding().
675 return !mModel.isLoadingWorkspace();
676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 static int getScreen() {
679 synchronized (sLock) {
680 return sScreen;
681 }
682 }
683
684 static void setScreen(int screen) {
685 synchronized (sLock) {
686 sScreen = screen;
687 }
688 }
689
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000690 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100691 if (Build.VERSION.SDK_INT >= 17) {
692 return View.generateViewId();
693 } else {
694 // View.generateViewId() is not available. The following fallback logic is a copy
695 // of its implementation.
696 for (;;) {
697 final int result = sNextGeneratedId.get();
698 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
699 int newValue = result + 1;
700 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
701 if (sNextGeneratedId.compareAndSet(result, newValue)) {
702 return result;
703 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000704 }
705 }
706 }
707
708 public int getViewIdForItem(ItemInfo info) {
709 // This cast is safe given the > 2B range for int.
710 int itemId = (int) info.id;
711 if (mItemIdToViewId.containsKey(itemId)) {
712 return mItemIdToViewId.get(itemId);
713 }
714 int viewId = generateViewId();
715 mItemIdToViewId.put(itemId, viewId);
716 return viewId;
717 }
718
719 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700720 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
721 * a configuration step, this allows the proper animations to run after other transitions.
722 */
Adam Cohendb364c32014-05-20 14:23:40 -0700723 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700724 long screenId = args.screenId;
725 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
726 // When the screen id represents an actual screen (as opposed to a rank) we make sure
727 // that the drop page actually exists.
728 screenId = ensurePendingDropLayoutExists(args.screenId);
729 }
Adam Cohendb364c32014-05-20 14:23:40 -0700730
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700733 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700735 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700737 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700739 case REQUEST_RECONFIGURE_APPWIDGET:
740 completeRestoreAppWidget(args.appWidgetId);
741 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 }
Michael Jurka27614d22012-04-02 06:40:22 -0700743 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
744 // if you turned the screen off and then back while in All Apps, Launcher would not
745 // return to the workspace. Clearing mAddInfo.container here fixes this issue
746 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700747 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 protected void onActivityResult(
752 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700753 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700754 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700755 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800756 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700757
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 Runnable exitSpringLoaded = new Runnable() {
759 @Override
760 public void run() {
761 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
762 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
763 }
764 };
765
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
769 if (resultCode == RESULT_CANCELED) {
770 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700771 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
775 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 }
777 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200778 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
779 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
780 mWorkspace.exitOverviewMode(false);
781 }
782 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784
Adam Cohened66b2b2012-01-23 17:28:51 -0800785 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
786 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700787
Adam Cohendb364c32014-05-20 14:23:40 -0700788 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 // We have special handling for widgets
790 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800791 final int appWidgetId;
792 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
793 : -1;
794 if (widgetId < 0) {
795 appWidgetId = pendingAddWidgetId;
796 } else {
797 appWidgetId = widgetId;
798 }
799
Adam Cohenad4e15c2013-10-17 16:21:35 -0700800 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800801 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700802 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
803 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 result = RESULT_CANCELED;
805 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700806 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700807 @Override
808 public void run() {
809 exitSpringLoadedDragModeDelayed(false, 0, null);
810 }
811 };
Adam Cohendb364c32014-05-20 14:23:40 -0700812 if (workspaceLocked) {
813 // No need to remove the empty screen if we're mid-binding, as the
814 // the bind will not add the empty screen.
815 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
816 } else {
817 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
818 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
819 }
Winson Chung5aaab772012-04-27 15:24:02 -0700820 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700821 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700822 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
823 // When the screen id represents an actual screen (as opposed to a rank)
824 // we make sure that the drop page actually exists.
825 mPendingAddInfo.screenId =
826 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
827 }
Adam Cohendb364c32014-05-20 14:23:40 -0700828 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
829
830 dropLayout.setDropPending(true);
831 final Runnable onComplete = new Runnable() {
832 @Override
833 public void run() {
834 completeTwoStageWidgetDrop(resultCode, appWidgetId);
835 dropLayout.setDropPending(false);
836 }
837 };
838 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
840 } else {
841 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
842 mPendingAddInfo);
843 sPendingAddItem = args;
844 }
Winson Chung5aaab772012-04-27 15:24:02 -0700845 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 return;
847 }
848
Sunny Goyalff572272014-07-23 13:58:07 -0700849 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
850 if (resultCode == RESULT_OK) {
851 // Update the widget view.
852 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
853 pendingAddWidgetId, mPendingAddInfo);
854 if (workspaceLocked) {
855 sPendingAddItem = args;
856 } else {
857 completeAdd(args);
858 }
859 }
860 // Leave the widget in the pending state if the user canceled the configure.
861 return;
862 }
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 // The pattern used here is that a user PICKs a specific application,
865 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
868 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700869 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700870 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
871 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800872 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700873 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800874 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700875 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700876 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
877 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 }
Adam Cohen00074722013-10-11 17:46:09 -0700879 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700880 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700881 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 }
885
Adam Cohendb364c32014-05-20 14:23:40 -0700886 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
887 appWidgetId, ItemInfo info) {
888 PendingAddArguments args = new PendingAddArguments();
889 args.requestCode = requestCode;
890 args.intent = data;
891 args.container = info.container;
892 args.screenId = info.screenId;
893 args.cellX = info.cellX;
894 args.cellY = info.cellY;
895 args.appWidgetId = appWidgetId;
896 return args;
897 }
898
899 /**
900 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
901 *
902 * @param screenId the screen id to check
903 * @return the new screen, or screenId if it exists
904 */
905 private long ensurePendingDropLayoutExists(long screenId) {
906 CellLayout dropLayout =
907 (CellLayout) mWorkspace.getScreenWithId(screenId);
908 if (dropLayout == null) {
909 // it's possible that the add screen was removed because it was
910 // empty and a re-bind occurred
911 mWorkspace.addExtraEmptyScreen();
912 return mWorkspace.commitExtraEmptyScreen();
913 } else {
914 return screenId;
915 }
916 }
917
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700919 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 Runnable onCompleteRunnable = null;
922 int animationType = 0;
923
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 if (resultCode == RESULT_OK) {
926 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
927 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 mPendingAddWidgetInfo);
929 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 onCompleteRunnable = new Runnable() {
931 @Override
932 public void run() {
933 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
936 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 }
938 };
939 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 if (mDragLayer.getAnimatedView() != null) {
944 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
945 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
946 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700947 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 // The animated view may be null in the case of a rotation during widget configuration
949 onCompleteRunnable.run();
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 protected void onStop() {
955 super.onStop();
956 FirstFrameAnimatorHelper.setIsVisible(false);
957 }
958
959 @Override
960 protected void onStart() {
961 super.onStart();
962 FirstFrameAnimatorHelper.setIsVisible(true);
963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700973
Winson Chung4a2afa32012-07-19 14:53:05 -0700974 // Restore the previous launcher state
975 if (mOnResumeState == State.WORKSPACE) {
976 showWorkspace(false);
977 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800978 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 }
980 mOnResumeState = State.NONE;
981
Craig Mautner360310b2012-10-26 15:13:08 -0700982 // Background was set to gradient in onPause(), restore to black if in all apps.
983 setWorkspaceBackground(mState == State.WORKSPACE);
984
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800985 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700986 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700987 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500988 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400989 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700990 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700992 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200993 // We might have postponed some bind calls until onResume (see waitUntilResume) --
994 // execute them here
995 long startTimeCallbacks = 0;
996 if (DEBUG_RESUME_TIME) {
997 startTimeCallbacks = System.currentTimeMillis();
998 }
999
1000 if (mAppsCustomizeContent != null) {
1001 mAppsCustomizeContent.setBulkBind(true);
1002 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001003 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1004 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001005 }
1006 if (mAppsCustomizeContent != null) {
1007 mAppsCustomizeContent.setBulkBind(false);
1008 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001009 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001010 if (DEBUG_RESUME_TIME) {
1011 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1012 (System.currentTimeMillis() - startTimeCallbacks));
1013 }
Michael Jurka447bf842013-05-15 14:52:15 +02001014 }
Michael Jurka54554252013-08-01 12:52:23 +02001015 if (mOnResumeCallbacks.size() > 0) {
1016 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1017 mOnResumeCallbacks.get(i).run();
1018 }
1019 mOnResumeCallbacks.clear();
1020 }
Winson Chunge4e50662012-01-23 14:45:13 -08001021
1022 // Reset the pressed state of icons that were locked in the press state while activities
1023 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001024 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001025 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001026 mWaitingForResume.setStayPressed(false);
1027 }
Winson Chung82963d52013-10-09 11:20:57 -07001028
Adam Cohen06dff352012-06-01 17:17:08 -07001029 // It is possible that widgets can receive updates while launcher is not in the foreground.
1030 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1031 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1032 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001033 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001034
Winson Chung780fe592013-09-26 14:48:44 -07001035 // Process any items that were added while Launcher was away.
1036 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1037
Winson Chung5841efa2013-09-30 18:06:44 -07001038 // Update the voice search button proxy
1039 updateVoiceButtonProxyVisible(false);
1040
Adam Cohenf9426d52012-06-04 17:26:21 -07001041 // Again, as with the above scenario, it's possible that one or more of the global icons
1042 // were updated in the wrong orientation.
1043 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001044 if (DEBUG_RESUME_TIME) {
1045 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1046 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001047
1048 if (mWorkspace.getCustomContentCallbacks() != null) {
1049 // If we are resuming and the custom content is the current page, we call onShow().
1050 // It is also poassible that onShow will instead be called slightly after first layout
1051 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1052 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001053 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 }
1055 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001056 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001057 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001058
1059 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001060 }
1061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001064 // Ensure that items added to Launcher are queued until Launcher returns
1065 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001066 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001067
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001069 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001070 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001071 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001072
1073 // We call onHide() aggressively. The custom content callbacks should be able to
1074 // debounce excess onHide calls.
1075 if (mWorkspace.getCustomContentCallbacks() != null) {
1076 mWorkspace.getCustomContentCallbacks().onHide();
1077 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 QSBScroller mQsbScroller = new QSBScroller() {
1081 int scrollY = 0;
1082
1083 @Override
1084 public void setScrollY(int scroll) {
1085 scrollY = scroll;
1086
1087 if (mWorkspace.isOnOrMovingToCustomContent()) {
1088 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001089 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
1091 }
1092 };
1093
1094 public void resetQSBScroll() {
1095 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001096 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
1099 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1101 // by a onResume or by scrolling otherwise.
1102 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001103
1104 // Custom content is completely hidden
1105 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001106
1107 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1108 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001109
1110 // Indicates whether the user is allowed to scroll away from the custom content.
1111 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 }
1113
Jorim Jaggid017f882014-01-14 17:08:48 -08001114 protected boolean hasSettings() {
1115 return false;
1116 }
1117
Adam Cohenbffe7452013-07-22 18:21:45 -07001118 public interface QSBScroller {
1119 public void setScrollY(int scrollY);
1120 }
1121
Winson Chung98ca0f72013-07-29 12:58:51 -07001122 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001123 CustomContentCallbacks callbacks, String description) {
1124 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001125 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001126 }
1127
Adam Cohenedb40762013-07-18 16:45:45 -07001128 // The custom content needs to offset its content to account for the QSB
1129 public int getTopOffsetForCustomContent() {
1130 return mWorkspace.getPaddingTop();
1131 }
1132
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001133 @Override
1134 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001136 if (mModel.isCurrentCallbacks(this)) {
1137 mModel.stopLoader();
1138 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001139 if (mAppsCustomizeContent != null) {
1140 mAppsCustomizeContent.surrender();
1141 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001142 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001143 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001144
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001145 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001146 @Override
1147 public void onWindowFocusChanged(boolean hasFocus) {
1148 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001149 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001150 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 private boolean acceptFilter() {
1153 final InputMethodManager inputManager = (InputMethodManager)
1154 getSystemService(Context.INPUT_METHOD_SERVICE);
1155 return !inputManager.isFullscreenMode();
1156 }
1157
1158 @Override
1159 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001160 final int uniChar = event.getUnicodeChar();
1161 final boolean handled = super.onKeyDown(keyCode, event);
1162 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1163 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1165 keyCode, event);
1166 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001167 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001168 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 // showSearchDialog()
1170 // If there are multiple keystrokes before the search dialog takes focus,
1171 // onSearchRequested() will be called for every keystroke,
1172 // but it is idempotent, so it's fine.
1173 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
1176
Joe Onorato8a9625e2010-01-28 15:55:35 -08001177 // Eat the long press event so the keyboard doesn't come up.
1178 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1179 return true;
1180 }
1181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 return handled;
1183 }
1184
Karl Rosaen138a0412009-04-23 19:00:21 -07001185 private String getTypedText() {
1186 return mDefaultKeySsb.toString();
1187 }
1188
1189 private void clearTypedText() {
1190 mDefaultKeySsb.clear();
1191 mDefaultKeySsb.clearSpans();
1192 Selection.setSelection(mDefaultKeySsb, 0);
1193 }
1194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001196 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1197 * State
1198 */
1199 private static State intToState(int stateOrdinal) {
1200 State state = State.WORKSPACE;
1201 final State[] stateValues = State.values();
1202 for (int i = 0; i < stateValues.length; i++) {
1203 if (stateValues[i].ordinal() == stateOrdinal) {
1204 state = stateValues[i];
1205 break;
1206 }
1207 }
1208 return state;
1209 }
1210
1211 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 * Restores the previous state, if it exists.
1213 *
1214 * @param savedState The previous state.
1215 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001216 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 private void restoreState(Bundle savedState) {
1218 if (savedState == null) {
1219 return;
1220 }
1221
Michael Jurka883f55b2010-10-21 15:47:14 -07001222 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001223 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001224 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001225 }
1226
Adam Cohen21cd0022013-10-09 18:57:02 -07001227 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1228 PagedView.INVALID_RESTORE_PAGE);
1229 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001230 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 }
1232
Winson Chung3d503fb2011-07-13 17:25:49 -07001233 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001234 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235
Winson Chung3d503fb2011-07-13 17:25:49 -07001236 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1237 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001238 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001239 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1240 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001241 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1242 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1243 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001244 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001245 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 mRestoring = true;
1247 }
1248
1249 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1250 if (renameFolder) {
1251 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001252 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 mRestoring = true;
1254 }
Winson Chunga12a2502010-12-20 14:41:35 -08001255
Winson Chung785d2eb2011-04-14 16:08:02 -07001256 // Restore the AppsCustomize tab
1257 if (mAppsCustomizeTabHost != null) {
1258 String curTab = savedState.getString("apps_customize_currentTab");
1259 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001260 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001261 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001262 mAppsCustomizeContent.loadAssociatedPages(
1263 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001264 }
1265
Winson Chung5afbf7b2011-07-25 11:53:08 -07001266 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1267 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001268 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001269 mItemIdToViewId = (HashMap<Integer, Integer>)
1270 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
1273 /**
1274 * Finds all the views we need and configure them properly.
1275 */
1276 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001277 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001278
Craig Mautner360310b2012-10-26 15:13:08 -07001279 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001280 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001281 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1282 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001283 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001284 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001285
John Spurlock77e1f472013-09-11 10:09:51 -04001286 mLauncherView.setSystemUiVisibility(
1287 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001288 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289
Winson Chung4c98d922011-05-31 16:50:48 -07001290 // Setup the drag layer
1291 mDragLayer.setup(this, dragController);
1292
Winson Chung3d503fb2011-07-13 17:25:49 -07001293 // Setup the hotseat
1294 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1295 if (mHotseat != null) {
1296 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001297 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 }
1299
Jorim Jaggid017f882014-01-14 17:08:48 -08001300 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001301 View widgetButton = findViewById(R.id.widget_button);
1302 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001303 @Override
1304 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001305 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001306 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001307 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001308 }
1309 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001310 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1311
1312 View wallpaperButton = findViewById(R.id.wallpaper_button);
1313 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001314 @Override
1315 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001316 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001317 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001318 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001319 }
1320 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001321 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1322
1323 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001324 if (hasSettings()) {
1325 settingsButton.setOnClickListener(new OnClickListener() {
1326 @Override
1327 public void onClick(View arg0) {
1328 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001329 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001330 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001331 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001332 });
1333 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1334 } else {
1335 settingsButton.setVisibility(View.GONE);
1336 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1337 lp.gravity = Gravity.END | Gravity.TOP;
1338 widgetButton.requestLayout();
1339 }
1340
Adam Cohendbdff6b2013-09-18 19:09:15 -07001341 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001342
Winson Chung4c98d922011-05-31 16:50:48 -07001343 // Setup the workspace
1344 mWorkspace.setHapticFeedbackEnabled(false);
1345 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001346 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001347 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001348
Winson Chungf0ea4d32011-06-06 14:27:16 -07001349 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001350 mSearchDropTargetBar = (SearchDropTargetBar)
1351 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001352
Winson Chungf0ea4d32011-06-06 14:27:16 -07001353 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001355 mAppsCustomizeContent = (AppsCustomizePagedView)
1356 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1357 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001360 dragController.setDragScoller(mWorkspace);
1361 dragController.setScrollView(mDragLayer);
1362 dragController.setMoveTarget(mWorkspace);
1363 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001364 if (mSearchDropTargetBar != null) {
1365 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001366 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001367
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001368 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001369 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001370 mWeightWatcher = new WeightWatcher(this);
1371 mWeightWatcher.setAlpha(0.5f);
1372 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001373 new FrameLayout.LayoutParams(
1374 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001375 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001376 Gravity.BOTTOM)
1377 );
Adam Cohen39a06042013-07-19 14:30:12 -07001378
1379 boolean show = shouldShowWeightWatcher();
1380 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
1383
1384 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001385 * Sets the all apps button. This method is called from {@link Hotseat}.
1386 */
1387 public void setAllAppsButton(View allAppsButton) {
1388 mAllAppsButton = allAppsButton;
1389 }
1390
1391 public View getAllAppsButton() {
1392 return mAllAppsButton;
1393 }
1394
1395 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 * Creates a view representing a shortcut.
1397 *
1398 * @param info The data structure describing the shortcut.
1399 *
1400 * @return A View inflated from R.layout.application.
1401 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001402 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001404 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
1406
1407 /**
1408 * Creates a view representing a shortcut inflated from the specified resource.
1409 *
1410 * @param layoutResId The id of the XML layout used to create the shortcut.
1411 * @param parent The group the shortcut belongs to.
1412 * @param info The data structure describing the shortcut.
1413 *
1414 * @return A View inflated from layoutResId.
1415 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001416 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001417 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001418 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001420 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return favorite;
1422 }
1423
1424 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 * Add a shortcut to the workspace.
1426 *
1427 * @param data The intent describing the shortcut.
1428 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001430 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 int cellY) {
1432 int[] cellXY = mTmpAddItemCellCoordinates;
1433 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001434 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001435
Michael Jurkad3ef3062010-11-23 16:23:58 -08001436 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437
Adam Cohen558baaf2011-08-15 15:22:57 -07001438 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001439 if (info == null) {
1440 return;
1441 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001442 final View view = createShortcut(info);
1443
Adam Cohenfbba09b2011-07-18 21:43:05 -07001444 // First we check if we already know the exact location where we want to add this item.
1445 if (cellX >= 0 && cellY >= 0) {
1446 cellXY[0] = cellX;
1447 cellXY[1] = cellY;
1448 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001449
1450 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001451 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001452 true, null,null)) {
1453 return;
1454 }
1455 DragObject dragObject = new DragObject();
1456 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001457 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1458 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001459 return;
1460 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001462 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001464 foundCellSpan = (result != null);
1465 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001466 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001467 }
1468
1469 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001470 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001471 return;
1472 }
1473
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475
1476 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001477 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480 }
1481
Adam Cohen2f093b62012-04-30 18:59:53 -07001482 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1483 int minHeight) {
1484 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001485 // We want to account for the extra amount of padding that we are adding to the widget
1486 // to ensure that it gets the full amount of space that it has requested
1487 int requiredWidth = minWidth + padding.left + padding.right;
1488 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001489 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001490 }
1491
Adam Cohen2f093b62012-04-30 18:59:53 -07001492 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1493 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001494 }
1495
Adam Cohen2f093b62012-04-30 18:59:53 -07001496 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1497 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001498 }
1499
Adam Cohen2f093b62012-04-30 18:59:53 -07001500 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1501 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001502 }
1503
Adam Cohen2f093b62012-04-30 18:59:53 -07001504 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1505 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001506 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001507 }
1508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001510 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001512 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001513 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001515 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001516 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1517 if (appWidgetInfo == null) {
1518 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001521 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001522 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001523
Adam Cohen2f093b62012-04-30 18:59:53 -07001524 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1525 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001528 // We have saved the position to which the widget was dragged-- this really only matters
1529 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 int[] cellXY = mTmpAddItemCellCoordinates;
1531 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001532 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001533 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1535 cellXY[0] = mPendingAddInfo.cellX;
1536 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001537 spanXY[0] = mPendingAddInfo.spanX;
1538 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001539 foundCellSpan = true;
1540 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001543 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1544 spanXY[1], cellXY, finalSpan);
1545 spanXY[0] = finalSpan[0];
1546 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001549 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001550 }
1551
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001553 if (appWidgetId != -1) {
1554 // Deleting an app widget ID is a void call but writes to disk before returning
1555 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001556 new AsyncTask<Void, Void, Void>() {
1557 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001558 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001559 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001560 }
Michael Jurka43467462013-11-14 12:03:58 +01001561 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001562 }
Winson Chung93eef082012-03-23 15:59:27 -07001563 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001564 return;
1565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001567 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001568 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1569 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570 launcherInfo.spanX = spanXY[0];
1571 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001572 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1573 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001574 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001580 if (hostView == null) {
1581 // Perform actual inflation because we're live
1582 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1583 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1584 } else {
1585 // The AppWidgetHostView has already been inflated and instantiated
1586 launcherInfo.hostView = hostView;
1587 }
Romain Guycbb89e42009-06-08 15:52:54 -07001588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001591 launcherInfo.notifyWidgetSizeChanged(this);
1592
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001594 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001595
1596 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001598 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Romain Guycbb89e42009-06-08 15:52:54 -07001600
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1602 @Override
1603 public void onReceive(Context context, Intent intent) {
1604 final String action = intent.getAction();
1605 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1606 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001607 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001608 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001609
Winson Chungc100e8e2011-08-09 16:02:43 -07001610 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001611 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001612 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001613 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001614 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1616 mUserPresent = true;
1617 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001618 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1619 mModel.resetLoadedState(false, true);
1620 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1621 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1622 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1623 mModel.resetLoadedState(false, true);
1624 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1625 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1626 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001627 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1628 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1629 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 }
1631 }
1632 };
1633
1634 @Override
1635 public void onAttachedToWindow() {
1636 super.onAttachedToWindow();
1637
1638 // Listen for broadcasts related to user-presence
1639 final IntentFilter filter = new IntentFilter();
1640 filter.addAction(Intent.ACTION_SCREEN_OFF);
1641 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001642 // For handling managed profiles
1643 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1644 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001645 if (ENABLE_DEBUG_INTENTS) {
1646 filter.addAction(DebugIntents.DELETE_DATABASE);
1647 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1648 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001650 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001651 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001652 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 mVisible = true;
1654 }
1655
Adam Cohen0b395352014-06-09 22:54:36 +00001656 /**
1657 * Sets up transparent navigation and status bars in LMP.
1658 * This method is a no-op for other platform versions.
1659 */
1660 @TargetApi(19)
1661 private void setupTransparentSystemBarsForLmp() {
1662 // TODO(sansid): use the APIs directly when compiling against L sdk.
1663 // Currently we use reflection to access the flags and the API to set the transparency
1664 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001665 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001666 try {
1667 getWindow().getAttributes().systemUiVisibility |=
1668 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1669 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1670 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1671 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1672 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1673 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1674 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1675 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1676
1677 Method setStatusBarColorMethod =
1678 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1679 Method setNavigationBarColorMethod =
1680 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1681 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1682 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1683 } catch (NoSuchFieldException e) {
1684 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1685 } catch (NoSuchMethodException ex) {
1686 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1687 } catch (IllegalAccessException e) {
1688 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1689 } catch (IllegalArgumentException e) {
1690 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1691 } catch (InvocationTargetException e) {
1692 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1693 } finally {}
1694 }
1695 }
1696
Adam Cohended9f8d2010-11-03 13:25:16 -07001697 @Override
1698 public void onDetachedFromWindow() {
1699 super.onDetachedFromWindow();
1700 mVisible = false;
1701
Adam Cohend113e0c2010-11-11 10:48:05 -08001702 if (mAttached) {
1703 unregisterReceiver(mReceiver);
1704 mAttached = false;
1705 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 updateRunning();
1707 }
1708
1709 public void onWindowVisibilityChanged(int visibility) {
1710 mVisible = visibility == View.VISIBLE;
1711 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001712 // The following code used to be in onResume, but it turns out onResume is called when
1713 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1714 // is a more appropriate event to handle
1715 if (mVisible) {
1716 mAppsCustomizeTabHost.onWindowVisible();
1717 if (!mWorkspaceLoading) {
1718 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001719 // We want to let Launcher draw itself at least once before we force it to build
1720 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001721 // apps is nice and speedy.
1722 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001723 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001724 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001725 if (mStarted) return;
1726 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001727 // We delay the layer building a bit in order to give
1728 // other message processing a time to run. In particular
1729 // this avoids a delay in hiding the IME if it was
1730 // currently shown, because doing that may involve
1731 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001732 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001733 final ViewTreeObserver.OnDrawListener listener = this;
1734 mWorkspace.post(new Runnable() {
1735 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001736 if (mWorkspace != null &&
1737 mWorkspace.getViewTreeObserver() != null) {
1738 mWorkspace.getViewTreeObserver().
1739 removeOnDrawListener(listener);
1740 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001741 }
1742 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001743 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 }
1745 });
1746 }
1747 clearTypedText();
1748 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 }
1750
1751 private void sendAdvanceMessage(long delay) {
1752 mHandler.removeMessages(ADVANCE_MSG);
1753 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1754 mHandler.sendMessageDelayed(msg, delay);
1755 mAutoAdvanceSentTime = System.currentTimeMillis();
1756 }
1757
1758 private void updateRunning() {
1759 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1760 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1761 mAutoAdvanceRunning = autoAdvanceRunning;
1762 if (autoAdvanceRunning) {
1763 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1764 sendAdvanceMessage(delay);
1765 } else {
1766 if (!mWidgetsToAdvance.isEmpty()) {
1767 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1768 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1769 }
1770 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001771 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
1773 }
1774 }
1775
1776 private final Handler mHandler = new Handler() {
1777 @Override
1778 public void handleMessage(Message msg) {
1779 if (msg.what == ADVANCE_MSG) {
1780 int i = 0;
1781 for (View key: mWidgetsToAdvance.keySet()) {
1782 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1783 final int delay = mAdvanceStagger * i;
1784 if (v instanceof Advanceable) {
1785 postDelayed(new Runnable() {
1786 public void run() {
1787 ((Advanceable) v).advance();
1788 }
1789 }, delay);
1790 }
1791 i++;
1792 }
1793 sendAdvanceMessage(mAdvanceInterval);
1794 }
1795 }
1796 };
1797
1798 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001799 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1801 if (v instanceof Advanceable) {
1802 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001803 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 updateRunning();
1805 }
1806 }
1807
1808 void removeWidgetToAutoAdvance(View hostView) {
1809 if (mWidgetsToAdvance.containsKey(hostView)) {
1810 mWidgetsToAdvance.remove(hostView);
1811 updateRunning();
1812 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 }
1814
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 launcherInfo.hostView = null;
1818 }
1819
Winson Chung93eef082012-03-23 15:59:27 -07001820 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1821 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1822 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 }
1824
Winson Chunga6945242014-01-08 14:04:34 -08001825 public DragLayer getDragLayer() {
1826 return mDragLayer;
1827 }
1828
1829 public Workspace getWorkspace() {
1830 return mWorkspace;
1831 }
1832
1833 public Hotseat getHotseat() {
1834 return mHotseat;
1835 }
1836
Jorim Jaggid017f882014-01-14 17:08:48 -08001837 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001838 return mOverviewPanel;
1839 }
1840
1841 public SearchDropTargetBar getSearchBar() {
1842 return mSearchDropTargetBar;
1843 }
1844
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001845 public LauncherAppWidgetHost getAppWidgetHost() {
1846 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
Romain Guycbb89e42009-06-08 15:52:54 -07001848
Winson Chunga9abd0e2010-10-27 17:18:37 -07001849 public LauncherModel getModel() {
1850 return mModel;
1851 }
1852
Winson Chunga6945242014-01-08 14:04:34 -08001853 protected SharedPreferences getSharedPrefs() {
1854 return mSharedPrefs;
1855 }
1856
Bjorn Bringertc459e522013-06-07 19:36:01 +01001857 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001858 getWindow().closeAllPanels();
1859
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001860 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001861 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 @Override
1865 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001866 long startTime = 0;
1867 if (DEBUG_RESUME_TIME) {
1868 startTime = System.currentTimeMillis();
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 super.onNewIntent(intent);
1871
1872 // Close the menu
1873 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001874 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001875 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001876
Adam Cohened307df2013-10-02 09:37:31 -07001877 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1878 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1879 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001880
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 if (mWorkspace == null) {
1882 // Can be cases where mWorkspace is null, this prevents a NPE
1883 return;
1884 }
1885 Folder openFolder = mWorkspace.getOpenFolder();
1886 // In all these cases, only animate if we're already on home
1887 mWorkspace.exitWidgetResizeMode();
1888 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001889 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001891 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001892
Adam Cohen6fecd412013-10-02 17:41:50 -07001893 closeFolder();
1894 exitSpringLoadedDragMode();
1895
1896 // If we are already on home, then just animate back to the workspace,
1897 // otherwise, just wait until onResume to set the state back to Workspace
1898 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001899 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001900 } else {
1901 mOnResumeState = State.WORKSPACE;
1902 }
1903
1904 final View v = getWindow().peekDecorView();
1905 if (v != null && v.getWindowToken() != null) {
1906 InputMethodManager imm = (InputMethodManager)getSystemService(
1907 INPUT_METHOD_SERVICE);
1908 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1909 }
1910
1911 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001912 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001913 mAppsCustomizeTabHost.reset();
1914 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001915
1916 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
Adam Cohened307df2013-10-02 09:37:31 -07001918
Michael Jurka447bf842013-05-15 14:52:15 +02001919 if (DEBUG_RESUME_TIME) {
1920 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Adam Coppa120b8e2013-11-12 16:26:04 +00001924 /**
1925 * Override point for subclasses to prevent movement to the default screen when the home
1926 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1927 */
1928 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1929 return true;
1930 }
1931
1932 /**
1933 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1934 */
1935 protected void onHomeIntent() {
1936 // Do nothing
1937 }
1938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001940 public void onRestoreInstanceState(Bundle state) {
1941 super.onRestoreInstanceState(state);
1942 for (int page: mSynchronouslyBoundPages) {
1943 mWorkspace.restoreInstanceStateForChild(page);
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946
1947 @Override
1948 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001949 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001950 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1951 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001952 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001953 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Michael Jurka883f55b2010-10-21 15:47:14 -07001955 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001956 // We close any open folder since it will not be re-opened, and we need to make sure
1957 // this state is reflected.
1958 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959
Adam Cohendcd297f2013-06-18 13:13:40 -07001960 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001961 mWaitingForResult) {
1962 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1965 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1967 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1968 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001969 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
1972 if (mFolderInfo != null && mWaitingForResult) {
1973 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1974 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1975 }
Michael Jurka946ad472010-07-09 18:05:18 -07001976
Winson Chung785d2eb2011-04-14 16:08:02 -07001977 // Save the current AppsCustomize tab
1978 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001979 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1980 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001981 if (currentTabTag != null) {
1982 outState.putString("apps_customize_currentTab", currentTabTag);
1983 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001984 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1985 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001986 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001987 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
1990 @Override
1991 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001993
Winson Chunge7a03942011-08-05 15:05:12 -07001994 // Remove all pending runnables
1995 mHandler.removeMessages(ADVANCE_MSG);
1996 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001997 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001998
Winson Chungcd2b0142011-06-08 16:02:26 -07001999 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002000 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002001
2002 // It's possible to receive onDestroy after a new Launcher activity has
2003 // been created. In this case, don't interfere with the new Launcher.
2004 if (mModel.isCurrentCallbacks(this)) {
2005 mModel.stopLoader();
2006 app.setLauncher(null);
2007 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002010 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002012 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002014 mAppWidgetHost = null;
2015
2016 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017
2018 TextKeyListener.getInstance().release();
2019
Winson Chung81b52252012-08-27 15:34:29 -07002020 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2021 // to prevent leaking Launcher activities on orientation change.
2022 if (mModel != null) {
2023 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2024 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002025
2026 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002027 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002028
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002029 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002030 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002031 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002032 mWorkspace = null;
2033 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002034
Sunny Goyale755d462014-07-22 13:48:29 -07002035 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002036 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 }
2038
Adam Cohena9cf38f2011-05-02 15:36:58 -07002039 public DragController getDragController() {
2040 return mDragController;
2041 }
2042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
2044 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002045 if (requestCode >= 0) {
2046 setWaitingForResult(true);
2047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 super.startActivityForResult(intent, requestCode);
2049 }
2050
Winson Chung70d72102011-08-12 11:24:35 -07002051 /**
2052 * Indicates that we want global search for this activity by setting the globalSearch
2053 * argument for {@link #startSearch} to true.
2054 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002056 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002058
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002059 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002060
Karl Rosaen138a0412009-04-23 19:00:21 -07002061 if (initialQuery == null) {
2062 // Use any text typed in the launcher as the initial query
2063 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 if (appSearchData == null) {
2066 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002067 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
Winson Chungadf0c182012-08-23 14:59:07 -07002069 Rect sourceBounds = new Rect();
2070 if (mSearchDropTargetBar != null) {
2071 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2072 }
Romain Guycbb89e42009-06-08 15:52:54 -07002073
Ian Parkinson047036e2014-09-01 15:40:53 +01002074 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 if (clearTextImmediately) {
2077 clearTypedText();
2078 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002079 }
2080
Ian Parkinson047036e2014-09-01 15:40:53 +01002081 /**
2082 * Start a text search.
2083 *
2084 * @return {@code true} if the search will start immediately, so any further keypresses
2085 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2086 * to buffer keypresses.
2087 */
2088 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002090 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002092 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002093 }
2094
2095 /**
2096 * Starts the global search activity. This code is a copied from SearchManager
2097 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002098 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002099 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002100 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2102 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2103 if (globalSearchActivity == null) {
2104 Log.w(TAG, "No global search activity found.");
2105 return;
2106 }
2107 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2108 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2109 intent.setComponent(globalSearchActivity);
2110 // Make sure that we have a Bundle to put source in
2111 if (appSearchData == null) {
2112 appSearchData = new Bundle();
2113 } else {
2114 appSearchData = new Bundle(appSearchData);
2115 }
2116 // Set source to package name of app that starts global search, if not set already.
2117 if (!appSearchData.containsKey("source")) {
2118 appSearchData.putString("source", getPackageName());
2119 }
2120 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2121 if (!TextUtils.isEmpty(initialQuery)) {
2122 intent.putExtra(SearchManager.QUERY, initialQuery);
2123 }
2124 if (selectInitialQuery) {
2125 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2126 }
2127 intent.setSourceBounds(sourceBounds);
2128 try {
2129 startActivity(intent);
2130 } catch (ActivityNotFoundException ex) {
2131 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 }
2134
Yura4f93ec62014-02-04 14:15:21 +00002135 public boolean isOnCustomContent() {
2136 return mWorkspace.isOnOrMovingToCustomContent();
2137 }
2138
Winson Chung70d72102011-08-12 11:24:35 -07002139 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002140 public boolean onPrepareOptionsMenu(Menu menu) {
2141 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002142 if (!isOnCustomContent()) {
2143 // Close any open folders
2144 closeFolder();
2145 // Stop resizing any widgets
2146 mWorkspace.exitWidgetResizeMode();
2147 if (!mWorkspace.isInOverviewMode()) {
2148 // Show the overview mode
2149 showOverviewMode(true);
2150 } else {
2151 showWorkspace(true);
2152 }
Winson Chunge0298742014-01-17 12:03:00 -08002153 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002154 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002157 @Override
2158 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002159 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002160 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002161 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002162 }
2163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 public boolean isWorkspaceLocked() {
2165 return mWorkspaceLoading || mWaitingForResult;
2166 }
2167
Adam Cohen517a7f52014-03-01 12:12:59 -08002168 public boolean isWorkspaceLoading() {
2169 return mWorkspaceLoading;
2170 }
2171
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002172 private void setWorkspaceLoading(boolean value) {
2173 boolean isLocked = isWorkspaceLocked();
2174 mWorkspaceLoading = value;
2175 if (isLocked != isWorkspaceLocked()) {
2176 onWorkspaceLockedChanged();
2177 }
2178 }
2179
2180 private void setWaitingForResult(boolean value) {
2181 boolean isLocked = isWorkspaceLocked();
2182 mWaitingForResult = value;
2183 if (isLocked != isWorkspaceLocked()) {
2184 onWorkspaceLockedChanged();
2185 }
2186 }
2187
2188 protected void onWorkspaceLockedChanged() { }
2189
Michael Jurka0280c3b2010-09-17 15:00:07 -07002190 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002191 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002192 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002193 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2194 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002195 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002196 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002198
Adam Cohenad4e15c2013-10-17 16:21:35 -07002199 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2200 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2201 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2202 }
2203
2204 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2205 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2206 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002207 if (appWidgetInfo.configure != null) {
2208 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002209 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002210
Bjorn Bringert7984c942009-12-09 15:38:25 +00002211 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002212 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2213 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002216 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002217 Runnable onComplete = new Runnable() {
2218 @Override
2219 public void run() {
2220 // Exit spring loaded mode if necessary after adding the widget
2221 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2222 null);
2223 }
2224 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002225 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002226 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002227 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 }
2229 }
Romain Guycbb89e42009-06-08 15:52:54 -07002230
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002231 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002232 // Close any folders that may be open.
2233 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002234 mWorkspace.moveToCustomContentScreen(animate);
2235 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236 /**
2237 * Process a shortcut drop.
2238 *
2239 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 * @param cell The cell it should be added to, optional
2242 * @param position The location on the screen where it was dropped, optional
2243 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002250
2251 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.cellX = cell[0];
2253 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002254 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255
2256 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2257 createShortcutIntent.setComponent(componentName);
2258 processShortcut(createShortcutIntent);
2259 }
2260
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261 /**
2262 * Process a widget drop.
2263 *
2264 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002266 * @param cell The cell it should be added to, optional
2267 * @param position The location on the screen where it was dropped, optional
2268 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002270 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002271 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002274 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002275 mPendingAddInfo.minSpanX = info.minSpanX;
2276 mPendingAddInfo.minSpanY = info.minSpanY;
2277
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.cellX = cell[0];
2280 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 if (span != null) {
2283 mPendingAddInfo.spanX = span[0];
2284 mPendingAddInfo.spanY = span[1];
2285 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002286
Adam Cohened66b2b2012-01-23 17:28:51 -08002287 AppWidgetHostView hostView = info.boundWidget;
2288 int appWidgetId;
2289 if (hostView != null) {
2290 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002291 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 // In this case, we either need to start an activity to get permission to bind
2294 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002296 Bundle options = info.bindOptions;
2297
Sunny Goyalffe83f12014-08-14 17:39:34 -07002298 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2299 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002300 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002301 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002302 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2305 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002307 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2308 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002309 // TODO: we need to make sure that this accounts for the options bundle.
2310 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002311 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2312 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002313 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 }
2315
Joe Onoratodeb98af2010-02-19 14:59:39 -08002316 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002317 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 }
2319
Winson Chung24ab2f12010-09-16 14:10:47 -07002320 void processWallpaper(Intent intent) {
2321 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2322 }
2323
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002326 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 folderInfo.title = getText(R.string.folder_name);
2328
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002332 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333
2334 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 FolderIcon newFolder =
2336 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002339 // Force measure the new folder icon
2340 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2341 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002342 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 }
Romain Guycbb89e42009-06-08 15:52:54 -07002344
Joe Onorato9c1289c2009-08-17 11:03:03 -04002345 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002346 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 }
2348
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002349 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002350 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002351 }
2352
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002353 /**
2354 * Registers various content observers. The current implementation registers
2355 * only a favorites observer to keep track of the favorites applications.
2356 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002357 private void registerContentObservers() {
2358 ContentResolver resolver = getContentResolver();
2359 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2360 true, mWidgetObserver);
2361 }
2362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 @Override
2364 public boolean dispatchKeyEvent(KeyEvent event) {
2365 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2366 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002368 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002369 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002370 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002371 dumpState();
2372 return true;
2373 }
2374 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002375 }
2376 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2377 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002378 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 return true;
2380 }
2381 }
2382
2383 return super.dispatchKeyEvent(event);
2384 }
2385
Joe Onorato88ec0992009-11-19 13:16:06 -08002386 @Override
2387 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002388 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002389 if (mAppsCustomizeContent.getContentType() ==
2390 AppsCustomizePagedView.ContentType.Applications) {
2391 showWorkspace(true);
2392 } else {
2393 showOverviewMode(true);
2394 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002395 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002396 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002397 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002398 Folder openFolder = mWorkspace.getOpenFolder();
2399 if (openFolder.isEditingName()) {
2400 openFolder.dismissEditingName();
2401 } else {
2402 closeFolder();
2403 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002404 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002405 mWorkspace.exitWidgetResizeMode();
2406
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002407 // Back button is a no-op here, but give at least some feedback for the button press
2408 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002409 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002410 }
2411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002413 * Re-listen when widgets are reset.
2414 */
2415 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002416 if (mAppWidgetHost != null) {
2417 mAppWidgetHost.startListening();
2418 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002419 }
2420
2421 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 * Launches the intent referred by the clicked shortcut.
2423 *
2424 * @param v The view representing the clicked shortcut.
2425 */
2426 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002427 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2428 // view has detached (it's possible for this to happen if the view is removed mid touch).
2429 if (v.getWindowToken() == null) {
2430 return;
2431 }
2432
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002433 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002434 return;
2435 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002436
Adam Cohen1697b792013-09-17 19:08:21 -07002437 if (v instanceof Workspace) {
2438 if (mWorkspace.isInOverviewMode()) {
2439 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002440 }
2441 return;
2442 }
2443
2444 if (v instanceof CellLayout) {
2445 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002446 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002447 }
2448 }
2449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002451 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002452 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002454 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002455 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002456 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002457 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002458 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002459 } else if (tag instanceof AppInfo) {
2460 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002461 } else if (tag instanceof LauncherAppWidgetInfo) {
2462 if (v instanceof PendingAppWidgetHostView) {
2463 onClickPendingWidget((PendingAppWidgetHostView) v);
2464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 }
2466 }
2467
Sunny Goyal508da152014-08-14 10:53:27 -07002468 public void onClickPagedViewIcon(View v) {
2469 startAppShortcutOrInfoActivity(v);
2470 }
2471
Michael Jurka0e260592010-06-30 17:07:39 -07002472 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002473 return false;
2474 }
2475
Michael Jurkaaf442092010-06-10 17:01:57 -07002476 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002477 * Event handler for the app widget view which has not fully restored.
2478 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002479 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2480 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002481 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002482 int widgetId = info.appWidgetId;
2483 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2484 if (appWidgetInfo != null) {
2485 mPendingAddWidgetInfo = appWidgetInfo;
2486 mPendingAddInfo.copyFrom(info);
2487 mPendingAddWidgetId = widgetId;
2488
Sunny Goyalffe83f12014-08-14 17:39:34 -07002489 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2490 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002491 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002492 } else if (info.installProgress < 0) {
2493 // The install has not been queued
2494 final String packageName = info.providerName.getPackageName();
2495 showBrokenAppInstallDialog(packageName,
2496 new DialogInterface.OnClickListener() {
2497 public void onClick(DialogInterface dialog, int id) {
2498 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2499 }
2500 });
2501 } else {
2502 // Download has started.
2503 final String packageName = info.providerName.getPackageName();
2504 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 }
2506 }
2507
2508 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002509 * Event handler for the search button
2510 *
2511 * @param v The view that was clicked.
2512 */
2513 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002514 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2515
Amith Yamasania135ba82011-08-09 17:42:01 -07002516 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002517 }
2518
2519 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002520 * Event handler for the voice button
2521 *
2522 * @param v The view that was clicked.
2523 */
2524 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002525 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002526
Bjorn Bringertc459e522013-06-07 19:36:01 +01002527 startVoice();
2528 }
2529
2530 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002531 try {
2532 final SearchManager searchManager =
2533 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2534 ComponentName activityName = searchManager.getGlobalSearchActivity();
2535 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002536 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002537 if (activityName != null) {
2538 intent.setPackage(activityName.getPackageName());
2539 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002540 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002541 } catch (ActivityNotFoundException e) {
2542 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002543 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002544 startActivitySafely(null, intent, "onClickVoiceButton");
2545 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002546 }
2547
2548 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002549 * Event handler for the "grid" button that appears on the home screen, which
2550 * enters all apps mode.
2551 *
2552 * @param v The view that was clicked.
2553 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002554 protected void onClickAllAppsButton(View v) {
2555 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2556 if (isAllAppsVisible()) {
2557 showWorkspace(true);
2558 } else {
2559 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2560 }
2561 }
2562
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002563 private void showBrokenAppInstallDialog(final String packageName,
2564 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyalf8763012014-09-23 16:22:10 -07002565 new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault))
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002566 .setTitle(R.string.abandoned_promises_title)
2567 .setMessage(R.string.abandoned_promise_explanation)
2568 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2569 .setNeutralButton(R.string.abandoned_clean_this,
2570 new DialogInterface.OnClickListener() {
2571 public void onClick(DialogInterface dialog, int id) {
2572 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2573 mWorkspace.removeAbandonedPromise(packageName, user);
2574 }
2575 })
2576 .create().show();
2577 return;
2578 }
2579
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 /**
2581 * Event handler for an app shortcut click.
2582 *
2583 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2584 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002585 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2587 Object tag = v.getTag();
2588 if (!(tag instanceof ShortcutInfo)) {
2589 throw new IllegalArgumentException("Input must be a Shortcut");
2590 }
2591
2592 // Open shortcut
2593 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002594
2595 if (shortcut.isDisabled != 0) {
2596 int error = R.string.activity_not_available;
2597 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2598 error = R.string.safemode_shortcut_error;
2599 }
2600 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2601 return;
2602 }
2603
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 final Intent intent = shortcut.intent;
2605
2606 // Check for special shortcuts
2607 if (intent.getComponent() != null) {
2608 final String shortcutClass = intent.getComponent().getClassName();
2609
2610 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2611 MemoryDumpActivity.startDump(this);
2612 return;
2613 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2614 toggleShowWeightWatcher();
2615 return;
2616 }
2617 }
2618
Chris Wren40c5ed32014-06-24 18:24:23 -04002619 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002620 if ((v instanceof BubbleTextView)
2621 && shortcut.isPromise()
2622 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002624 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002625 new DialogInterface.OnClickListener() {
2626 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002627 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 return;
2631 }
2632
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002633 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002634 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 }
2636
Sunny Goyal508da152014-08-14 10:53:27 -07002637 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002639 final ShortcutInfo shortcut;
2640 final Intent intent;
2641 if (tag instanceof ShortcutInfo) {
2642 shortcut = (ShortcutInfo) tag;
2643 intent = shortcut.intent;
2644 int[] pos = new int[2];
2645 v.getLocationOnScreen(pos);
2646 intent.setSourceBounds(new Rect(pos[0], pos[1],
2647 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002648
Sunny Goyal508da152014-08-14 10:53:27 -07002649 } else if (tag instanceof AppInfo) {
2650 shortcut = null;
2651 intent = ((AppInfo) tag).intent;
2652 } else {
2653 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2654 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002655
2656 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002657 mStats.recordLaunch(intent, shortcut);
2658
2659 if (success && v instanceof BubbleTextView) {
2660 mWaitingForResume = (BubbleTextView) v;
2661 mWaitingForResume.setStayPressed(true);
2662 }
2663 }
2664
2665 /**
2666 * Event handler for a folder icon click.
2667 *
2668 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2669 */
2670 protected void onClickFolderIcon(View v) {
2671 if (LOGD) Log.d(TAG, "onClickFolder");
2672 if (!(v instanceof FolderIcon)){
2673 throw new IllegalArgumentException("Input must be a FolderIcon");
2674 }
2675
2676 FolderIcon folderIcon = (FolderIcon) v;
2677 final FolderInfo info = folderIcon.getFolderInfo();
2678 Folder openFolder = mWorkspace.getFolderForTag(info);
2679
2680 // If the folder info reports that the associated folder is open, then verify that
2681 // it is actually opened. There have been a few instances where this gets out of sync.
2682 if (info.opened && openFolder == null) {
2683 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2684 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2685 info.opened = false;
2686 }
2687
2688 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2689 // Close any open folder
2690 closeFolder();
2691 // Open the requested folder
2692 openFolder(folderIcon);
2693 } else {
2694 // Find the open folder...
2695 int folderScreen;
2696 if (openFolder != null) {
2697 folderScreen = mWorkspace.getPageForView(openFolder);
2698 // .. and close it
2699 closeFolder(openFolder);
2700 if (folderScreen != mWorkspace.getCurrentPage()) {
2701 // Close any folder open on the current screen
2702 closeFolder();
2703 // Pull the folder onto this screen
2704 openFolder(folderIcon);
2705 }
2706 }
2707 }
Michael Jurka5130e402011-10-13 04:55:35 -07002708 }
2709
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002710 /**
2711 * Event handler for the (Add) Widgets button that appears after a long press
2712 * on the home screen.
2713 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002714 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002716 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2717 }
2718
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002719 /**
2720 * Event handler for the wallpaper picker button that appears after a long press
2721 * on the home screen.
2722 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002723 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2725 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2726 pickWallpaper.setComponent(getWallpaperPickerComponent());
2727 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2728 }
2729
2730 /**
2731 * Event handler for a click on the settings button that appears after a long press
2732 * on the home screen.
2733 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002734 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2736 }
2737
Michael Jurka5130e402011-10-13 04:55:35 -07002738 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002739 // Provide the same haptic feedback that the system offers for virtual keys.
2740 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002741 }
2742
Adam Cohen61f560d2013-09-30 15:58:20 -07002743 public void performHapticFeedbackOnTouchDown(View v) {
2744 // Provide the same haptic feedback that the system offers for virtual keys.
2745 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2746 }
2747
2748 public View.OnTouchListener getHapticFeedbackTouchListener() {
2749 if (mHapticFeedbackTouchListener == null) {
2750 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2751 @Override
2752 public boolean onTouch(View v, MotionEvent event) {
2753 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2754 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2755 }
2756 return false;
2757 }
2758 };
2759 }
2760 return mHapticFeedbackTouchListener;
2761 }
2762
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002763 public void onDragStarted(View view) {}
2764
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002765 /**
2766 * Called when the user stops interacting with the launcher.
2767 * This implies that the user is now on the homescreen and is not doing housekeeping.
2768 */
2769 protected void onInteractionEnd() {}
2770
2771 /**
2772 * Called when the user starts interacting with the launcher.
2773 * The possible interactions are:
2774 * - open all apps
2775 * - reorder an app shortcut, or a widget
2776 * - open the overview mode.
2777 * This is a good time to stop doing things that only make sense
2778 * when the user is on the homescreen and not doing housekeeping.
2779 */
2780 protected void onInteractionBegin() {}
2781
Kenny Guyf07af7b2014-07-31 11:39:16 +01002782 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002783 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002784 try {
2785 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2786 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2787 launcherApps.showAppDetailsForProfile(componentName, user);
2788 } catch (SecurityException e) {
2789 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2790 Log.e(TAG, "Launcher does not have permission to launch settings");
2791 } catch (ActivityNotFoundException e) {
2792 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2793 Log.e(TAG, "Unable to launch settings");
2794 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002795 }
2796
Michael Jurka1e2f4652013-07-08 18:03:46 -07002797 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002798 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2799 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002800 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002801 // System applications cannot be installed. For now, show a toast explaining that.
2802 // We may give them the option of disabling apps this way.
2803 int messageId = R.string.uninstall_system_app_text;
2804 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002805 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002806 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002807 String packageName = componentName.getPackageName();
2808 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002809 Intent intent = new Intent(
2810 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002811 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2812 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002813 if (user != null) {
2814 user.addToIntent(intent, Intent.EXTRA_USER);
2815 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002816 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002817 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002818 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002819 }
2820
Michael Jurka86a720e2012-05-09 11:23:23 -07002821 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002822 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002824 // Only launch using the new animation if the shortcut has not opted out (this is a
2825 // private contract between launcher and may be ignored in the future).
2826 boolean useLaunchAnimation = (v != null) &&
2827 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002828 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2829 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002830
Kenny Guy1317e2d2014-05-08 18:52:50 +01002831 UserHandleCompat user = null;
2832 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2833 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2834 user = userManager.getUserForSerialNumber(serialNumber);
2835 }
2836
2837 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002838 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002839 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002840 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2841 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002842 optsBundle = opts.toBundle();
2843 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002844
2845 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2846 // Could be launching some bookkeeping activity
2847 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002848 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002849 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002850 launcherApps.startActivityForProfile(intent.getComponent(), user,
2851 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002852 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002853 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 } catch (SecurityException e) {
2855 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002856 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002858 "or use the exported attribute for this activity. "
2859 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002861 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002863
Michael Jurka86a720e2012-05-09 11:23:23 -07002864 boolean startActivitySafely(View v, Intent intent, Object tag) {
2865 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002866 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2867 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2868 return false;
2869 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002870 try {
2871 success = startActivity(v, intent, tag);
2872 } catch (ActivityNotFoundException e) {
2873 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2874 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2875 }
2876 return success;
2877 }
2878
Adam Cohen268c4752012-06-06 17:47:33 -07002879 /**
2880 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2881 * in the DragLayer in the exact absolute location of the original FolderIcon.
2882 */
2883 private void copyFolderIconToImage(FolderIcon fi) {
2884 final int width = fi.getMeasuredWidth();
2885 final int height = fi.getMeasuredHeight();
2886
2887 // Lazy load ImageView, Bitmap and Canvas
2888 if (mFolderIconImageView == null) {
2889 mFolderIconImageView = new ImageView(this);
2890 }
2891 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2892 mFolderIconBitmap.getHeight() != height) {
2893 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2894 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2895 }
2896
2897 DragLayer.LayoutParams lp;
2898 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2899 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2900 } else {
2901 lp = new DragLayer.LayoutParams(width, height);
2902 }
2903
Adam Cohen307fe232012-08-16 17:55:58 -07002904 // The layout from which the folder is being opened may be scaled, adjust the starting
2905 // view size by this scale factor.
2906 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002907 lp.customPosition = true;
2908 lp.x = mRectForFolderAnimation.left;
2909 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002910 lp.width = (int) (scale * width);
2911 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002912
2913 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2914 fi.draw(mFolderIconCanvas);
2915 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002916 if (fi.getFolder() != null) {
2917 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2918 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002919 }
Adam Cohen268c4752012-06-06 17:47:33 -07002920 // Just in case this image view is still in the drag layer from a previous animation,
2921 // we remove it and re-add it.
2922 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2923 mDragLayer.removeView(mFolderIconImageView);
2924 }
2925 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002926 if (fi.getFolder() != null) {
2927 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002928 }
Adam Cohen268c4752012-06-06 17:47:33 -07002929 }
2930
Adam Cohen2801caf2011-05-13 20:57:39 -07002931 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002932 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002933 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2934 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2935 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2936
Adam Cohenc51934b2011-07-26 21:07:43 -07002937 FolderInfo info = (FolderInfo) fi.getTag();
2938 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2939 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002940 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2941 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002942 }
2943
Adam Cohen268c4752012-06-06 17:47:33 -07002944 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2945 copyFolderIconToImage(fi);
2946 fi.setVisibility(View.INVISIBLE);
2947
Michael Jurka2ecf9952012-06-18 12:52:28 -07002948 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002949 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002950 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002951 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2952 }
2953 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002954 oa.start();
2955 }
2956
Adam Cohen268c4752012-06-06 17:47:33 -07002957 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002958 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002959 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2960 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2961 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2962
Adam Cohen268c4752012-06-06 17:47:33 -07002963 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002964
Adam Cohen268c4752012-06-06 17:47:33 -07002965 // We remove and re-draw the FolderIcon in-case it has changed
2966 mDragLayer.removeView(mFolderIconImageView);
2967 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002968 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002969 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002970 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002971 oa.addListener(new AnimatorListenerAdapter() {
2972 @Override
2973 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002974 if (cl != null) {
2975 cl.clearFolderLeaveBehind();
2976 // Remove the ImageView copy of the FolderIcon and make the original visible.
2977 mDragLayer.removeView(mFolderIconImageView);
2978 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002979 }
2980 }
2981 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002982 oa.start();
2983 }
2984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002986 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 * is animated relative to the specified View. If the View is null, no animation
2988 * is played.
2989 *
2990 * @param folderInfo The FolderInfo describing the folder to open.
2991 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002992 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002993 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002994 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995
Adam Cohena9cf38f2011-05-02 15:36:58 -07002996 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997
Adam Cohen4554ee12011-08-03 16:13:21 -07002998 // Just verify that the folder hasn't already been added to the DragLayer.
2999 // There was a one-off crash where the folder had a parent already.
3000 if (folder.getParent() == null) {
3001 mDragLayer.addView(folder);
3002 mDragController.addDropTarget((DropTarget) folder);
3003 } else {
3004 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3005 folder.getParent() + ").");
3006 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003007 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003008 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003009
3010 // Notify the accessibility manager that this folder "window" has appeared and occluded
3011 // the workspace items
3012 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3013 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003014 }
3015
3016 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003017 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003018 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003019 if (folder.isEditingName()) {
3020 folder.dismissEditingName();
3021 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003022 closeFolder(folder);
3023 }
3024 }
3025
3026 void closeFolder(Folder folder) {
3027 folder.getInfo().opened = false;
3028
3029 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3030 if (parent != null) {
3031 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3032 shrinkAndFadeInFolderIcon(fi);
3033 }
3034 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003035
3036 // Notify the accessibility manager that this folder "window" has disappeard and no
3037 // longer occludeds the workspace items
3038 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 }
3040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003042 if (!isDraggingEnabled()) return false;
3043 if (isWorkspaceLocked()) return false;
3044 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045
Adam Cohen1697b792013-09-17 19:08:21 -07003046 if (v instanceof Workspace) {
3047 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003048 if (mWorkspace.enterOverviewMode()) {
3049 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3050 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3051 return true;
3052 } else {
3053 return false;
3054 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003055 } else {
3056 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003057 }
Adam Cohen1697b792013-09-17 19:08:21 -07003058 }
3059
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003060 CellLayout.CellInfo longClickCellInfo = null;
3061 View itemUnderLongClick = null;
3062 if (v.getTag() instanceof ItemInfo) {
3063 ItemInfo info = (ItemInfo) v.getTag();
3064 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3065 itemUnderLongClick = longClickCellInfo.cell;
3066 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067 }
3068
Winson Chung3d503fb2011-07-13 17:25:49 -07003069 // The hotseat touch handling does not go through Workspace, and we always allow long press
3070 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003071 final boolean inHotseat = isHotseatLayout(v);
3072 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003073 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003074 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003075 // User long pressed on empty space
3076 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3077 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003078 if (mWorkspace.isInOverviewMode()) {
3079 mWorkspace.startReordering(v);
3080 } else {
3081 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003084 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3085 mHotseat.getOrderInHotseat(
3086 longClickCellInfo.cellX,
3087 longClickCellInfo.cellY));
3088 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003090 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 }
3092 }
3093 }
3094 return true;
3095 }
3096
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 boolean isHotseatLayout(View layout) {
3098 return mHotseat != null && layout != null &&
3099 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3100 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003101
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 /**
3103 * Returns the CellLayout of the specified container at the specified screen.
3104 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003105 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003106 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3107 if (mHotseat != null) {
3108 return mHotseat.getLayout();
3109 } else {
3110 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003111 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003112 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003113 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003114 }
3115 }
3116
Daniel Sandler843e8602010-06-07 14:59:01 -04003117 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003118 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003119 }
3120
Craig Mautner360310b2012-10-26 15:13:08 -07003121 private void setWorkspaceBackground(boolean workspace) {
3122 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003123 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003124 }
3125
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003126 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003127 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3128 int curflags = getWindow().getAttributes().flags
3129 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3130 if (wpflags != curflags) {
3131 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3132 }
Craig Mautner360310b2012-10-26 15:13:08 -07003133 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003134 }
3135
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003136 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3137 if (v instanceof LauncherTransitionable) {
3138 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3139 }
3140 }
3141
Michael Jurkabed61d22012-02-14 22:51:29 -08003142 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3143 if (v instanceof LauncherTransitionable) {
3144 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3145 }
Winson Chung70442722012-02-10 15:43:22 -08003146
3147 // Update the workspace transition step as well
3148 dispatchOnLauncherTransitionStep(v, 0f);
3149 }
3150
3151 private void dispatchOnLauncherTransitionStep(View v, float t) {
3152 if (v instanceof LauncherTransitionable) {
3153 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3154 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003155 }
3156
3157 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3158 if (v instanceof LauncherTransitionable) {
3159 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3160 }
Winson Chung70442722012-02-10 15:43:22 -08003161
3162 // Update the workspace transition step as well
3163 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003164 }
3165
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003166 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003167 * Things to test when changing the following seven functions.
3168 * - Home from workspace
3169 * - from center screen
3170 * - from other screens
3171 * - Home from all apps
3172 * - from center screen
3173 * - from other screens
3174 * - Back from all apps
3175 * - from center screen
3176 * - from other screens
3177 * - Launch app from workspace and quit
3178 * - with back
3179 * - with home
3180 * - Launch app from all apps and quit
3181 * - with back
3182 * - with home
3183 * - Go to a screen that's not the default, then all
3184 * apps, and launch and app, and go back
3185 * - with back
3186 * -with home
3187 * - On workspace, long press power and go back
3188 * - with back
3189 * - with home
3190 * - On all apps, long press power and go back
3191 * - with back
3192 * - with home
3193 * - On workspace, power off
3194 * - On all apps, power off
3195 * - Launch an app and turn off the screen while in that app
3196 * - Go back with home key
3197 * - Go back with back key TODO: make this not go to workspace
3198 * - From all apps
3199 * - From workspace
3200 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3201 * - From all apps
3202 * - From the center workspace
3203 * - From another workspace
3204 */
3205
3206 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003207 * Zoom the camera out from the workspace to reveal 'toView'.
3208 * Assumes that the view to show is anchored at either the very top or very bottom
3209 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003210 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003211 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003212 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3213 showAppsCustomizeHelper(animated, springLoaded, contentType);
3214 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003215
Winson Chungc58497e2013-09-03 17:48:37 -07003216 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3217 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003218 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003219 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003220 mStateAnimation.cancel();
3221 mStateAnimation = null;
3222 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003223
Kenny Guyd794a3f2014-09-16 15:17:58 +01003224 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003225
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003226 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003227
Winson Chungf0ea4d32011-06-06 14:27:16 -07003228 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3229 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003230 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003231 final int itemsAlphaStagger =
3232 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003233
Winson Chungf0ea4d32011-06-06 14:27:16 -07003234 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003235 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003236 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003237
Adam Cohen2854d252014-08-27 16:04:07 -07003238 final ArrayList<View> layerViews = new ArrayList<View>();
3239
Adam Cohen6c5891a2014-07-09 23:53:15 -07003240 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3241 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003242 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003243 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003244 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003245 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3246 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003247 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3248 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003249
Adam Cohena38dc902014-09-07 17:48:55 +02003250 // If for some reason our views aren't initialized, don't animate
3251 boolean initialized = getAllAppsButton() != null;
3252
3253 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003254 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003255 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3256 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003257
Adam Cohen9bfdb762014-07-21 17:44:06 -07003258 final View page = content.getPageAt(content.getCurrentPage());
3259 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003260
Adam Cohen63f1ec02014-08-12 09:23:13 -07003261 final float initialPanelAlpha = 1f;
3262
3263 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3264 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003265 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3266 } else {
3267 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3268 }
3269
Adam Cohen9bfdb762014-07-21 17:44:06 -07003270 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003271 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003272 revealView.setVisibility(View.VISIBLE);
3273 // We need to hide this view as the animation start will be posted.
3274 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003275
Adam Cohen9bfdb762014-07-21 17:44:06 -07003276 int width = revealView.getMeasuredWidth();
3277 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003278 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003279
Adam Cohen63f1ec02014-08-12 09:23:13 -07003280 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003281 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003282
Adam Cohen63f1ec02014-08-12 09:23:13 -07003283 // Get the y delta between the center of the page and the center of the all apps button
3284 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3285 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003286
Adam Cohen2854d252014-08-27 16:04:07 -07003287 float alpha = 0;
3288 float xDrift = 0;
3289 float yDrift = 0;
3290 if (material) {
3291 alpha = isWidgetTray ? 0.3f : 1f;
3292 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3293 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3294 } else {
3295 yDrift = 2 * height / 3;
3296 xDrift = 0;
3297 }
3298 final float initAlpha = alpha;
3299
Adam Cohen9bfdb762014-07-21 17:44:06 -07003300 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003301 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003302 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003303 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003304 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003305 PropertyValuesHolder panelDriftX =
3306 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003307
Adam Cohen2854d252014-08-27 16:04:07 -07003308 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3309 panelAlpha, panelDriftY, panelDriftX);
3310
Adam Cohen9bfdb762014-07-21 17:44:06 -07003311 panelAlphaAndDrift.setDuration(revealDuration);
3312 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003313
Adam Cohen9bfdb762014-07-21 17:44:06 -07003314 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003315
Adam Cohen4e243a22014-08-10 18:30:55 -07003316 if (page != null) {
3317 page.setVisibility(View.VISIBLE);
3318 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003319 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003320
Adam Cohen2854d252014-08-27 16:04:07 -07003321 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3322 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003323 pageDrift.setDuration(revealDuration);
3324 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003325 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003326 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003327
Adam Cohen63f1ec02014-08-12 09:23:13 -07003328 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003329 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003330 itemsAlpha.setDuration(revealDuration);
3331 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3332 itemsAlpha.setStartDelay(itemsAlphaStagger);
3333 mStateAnimation.play(itemsAlpha);
3334 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003335
Adam Cohen4e243a22014-08-10 18:30:55 -07003336 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3337 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003338 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003339 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003340 indicatorsAlpha.setDuration(revealDuration);
3341 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003342
Adam Cohen9bfdb762014-07-21 17:44:06 -07003343 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003344 final View allApps = getAllAppsButton();
3345 int allAppsButtonSize = LauncherAppState.getInstance().
3346 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3347 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003348 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003349 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003350 reveal.setDuration(revealDuration);
3351 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003352
3353 reveal.addListener(new AnimatorListenerAdapter() {
3354 public void onAnimationStart(Animator animation) {
3355 if (!isWidgetTray) {
3356 allApps.setVisibility(View.INVISIBLE);
3357 }
3358 }
3359 public void onAnimationEnd(Animator animation) {
3360 if (!isWidgetTray) {
3361 allApps.setVisibility(View.VISIBLE);
3362 }
3363 }
3364 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003365 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003366 }
Michael Jurka1899a362011-11-03 13:50:45 -07003367
Adam Cohen9bfdb762014-07-21 17:44:06 -07003368 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3369 @Override
3370 public void onAnimationEnd(Animator animation) {
3371 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3372 dispatchOnLauncherTransitionEnd(toView, animated, false);
3373
3374 revealView.setVisibility(View.INVISIBLE);
3375 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003376 if (page != null) {
3377 page.setLayerType(View.LAYER_TYPE_NONE, null);
3378 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003379 content.setPageBackgroundsVisible(true);
3380
3381 // Hide the search bar
3382 if (mSearchDropTargetBar != null) {
3383 mSearchDropTargetBar.hideSearchBar(false);
3384 }
3385 }
3386
Adam Cohen9bfdb762014-07-21 17:44:06 -07003387 });
3388
Adam Cohen6c5891a2014-07-09 23:53:15 -07003389 if (workspaceAnim != null) {
3390 mStateAnimation.play(workspaceAnim);
3391 }
Adam Cohen2854d252014-08-27 16:04:07 -07003392
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003393 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3394 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003395 final AnimatorSet stateAnimation = mStateAnimation;
3396 final Runnable startAnimRunnable = new Runnable() {
3397 public void run() {
3398 // Check that mStateAnimation hasn't changed while
3399 // we waited for a layout/draw pass
3400 if (mStateAnimation != stateAnimation)
3401 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003402 dispatchOnLauncherTransitionStart(fromView, animated, false);
3403 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003404
3405 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003406 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003407 for (int i = 0; i < layerViews.size(); i++) {
3408 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003409 if (v != null) {
3410 boolean attached = true;
3411 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3412 attached = v.isAttachedToWindow();
3413 }
3414 if (attached) v.buildLayer();
3415 }
Adam Cohen2854d252014-08-27 16:04:07 -07003416 }
3417 }
3418 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003419 }
3420 };
Adam Cohen2854d252014-08-27 16:04:07 -07003421 toView.bringToFront();
3422 toView.setVisibility(View.VISIBLE);
3423 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003424 } else {
3425 toView.setTranslationX(0.0f);
3426 toView.setTranslationY(0.0f);
3427 toView.setScaleX(1.0f);
3428 toView.setScaleY(1.0f);
3429 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003430 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003431
Daniel Sandlere4f98912013-06-25 15:13:26 -04003432 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003433 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003434 if (mSearchDropTargetBar != null) {
3435 mSearchDropTargetBar.hideSearchBar(false);
3436 }
Michael Jurkaabded662011-03-04 12:06:57 -08003437 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003438 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003439 dispatchOnLauncherTransitionStart(fromView, animated, false);
3440 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003441 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003442 dispatchOnLauncherTransitionStart(toView, animated, false);
3443 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003444 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003445 }
3446
3447 /**
3448 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003449 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003450 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003451 */
Adam Cohened307df2013-10-02 09:37:31 -07003452 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003453 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003454
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003456 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457 mStateAnimation.cancel();
3458 mStateAnimation = null;
3459 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003460
Kenny Guyd794a3f2014-09-16 15:17:58 +01003461 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003462 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003463
Winson Chungf0ea4d32011-06-06 14:27:16 -07003464 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003465 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003466 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003467 final int itemsAlphaStagger =
3468 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003469
Winson Chungf0ea4d32011-06-06 14:27:16 -07003470 final float scaleFactor = (float)
3471 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3472 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003473 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003474 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003475 final ArrayList<View> layerViews = new ArrayList<View>();
3476
Adam Cohened307df2013-10-02 09:37:31 -07003477 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003478 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003479 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003480 } else if (toState == Workspace.State.SPRING_LOADED ||
3481 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003482 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003483 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003484 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003485
Adam Cohena38dc902014-09-07 17:48:55 +02003486 // If for some reason our views aren't initialized, don't animate
3487 boolean initialized = getAllAppsButton() != null;
3488
3489 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003491 if (workspaceAnim != null) {
3492 mStateAnimation.play(workspaceAnim);
3493 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003494
Adam Cohen9bfdb762014-07-21 17:44:06 -07003495 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3496 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003497
Adam Cohen9bfdb762014-07-21 17:44:06 -07003498 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003499
3500 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3501 int count = content.getChildCount();
3502 for (int i = 0; i < count; i++) {
3503 View child = content.getChildAt(i);
3504 if (child != page) {
3505 child.setVisibility(View.INVISIBLE);
3506 }
3507 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003508 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003509
Adam Cohen2854d252014-08-27 16:04:07 -07003510 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3511 // don't perform all these no-op animations. In particularly, this was causing
3512 // the all-apps button to pop in and out.
3513 if (fromView.getVisibility() == View.VISIBLE) {
3514 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3515 final boolean isWidgetTray =
3516 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003517
Adam Cohen2854d252014-08-27 16:04:07 -07003518 if (isWidgetTray) {
3519 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3520 } else {
3521 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003522 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003523
Adam Cohen2854d252014-08-27 16:04:07 -07003524 int width = revealView.getMeasuredWidth();
3525 int height = revealView.getMeasuredHeight();
3526 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3527
3528 // Hide the real page background, and swap in the fake one
3529 revealView.setVisibility(View.VISIBLE);
3530 content.setPageBackgroundsVisible(false);
3531
3532 final View allAppsButton = getAllAppsButton();
3533 revealView.setTranslationY(0);
3534 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3535 allAppsButton, null);
3536
3537 float xDrift = 0;
3538 float yDrift = 0;
3539 if (material) {
3540 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3541 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3542 } else {
3543 yDrift = 5 * height / 4;
3544 xDrift = 0;
3545 }
3546
3547 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3548 TimeInterpolator decelerateInterpolator = material ?
3549 new LogDecelerateInterpolator(100, 0) :
3550 new LogDecelerateInterpolator(30, 0);
3551
3552 // The vertical motion of the apps panel should be delayed by one frame
3553 // from the conceal animation in order to give the right feel. We correpsondingly
3554 // shorten the duration so that the slide and conceal end at the same time.
3555 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3556 0, yDrift);
3557 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3558 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3559 panelDriftY.setInterpolator(decelerateInterpolator);
3560 mStateAnimation.play(panelDriftY);
3561
3562 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3563 0, xDrift);
3564 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3565 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3566 panelDriftX.setInterpolator(decelerateInterpolator);
3567 mStateAnimation.play(panelDriftX);
3568
3569 if (isWidgetTray || !material) {
3570 float finalAlpha = material ? 0.4f : 0f;
3571 revealView.setAlpha(1f);
3572 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3573 1f, finalAlpha);
3574 panelAlpha.setDuration(revealDuration);
3575 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3576 new AccelerateInterpolator(1.5f));
3577 mStateAnimation.play(panelAlpha);
3578 }
3579
3580 if (page != null) {
3581 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3582
3583 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3584 0, yDrift);
3585 page.setTranslationY(0);
3586 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3587 pageDrift.setInterpolator(decelerateInterpolator);
3588 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3589 mStateAnimation.play(pageDrift);
3590
3591 page.setAlpha(1f);
3592 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3593 itemsAlpha.setDuration(100);
3594 itemsAlpha.setInterpolator(decelerateInterpolator);
3595 mStateAnimation.play(itemsAlpha);
3596 }
3597
3598 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3599 pageIndicators.setAlpha(1f);
3600 ObjectAnimator indicatorsAlpha =
3601 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3602 indicatorsAlpha.setDuration(revealDuration);
3603 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3604 mStateAnimation.play(indicatorsAlpha);
3605
3606 width = revealView.getMeasuredWidth();
3607
3608 if (material) {
3609 if (!isWidgetTray) {
3610 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003611 }
Adam Cohen2854d252014-08-27 16:04:07 -07003612 int allAppsButtonSize = LauncherAppState.getInstance().
3613 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3614 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3615 Animator reveal =
3616 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3617 height / 2, revealRadius, finalRadius);
3618 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3619 reveal.setDuration(revealDuration);
3620 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003621
Adam Cohen2854d252014-08-27 16:04:07 -07003622 reveal.addListener(new AnimatorListenerAdapter() {
3623 public void onAnimationEnd(Animator animation) {
3624 revealView.setVisibility(View.INVISIBLE);
3625 if (!isWidgetTray) {
3626 allAppsButton.setVisibility(View.VISIBLE);
3627 }
3628 }
3629 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003630
Adam Cohen2854d252014-08-27 16:04:07 -07003631 mStateAnimation.play(reveal);
3632 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003633
Adam Cohen2854d252014-08-27 16:04:07 -07003634 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3635 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3636 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003637 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003638
3639 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003640 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003641 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003642 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003643 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3644 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003645 if (onCompleteRunnable != null) {
3646 onCompleteRunnable.run();
3647 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003648
3649 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003650 if (page != null) {
3651 page.setLayerType(View.LAYER_TYPE_NONE, null);
3652 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003653 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003654 // Unhide side pages
3655 int count = content.getChildCount();
3656 for (int i = 0; i < count; i++) {
3657 View child = content.getChildAt(i);
3658 child.setVisibility(View.VISIBLE);
3659 }
3660
3661 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003662 if (page != null) {
3663 page.setTranslationX(0);
3664 page.setTranslationY(0);
3665 page.setAlpha(1);
3666 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003667 content.setCurrentPage(content.getNextPage());
3668
Chet Haasebc2f0822012-10-26 17:59:53 -07003669 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003670 }
3671 });
3672
Adam Cohen2854d252014-08-27 16:04:07 -07003673 final AnimatorSet stateAnimation = mStateAnimation;
3674 final Runnable startAnimRunnable = new Runnable() {
3675 public void run() {
3676 // Check that mStateAnimation hasn't changed while
3677 // we waited for a layout/draw pass
3678 if (mStateAnimation != stateAnimation)
3679 return;
3680 dispatchOnLauncherTransitionStart(fromView, animated, false);
3681 dispatchOnLauncherTransitionStart(toView, animated, false);
3682
Kenny Guyd794a3f2014-09-16 15:17:58 +01003683 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003684 for (int i = 0; i < layerViews.size(); i++) {
3685 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003686 if (v != null) {
3687 boolean attached = true;
3688 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3689 attached = v.isAttachedToWindow();
3690 }
3691 if (attached) v.buildLayer();
3692 }
Adam Cohen2854d252014-08-27 16:04:07 -07003693 }
3694 }
3695 mStateAnimation.start();
3696 }
3697 };
3698 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003699 } else {
3700 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003701 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003702 dispatchOnLauncherTransitionStart(fromView, animated, true);
3703 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003704 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003705 dispatchOnLauncherTransitionStart(toView, animated, true);
3706 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003707 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003708 }
3709
Michael Jurkae326f182011-11-21 14:05:46 -08003710 @Override
3711 public void onTrimMemory(int level) {
3712 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003713 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003714 mAppsCustomizeTabHost.onTrimMemory();
3715 }
3716 }
3717
Adam Cohened307df2013-10-02 09:37:31 -07003718 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003719 showWorkspace(animated, null);
3720 }
3721
Adam Cohened307df2013-10-02 09:37:31 -07003722 protected void showWorkspace() {
3723 showWorkspace(true);
3724 }
3725
Adam Cohened66b2b2012-01-23 17:28:51 -08003726 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003727 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003728 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003729 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003730 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003731
Winson Chungc7d2b602012-05-16 17:02:20 -07003732 // Show the search bar (only animate if we were showing the drop target bar in spring
3733 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003734 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003735 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003736 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003737
Michael Jurkab3e22d92011-10-31 15:58:33 -07003738 // Set focus to the AppsCustomize button
3739 if (mAllAppsButton != null) {
3740 mAllAppsButton.requestFocus();
3741 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003742 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003743
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003744 // Change the state *after* we've called all the transition code
3745 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003746
Winson Chung5fb63472011-02-02 17:03:37 -08003747 // Resume the auto-advance of widgets
3748 mUserPresent = true;
3749 updateRunning();
3750
alanv1d4fde62012-10-17 13:15:47 -07003751 // Send an accessibility event to announce the context change
3752 getWindow().getDecorView()
3753 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003754
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003755 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003756 }
3757
Adam Cohened307df2013-10-02 09:37:31 -07003758 void showOverviewMode(boolean animated) {
3759 mWorkspace.setVisibility(View.VISIBLE);
3760 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3761 mState = State.WORKSPACE;
3762 onWorkspaceShown(animated);
3763 }
3764
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003765 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003766 }
3767
Winson Chung82963d52013-10-09 11:20:57 -07003768 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3769 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003770 if (mState != State.WORKSPACE) return;
3771
Winson Chung82963d52013-10-09 11:20:57 -07003772 if (resetPageToZero) {
3773 mAppsCustomizeTabHost.reset();
3774 }
Winson Chungc58497e2013-09-03 17:48:37 -07003775 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003776 mAppsCustomizeTabHost.post(new Runnable() {
3777 @Override
3778 public void run() {
3779 // We post this in-case the all apps view isn't yet constructed.
3780 mAppsCustomizeTabHost.requestFocus();
3781 }
3782 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003783
Michael Jurkab3e22d92011-10-31 15:58:33 -07003784 // Change the state *after* we've called all the transition code
3785 mState = State.APPS_CUSTOMIZE;
3786
3787 // Pause the auto-advance of widgets until we are out of AllApps
3788 mUserPresent = false;
3789 updateRunning();
3790 closeFolder();
3791
3792 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003793 getWindow().getDecorView()
3794 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003795 }
3796
Adam Cohen7777d962011-08-18 18:58:38 -07003797 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003798 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003799 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003800 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003801 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003802 }
Adam Cohen7777d962011-08-18 18:58:38 -07003803
Adam Cohenad4e15c2013-10-17 16:21:35 -07003804 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003805 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003806 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3807
Winson Chunge7a03942011-08-05 15:05:12 -07003808 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003809 @Override
3810 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003811 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003812 // Before we show workspace, hide all apps again because
3813 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3814 // clean up our state transition functions
3815 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003816 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003817 } else {
3818 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003819 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003820 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003821 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003822 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003823
Michael Jurkad3ef3062010-11-23 16:23:58 -08003824 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003825 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003826 final boolean animated = true;
3827 final boolean springLoaded = true;
3828 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003829 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003830 }
3831 // Otherwise, we are not in spring loaded mode, so don't do anything.
3832 }
3833
Michael Jurkab3e22d92011-10-31 15:58:33 -07003834 void lockAllApps() {
3835 // TODO
3836 }
3837
3838 void unlockAllApps() {
3839 // TODO
3840 }
3841
Winson Chungf0ea4d32011-06-06 14:27:16 -07003842 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003843 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003844 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003845 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003846 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003847 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003848 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003849 int duration = 0;
3850 if (mSearchDropTargetBar != null) {
3851 duration = mSearchDropTargetBar.getTransitionOutDuration();
3852 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003853 mHotseat.animate().alpha(0f).setDuration(duration);
3854 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003855 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003856 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003857 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003858 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003859 }
3860
Patrick Dubroy5f445422011-02-18 14:35:21 -08003861 /**
3862 * Add an item from all apps or customize onto the given workspace screen.
3863 * If layout is null, add to the current screen.
3864 */
3865 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003866 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003867 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003868 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003869 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003870
Winson Chungdff8ebb2011-09-08 17:25:31 -07003871 /** Maps the current orientation to an index for referencing orientation correct global icons */
3872 private int getCurrentOrientationIndexForGlobalIcons() {
3873 // default - 0, landscape - 1
3874 switch (getResources().getConfiguration().orientation) {
3875 case Configuration.ORIENTATION_LANDSCAPE:
3876 return 1;
3877 default:
3878 return 0;
3879 }
3880 }
3881
Michael Jurkaae65ee32012-04-30 11:45:54 -07003882 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003883 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003884 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003885 // Look for the toolbar icon specified in the activity meta-data
3886 Bundle metaData = packageManager.getActivityInfo(
3887 activityName, PackageManager.GET_META_DATA).metaData;
3888 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003889 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003890 if (iconResId != 0) {
3891 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003892 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003893 }
3894 }
3895 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003896 // This can happen if the activity defines an invalid drawable
3897 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3898 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003899 } catch (Resources.NotFoundException nfe) {
3900 // This can happen if the activity defines an invalid drawable
3901 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3902 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003903 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003904 return null;
3905 }
3906
3907 // if successful in getting icon, return it; otherwise, set button to use default drawable
3908 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003909 int buttonId, ComponentName activityName, int fallbackDrawableId,
3910 String toolbarResourceName) {
3911 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003912 Resources r = getResources();
3913 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3914 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003915
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003916 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003917 // If we were unable to find the icon via the meta-data, use a generic one
3918 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003919 toolbarIcon = r.getDrawable(fallbackDrawableId);
3920 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003921 if (button != null) {
3922 button.setCompoundDrawables(toolbarIcon, null, null, null);
3923 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003924 return null;
3925 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003926 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003927 if (button != null) {
3928 button.setCompoundDrawables(toolbarIcon, null, null, null);
3929 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003930 return toolbarIcon.getConstantState();
3931 }
3932 }
3933
3934 // if successful in getting icon, return it; otherwise, set button to use default drawable
3935 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003936 int buttonId, ComponentName activityName, int fallbackDrawableId,
3937 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003938 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003939 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003940
Michael Jurka19e0fc52011-07-22 18:00:21 -07003941 if (button != null) {
3942 // If we were unable to find the icon via the meta-data, use a
3943 // generic one
3944 if (toolbarIcon == null) {
3945 button.setImageResource(fallbackDrawableId);
3946 } else {
3947 button.setImageDrawable(toolbarIcon);
3948 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003949 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003950
3951 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3952
Michael Jurka0423dcf2010-10-05 14:56:18 -07003953 }
3954
Winson Chung1b884092012-06-08 17:13:02 -07003955 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003956 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003957 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003958 }
3959
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003960 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003961 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003962 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003963 }
3964
Winson Chungbb185bd2011-11-21 12:31:42 -08003965 private void invalidatePressedFocusedStates(View container, View button) {
3966 if (container instanceof HolographicLinearLayout) {
3967 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3968 layout.invalidatePressedFocusedStates();
3969 } else if (button instanceof HolographicImageView) {
3970 HolographicImageView view = (HolographicImageView) button;
3971 view.invalidatePressedFocusedStates();
3972 }
3973 }
3974
Cristina Stancu476493b2013-08-07 17:20:14 +01003975 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003976 if (mQsb == null) {
3977 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3978 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003979 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003980 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003981 }
3982
3983 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003984 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003985 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003986 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003987 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003988
Winson Chung1cad91e2011-05-25 17:41:01 -07003989 final SearchManager searchManager =
3990 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3991 ComponentName activityName = searchManager.getGlobalSearchActivity();
3992 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003993 int coi = getCurrentOrientationIndexForGlobalIcons();
3994 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003995 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3996 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3997 if (sGlobalSearchIcon[coi] == null) {
3998 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3999 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4000 TOOLBAR_ICON_METADATA_NAME);
4001 }
4002
Winson Chungc51db6a2011-10-05 11:44:49 -07004003 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4004 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004005 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004006 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004007 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004008 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004009 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4010 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004011 if (searchButton != null) searchButton.setVisibility(View.GONE);
4012 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004013 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004014 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004015 }
4016 }
4017
Cristina Stancu476493b2013-08-07 17:20:14 +01004018 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004019 final View searchButtonContainer = findViewById(R.id.search_button_container);
4020 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004021 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004022 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004023 }
4024
Cristina Stancu476493b2013-08-07 17:20:14 +01004025 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004026 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004027 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004028
Winson Chungc51db6a2011-10-05 11:44:49 -07004029 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004030 final SearchManager searchManager =
4031 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4032 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4033
4034 ComponentName activityName = null;
4035 if (globalSearchActivity != null) {
4036 // Check if the global search activity handles voice search
4037 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4038 intent.setPackage(globalSearchActivity.getPackageName());
4039 activityName = intent.resolveActivity(getPackageManager());
4040 }
4041
4042 if (activityName == null) {
4043 // Fallback: check if an activity other than the global search activity
4044 // resolves this
4045 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4046 activityName = intent.resolveActivity(getPackageManager());
4047 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004048 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004049 int coi = getCurrentOrientationIndexForGlobalIcons();
4050 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004051 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4052 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4053 if (sVoiceSearchIcon[coi] == null) {
4054 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4055 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4056 TOOLBAR_ICON_METADATA_NAME);
4057 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004058 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004059 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004060 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004061 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004062 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004063 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004064 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004065 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004066 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004067 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004068 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004069 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004070
Cristina Stancu476493b2013-08-07 17:20:14 +01004071 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004072 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4073 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004074 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004075 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004076 }
4077
Winson Chung5841efa2013-09-30 18:06:44 -07004078 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004079 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4080 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004081 boolean visible = !forceDisableVoiceButtonProxy &&
4082 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004083 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004084 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004085 }
4086 }
Winson Chung5841efa2013-09-30 18:06:44 -07004087
4088 /**
4089 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4090 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4091 */
4092 public void disableVoiceButtonProxy(boolean disabled) {
4093 updateVoiceButtonProxyVisible(disabled);
4094 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004095
Michael Jurkad7c28052012-04-27 15:43:36 -07004096 @Override
4097 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004098 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004099 final List<CharSequence> text = event.getText();
4100 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004101 // Populate event with a fake title based on the current state.
4102 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004103 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004104 } else {
4105 text.add(getString(R.string.all_apps_home_button_label));
4106 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004107 return result;
4108 }
4109
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004110 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004111 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004112 */
4113 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4114 @Override
4115 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004116 closeSystemDialogs();
4117 }
4118 }
4119
4120 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004121 * Receives notifications whenever the appwidgets are reset.
4122 */
4123 private class AppWidgetResetObserver extends ContentObserver {
4124 public AppWidgetResetObserver() {
4125 super(new Handler());
4126 }
4127
4128 @Override
4129 public void onChange(boolean selfChange) {
4130 onAppWidgetReset();
4131 }
4132 }
4133
4134 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004135 * If the activity is currently paused, signal that we need to run the passed Runnable
4136 * in onResume.
4137 *
4138 * This needs to be called from incoming places where resources might have been loaded
4139 * while we are paused. That is becaues the Configuration might be wrong
4140 * when we're not running, and if it comes back to what it was when we
4141 * were paused, we are not restarted.
4142 *
4143 * Implementation of the method from LauncherModel.Callbacks.
4144 *
4145 * @return true if we are currently paused. The caller might be able to
4146 * skip some work in that case since we will come back again.
4147 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004148 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004149 if (mPaused) {
4150 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004151 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004152 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004153 }
4154 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004155 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004156 return true;
4157 } else {
4158 return false;
4159 }
4160 }
4161
Michael Jurkac402cd92013-05-20 15:49:32 +02004162 private boolean waitUntilResume(Runnable run) {
4163 return waitUntilResume(run, false);
4164 }
4165
Michael Jurka1e2f4652013-07-08 18:03:46 -07004166 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004167 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004168 }
4169
Michael Jurka7607c2f2013-04-03 14:33:19 -07004170 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004171 * If the activity is currently paused, signal that we need to re-run the loader
4172 * in onResume.
4173 *
4174 * This needs to be called from incoming places where resources might have been loaded
4175 * while we are paused. That is becaues the Configuration might be wrong
4176 * when we're not running, and if it comes back to what it was when we
4177 * were paused, we are not restarted.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 *
4181 * @return true if we are currently paused. The caller might be able to
4182 * skip some work in that case since we will come back again.
4183 */
4184 public boolean setLoadOnResume() {
4185 if (mPaused) {
4186 Log.i(TAG, "setLoadOnResume");
4187 mOnResumeNeedsLoad = true;
4188 return true;
4189 } else {
4190 return false;
4191 }
4192 }
4193
4194 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004195 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004196 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004197 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004198 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004199 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004200 } else {
4201 return SCREEN_COUNT / 2;
4202 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004203 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004204
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 /**
4206 * Refreshes the shortcuts shown on the workspace.
4207 *
4208 * Implementation of the method from LauncherModel.Callbacks.
4209 */
4210 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004211 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004212
Michael Jurka7607c2f2013-04-03 14:33:19 -07004213 // If we're starting binding all over again, clear any bind calls we'd postponed in
4214 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4215 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004216 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004217
Winson Chungd64d1762013-08-20 14:37:16 -07004218 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004219 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004220 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004221
Michael Jurka05bf6442011-11-30 20:28:53 -08004222 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004223 if (mHotseat != null) {
4224 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004225 }
4226 }
4227
Adam Cohendcd297f2013-06-18 13:13:40 -07004228 @Override
4229 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004230 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004231
Adam Cohen5084cba2013-09-03 12:01:16 -07004232 // If there are no screens, we need to have an empty screen
4233 if (orderedScreenIds.size() == 0) {
4234 mWorkspace.addExtraEmptyScreen();
4235 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004236
4237 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004238 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004239 if (hasCustomContentToLeft()) {
4240 mWorkspace.createCustomContentContainer();
4241 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004242 }
Winson Chung64359a52013-07-08 17:17:08 -07004243 }
4244
4245 @Override
4246 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004247 // Log to disk
4248 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4249 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4250 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004251 int count = orderedScreenIds.size();
4252 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004253 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004254 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004255 }
4256
Adam Cohen39a06042013-07-19 14:30:12 -07004257 private boolean shouldShowWeightWatcher() {
4258 String spKey = LauncherAppState.getSharedPreferencesKey();
4259 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004260 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004261
4262 return show;
4263 }
4264
4265 private void toggleShowWeightWatcher() {
4266 String spKey = LauncherAppState.getSharedPreferencesKey();
4267 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4268 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4269
4270 show = !show;
4271
4272 SharedPreferences.Editor editor = sp.edit();
4273 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4274 editor.commit();
4275
4276 if (mWeightWatcher != null) {
4277 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4278 }
4279 }
4280
Winson Chungd64d1762013-08-20 14:37:16 -07004281 public void bindAppsAdded(final ArrayList<Long> newScreens,
4282 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004283 final ArrayList<ItemInfo> addAnimated,
4284 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004285 Runnable r = new Runnable() {
4286 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004287 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004288 }
4289 };
4290 if (waitUntilResume(r)) {
4291 return;
4292 }
4293
Winson Chungd64d1762013-08-20 14:37:16 -07004294 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004295 if (newScreens != null) {
4296 bindAddScreens(newScreens);
4297 }
Winson Chungd64d1762013-08-20 14:37:16 -07004298
4299 // We add the items without animation on non-visible pages, and with
4300 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004301 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004302 bindItems(addNotAnimated, 0,
4303 addNotAnimated.size(), false);
4304 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004305 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004306 bindItems(addAnimated, 0,
4307 addAnimated.size(), true);
4308 }
Winson Chungc58497e2013-09-03 17:48:37 -07004309
Winson Chung87412982013-10-03 18:34:14 -07004310 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004311 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004312
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004313 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004314 addedApps != null && mAppsCustomizeContent != null) {
4315 mAppsCustomizeContent.addApps(addedApps);
4316 }
Winson Chungd64d1762013-08-20 14:37:16 -07004317 }
4318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004319 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 * Bind the items start-end from the list.
4321 *
4322 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004323 */
Winson Chung64359a52013-07-08 17:17:08 -07004324 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4325 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004326 Runnable r = new Runnable() {
4327 public void run() {
4328 bindItems(shortcuts, start, end, forceAnimateIcons);
4329 }
4330 };
4331 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004332 return;
4333 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004334
Winson Chungf0c6ae02012-03-21 16:10:31 -07004335 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004336 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4337 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004338 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004339 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004340 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004341 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004342 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004343
4344 // Short circuit if we are loading dock items for a configuration which has no dock
4345 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4346 mHotseat == null) {
4347 continue;
4348 }
4349
Joe Onorato9c1289c2009-08-17 11:03:03 -04004350 switch (item.itemType) {
4351 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4352 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004353 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004354 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004355
Winson Chung64359a52013-07-08 17:17:08 -07004356 /*
4357 * TODO: FIX collision case
4358 */
Winson Chungce376632013-07-11 16:12:41 -07004359 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4360 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4361 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004362 View v = cl.getChildAt(item.cellX, item.cellY);
4363 Object tag = v.getTag();
4364 String desc = "Collision while binding workspace item: " + item
4365 + ". Collides with " + tag;
4366 if (LauncherAppState.isDogfoodBuild()) {
4367 throw (new RuntimeException(desc));
4368 } else {
4369 Log.d(TAG, desc);
4370 }
Winson Chungce376632013-07-11 16:12:41 -07004371 }
Winson Chung64359a52013-07-08 17:17:08 -07004372 }
4373
Adam Cohendcd297f2013-06-18 13:13:40 -07004374 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4375 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004376 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004377 // Animate all the applications up now
4378 shortcut.setAlpha(0f);
4379 shortcut.setScaleX(0f);
4380 shortcut.setScaleY(0f);
4381 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004382 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004384 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004385 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004386 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004387 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004388 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004389 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4390 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004391 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004392 default:
4393 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004395 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004396
Winson Chung997a9232013-07-24 15:33:46 -07004397 if (animateIcons) {
4398 // Animate to the correct page
4399 if (newShortcutsScreenId > -1) {
4400 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004401 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004402 final Runnable startBounceAnimRunnable = new Runnable() {
4403 public void run() {
4404 anim.playTogether(bounceAnims);
4405 anim.start();
4406 }
4407 };
Winson Chung997a9232013-07-24 15:33:46 -07004408 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004409 // We post the animation slightly delayed to prevent slowdowns
4410 // when we are loading right after we return to launcher.
4411 mWorkspace.postDelayed(new Runnable() {
4412 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004413 if (mWorkspace != null) {
4414 mWorkspace.snapToPage(newScreenIndex);
4415 mWorkspace.postDelayed(startBounceAnimRunnable,
4416 NEW_APPS_ANIMATION_DELAY);
4417 }
Winson Chung94d67682013-09-25 16:29:40 -07004418 }
4419 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004420 } else {
4421 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004422 }
4423 }
Winson Chung64359a52013-07-08 17:17:08 -07004424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004425 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004426 }
4427
Joe Onorato9c1289c2009-08-17 11:03:03 -04004428 /**
4429 * Implementation of the method from LauncherModel.Callbacks.
4430 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004431 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004432 Runnable r = new Runnable() {
4433 public void run() {
4434 bindFolders(folders);
4435 }
4436 };
4437 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004438 return;
4439 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004440 sFolders.clear();
4441 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004442 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004443
4444 /**
4445 * Add the views for a widget to the workspace.
4446 *
4447 * Implementation of the method from LauncherModel.Callbacks.
4448 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004449 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004450 Runnable r = new Runnable() {
4451 public void run() {
4452 bindAppWidget(item);
4453 }
4454 };
4455 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004456 return;
4457 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004458
Daniel Sandler843e8602010-06-07 14:59:01 -04004459 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4460 if (DEBUG_WIDGETS) {
4461 Log.d(TAG, "bindAppWidget: " + item);
4462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004463 final Workspace workspace = mWorkspace;
4464
Sunny Goyalff572272014-07-23 13:58:07 -07004465 AppWidgetProviderInfo appWidgetInfo;
4466 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4467 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4468
4469 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4470 if (appWidgetInfo == null) {
4471 if (DEBUG_WIDGETS) {
4472 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4473 + " belongs to component " + item.providerName
4474 + ", as the povider is null");
4475 }
4476 LauncherModel.deleteItemFromDatabase(this, item);
4477 return;
4478 }
4479 // Note: This assumes that the id remap broadcast is received before this step.
4480 // If that is not the case, the id remap will be ignored and user may see the
4481 // click to setup view.
4482 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4483 pendingInfo.spanX = item.spanX;
4484 pendingInfo.spanY = item.spanY;
4485 pendingInfo.minSpanX = item.minSpanX;
4486 pendingInfo.minSpanY = item.minSpanY;
4487 Bundle options =
4488 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4489
Sunny Goyalff572272014-07-23 13:58:07 -07004490 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004491 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4492 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004493
4494 // TODO consider showing a permission dialog when the widget is clicked.
4495 if (!success) {
4496 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4497 if (DEBUG_WIDGETS) {
4498 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4499 + " belongs to component " + item.providerName
4500 + ", as the launcher is unable to bing a new widget id");
4501 }
4502 LauncherModel.deleteItemFromDatabase(this, item);
4503 return;
4504 }
4505
4506 item.appWidgetId = newWidgetId;
4507
4508 // If the widget has a configure activity, it is still needs to set it up, otherwise
4509 // the widget is ready to go.
4510 item.restoreStatus = (appWidgetInfo.configure == null)
4511 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4512 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4513
4514 LauncherModel.updateItemInDatabase(this, item);
4515 }
4516
Sunny Goyal651077b2014-06-30 14:15:31 -07004517 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4518 final int appWidgetId = item.appWidgetId;
4519 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4520 if (DEBUG_WIDGETS) {
4521 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4522 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004523
Sunny Goyal651077b2014-06-30 14:15:31 -07004524 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4525 } else {
4526 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004527 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4528 view.updateIcon(mIconCache);
4529 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004530 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004531 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004532 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004533
Joe Onorato9c1289c2009-08-17 11:03:03 -04004534 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004535 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004536
Adam Cohendcd297f2013-06-18 13:13:40 -07004537 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004538 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004539 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4540
Joe Onorato9c1289c2009-08-17 11:03:03 -04004541 workspace.requestLayout();
4542
Daniel Sandler843e8602010-06-07 14:59:01 -04004543 if (DEBUG_WIDGETS) {
4544 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4545 + (SystemClock.uptimeMillis()-start) + "ms");
4546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004547 }
4548
Sunny Goyalff572272014-07-23 13:58:07 -07004549 /**
4550 * Restores a pending widget.
4551 *
4552 * @param appWidgetId The app widget id
4553 * @param cellInfo The position on screen where to create the widget.
4554 */
4555 private void completeRestoreAppWidget(final int appWidgetId) {
4556 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4557 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4558 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4559 return;
4560 }
4561
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004562 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004563 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4564
4565 mWorkspace.reinflateWidgetsIfNecessary();
4566 LauncherModel.updateItemInDatabase(this, info);
4567 }
4568
Adam Cohen1462de32012-07-24 22:34:36 -07004569 public void onPageBoundSynchronously(int page) {
4570 mSynchronouslyBoundPages.add(page);
4571 }
4572
Joe Onorato9c1289c2009-08-17 11:03:03 -04004573 /**
4574 * Callback saying that there aren't any more items to bind.
4575 *
4576 * Implementation of the method from LauncherModel.Callbacks.
4577 */
Adam Cohene25af792013-06-06 23:08:25 -07004578 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004579 Runnable r = new Runnable() {
4580 public void run() {
4581 finishBindingItems(upgradePath);
4582 }
4583 };
4584 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004585 return;
4586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004587 if (mSavedState != null) {
4588 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004589 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004591 mSavedState = null;
4592 }
4593
Adam Cohen1462de32012-07-24 22:34:36 -07004594 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004595
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004596 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004597 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004598
4599 // If we received the result of any pending adds while the loader was running (e.g. the
4600 // widget configuration forced an orientation change), process them now.
4601 if (sPendingAddItem != null) {
4602 final long screenId = completeAdd(sPendingAddItem);
4603
4604 // TODO: this moves the user to the page where the pending item was added. Ideally,
4605 // the screen would be guaranteed to exist after bind, and the page would be set through
4606 // the workspace restore process.
4607 mWorkspace.post(new Runnable() {
4608 @Override
4609 public void run() {
4610 mWorkspace.snapToScreenId(screenId);
4611 }
4612 });
4613 sPendingAddItem = null;
4614 }
4615
Adam Cohene25af792013-06-06 23:08:25 -07004616 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004617 mWorkspace.getUniqueComponents(true, null);
4618 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004619 }
Sunny Goyale755d462014-07-22 13:48:29 -07004620 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004621 }
4622
Adam Cohen3ed316a2014-07-23 18:21:20 -07004623 private void sendLoadingCompleteBroadcastIfNecessary() {
4624 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4625 String permission =
4626 getResources().getString(R.string.receive_first_load_broadcast_permission);
4627 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4628 sendBroadcast(intent, permission);
4629 SharedPreferences.Editor editor = mSharedPrefs.edit();
4630 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4631 editor.apply();
4632 }
4633 }
4634
Winson Chunga0b7e862013-09-05 16:03:15 -07004635 public boolean isAllAppsButtonRank(int rank) {
4636 if (mHotseat != null) {
4637 return mHotseat.isAllAppsButtonRank(rank);
4638 }
4639 return false;
4640 }
4641
Winson Chunga2413752012-04-03 14:22:34 -07004642 private boolean canRunNewAppsAnimation() {
4643 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4644 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4645 }
4646
Winson Chungc9168342013-06-26 14:54:55 -07004647 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4648 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4649 PropertyValuesHolder.ofFloat("alpha", 1f),
4650 PropertyValuesHolder.ofFloat("scaleX", 1f),
4651 PropertyValuesHolder.ofFloat("scaleY", 1f));
4652 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4653 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4654 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4655 return bounceAnim;
4656 }
4657
Adam Cohen27772732013-11-11 14:00:56 +00004658 public boolean useVerticalBarLayout() {
4659 return LauncherAppState.getInstance().getDynamicGrid().
4660 getDeviceProfile().isVerticalBarLayout();
4661 }
4662
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004663 protected Rect getSearchBarBounds() {
4664 return LauncherAppState.getInstance().getDynamicGrid().
4665 getDeviceProfile().getSearchBarBounds();
4666 }
4667
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004668 @Override
4669 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004670 boolean searchVisible = updateGlobalSearchIcon();
4671 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004672 if (mSearchDropTargetBar != null) {
4673 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4674 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004675 }
4676
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004677 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004678 * Add the icons for all apps.
4679 *
4680 * Implementation of the method from LauncherModel.Callbacks.
4681 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004682 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004683 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004684 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4685 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4686 getHotseat().addAllAppsFolder(mIconCache, apps,
4687 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4688 }
4689 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004690 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004691 if (mAppsCustomizeContent != null) {
4692 mAppsCustomizeContent.onPackagesUpdated(
4693 LauncherModel.getSortedWidgetsAndShortcuts(this));
4694 }
Winson Chungc58497e2013-09-03 17:48:37 -07004695 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004696 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004697 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004698 mAppsCustomizeContent.onPackagesUpdated(
4699 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004700 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004701 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004702 }
4703
4704 /**
4705 * A package was updated.
4706 *
4707 * Implementation of the method from LauncherModel.Callbacks.
4708 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004709 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004710 Runnable r = new Runnable() {
4711 public void run() {
4712 bindAppsUpdated(apps);
4713 }
4714 };
4715 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004716 return;
4717 }
4718
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004719 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004720 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004721 }
Winson Chungc58497e2013-09-03 17:48:37 -07004722
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004723 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004724 mAppsCustomizeContent != null) {
4725 mAppsCustomizeContent.updateApps(apps);
4726 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004727 }
4728
4729 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004730 * Some shortcuts were updated in the background.
4731 *
4732 * Implementation of the method from LauncherModel.Callbacks.
4733 */
4734 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4735 Runnable r = new Runnable() {
4736 public void run() {
4737 bindShortcutsUpdated(shortcuts);
4738 }
4739 };
4740 if (waitUntilResume(r)) {
4741 return;
4742 }
4743
4744 if (mWorkspace != null) {
4745 mWorkspace.updateShortcuts(shortcuts);
4746 }
4747 }
4748
4749 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004750 * Update the state of a package, typically related to install state.
4751 *
4752 * Implementation of the method from LauncherModel.Callbacks.
4753 */
Sunny Goyale755d462014-07-22 13:48:29 -07004754 @Override
4755 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004756 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004757 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004758 }
4759 }
4760
4761 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004762 * Update the label and icon of all the icons in a package
4763 *
4764 * Implementation of the method from LauncherModel.Callbacks.
4765 */
4766 @Override
4767 public void updatePackageBadge(String packageName) {
4768 if (mWorkspace != null) {
4769 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4770 }
4771 }
4772
4773 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004774 * A package was uninstalled. We take both the super set of packageNames
4775 * in addition to specific applications to remove, the reason being that
4776 * this can be called when a package is updated as well. In that scenario,
4777 * we only remove specific components from the workspace, where as
4778 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004779 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004780 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004781 * Implementation of the method from LauncherModel.Callbacks.
4782 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004783 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004784 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004785 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004786 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004787 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004788 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004789 }
Winson Chungd64d1762013-08-20 14:37:16 -07004790 };
4791 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004792 return;
4793 }
4794
Sunny Goyal1a745e82014-10-02 15:58:31 -07004795 if (reason == 0) {
4796 if (!packageNames.isEmpty()) {
4797 mWorkspace.removeItemsByPackageName(packageNames, user);
4798 }
4799 if (!appInfos.isEmpty()) {
4800 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
4801 }
4802 } else {
4803 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004804 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004805
Winson Chunga1820962011-10-03 16:31:06 -07004806 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004807 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004808
Winson Chungdf95eb12013-10-16 14:57:07 -07004809 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004810 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004811 mAppsCustomizeContent != null) {
4812 mAppsCustomizeContent.removeApps(appInfos);
4813 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004814 }
Joe Onoratobe386092009-11-17 17:32:16 -08004815
4816 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004817 * A number of packages were updated.
4818 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004819 private ArrayList<Object> mWidgetsAndShortcuts;
4820 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004821 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004822 bindPackagesUpdated(mWidgetsAndShortcuts);
4823 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004824 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004825 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004826 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4827 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4828 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004829 return;
4830 }
4831
Winson Chung64359a52013-07-08 17:17:08 -07004832 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004833 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004834 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004835 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004836 }
4837
Winson Chung400438b2011-01-16 17:53:48 -08004838 private int mapConfigurationOriActivityInfoOri(int configOri) {
4839 final Display d = getWindowManager().getDefaultDisplay();
4840 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4841 switch (d.getRotation()) {
4842 case Surface.ROTATION_0:
4843 case Surface.ROTATION_180:
4844 // We are currently in the same basic orientation as the natural orientation
4845 naturalOri = configOri;
4846 break;
4847 case Surface.ROTATION_90:
4848 case Surface.ROTATION_270:
4849 // We are currently in the other basic orientation to the natural orientation
4850 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4851 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4852 break;
4853 }
4854
4855 int[] oriMap = {
4856 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4857 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4858 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4859 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4860 };
4861 // Since the map starts at portrait, we need to offset if this device's natural orientation
4862 // is landscape.
4863 int indexOffset = 0;
4864 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4865 indexOffset = 1;
4866 }
4867 return oriMap[(d.getRotation() + indexOffset) % 4];
4868 }
Adam Cohen446e9402011-09-15 18:21:21 -07004869
Winson Chung4b919f82012-05-01 10:44:08 -07004870 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004871 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004872 getResources().getBoolean(R.bool.allow_rotation);
4873 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004874 }
Winson Chung4b919f82012-05-01 10:44:08 -07004875 public void lockScreenOrientation() {
4876 if (isRotationEnabled()) {
4877 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4878 .getConfiguration().orientation));
4879 }
4880 }
4881 public void unlockScreenOrientation(boolean immediate) {
4882 if (isRotationEnabled()) {
4883 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004884 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004885 } else {
4886 mHandler.postDelayed(new Runnable() {
4887 public void run() {
4888 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4889 }
4890 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004891 }
Winson Chung4b919f82012-05-01 10:44:08 -07004892 }
Winson Chung400438b2011-01-16 17:53:48 -08004893 }
4894
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004895 /**
4896 * Called when the SearchBar hint should be changed.
4897 *
4898 * @param hint the hint to be displayed in the search bar.
4899 */
4900 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004901
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004902 }
4903
Winson Chunge43a1e72014-01-15 10:33:02 -08004904 protected boolean isLauncherPreinstalled() {
4905 PackageManager pm = getPackageManager();
4906 try {
4907 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4908 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4909 return true;
4910 } else {
4911 return false;
4912 }
4913 } catch (NameNotFoundException e) {
4914 e.printStackTrace();
4915 return false;
4916 }
4917 }
4918
Adam Cohenea90f832014-05-21 15:03:34 -07004919 /**
4920 * This method indicates whether or not we should suggest default wallpaper dimensions
4921 * when our wallpaper cropper was not yet used to set a wallpaper.
4922 */
4923 protected boolean overrideWallpaperDimensions() {
4924 return true;
4925 }
4926
Adam Cohen5eed5d82014-05-19 13:12:13 -07004927 protected boolean shouldClingFocusHotseatApp() {
4928 return false;
4929 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004930 protected String getFirstRunClingSearchBarHint() {
4931 return "";
4932 }
4933 protected String getFirstRunCustomContentHint() {
4934 return "";
4935 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004936 protected int getFirstRunFocusedHotseatAppDrawableId() {
4937 return -1;
4938 }
4939 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4940 return null;
4941 }
4942 protected int getFirstRunFocusedHotseatAppRank() {
4943 return -1;
4944 }
4945 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4946 return "";
4947 }
4948 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4949 return "";
4950 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004951
Adam Cohen432609a2014-03-13 17:03:22 -07004952 /**
4953 * To be overridden by subclasses to indicate that there is an activity to launch
4954 * before showing the standard launcher experience.
4955 */
4956 protected boolean hasFirstRunActivity() {
4957 return false;
4958 }
4959
4960 /**
4961 * To be overridden by subclasses to launch any first run activity
4962 */
4963 protected Intent getFirstRunActivity() {
4964 return null;
4965 }
4966
Winson Chunga6945242014-01-08 14:04:34 -08004967 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004968 return !ActivityManager.isRunningInTestHarness() &&
4969 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004970 }
4971
Adam Cohen4a9423d2014-03-28 14:22:31 -07004972 protected boolean hasRunFirstRunActivity() {
4973 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4974 }
4975
Adam Cohen432609a2014-03-13 17:03:22 -07004976 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004977 if (shouldRunFirstRunActivity() &&
4978 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004979 Intent firstRunIntent = getFirstRunActivity();
4980 if (firstRunIntent != null) {
4981 startActivity(firstRunIntent);
4982 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004983 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004984 }
4985 }
Adam Cohen432609a2014-03-13 17:03:22 -07004986 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004987 }
4988
4989 private void markFirstRunActivityShown() {
4990 SharedPreferences.Editor editor = mSharedPrefs.edit();
4991 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4992 editor.apply();
4993 }
4994
Adam Cohen432609a2014-03-13 17:03:22 -07004995 /**
4996 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4997 * screen that must be displayed and dismissed.
4998 */
4999 protected boolean hasDismissableIntroScreen() {
5000 return false;
5001 }
5002
5003 /**
5004 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5005 */
5006 protected View getIntroScreen() {
5007 return null;
5008 }
5009
5010 /**
5011 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5012 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5013 */
5014 private boolean shouldShowIntroScreen() {
5015 return hasDismissableIntroScreen() &&
5016 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5017 }
5018
5019 protected void showIntroScreen() {
5020 View introScreen = getIntroScreen();
5021 changeWallpaperVisiblity(false);
5022 if (introScreen != null) {
5023 mDragLayer.showOverlayView(introScreen);
5024 }
5025 }
5026
5027 public void dismissIntroScreen() {
5028 markIntroScreenDismissed();
5029 if (showFirstRunActivity()) {
5030 // We delay hiding the intro view until the first run activity is showing. This
5031 // avoids a blip.
5032 mWorkspace.postDelayed(new Runnable() {
5033 @Override
5034 public void run() {
5035 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005036 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005037 }
5038 }, ACTIVITY_START_DELAY);
5039 } else {
5040 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005041 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005042 }
5043 changeWallpaperVisiblity(true);
5044 }
5045
5046 private void markIntroScreenDismissed() {
5047 SharedPreferences.Editor editor = mSharedPrefs.edit();
5048 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5049 editor.apply();
5050 }
5051
Sunny Goyal88d60f12014-09-08 03:47:29 -07005052 private void showFirstRunClings() {
5053 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5054 // on the device, then we always show the first run cling experience (or if there is no
5055 // launcher2). Otherwise, we prompt the user upon started for migration
5056 LauncherClings launcherClings = new LauncherClings(this);
5057 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5058 if (mModel.canMigrateFromOldLauncherDb(this)) {
5059 launcherClings.showMigrationCling();
5060 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005061 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005062 }
5063 }
5064 }
5065
Winson Chunga6945242014-01-08 14:04:34 -08005066 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005067 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5068 if (mHotseat != null) mHotseat.setAlpha(1f);
5069 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5070 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005071 }
5072
5073 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005074 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5075 if (mHotseat != null) mHotseat.setAlpha(0f);
5076 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5077 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005078 }
5079
Mathew Inwood72fbec12013-11-19 15:45:07 +00005080 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005081 // Called from search suggestion, not supported in other profiles.
5082 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5083 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5084 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5085 myUser);
5086 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005087 return null;
5088 }
Kenny Guyed131872014-04-30 03:02:21 +01005089 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005090 }
5091
5092 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5093 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005094 // Called from search suggestion, not supported in other profiles.
5095 return createShortcutDragInfo(shortcutIntent, caption, icon,
5096 UserHandleCompat.myUserHandle());
5097 }
5098
5099 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5100 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005101 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005102 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005103 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005104 }
5105
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005106 protected void moveWorkspaceToDefaultScreen() {
5107 mWorkspace.moveToDefaultScreen(false);
5108 }
5109
Mathew Inwood72fbec12013-11-19 15:45:07 +00005110 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5111 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005112 mWorkspace.onExternalDragStartedWithItem(dragView);
5113 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005114 }
5115
Anjali Koppalf5d29132014-02-28 13:33:27 -08005116 @Override
5117 public void onPageSwitch(View newPage, int newPageIndex) {
5118 }
5119
Winson Chung80baf5a2010-08-09 16:03:15 -07005120 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005121 * Prints out out state for debugging.
5122 */
5123 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005124 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005125 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005126 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5127 Log.d(TAG, "mRestoring=" + mRestoring);
5128 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5129 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005130 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005131 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005132
Winson Chung785d2eb2011-04-14 16:08:02 -07005133 if (mAppsCustomizeContent != null) {
5134 mAppsCustomizeContent.dumpState();
5135 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005136 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005137 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005138
5139 @Override
5140 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5141 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005142 synchronized (sDumpLogs) {
5143 writer.println(" ");
5144 writer.println("Debug logs: ");
5145 for (int i = 0; i < sDumpLogs.size(); i++) {
5146 writer.println(" " + sDumpLogs.get(i));
5147 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005148 }
5149 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005150
5151 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005152 if (DEBUG_DUMP_LOG) {
5153 synchronized (sDumpLogs) {
5154 Log.d(TAG, "");
5155 Log.d(TAG, "*********************");
5156 Log.d(TAG, "Launcher debug logs: ");
5157 for (int i = 0; i < sDumpLogs.size(); i++) {
5158 Log.d(TAG, " " + sDumpLogs.get(i));
5159 }
5160 Log.d(TAG, "*********************");
5161 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005162 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005163 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005164 }
5165
5166 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005167 addDumpLog(tag, log, null, debugLog);
5168 }
5169
5170 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005171 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005172 if (e != null) {
5173 Log.d(tag, log, e);
5174 } else {
5175 Log.d(tag, log);
5176 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005177 }
Winson Chungede41292013-09-19 16:27:36 -07005178 if (DEBUG_DUMP_LOG) {
5179 sDateStamp.setTime(System.currentTimeMillis());
5180 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005181 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5182 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005183 }
Winson Chungede41292013-09-19 16:27:36 -07005184 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005185 }
5186
Winson Chungede41292013-09-19 16:27:36 -07005187 public void dumpLogsToLocalData() {
5188 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005189 new AsyncTask<Void, Void, Void>() {
5190 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005191 boolean success = false;
5192 sDateStamp.setTime(sRunStart);
5193 String FILENAME = sDateStamp.getMonth() + "-"
5194 + sDateStamp.getDay() + "_"
5195 + sDateStamp.getHours() + "-"
5196 + sDateStamp.getMinutes() + "_"
5197 + sDateStamp.getSeconds() + ".txt";
5198
5199 FileOutputStream fos = null;
5200 File outFile = null;
5201 try {
5202 outFile = new File(getFilesDir(), FILENAME);
5203 outFile.createNewFile();
5204 fos = new FileOutputStream(outFile);
5205 } catch (Exception e) {
5206 e.printStackTrace();
5207 }
5208 if (fos != null) {
5209 PrintWriter writer = new PrintWriter(fos);
5210
5211 writer.println(" ");
5212 writer.println("Debug logs: ");
5213 synchronized (sDumpLogs) {
5214 for (int i = 0; i < sDumpLogs.size(); i++) {
5215 writer.println(" " + sDumpLogs.get(i));
5216 }
5217 }
5218 writer.close();
5219 }
5220 try {
5221 if (fos != null) {
5222 fos.close();
5223 success = true;
5224 }
5225 } catch (IOException e) {
5226 e.printStackTrace();
5227 }
Michael Jurka43467462013-11-14 12:03:58 +01005228 return null;
Winson Chungede41292013-09-19 16:27:36 -07005229 }
Michael Jurka43467462013-11-14 12:03:58 +01005230 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005231 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005233}
Michael Jurka2763be32011-02-24 11:19:57 -08005234
Michael Jurkaabded662011-03-04 12:06:57 -08005235interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005236 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005237 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005238 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005239 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005240 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005241}
Dan Sandlerd5024042014-01-09 15:01:33 -05005242
5243interface DebugIntents {
5244 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5245 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005246}