blob: 3762c2f50b826267b16a535ec927adef7a14bbf3 [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;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070085import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080086import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070087import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070090import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070093import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700101
Sunny Goyal651077b2014-06-30 14:15:31 -0700102import com.android.launcher3.DropTarget.DragObject;
103import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700107import com.android.launcher3.compat.PackageInstallerCompat;
108import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100109import com.android.launcher3.compat.UserHandleCompat;
110import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
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
Michael Jurka2ecf9952012-06-18 12:52:28 -07002035 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
Adam Cohena9cf38f2011-05-02 15:36:58 -07002038 public DragController getDragController() {
2039 return mDragController;
2040 }
2041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
2043 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002044 if (requestCode >= 0) {
2045 setWaitingForResult(true);
2046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 super.startActivityForResult(intent, requestCode);
2048 }
2049
Winson Chung70d72102011-08-12 11:24:35 -07002050 /**
2051 * Indicates that we want global search for this activity by setting the globalSearch
2052 * argument for {@link #startSearch} to true.
2053 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002055 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002057
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002058 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002059
Karl Rosaen138a0412009-04-23 19:00:21 -07002060 if (initialQuery == null) {
2061 // Use any text typed in the launcher as the initial query
2062 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 if (appSearchData == null) {
2065 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002066 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
Winson Chungadf0c182012-08-23 14:59:07 -07002068 Rect sourceBounds = new Rect();
2069 if (mSearchDropTargetBar != null) {
2070 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2071 }
Romain Guycbb89e42009-06-08 15:52:54 -07002072
Ian Parkinson047036e2014-09-01 15:40:53 +01002073 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002074 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002075 if (clearTextImmediately) {
2076 clearTypedText();
2077 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002078 }
2079
Ian Parkinson047036e2014-09-01 15:40:53 +01002080 /**
2081 * Start a text search.
2082 *
2083 * @return {@code true} if the search will start immediately, so any further keypresses
2084 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2085 * to buffer keypresses.
2086 */
2087 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002089 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002091 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 }
2093
2094 /**
2095 * Starts the global search activity. This code is a copied from SearchManager
2096 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002097 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002098 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2101 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2102 if (globalSearchActivity == null) {
2103 Log.w(TAG, "No global search activity found.");
2104 return;
2105 }
2106 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2107 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2108 intent.setComponent(globalSearchActivity);
2109 // Make sure that we have a Bundle to put source in
2110 if (appSearchData == null) {
2111 appSearchData = new Bundle();
2112 } else {
2113 appSearchData = new Bundle(appSearchData);
2114 }
2115 // Set source to package name of app that starts global search, if not set already.
2116 if (!appSearchData.containsKey("source")) {
2117 appSearchData.putString("source", getPackageName());
2118 }
2119 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2120 if (!TextUtils.isEmpty(initialQuery)) {
2121 intent.putExtra(SearchManager.QUERY, initialQuery);
2122 }
2123 if (selectInitialQuery) {
2124 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2125 }
2126 intent.setSourceBounds(sourceBounds);
2127 try {
2128 startActivity(intent);
2129 } catch (ActivityNotFoundException ex) {
2130 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 }
2133
Yura4f93ec62014-02-04 14:15:21 +00002134 public boolean isOnCustomContent() {
2135 return mWorkspace.isOnOrMovingToCustomContent();
2136 }
2137
Winson Chung70d72102011-08-12 11:24:35 -07002138 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002139 public boolean onPrepareOptionsMenu(Menu menu) {
2140 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002141 if (!isOnCustomContent()) {
2142 // Close any open folders
2143 closeFolder();
2144 // Stop resizing any widgets
2145 mWorkspace.exitWidgetResizeMode();
2146 if (!mWorkspace.isInOverviewMode()) {
2147 // Show the overview mode
2148 showOverviewMode(true);
2149 } else {
2150 showWorkspace(true);
2151 }
Winson Chunge0298742014-01-17 12:03:00 -08002152 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002153 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002154 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002156 @Override
2157 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002158 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002159 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002160 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002161 }
2162
Joe Onorato9c1289c2009-08-17 11:03:03 -04002163 public boolean isWorkspaceLocked() {
2164 return mWorkspaceLoading || mWaitingForResult;
2165 }
2166
Adam Cohen517a7f52014-03-01 12:12:59 -08002167 public boolean isWorkspaceLoading() {
2168 return mWorkspaceLoading;
2169 }
2170
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002171 private void setWorkspaceLoading(boolean value) {
2172 boolean isLocked = isWorkspaceLocked();
2173 mWorkspaceLoading = value;
2174 if (isLocked != isWorkspaceLocked()) {
2175 onWorkspaceLockedChanged();
2176 }
2177 }
2178
2179 private void setWaitingForResult(boolean value) {
2180 boolean isLocked = isWorkspaceLocked();
2181 mWaitingForResult = value;
2182 if (isLocked != isWorkspaceLocked()) {
2183 onWorkspaceLockedChanged();
2184 }
2185 }
2186
2187 protected void onWorkspaceLockedChanged() { }
2188
Michael Jurka0280c3b2010-09-17 15:00:07 -07002189 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002190 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2193 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002194 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002195 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002197
Adam Cohenad4e15c2013-10-17 16:21:35 -07002198 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2199 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2200 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2201 }
2202
2203 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2204 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2205 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002206 if (appWidgetInfo.configure != null) {
2207 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002208 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002209
Bjorn Bringert7984c942009-12-09 15:38:25 +00002210 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002211 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2212 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002215 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002216 Runnable onComplete = new Runnable() {
2217 @Override
2218 public void run() {
2219 // Exit spring loaded mode if necessary after adding the widget
2220 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2221 null);
2222 }
2223 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002224 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002225 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002226 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228 }
Romain Guycbb89e42009-06-08 15:52:54 -07002229
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002230 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002231 // Close any folders that may be open.
2232 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 mWorkspace.moveToCustomContentScreen(animate);
2234 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 /**
2236 * Process a shortcut drop.
2237 *
2238 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 * @param cell The cell it should be added to, optional
2241 * @param position The location on the screen where it was dropped, optional
2242 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249
2250 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.cellX = cell[0];
2252 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254
2255 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2256 createShortcutIntent.setComponent(componentName);
2257 processShortcut(createShortcutIntent);
2258 }
2259
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260 /**
2261 * Process a widget drop.
2262 *
2263 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002265 * @param cell The cell it should be added to, optional
2266 * @param position The location on the screen where it was dropped, optional
2267 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002268 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002269 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002270 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002271 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 mPendingAddInfo.minSpanX = info.minSpanX;
2275 mPendingAddInfo.minSpanY = info.minSpanY;
2276
Adam Cohenfbba09b2011-07-18 21:43:05 -07002277 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.cellX = cell[0];
2279 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 if (span != null) {
2282 mPendingAddInfo.spanX = span[0];
2283 mPendingAddInfo.spanY = span[1];
2284 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285
Adam Cohened66b2b2012-01-23 17:28:51 -08002286 AppWidgetHostView hostView = info.boundWidget;
2287 int appWidgetId;
2288 if (hostView != null) {
2289 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002290 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 // In this case, we either need to start an activity to get permission to bind
2293 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002295 Bundle options = info.bindOptions;
2296
Sunny Goyalffe83f12014-08-14 17:39:34 -07002297 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2298 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002299 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002300 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002301 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002302 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2304 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2305 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002306 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2307 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002308 // TODO: we need to make sure that this accounts for the options bundle.
2309 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002310 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2311 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002312 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313 }
2314
Joe Onoratodeb98af2010-02-19 14:59:39 -08002315 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002316 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 }
2318
Winson Chung24ab2f12010-09-16 14:10:47 -07002319 void processWallpaper(Intent intent) {
2320 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2321 }
2322
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002325 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 folderInfo.title = getText(R.string.folder_name);
2327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002331 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332
2333 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 FolderIcon newFolder =
2335 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002338 // Force measure the new folder icon
2339 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2340 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002341 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
Romain Guycbb89e42009-06-08 15:52:54 -07002343
Joe Onorato9c1289c2009-08-17 11:03:03 -04002344 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002345 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 }
2347
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002348 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002349 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002350 }
2351
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002352 /**
2353 * Registers various content observers. The current implementation registers
2354 * only a favorites observer to keep track of the favorites applications.
2355 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002356 private void registerContentObservers() {
2357 ContentResolver resolver = getContentResolver();
2358 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2359 true, mWidgetObserver);
2360 }
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 @Override
2363 public boolean dispatchKeyEvent(KeyEvent event) {
2364 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2365 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002367 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002368 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002369 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002370 dumpState();
2371 return true;
2372 }
2373 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002374 }
2375 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2376 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002377 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 return true;
2379 }
2380 }
2381
2382 return super.dispatchKeyEvent(event);
2383 }
2384
Joe Onorato88ec0992009-11-19 13:16:06 -08002385 @Override
2386 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002387 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002388 if (mAppsCustomizeContent.getContentType() ==
2389 AppsCustomizePagedView.ContentType.Applications) {
2390 showWorkspace(true);
2391 } else {
2392 showOverviewMode(true);
2393 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002394 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002395 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002396 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002397 Folder openFolder = mWorkspace.getOpenFolder();
2398 if (openFolder.isEditingName()) {
2399 openFolder.dismissEditingName();
2400 } else {
2401 closeFolder();
2402 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002403 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002404 mWorkspace.exitWidgetResizeMode();
2405
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002406 // Back button is a no-op here, but give at least some feedback for the button press
2407 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002408 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002412 * Re-listen when widgets are reset.
2413 */
2414 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002415 if (mAppWidgetHost != null) {
2416 mAppWidgetHost.startListening();
2417 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002418 }
2419
2420 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 * Launches the intent referred by the clicked shortcut.
2422 *
2423 * @param v The view representing the clicked shortcut.
2424 */
2425 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002426 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2427 // view has detached (it's possible for this to happen if the view is removed mid touch).
2428 if (v.getWindowToken() == null) {
2429 return;
2430 }
2431
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002432 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002433 return;
2434 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002435
Adam Cohen1697b792013-09-17 19:08:21 -07002436 if (v instanceof Workspace) {
2437 if (mWorkspace.isInOverviewMode()) {
2438 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 }
2440 return;
2441 }
2442
2443 if (v instanceof CellLayout) {
2444 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002445 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002446 }
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002450 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002451 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002453 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002455 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002456 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002457 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002458 } else if (tag instanceof AppInfo) {
2459 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002460 } else if (tag instanceof LauncherAppWidgetInfo) {
2461 if (v instanceof PendingAppWidgetHostView) {
2462 onClickPendingWidget((PendingAppWidgetHostView) v);
2463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 }
2465 }
2466
Sunny Goyal508da152014-08-14 10:53:27 -07002467 public void onClickPagedViewIcon(View v) {
2468 startAppShortcutOrInfoActivity(v);
2469 }
2470
Michael Jurka0e260592010-06-30 17:07:39 -07002471 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002472 return false;
2473 }
2474
Michael Jurkaaf442092010-06-10 17:01:57 -07002475 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002476 * Event handler for the app widget view which has not fully restored.
2477 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002478 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2479 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002480 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002481 int widgetId = info.appWidgetId;
2482 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2483 if (appWidgetInfo != null) {
2484 mPendingAddWidgetInfo = appWidgetInfo;
2485 mPendingAddInfo.copyFrom(info);
2486 mPendingAddWidgetId = widgetId;
2487
Sunny Goyalffe83f12014-08-14 17:39:34 -07002488 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2489 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002490 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002491 } else if (info.installProgress < 0) {
2492 // The install has not been queued
2493 final String packageName = info.providerName.getPackageName();
2494 showBrokenAppInstallDialog(packageName,
2495 new DialogInterface.OnClickListener() {
2496 public void onClick(DialogInterface dialog, int id) {
2497 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2498 }
2499 });
2500 } else {
2501 // Download has started.
2502 final String packageName = info.providerName.getPackageName();
2503 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002504 }
2505 }
2506
2507 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002508 * Event handler for the search button
2509 *
2510 * @param v The view that was clicked.
2511 */
2512 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002513 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2514
Amith Yamasania135ba82011-08-09 17:42:01 -07002515 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002516 }
2517
2518 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002519 * Event handler for the voice button
2520 *
2521 * @param v The view that was clicked.
2522 */
2523 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002524 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002525
Bjorn Bringertc459e522013-06-07 19:36:01 +01002526 startVoice();
2527 }
2528
2529 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002530 try {
2531 final SearchManager searchManager =
2532 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2533 ComponentName activityName = searchManager.getGlobalSearchActivity();
2534 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002535 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002536 if (activityName != null) {
2537 intent.setPackage(activityName.getPackageName());
2538 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002539 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002540 } catch (ActivityNotFoundException e) {
2541 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002542 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002543 startActivitySafely(null, intent, "onClickVoiceButton");
2544 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002545 }
2546
2547 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002548 * Event handler for the "grid" button that appears on the home screen, which
2549 * enters all apps mode.
2550 *
2551 * @param v The view that was clicked.
2552 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 protected void onClickAllAppsButton(View v) {
2554 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2555 if (isAllAppsVisible()) {
2556 showWorkspace(true);
2557 } else {
2558 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2559 }
2560 }
2561
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 private void showBrokenAppInstallDialog(final String packageName,
2563 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002564 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 .setTitle(R.string.abandoned_promises_title)
2566 .setMessage(R.string.abandoned_promise_explanation)
2567 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2568 .setNeutralButton(R.string.abandoned_clean_this,
2569 new DialogInterface.OnClickListener() {
2570 public void onClick(DialogInterface dialog, int id) {
2571 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2572 mWorkspace.removeAbandonedPromise(packageName, user);
2573 }
2574 })
2575 .create().show();
2576 return;
2577 }
2578
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 /**
2580 * Event handler for an app shortcut click.
2581 *
2582 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2583 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002584 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2586 Object tag = v.getTag();
2587 if (!(tag instanceof ShortcutInfo)) {
2588 throw new IllegalArgumentException("Input must be a Shortcut");
2589 }
2590
2591 // Open shortcut
2592 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002593
2594 if (shortcut.isDisabled != 0) {
2595 int error = R.string.activity_not_available;
2596 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2597 error = R.string.safemode_shortcut_error;
2598 }
2599 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2600 return;
2601 }
2602
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 final Intent intent = shortcut.intent;
2604
2605 // Check for special shortcuts
2606 if (intent.getComponent() != null) {
2607 final String shortcutClass = intent.getComponent().getClassName();
2608
2609 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2610 MemoryDumpActivity.startDump(this);
2611 return;
2612 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2613 toggleShowWeightWatcher();
2614 return;
2615 }
2616 }
2617
Chris Wren40c5ed32014-06-24 18:24:23 -04002618 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002619 if ((v instanceof BubbleTextView)
2620 && shortcut.isPromise()
2621 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002623 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 new DialogInterface.OnClickListener() {
2625 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002626 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 return;
2630 }
2631
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002633 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002634 }
2635
Sunny Goyal508da152014-08-14 10:53:27 -07002636 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002637 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002638 final ShortcutInfo shortcut;
2639 final Intent intent;
2640 if (tag instanceof ShortcutInfo) {
2641 shortcut = (ShortcutInfo) tag;
2642 intent = shortcut.intent;
2643 int[] pos = new int[2];
2644 v.getLocationOnScreen(pos);
2645 intent.setSourceBounds(new Rect(pos[0], pos[1],
2646 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002647
Sunny Goyal508da152014-08-14 10:53:27 -07002648 } else if (tag instanceof AppInfo) {
2649 shortcut = null;
2650 intent = ((AppInfo) tag).intent;
2651 } else {
2652 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2653 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654
2655 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002656 mStats.recordLaunch(intent, shortcut);
2657
2658 if (success && v instanceof BubbleTextView) {
2659 mWaitingForResume = (BubbleTextView) v;
2660 mWaitingForResume.setStayPressed(true);
2661 }
2662 }
2663
2664 /**
2665 * Event handler for a folder icon click.
2666 *
2667 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2668 */
2669 protected void onClickFolderIcon(View v) {
2670 if (LOGD) Log.d(TAG, "onClickFolder");
2671 if (!(v instanceof FolderIcon)){
2672 throw new IllegalArgumentException("Input must be a FolderIcon");
2673 }
2674
2675 FolderIcon folderIcon = (FolderIcon) v;
2676 final FolderInfo info = folderIcon.getFolderInfo();
2677 Folder openFolder = mWorkspace.getFolderForTag(info);
2678
2679 // If the folder info reports that the associated folder is open, then verify that
2680 // it is actually opened. There have been a few instances where this gets out of sync.
2681 if (info.opened && openFolder == null) {
2682 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2683 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2684 info.opened = false;
2685 }
2686
2687 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2688 // Close any open folder
2689 closeFolder();
2690 // Open the requested folder
2691 openFolder(folderIcon);
2692 } else {
2693 // Find the open folder...
2694 int folderScreen;
2695 if (openFolder != null) {
2696 folderScreen = mWorkspace.getPageForView(openFolder);
2697 // .. and close it
2698 closeFolder(openFolder);
2699 if (folderScreen != mWorkspace.getCurrentPage()) {
2700 // Close any folder open on the current screen
2701 closeFolder();
2702 // Pull the folder onto this screen
2703 openFolder(folderIcon);
2704 }
2705 }
2706 }
Michael Jurka5130e402011-10-13 04:55:35 -07002707 }
2708
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002709 /**
2710 * Event handler for the (Add) Widgets button that appears after a long press
2711 * on the home screen.
2712 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002713 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002714 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002715 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2716 }
2717
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 /**
2719 * Event handler for the wallpaper picker button that appears after a long press
2720 * on the home screen.
2721 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002722 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2724 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2725 pickWallpaper.setComponent(getWallpaperPickerComponent());
2726 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2727 }
2728
2729 /**
2730 * Event handler for a click on the settings button that appears after a long press
2731 * on the home screen.
2732 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002733 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002734 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2735 }
2736
Michael Jurka5130e402011-10-13 04:55:35 -07002737 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002738 // Provide the same haptic feedback that the system offers for virtual keys.
2739 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002740 }
2741
Adam Cohen61f560d2013-09-30 15:58:20 -07002742 public void performHapticFeedbackOnTouchDown(View v) {
2743 // Provide the same haptic feedback that the system offers for virtual keys.
2744 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2745 }
2746
2747 public View.OnTouchListener getHapticFeedbackTouchListener() {
2748 if (mHapticFeedbackTouchListener == null) {
2749 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2750 @Override
2751 public boolean onTouch(View v, MotionEvent event) {
2752 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2753 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2754 }
2755 return false;
2756 }
2757 };
2758 }
2759 return mHapticFeedbackTouchListener;
2760 }
2761
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002762 public void onDragStarted(View view) {}
2763
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002764 /**
2765 * Called when the user stops interacting with the launcher.
2766 * This implies that the user is now on the homescreen and is not doing housekeeping.
2767 */
2768 protected void onInteractionEnd() {}
2769
2770 /**
2771 * Called when the user starts interacting with the launcher.
2772 * The possible interactions are:
2773 * - open all apps
2774 * - reorder an app shortcut, or a widget
2775 * - open the overview mode.
2776 * This is a good time to stop doing things that only make sense
2777 * when the user is on the homescreen and not doing housekeeping.
2778 */
2779 protected void onInteractionBegin() {}
2780
Kenny Guyf07af7b2014-07-31 11:39:16 +01002781 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002782 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002783 try {
2784 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2785 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2786 launcherApps.showAppDetailsForProfile(componentName, user);
2787 } catch (SecurityException e) {
2788 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2789 Log.e(TAG, "Launcher does not have permission to launch settings");
2790 } catch (ActivityNotFoundException e) {
2791 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2792 Log.e(TAG, "Unable to launch settings");
2793 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002794 }
2795
Michael Jurka1e2f4652013-07-08 18:03:46 -07002796 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002797 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2798 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002799 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002800 // System applications cannot be installed. For now, show a toast explaining that.
2801 // We may give them the option of disabling apps this way.
2802 int messageId = R.string.uninstall_system_app_text;
2803 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002804 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002805 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002806 String packageName = componentName.getPackageName();
2807 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002808 Intent intent = new Intent(
2809 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002810 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2811 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002812 if (user != null) {
2813 user.addToIntent(intent, Intent.EXTRA_USER);
2814 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002815 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002816 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002817 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002818 }
2819
Michael Jurka86a720e2012-05-09 11:23:23 -07002820 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002821 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002823 // Only launch using the new animation if the shortcut has not opted out (this is a
2824 // private contract between launcher and may be ignored in the future).
2825 boolean useLaunchAnimation = (v != null) &&
2826 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002827 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2828 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002829
Kenny Guy1317e2d2014-05-08 18:52:50 +01002830 UserHandleCompat user = null;
2831 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2832 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2833 user = userManager.getUserForSerialNumber(serialNumber);
2834 }
2835
2836 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002837 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002838 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002839 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2840 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002841 optsBundle = opts.toBundle();
2842 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002843
2844 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2845 // Could be launching some bookkeeping activity
2846 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002847 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002848 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002849 launcherApps.startActivityForProfile(intent.getComponent(), user,
2850 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002851 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002852 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 } catch (SecurityException e) {
2854 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002855 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002857 "or use the exported attribute for this activity. "
2858 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002860 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002862
Michael Jurka86a720e2012-05-09 11:23:23 -07002863 boolean startActivitySafely(View v, Intent intent, Object tag) {
2864 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002865 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2866 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2867 return false;
2868 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002869 try {
2870 success = startActivity(v, intent, tag);
2871 } catch (ActivityNotFoundException e) {
2872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2873 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2874 }
2875 return success;
2876 }
2877
Adam Cohen268c4752012-06-06 17:47:33 -07002878 /**
2879 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2880 * in the DragLayer in the exact absolute location of the original FolderIcon.
2881 */
2882 private void copyFolderIconToImage(FolderIcon fi) {
2883 final int width = fi.getMeasuredWidth();
2884 final int height = fi.getMeasuredHeight();
2885
2886 // Lazy load ImageView, Bitmap and Canvas
2887 if (mFolderIconImageView == null) {
2888 mFolderIconImageView = new ImageView(this);
2889 }
2890 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2891 mFolderIconBitmap.getHeight() != height) {
2892 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2893 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2894 }
2895
2896 DragLayer.LayoutParams lp;
2897 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2898 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2899 } else {
2900 lp = new DragLayer.LayoutParams(width, height);
2901 }
2902
Adam Cohen307fe232012-08-16 17:55:58 -07002903 // The layout from which the folder is being opened may be scaled, adjust the starting
2904 // view size by this scale factor.
2905 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002906 lp.customPosition = true;
2907 lp.x = mRectForFolderAnimation.left;
2908 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002909 lp.width = (int) (scale * width);
2910 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002911
2912 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2913 fi.draw(mFolderIconCanvas);
2914 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002915 if (fi.getFolder() != null) {
2916 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2917 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002918 }
Adam Cohen268c4752012-06-06 17:47:33 -07002919 // Just in case this image view is still in the drag layer from a previous animation,
2920 // we remove it and re-add it.
2921 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2922 mDragLayer.removeView(mFolderIconImageView);
2923 }
2924 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002925 if (fi.getFolder() != null) {
2926 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002927 }
Adam Cohen268c4752012-06-06 17:47:33 -07002928 }
2929
Adam Cohen2801caf2011-05-13 20:57:39 -07002930 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002931 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002932 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2933 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2934 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2935
Adam Cohenc51934b2011-07-26 21:07:43 -07002936 FolderInfo info = (FolderInfo) fi.getTag();
2937 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2938 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002939 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2940 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002941 }
2942
Adam Cohen268c4752012-06-06 17:47:33 -07002943 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2944 copyFolderIconToImage(fi);
2945 fi.setVisibility(View.INVISIBLE);
2946
Michael Jurka2ecf9952012-06-18 12:52:28 -07002947 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002948 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002949 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002950 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2951 }
2952 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002953 oa.start();
2954 }
2955
Adam Cohen268c4752012-06-06 17:47:33 -07002956 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002957 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002958 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2959 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2960 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2961
Adam Cohen268c4752012-06-06 17:47:33 -07002962 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002963
Adam Cohen268c4752012-06-06 17:47:33 -07002964 // We remove and re-draw the FolderIcon in-case it has changed
2965 mDragLayer.removeView(mFolderIconImageView);
2966 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002967 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002968 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002969 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002970 oa.addListener(new AnimatorListenerAdapter() {
2971 @Override
2972 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002973 if (cl != null) {
2974 cl.clearFolderLeaveBehind();
2975 // Remove the ImageView copy of the FolderIcon and make the original visible.
2976 mDragLayer.removeView(mFolderIconImageView);
2977 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002978 }
2979 }
2980 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002981 oa.start();
2982 }
2983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002985 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986 * is animated relative to the specified View. If the View is null, no animation
2987 * is played.
2988 *
2989 * @param folderInfo The FolderInfo describing the folder to open.
2990 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002991 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002992 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002993 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994
Adam Cohena9cf38f2011-05-02 15:36:58 -07002995 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002996
Adam Cohen4554ee12011-08-03 16:13:21 -07002997 // Just verify that the folder hasn't already been added to the DragLayer.
2998 // There was a one-off crash where the folder had a parent already.
2999 if (folder.getParent() == null) {
3000 mDragLayer.addView(folder);
3001 mDragController.addDropTarget((DropTarget) folder);
3002 } else {
3003 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3004 folder.getParent() + ").");
3005 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003006 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003007 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003008
3009 // Notify the accessibility manager that this folder "window" has appeared and occluded
3010 // the workspace items
3011 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3012 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003013 }
3014
3015 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003016 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003017 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003018 if (folder.isEditingName()) {
3019 folder.dismissEditingName();
3020 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003021 closeFolder(folder);
3022 }
3023 }
3024
3025 void closeFolder(Folder folder) {
3026 folder.getInfo().opened = false;
3027
3028 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3029 if (parent != null) {
3030 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3031 shrinkAndFadeInFolderIcon(fi);
3032 }
3033 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003034
3035 // Notify the accessibility manager that this folder "window" has disappeard and no
3036 // longer occludeds the workspace items
3037 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003041 if (!isDraggingEnabled()) return false;
3042 if (isWorkspaceLocked()) return false;
3043 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044
Adam Cohen1697b792013-09-17 19:08:21 -07003045 if (v instanceof Workspace) {
3046 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003047 if (mWorkspace.enterOverviewMode()) {
3048 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3049 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3050 return true;
3051 } else {
3052 return false;
3053 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003054 } else {
3055 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003056 }
Adam Cohen1697b792013-09-17 19:08:21 -07003057 }
3058
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003059 CellLayout.CellInfo longClickCellInfo = null;
3060 View itemUnderLongClick = null;
3061 if (v.getTag() instanceof ItemInfo) {
3062 ItemInfo info = (ItemInfo) v.getTag();
3063 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3064 itemUnderLongClick = longClickCellInfo.cell;
3065 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 }
3067
Winson Chung3d503fb2011-07-13 17:25:49 -07003068 // The hotseat touch handling does not go through Workspace, and we always allow long press
3069 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003070 final boolean inHotseat = isHotseatLayout(v);
3071 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003072 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003073 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003074 // User long pressed on empty space
3075 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3076 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003077 if (mWorkspace.isInOverviewMode()) {
3078 mWorkspace.startReordering(v);
3079 } else {
3080 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003083 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3084 mHotseat.getOrderInHotseat(
3085 longClickCellInfo.cellX,
3086 longClickCellInfo.cellY));
3087 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003088 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003089 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 }
3091 }
3092 }
3093 return true;
3094 }
3095
Winson Chung3d503fb2011-07-13 17:25:49 -07003096 boolean isHotseatLayout(View layout) {
3097 return mHotseat != null && layout != null &&
3098 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3099 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003100
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 /**
3102 * Returns the CellLayout of the specified container at the specified screen.
3103 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003104 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003105 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3106 if (mHotseat != null) {
3107 return mHotseat.getLayout();
3108 } else {
3109 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003110 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003112 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003113 }
3114 }
3115
Daniel Sandler843e8602010-06-07 14:59:01 -04003116 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003117 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003118 }
3119
Craig Mautner360310b2012-10-26 15:13:08 -07003120 private void setWorkspaceBackground(boolean workspace) {
3121 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003122 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003123 }
3124
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003125 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003126 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3127 int curflags = getWindow().getAttributes().flags
3128 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3129 if (wpflags != curflags) {
3130 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3131 }
Craig Mautner360310b2012-10-26 15:13:08 -07003132 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003133 }
3134
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003135 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3136 if (v instanceof LauncherTransitionable) {
3137 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3138 }
3139 }
3140
Michael Jurkabed61d22012-02-14 22:51:29 -08003141 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3142 if (v instanceof LauncherTransitionable) {
3143 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3144 }
Winson Chung70442722012-02-10 15:43:22 -08003145
3146 // Update the workspace transition step as well
3147 dispatchOnLauncherTransitionStep(v, 0f);
3148 }
3149
3150 private void dispatchOnLauncherTransitionStep(View v, float t) {
3151 if (v instanceof LauncherTransitionable) {
3152 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3153 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003154 }
3155
3156 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3157 if (v instanceof LauncherTransitionable) {
3158 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3159 }
Winson Chung70442722012-02-10 15:43:22 -08003160
3161 // Update the workspace transition step as well
3162 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003163 }
3164
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003165 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003166 * Things to test when changing the following seven functions.
3167 * - Home from workspace
3168 * - from center screen
3169 * - from other screens
3170 * - Home from all apps
3171 * - from center screen
3172 * - from other screens
3173 * - Back from all apps
3174 * - from center screen
3175 * - from other screens
3176 * - Launch app from workspace and quit
3177 * - with back
3178 * - with home
3179 * - Launch app from all apps and quit
3180 * - with back
3181 * - with home
3182 * - Go to a screen that's not the default, then all
3183 * apps, and launch and app, and go back
3184 * - with back
3185 * -with home
3186 * - On workspace, long press power and go back
3187 * - with back
3188 * - with home
3189 * - On all apps, long press power and go back
3190 * - with back
3191 * - with home
3192 * - On workspace, power off
3193 * - On all apps, power off
3194 * - Launch an app and turn off the screen while in that app
3195 * - Go back with home key
3196 * - Go back with back key TODO: make this not go to workspace
3197 * - From all apps
3198 * - From workspace
3199 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3200 * - From all apps
3201 * - From the center workspace
3202 * - From another workspace
3203 */
3204
3205 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003206 * Zoom the camera out from the workspace to reveal 'toView'.
3207 * Assumes that the view to show is anchored at either the very top or very bottom
3208 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003209 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003210 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003211 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3212 showAppsCustomizeHelper(animated, springLoaded, contentType);
3213 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003214
Winson Chungc58497e2013-09-03 17:48:37 -07003215 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3216 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003217 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003218 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003219 mStateAnimation.cancel();
3220 mStateAnimation = null;
3221 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003222
Kenny Guyd794a3f2014-09-16 15:17:58 +01003223 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003224
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003225 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003226
Winson Chungf0ea4d32011-06-06 14:27:16 -07003227 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3228 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003229 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003230 final int itemsAlphaStagger =
3231 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003232
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003234 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003235 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003236
Adam Cohen2854d252014-08-27 16:04:07 -07003237 final ArrayList<View> layerViews = new ArrayList<View>();
3238
Adam Cohen6c5891a2014-07-09 23:53:15 -07003239 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3240 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003241 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003242 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003243 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003244 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3245 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003246 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3247 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003248
Adam Cohena38dc902014-09-07 17:48:55 +02003249 // If for some reason our views aren't initialized, don't animate
3250 boolean initialized = getAllAppsButton() != null;
3251
3252 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003253 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003254 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3255 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003256
Adam Cohen9bfdb762014-07-21 17:44:06 -07003257 final View page = content.getPageAt(content.getCurrentPage());
3258 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003259
Adam Cohen63f1ec02014-08-12 09:23:13 -07003260 final float initialPanelAlpha = 1f;
3261
3262 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3263 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003264 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3265 } else {
3266 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3267 }
3268
Adam Cohen9bfdb762014-07-21 17:44:06 -07003269 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003270 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003271 revealView.setVisibility(View.VISIBLE);
3272 // We need to hide this view as the animation start will be posted.
3273 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003274
Adam Cohen9bfdb762014-07-21 17:44:06 -07003275 int width = revealView.getMeasuredWidth();
3276 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003277 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003278
Adam Cohen63f1ec02014-08-12 09:23:13 -07003279 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003280 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003281
Adam Cohen63f1ec02014-08-12 09:23:13 -07003282 // Get the y delta between the center of the page and the center of the all apps button
3283 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3284 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003285
Adam Cohen2854d252014-08-27 16:04:07 -07003286 float alpha = 0;
3287 float xDrift = 0;
3288 float yDrift = 0;
3289 if (material) {
3290 alpha = isWidgetTray ? 0.3f : 1f;
3291 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3292 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3293 } else {
3294 yDrift = 2 * height / 3;
3295 xDrift = 0;
3296 }
3297 final float initAlpha = alpha;
3298
Adam Cohen9bfdb762014-07-21 17:44:06 -07003299 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003300 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003301 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003302 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003303 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003304 PropertyValuesHolder panelDriftX =
3305 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003306
Adam Cohen2854d252014-08-27 16:04:07 -07003307 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3308 panelAlpha, panelDriftY, panelDriftX);
3309
Adam Cohen9bfdb762014-07-21 17:44:06 -07003310 panelAlphaAndDrift.setDuration(revealDuration);
3311 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003312
Adam Cohen9bfdb762014-07-21 17:44:06 -07003313 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003314
Adam Cohen4e243a22014-08-10 18:30:55 -07003315 if (page != null) {
3316 page.setVisibility(View.VISIBLE);
3317 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003318 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003319
Adam Cohen2854d252014-08-27 16:04:07 -07003320 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3321 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003322 pageDrift.setDuration(revealDuration);
3323 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003324 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003325 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003326
Adam Cohen63f1ec02014-08-12 09:23:13 -07003327 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003328 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003329 itemsAlpha.setDuration(revealDuration);
3330 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3331 itemsAlpha.setStartDelay(itemsAlphaStagger);
3332 mStateAnimation.play(itemsAlpha);
3333 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334
Adam Cohen4e243a22014-08-10 18:30:55 -07003335 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3336 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003337 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003338 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003339 indicatorsAlpha.setDuration(revealDuration);
3340 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003341
Adam Cohen9bfdb762014-07-21 17:44:06 -07003342 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003343 final View allApps = getAllAppsButton();
3344 int allAppsButtonSize = LauncherAppState.getInstance().
3345 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3346 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003347 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003348 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003349 reveal.setDuration(revealDuration);
3350 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003351
3352 reveal.addListener(new AnimatorListenerAdapter() {
3353 public void onAnimationStart(Animator animation) {
3354 if (!isWidgetTray) {
3355 allApps.setVisibility(View.INVISIBLE);
3356 }
3357 }
3358 public void onAnimationEnd(Animator animation) {
3359 if (!isWidgetTray) {
3360 allApps.setVisibility(View.VISIBLE);
3361 }
3362 }
3363 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003364 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003365 }
Michael Jurka1899a362011-11-03 13:50:45 -07003366
Adam Cohen9bfdb762014-07-21 17:44:06 -07003367 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3368 @Override
3369 public void onAnimationEnd(Animator animation) {
3370 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3371 dispatchOnLauncherTransitionEnd(toView, animated, false);
3372
3373 revealView.setVisibility(View.INVISIBLE);
3374 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003375 if (page != null) {
3376 page.setLayerType(View.LAYER_TYPE_NONE, null);
3377 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 content.setPageBackgroundsVisible(true);
3379
3380 // Hide the search bar
3381 if (mSearchDropTargetBar != null) {
3382 mSearchDropTargetBar.hideSearchBar(false);
3383 }
3384 }
3385
Adam Cohen9bfdb762014-07-21 17:44:06 -07003386 });
3387
Adam Cohen6c5891a2014-07-09 23:53:15 -07003388 if (workspaceAnim != null) {
3389 mStateAnimation.play(workspaceAnim);
3390 }
Adam Cohen2854d252014-08-27 16:04:07 -07003391
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003392 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3393 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003394 final AnimatorSet stateAnimation = mStateAnimation;
3395 final Runnable startAnimRunnable = new Runnable() {
3396 public void run() {
3397 // Check that mStateAnimation hasn't changed while
3398 // we waited for a layout/draw pass
3399 if (mStateAnimation != stateAnimation)
3400 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003401 dispatchOnLauncherTransitionStart(fromView, animated, false);
3402 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003403
3404 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003405 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003406 for (int i = 0; i < layerViews.size(); i++) {
3407 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003408 if (v != null) {
3409 boolean attached = true;
3410 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3411 attached = v.isAttachedToWindow();
3412 }
3413 if (attached) v.buildLayer();
3414 }
Adam Cohen2854d252014-08-27 16:04:07 -07003415 }
3416 }
3417 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003418 }
3419 };
Adam Cohen2854d252014-08-27 16:04:07 -07003420 toView.bringToFront();
3421 toView.setVisibility(View.VISIBLE);
3422 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003423 } else {
3424 toView.setTranslationX(0.0f);
3425 toView.setTranslationY(0.0f);
3426 toView.setScaleX(1.0f);
3427 toView.setScaleY(1.0f);
3428 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003429 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003430
Daniel Sandlere4f98912013-06-25 15:13:26 -04003431 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003432 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003433 if (mSearchDropTargetBar != null) {
3434 mSearchDropTargetBar.hideSearchBar(false);
3435 }
Michael Jurkaabded662011-03-04 12:06:57 -08003436 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003437 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003438 dispatchOnLauncherTransitionStart(fromView, animated, false);
3439 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003440 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003441 dispatchOnLauncherTransitionStart(toView, animated, false);
3442 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003443 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003444 }
3445
3446 /**
3447 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003449 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003450 */
Adam Cohened307df2013-10-02 09:37:31 -07003451 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003452 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003453
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003455 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456 mStateAnimation.cancel();
3457 mStateAnimation = null;
3458 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003459
Kenny Guyd794a3f2014-09-16 15:17:58 +01003460 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003461 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003462
Winson Chungf0ea4d32011-06-06 14:27:16 -07003463 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003464 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003465 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003466 final int itemsAlphaStagger =
3467 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003468
Winson Chungf0ea4d32011-06-06 14:27:16 -07003469 final float scaleFactor = (float)
3470 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3471 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003472 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003473 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003474 final ArrayList<View> layerViews = new ArrayList<View>();
3475
Adam Cohened307df2013-10-02 09:37:31 -07003476 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003477 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003478 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003479 } else if (toState == Workspace.State.SPRING_LOADED ||
3480 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003481 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003482 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003483 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003484
Adam Cohena38dc902014-09-07 17:48:55 +02003485 // If for some reason our views aren't initialized, don't animate
3486 boolean initialized = getAllAppsButton() != null;
3487
3488 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003489 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003490 if (workspaceAnim != null) {
3491 mStateAnimation.play(workspaceAnim);
3492 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003493
Adam Cohen9bfdb762014-07-21 17:44:06 -07003494 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3495 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003496
Adam Cohen9bfdb762014-07-21 17:44:06 -07003497 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003498
3499 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3500 int count = content.getChildCount();
3501 for (int i = 0; i < count; i++) {
3502 View child = content.getChildAt(i);
3503 if (child != page) {
3504 child.setVisibility(View.INVISIBLE);
3505 }
3506 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003507 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003508
Adam Cohen2854d252014-08-27 16:04:07 -07003509 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3510 // don't perform all these no-op animations. In particularly, this was causing
3511 // the all-apps button to pop in and out.
3512 if (fromView.getVisibility() == View.VISIBLE) {
3513 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3514 final boolean isWidgetTray =
3515 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003516
Adam Cohen2854d252014-08-27 16:04:07 -07003517 if (isWidgetTray) {
3518 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3519 } else {
3520 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003521 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003522
Adam Cohen2854d252014-08-27 16:04:07 -07003523 int width = revealView.getMeasuredWidth();
3524 int height = revealView.getMeasuredHeight();
3525 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3526
3527 // Hide the real page background, and swap in the fake one
3528 revealView.setVisibility(View.VISIBLE);
3529 content.setPageBackgroundsVisible(false);
3530
3531 final View allAppsButton = getAllAppsButton();
3532 revealView.setTranslationY(0);
3533 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3534 allAppsButton, null);
3535
3536 float xDrift = 0;
3537 float yDrift = 0;
3538 if (material) {
3539 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3540 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3541 } else {
3542 yDrift = 5 * height / 4;
3543 xDrift = 0;
3544 }
3545
3546 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3547 TimeInterpolator decelerateInterpolator = material ?
3548 new LogDecelerateInterpolator(100, 0) :
3549 new LogDecelerateInterpolator(30, 0);
3550
3551 // The vertical motion of the apps panel should be delayed by one frame
3552 // from the conceal animation in order to give the right feel. We correpsondingly
3553 // shorten the duration so that the slide and conceal end at the same time.
3554 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3555 0, yDrift);
3556 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3557 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3558 panelDriftY.setInterpolator(decelerateInterpolator);
3559 mStateAnimation.play(panelDriftY);
3560
3561 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3562 0, xDrift);
3563 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3564 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3565 panelDriftX.setInterpolator(decelerateInterpolator);
3566 mStateAnimation.play(panelDriftX);
3567
3568 if (isWidgetTray || !material) {
3569 float finalAlpha = material ? 0.4f : 0f;
3570 revealView.setAlpha(1f);
3571 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3572 1f, finalAlpha);
3573 panelAlpha.setDuration(revealDuration);
3574 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3575 new AccelerateInterpolator(1.5f));
3576 mStateAnimation.play(panelAlpha);
3577 }
3578
3579 if (page != null) {
3580 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3581
3582 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3583 0, yDrift);
3584 page.setTranslationY(0);
3585 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3586 pageDrift.setInterpolator(decelerateInterpolator);
3587 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3588 mStateAnimation.play(pageDrift);
3589
3590 page.setAlpha(1f);
3591 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3592 itemsAlpha.setDuration(100);
3593 itemsAlpha.setInterpolator(decelerateInterpolator);
3594 mStateAnimation.play(itemsAlpha);
3595 }
3596
3597 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3598 pageIndicators.setAlpha(1f);
3599 ObjectAnimator indicatorsAlpha =
3600 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3601 indicatorsAlpha.setDuration(revealDuration);
3602 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3603 mStateAnimation.play(indicatorsAlpha);
3604
3605 width = revealView.getMeasuredWidth();
3606
3607 if (material) {
3608 if (!isWidgetTray) {
3609 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003610 }
Adam Cohen2854d252014-08-27 16:04:07 -07003611 int allAppsButtonSize = LauncherAppState.getInstance().
3612 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3613 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3614 Animator reveal =
3615 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3616 height / 2, revealRadius, finalRadius);
3617 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3618 reveal.setDuration(revealDuration);
3619 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003620
Adam Cohen2854d252014-08-27 16:04:07 -07003621 reveal.addListener(new AnimatorListenerAdapter() {
3622 public void onAnimationEnd(Animator animation) {
3623 revealView.setVisibility(View.INVISIBLE);
3624 if (!isWidgetTray) {
3625 allAppsButton.setVisibility(View.VISIBLE);
3626 }
3627 }
3628 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003629
Adam Cohen2854d252014-08-27 16:04:07 -07003630 mStateAnimation.play(reveal);
3631 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003632
Adam Cohen2854d252014-08-27 16:04:07 -07003633 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3634 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3635 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003636 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003637
3638 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003639 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003640 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003641 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003642 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3643 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003644 if (onCompleteRunnable != null) {
3645 onCompleteRunnable.run();
3646 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003647
3648 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003649 if (page != null) {
3650 page.setLayerType(View.LAYER_TYPE_NONE, null);
3651 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003652 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003653 // Unhide side pages
3654 int count = content.getChildCount();
3655 for (int i = 0; i < count; i++) {
3656 View child = content.getChildAt(i);
3657 child.setVisibility(View.VISIBLE);
3658 }
3659
3660 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003661 if (page != null) {
3662 page.setTranslationX(0);
3663 page.setTranslationY(0);
3664 page.setAlpha(1);
3665 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003666 content.setCurrentPage(content.getNextPage());
3667
Chet Haasebc2f0822012-10-26 17:59:53 -07003668 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003669 }
3670 });
3671
Adam Cohen2854d252014-08-27 16:04:07 -07003672 final AnimatorSet stateAnimation = mStateAnimation;
3673 final Runnable startAnimRunnable = new Runnable() {
3674 public void run() {
3675 // Check that mStateAnimation hasn't changed while
3676 // we waited for a layout/draw pass
3677 if (mStateAnimation != stateAnimation)
3678 return;
3679 dispatchOnLauncherTransitionStart(fromView, animated, false);
3680 dispatchOnLauncherTransitionStart(toView, animated, false);
3681
Kenny Guyd794a3f2014-09-16 15:17:58 +01003682 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003683 for (int i = 0; i < layerViews.size(); i++) {
3684 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003685 if (v != null) {
3686 boolean attached = true;
3687 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3688 attached = v.isAttachedToWindow();
3689 }
3690 if (attached) v.buildLayer();
3691 }
Adam Cohen2854d252014-08-27 16:04:07 -07003692 }
3693 }
3694 mStateAnimation.start();
3695 }
3696 };
3697 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003698 } else {
3699 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003700 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003701 dispatchOnLauncherTransitionStart(fromView, animated, true);
3702 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003703 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003704 dispatchOnLauncherTransitionStart(toView, animated, true);
3705 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003706 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003707 }
3708
Michael Jurkae326f182011-11-21 14:05:46 -08003709 @Override
3710 public void onTrimMemory(int level) {
3711 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003712 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003713 mAppsCustomizeTabHost.onTrimMemory();
3714 }
3715 }
3716
Adam Cohened307df2013-10-02 09:37:31 -07003717 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003718 showWorkspace(animated, null);
3719 }
3720
Adam Cohened307df2013-10-02 09:37:31 -07003721 protected void showWorkspace() {
3722 showWorkspace(true);
3723 }
3724
Adam Cohened66b2b2012-01-23 17:28:51 -08003725 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003726 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003727 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003728 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003729 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003730
Winson Chungc7d2b602012-05-16 17:02:20 -07003731 // Show the search bar (only animate if we were showing the drop target bar in spring
3732 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003733 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003734 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003735 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003736
Michael Jurkab3e22d92011-10-31 15:58:33 -07003737 // Set focus to the AppsCustomize button
3738 if (mAllAppsButton != null) {
3739 mAllAppsButton.requestFocus();
3740 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003741 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003743 // Change the state *after* we've called all the transition code
3744 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003745
Winson Chung5fb63472011-02-02 17:03:37 -08003746 // Resume the auto-advance of widgets
3747 mUserPresent = true;
3748 updateRunning();
3749
alanv1d4fde62012-10-17 13:15:47 -07003750 // Send an accessibility event to announce the context change
3751 getWindow().getDecorView()
3752 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003753
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003754 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003755 }
3756
Adam Cohened307df2013-10-02 09:37:31 -07003757 void showOverviewMode(boolean animated) {
3758 mWorkspace.setVisibility(View.VISIBLE);
3759 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3760 mState = State.WORKSPACE;
3761 onWorkspaceShown(animated);
3762 }
3763
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003764 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003765 }
3766
Winson Chung82963d52013-10-09 11:20:57 -07003767 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3768 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003769 if (mState != State.WORKSPACE) return;
3770
Winson Chung82963d52013-10-09 11:20:57 -07003771 if (resetPageToZero) {
3772 mAppsCustomizeTabHost.reset();
3773 }
Winson Chungc58497e2013-09-03 17:48:37 -07003774 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003775 mAppsCustomizeTabHost.post(new Runnable() {
3776 @Override
3777 public void run() {
3778 // We post this in-case the all apps view isn't yet constructed.
3779 mAppsCustomizeTabHost.requestFocus();
3780 }
3781 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003782
Michael Jurkab3e22d92011-10-31 15:58:33 -07003783 // Change the state *after* we've called all the transition code
3784 mState = State.APPS_CUSTOMIZE;
3785
3786 // Pause the auto-advance of widgets until we are out of AllApps
3787 mUserPresent = false;
3788 updateRunning();
3789 closeFolder();
3790
3791 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003792 getWindow().getDecorView()
3793 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003794 }
3795
Adam Cohen7777d962011-08-18 18:58:38 -07003796 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003797 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003798 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003799 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003800 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003801 }
Adam Cohen7777d962011-08-18 18:58:38 -07003802
Adam Cohenad4e15c2013-10-17 16:21:35 -07003803 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003804 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003805 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3806
Winson Chunge7a03942011-08-05 15:05:12 -07003807 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003808 @Override
3809 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003810 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003811 // Before we show workspace, hide all apps again because
3812 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3813 // clean up our state transition functions
3814 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003815 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003816 } else {
3817 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003818 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003819 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003820 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003821 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003822
Michael Jurkad3ef3062010-11-23 16:23:58 -08003823 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003824 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003825 final boolean animated = true;
3826 final boolean springLoaded = true;
3827 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003828 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003829 }
3830 // Otherwise, we are not in spring loaded mode, so don't do anything.
3831 }
3832
Michael Jurkab3e22d92011-10-31 15:58:33 -07003833 void lockAllApps() {
3834 // TODO
3835 }
3836
3837 void unlockAllApps() {
3838 // TODO
3839 }
3840
Winson Chungf0ea4d32011-06-06 14:27:16 -07003841 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003842 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003843 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003844 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003845 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003847 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003848 int duration = 0;
3849 if (mSearchDropTargetBar != null) {
3850 duration = mSearchDropTargetBar.getTransitionOutDuration();
3851 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003852 mHotseat.animate().alpha(0f).setDuration(duration);
3853 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003854 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003855 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003856 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003857 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003858 }
3859
Patrick Dubroy5f445422011-02-18 14:35:21 -08003860 /**
3861 * Add an item from all apps or customize onto the given workspace screen.
3862 * If layout is null, add to the current screen.
3863 */
3864 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003865 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003866 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003867 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003868 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003869
Winson Chungdff8ebb2011-09-08 17:25:31 -07003870 /** Maps the current orientation to an index for referencing orientation correct global icons */
3871 private int getCurrentOrientationIndexForGlobalIcons() {
3872 // default - 0, landscape - 1
3873 switch (getResources().getConfiguration().orientation) {
3874 case Configuration.ORIENTATION_LANDSCAPE:
3875 return 1;
3876 default:
3877 return 0;
3878 }
3879 }
3880
Michael Jurkaae65ee32012-04-30 11:45:54 -07003881 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003882 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003883 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003884 // Look for the toolbar icon specified in the activity meta-data
3885 Bundle metaData = packageManager.getActivityInfo(
3886 activityName, PackageManager.GET_META_DATA).metaData;
3887 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003888 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003889 if (iconResId != 0) {
3890 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003891 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003892 }
3893 }
3894 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003895 // This can happen if the activity defines an invalid drawable
3896 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3897 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003898 } catch (Resources.NotFoundException nfe) {
3899 // This can happen if the activity defines an invalid drawable
3900 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3901 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003902 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003903 return null;
3904 }
3905
3906 // if successful in getting icon, return it; otherwise, set button to use default drawable
3907 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003908 int buttonId, ComponentName activityName, int fallbackDrawableId,
3909 String toolbarResourceName) {
3910 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003911 Resources r = getResources();
3912 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3913 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003914
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003915 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003916 // If we were unable to find the icon via the meta-data, use a generic one
3917 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003918 toolbarIcon = r.getDrawable(fallbackDrawableId);
3919 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003920 if (button != null) {
3921 button.setCompoundDrawables(toolbarIcon, null, null, null);
3922 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003923 return null;
3924 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003925 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003926 if (button != null) {
3927 button.setCompoundDrawables(toolbarIcon, null, null, null);
3928 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003929 return toolbarIcon.getConstantState();
3930 }
3931 }
3932
3933 // if successful in getting icon, return it; otherwise, set button to use default drawable
3934 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003935 int buttonId, ComponentName activityName, int fallbackDrawableId,
3936 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003937 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003938 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003939
Michael Jurka19e0fc52011-07-22 18:00:21 -07003940 if (button != null) {
3941 // If we were unable to find the icon via the meta-data, use a
3942 // generic one
3943 if (toolbarIcon == null) {
3944 button.setImageResource(fallbackDrawableId);
3945 } else {
3946 button.setImageDrawable(toolbarIcon);
3947 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003948 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003949
3950 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3951
Michael Jurka0423dcf2010-10-05 14:56:18 -07003952 }
3953
Winson Chung1b884092012-06-08 17:13:02 -07003954 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003955 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003956 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003957 }
3958
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003959 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003960 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003961 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003962 }
3963
Winson Chungbb185bd2011-11-21 12:31:42 -08003964 private void invalidatePressedFocusedStates(View container, View button) {
3965 if (container instanceof HolographicLinearLayout) {
3966 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3967 layout.invalidatePressedFocusedStates();
3968 } else if (button instanceof HolographicImageView) {
3969 HolographicImageView view = (HolographicImageView) button;
3970 view.invalidatePressedFocusedStates();
3971 }
3972 }
3973
Cristina Stancu476493b2013-08-07 17:20:14 +01003974 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003975 if (mQsb == null) {
3976 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3977 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003978 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003979 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003980 }
3981
3982 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003983 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003984 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003985 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003986 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003987
Winson Chung1cad91e2011-05-25 17:41:01 -07003988 final SearchManager searchManager =
3989 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3990 ComponentName activityName = searchManager.getGlobalSearchActivity();
3991 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003992 int coi = getCurrentOrientationIndexForGlobalIcons();
3993 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003994 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3995 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3996 if (sGlobalSearchIcon[coi] == null) {
3997 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3998 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3999 TOOLBAR_ICON_METADATA_NAME);
4000 }
4001
Winson Chungc51db6a2011-10-05 11:44:49 -07004002 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4003 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004004 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004005 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004006 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004007 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004008 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4009 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004010 if (searchButton != null) searchButton.setVisibility(View.GONE);
4011 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004012 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004013 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004014 }
4015 }
4016
Cristina Stancu476493b2013-08-07 17:20:14 +01004017 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004018 final View searchButtonContainer = findViewById(R.id.search_button_container);
4019 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004020 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004021 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004022 }
4023
Cristina Stancu476493b2013-08-07 17:20:14 +01004024 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004025 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004026 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004027
Winson Chungc51db6a2011-10-05 11:44:49 -07004028 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004029 final SearchManager searchManager =
4030 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4031 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4032
4033 ComponentName activityName = null;
4034 if (globalSearchActivity != null) {
4035 // Check if the global search activity handles voice search
4036 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4037 intent.setPackage(globalSearchActivity.getPackageName());
4038 activityName = intent.resolveActivity(getPackageManager());
4039 }
4040
4041 if (activityName == null) {
4042 // Fallback: check if an activity other than the global search activity
4043 // resolves this
4044 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4045 activityName = intent.resolveActivity(getPackageManager());
4046 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004047 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004048 int coi = getCurrentOrientationIndexForGlobalIcons();
4049 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004050 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4051 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4052 if (sVoiceSearchIcon[coi] == null) {
4053 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4054 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4055 TOOLBAR_ICON_METADATA_NAME);
4056 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004057 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004058 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004059 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004060 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004061 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004062 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004063 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004064 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004065 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004066 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004067 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004068 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004069
Cristina Stancu476493b2013-08-07 17:20:14 +01004070 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004071 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4072 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004073 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004074 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004075 }
4076
Winson Chung5841efa2013-09-30 18:06:44 -07004077 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004078 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4079 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004080 boolean visible = !forceDisableVoiceButtonProxy &&
4081 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004082 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004083 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004084 }
4085 }
Winson Chung5841efa2013-09-30 18:06:44 -07004086
4087 /**
4088 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4089 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4090 */
4091 public void disableVoiceButtonProxy(boolean disabled) {
4092 updateVoiceButtonProxyVisible(disabled);
4093 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004094
Michael Jurkad7c28052012-04-27 15:43:36 -07004095 @Override
4096 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004097 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004098 final List<CharSequence> text = event.getText();
4099 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004100 // Populate event with a fake title based on the current state.
4101 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004102 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004103 } else {
4104 text.add(getString(R.string.all_apps_home_button_label));
4105 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004106 return result;
4107 }
4108
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004109 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004110 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004111 */
4112 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4113 @Override
4114 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004115 closeSystemDialogs();
4116 }
4117 }
4118
4119 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004120 * Receives notifications whenever the appwidgets are reset.
4121 */
4122 private class AppWidgetResetObserver extends ContentObserver {
4123 public AppWidgetResetObserver() {
4124 super(new Handler());
4125 }
4126
4127 @Override
4128 public void onChange(boolean selfChange) {
4129 onAppWidgetReset();
4130 }
4131 }
4132
4133 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004134 * If the activity is currently paused, signal that we need to run the passed Runnable
4135 * in onResume.
4136 *
4137 * This needs to be called from incoming places where resources might have been loaded
4138 * while we are paused. That is becaues the Configuration might be wrong
4139 * when we're not running, and if it comes back to what it was when we
4140 * were paused, we are not restarted.
4141 *
4142 * Implementation of the method from LauncherModel.Callbacks.
4143 *
4144 * @return true if we are currently paused. The caller might be able to
4145 * skip some work in that case since we will come back again.
4146 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004147 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004148 if (mPaused) {
4149 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004150 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004151 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004152 }
4153 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004154 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004155 return true;
4156 } else {
4157 return false;
4158 }
4159 }
4160
Michael Jurkac402cd92013-05-20 15:49:32 +02004161 private boolean waitUntilResume(Runnable run) {
4162 return waitUntilResume(run, false);
4163 }
4164
Michael Jurka1e2f4652013-07-08 18:03:46 -07004165 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004166 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004167 }
4168
Michael Jurka7607c2f2013-04-03 14:33:19 -07004169 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004170 * If the activity is currently paused, signal that we need to re-run the loader
4171 * in onResume.
4172 *
4173 * This needs to be called from incoming places where resources might have been loaded
4174 * while we are paused. That is becaues the Configuration might be wrong
4175 * when we're not running, and if it comes back to what it was when we
4176 * were paused, we are not restarted.
4177 *
4178 * Implementation of the method from LauncherModel.Callbacks.
4179 *
4180 * @return true if we are currently paused. The caller might be able to
4181 * skip some work in that case since we will come back again.
4182 */
4183 public boolean setLoadOnResume() {
4184 if (mPaused) {
4185 Log.i(TAG, "setLoadOnResume");
4186 mOnResumeNeedsLoad = true;
4187 return true;
4188 } else {
4189 return false;
4190 }
4191 }
4192
4193 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004195 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004197 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004198 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004199 } else {
4200 return SCREEN_COUNT / 2;
4201 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004203
Joe Onorato9c1289c2009-08-17 11:03:03 -04004204 /**
4205 * Refreshes the shortcuts shown on the workspace.
4206 *
4207 * Implementation of the method from LauncherModel.Callbacks.
4208 */
4209 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004210 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004211
Michael Jurka7607c2f2013-04-03 14:33:19 -07004212 // If we're starting binding all over again, clear any bind calls we'd postponed in
4213 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4214 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004215 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004216
Winson Chungd64d1762013-08-20 14:37:16 -07004217 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004218 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004219 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004220
Michael Jurka05bf6442011-11-30 20:28:53 -08004221 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004222 if (mHotseat != null) {
4223 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004224 }
4225 }
4226
Adam Cohendcd297f2013-06-18 13:13:40 -07004227 @Override
4228 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004229 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004230
Adam Cohen5084cba2013-09-03 12:01:16 -07004231 // If there are no screens, we need to have an empty screen
4232 if (orderedScreenIds.size() == 0) {
4233 mWorkspace.addExtraEmptyScreen();
4234 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004235
4236 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004237 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004238 if (hasCustomContentToLeft()) {
4239 mWorkspace.createCustomContentContainer();
4240 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004241 }
Winson Chung64359a52013-07-08 17:17:08 -07004242 }
4243
4244 @Override
4245 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004246 // Log to disk
4247 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4248 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4249 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004250 int count = orderedScreenIds.size();
4251 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004252 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004253 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004254 }
4255
Adam Cohen39a06042013-07-19 14:30:12 -07004256 private boolean shouldShowWeightWatcher() {
4257 String spKey = LauncherAppState.getSharedPreferencesKey();
4258 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004259 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004260
4261 return show;
4262 }
4263
4264 private void toggleShowWeightWatcher() {
4265 String spKey = LauncherAppState.getSharedPreferencesKey();
4266 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4267 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4268
4269 show = !show;
4270
4271 SharedPreferences.Editor editor = sp.edit();
4272 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4273 editor.commit();
4274
4275 if (mWeightWatcher != null) {
4276 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4277 }
4278 }
4279
Winson Chungd64d1762013-08-20 14:37:16 -07004280 public void bindAppsAdded(final ArrayList<Long> newScreens,
4281 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004282 final ArrayList<ItemInfo> addAnimated,
4283 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004284 Runnable r = new Runnable() {
4285 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004286 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004287 }
4288 };
4289 if (waitUntilResume(r)) {
4290 return;
4291 }
4292
Winson Chungd64d1762013-08-20 14:37:16 -07004293 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004294 if (newScreens != null) {
4295 bindAddScreens(newScreens);
4296 }
Winson Chungd64d1762013-08-20 14:37:16 -07004297
4298 // We add the items without animation on non-visible pages, and with
4299 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004300 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004301 bindItems(addNotAnimated, 0,
4302 addNotAnimated.size(), false);
4303 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004304 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004305 bindItems(addAnimated, 0,
4306 addAnimated.size(), true);
4307 }
Winson Chungc58497e2013-09-03 17:48:37 -07004308
Winson Chung87412982013-10-03 18:34:14 -07004309 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004310 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004311
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004312 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004313 addedApps != null && mAppsCustomizeContent != null) {
4314 mAppsCustomizeContent.addApps(addedApps);
4315 }
Winson Chungd64d1762013-08-20 14:37:16 -07004316 }
4317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004318 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004319 * Bind the items start-end from the list.
4320 *
4321 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004322 */
Winson Chung64359a52013-07-08 17:17:08 -07004323 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4324 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004325 Runnable r = new Runnable() {
4326 public void run() {
4327 bindItems(shortcuts, start, end, forceAnimateIcons);
4328 }
4329 };
4330 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004331 return;
4332 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004333
Winson Chungf0c6ae02012-03-21 16:10:31 -07004334 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004335 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4336 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004337 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004338 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004339 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004340 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004341 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004342
4343 // Short circuit if we are loading dock items for a configuration which has no dock
4344 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4345 mHotseat == null) {
4346 continue;
4347 }
4348
Joe Onorato9c1289c2009-08-17 11:03:03 -04004349 switch (item.itemType) {
4350 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4351 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004352 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004353 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004354
Winson Chung64359a52013-07-08 17:17:08 -07004355 /*
4356 * TODO: FIX collision case
4357 */
Winson Chungce376632013-07-11 16:12:41 -07004358 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4359 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4360 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004361 View v = cl.getChildAt(item.cellX, item.cellY);
4362 Object tag = v.getTag();
4363 String desc = "Collision while binding workspace item: " + item
4364 + ". Collides with " + tag;
4365 if (LauncherAppState.isDogfoodBuild()) {
4366 throw (new RuntimeException(desc));
4367 } else {
4368 Log.d(TAG, desc);
4369 }
Winson Chungce376632013-07-11 16:12:41 -07004370 }
Winson Chung64359a52013-07-08 17:17:08 -07004371 }
4372
Adam Cohendcd297f2013-06-18 13:13:40 -07004373 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4374 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004375 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004376 // Animate all the applications up now
4377 shortcut.setAlpha(0f);
4378 shortcut.setScaleX(0f);
4379 shortcut.setScaleY(0f);
4380 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004381 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004383 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004384 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004385 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004386 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004387 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004388 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4389 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004390 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004391 default:
4392 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004394 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004395
Winson Chung997a9232013-07-24 15:33:46 -07004396 if (animateIcons) {
4397 // Animate to the correct page
4398 if (newShortcutsScreenId > -1) {
4399 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004400 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004401 final Runnable startBounceAnimRunnable = new Runnable() {
4402 public void run() {
4403 anim.playTogether(bounceAnims);
4404 anim.start();
4405 }
4406 };
Winson Chung997a9232013-07-24 15:33:46 -07004407 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004408 // We post the animation slightly delayed to prevent slowdowns
4409 // when we are loading right after we return to launcher.
4410 mWorkspace.postDelayed(new Runnable() {
4411 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004412 if (mWorkspace != null) {
4413 mWorkspace.snapToPage(newScreenIndex);
4414 mWorkspace.postDelayed(startBounceAnimRunnable,
4415 NEW_APPS_ANIMATION_DELAY);
4416 }
Winson Chung94d67682013-09-25 16:29:40 -07004417 }
4418 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004419 } else {
4420 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004421 }
4422 }
Winson Chung64359a52013-07-08 17:17:08 -07004423 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004424 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004425 }
4426
Joe Onorato9c1289c2009-08-17 11:03:03 -04004427 /**
4428 * Implementation of the method from LauncherModel.Callbacks.
4429 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004430 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004431 Runnable r = new Runnable() {
4432 public void run() {
4433 bindFolders(folders);
4434 }
4435 };
4436 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004437 return;
4438 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004439 sFolders.clear();
4440 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004441 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004442
4443 /**
4444 * Add the views for a widget to the workspace.
4445 *
4446 * Implementation of the method from LauncherModel.Callbacks.
4447 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004448 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004449 Runnable r = new Runnable() {
4450 public void run() {
4451 bindAppWidget(item);
4452 }
4453 };
4454 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004455 return;
4456 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004457
Daniel Sandler843e8602010-06-07 14:59:01 -04004458 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4459 if (DEBUG_WIDGETS) {
4460 Log.d(TAG, "bindAppWidget: " + item);
4461 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004462 final Workspace workspace = mWorkspace;
4463
Sunny Goyalff572272014-07-23 13:58:07 -07004464 AppWidgetProviderInfo appWidgetInfo;
4465 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4466 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4467
4468 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4469 if (appWidgetInfo == null) {
4470 if (DEBUG_WIDGETS) {
4471 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4472 + " belongs to component " + item.providerName
4473 + ", as the povider is null");
4474 }
4475 LauncherModel.deleteItemFromDatabase(this, item);
4476 return;
4477 }
4478 // Note: This assumes that the id remap broadcast is received before this step.
4479 // If that is not the case, the id remap will be ignored and user may see the
4480 // click to setup view.
4481 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4482 pendingInfo.spanX = item.spanX;
4483 pendingInfo.spanY = item.spanY;
4484 pendingInfo.minSpanX = item.minSpanX;
4485 pendingInfo.minSpanY = item.minSpanY;
4486 Bundle options =
4487 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4488
Sunny Goyalff572272014-07-23 13:58:07 -07004489 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004490 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4491 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004492
4493 // TODO consider showing a permission dialog when the widget is clicked.
4494 if (!success) {
4495 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4496 if (DEBUG_WIDGETS) {
4497 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4498 + " belongs to component " + item.providerName
4499 + ", as the launcher is unable to bing a new widget id");
4500 }
4501 LauncherModel.deleteItemFromDatabase(this, item);
4502 return;
4503 }
4504
4505 item.appWidgetId = newWidgetId;
4506
4507 // If the widget has a configure activity, it is still needs to set it up, otherwise
4508 // the widget is ready to go.
4509 item.restoreStatus = (appWidgetInfo.configure == null)
4510 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4511 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4512
4513 LauncherModel.updateItemInDatabase(this, item);
4514 }
4515
Sunny Goyal651077b2014-06-30 14:15:31 -07004516 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4517 final int appWidgetId = item.appWidgetId;
4518 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4519 if (DEBUG_WIDGETS) {
4520 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4521 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004522
Sunny Goyal651077b2014-06-30 14:15:31 -07004523 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4524 } else {
4525 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004526 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4527 view.updateIcon(mIconCache);
4528 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004529 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004530 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004532
Joe Onorato9c1289c2009-08-17 11:03:03 -04004533 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004534 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004535
Adam Cohendcd297f2013-06-18 13:13:40 -07004536 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004537 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004538 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4539
Joe Onorato9c1289c2009-08-17 11:03:03 -04004540 workspace.requestLayout();
4541
Daniel Sandler843e8602010-06-07 14:59:01 -04004542 if (DEBUG_WIDGETS) {
4543 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4544 + (SystemClock.uptimeMillis()-start) + "ms");
4545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004546 }
4547
Sunny Goyalff572272014-07-23 13:58:07 -07004548 /**
4549 * Restores a pending widget.
4550 *
4551 * @param appWidgetId The app widget id
4552 * @param cellInfo The position on screen where to create the widget.
4553 */
4554 private void completeRestoreAppWidget(final int appWidgetId) {
4555 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4556 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4557 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4558 return;
4559 }
4560
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004561 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004562 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4563
4564 mWorkspace.reinflateWidgetsIfNecessary();
4565 LauncherModel.updateItemInDatabase(this, info);
4566 }
4567
Adam Cohen1462de32012-07-24 22:34:36 -07004568 public void onPageBoundSynchronously(int page) {
4569 mSynchronouslyBoundPages.add(page);
4570 }
4571
Joe Onorato9c1289c2009-08-17 11:03:03 -04004572 /**
4573 * Callback saying that there aren't any more items to bind.
4574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Adam Cohene25af792013-06-06 23:08:25 -07004577 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004578 Runnable r = new Runnable() {
4579 public void run() {
4580 finishBindingItems(upgradePath);
4581 }
4582 };
4583 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004584 return;
4585 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004586 if (mSavedState != null) {
4587 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004588 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004590 mSavedState = null;
4591 }
4592
Adam Cohen1462de32012-07-24 22:34:36 -07004593 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004594
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004595 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004596 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004597
4598 // If we received the result of any pending adds while the loader was running (e.g. the
4599 // widget configuration forced an orientation change), process them now.
4600 if (sPendingAddItem != null) {
4601 final long screenId = completeAdd(sPendingAddItem);
4602
4603 // TODO: this moves the user to the page where the pending item was added. Ideally,
4604 // the screen would be guaranteed to exist after bind, and the page would be set through
4605 // the workspace restore process.
4606 mWorkspace.post(new Runnable() {
4607 @Override
4608 public void run() {
4609 mWorkspace.snapToScreenId(screenId);
4610 }
4611 });
4612 sPendingAddItem = null;
4613 }
4614
Adam Cohene25af792013-06-06 23:08:25 -07004615 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004616 mWorkspace.getUniqueComponents(true, null);
4617 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004618 }
Sunny Goyale755d462014-07-22 13:48:29 -07004619 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004620 }
4621
Adam Cohen3ed316a2014-07-23 18:21:20 -07004622 private void sendLoadingCompleteBroadcastIfNecessary() {
4623 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4624 String permission =
4625 getResources().getString(R.string.receive_first_load_broadcast_permission);
4626 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4627 sendBroadcast(intent, permission);
4628 SharedPreferences.Editor editor = mSharedPrefs.edit();
4629 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4630 editor.apply();
4631 }
4632 }
4633
Winson Chunga0b7e862013-09-05 16:03:15 -07004634 public boolean isAllAppsButtonRank(int rank) {
4635 if (mHotseat != null) {
4636 return mHotseat.isAllAppsButtonRank(rank);
4637 }
4638 return false;
4639 }
4640
Winson Chunga2413752012-04-03 14:22:34 -07004641 private boolean canRunNewAppsAnimation() {
4642 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4643 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4644 }
4645
Winson Chungc9168342013-06-26 14:54:55 -07004646 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4647 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4648 PropertyValuesHolder.ofFloat("alpha", 1f),
4649 PropertyValuesHolder.ofFloat("scaleX", 1f),
4650 PropertyValuesHolder.ofFloat("scaleY", 1f));
4651 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4652 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4653 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4654 return bounceAnim;
4655 }
4656
Adam Cohen27772732013-11-11 14:00:56 +00004657 public boolean useVerticalBarLayout() {
4658 return LauncherAppState.getInstance().getDynamicGrid().
4659 getDeviceProfile().isVerticalBarLayout();
4660 }
4661
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004662 protected Rect getSearchBarBounds() {
4663 return LauncherAppState.getInstance().getDynamicGrid().
4664 getDeviceProfile().getSearchBarBounds();
4665 }
4666
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004667 @Override
4668 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004669 boolean searchVisible = updateGlobalSearchIcon();
4670 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004671 if (mSearchDropTargetBar != null) {
4672 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4673 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004674 }
4675
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004676 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004677 * Add the icons for all apps.
4678 *
4679 * Implementation of the method from LauncherModel.Callbacks.
4680 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004681 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004682 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004683 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4684 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4685 getHotseat().addAllAppsFolder(mIconCache, apps,
4686 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4687 }
4688 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004689 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004690 if (mAppsCustomizeContent != null) {
4691 mAppsCustomizeContent.onPackagesUpdated(
4692 LauncherModel.getSortedWidgetsAndShortcuts(this));
4693 }
Winson Chungc58497e2013-09-03 17:48:37 -07004694 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004695 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004696 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004697 mAppsCustomizeContent.onPackagesUpdated(
4698 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004699 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004701 }
4702
4703 /**
4704 * A package was updated.
4705 *
4706 * Implementation of the method from LauncherModel.Callbacks.
4707 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004708 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004709 Runnable r = new Runnable() {
4710 public void run() {
4711 bindAppsUpdated(apps);
4712 }
4713 };
4714 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004715 return;
4716 }
4717
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004718 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004719 mAppsCustomizeContent != null) {
4720 mAppsCustomizeContent.updateApps(apps);
4721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004722 }
4723
Sunny Goyal4390ace2014-10-13 11:33:11 -07004724 @Override
4725 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4726 Runnable r = new Runnable() {
4727 public void run() {
4728 bindWidgetsRestored(widgets);
4729 }
4730 };
4731 if (waitUntilResume(r)) {
4732 return;
4733 }
4734 mWorkspace.widgetsRestored(widgets);
4735 }
4736
Joe Onorato9c1289c2009-08-17 11:03:03 -04004737 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004738 * Some shortcuts were updated in the background.
4739 *
4740 * Implementation of the method from LauncherModel.Callbacks.
4741 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004742 @Override
4743 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4744 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004745 Runnable r = new Runnable() {
4746 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004747 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004748 }
4749 };
4750 if (waitUntilResume(r)) {
4751 return;
4752 }
4753
Sunny Goyal4390ace2014-10-13 11:33:11 -07004754 if (!updated.isEmpty()) {
4755 mWorkspace.updateShortcuts(updated);
4756 }
4757
4758 if (!removed.isEmpty()) {
4759 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4760 for (ShortcutInfo si : removed) {
4761 removedComponents.add(si.getTargetComponent());
4762 }
4763 mWorkspace.removeItemsByComponentName(removedComponents, user);
4764 // Notify the drag controller
4765 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004766 }
4767 }
4768
4769 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004770 * Update the state of a package, typically related to install state.
4771 *
4772 * Implementation of the method from LauncherModel.Callbacks.
4773 */
Sunny Goyale755d462014-07-22 13:48:29 -07004774 @Override
4775 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004776 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004777 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004778 }
4779 }
4780
4781 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004782 * Update the label and icon of all the icons in a package
4783 *
4784 * Implementation of the method from LauncherModel.Callbacks.
4785 */
4786 @Override
4787 public void updatePackageBadge(String packageName) {
4788 if (mWorkspace != null) {
4789 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4790 }
4791 }
4792
4793 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004794 * A package was uninstalled. We take both the super set of packageNames
4795 * in addition to specific applications to remove, the reason being that
4796 * this can be called when a package is updated as well. In that scenario,
4797 * we only remove specific components from the workspace, where as
4798 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004799 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004800 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004801 * Implementation of the method from LauncherModel.Callbacks.
4802 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004803 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004804 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004805 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004806 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004807 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004808 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004809 }
Winson Chungd64d1762013-08-20 14:37:16 -07004810 };
4811 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004812 return;
4813 }
4814
Sunny Goyal1a745e82014-10-02 15:58:31 -07004815 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004816 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4817 for (AppInfo info : appInfos) {
4818 removedComponents.add(info.componentName);
4819 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004820 if (!packageNames.isEmpty()) {
4821 mWorkspace.removeItemsByPackageName(packageNames, user);
4822 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004823 if (!removedComponents.isEmpty()) {
4824 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004825 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004826 // Notify the drag controller
4827 mDragController.onAppsRemoved(packageNames, removedComponents);
4828
Sunny Goyal1a745e82014-10-02 15:58:31 -07004829 } else {
4830 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004831 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004832
Winson Chungdf95eb12013-10-16 14:57:07 -07004833 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004834 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004835 mAppsCustomizeContent != null) {
4836 mAppsCustomizeContent.removeApps(appInfos);
4837 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004838 }
Joe Onoratobe386092009-11-17 17:32:16 -08004839
4840 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004841 * A number of packages were updated.
4842 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004843 private ArrayList<Object> mWidgetsAndShortcuts;
4844 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004845 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004846 bindPackagesUpdated(mWidgetsAndShortcuts);
4847 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004848 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004849 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004850 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4851 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4852 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004853 return;
4854 }
4855
Winson Chung64359a52013-07-08 17:17:08 -07004856 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004857 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004858 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004859 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004860 }
4861
Winson Chung400438b2011-01-16 17:53:48 -08004862 private int mapConfigurationOriActivityInfoOri(int configOri) {
4863 final Display d = getWindowManager().getDefaultDisplay();
4864 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4865 switch (d.getRotation()) {
4866 case Surface.ROTATION_0:
4867 case Surface.ROTATION_180:
4868 // We are currently in the same basic orientation as the natural orientation
4869 naturalOri = configOri;
4870 break;
4871 case Surface.ROTATION_90:
4872 case Surface.ROTATION_270:
4873 // We are currently in the other basic orientation to the natural orientation
4874 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4875 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4876 break;
4877 }
4878
4879 int[] oriMap = {
4880 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4881 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4882 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4883 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4884 };
4885 // Since the map starts at portrait, we need to offset if this device's natural orientation
4886 // is landscape.
4887 int indexOffset = 0;
4888 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4889 indexOffset = 1;
4890 }
4891 return oriMap[(d.getRotation() + indexOffset) % 4];
4892 }
Adam Cohen446e9402011-09-15 18:21:21 -07004893
Winson Chung4b919f82012-05-01 10:44:08 -07004894 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004895 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004896 getResources().getBoolean(R.bool.allow_rotation);
4897 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004898 }
Winson Chung4b919f82012-05-01 10:44:08 -07004899 public void lockScreenOrientation() {
4900 if (isRotationEnabled()) {
4901 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4902 .getConfiguration().orientation));
4903 }
4904 }
4905 public void unlockScreenOrientation(boolean immediate) {
4906 if (isRotationEnabled()) {
4907 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004908 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004909 } else {
4910 mHandler.postDelayed(new Runnable() {
4911 public void run() {
4912 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4913 }
4914 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004915 }
Winson Chung4b919f82012-05-01 10:44:08 -07004916 }
Winson Chung400438b2011-01-16 17:53:48 -08004917 }
4918
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004919 /**
4920 * Called when the SearchBar hint should be changed.
4921 *
4922 * @param hint the hint to be displayed in the search bar.
4923 */
4924 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004925
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004926 }
4927
Winson Chunge43a1e72014-01-15 10:33:02 -08004928 protected boolean isLauncherPreinstalled() {
4929 PackageManager pm = getPackageManager();
4930 try {
4931 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4932 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4933 return true;
4934 } else {
4935 return false;
4936 }
4937 } catch (NameNotFoundException e) {
4938 e.printStackTrace();
4939 return false;
4940 }
4941 }
4942
Adam Cohenea90f832014-05-21 15:03:34 -07004943 /**
4944 * This method indicates whether or not we should suggest default wallpaper dimensions
4945 * when our wallpaper cropper was not yet used to set a wallpaper.
4946 */
4947 protected boolean overrideWallpaperDimensions() {
4948 return true;
4949 }
4950
Adam Cohen432609a2014-03-13 17:03:22 -07004951 /**
4952 * To be overridden by subclasses to indicate that there is an activity to launch
4953 * before showing the standard launcher experience.
4954 */
4955 protected boolean hasFirstRunActivity() {
4956 return false;
4957 }
4958
4959 /**
4960 * To be overridden by subclasses to launch any first run activity
4961 */
4962 protected Intent getFirstRunActivity() {
4963 return null;
4964 }
4965
Winson Chunga6945242014-01-08 14:04:34 -08004966 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004967 return !ActivityManager.isRunningInTestHarness() &&
4968 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004969 }
4970
Adam Cohen4a9423d2014-03-28 14:22:31 -07004971 protected boolean hasRunFirstRunActivity() {
4972 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4973 }
4974
Adam Cohen432609a2014-03-13 17:03:22 -07004975 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004976 if (shouldRunFirstRunActivity() &&
4977 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004978 Intent firstRunIntent = getFirstRunActivity();
4979 if (firstRunIntent != null) {
4980 startActivity(firstRunIntent);
4981 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004982 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004983 }
4984 }
Adam Cohen432609a2014-03-13 17:03:22 -07004985 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004986 }
4987
4988 private void markFirstRunActivityShown() {
4989 SharedPreferences.Editor editor = mSharedPrefs.edit();
4990 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4991 editor.apply();
4992 }
4993
Adam Cohen432609a2014-03-13 17:03:22 -07004994 /**
4995 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4996 * screen that must be displayed and dismissed.
4997 */
4998 protected boolean hasDismissableIntroScreen() {
4999 return false;
5000 }
5001
5002 /**
5003 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5004 */
5005 protected View getIntroScreen() {
5006 return null;
5007 }
5008
5009 /**
5010 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5011 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5012 */
5013 private boolean shouldShowIntroScreen() {
5014 return hasDismissableIntroScreen() &&
5015 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5016 }
5017
5018 protected void showIntroScreen() {
5019 View introScreen = getIntroScreen();
5020 changeWallpaperVisiblity(false);
5021 if (introScreen != null) {
5022 mDragLayer.showOverlayView(introScreen);
5023 }
5024 }
5025
5026 public void dismissIntroScreen() {
5027 markIntroScreenDismissed();
5028 if (showFirstRunActivity()) {
5029 // We delay hiding the intro view until the first run activity is showing. This
5030 // avoids a blip.
5031 mWorkspace.postDelayed(new Runnable() {
5032 @Override
5033 public void run() {
5034 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005035 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005036 }
5037 }, ACTIVITY_START_DELAY);
5038 } else {
5039 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005040 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005041 }
5042 changeWallpaperVisiblity(true);
5043 }
5044
5045 private void markIntroScreenDismissed() {
5046 SharedPreferences.Editor editor = mSharedPrefs.edit();
5047 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5048 editor.apply();
5049 }
5050
Sunny Goyal88d60f12014-09-08 03:47:29 -07005051 private void showFirstRunClings() {
5052 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5053 // on the device, then we always show the first run cling experience (or if there is no
5054 // launcher2). Otherwise, we prompt the user upon started for migration
5055 LauncherClings launcherClings = new LauncherClings(this);
5056 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5057 if (mModel.canMigrateFromOldLauncherDb(this)) {
5058 launcherClings.showMigrationCling();
5059 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005060 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005061 }
5062 }
5063 }
5064
Winson Chunga6945242014-01-08 14:04:34 -08005065 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005066 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5067 if (mHotseat != null) mHotseat.setAlpha(1f);
5068 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5069 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005070 }
5071
5072 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005073 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5074 if (mHotseat != null) mHotseat.setAlpha(0f);
5075 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5076 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005077 }
5078
Mathew Inwood72fbec12013-11-19 15:45:07 +00005079 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005080 // Called from search suggestion, not supported in other profiles.
5081 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5082 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5083 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5084 myUser);
5085 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005086 return null;
5087 }
Kenny Guyed131872014-04-30 03:02:21 +01005088 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005089 }
5090
5091 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5092 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005093 // Called from search suggestion, not supported in other profiles.
5094 return createShortcutDragInfo(shortcutIntent, caption, icon,
5095 UserHandleCompat.myUserHandle());
5096 }
5097
5098 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5099 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005100 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005101 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005102 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005103 }
5104
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005105 protected void moveWorkspaceToDefaultScreen() {
5106 mWorkspace.moveToDefaultScreen(false);
5107 }
5108
Mathew Inwood72fbec12013-11-19 15:45:07 +00005109 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5110 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005111 mWorkspace.onExternalDragStartedWithItem(dragView);
5112 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005113 }
5114
Anjali Koppalf5d29132014-02-28 13:33:27 -08005115 @Override
5116 public void onPageSwitch(View newPage, int newPageIndex) {
5117 }
5118
Winson Chung80baf5a2010-08-09 16:03:15 -07005119 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005120 * Prints out out state for debugging.
5121 */
5122 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005123 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005124 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005125 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5126 Log.d(TAG, "mRestoring=" + mRestoring);
5127 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5128 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005129 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005130 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005131
Winson Chung785d2eb2011-04-14 16:08:02 -07005132 if (mAppsCustomizeContent != null) {
5133 mAppsCustomizeContent.dumpState();
5134 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005135 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005136 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005137
5138 @Override
5139 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5140 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005141 synchronized (sDumpLogs) {
5142 writer.println(" ");
5143 writer.println("Debug logs: ");
5144 for (int i = 0; i < sDumpLogs.size(); i++) {
5145 writer.println(" " + sDumpLogs.get(i));
5146 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005147 }
5148 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005149
5150 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005151 if (DEBUG_DUMP_LOG) {
5152 synchronized (sDumpLogs) {
5153 Log.d(TAG, "");
5154 Log.d(TAG, "*********************");
5155 Log.d(TAG, "Launcher debug logs: ");
5156 for (int i = 0; i < sDumpLogs.size(); i++) {
5157 Log.d(TAG, " " + sDumpLogs.get(i));
5158 }
5159 Log.d(TAG, "*********************");
5160 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005161 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005162 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005163 }
5164
5165 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005166 addDumpLog(tag, log, null, debugLog);
5167 }
5168
5169 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005170 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005171 if (e != null) {
5172 Log.d(tag, log, e);
5173 } else {
5174 Log.d(tag, log);
5175 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005176 }
Winson Chungede41292013-09-19 16:27:36 -07005177 if (DEBUG_DUMP_LOG) {
5178 sDateStamp.setTime(System.currentTimeMillis());
5179 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005180 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5181 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005182 }
Winson Chungede41292013-09-19 16:27:36 -07005183 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005184 }
5185
Winson Chungede41292013-09-19 16:27:36 -07005186 public void dumpLogsToLocalData() {
5187 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005188 new AsyncTask<Void, Void, Void>() {
5189 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005190 boolean success = false;
5191 sDateStamp.setTime(sRunStart);
5192 String FILENAME = sDateStamp.getMonth() + "-"
5193 + sDateStamp.getDay() + "_"
5194 + sDateStamp.getHours() + "-"
5195 + sDateStamp.getMinutes() + "_"
5196 + sDateStamp.getSeconds() + ".txt";
5197
5198 FileOutputStream fos = null;
5199 File outFile = null;
5200 try {
5201 outFile = new File(getFilesDir(), FILENAME);
5202 outFile.createNewFile();
5203 fos = new FileOutputStream(outFile);
5204 } catch (Exception e) {
5205 e.printStackTrace();
5206 }
5207 if (fos != null) {
5208 PrintWriter writer = new PrintWriter(fos);
5209
5210 writer.println(" ");
5211 writer.println("Debug logs: ");
5212 synchronized (sDumpLogs) {
5213 for (int i = 0; i < sDumpLogs.size(); i++) {
5214 writer.println(" " + sDumpLogs.get(i));
5215 }
5216 }
5217 writer.close();
5218 }
5219 try {
5220 if (fos != null) {
5221 fos.close();
5222 success = true;
5223 }
5224 } catch (IOException e) {
5225 e.printStackTrace();
5226 }
Michael Jurka43467462013-11-14 12:03:58 +01005227 return null;
Winson Chungede41292013-09-19 16:27:36 -07005228 }
Michael Jurka43467462013-11-14 12:03:58 +01005229 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005230 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005232}
Michael Jurka2763be32011-02-24 11:19:57 -08005233
Michael Jurkaabded662011-03-04 12:06:57 -08005234interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005235 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005236 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005237 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005238 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005239 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005240}
Dan Sandlerd5024042014-01-09 15:01:33 -05005241
5242interface DebugIntents {
5243 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5244 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005245}