blob: 6780812864d1e49d34c8bffebd1b1c69d82097b5 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Sunny Goyalf8763012014-09-23 16:22:10 -070076import android.view.ContextThemeWrapper;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.Display;
78import android.view.Gravity;
79import android.view.HapticFeedbackConstants;
80import android.view.KeyEvent;
81import android.view.LayoutInflater;
82import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.MotionEvent;
84import android.view.Surface;
85import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070086import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080087import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070088import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
90import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080091import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Sunny Goyal651077b2014-06-30 14:15:31 -070092import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080094import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070095import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080096import android.view.animation.DecelerateInterpolator;
Adam Cohen2854d252014-08-27 16:04:07 -070097import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070099import android.widget.Advanceable;
100import android.widget.FrameLayout;
101import android.widget.ImageView;
102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700104
Sunny Goyal651077b2014-06-30 14:15:31 -0700105import com.android.launcher3.DropTarget.DragObject;
106import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700107import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100108import com.android.launcher3.compat.LauncherActivityInfoCompat;
109import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700110import com.android.launcher3.compat.PackageInstallerCompat;
111import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100112import com.android.launcher3.compat.UserHandleCompat;
113import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700114
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.DataInputStream;
116import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700122import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000123import java.lang.reflect.Field;
124import java.lang.reflect.InvocationTargetException;
125import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700128import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700129import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000132import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100137public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700138 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700139 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800140 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700141 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400144 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700145 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700147 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
Dan Sandlerd5024042014-01-09 15:01:33 -0500149 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
167
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800168 static final int SCREEN_COUNT = 5;
169 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Romain Guy98d01652009-06-30 16:21:04 -0700171 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 // To turn on these properties, type
173 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
174 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
175 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800176 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chung2672ff92012-05-04 16:22:30 -0700178 // The Intent extra that defines whether to ignore the launch animation
179 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400180 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
183 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700184 // Type: int
185 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
188 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
190 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700191 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700193 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: boolean
195 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
196 // Type: long
197 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700198 // Type: int
199 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
200 // Type: int
201 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
202 // Type: parcelable
203 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000204 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800205 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000206 // Type: int[]
207 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Adam Cohen432609a2014-03-13 17:03:22 -0700209 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800210 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
211
Adam Cohen3ed316a2014-07-23 18:21:20 -0700212 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
213 static final String ACTION_FIRST_LOAD_COMPLETE =
214 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
215
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100216 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700217 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100218 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700219 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100220 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700221
Adam Cohen39a06042013-07-19 14:30:12 -0700222 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700223 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700224
Winson Chunga6945242014-01-08 14:04:34 -0800225 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
226
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700228 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700229 private State mState = State.WORKSPACE;
230 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700231
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700232 private boolean mIsSafeModeEnabled;
233
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700235 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
236 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700237 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800240 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000242 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
243 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
244
Winson Chunga2413752012-04-03 14:22:34 -0700245 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700246 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
247 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700248 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700249 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700250
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800251 private final BroadcastReceiver mCloseSystemDialogsReceiver
252 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800253 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private LayoutInflater mInflater;
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700258 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800259 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800260 private DragLayer mDragLayer;
261 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700262 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Sunny Goyalffe83f12014-08-14 17:39:34 -0700264 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700266
Michael Jurkac9d95c52011-08-29 14:03:34 -0700267 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700268 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800269 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700270
Michael Jurka0280c3b2010-09-17 15:00:07 -0700271 private int[] mTmpAddItemCellCoordinates = new int[2];
272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private FolderInfo mFolderInfo;
274
Winson Chung3d503fb2011-07-13 17:25:49 -0700275 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800276 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700277
Michael Jurka838a4ca2011-02-07 13:33:06 -0800278 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700279
Winson Chungc51db6a2011-10-05 11:44:49 -0700280 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 private AppsCustomizeTabHost mAppsCustomizeTabHost;
282 private AppsCustomizePagedView mAppsCustomizeContent;
283 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800284 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
288 // scroll issues (because the workspace may not have been measured yet) and extra work.
289 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
290 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
292 private SpannableStringBuilder mDefaultKeySsb = null;
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 private boolean mWorkspaceLoading = true;
295
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800296 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private boolean mRestoring;
298 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700299 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Michael Jurka1e2f4652013-07-08 18:03:46 -0700301 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700302 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private Bundle mSavedInstanceState;
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mUserPresent = true;
309 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700310 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800311 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700313 private static LocaleConfiguration sLocaleConfiguration = null;
314
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700315 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700316
Adam Cohen61f560d2013-09-30 15:58:20 -0700317 private View.OnTouchListener mHapticFeedbackTouchListener;
318
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 // Related to the auto-advancing of widgets
320 private final int ADVANCE_MSG = 1;
321 private final int mAdvanceInterval = 20000;
322 private final int mAdvanceStagger = 250;
323 private long mAutoAdvanceSentTime;
324 private long mAutoAdvanceTimeLeft = -1;
325 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
326 new HashMap<View, AppWidgetProviderInfo>();
327
Winson Chung400438b2011-01-16 17:53:48 -0800328 // Determines how long to wait after a rotation before restoring the screen orientation to
329 // match the sensor state.
330 private final int mRestoreScreenOrientationDelay = 500;
331
Michael Jurka4ef207b2010-11-29 17:05:45 -0800332 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700333 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
334 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700335
Craig Mautner360310b2012-10-26 15:13:08 -0700336 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700337
Adam Cohen1462de32012-07-24 22:34:36 -0700338 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700339 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700340
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700341 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700342 static Date sDateStamp = new Date();
343 static DateFormat sDateFormat =
344 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
345 static long sRunStart = System.currentTimeMillis();
346 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700347
Winson Chung46353de2012-02-16 14:05:10 -0800348 // We only want to get the SharedPreferences once since it does an FS stat each time we get
349 // it from the context.
350 private SharedPreferences mSharedPrefs;
351
Adam Cohene25af792013-06-06 23:08:25 -0700352 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
353
Winson Chungf0c6ae02012-03-21 16:10:31 -0700354 // Holds the page that we need to animate to, and the icon views that we need to animate up
355 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700356 private ImageView mFolderIconImageView;
357 private Bitmap mFolderIconBitmap;
358 private Canvas mFolderIconCanvas;
359 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700360
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 private BubbleTextView mWaitingForResume;
362
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 private Runnable mBuildLayersRunnable = new Runnable() {
364 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700365 if (mWorkspace != null) {
366 mWorkspace.buildPageHardwareLayers();
367 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700368 }
369 };
370
Adam Cohendb364c32014-05-20 14:23:40 -0700371 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372
Michael Jurka7267fa52013-09-26 15:29:57 -0700373 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800374
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 private static class PendingAddArguments {
376 int requestCode;
377 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700378 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int cellX;
381 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700382 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 }
384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 private Stats mStats;
386
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700387 FocusIndicatorView mFocusHandler;
388
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800389 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800390 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700391 }
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 @Override
394 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700395 if (DEBUG_STRICT_MODE) {
396 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
397 .detectDiskReads()
398 .detectDiskWrites()
399 .detectNetwork() // or .detectAll() for all detectable problems
400 .penaltyLog()
401 .build());
402 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
403 .detectLeakedSqlLiteObjects()
404 .detectLeakedClosableObjects()
405 .penaltyLog()
406 .penaltyDeath()
407 .build());
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700414 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 // Determine the dynamic grid properties
416 Point smallestSize = new Point();
417 Point largestSize = new Point();
418 Point realSize = new Point();
419 Display display = getWindowManager().getDefaultDisplay();
420 display.getCurrentSizeRange(smallestSize, largestSize);
421 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700422 DisplayMetrics dm = new DisplayMetrics();
423 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700424
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 // Lazy-initialize the dynamic grid
426 DeviceProfile grid = app.initDynamicGrid(this,
427 Math.min(smallestSize.x, smallestSize.y),
428 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700429 realSize.x, realSize.y,
430 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700431
432 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400433 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700434 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700435 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800436 mModel = app.setLauncher(this);
437 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700438 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400439 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
Sunny Goyalffe83f12014-08-14 17:39:34 -0700444 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100463 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800465 registerContentObservers();
466
Joe Onorato7c312c12009-08-13 21:36:53 -0700467 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 mSavedState = savedInstanceState;
470 restoreState(mSavedState);
471
472 if (PROFILE_STARTUP) {
473 android.os.Debug.stopMethodTracing();
474 }
475
476 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700477 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 // If the user leaves launcher, then we should just load items asynchronously when
479 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500480 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 } else {
482 // We only load the page synchronously if the user rotates (or triggers a
483 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800484 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
487
488 // For handling default keys
489 mDefaultKeySsb = new SpannableStringBuilder();
490 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800491
492 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
493 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 updateGlobalIcons();
496
497 // On large interfaces, we want the screen to auto-rotate based on the current orientation
498 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700499
Adam Cohen432609a2014-03-13 17:03:22 -0700500 if (shouldShowIntroScreen()) {
501 showIntroScreen();
502 } else {
503 showFirstRunActivity();
Sunny Goyal88d60f12014-09-08 03:47:29 -0700504 showFirstRunClings();
Winson Chunga6945242014-01-08 14:04:34 -0800505 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700506 }
507
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700508 @Override
509 public void onLauncherProviderChange() { }
510
Winson Chung98ca0f72013-07-29 12:58:51 -0700511 /** To be overriden by subclasses to hint to Launcher that we have custom content */
512 protected boolean hasCustomContentToLeft() {
513 return false;
514 }
515
Dave Hawkeya8881582013-09-17 15:55:33 -0600516 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500517 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600518 * {@link #addToCustomContentPage}. This will only be invoked if
519 * {@link #hasCustomContentToLeft()} is {@code true}.
520 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500521 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 }
523
524 /**
525 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
526 * ensure the custom content page is added or removed if necessary.
527 */
528 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600529 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600530 // Not bound yet, wait for bindScreens to be called.
531 return;
532 }
533
534 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
535 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500536 mWorkspace.createCustomContentContainer();
537 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
539 mWorkspace.removeCustomContentPage();
540 }
541 }
542
Adam Cohenf9426d52012-06-04 17:26:21 -0700543 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700544 boolean searchVisible = false;
545 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800546 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700547 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700548 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700549 searchVisible = updateGlobalSearchIcon();
550 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700551 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700552 if (sGlobalSearchIcon[coi] != null) {
553 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700554 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700555 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700556 if (sVoiceSearchIcon[coi] != null) {
557 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700558 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700559 }
Winson Chungadf0c182012-08-23 14:59:07 -0700560 if (mSearchDropTargetBar != null) {
561 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
Romain Guycbb89e42009-06-08 15:52:54 -0700564
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700566 if (sLocaleConfiguration == null) {
567 new AsyncTask<Void, Void, LocaleConfiguration>() {
568 @Override
569 protected LocaleConfiguration doInBackground(Void... unused) {
570 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
571 readConfiguration(Launcher.this, localeConfiguration);
572 return localeConfiguration;
573 }
574
575 @Override
576 protected void onPostExecute(LocaleConfiguration result) {
577 sLocaleConfiguration = result;
578 checkForLocaleChange(); // recursive, but now with a locale configuration
579 }
580 }.execute();
581 return;
582 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700583
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final Configuration configuration = getResources().getConfiguration();
585
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 final String locale = configuration.locale.toString();
588
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final int mcc = configuration.mcc;
591
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 final int mnc = configuration.mnc;
594
Romain Guy84f296c2009-11-04 15:00:44 -0800595 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596
Romain Guy84f296c2009-11-04 15:00:44 -0800597 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 sLocaleConfiguration.locale = locale;
599 sLocaleConfiguration.mcc = mcc;
600 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700601
Joe Onorato0589f0f2010-02-08 13:44:00 -0800602 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603
604 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100605 new AsyncTask<Void, Void, Void>() {
606 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100608 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 }
Michael Jurka43467462013-11-14 12:03:58 +0100610 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700611 }
612 }
613
614 private static class LocaleConfiguration {
615 public String locale;
616 public int mcc = -1;
617 public int mnc = -1;
618 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700619
Romain Guy98d01652009-06-30 16:21:04 -0700620 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
621 DataInputStream in = null;
622 try {
623 in = new DataInputStream(context.openFileInput(PREFERENCES));
624 configuration.locale = in.readUTF();
625 configuration.mcc = in.readInt();
626 configuration.mnc = in.readInt();
627 } catch (FileNotFoundException e) {
628 // Ignore
629 } catch (IOException e) {
630 // Ignore
631 } finally {
632 if (in != null) {
633 try {
634 in.close();
635 } catch (IOException e) {
636 // Ignore
637 }
638 }
639 }
640 }
641
642 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
643 DataOutputStream out = null;
644 try {
645 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
646 out.writeUTF(configuration.locale);
647 out.writeInt(configuration.mcc);
648 out.writeInt(configuration.mnc);
649 out.flush();
650 } catch (FileNotFoundException e) {
651 // Ignore
652 } catch (IOException e) {
653 //noinspection ResultOfMethodCallIgnored
654 context.getFileStreamPath(PREFERENCES).delete();
655 } finally {
656 if (out != null) {
657 try {
658 out.close();
659 } catch (IOException e) {
660 // Ignore
661 }
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664 }
665
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666 public Stats getStats() {
667 return mStats;
668 }
669
Bjorn Bringertc459e522013-06-07 19:36:01 +0100670 public LayoutInflater getInflater() {
671 return mInflater;
672 }
673
Winson Chung36a62fe2012-05-06 18:04:42 -0700674 boolean isDraggingEnabled() {
675 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
676 // that is subsequently removed from the workspace in startBinding().
677 return !mModel.isLoadingWorkspace();
678 }
679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 static int getScreen() {
681 synchronized (sLock) {
682 return sScreen;
683 }
684 }
685
686 static void setScreen(int screen) {
687 synchronized (sLock) {
688 sScreen = screen;
689 }
690 }
691
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000693 * Copied from View -- the View version of the method isn't called
694 * anywhere else in our process and only exists for API level 17+,
695 * so it's ok to keep our own version with no API requirement.
696 */
697 public static int generateViewId() {
698 for (;;) {
699 final int result = sNextGeneratedId.get();
700 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
701 int newValue = result + 1;
702 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
703 if (sNextGeneratedId.compareAndSet(result, newValue)) {
704 return result;
705 }
706 }
707 }
708
709 public int getViewIdForItem(ItemInfo info) {
710 // This cast is safe given the > 2B range for int.
711 int itemId = (int) info.id;
712 if (mItemIdToViewId.containsKey(itemId)) {
713 return mItemIdToViewId.get(itemId);
714 }
715 int viewId = generateViewId();
716 mItemIdToViewId.put(itemId, viewId);
717 return viewId;
718 }
719
720 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700721 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
722 * a configuration step, this allows the proper animations to run after other transitions.
723 */
Adam Cohendb364c32014-05-20 14:23:40 -0700724 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700725 long screenId = args.screenId;
726 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
727 // When the screen id represents an actual screen (as opposed to a rank) we make sure
728 // that the drop page actually exists.
729 screenId = ensurePendingDropLayoutExists(args.screenId);
730 }
Adam Cohendb364c32014-05-20 14:23:40 -0700731
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800733 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700734 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700735 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700736 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800737 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700738 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700739 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700740 case REQUEST_RECONFIGURE_APPWIDGET:
741 completeRestoreAppWidget(args.appWidgetId);
742 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 }
Michael Jurka27614d22012-04-02 06:40:22 -0700744 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
745 // if you turned the screen off and then back while in All Apps, Launcher would not
746 // return to the workspace. Clearing mAddInfo.container here fixes this issue
747 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700748 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800749 }
750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 protected void onActivityResult(
753 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700754 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700755 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700756 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800757 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700758
Adam Cohenad4e15c2013-10-17 16:21:35 -0700759 Runnable exitSpringLoaded = new Runnable() {
760 @Override
761 public void run() {
762 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
763 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
764 }
765 };
766
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
770 if (resultCode == RESULT_CANCELED) {
771 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700772 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
776 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 }
778 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200779 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
780 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
781 mWorkspace.exitOverviewMode(false);
782 }
783 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200785
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
787 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700788
Adam Cohendb364c32014-05-20 14:23:40 -0700789 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 // We have special handling for widgets
791 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800792 final int appWidgetId;
793 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
794 : -1;
795 if (widgetId < 0) {
796 appWidgetId = pendingAddWidgetId;
797 } else {
798 appWidgetId = widgetId;
799 }
800
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800802 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700803 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
804 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 result = RESULT_CANCELED;
806 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700808 @Override
809 public void run() {
810 exitSpringLoadedDragModeDelayed(false, 0, null);
811 }
812 };
Adam Cohendb364c32014-05-20 14:23:40 -0700813 if (workspaceLocked) {
814 // No need to remove the empty screen if we're mid-binding, as the
815 // the bind will not add the empty screen.
816 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
817 } else {
818 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
819 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
820 }
Winson Chung5aaab772012-04-27 15:24:02 -0700821 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700822 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700823 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
824 // When the screen id represents an actual screen (as opposed to a rank)
825 // we make sure that the drop page actually exists.
826 mPendingAddInfo.screenId =
827 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
828 }
Adam Cohendb364c32014-05-20 14:23:40 -0700829 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
830
831 dropLayout.setDropPending(true);
832 final Runnable onComplete = new Runnable() {
833 @Override
834 public void run() {
835 completeTwoStageWidgetDrop(resultCode, appWidgetId);
836 dropLayout.setDropPending(false);
837 }
838 };
839 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
840 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
841 } else {
842 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
843 mPendingAddInfo);
844 sPendingAddItem = args;
845 }
Winson Chung5aaab772012-04-27 15:24:02 -0700846 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 return;
848 }
849
Sunny Goyalff572272014-07-23 13:58:07 -0700850 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
851 if (resultCode == RESULT_OK) {
852 // Update the widget view.
853 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
854 pendingAddWidgetId, mPendingAddInfo);
855 if (workspaceLocked) {
856 sPendingAddItem = args;
857 } else {
858 completeAdd(args);
859 }
860 }
861 // Leave the widget in the pending state if the user canceled the configure.
862 return;
863 }
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 // The pattern used here is that a user PICKs a specific application,
866 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
869 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700870 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700871 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
872 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800873 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700874 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800875 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700876 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700877 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
878 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
Adam Cohen00074722013-10-11 17:46:09 -0700880 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700881 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700882 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
886
Adam Cohendb364c32014-05-20 14:23:40 -0700887 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
888 appWidgetId, ItemInfo info) {
889 PendingAddArguments args = new PendingAddArguments();
890 args.requestCode = requestCode;
891 args.intent = data;
892 args.container = info.container;
893 args.screenId = info.screenId;
894 args.cellX = info.cellX;
895 args.cellY = info.cellY;
896 args.appWidgetId = appWidgetId;
897 return args;
898 }
899
900 /**
901 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
902 *
903 * @param screenId the screen id to check
904 * @return the new screen, or screenId if it exists
905 */
906 private long ensurePendingDropLayoutExists(long screenId) {
907 CellLayout dropLayout =
908 (CellLayout) mWorkspace.getScreenWithId(screenId);
909 if (dropLayout == null) {
910 // it's possible that the add screen was removed because it was
911 // empty and a re-bind occurred
912 mWorkspace.addExtraEmptyScreen();
913 return mWorkspace.commitExtraEmptyScreen();
914 } else {
915 return screenId;
916 }
917 }
918
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700920 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 Runnable onCompleteRunnable = null;
923 int animationType = 0;
924
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 if (resultCode == RESULT_OK) {
927 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
928 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 mPendingAddWidgetInfo);
930 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 onCompleteRunnable = new Runnable() {
932 @Override
933 public void run() {
934 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
937 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 }
939 };
940 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800941 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 if (mDragLayer.getAnimatedView() != null) {
945 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
946 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
947 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700948 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700949 // The animated view may be null in the case of a rotation during widget configuration
950 onCompleteRunnable.run();
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953
954 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700955 protected void onStop() {
956 super.onStop();
957 FirstFrameAnimatorHelper.setIsVisible(false);
958 }
959
960 @Override
961 protected void onStart() {
962 super.onStart();
963 FirstFrameAnimatorHelper.setIsVisible(true);
964 }
965
966 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200968 long startTime = 0;
969 if (DEBUG_RESUME_TIME) {
970 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400971 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700974
Winson Chung4a2afa32012-07-19 14:53:05 -0700975 // Restore the previous launcher state
976 if (mOnResumeState == State.WORKSPACE) {
977 showWorkspace(false);
978 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800979 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 }
981 mOnResumeState = State.NONE;
982
Craig Mautner360310b2012-10-26 15:13:08 -0700983 // Background was set to gradient in onPause(), restore to black if in all apps.
984 setWorkspaceBackground(mState == State.WORKSPACE);
985
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800986 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700987 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700988 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500989 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700991 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700993 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200994 // We might have postponed some bind calls until onResume (see waitUntilResume) --
995 // execute them here
996 long startTimeCallbacks = 0;
997 if (DEBUG_RESUME_TIME) {
998 startTimeCallbacks = System.currentTimeMillis();
999 }
1000
1001 if (mAppsCustomizeContent != null) {
1002 mAppsCustomizeContent.setBulkBind(true);
1003 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001004 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1005 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001006 }
1007 if (mAppsCustomizeContent != null) {
1008 mAppsCustomizeContent.setBulkBind(false);
1009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 if (DEBUG_RESUME_TIME) {
1012 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1013 (System.currentTimeMillis() - startTimeCallbacks));
1014 }
Michael Jurka447bf842013-05-15 14:52:15 +02001015 }
Michael Jurka54554252013-08-01 12:52:23 +02001016 if (mOnResumeCallbacks.size() > 0) {
1017 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1018 mOnResumeCallbacks.get(i).run();
1019 }
1020 mOnResumeCallbacks.clear();
1021 }
Winson Chunge4e50662012-01-23 14:45:13 -08001022
1023 // Reset the pressed state of icons that were locked in the press state while activities
1024 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001025 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001026 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001027 mWaitingForResume.setStayPressed(false);
1028 }
Winson Chung82963d52013-10-09 11:20:57 -07001029
Adam Cohen06dff352012-06-01 17:17:08 -07001030 // It is possible that widgets can receive updates while launcher is not in the foreground.
1031 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1032 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1033 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001034 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001035
Winson Chung780fe592013-09-26 14:48:44 -07001036 // Process any items that were added while Launcher was away.
1037 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1038
Winson Chung5841efa2013-09-30 18:06:44 -07001039 // Update the voice search button proxy
1040 updateVoiceButtonProxyVisible(false);
1041
Adam Cohenf9426d52012-06-04 17:26:21 -07001042 // Again, as with the above scenario, it's possible that one or more of the global icons
1043 // were updated in the wrong orientation.
1044 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001045 if (DEBUG_RESUME_TIME) {
1046 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1047 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001048
1049 if (mWorkspace.getCustomContentCallbacks() != null) {
1050 // If we are resuming and the custom content is the current page, we call onShow().
1051 // It is also poassible that onShow will instead be called slightly after first layout
1052 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1053 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001054 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001055 }
1056 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001057 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001058 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001059
1060 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001061 }
1062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001065 // Ensure that items added to Launcher are queued until Launcher returns
1066 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001067 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001068
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001070 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001071 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001072 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001073
1074 // We call onHide() aggressively. The custom content callbacks should be able to
1075 // debounce excess onHide calls.
1076 if (mWorkspace.getCustomContentCallbacks() != null) {
1077 mWorkspace.getCustomContentCallbacks().onHide();
1078 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 }
Romain Guycbb89e42009-06-08 15:52:54 -07001080
Adam Cohenbffe7452013-07-22 18:21:45 -07001081 QSBScroller mQsbScroller = new QSBScroller() {
1082 int scrollY = 0;
1083
1084 @Override
1085 public void setScrollY(int scroll) {
1086 scrollY = scroll;
1087
1088 if (mWorkspace.isOnOrMovingToCustomContent()) {
1089 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001090 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
1092 }
1093 };
1094
1095 public void resetQSBScroll() {
1096 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001097 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 }
1099
1100 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001101 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1102 // by a onResume or by scrolling otherwise.
1103 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001104
1105 // Custom content is completely hidden
1106 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001107
1108 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1109 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001110
1111 // Indicates whether the user is allowed to scroll away from the custom content.
1112 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
1114
Jorim Jaggid017f882014-01-14 17:08:48 -08001115 protected boolean hasSettings() {
1116 return false;
1117 }
1118
Adam Cohenbffe7452013-07-22 18:21:45 -07001119 public interface QSBScroller {
1120 public void setScrollY(int scrollY);
1121 }
1122
Winson Chung98ca0f72013-07-29 12:58:51 -07001123 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001124 CustomContentCallbacks callbacks, String description) {
1125 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001126 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001127 }
1128
Adam Cohenedb40762013-07-18 16:45:45 -07001129 // The custom content needs to offset its content to account for the QSB
1130 public int getTopOffsetForCustomContent() {
1131 return mWorkspace.getPaddingTop();
1132 }
1133
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001134 @Override
1135 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001137 if (mModel.isCurrentCallbacks(this)) {
1138 mModel.stopLoader();
1139 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001140 if (mAppsCustomizeContent != null) {
1141 mAppsCustomizeContent.surrender();
1142 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001143 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001144 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001145
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001146 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001147 @Override
1148 public void onWindowFocusChanged(boolean hasFocus) {
1149 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001150 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001151 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 private boolean acceptFilter() {
1154 final InputMethodManager inputManager = (InputMethodManager)
1155 getSystemService(Context.INPUT_METHOD_SERVICE);
1156 return !inputManager.isFullscreenMode();
1157 }
1158
1159 @Override
1160 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001161 final int uniChar = event.getUnicodeChar();
1162 final boolean handled = super.onKeyDown(keyCode, event);
1163 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1164 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1166 keyCode, event);
1167 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001168 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001169 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 // showSearchDialog()
1171 // If there are multiple keystrokes before the search dialog takes focus,
1172 // onSearchRequested() will be called for every keystroke,
1173 // but it is idempotent, so it's fine.
1174 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176 }
1177
Joe Onorato8a9625e2010-01-28 15:55:35 -08001178 // Eat the long press event so the keyboard doesn't come up.
1179 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1180 return true;
1181 }
1182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 return handled;
1184 }
1185
Karl Rosaen138a0412009-04-23 19:00:21 -07001186 private String getTypedText() {
1187 return mDefaultKeySsb.toString();
1188 }
1189
1190 private void clearTypedText() {
1191 mDefaultKeySsb.clear();
1192 mDefaultKeySsb.clearSpans();
1193 Selection.setSelection(mDefaultKeySsb, 0);
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001197 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1198 * State
1199 */
1200 private static State intToState(int stateOrdinal) {
1201 State state = State.WORKSPACE;
1202 final State[] stateValues = State.values();
1203 for (int i = 0; i < stateValues.length; i++) {
1204 if (stateValues[i].ordinal() == stateOrdinal) {
1205 state = stateValues[i];
1206 break;
1207 }
1208 }
1209 return state;
1210 }
1211
1212 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 * Restores the previous state, if it exists.
1214 *
1215 * @param savedState The previous state.
1216 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001217 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 private void restoreState(Bundle savedState) {
1219 if (savedState == null) {
1220 return;
1221 }
1222
Michael Jurka883f55b2010-10-21 15:47:14 -07001223 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001224 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001225 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001226 }
1227
Adam Cohen21cd0022013-10-09 18:57:02 -07001228 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1229 PagedView.INVALID_RESTORE_PAGE);
1230 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001231 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 }
1233
Winson Chung3d503fb2011-07-13 17:25:49 -07001234 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001235 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236
Winson Chung3d503fb2011-07-13 17:25:49 -07001237 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1238 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001239 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001240 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1241 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001242 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1243 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1244 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001245 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001246 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 mRestoring = true;
1248 }
1249
1250 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1251 if (renameFolder) {
1252 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001253 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 mRestoring = true;
1255 }
Winson Chunga12a2502010-12-20 14:41:35 -08001256
Winson Chung785d2eb2011-04-14 16:08:02 -07001257 // Restore the AppsCustomize tab
1258 if (mAppsCustomizeTabHost != null) {
1259 String curTab = savedState.getString("apps_customize_currentTab");
1260 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001261 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001262 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001263 mAppsCustomizeContent.loadAssociatedPages(
1264 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001265 }
1266
Winson Chung5afbf7b2011-07-25 11:53:08 -07001267 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1268 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001269 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001270 mItemIdToViewId = (HashMap<Integer, Integer>)
1271 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
1273
1274 /**
1275 * Finds all the views we need and configure them properly.
1276 */
1277 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001278 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001279
Craig Mautner360310b2012-10-26 15:13:08 -07001280 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001281 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001282 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1283 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001284 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001285 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001286
John Spurlock77e1f472013-09-11 10:09:51 -04001287 mLauncherView.setSystemUiVisibility(
1288 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001289 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
Winson Chung4c98d922011-05-31 16:50:48 -07001291 // Setup the drag layer
1292 mDragLayer.setup(this, dragController);
1293
Winson Chung3d503fb2011-07-13 17:25:49 -07001294 // Setup the hotseat
1295 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1296 if (mHotseat != null) {
1297 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001298 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001299 }
1300
Jorim Jaggid017f882014-01-14 17:08:48 -08001301 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001302 View widgetButton = findViewById(R.id.widget_button);
1303 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001304 @Override
1305 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001306 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001307 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001308 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001309 }
1310 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001311 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1312
1313 View wallpaperButton = findViewById(R.id.wallpaper_button);
1314 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001315 @Override
1316 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001317 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001318 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001319 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001320 }
1321 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001322 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1323
1324 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001325 if (hasSettings()) {
1326 settingsButton.setOnClickListener(new OnClickListener() {
1327 @Override
1328 public void onClick(View arg0) {
1329 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001330 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001331 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001332 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001333 });
1334 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1335 } else {
1336 settingsButton.setVisibility(View.GONE);
1337 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1338 lp.gravity = Gravity.END | Gravity.TOP;
1339 widgetButton.requestLayout();
1340 }
1341
Adam Cohendbdff6b2013-09-18 19:09:15 -07001342 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001343
Winson Chung4c98d922011-05-31 16:50:48 -07001344 // Setup the workspace
1345 mWorkspace.setHapticFeedbackEnabled(false);
1346 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001347 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001348 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001349
Winson Chungf0ea4d32011-06-06 14:27:16 -07001350 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001351 mSearchDropTargetBar = (SearchDropTargetBar)
1352 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001353
Winson Chungf0ea4d32011-06-06 14:27:16 -07001354 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001355 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001356 mAppsCustomizeContent = (AppsCustomizePagedView)
1357 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1358 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001361 dragController.setDragScoller(mWorkspace);
1362 dragController.setScrollView(mDragLayer);
1363 dragController.setMoveTarget(mWorkspace);
1364 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001365 if (mSearchDropTargetBar != null) {
1366 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001367 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001368
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001369 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001370 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001371 mWeightWatcher = new WeightWatcher(this);
1372 mWeightWatcher.setAlpha(0.5f);
1373 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001374 new FrameLayout.LayoutParams(
1375 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001376 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001377 Gravity.BOTTOM)
1378 );
Adam Cohen39a06042013-07-19 14:30:12 -07001379
1380 boolean show = shouldShowWeightWatcher();
1381 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
1385 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001386 * Sets the all apps button. This method is called from {@link Hotseat}.
1387 */
1388 public void setAllAppsButton(View allAppsButton) {
1389 mAllAppsButton = allAppsButton;
1390 }
1391
1392 public View getAllAppsButton() {
1393 return mAllAppsButton;
1394 }
1395
1396 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 * Creates a view representing a shortcut.
1398 *
1399 * @param info The data structure describing the shortcut.
1400 *
1401 * @return A View inflated from R.layout.application.
1402 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001403 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001405 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
1407
1408 /**
1409 * Creates a view representing a shortcut inflated from the specified resource.
1410 *
1411 * @param layoutResId The id of the XML layout used to create the shortcut.
1412 * @param parent The group the shortcut belongs to.
1413 * @param info The data structure describing the shortcut.
1414 *
1415 * @return A View inflated from layoutResId.
1416 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001417 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001418 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001419 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001421 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 return favorite;
1423 }
1424
1425 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 * Add a shortcut to the workspace.
1427 *
1428 * @param data The intent describing the shortcut.
1429 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001431 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 int cellY) {
1433 int[] cellXY = mTmpAddItemCellCoordinates;
1434 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001435 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001436
Michael Jurkad3ef3062010-11-23 16:23:58 -08001437 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438
Adam Cohen558baaf2011-08-15 15:22:57 -07001439 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001440 if (info == null) {
1441 return;
1442 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001443 final View view = createShortcut(info);
1444
Adam Cohenfbba09b2011-07-18 21:43:05 -07001445 // First we check if we already know the exact location where we want to add this item.
1446 if (cellX >= 0 && cellY >= 0) {
1447 cellXY[0] = cellX;
1448 cellXY[1] = cellY;
1449 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001450
1451 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001452 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001453 true, null,null)) {
1454 return;
1455 }
1456 DragObject dragObject = new DragObject();
1457 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001458 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1459 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001460 return;
1461 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001463 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001465 foundCellSpan = (result != null);
1466 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001468 }
1469
1470 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001471 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472 return;
1473 }
1474
Adam Cohendcd297f2013-06-18 13:13:40 -07001475 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
1477 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001478 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
1481 }
1482
Adam Cohen2f093b62012-04-30 18:59:53 -07001483 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1484 int minHeight) {
1485 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001486 // We want to account for the extra amount of padding that we are adding to the widget
1487 // to ensure that it gets the full amount of space that it has requested
1488 int requiredWidth = minWidth + padding.left + padding.right;
1489 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001490 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001491 }
1492
Adam Cohen2f093b62012-04-30 18:59:53 -07001493 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1494 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001495 }
1496
Adam Cohen2f093b62012-04-30 18:59:53 -07001497 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1498 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001499 }
1500
Adam Cohen2f093b62012-04-30 18:59:53 -07001501 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1502 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001503 }
1504
Adam Cohen2f093b62012-04-30 18:59:53 -07001505 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1506 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001507 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001508 }
1509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001511 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001513 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001514 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001517 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1518 if (appWidgetInfo == null) {
1519 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1520 }
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001522 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001523 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001524
Adam Cohen2f093b62012-04-30 18:59:53 -07001525 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1526 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001529 // We have saved the position to which the widget was dragged-- this really only matters
1530 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 int[] cellXY = mTmpAddItemCellCoordinates;
1532 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001533 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1536 cellXY[0] = mPendingAddInfo.cellX;
1537 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001538 spanXY[0] = mPendingAddInfo.spanX;
1539 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 foundCellSpan = true;
1541 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001544 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1545 spanXY[1], cellXY, finalSpan);
1546 spanXY[0] = finalSpan[0];
1547 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001548 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001550 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001551 }
1552
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001554 if (appWidgetId != -1) {
1555 // Deleting an app widget ID is a void call but writes to disk before returning
1556 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001557 new AsyncTask<Void, Void, Void>() {
1558 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001559 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001560 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001561 }
Michael Jurka43467462013-11-14 12:03:58 +01001562 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001563 }
Winson Chung93eef082012-03-23 15:59:27 -07001564 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001565 return;
1566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001568 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001569 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1570 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001571 launcherInfo.spanX = spanXY[0];
1572 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001573 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1574 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001575 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579
1580 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 if (hostView == null) {
1582 // Perform actual inflation because we're live
1583 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1584 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1585 } else {
1586 // The AppWidgetHostView has already been inflated and instantiated
1587 launcherInfo.hostView = hostView;
1588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001592 launcherInfo.notifyWidgetSizeChanged(this);
1593
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001595 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001596
1597 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001599 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
Romain Guycbb89e42009-06-08 15:52:54 -07001601
Adam Cohended9f8d2010-11-03 13:25:16 -07001602 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1603 @Override
1604 public void onReceive(Context context, Intent intent) {
1605 final String action = intent.getAction();
1606 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1607 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001608 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001609 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001610
Winson Chungc100e8e2011-08-09 16:02:43 -07001611 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001612 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001613 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001614 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001615 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1617 mUserPresent = true;
1618 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001619 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1620 mModel.resetLoadedState(false, true);
1621 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1622 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1623 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1624 mModel.resetLoadedState(false, true);
1625 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1626 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1627 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001628 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1629 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1630 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 }
1632 }
1633 };
1634
1635 @Override
1636 public void onAttachedToWindow() {
1637 super.onAttachedToWindow();
1638
1639 // Listen for broadcasts related to user-presence
1640 final IntentFilter filter = new IntentFilter();
1641 filter.addAction(Intent.ACTION_SCREEN_OFF);
1642 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001643 // For handling managed profiles
1644 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1645 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001646 if (ENABLE_DEBUG_INTENTS) {
1647 filter.addAction(DebugIntents.DELETE_DATABASE);
1648 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1649 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001651 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001652 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001653 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 mVisible = true;
1655 }
1656
Adam Cohen0b395352014-06-09 22:54:36 +00001657 /**
1658 * Sets up transparent navigation and status bars in LMP.
1659 * This method is a no-op for other platform versions.
1660 */
1661 @TargetApi(19)
1662 private void setupTransparentSystemBarsForLmp() {
1663 // TODO(sansid): use the APIs directly when compiling against L sdk.
1664 // Currently we use reflection to access the flags and the API to set the transparency
1665 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001666 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001667 try {
1668 getWindow().getAttributes().systemUiVisibility |=
1669 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1670 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1671 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1672 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1673 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1674 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1675 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1676 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1677
1678 Method setStatusBarColorMethod =
1679 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1680 Method setNavigationBarColorMethod =
1681 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1682 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1683 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1684 } catch (NoSuchFieldException e) {
1685 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1686 } catch (NoSuchMethodException ex) {
1687 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1688 } catch (IllegalAccessException e) {
1689 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1690 } catch (IllegalArgumentException e) {
1691 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1692 } catch (InvocationTargetException e) {
1693 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1694 } finally {}
1695 }
1696 }
1697
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 @Override
1699 public void onDetachedFromWindow() {
1700 super.onDetachedFromWindow();
1701 mVisible = false;
1702
Adam Cohend113e0c2010-11-11 10:48:05 -08001703 if (mAttached) {
1704 unregisterReceiver(mReceiver);
1705 mAttached = false;
1706 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 updateRunning();
1708 }
1709
1710 public void onWindowVisibilityChanged(int visibility) {
1711 mVisible = visibility == View.VISIBLE;
1712 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001713 // The following code used to be in onResume, but it turns out onResume is called when
1714 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1715 // is a more appropriate event to handle
1716 if (mVisible) {
1717 mAppsCustomizeTabHost.onWindowVisible();
1718 if (!mWorkspaceLoading) {
1719 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001720 // We want to let Launcher draw itself at least once before we force it to build
1721 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001722 // apps is nice and speedy.
1723 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001724 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001725 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001726 if (mStarted) return;
1727 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001728 // We delay the layer building a bit in order to give
1729 // other message processing a time to run. In particular
1730 // this avoids a delay in hiding the IME if it was
1731 // currently shown, because doing that may involve
1732 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001733 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001734 final ViewTreeObserver.OnDrawListener listener = this;
1735 mWorkspace.post(new Runnable() {
1736 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001737 if (mWorkspace != null &&
1738 mWorkspace.getViewTreeObserver() != null) {
1739 mWorkspace.getViewTreeObserver().
1740 removeOnDrawListener(listener);
1741 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001742 }
1743 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001745 }
1746 });
1747 }
1748 clearTypedText();
1749 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751
1752 private void sendAdvanceMessage(long delay) {
1753 mHandler.removeMessages(ADVANCE_MSG);
1754 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1755 mHandler.sendMessageDelayed(msg, delay);
1756 mAutoAdvanceSentTime = System.currentTimeMillis();
1757 }
1758
1759 private void updateRunning() {
1760 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1761 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1762 mAutoAdvanceRunning = autoAdvanceRunning;
1763 if (autoAdvanceRunning) {
1764 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1765 sendAdvanceMessage(delay);
1766 } else {
1767 if (!mWidgetsToAdvance.isEmpty()) {
1768 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1769 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1770 }
1771 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001772 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
1774 }
1775 }
1776
1777 private final Handler mHandler = new Handler() {
1778 @Override
1779 public void handleMessage(Message msg) {
1780 if (msg.what == ADVANCE_MSG) {
1781 int i = 0;
1782 for (View key: mWidgetsToAdvance.keySet()) {
1783 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1784 final int delay = mAdvanceStagger * i;
1785 if (v instanceof Advanceable) {
1786 postDelayed(new Runnable() {
1787 public void run() {
1788 ((Advanceable) v).advance();
1789 }
1790 }, delay);
1791 }
1792 i++;
1793 }
1794 sendAdvanceMessage(mAdvanceInterval);
1795 }
1796 }
1797 };
1798
1799 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001800 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1802 if (v instanceof Advanceable) {
1803 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001804 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 updateRunning();
1806 }
1807 }
1808
1809 void removeWidgetToAutoAdvance(View hostView) {
1810 if (mWidgetsToAdvance.containsKey(hostView)) {
1811 mWidgetsToAdvance.remove(hostView);
1812 updateRunning();
1813 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001814 }
1815
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001817 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 launcherInfo.hostView = null;
1819 }
1820
Winson Chung93eef082012-03-23 15:59:27 -07001821 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1822 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1823 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001824 }
1825
Winson Chunga6945242014-01-08 14:04:34 -08001826 public DragLayer getDragLayer() {
1827 return mDragLayer;
1828 }
1829
1830 public Workspace getWorkspace() {
1831 return mWorkspace;
1832 }
1833
1834 public Hotseat getHotseat() {
1835 return mHotseat;
1836 }
1837
Jorim Jaggid017f882014-01-14 17:08:48 -08001838 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001839 return mOverviewPanel;
1840 }
1841
1842 public SearchDropTargetBar getSearchBar() {
1843 return mSearchDropTargetBar;
1844 }
1845
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001846 public LauncherAppWidgetHost getAppWidgetHost() {
1847 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
Romain Guycbb89e42009-06-08 15:52:54 -07001849
Winson Chunga9abd0e2010-10-27 17:18:37 -07001850 public LauncherModel getModel() {
1851 return mModel;
1852 }
1853
Winson Chunga6945242014-01-08 14:04:34 -08001854 protected SharedPreferences getSharedPrefs() {
1855 return mSharedPrefs;
1856 }
1857
Bjorn Bringertc459e522013-06-07 19:36:01 +01001858 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001859 getWindow().closeAllPanels();
1860
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001861 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001862 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 @Override
1866 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001867 long startTime = 0;
1868 if (DEBUG_RESUME_TIME) {
1869 startTime = System.currentTimeMillis();
1870 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 super.onNewIntent(intent);
1872
1873 // Close the menu
1874 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001875 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001876 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001877
Adam Cohened307df2013-10-02 09:37:31 -07001878 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1879 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1880 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001881
Adam Cohen6fecd412013-10-02 17:41:50 -07001882 if (mWorkspace == null) {
1883 // Can be cases where mWorkspace is null, this prevents a NPE
1884 return;
1885 }
1886 Folder openFolder = mWorkspace.getOpenFolder();
1887 // In all these cases, only animate if we're already on home
1888 mWorkspace.exitWidgetResizeMode();
1889 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001890 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001892 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001893
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 closeFolder();
1895 exitSpringLoadedDragMode();
1896
1897 // If we are already on home, then just animate back to the workspace,
1898 // otherwise, just wait until onResume to set the state back to Workspace
1899 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001900 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 } else {
1902 mOnResumeState = State.WORKSPACE;
1903 }
1904
1905 final View v = getWindow().peekDecorView();
1906 if (v != null && v.getWindowToken() != null) {
1907 InputMethodManager imm = (InputMethodManager)getSystemService(
1908 INPUT_METHOD_SERVICE);
1909 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1910 }
1911
1912 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001913 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 mAppsCustomizeTabHost.reset();
1915 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001916
1917 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
Adam Cohened307df2013-10-02 09:37:31 -07001919
Michael Jurka447bf842013-05-15 14:52:15 +02001920 if (DEBUG_RESUME_TIME) {
1921 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
1924
Adam Coppa120b8e2013-11-12 16:26:04 +00001925 /**
1926 * Override point for subclasses to prevent movement to the default screen when the home
1927 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1928 */
1929 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1930 return true;
1931 }
1932
1933 /**
1934 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1935 */
1936 protected void onHomeIntent() {
1937 // Do nothing
1938 }
1939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001941 public void onRestoreInstanceState(Bundle state) {
1942 super.onRestoreInstanceState(state);
1943 for (int page: mSynchronouslyBoundPages) {
1944 mWorkspace.restoreInstanceStateForChild(page);
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
1948 @Override
1949 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001950 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001951 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1952 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001953 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001954 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955
Michael Jurka883f55b2010-10-21 15:47:14 -07001956 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001957 // We close any open folder since it will not be re-opened, and we need to make sure
1958 // this state is reflected.
1959 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohendcd297f2013-06-18 13:13:40 -07001961 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 mWaitingForResult) {
1963 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001964 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001965 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1966 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1968 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1969 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001970 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972
1973 if (mFolderInfo != null && mWaitingForResult) {
1974 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1975 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1976 }
Michael Jurka946ad472010-07-09 18:05:18 -07001977
Winson Chung785d2eb2011-04-14 16:08:02 -07001978 // Save the current AppsCustomize tab
1979 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001980 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1981 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001982 if (currentTabTag != null) {
1983 outState.putString("apps_customize_currentTab", currentTabTag);
1984 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001985 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1986 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001987 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001988 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
1991 @Override
1992 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001994
Winson Chunge7a03942011-08-05 15:05:12 -07001995 // Remove all pending runnables
1996 mHandler.removeMessages(ADVANCE_MSG);
1997 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001998 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001999
Winson Chungcd2b0142011-06-08 16:02:26 -07002000 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002001 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002002
2003 // It's possible to receive onDestroy after a new Launcher activity has
2004 // been created. In this case, don't interfere with the new Launcher.
2005 if (mModel.isCurrentCallbacks(this)) {
2006 mModel.stopLoader();
2007 app.setLauncher(null);
2008 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002011 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002013 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015 mAppWidgetHost = null;
2016
2017 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018
2019 TextKeyListener.getInstance().release();
2020
Winson Chung81b52252012-08-27 15:34:29 -07002021 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2022 // to prevent leaking Launcher activities on orientation change.
2023 if (mModel != null) {
2024 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2025 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002026
2027 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002028 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002029
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002030 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002031 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002032 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002033 mWorkspace = null;
2034 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002035
Sunny Goyale755d462014-07-22 13:48:29 -07002036 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002037 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
Adam Cohena9cf38f2011-05-02 15:36:58 -07002040 public DragController getDragController() {
2041 return mDragController;
2042 }
2043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 @Override
2045 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002046 if (requestCode >= 0) {
2047 setWaitingForResult(true);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 super.startActivityForResult(intent, requestCode);
2050 }
2051
Winson Chung70d72102011-08-12 11:24:35 -07002052 /**
2053 * Indicates that we want global search for this activity by setting the globalSearch
2054 * argument for {@link #startSearch} to true.
2055 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002057 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002059
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002060 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002061
Karl Rosaen138a0412009-04-23 19:00:21 -07002062 if (initialQuery == null) {
2063 // Use any text typed in the launcher as the initial query
2064 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 if (appSearchData == null) {
2067 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002068 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
Winson Chungadf0c182012-08-23 14:59:07 -07002070 Rect sourceBounds = new Rect();
2071 if (mSearchDropTargetBar != null) {
2072 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2073 }
Romain Guycbb89e42009-06-08 15:52:54 -07002074
Ian Parkinson047036e2014-09-01 15:40:53 +01002075 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002076 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002077 if (clearTextImmediately) {
2078 clearTypedText();
2079 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002080 }
2081
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 /**
2083 * Start a text search.
2084 *
2085 * @return {@code true} if the search will start immediately, so any further keypresses
2086 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2087 * to buffer keypresses.
2088 */
2089 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002090 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002091 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002092 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002093 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002094 }
2095
2096 /**
2097 * Starts the global search activity. This code is a copied from SearchManager
2098 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002100 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002101 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002102 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2103 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2104 if (globalSearchActivity == null) {
2105 Log.w(TAG, "No global search activity found.");
2106 return;
2107 }
2108 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2109 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2110 intent.setComponent(globalSearchActivity);
2111 // Make sure that we have a Bundle to put source in
2112 if (appSearchData == null) {
2113 appSearchData = new Bundle();
2114 } else {
2115 appSearchData = new Bundle(appSearchData);
2116 }
2117 // Set source to package name of app that starts global search, if not set already.
2118 if (!appSearchData.containsKey("source")) {
2119 appSearchData.putString("source", getPackageName());
2120 }
2121 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2122 if (!TextUtils.isEmpty(initialQuery)) {
2123 intent.putExtra(SearchManager.QUERY, initialQuery);
2124 }
2125 if (selectInitialQuery) {
2126 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2127 }
2128 intent.setSourceBounds(sourceBounds);
2129 try {
2130 startActivity(intent);
2131 } catch (ActivityNotFoundException ex) {
2132 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
2135
Yura4f93ec62014-02-04 14:15:21 +00002136 public boolean isOnCustomContent() {
2137 return mWorkspace.isOnOrMovingToCustomContent();
2138 }
2139
Winson Chung70d72102011-08-12 11:24:35 -07002140 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002141 public boolean onPrepareOptionsMenu(Menu menu) {
2142 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002143 if (!isOnCustomContent()) {
2144 // Close any open folders
2145 closeFolder();
2146 // Stop resizing any widgets
2147 mWorkspace.exitWidgetResizeMode();
2148 if (!mWorkspace.isInOverviewMode()) {
2149 // Show the overview mode
2150 showOverviewMode(true);
2151 } else {
2152 showWorkspace(true);
2153 }
Winson Chunge0298742014-01-17 12:03:00 -08002154 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002155 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002156 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002158 @Override
2159 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002160 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002161 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002162 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 public boolean isWorkspaceLocked() {
2166 return mWorkspaceLoading || mWaitingForResult;
2167 }
2168
Adam Cohen517a7f52014-03-01 12:12:59 -08002169 public boolean isWorkspaceLoading() {
2170 return mWorkspaceLoading;
2171 }
2172
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002173 private void setWorkspaceLoading(boolean value) {
2174 boolean isLocked = isWorkspaceLocked();
2175 mWorkspaceLoading = value;
2176 if (isLocked != isWorkspaceLocked()) {
2177 onWorkspaceLockedChanged();
2178 }
2179 }
2180
2181 private void setWaitingForResult(boolean value) {
2182 boolean isLocked = isWorkspaceLocked();
2183 mWaitingForResult = value;
2184 if (isLocked != isWorkspaceLocked()) {
2185 onWorkspaceLockedChanged();
2186 }
2187 }
2188
2189 protected void onWorkspaceLockedChanged() { }
2190
Michael Jurka0280c3b2010-09-17 15:00:07 -07002191 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002194 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2195 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002196 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002197 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002198 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002199
Adam Cohenad4e15c2013-10-17 16:21:35 -07002200 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2201 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2202 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2203 }
2204
2205 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2206 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2207 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002208 if (appWidgetInfo.configure != null) {
2209 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002210 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002211
Bjorn Bringert7984c942009-12-09 15:38:25 +00002212 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002213 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2214 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2215
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002217 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002218 Runnable onComplete = new Runnable() {
2219 @Override
2220 public void run() {
2221 // Exit spring loaded mode if necessary after adding the widget
2222 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2223 null);
2224 }
2225 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002227 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002228 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230 }
Romain Guycbb89e42009-06-08 15:52:54 -07002231
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002232 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002233 // Close any folders that may be open.
2234 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002235 mWorkspace.moveToCustomContentScreen(animate);
2236 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002237 /**
2238 * Process a shortcut drop.
2239 *
2240 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002241 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002242 * @param cell The cell it should be added to, optional
2243 * @param position The location on the screen where it was dropped, optional
2244 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002245 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002249 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002251
2252 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.cellX = cell[0];
2254 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256
2257 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2258 createShortcutIntent.setComponent(componentName);
2259 processShortcut(createShortcutIntent);
2260 }
2261
Adam Cohenfbba09b2011-07-18 21:43:05 -07002262 /**
2263 * Process a widget drop.
2264 *
2265 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002267 * @param cell The cell it should be added to, optional
2268 * @param position The location on the screen where it was dropped, optional
2269 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002271 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002272 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002274 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002276 mPendingAddInfo.minSpanX = info.minSpanX;
2277 mPendingAddInfo.minSpanY = info.minSpanY;
2278
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.cellX = cell[0];
2281 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002282 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002283 if (span != null) {
2284 mPendingAddInfo.spanX = span[0];
2285 mPendingAddInfo.spanY = span[1];
2286 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287
Adam Cohened66b2b2012-01-23 17:28:51 -08002288 AppWidgetHostView hostView = info.boundWidget;
2289 int appWidgetId;
2290 if (hostView != null) {
2291 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002293 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002294 // In this case, we either need to start an activity to get permission to bind
2295 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002296 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002297 Bundle options = info.bindOptions;
2298
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2300 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002301 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002302 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002303 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002304 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002305 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2306 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002308 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2309 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002310 // TODO: we need to make sure that this accounts for the options bundle.
2311 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002312 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2313 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002314 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 }
2316
Joe Onoratodeb98af2010-02-19 14:59:39 -08002317 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002318 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 }
2320
Winson Chung24ab2f12010-09-16 14:10:47 -07002321 void processWallpaper(Intent intent) {
2322 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2323 }
2324
Adam Cohendcd297f2013-06-18 13:13:40 -07002325 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002327 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 folderInfo.title = getText(R.string.folder_name);
2329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002333 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334
2335 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 FolderIcon newFolder =
2337 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002340 // Force measure the new folder icon
2341 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2342 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002343 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 }
Romain Guycbb89e42009-06-08 15:52:54 -07002345
Joe Onorato9c1289c2009-08-17 11:03:03 -04002346 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002347 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002348 }
2349
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002350 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002351 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002352 }
2353
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002354 /**
2355 * Registers various content observers. The current implementation registers
2356 * only a favorites observer to keep track of the favorites applications.
2357 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002358 private void registerContentObservers() {
2359 ContentResolver resolver = getContentResolver();
2360 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2361 true, mWidgetObserver);
2362 }
2363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 @Override
2365 public boolean dispatchKeyEvent(KeyEvent event) {
2366 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2367 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002369 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002370 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002371 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002372 dumpState();
2373 return true;
2374 }
2375 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002376 }
2377 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2378 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002379 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 return true;
2381 }
2382 }
2383
2384 return super.dispatchKeyEvent(event);
2385 }
2386
Joe Onorato88ec0992009-11-19 13:16:06 -08002387 @Override
2388 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002389 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002390 if (mAppsCustomizeContent.getContentType() ==
2391 AppsCustomizePagedView.ContentType.Applications) {
2392 showWorkspace(true);
2393 } else {
2394 showOverviewMode(true);
2395 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002396 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002397 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002398 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002399 Folder openFolder = mWorkspace.getOpenFolder();
2400 if (openFolder.isEditingName()) {
2401 openFolder.dismissEditingName();
2402 } else {
2403 closeFolder();
2404 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002405 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002406 mWorkspace.exitWidgetResizeMode();
2407
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002408 // Back button is a no-op here, but give at least some feedback for the button press
2409 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002410 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002411 }
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002414 * Re-listen when widgets are reset.
2415 */
2416 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002417 if (mAppWidgetHost != null) {
2418 mAppWidgetHost.startListening();
2419 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002420 }
2421
2422 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 * Launches the intent referred by the clicked shortcut.
2424 *
2425 * @param v The view representing the clicked shortcut.
2426 */
2427 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002428 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2429 // view has detached (it's possible for this to happen if the view is removed mid touch).
2430 if (v.getWindowToken() == null) {
2431 return;
2432 }
2433
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002434 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002435 return;
2436 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002437
Adam Cohen1697b792013-09-17 19:08:21 -07002438 if (v instanceof Workspace) {
2439 if (mWorkspace.isInOverviewMode()) {
2440 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002441 }
2442 return;
2443 }
2444
2445 if (v instanceof CellLayout) {
2446 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002447 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002448 }
2449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002452 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002453 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002455 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002456 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002457 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002458 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002459 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002460 } else if (tag instanceof AppInfo) {
2461 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002462 } else if (tag instanceof LauncherAppWidgetInfo) {
2463 if (v instanceof PendingAppWidgetHostView) {
2464 onClickPendingWidget((PendingAppWidgetHostView) v);
2465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 }
2467 }
2468
Sunny Goyal508da152014-08-14 10:53:27 -07002469 public void onClickPagedViewIcon(View v) {
2470 startAppShortcutOrInfoActivity(v);
2471 }
2472
Michael Jurka0e260592010-06-30 17:07:39 -07002473 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002474 return false;
2475 }
2476
Michael Jurkaaf442092010-06-10 17:01:57 -07002477 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002478 * Event handler for the app widget view which has not fully restored.
2479 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002480 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2481 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002482 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002483 int widgetId = info.appWidgetId;
2484 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2485 if (appWidgetInfo != null) {
2486 mPendingAddWidgetInfo = appWidgetInfo;
2487 mPendingAddInfo.copyFrom(info);
2488 mPendingAddWidgetId = widgetId;
2489
Sunny Goyalffe83f12014-08-14 17:39:34 -07002490 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2491 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002492 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002493 } else if (info.installProgress < 0) {
2494 // The install has not been queued
2495 final String packageName = info.providerName.getPackageName();
2496 showBrokenAppInstallDialog(packageName,
2497 new DialogInterface.OnClickListener() {
2498 public void onClick(DialogInterface dialog, int id) {
2499 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2500 }
2501 });
2502 } else {
2503 // Download has started.
2504 final String packageName = info.providerName.getPackageName();
2505 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002506 }
2507 }
2508
2509 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002510 * Event handler for the search button
2511 *
2512 * @param v The view that was clicked.
2513 */
2514 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002515 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2516
Amith Yamasania135ba82011-08-09 17:42:01 -07002517 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002518 }
2519
2520 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002521 * Event handler for the voice button
2522 *
2523 * @param v The view that was clicked.
2524 */
2525 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002526 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002527
Bjorn Bringertc459e522013-06-07 19:36:01 +01002528 startVoice();
2529 }
2530
2531 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002532 try {
2533 final SearchManager searchManager =
2534 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2535 ComponentName activityName = searchManager.getGlobalSearchActivity();
2536 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002537 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002538 if (activityName != null) {
2539 intent.setPackage(activityName.getPackageName());
2540 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002541 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002542 } catch (ActivityNotFoundException e) {
2543 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002544 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002545 startActivitySafely(null, intent, "onClickVoiceButton");
2546 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002547 }
2548
2549 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002550 * Event handler for the "grid" button that appears on the home screen, which
2551 * enters all apps mode.
2552 *
2553 * @param v The view that was clicked.
2554 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002555 protected void onClickAllAppsButton(View v) {
2556 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2557 if (isAllAppsVisible()) {
2558 showWorkspace(true);
2559 } else {
2560 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2561 }
2562 }
2563
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002564 private void showBrokenAppInstallDialog(final String packageName,
2565 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyalf8763012014-09-23 16:22:10 -07002566 new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault))
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 .setTitle(R.string.abandoned_promises_title)
2568 .setMessage(R.string.abandoned_promise_explanation)
2569 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2570 .setNeutralButton(R.string.abandoned_clean_this,
2571 new DialogInterface.OnClickListener() {
2572 public void onClick(DialogInterface dialog, int id) {
2573 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2574 mWorkspace.removeAbandonedPromise(packageName, user);
2575 }
2576 })
2577 .create().show();
2578 return;
2579 }
2580
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002581 /**
2582 * Event handler for an app shortcut click.
2583 *
2584 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2585 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002586 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002587 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2588 Object tag = v.getTag();
2589 if (!(tag instanceof ShortcutInfo)) {
2590 throw new IllegalArgumentException("Input must be a Shortcut");
2591 }
2592
2593 // Open shortcut
2594 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2595 final Intent intent = shortcut.intent;
2596
2597 // Check for special shortcuts
2598 if (intent.getComponent() != null) {
2599 final String shortcutClass = intent.getComponent().getClassName();
2600
2601 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2602 MemoryDumpActivity.startDump(this);
2603 return;
2604 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2605 toggleShowWeightWatcher();
2606 return;
2607 }
2608 }
2609
Chris Wren40c5ed32014-06-24 18:24:23 -04002610 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002611 if ((v instanceof BubbleTextView)
2612 && shortcut.isPromise()
2613 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002614 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002615 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002616 new DialogInterface.OnClickListener() {
2617 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002618 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002619 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002621 return;
2622 }
2623
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002625 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002626 }
2627
Sunny Goyal508da152014-08-14 10:53:27 -07002628 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002630 final ShortcutInfo shortcut;
2631 final Intent intent;
2632 if (tag instanceof ShortcutInfo) {
2633 shortcut = (ShortcutInfo) tag;
2634 intent = shortcut.intent;
2635 int[] pos = new int[2];
2636 v.getLocationOnScreen(pos);
2637 intent.setSourceBounds(new Rect(pos[0], pos[1],
2638 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002639
Sunny Goyal508da152014-08-14 10:53:27 -07002640 } else if (tag instanceof AppInfo) {
2641 shortcut = null;
2642 intent = ((AppInfo) tag).intent;
2643 } else {
2644 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2645 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646
2647 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 mStats.recordLaunch(intent, shortcut);
2649
2650 if (success && v instanceof BubbleTextView) {
2651 mWaitingForResume = (BubbleTextView) v;
2652 mWaitingForResume.setStayPressed(true);
2653 }
2654 }
2655
2656 /**
2657 * Event handler for a folder icon click.
2658 *
2659 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2660 */
2661 protected void onClickFolderIcon(View v) {
2662 if (LOGD) Log.d(TAG, "onClickFolder");
2663 if (!(v instanceof FolderIcon)){
2664 throw new IllegalArgumentException("Input must be a FolderIcon");
2665 }
2666
2667 FolderIcon folderIcon = (FolderIcon) v;
2668 final FolderInfo info = folderIcon.getFolderInfo();
2669 Folder openFolder = mWorkspace.getFolderForTag(info);
2670
2671 // If the folder info reports that the associated folder is open, then verify that
2672 // it is actually opened. There have been a few instances where this gets out of sync.
2673 if (info.opened && openFolder == null) {
2674 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2675 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2676 info.opened = false;
2677 }
2678
2679 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2680 // Close any open folder
2681 closeFolder();
2682 // Open the requested folder
2683 openFolder(folderIcon);
2684 } else {
2685 // Find the open folder...
2686 int folderScreen;
2687 if (openFolder != null) {
2688 folderScreen = mWorkspace.getPageForView(openFolder);
2689 // .. and close it
2690 closeFolder(openFolder);
2691 if (folderScreen != mWorkspace.getCurrentPage()) {
2692 // Close any folder open on the current screen
2693 closeFolder();
2694 // Pull the folder onto this screen
2695 openFolder(folderIcon);
2696 }
2697 }
2698 }
Michael Jurka5130e402011-10-13 04:55:35 -07002699 }
2700
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002701 /**
2702 * Event handler for the (Add) Widgets button that appears after a long press
2703 * on the home screen.
2704 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002705 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002707 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2708 }
2709
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002710 /**
2711 * Event handler for the wallpaper picker button that appears after a long press
2712 * on the home screen.
2713 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002714 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2716 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2717 pickWallpaper.setComponent(getWallpaperPickerComponent());
2718 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2719 }
2720
2721 /**
2722 * Event handler for a click on the settings button that appears after a long press
2723 * on the home screen.
2724 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002725 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2727 }
2728
Michael Jurka5130e402011-10-13 04:55:35 -07002729 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002730 // Provide the same haptic feedback that the system offers for virtual keys.
2731 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002732 }
2733
Adam Cohen61f560d2013-09-30 15:58:20 -07002734 public void performHapticFeedbackOnTouchDown(View v) {
2735 // Provide the same haptic feedback that the system offers for virtual keys.
2736 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2737 }
2738
2739 public View.OnTouchListener getHapticFeedbackTouchListener() {
2740 if (mHapticFeedbackTouchListener == null) {
2741 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2742 @Override
2743 public boolean onTouch(View v, MotionEvent event) {
2744 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2745 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2746 }
2747 return false;
2748 }
2749 };
2750 }
2751 return mHapticFeedbackTouchListener;
2752 }
2753
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002754 public void onDragStarted(View view) {}
2755
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002756 /**
2757 * Called when the user stops interacting with the launcher.
2758 * This implies that the user is now on the homescreen and is not doing housekeeping.
2759 */
2760 protected void onInteractionEnd() {}
2761
2762 /**
2763 * Called when the user starts interacting with the launcher.
2764 * The possible interactions are:
2765 * - open all apps
2766 * - reorder an app shortcut, or a widget
2767 * - open the overview mode.
2768 * This is a good time to stop doing things that only make sense
2769 * when the user is on the homescreen and not doing housekeeping.
2770 */
2771 protected void onInteractionBegin() {}
2772
Kenny Guyf07af7b2014-07-31 11:39:16 +01002773 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002774 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002775 try {
2776 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2777 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2778 launcherApps.showAppDetailsForProfile(componentName, user);
2779 } catch (SecurityException e) {
2780 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2781 Log.e(TAG, "Launcher does not have permission to launch settings");
2782 } catch (ActivityNotFoundException e) {
2783 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2784 Log.e(TAG, "Unable to launch settings");
2785 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002786 }
2787
Michael Jurka1e2f4652013-07-08 18:03:46 -07002788 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002789 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2790 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002791 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002792 // System applications cannot be installed. For now, show a toast explaining that.
2793 // We may give them the option of disabling apps this way.
2794 int messageId = R.string.uninstall_system_app_text;
2795 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002796 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002797 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002798 String packageName = componentName.getPackageName();
2799 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002800 Intent intent = new Intent(
2801 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002802 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2803 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002804 if (user != null) {
2805 user.addToIntent(intent, Intent.EXTRA_USER);
2806 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002807 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002808 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002809 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002810 }
2811
Michael Jurka86a720e2012-05-09 11:23:23 -07002812 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002813 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002815 // Only launch using the new animation if the shortcut has not opted out (this is a
2816 // private contract between launcher and may be ignored in the future).
2817 boolean useLaunchAnimation = (v != null) &&
2818 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002819 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2820 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002821
Kenny Guy1317e2d2014-05-08 18:52:50 +01002822 UserHandleCompat user = null;
2823 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2824 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2825 user = userManager.getUserForSerialNumber(serialNumber);
2826 }
2827
2828 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002829 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002830 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002831 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2832 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002833 optsBundle = opts.toBundle();
2834 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002835
2836 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2837 // Could be launching some bookkeeping activity
2838 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002839 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002840 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002841 launcherApps.startActivityForProfile(intent.getComponent(), user,
2842 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002843 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002844 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 } catch (SecurityException e) {
2846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002847 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002849 "or use the exported attribute for this activity. "
2850 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002852 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002854
Michael Jurka86a720e2012-05-09 11:23:23 -07002855 boolean startActivitySafely(View v, Intent intent, Object tag) {
2856 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002857 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2858 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2859 return false;
2860 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002861 try {
2862 success = startActivity(v, intent, tag);
2863 } catch (ActivityNotFoundException e) {
2864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2865 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2866 }
2867 return success;
2868 }
2869
Adam Cohen268c4752012-06-06 17:47:33 -07002870 /**
2871 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2872 * in the DragLayer in the exact absolute location of the original FolderIcon.
2873 */
2874 private void copyFolderIconToImage(FolderIcon fi) {
2875 final int width = fi.getMeasuredWidth();
2876 final int height = fi.getMeasuredHeight();
2877
2878 // Lazy load ImageView, Bitmap and Canvas
2879 if (mFolderIconImageView == null) {
2880 mFolderIconImageView = new ImageView(this);
2881 }
2882 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2883 mFolderIconBitmap.getHeight() != height) {
2884 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2885 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2886 }
2887
2888 DragLayer.LayoutParams lp;
2889 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2890 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2891 } else {
2892 lp = new DragLayer.LayoutParams(width, height);
2893 }
2894
Adam Cohen307fe232012-08-16 17:55:58 -07002895 // The layout from which the folder is being opened may be scaled, adjust the starting
2896 // view size by this scale factor.
2897 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002898 lp.customPosition = true;
2899 lp.x = mRectForFolderAnimation.left;
2900 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002901 lp.width = (int) (scale * width);
2902 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002903
2904 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2905 fi.draw(mFolderIconCanvas);
2906 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002907 if (fi.getFolder() != null) {
2908 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2909 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002910 }
Adam Cohen268c4752012-06-06 17:47:33 -07002911 // Just in case this image view is still in the drag layer from a previous animation,
2912 // we remove it and re-add it.
2913 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2914 mDragLayer.removeView(mFolderIconImageView);
2915 }
2916 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002917 if (fi.getFolder() != null) {
2918 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002919 }
Adam Cohen268c4752012-06-06 17:47:33 -07002920 }
2921
Adam Cohen2801caf2011-05-13 20:57:39 -07002922 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002923 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002924 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2925 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2926 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2927
Adam Cohenc51934b2011-07-26 21:07:43 -07002928 FolderInfo info = (FolderInfo) fi.getTag();
2929 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2930 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002931 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2932 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002933 }
2934
Adam Cohen268c4752012-06-06 17:47:33 -07002935 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2936 copyFolderIconToImage(fi);
2937 fi.setVisibility(View.INVISIBLE);
2938
Michael Jurka2ecf9952012-06-18 12:52:28 -07002939 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002940 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002941 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002942 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2943 }
2944 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002945 oa.start();
2946 }
2947
Adam Cohen268c4752012-06-06 17:47:33 -07002948 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002949 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002950 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2951 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2952 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2953
Adam Cohen268c4752012-06-06 17:47:33 -07002954 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002955
Adam Cohen268c4752012-06-06 17:47:33 -07002956 // We remove and re-draw the FolderIcon in-case it has changed
2957 mDragLayer.removeView(mFolderIconImageView);
2958 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002959 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002960 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002961 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002962 oa.addListener(new AnimatorListenerAdapter() {
2963 @Override
2964 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002965 if (cl != null) {
2966 cl.clearFolderLeaveBehind();
2967 // Remove the ImageView copy of the FolderIcon and make the original visible.
2968 mDragLayer.removeView(mFolderIconImageView);
2969 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002970 }
2971 }
2972 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002973 oa.start();
2974 }
2975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002977 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 * is animated relative to the specified View. If the View is null, no animation
2979 * is played.
2980 *
2981 * @param folderInfo The FolderInfo describing the folder to open.
2982 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002983 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002984 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002985 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986
Adam Cohena9cf38f2011-05-02 15:36:58 -07002987 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002988
Adam Cohen4554ee12011-08-03 16:13:21 -07002989 // Just verify that the folder hasn't already been added to the DragLayer.
2990 // There was a one-off crash where the folder had a parent already.
2991 if (folder.getParent() == null) {
2992 mDragLayer.addView(folder);
2993 mDragController.addDropTarget((DropTarget) folder);
2994 } else {
2995 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2996 folder.getParent() + ").");
2997 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002998 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002999 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003000
3001 // Notify the accessibility manager that this folder "window" has appeared and occluded
3002 // the workspace items
3003 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3004 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003005 }
3006
3007 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003008 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003009 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003010 if (folder.isEditingName()) {
3011 folder.dismissEditingName();
3012 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003013 closeFolder(folder);
3014 }
3015 }
3016
3017 void closeFolder(Folder folder) {
3018 folder.getInfo().opened = false;
3019
3020 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3021 if (parent != null) {
3022 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3023 shrinkAndFadeInFolderIcon(fi);
3024 }
3025 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003026
3027 // Notify the accessibility manager that this folder "window" has disappeard and no
3028 // longer occludeds the workspace items
3029 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 }
3031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003033 if (!isDraggingEnabled()) return false;
3034 if (isWorkspaceLocked()) return false;
3035 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036
Adam Cohen1697b792013-09-17 19:08:21 -07003037 if (v instanceof Workspace) {
3038 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003039 if (mWorkspace.enterOverviewMode()) {
3040 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3041 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3042 return true;
3043 } else {
3044 return false;
3045 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003046 } else {
3047 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003048 }
Adam Cohen1697b792013-09-17 19:08:21 -07003049 }
3050
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003051 CellLayout.CellInfo longClickCellInfo = null;
3052 View itemUnderLongClick = null;
3053 if (v.getTag() instanceof ItemInfo) {
3054 ItemInfo info = (ItemInfo) v.getTag();
3055 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3056 itemUnderLongClick = longClickCellInfo.cell;
3057 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003058 }
3059
Winson Chung3d503fb2011-07-13 17:25:49 -07003060 // The hotseat touch handling does not go through Workspace, and we always allow long press
3061 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003062 final boolean inHotseat = isHotseatLayout(v);
3063 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003065 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003066 // User long pressed on empty space
3067 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3068 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003069 if (mWorkspace.isInOverviewMode()) {
3070 mWorkspace.startReordering(v);
3071 } else {
3072 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003074 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003075 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3076 mHotseat.getOrderInHotseat(
3077 longClickCellInfo.cellX,
3078 longClickCellInfo.cellY));
3079 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003081 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
3083 }
3084 }
3085 return true;
3086 }
3087
Winson Chung3d503fb2011-07-13 17:25:49 -07003088 boolean isHotseatLayout(View layout) {
3089 return mHotseat != null && layout != null &&
3090 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3091 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003092
Winson Chung3d503fb2011-07-13 17:25:49 -07003093 /**
3094 * Returns the CellLayout of the specified container at the specified screen.
3095 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003096 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3098 if (mHotseat != null) {
3099 return mHotseat.getLayout();
3100 } else {
3101 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003102 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003103 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003104 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003105 }
3106 }
3107
Daniel Sandler843e8602010-06-07 14:59:01 -04003108 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003109 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003110 }
3111
Craig Mautner360310b2012-10-26 15:13:08 -07003112 private void setWorkspaceBackground(boolean workspace) {
3113 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003114 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003115 }
3116
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003117 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003118 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3119 int curflags = getWindow().getAttributes().flags
3120 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3121 if (wpflags != curflags) {
3122 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3123 }
Craig Mautner360310b2012-10-26 15:13:08 -07003124 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003125 }
3126
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003127 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3128 if (v instanceof LauncherTransitionable) {
3129 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3130 }
3131 }
3132
Michael Jurkabed61d22012-02-14 22:51:29 -08003133 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3134 if (v instanceof LauncherTransitionable) {
3135 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3136 }
Winson Chung70442722012-02-10 15:43:22 -08003137
3138 // Update the workspace transition step as well
3139 dispatchOnLauncherTransitionStep(v, 0f);
3140 }
3141
3142 private void dispatchOnLauncherTransitionStep(View v, float t) {
3143 if (v instanceof LauncherTransitionable) {
3144 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3145 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003146 }
3147
3148 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3149 if (v instanceof LauncherTransitionable) {
3150 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3151 }
Winson Chung70442722012-02-10 15:43:22 -08003152
3153 // Update the workspace transition step as well
3154 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003155 }
3156
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003157 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003158 * Things to test when changing the following seven functions.
3159 * - Home from workspace
3160 * - from center screen
3161 * - from other screens
3162 * - Home from all apps
3163 * - from center screen
3164 * - from other screens
3165 * - Back from all apps
3166 * - from center screen
3167 * - from other screens
3168 * - Launch app from workspace and quit
3169 * - with back
3170 * - with home
3171 * - Launch app from all apps and quit
3172 * - with back
3173 * - with home
3174 * - Go to a screen that's not the default, then all
3175 * apps, and launch and app, and go back
3176 * - with back
3177 * -with home
3178 * - On workspace, long press power and go back
3179 * - with back
3180 * - with home
3181 * - On all apps, long press power and go back
3182 * - with back
3183 * - with home
3184 * - On workspace, power off
3185 * - On all apps, power off
3186 * - Launch an app and turn off the screen while in that app
3187 * - Go back with home key
3188 * - Go back with back key TODO: make this not go to workspace
3189 * - From all apps
3190 * - From workspace
3191 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3192 * - From all apps
3193 * - From the center workspace
3194 * - From another workspace
3195 */
3196
3197 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003198 * Zoom the camera out from the workspace to reveal 'toView'.
3199 * Assumes that the view to show is anchored at either the very top or very bottom
3200 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003201 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003202 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003203 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3204 showAppsCustomizeHelper(animated, springLoaded, contentType);
3205 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003206
Winson Chungc58497e2013-09-03 17:48:37 -07003207 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3208 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003209 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003210 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003211 mStateAnimation.cancel();
3212 mStateAnimation = null;
3213 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003214
Kenny Guyd794a3f2014-09-16 15:17:58 +01003215 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003216
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003217 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003218
Winson Chungf0ea4d32011-06-06 14:27:16 -07003219 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3220 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003221 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003222 final int itemsAlphaStagger =
3223 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003224
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003226 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003227 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003228
Adam Cohen2854d252014-08-27 16:04:07 -07003229 final ArrayList<View> layerViews = new ArrayList<View>();
3230
Adam Cohen6c5891a2014-07-09 23:53:15 -07003231 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3232 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003233 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003234 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003235 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003236 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3237 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003238 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3239 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003240
Adam Cohena38dc902014-09-07 17:48:55 +02003241 // If for some reason our views aren't initialized, don't animate
3242 boolean initialized = getAllAppsButton() != null;
3243
3244 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003245 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003246 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3247 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003248
Adam Cohen9bfdb762014-07-21 17:44:06 -07003249 final View page = content.getPageAt(content.getCurrentPage());
3250 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003251
Adam Cohen63f1ec02014-08-12 09:23:13 -07003252 final float initialPanelAlpha = 1f;
3253
3254 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3255 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003256 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3257 } else {
3258 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3259 }
3260
Adam Cohen9bfdb762014-07-21 17:44:06 -07003261 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003262 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003263 revealView.setVisibility(View.VISIBLE);
3264 // We need to hide this view as the animation start will be posted.
3265 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003266
Adam Cohen9bfdb762014-07-21 17:44:06 -07003267 int width = revealView.getMeasuredWidth();
3268 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003269 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003270
Adam Cohen63f1ec02014-08-12 09:23:13 -07003271 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003272 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003273
Adam Cohen63f1ec02014-08-12 09:23:13 -07003274 // Get the y delta between the center of the page and the center of the all apps button
3275 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3276 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003277
Adam Cohen2854d252014-08-27 16:04:07 -07003278 float alpha = 0;
3279 float xDrift = 0;
3280 float yDrift = 0;
3281 if (material) {
3282 alpha = isWidgetTray ? 0.3f : 1f;
3283 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3284 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3285 } else {
3286 yDrift = 2 * height / 3;
3287 xDrift = 0;
3288 }
3289 final float initAlpha = alpha;
3290
Adam Cohen9bfdb762014-07-21 17:44:06 -07003291 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003292 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003293 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003294 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003295 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003296 PropertyValuesHolder panelDriftX =
3297 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003298
Adam Cohen2854d252014-08-27 16:04:07 -07003299 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3300 panelAlpha, panelDriftY, panelDriftX);
3301
Adam Cohen9bfdb762014-07-21 17:44:06 -07003302 panelAlphaAndDrift.setDuration(revealDuration);
3303 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003304
Adam Cohen9bfdb762014-07-21 17:44:06 -07003305 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003306
Adam Cohen4e243a22014-08-10 18:30:55 -07003307 if (page != null) {
3308 page.setVisibility(View.VISIBLE);
3309 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003310 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003311
Adam Cohen2854d252014-08-27 16:04:07 -07003312 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3313 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003314 pageDrift.setDuration(revealDuration);
3315 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003316 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003317 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003318
Adam Cohen63f1ec02014-08-12 09:23:13 -07003319 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003320 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003321 itemsAlpha.setDuration(revealDuration);
3322 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3323 itemsAlpha.setStartDelay(itemsAlphaStagger);
3324 mStateAnimation.play(itemsAlpha);
3325 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003326
Adam Cohen4e243a22014-08-10 18:30:55 -07003327 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3328 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003329 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003330 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003331 indicatorsAlpha.setDuration(revealDuration);
3332 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003333
Adam Cohen9bfdb762014-07-21 17:44:06 -07003334 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003335 final View allApps = getAllAppsButton();
3336 int allAppsButtonSize = LauncherAppState.getInstance().
3337 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3338 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003339 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003340 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003341 reveal.setDuration(revealDuration);
3342 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003343
3344 reveal.addListener(new AnimatorListenerAdapter() {
3345 public void onAnimationStart(Animator animation) {
3346 if (!isWidgetTray) {
3347 allApps.setVisibility(View.INVISIBLE);
3348 }
3349 }
3350 public void onAnimationEnd(Animator animation) {
3351 if (!isWidgetTray) {
3352 allApps.setVisibility(View.VISIBLE);
3353 }
3354 }
3355 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003356 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003357 }
Michael Jurka1899a362011-11-03 13:50:45 -07003358
Adam Cohen9bfdb762014-07-21 17:44:06 -07003359 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3360 @Override
3361 public void onAnimationEnd(Animator animation) {
3362 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3363 dispatchOnLauncherTransitionEnd(toView, animated, false);
3364
3365 revealView.setVisibility(View.INVISIBLE);
3366 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003367 if (page != null) {
3368 page.setLayerType(View.LAYER_TYPE_NONE, null);
3369 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003370 content.setPageBackgroundsVisible(true);
3371
3372 // Hide the search bar
3373 if (mSearchDropTargetBar != null) {
3374 mSearchDropTargetBar.hideSearchBar(false);
3375 }
3376 }
3377
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 });
3379
Adam Cohen6c5891a2014-07-09 23:53:15 -07003380 if (workspaceAnim != null) {
3381 mStateAnimation.play(workspaceAnim);
3382 }
Adam Cohen2854d252014-08-27 16:04:07 -07003383
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003384 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3385 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003386 final AnimatorSet stateAnimation = mStateAnimation;
3387 final Runnable startAnimRunnable = new Runnable() {
3388 public void run() {
3389 // Check that mStateAnimation hasn't changed while
3390 // we waited for a layout/draw pass
3391 if (mStateAnimation != stateAnimation)
3392 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003393 dispatchOnLauncherTransitionStart(fromView, animated, false);
3394 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003395
3396 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003397 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003398 for (int i = 0; i < layerViews.size(); i++) {
3399 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003400 if (v != null) {
3401 boolean attached = true;
3402 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3403 attached = v.isAttachedToWindow();
3404 }
3405 if (attached) v.buildLayer();
3406 }
Adam Cohen2854d252014-08-27 16:04:07 -07003407 }
3408 }
3409 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003410 }
3411 };
Adam Cohen2854d252014-08-27 16:04:07 -07003412 toView.bringToFront();
3413 toView.setVisibility(View.VISIBLE);
3414 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003415 } else {
3416 toView.setTranslationX(0.0f);
3417 toView.setTranslationY(0.0f);
3418 toView.setScaleX(1.0f);
3419 toView.setScaleY(1.0f);
3420 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003421 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003422
Daniel Sandlere4f98912013-06-25 15:13:26 -04003423 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003424 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003425 if (mSearchDropTargetBar != null) {
3426 mSearchDropTargetBar.hideSearchBar(false);
3427 }
Michael Jurkaabded662011-03-04 12:06:57 -08003428 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003429 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003430 dispatchOnLauncherTransitionStart(fromView, animated, false);
3431 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003432 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003433 dispatchOnLauncherTransitionStart(toView, animated, false);
3434 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003435 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003436 }
3437
3438 /**
3439 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003441 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003442 */
Adam Cohened307df2013-10-02 09:37:31 -07003443 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003444 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003445
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003447 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448 mStateAnimation.cancel();
3449 mStateAnimation = null;
3450 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003451
Kenny Guyd794a3f2014-09-16 15:17:58 +01003452 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003453 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003454
Winson Chungf0ea4d32011-06-06 14:27:16 -07003455 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003456 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003457 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003458 final int itemsAlphaStagger =
3459 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003460
Winson Chungf0ea4d32011-06-06 14:27:16 -07003461 final float scaleFactor = (float)
3462 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3463 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003464 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003465 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003466 final ArrayList<View> layerViews = new ArrayList<View>();
3467
Adam Cohened307df2013-10-02 09:37:31 -07003468 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003469 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003470 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003471 } else if (toState == Workspace.State.SPRING_LOADED ||
3472 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003473 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003474 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003475 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003476
Adam Cohena38dc902014-09-07 17:48:55 +02003477 // If for some reason our views aren't initialized, don't animate
3478 boolean initialized = getAllAppsButton() != null;
3479
3480 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003481 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003482 if (workspaceAnim != null) {
3483 mStateAnimation.play(workspaceAnim);
3484 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003485
Adam Cohen9bfdb762014-07-21 17:44:06 -07003486 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3487 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003488
Adam Cohen9bfdb762014-07-21 17:44:06 -07003489 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003490
3491 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3492 int count = content.getChildCount();
3493 for (int i = 0; i < count; i++) {
3494 View child = content.getChildAt(i);
3495 if (child != page) {
3496 child.setVisibility(View.INVISIBLE);
3497 }
3498 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003499 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003500
Adam Cohen2854d252014-08-27 16:04:07 -07003501 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3502 // don't perform all these no-op animations. In particularly, this was causing
3503 // the all-apps button to pop in and out.
3504 if (fromView.getVisibility() == View.VISIBLE) {
3505 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3506 final boolean isWidgetTray =
3507 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003508
Adam Cohen2854d252014-08-27 16:04:07 -07003509 if (isWidgetTray) {
3510 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3511 } else {
3512 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003513 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003514
Adam Cohen2854d252014-08-27 16:04:07 -07003515 int width = revealView.getMeasuredWidth();
3516 int height = revealView.getMeasuredHeight();
3517 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3518
3519 // Hide the real page background, and swap in the fake one
3520 revealView.setVisibility(View.VISIBLE);
3521 content.setPageBackgroundsVisible(false);
3522
3523 final View allAppsButton = getAllAppsButton();
3524 revealView.setTranslationY(0);
3525 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3526 allAppsButton, null);
3527
3528 float xDrift = 0;
3529 float yDrift = 0;
3530 if (material) {
3531 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3532 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3533 } else {
3534 yDrift = 5 * height / 4;
3535 xDrift = 0;
3536 }
3537
3538 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3539 TimeInterpolator decelerateInterpolator = material ?
3540 new LogDecelerateInterpolator(100, 0) :
3541 new LogDecelerateInterpolator(30, 0);
3542
3543 // The vertical motion of the apps panel should be delayed by one frame
3544 // from the conceal animation in order to give the right feel. We correpsondingly
3545 // shorten the duration so that the slide and conceal end at the same time.
3546 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3547 0, yDrift);
3548 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3549 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3550 panelDriftY.setInterpolator(decelerateInterpolator);
3551 mStateAnimation.play(panelDriftY);
3552
3553 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3554 0, xDrift);
3555 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3556 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3557 panelDriftX.setInterpolator(decelerateInterpolator);
3558 mStateAnimation.play(panelDriftX);
3559
3560 if (isWidgetTray || !material) {
3561 float finalAlpha = material ? 0.4f : 0f;
3562 revealView.setAlpha(1f);
3563 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3564 1f, finalAlpha);
3565 panelAlpha.setDuration(revealDuration);
3566 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3567 new AccelerateInterpolator(1.5f));
3568 mStateAnimation.play(panelAlpha);
3569 }
3570
3571 if (page != null) {
3572 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3573
3574 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3575 0, yDrift);
3576 page.setTranslationY(0);
3577 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3578 pageDrift.setInterpolator(decelerateInterpolator);
3579 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3580 mStateAnimation.play(pageDrift);
3581
3582 page.setAlpha(1f);
3583 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3584 itemsAlpha.setDuration(100);
3585 itemsAlpha.setInterpolator(decelerateInterpolator);
3586 mStateAnimation.play(itemsAlpha);
3587 }
3588
3589 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3590 pageIndicators.setAlpha(1f);
3591 ObjectAnimator indicatorsAlpha =
3592 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3593 indicatorsAlpha.setDuration(revealDuration);
3594 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3595 mStateAnimation.play(indicatorsAlpha);
3596
3597 width = revealView.getMeasuredWidth();
3598
3599 if (material) {
3600 if (!isWidgetTray) {
3601 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003602 }
Adam Cohen2854d252014-08-27 16:04:07 -07003603 int allAppsButtonSize = LauncherAppState.getInstance().
3604 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3605 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3606 Animator reveal =
3607 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3608 height / 2, revealRadius, finalRadius);
3609 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3610 reveal.setDuration(revealDuration);
3611 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003612
Adam Cohen2854d252014-08-27 16:04:07 -07003613 reveal.addListener(new AnimatorListenerAdapter() {
3614 public void onAnimationEnd(Animator animation) {
3615 revealView.setVisibility(View.INVISIBLE);
3616 if (!isWidgetTray) {
3617 allAppsButton.setVisibility(View.VISIBLE);
3618 }
3619 }
3620 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003621
Adam Cohen2854d252014-08-27 16:04:07 -07003622 mStateAnimation.play(reveal);
3623 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003624
Adam Cohen2854d252014-08-27 16:04:07 -07003625 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3626 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3627 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003628 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003629
3630 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003631 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003632 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003633 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003634 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3635 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003636 if (onCompleteRunnable != null) {
3637 onCompleteRunnable.run();
3638 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003639
3640 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003641 if (page != null) {
3642 page.setLayerType(View.LAYER_TYPE_NONE, null);
3643 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003644 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003645 // Unhide side pages
3646 int count = content.getChildCount();
3647 for (int i = 0; i < count; i++) {
3648 View child = content.getChildAt(i);
3649 child.setVisibility(View.VISIBLE);
3650 }
3651
3652 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003653 if (page != null) {
3654 page.setTranslationX(0);
3655 page.setTranslationY(0);
3656 page.setAlpha(1);
3657 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003658 content.setCurrentPage(content.getNextPage());
3659
Chet Haasebc2f0822012-10-26 17:59:53 -07003660 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003661 }
3662 });
3663
Adam Cohen2854d252014-08-27 16:04:07 -07003664 final AnimatorSet stateAnimation = mStateAnimation;
3665 final Runnable startAnimRunnable = new Runnable() {
3666 public void run() {
3667 // Check that mStateAnimation hasn't changed while
3668 // we waited for a layout/draw pass
3669 if (mStateAnimation != stateAnimation)
3670 return;
3671 dispatchOnLauncherTransitionStart(fromView, animated, false);
3672 dispatchOnLauncherTransitionStart(toView, animated, false);
3673
Kenny Guyd794a3f2014-09-16 15:17:58 +01003674 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003675 for (int i = 0; i < layerViews.size(); i++) {
3676 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003677 if (v != null) {
3678 boolean attached = true;
3679 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3680 attached = v.isAttachedToWindow();
3681 }
3682 if (attached) v.buildLayer();
3683 }
Adam Cohen2854d252014-08-27 16:04:07 -07003684 }
3685 }
3686 mStateAnimation.start();
3687 }
3688 };
3689 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003690 } else {
3691 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003692 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003693 dispatchOnLauncherTransitionStart(fromView, animated, true);
3694 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003695 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003696 dispatchOnLauncherTransitionStart(toView, animated, true);
3697 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003698 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003699 }
3700
Michael Jurkae326f182011-11-21 14:05:46 -08003701 @Override
3702 public void onTrimMemory(int level) {
3703 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003704 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003705 mAppsCustomizeTabHost.onTrimMemory();
3706 }
3707 }
3708
Adam Cohened307df2013-10-02 09:37:31 -07003709 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003710 showWorkspace(animated, null);
3711 }
3712
Adam Cohened307df2013-10-02 09:37:31 -07003713 protected void showWorkspace() {
3714 showWorkspace(true);
3715 }
3716
Adam Cohened66b2b2012-01-23 17:28:51 -08003717 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003718 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003719 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003720 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003721 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003722
Winson Chungc7d2b602012-05-16 17:02:20 -07003723 // Show the search bar (only animate if we were showing the drop target bar in spring
3724 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003725 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003726 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003727 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003728
Michael Jurkab3e22d92011-10-31 15:58:33 -07003729 // Set focus to the AppsCustomize button
3730 if (mAllAppsButton != null) {
3731 mAllAppsButton.requestFocus();
3732 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003733 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003734
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003735 // Change the state *after* we've called all the transition code
3736 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003737
Winson Chung5fb63472011-02-02 17:03:37 -08003738 // Resume the auto-advance of widgets
3739 mUserPresent = true;
3740 updateRunning();
3741
alanv1d4fde62012-10-17 13:15:47 -07003742 // Send an accessibility event to announce the context change
3743 getWindow().getDecorView()
3744 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003745
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003746 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003747 }
3748
Adam Cohened307df2013-10-02 09:37:31 -07003749 void showOverviewMode(boolean animated) {
3750 mWorkspace.setVisibility(View.VISIBLE);
3751 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3752 mState = State.WORKSPACE;
3753 onWorkspaceShown(animated);
3754 }
3755
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003756 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003757 }
3758
Winson Chung82963d52013-10-09 11:20:57 -07003759 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3760 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003761 if (mState != State.WORKSPACE) return;
3762
Winson Chung82963d52013-10-09 11:20:57 -07003763 if (resetPageToZero) {
3764 mAppsCustomizeTabHost.reset();
3765 }
Winson Chungc58497e2013-09-03 17:48:37 -07003766 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003767 mAppsCustomizeTabHost.post(new Runnable() {
3768 @Override
3769 public void run() {
3770 // We post this in-case the all apps view isn't yet constructed.
3771 mAppsCustomizeTabHost.requestFocus();
3772 }
3773 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003774
Michael Jurkab3e22d92011-10-31 15:58:33 -07003775 // Change the state *after* we've called all the transition code
3776 mState = State.APPS_CUSTOMIZE;
3777
3778 // Pause the auto-advance of widgets until we are out of AllApps
3779 mUserPresent = false;
3780 updateRunning();
3781 closeFolder();
3782
3783 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003784 getWindow().getDecorView()
3785 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003786 }
3787
Adam Cohen7777d962011-08-18 18:58:38 -07003788 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003789 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003790 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003791 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003792 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003793 }
Adam Cohen7777d962011-08-18 18:58:38 -07003794
Adam Cohenad4e15c2013-10-17 16:21:35 -07003795 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003796 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003797 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3798
Winson Chunge7a03942011-08-05 15:05:12 -07003799 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003800 @Override
3801 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003802 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003803 // Before we show workspace, hide all apps again because
3804 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3805 // clean up our state transition functions
3806 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003807 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003808 } else {
3809 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003810 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003811 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003812 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003813 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003814
Michael Jurkad3ef3062010-11-23 16:23:58 -08003815 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003816 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003817 final boolean animated = true;
3818 final boolean springLoaded = true;
3819 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003820 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003821 }
3822 // Otherwise, we are not in spring loaded mode, so don't do anything.
3823 }
3824
Michael Jurkab3e22d92011-10-31 15:58:33 -07003825 void lockAllApps() {
3826 // TODO
3827 }
3828
3829 void unlockAllApps() {
3830 // TODO
3831 }
3832
Winson Chungf0ea4d32011-06-06 14:27:16 -07003833 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003834 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003835 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003836 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003837 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003838 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003839 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003840 int duration = 0;
3841 if (mSearchDropTargetBar != null) {
3842 duration = mSearchDropTargetBar.getTransitionOutDuration();
3843 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003844 mHotseat.animate().alpha(0f).setDuration(duration);
3845 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003847 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003848 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003849 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003850 }
3851
Patrick Dubroy5f445422011-02-18 14:35:21 -08003852 /**
3853 * Add an item from all apps or customize onto the given workspace screen.
3854 * If layout is null, add to the current screen.
3855 */
3856 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003857 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003858 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003859 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003860 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003861
Winson Chungdff8ebb2011-09-08 17:25:31 -07003862 /** Maps the current orientation to an index for referencing orientation correct global icons */
3863 private int getCurrentOrientationIndexForGlobalIcons() {
3864 // default - 0, landscape - 1
3865 switch (getResources().getConfiguration().orientation) {
3866 case Configuration.ORIENTATION_LANDSCAPE:
3867 return 1;
3868 default:
3869 return 0;
3870 }
3871 }
3872
Michael Jurkaae65ee32012-04-30 11:45:54 -07003873 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003874 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003875 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003876 // Look for the toolbar icon specified in the activity meta-data
3877 Bundle metaData = packageManager.getActivityInfo(
3878 activityName, PackageManager.GET_META_DATA).metaData;
3879 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003880 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003881 if (iconResId != 0) {
3882 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003883 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003884 }
3885 }
3886 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003887 // This can happen if the activity defines an invalid drawable
3888 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3889 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003890 } catch (Resources.NotFoundException nfe) {
3891 // This can happen if the activity defines an invalid drawable
3892 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3893 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003894 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003895 return null;
3896 }
3897
3898 // if successful in getting icon, return it; otherwise, set button to use default drawable
3899 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003900 int buttonId, ComponentName activityName, int fallbackDrawableId,
3901 String toolbarResourceName) {
3902 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003903 Resources r = getResources();
3904 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3905 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003906
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003907 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003908 // If we were unable to find the icon via the meta-data, use a generic one
3909 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003910 toolbarIcon = r.getDrawable(fallbackDrawableId);
3911 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003912 if (button != null) {
3913 button.setCompoundDrawables(toolbarIcon, null, null, null);
3914 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003915 return null;
3916 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003917 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003918 if (button != null) {
3919 button.setCompoundDrawables(toolbarIcon, null, null, null);
3920 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003921 return toolbarIcon.getConstantState();
3922 }
3923 }
3924
3925 // if successful in getting icon, return it; otherwise, set button to use default drawable
3926 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003927 int buttonId, ComponentName activityName, int fallbackDrawableId,
3928 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003929 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003930 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003931
Michael Jurka19e0fc52011-07-22 18:00:21 -07003932 if (button != null) {
3933 // If we were unable to find the icon via the meta-data, use a
3934 // generic one
3935 if (toolbarIcon == null) {
3936 button.setImageResource(fallbackDrawableId);
3937 } else {
3938 button.setImageDrawable(toolbarIcon);
3939 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003940 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003941
3942 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3943
Michael Jurka0423dcf2010-10-05 14:56:18 -07003944 }
3945
Winson Chung1b884092012-06-08 17:13:02 -07003946 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003947 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003948 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003949 }
3950
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003951 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003952 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003953 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003954 }
3955
Winson Chungbb185bd2011-11-21 12:31:42 -08003956 private void invalidatePressedFocusedStates(View container, View button) {
3957 if (container instanceof HolographicLinearLayout) {
3958 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3959 layout.invalidatePressedFocusedStates();
3960 } else if (button instanceof HolographicImageView) {
3961 HolographicImageView view = (HolographicImageView) button;
3962 view.invalidatePressedFocusedStates();
3963 }
3964 }
3965
Cristina Stancu476493b2013-08-07 17:20:14 +01003966 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003967 if (mQsb == null) {
3968 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3969 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003970 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003971 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003972 }
3973
3974 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003975 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003976 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003977 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003978 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003979
Winson Chung1cad91e2011-05-25 17:41:01 -07003980 final SearchManager searchManager =
3981 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3982 ComponentName activityName = searchManager.getGlobalSearchActivity();
3983 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003984 int coi = getCurrentOrientationIndexForGlobalIcons();
3985 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003986 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3987 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3988 if (sGlobalSearchIcon[coi] == null) {
3989 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3990 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3991 TOOLBAR_ICON_METADATA_NAME);
3992 }
3993
Winson Chungc51db6a2011-10-05 11:44:49 -07003994 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3995 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003996 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003997 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003998 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003999 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004000 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4001 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004002 if (searchButton != null) searchButton.setVisibility(View.GONE);
4003 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004004 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004005 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004006 }
4007 }
4008
Cristina Stancu476493b2013-08-07 17:20:14 +01004009 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004010 final View searchButtonContainer = findViewById(R.id.search_button_container);
4011 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004012 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004013 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004014 }
4015
Cristina Stancu476493b2013-08-07 17:20:14 +01004016 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004017 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004018 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004019
Winson Chungc51db6a2011-10-05 11:44:49 -07004020 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004021 final SearchManager searchManager =
4022 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4023 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4024
4025 ComponentName activityName = null;
4026 if (globalSearchActivity != null) {
4027 // Check if the global search activity handles voice search
4028 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4029 intent.setPackage(globalSearchActivity.getPackageName());
4030 activityName = intent.resolveActivity(getPackageManager());
4031 }
4032
4033 if (activityName == null) {
4034 // Fallback: check if an activity other than the global search activity
4035 // resolves this
4036 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4037 activityName = intent.resolveActivity(getPackageManager());
4038 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004039 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004040 int coi = getCurrentOrientationIndexForGlobalIcons();
4041 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004042 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4043 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4044 if (sVoiceSearchIcon[coi] == null) {
4045 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4046 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4047 TOOLBAR_ICON_METADATA_NAME);
4048 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004049 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004050 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004051 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004052 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004053 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004054 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004055 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004056 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004057 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004058 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004059 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004060 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004061
Cristina Stancu476493b2013-08-07 17:20:14 +01004062 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004063 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4064 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004065 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004066 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004067 }
4068
Winson Chung5841efa2013-09-30 18:06:44 -07004069 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004070 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4071 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004072 boolean visible = !forceDisableVoiceButtonProxy &&
4073 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004074 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004075 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004076 }
4077 }
Winson Chung5841efa2013-09-30 18:06:44 -07004078
4079 /**
4080 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4081 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4082 */
4083 public void disableVoiceButtonProxy(boolean disabled) {
4084 updateVoiceButtonProxyVisible(disabled);
4085 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004086
Michael Jurkad7c28052012-04-27 15:43:36 -07004087 @Override
4088 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004089 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004090 final List<CharSequence> text = event.getText();
4091 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004092 // Populate event with a fake title based on the current state.
4093 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004094 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004095 } else {
4096 text.add(getString(R.string.all_apps_home_button_label));
4097 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004098 return result;
4099 }
4100
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004101 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004102 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004103 */
4104 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4105 @Override
4106 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004107 closeSystemDialogs();
4108 }
4109 }
4110
4111 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004112 * Receives notifications whenever the appwidgets are reset.
4113 */
4114 private class AppWidgetResetObserver extends ContentObserver {
4115 public AppWidgetResetObserver() {
4116 super(new Handler());
4117 }
4118
4119 @Override
4120 public void onChange(boolean selfChange) {
4121 onAppWidgetReset();
4122 }
4123 }
4124
4125 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004126 * If the activity is currently paused, signal that we need to run the passed Runnable
4127 * in onResume.
4128 *
4129 * This needs to be called from incoming places where resources might have been loaded
4130 * while we are paused. That is becaues the Configuration might be wrong
4131 * when we're not running, and if it comes back to what it was when we
4132 * were paused, we are not restarted.
4133 *
4134 * Implementation of the method from LauncherModel.Callbacks.
4135 *
4136 * @return true if we are currently paused. The caller might be able to
4137 * skip some work in that case since we will come back again.
4138 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004139 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004140 if (mPaused) {
4141 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004142 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004143 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004144 }
4145 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004146 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004147 return true;
4148 } else {
4149 return false;
4150 }
4151 }
4152
Michael Jurkac402cd92013-05-20 15:49:32 +02004153 private boolean waitUntilResume(Runnable run) {
4154 return waitUntilResume(run, false);
4155 }
4156
Michael Jurka1e2f4652013-07-08 18:03:46 -07004157 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004158 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004159 }
4160
Michael Jurka7607c2f2013-04-03 14:33:19 -07004161 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004162 * If the activity is currently paused, signal that we need to re-run the loader
4163 * in onResume.
4164 *
4165 * This needs to be called from incoming places where resources might have been loaded
4166 * while we are paused. That is becaues the Configuration might be wrong
4167 * when we're not running, and if it comes back to what it was when we
4168 * were paused, we are not restarted.
4169 *
4170 * Implementation of the method from LauncherModel.Callbacks.
4171 *
4172 * @return true if we are currently paused. The caller might be able to
4173 * skip some work in that case since we will come back again.
4174 */
4175 public boolean setLoadOnResume() {
4176 if (mPaused) {
4177 Log.i(TAG, "setLoadOnResume");
4178 mOnResumeNeedsLoad = true;
4179 return true;
4180 } else {
4181 return false;
4182 }
4183 }
4184
4185 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004187 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004189 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004190 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004191 } else {
4192 return SCREEN_COUNT / 2;
4193 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004195
Joe Onorato9c1289c2009-08-17 11:03:03 -04004196 /**
4197 * Refreshes the shortcuts shown on the workspace.
4198 *
4199 * Implementation of the method from LauncherModel.Callbacks.
4200 */
4201 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004202 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004203
Michael Jurka7607c2f2013-04-03 14:33:19 -07004204 // If we're starting binding all over again, clear any bind calls we'd postponed in
4205 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4206 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004207 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004208
Winson Chungd64d1762013-08-20 14:37:16 -07004209 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004210 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004211 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004212
Michael Jurka05bf6442011-11-30 20:28:53 -08004213 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004214 if (mHotseat != null) {
4215 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004216 }
4217 }
4218
Adam Cohendcd297f2013-06-18 13:13:40 -07004219 @Override
4220 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004221 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004222
Adam Cohen5084cba2013-09-03 12:01:16 -07004223 // If there are no screens, we need to have an empty screen
4224 if (orderedScreenIds.size() == 0) {
4225 mWorkspace.addExtraEmptyScreen();
4226 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004227
4228 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004229 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004230 if (hasCustomContentToLeft()) {
4231 mWorkspace.createCustomContentContainer();
4232 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004233 }
Winson Chung64359a52013-07-08 17:17:08 -07004234 }
4235
4236 @Override
4237 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004238 // Log to disk
4239 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4240 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4241 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004242 int count = orderedScreenIds.size();
4243 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004244 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004245 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004246 }
4247
Adam Cohen39a06042013-07-19 14:30:12 -07004248 private boolean shouldShowWeightWatcher() {
4249 String spKey = LauncherAppState.getSharedPreferencesKey();
4250 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004251 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004252
4253 return show;
4254 }
4255
4256 private void toggleShowWeightWatcher() {
4257 String spKey = LauncherAppState.getSharedPreferencesKey();
4258 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4259 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4260
4261 show = !show;
4262
4263 SharedPreferences.Editor editor = sp.edit();
4264 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4265 editor.commit();
4266
4267 if (mWeightWatcher != null) {
4268 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4269 }
4270 }
4271
Winson Chungd64d1762013-08-20 14:37:16 -07004272 public void bindAppsAdded(final ArrayList<Long> newScreens,
4273 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004274 final ArrayList<ItemInfo> addAnimated,
4275 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004276 Runnable r = new Runnable() {
4277 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004278 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004279 }
4280 };
4281 if (waitUntilResume(r)) {
4282 return;
4283 }
4284
Winson Chungd64d1762013-08-20 14:37:16 -07004285 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004286 if (newScreens != null) {
4287 bindAddScreens(newScreens);
4288 }
Winson Chungd64d1762013-08-20 14:37:16 -07004289
4290 // We add the items without animation on non-visible pages, and with
4291 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004292 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004293 bindItems(addNotAnimated, 0,
4294 addNotAnimated.size(), false);
4295 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004296 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004297 bindItems(addAnimated, 0,
4298 addAnimated.size(), true);
4299 }
Winson Chungc58497e2013-09-03 17:48:37 -07004300
Winson Chung87412982013-10-03 18:34:14 -07004301 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004302 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004303
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004304 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004305 addedApps != null && mAppsCustomizeContent != null) {
4306 mAppsCustomizeContent.addApps(addedApps);
4307 }
Winson Chungd64d1762013-08-20 14:37:16 -07004308 }
4309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004310 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 * Bind the items start-end from the list.
4312 *
4313 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004314 */
Winson Chung64359a52013-07-08 17:17:08 -07004315 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4316 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004317 Runnable r = new Runnable() {
4318 public void run() {
4319 bindItems(shortcuts, start, end, forceAnimateIcons);
4320 }
4321 };
4322 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004323 return;
4324 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004325
Winson Chungf0c6ae02012-03-21 16:10:31 -07004326 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004327 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4328 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004329 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004330 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004331 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004332 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004334
4335 // Short circuit if we are loading dock items for a configuration which has no dock
4336 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4337 mHotseat == null) {
4338 continue;
4339 }
4340
Joe Onorato9c1289c2009-08-17 11:03:03 -04004341 switch (item.itemType) {
4342 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4343 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004344 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004345 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004346
Winson Chung64359a52013-07-08 17:17:08 -07004347 /*
4348 * TODO: FIX collision case
4349 */
Winson Chungce376632013-07-11 16:12:41 -07004350 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4351 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4352 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004353 View v = cl.getChildAt(item.cellX, item.cellY);
4354 Object tag = v.getTag();
4355 String desc = "Collision while binding workspace item: " + item
4356 + ". Collides with " + tag;
4357 if (LauncherAppState.isDogfoodBuild()) {
4358 throw (new RuntimeException(desc));
4359 } else {
4360 Log.d(TAG, desc);
4361 }
Winson Chungce376632013-07-11 16:12:41 -07004362 }
Winson Chung64359a52013-07-08 17:17:08 -07004363 }
4364
Adam Cohendcd297f2013-06-18 13:13:40 -07004365 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4366 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004367 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004368 // Animate all the applications up now
4369 shortcut.setAlpha(0f);
4370 shortcut.setScaleX(0f);
4371 shortcut.setScaleY(0f);
4372 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004373 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004375 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004376 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004377 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004378 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004379 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004380 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4381 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004382 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004383 default:
4384 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004386 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004387
Winson Chung997a9232013-07-24 15:33:46 -07004388 if (animateIcons) {
4389 // Animate to the correct page
4390 if (newShortcutsScreenId > -1) {
4391 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004392 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004393 final Runnable startBounceAnimRunnable = new Runnable() {
4394 public void run() {
4395 anim.playTogether(bounceAnims);
4396 anim.start();
4397 }
4398 };
Winson Chung997a9232013-07-24 15:33:46 -07004399 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004400 // We post the animation slightly delayed to prevent slowdowns
4401 // when we are loading right after we return to launcher.
4402 mWorkspace.postDelayed(new Runnable() {
4403 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004404 if (mWorkspace != null) {
4405 mWorkspace.snapToPage(newScreenIndex);
4406 mWorkspace.postDelayed(startBounceAnimRunnable,
4407 NEW_APPS_ANIMATION_DELAY);
4408 }
Winson Chung94d67682013-09-25 16:29:40 -07004409 }
4410 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004411 } else {
4412 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004413 }
4414 }
Winson Chung64359a52013-07-08 17:17:08 -07004415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004416 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004417 }
4418
Joe Onorato9c1289c2009-08-17 11:03:03 -04004419 /**
4420 * Implementation of the method from LauncherModel.Callbacks.
4421 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004422 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004423 Runnable r = new Runnable() {
4424 public void run() {
4425 bindFolders(folders);
4426 }
4427 };
4428 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004429 return;
4430 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004431 sFolders.clear();
4432 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004433 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004434
4435 /**
4436 * Add the views for a widget to the workspace.
4437 *
4438 * Implementation of the method from LauncherModel.Callbacks.
4439 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004440 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004441 Runnable r = new Runnable() {
4442 public void run() {
4443 bindAppWidget(item);
4444 }
4445 };
4446 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004447 return;
4448 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004449
Daniel Sandler843e8602010-06-07 14:59:01 -04004450 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4451 if (DEBUG_WIDGETS) {
4452 Log.d(TAG, "bindAppWidget: " + item);
4453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004454 final Workspace workspace = mWorkspace;
4455
Sunny Goyalff572272014-07-23 13:58:07 -07004456 AppWidgetProviderInfo appWidgetInfo;
4457 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4458 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4459
4460 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4461 if (appWidgetInfo == null) {
4462 if (DEBUG_WIDGETS) {
4463 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4464 + " belongs to component " + item.providerName
4465 + ", as the povider is null");
4466 }
4467 LauncherModel.deleteItemFromDatabase(this, item);
4468 return;
4469 }
4470 // Note: This assumes that the id remap broadcast is received before this step.
4471 // If that is not the case, the id remap will be ignored and user may see the
4472 // click to setup view.
4473 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4474 pendingInfo.spanX = item.spanX;
4475 pendingInfo.spanY = item.spanY;
4476 pendingInfo.minSpanX = item.minSpanX;
4477 pendingInfo.minSpanY = item.minSpanY;
4478 Bundle options =
4479 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4480
Sunny Goyalff572272014-07-23 13:58:07 -07004481 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004482 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4483 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004484
4485 // TODO consider showing a permission dialog when the widget is clicked.
4486 if (!success) {
4487 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4488 if (DEBUG_WIDGETS) {
4489 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4490 + " belongs to component " + item.providerName
4491 + ", as the launcher is unable to bing a new widget id");
4492 }
4493 LauncherModel.deleteItemFromDatabase(this, item);
4494 return;
4495 }
4496
4497 item.appWidgetId = newWidgetId;
4498
4499 // If the widget has a configure activity, it is still needs to set it up, otherwise
4500 // the widget is ready to go.
4501 item.restoreStatus = (appWidgetInfo.configure == null)
4502 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4503 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4504
4505 LauncherModel.updateItemInDatabase(this, item);
4506 }
4507
Sunny Goyal651077b2014-06-30 14:15:31 -07004508 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4509 final int appWidgetId = item.appWidgetId;
4510 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4511 if (DEBUG_WIDGETS) {
4512 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4513 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004514
Sunny Goyal651077b2014-06-30 14:15:31 -07004515 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4516 } else {
4517 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004518 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4519 view.updateIcon(mIconCache);
4520 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004521 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004522 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004524
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004526 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004527
Adam Cohendcd297f2013-06-18 13:13:40 -07004528 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004529 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004530 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4531
Joe Onorato9c1289c2009-08-17 11:03:03 -04004532 workspace.requestLayout();
4533
Daniel Sandler843e8602010-06-07 14:59:01 -04004534 if (DEBUG_WIDGETS) {
4535 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4536 + (SystemClock.uptimeMillis()-start) + "ms");
4537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004538 }
4539
Sunny Goyalff572272014-07-23 13:58:07 -07004540 /**
4541 * Restores a pending widget.
4542 *
4543 * @param appWidgetId The app widget id
4544 * @param cellInfo The position on screen where to create the widget.
4545 */
4546 private void completeRestoreAppWidget(final int appWidgetId) {
4547 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4548 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4549 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4550 return;
4551 }
4552
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004553 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004554 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4555
4556 mWorkspace.reinflateWidgetsIfNecessary();
4557 LauncherModel.updateItemInDatabase(this, info);
4558 }
4559
Adam Cohen1462de32012-07-24 22:34:36 -07004560 public void onPageBoundSynchronously(int page) {
4561 mSynchronouslyBoundPages.add(page);
4562 }
4563
Joe Onorato9c1289c2009-08-17 11:03:03 -04004564 /**
4565 * Callback saying that there aren't any more items to bind.
4566 *
4567 * Implementation of the method from LauncherModel.Callbacks.
4568 */
Adam Cohene25af792013-06-06 23:08:25 -07004569 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004570 Runnable r = new Runnable() {
4571 public void run() {
4572 finishBindingItems(upgradePath);
4573 }
4574 };
4575 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004576 return;
4577 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004578 if (mSavedState != null) {
4579 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004580 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004581 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004582 mSavedState = null;
4583 }
4584
Adam Cohen1462de32012-07-24 22:34:36 -07004585 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004586
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004587 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004588 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004589
4590 // If we received the result of any pending adds while the loader was running (e.g. the
4591 // widget configuration forced an orientation change), process them now.
4592 if (sPendingAddItem != null) {
4593 final long screenId = completeAdd(sPendingAddItem);
4594
4595 // TODO: this moves the user to the page where the pending item was added. Ideally,
4596 // the screen would be guaranteed to exist after bind, and the page would be set through
4597 // the workspace restore process.
4598 mWorkspace.post(new Runnable() {
4599 @Override
4600 public void run() {
4601 mWorkspace.snapToScreenId(screenId);
4602 }
4603 });
4604 sPendingAddItem = null;
4605 }
4606
Adam Cohene25af792013-06-06 23:08:25 -07004607 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004608 mWorkspace.getUniqueComponents(true, null);
4609 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004610 }
Sunny Goyale755d462014-07-22 13:48:29 -07004611 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004612 }
4613
Adam Cohen3ed316a2014-07-23 18:21:20 -07004614 private void sendLoadingCompleteBroadcastIfNecessary() {
4615 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4616 String permission =
4617 getResources().getString(R.string.receive_first_load_broadcast_permission);
4618 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4619 sendBroadcast(intent, permission);
4620 SharedPreferences.Editor editor = mSharedPrefs.edit();
4621 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4622 editor.apply();
4623 }
4624 }
4625
Winson Chunga0b7e862013-09-05 16:03:15 -07004626 public boolean isAllAppsButtonRank(int rank) {
4627 if (mHotseat != null) {
4628 return mHotseat.isAllAppsButtonRank(rank);
4629 }
4630 return false;
4631 }
4632
Winson Chunga2413752012-04-03 14:22:34 -07004633 private boolean canRunNewAppsAnimation() {
4634 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4635 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4636 }
4637
Winson Chungc9168342013-06-26 14:54:55 -07004638 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4639 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4640 PropertyValuesHolder.ofFloat("alpha", 1f),
4641 PropertyValuesHolder.ofFloat("scaleX", 1f),
4642 PropertyValuesHolder.ofFloat("scaleY", 1f));
4643 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4644 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4645 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4646 return bounceAnim;
4647 }
4648
Adam Cohen27772732013-11-11 14:00:56 +00004649 public boolean useVerticalBarLayout() {
4650 return LauncherAppState.getInstance().getDynamicGrid().
4651 getDeviceProfile().isVerticalBarLayout();
4652 }
4653
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004654 protected Rect getSearchBarBounds() {
4655 return LauncherAppState.getInstance().getDynamicGrid().
4656 getDeviceProfile().getSearchBarBounds();
4657 }
4658
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004659 @Override
4660 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004661 boolean searchVisible = updateGlobalSearchIcon();
4662 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004663 if (mSearchDropTargetBar != null) {
4664 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4665 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004666 }
4667
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004668 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004669 * Add the icons for all apps.
4670 *
4671 * Implementation of the method from LauncherModel.Callbacks.
4672 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004673 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004674 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004675 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4676 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4677 getHotseat().addAllAppsFolder(mIconCache, apps,
4678 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4679 }
4680 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004681 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004682 if (mAppsCustomizeContent != null) {
4683 mAppsCustomizeContent.onPackagesUpdated(
4684 LauncherModel.getSortedWidgetsAndShortcuts(this));
4685 }
Winson Chungc58497e2013-09-03 17:48:37 -07004686 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004687 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004688 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004689 mAppsCustomizeContent.onPackagesUpdated(
4690 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004691 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004692 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004693 }
4694
4695 /**
4696 * A package was updated.
4697 *
4698 * Implementation of the method from LauncherModel.Callbacks.
4699 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004700 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004701 Runnable r = new Runnable() {
4702 public void run() {
4703 bindAppsUpdated(apps);
4704 }
4705 };
4706 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004707 return;
4708 }
4709
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004710 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004711 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004712 }
Winson Chungc58497e2013-09-03 17:48:37 -07004713
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004714 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004715 mAppsCustomizeContent != null) {
4716 mAppsCustomizeContent.updateApps(apps);
4717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004718 }
4719
4720 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004721 * Update the state of a package, typically related to install state.
4722 *
4723 * Implementation of the method from LauncherModel.Callbacks.
4724 */
Sunny Goyale755d462014-07-22 13:48:29 -07004725 @Override
4726 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004727 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004728 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004729 }
4730 }
4731
4732 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004733 * Update the label and icon of all the icons in a package
4734 *
4735 * Implementation of the method from LauncherModel.Callbacks.
4736 */
4737 @Override
4738 public void updatePackageBadge(String packageName) {
4739 if (mWorkspace != null) {
4740 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4741 }
4742 }
4743
4744 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004745 * A package was uninstalled. We take both the super set of packageNames
4746 * in addition to specific applications to remove, the reason being that
4747 * this can be called when a package is updated as well. In that scenario,
4748 * we only remove specific components from the workspace, where as
4749 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004750 *
4751 * Implementation of the method from LauncherModel.Callbacks.
4752 */
Winson Chung83892cc2013-05-01 16:53:33 -07004753 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004754 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004755 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004756 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004757 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004758 }
Winson Chungd64d1762013-08-20 14:37:16 -07004759 };
4760 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004761 return;
4762 }
4763
Winson Chungdf95eb12013-10-16 14:57:07 -07004764 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004765 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004766 }
4767 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004768 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004769 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004770
Winson Chunga1820962011-10-03 16:31:06 -07004771 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004772 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004773
Winson Chungdf95eb12013-10-16 14:57:07 -07004774 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004775 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004776 mAppsCustomizeContent != null) {
4777 mAppsCustomizeContent.removeApps(appInfos);
4778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004779 }
Joe Onoratobe386092009-11-17 17:32:16 -08004780
4781 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004782 * A number of packages were updated.
4783 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004784 private ArrayList<Object> mWidgetsAndShortcuts;
4785 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004786 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004787 bindPackagesUpdated(mWidgetsAndShortcuts);
4788 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004789 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004790 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004791 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4792 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4793 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004794 return;
4795 }
4796
Winson Chung64359a52013-07-08 17:17:08 -07004797 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004798 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004799 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004800 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004801 }
4802
Winson Chung400438b2011-01-16 17:53:48 -08004803 private int mapConfigurationOriActivityInfoOri(int configOri) {
4804 final Display d = getWindowManager().getDefaultDisplay();
4805 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4806 switch (d.getRotation()) {
4807 case Surface.ROTATION_0:
4808 case Surface.ROTATION_180:
4809 // We are currently in the same basic orientation as the natural orientation
4810 naturalOri = configOri;
4811 break;
4812 case Surface.ROTATION_90:
4813 case Surface.ROTATION_270:
4814 // We are currently in the other basic orientation to the natural orientation
4815 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4816 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4817 break;
4818 }
4819
4820 int[] oriMap = {
4821 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4822 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4823 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4824 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4825 };
4826 // Since the map starts at portrait, we need to offset if this device's natural orientation
4827 // is landscape.
4828 int indexOffset = 0;
4829 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4830 indexOffset = 1;
4831 }
4832 return oriMap[(d.getRotation() + indexOffset) % 4];
4833 }
Adam Cohen446e9402011-09-15 18:21:21 -07004834
Winson Chung4b919f82012-05-01 10:44:08 -07004835 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004836 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004837 getResources().getBoolean(R.bool.allow_rotation);
4838 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004839 }
Winson Chung4b919f82012-05-01 10:44:08 -07004840 public void lockScreenOrientation() {
4841 if (isRotationEnabled()) {
4842 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4843 .getConfiguration().orientation));
4844 }
4845 }
4846 public void unlockScreenOrientation(boolean immediate) {
4847 if (isRotationEnabled()) {
4848 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004849 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004850 } else {
4851 mHandler.postDelayed(new Runnable() {
4852 public void run() {
4853 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4854 }
4855 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004856 }
Winson Chung4b919f82012-05-01 10:44:08 -07004857 }
Winson Chung400438b2011-01-16 17:53:48 -08004858 }
4859
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004860 /**
4861 * Called when the SearchBar hint should be changed.
4862 *
4863 * @param hint the hint to be displayed in the search bar.
4864 */
4865 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004866
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004867 }
4868
Winson Chunge43a1e72014-01-15 10:33:02 -08004869 protected boolean isLauncherPreinstalled() {
4870 PackageManager pm = getPackageManager();
4871 try {
4872 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4873 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4874 return true;
4875 } else {
4876 return false;
4877 }
4878 } catch (NameNotFoundException e) {
4879 e.printStackTrace();
4880 return false;
4881 }
4882 }
4883
Adam Cohenea90f832014-05-21 15:03:34 -07004884 /**
4885 * This method indicates whether or not we should suggest default wallpaper dimensions
4886 * when our wallpaper cropper was not yet used to set a wallpaper.
4887 */
4888 protected boolean overrideWallpaperDimensions() {
4889 return true;
4890 }
4891
Adam Cohen5eed5d82014-05-19 13:12:13 -07004892 protected boolean shouldClingFocusHotseatApp() {
4893 return false;
4894 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004895 protected String getFirstRunClingSearchBarHint() {
4896 return "";
4897 }
4898 protected String getFirstRunCustomContentHint() {
4899 return "";
4900 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004901 protected int getFirstRunFocusedHotseatAppDrawableId() {
4902 return -1;
4903 }
4904 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4905 return null;
4906 }
4907 protected int getFirstRunFocusedHotseatAppRank() {
4908 return -1;
4909 }
4910 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4911 return "";
4912 }
4913 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4914 return "";
4915 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004916
Adam Cohen432609a2014-03-13 17:03:22 -07004917 /**
4918 * To be overridden by subclasses to indicate that there is an activity to launch
4919 * before showing the standard launcher experience.
4920 */
4921 protected boolean hasFirstRunActivity() {
4922 return false;
4923 }
4924
4925 /**
4926 * To be overridden by subclasses to launch any first run activity
4927 */
4928 protected Intent getFirstRunActivity() {
4929 return null;
4930 }
4931
Winson Chunga6945242014-01-08 14:04:34 -08004932 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004933 return !ActivityManager.isRunningInTestHarness() &&
4934 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004935 }
4936
Adam Cohen4a9423d2014-03-28 14:22:31 -07004937 protected boolean hasRunFirstRunActivity() {
4938 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4939 }
4940
Adam Cohen432609a2014-03-13 17:03:22 -07004941 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004942 if (shouldRunFirstRunActivity() &&
4943 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004944 Intent firstRunIntent = getFirstRunActivity();
4945 if (firstRunIntent != null) {
4946 startActivity(firstRunIntent);
4947 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004948 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004949 }
4950 }
Adam Cohen432609a2014-03-13 17:03:22 -07004951 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004952 }
4953
4954 private void markFirstRunActivityShown() {
4955 SharedPreferences.Editor editor = mSharedPrefs.edit();
4956 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4957 editor.apply();
4958 }
4959
Adam Cohen432609a2014-03-13 17:03:22 -07004960 /**
4961 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4962 * screen that must be displayed and dismissed.
4963 */
4964 protected boolean hasDismissableIntroScreen() {
4965 return false;
4966 }
4967
4968 /**
4969 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4970 */
4971 protected View getIntroScreen() {
4972 return null;
4973 }
4974
4975 /**
4976 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4977 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4978 */
4979 private boolean shouldShowIntroScreen() {
4980 return hasDismissableIntroScreen() &&
4981 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4982 }
4983
4984 protected void showIntroScreen() {
4985 View introScreen = getIntroScreen();
4986 changeWallpaperVisiblity(false);
4987 if (introScreen != null) {
4988 mDragLayer.showOverlayView(introScreen);
4989 }
4990 }
4991
4992 public void dismissIntroScreen() {
4993 markIntroScreenDismissed();
4994 if (showFirstRunActivity()) {
4995 // We delay hiding the intro view until the first run activity is showing. This
4996 // avoids a blip.
4997 mWorkspace.postDelayed(new Runnable() {
4998 @Override
4999 public void run() {
5000 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005001 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005002 }
5003 }, ACTIVITY_START_DELAY);
5004 } else {
5005 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005006 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005007 }
5008 changeWallpaperVisiblity(true);
5009 }
5010
5011 private void markIntroScreenDismissed() {
5012 SharedPreferences.Editor editor = mSharedPrefs.edit();
5013 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5014 editor.apply();
5015 }
5016
Sunny Goyal88d60f12014-09-08 03:47:29 -07005017 private void showFirstRunClings() {
5018 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5019 // on the device, then we always show the first run cling experience (or if there is no
5020 // launcher2). Otherwise, we prompt the user upon started for migration
5021 LauncherClings launcherClings = new LauncherClings(this);
5022 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5023 if (mModel.canMigrateFromOldLauncherDb(this)) {
5024 launcherClings.showMigrationCling();
5025 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005026 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005027 }
5028 }
5029 }
5030
Winson Chunga6945242014-01-08 14:04:34 -08005031 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005032 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5033 if (mHotseat != null) mHotseat.setAlpha(1f);
5034 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5035 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005036 }
5037
5038 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005039 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5040 if (mHotseat != null) mHotseat.setAlpha(0f);
5041 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5042 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005043 }
5044
Mathew Inwood72fbec12013-11-19 15:45:07 +00005045 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005046 // Called from search suggestion, not supported in other profiles.
5047 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5048 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5049 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5050 myUser);
5051 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005052 return null;
5053 }
Kenny Guyed131872014-04-30 03:02:21 +01005054 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005055 }
5056
5057 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5058 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005059 // Called from search suggestion, not supported in other profiles.
5060 return createShortcutDragInfo(shortcutIntent, caption, icon,
5061 UserHandleCompat.myUserHandle());
5062 }
5063
5064 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5065 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005066 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005067 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005068 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005069 }
5070
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005071 protected void moveWorkspaceToDefaultScreen() {
5072 mWorkspace.moveToDefaultScreen(false);
5073 }
5074
Mathew Inwood72fbec12013-11-19 15:45:07 +00005075 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5076 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005077 mWorkspace.onExternalDragStartedWithItem(dragView);
5078 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005079 }
5080
Anjali Koppalf5d29132014-02-28 13:33:27 -08005081 @Override
5082 public void onPageSwitch(View newPage, int newPageIndex) {
5083 }
5084
Winson Chung80baf5a2010-08-09 16:03:15 -07005085 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005086 * Prints out out state for debugging.
5087 */
5088 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005089 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005090 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005091 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5092 Log.d(TAG, "mRestoring=" + mRestoring);
5093 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5094 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005095 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005096 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005097
Winson Chung785d2eb2011-04-14 16:08:02 -07005098 if (mAppsCustomizeContent != null) {
5099 mAppsCustomizeContent.dumpState();
5100 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005101 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005102 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005103
5104 @Override
5105 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5106 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005107 synchronized (sDumpLogs) {
5108 writer.println(" ");
5109 writer.println("Debug logs: ");
5110 for (int i = 0; i < sDumpLogs.size(); i++) {
5111 writer.println(" " + sDumpLogs.get(i));
5112 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005113 }
5114 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005115
5116 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005117 if (DEBUG_DUMP_LOG) {
5118 synchronized (sDumpLogs) {
5119 Log.d(TAG, "");
5120 Log.d(TAG, "*********************");
5121 Log.d(TAG, "Launcher debug logs: ");
5122 for (int i = 0; i < sDumpLogs.size(); i++) {
5123 Log.d(TAG, " " + sDumpLogs.get(i));
5124 }
5125 Log.d(TAG, "*********************");
5126 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005127 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005128 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005129 }
5130
5131 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005132 addDumpLog(tag, log, null, debugLog);
5133 }
5134
5135 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005136 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005137 if (e != null) {
5138 Log.d(tag, log, e);
5139 } else {
5140 Log.d(tag, log);
5141 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005142 }
Winson Chungede41292013-09-19 16:27:36 -07005143 if (DEBUG_DUMP_LOG) {
5144 sDateStamp.setTime(System.currentTimeMillis());
5145 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005146 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5147 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005148 }
Winson Chungede41292013-09-19 16:27:36 -07005149 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005150 }
5151
Winson Chungede41292013-09-19 16:27:36 -07005152 public void dumpLogsToLocalData() {
5153 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005154 new AsyncTask<Void, Void, Void>() {
5155 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005156 boolean success = false;
5157 sDateStamp.setTime(sRunStart);
5158 String FILENAME = sDateStamp.getMonth() + "-"
5159 + sDateStamp.getDay() + "_"
5160 + sDateStamp.getHours() + "-"
5161 + sDateStamp.getMinutes() + "_"
5162 + sDateStamp.getSeconds() + ".txt";
5163
5164 FileOutputStream fos = null;
5165 File outFile = null;
5166 try {
5167 outFile = new File(getFilesDir(), FILENAME);
5168 outFile.createNewFile();
5169 fos = new FileOutputStream(outFile);
5170 } catch (Exception e) {
5171 e.printStackTrace();
5172 }
5173 if (fos != null) {
5174 PrintWriter writer = new PrintWriter(fos);
5175
5176 writer.println(" ");
5177 writer.println("Debug logs: ");
5178 synchronized (sDumpLogs) {
5179 for (int i = 0; i < sDumpLogs.size(); i++) {
5180 writer.println(" " + sDumpLogs.get(i));
5181 }
5182 }
5183 writer.close();
5184 }
5185 try {
5186 if (fos != null) {
5187 fos.close();
5188 success = true;
5189 }
5190 } catch (IOException e) {
5191 e.printStackTrace();
5192 }
Michael Jurka43467462013-11-14 12:03:58 +01005193 return null;
Winson Chungede41292013-09-19 16:27:36 -07005194 }
Michael Jurka43467462013-11-14 12:03:58 +01005195 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005196 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005198}
Michael Jurka2763be32011-02-24 11:19:57 -08005199
Michael Jurkaabded662011-03-04 12:06:57 -08005200interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005201 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005202 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005203 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005204 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005205 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005206}
Dan Sandlerd5024042014-01-09 15:01:33 -05005207
5208interface DebugIntents {
5209 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5210 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005211}