blob: 6603dcedb01f53360a8e5d75b12b1e0b0ef1316d [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
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000692 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100693 if (Build.VERSION.SDK_INT >= 17) {
694 return View.generateViewId();
695 } else {
696 // View.generateViewId() is not available. The following fallback logic is a copy
697 // of its implementation.
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 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 }
707 }
708 }
709
710 public int getViewIdForItem(ItemInfo info) {
711 // This cast is safe given the > 2B range for int.
712 int itemId = (int) info.id;
713 if (mItemIdToViewId.containsKey(itemId)) {
714 return mItemIdToViewId.get(itemId);
715 }
716 int viewId = generateViewId();
717 mItemIdToViewId.put(itemId, viewId);
718 return viewId;
719 }
720
721 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700722 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
723 * a configuration step, this allows the proper animations to run after other transitions.
724 */
Adam Cohendb364c32014-05-20 14:23:40 -0700725 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700726 long screenId = args.screenId;
727 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
728 // When the screen id represents an actual screen (as opposed to a rank) we make sure
729 // that the drop page actually exists.
730 screenId = ensurePendingDropLayoutExists(args.screenId);
731 }
Adam Cohendb364c32014-05-20 14:23:40 -0700732
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800733 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700735 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700736 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700737 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700739 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700740 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700741 case REQUEST_RECONFIGURE_APPWIDGET:
742 completeRestoreAppWidget(args.appWidgetId);
743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 }
Michael Jurka27614d22012-04-02 06:40:22 -0700745 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
746 // if you turned the screen off and then back while in All Apps, Launcher would not
747 // return to the workspace. Clearing mAddInfo.container here fixes this issue
748 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700749 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800750 }
751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700753 protected void onActivityResult(
754 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700755 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700756 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700757 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800758 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700759
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 Runnable exitSpringLoaded = new Runnable() {
761 @Override
762 public void run() {
763 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
764 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
765 }
766 };
767
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
771 if (resultCode == RESULT_CANCELED) {
772 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700773 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
777 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700778 }
779 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200780 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
781 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
782 mWorkspace.exitOverviewMode(false);
783 }
784 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200786
Adam Cohened66b2b2012-01-23 17:28:51 -0800787 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
788 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700789
Adam Cohendb364c32014-05-20 14:23:40 -0700790 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 // We have special handling for widgets
792 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800793 final int appWidgetId;
794 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
795 : -1;
796 if (widgetId < 0) {
797 appWidgetId = pendingAddWidgetId;
798 } else {
799 appWidgetId = widgetId;
800 }
801
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800803 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
805 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 result = RESULT_CANCELED;
807 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700808 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700809 @Override
810 public void run() {
811 exitSpringLoadedDragModeDelayed(false, 0, null);
812 }
813 };
Adam Cohendb364c32014-05-20 14:23:40 -0700814 if (workspaceLocked) {
815 // No need to remove the empty screen if we're mid-binding, as the
816 // the bind will not add the empty screen.
817 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
818 } else {
819 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
820 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
821 }
Winson Chung5aaab772012-04-27 15:24:02 -0700822 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700823 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700824 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
825 // When the screen id represents an actual screen (as opposed to a rank)
826 // we make sure that the drop page actually exists.
827 mPendingAddInfo.screenId =
828 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
829 }
Adam Cohendb364c32014-05-20 14:23:40 -0700830 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
831
832 dropLayout.setDropPending(true);
833 final Runnable onComplete = new Runnable() {
834 @Override
835 public void run() {
836 completeTwoStageWidgetDrop(resultCode, appWidgetId);
837 dropLayout.setDropPending(false);
838 }
839 };
840 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 } else {
843 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
844 mPendingAddInfo);
845 sPendingAddItem = args;
846 }
Winson Chung5aaab772012-04-27 15:24:02 -0700847 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800848 return;
849 }
850
Sunny Goyalff572272014-07-23 13:58:07 -0700851 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
852 if (resultCode == RESULT_OK) {
853 // Update the widget view.
854 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
855 pendingAddWidgetId, mPendingAddInfo);
856 if (workspaceLocked) {
857 sPendingAddItem = args;
858 } else {
859 completeAdd(args);
860 }
861 }
862 // Leave the widget in the pending state if the user canceled the configure.
863 return;
864 }
865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 // The pattern used here is that a user PICKs a specific application,
867 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700868
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
870 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700871 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700872 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
873 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800874 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700875 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800876 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700877 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700878 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
879 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 }
Adam Cohen00074722013-10-11 17:46:09 -0700881 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700882 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700883 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800886 }
887
Adam Cohendb364c32014-05-20 14:23:40 -0700888 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
889 appWidgetId, ItemInfo info) {
890 PendingAddArguments args = new PendingAddArguments();
891 args.requestCode = requestCode;
892 args.intent = data;
893 args.container = info.container;
894 args.screenId = info.screenId;
895 args.cellX = info.cellX;
896 args.cellY = info.cellY;
897 args.appWidgetId = appWidgetId;
898 return args;
899 }
900
901 /**
902 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
903 *
904 * @param screenId the screen id to check
905 * @return the new screen, or screenId if it exists
906 */
907 private long ensurePendingDropLayoutExists(long screenId) {
908 CellLayout dropLayout =
909 (CellLayout) mWorkspace.getScreenWithId(screenId);
910 if (dropLayout == null) {
911 // it's possible that the add screen was removed because it was
912 // empty and a re-bind occurred
913 mWorkspace.addExtraEmptyScreen();
914 return mWorkspace.commitExtraEmptyScreen();
915 } else {
916 return screenId;
917 }
918 }
919
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700921 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700922 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 Runnable onCompleteRunnable = null;
924 int animationType = 0;
925
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700926 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 if (resultCode == RESULT_OK) {
928 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
929 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700930 mPendingAddWidgetInfo);
931 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 onCompleteRunnable = new Runnable() {
933 @Override
934 public void run() {
935 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
938 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
940 };
941 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 if (mDragLayer.getAnimatedView() != null) {
946 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
947 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
948 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700949 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 // The animated view may be null in the case of a rotation during widget configuration
951 onCompleteRunnable.run();
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
955 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700956 protected void onStop() {
957 super.onStop();
958 FirstFrameAnimatorHelper.setIsVisible(false);
959 }
960
961 @Override
962 protected void onStart() {
963 super.onStart();
964 FirstFrameAnimatorHelper.setIsVisible(true);
965 }
966
967 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200969 long startTime = 0;
970 if (DEBUG_RESUME_TIME) {
971 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400972 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700975
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 // Restore the previous launcher state
977 if (mOnResumeState == State.WORKSPACE) {
978 showWorkspace(false);
979 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800980 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 }
982 mOnResumeState = State.NONE;
983
Craig Mautner360310b2012-10-26 15:13:08 -0700984 // Background was set to gradient in onPause(), restore to black if in all apps.
985 setWorkspaceBackground(mState == State.WORKSPACE);
986
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800987 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700988 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700989 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500990 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700992 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 // We might have postponed some bind calls until onResume (see waitUntilResume) --
996 // execute them here
997 long startTimeCallbacks = 0;
998 if (DEBUG_RESUME_TIME) {
999 startTimeCallbacks = System.currentTimeMillis();
1000 }
1001
1002 if (mAppsCustomizeContent != null) {
1003 mAppsCustomizeContent.setBulkBind(true);
1004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1006 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 }
1008 if (mAppsCustomizeContent != null) {
1009 mAppsCustomizeContent.setBulkBind(false);
1010 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001012 if (DEBUG_RESUME_TIME) {
1013 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1014 (System.currentTimeMillis() - startTimeCallbacks));
1015 }
Michael Jurka447bf842013-05-15 14:52:15 +02001016 }
Michael Jurka54554252013-08-01 12:52:23 +02001017 if (mOnResumeCallbacks.size() > 0) {
1018 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1019 mOnResumeCallbacks.get(i).run();
1020 }
1021 mOnResumeCallbacks.clear();
1022 }
Winson Chunge4e50662012-01-23 14:45:13 -08001023
1024 // Reset the pressed state of icons that were locked in the press state while activities
1025 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001026 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001027 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001028 mWaitingForResume.setStayPressed(false);
1029 }
Winson Chung82963d52013-10-09 11:20:57 -07001030
Adam Cohen06dff352012-06-01 17:17:08 -07001031 // It is possible that widgets can receive updates while launcher is not in the foreground.
1032 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1033 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1034 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001035 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001036
Winson Chung780fe592013-09-26 14:48:44 -07001037 // Process any items that were added while Launcher was away.
1038 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1039
Winson Chung5841efa2013-09-30 18:06:44 -07001040 // Update the voice search button proxy
1041 updateVoiceButtonProxyVisible(false);
1042
Adam Cohenf9426d52012-06-04 17:26:21 -07001043 // Again, as with the above scenario, it's possible that one or more of the global icons
1044 // were updated in the wrong orientation.
1045 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001046 if (DEBUG_RESUME_TIME) {
1047 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1048 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001049
1050 if (mWorkspace.getCustomContentCallbacks() != null) {
1051 // If we are resuming and the custom content is the current page, we call onShow().
1052 // It is also poassible that onShow will instead be called slightly after first layout
1053 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1054 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001055 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001056 }
1057 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001058 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001059 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001060
1061 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001062 }
1063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001066 // Ensure that items added to Launcher are queued until Launcher returns
1067 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001068 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001069
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001070 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001071 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001072 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001073 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001074
1075 // We call onHide() aggressively. The custom content callbacks should be able to
1076 // debounce excess onHide calls.
1077 if (mWorkspace.getCustomContentCallbacks() != null) {
1078 mWorkspace.getCustomContentCallbacks().onHide();
1079 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 }
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Adam Cohenbffe7452013-07-22 18:21:45 -07001082 QSBScroller mQsbScroller = new QSBScroller() {
1083 int scrollY = 0;
1084
1085 @Override
1086 public void setScrollY(int scroll) {
1087 scrollY = scroll;
1088
1089 if (mWorkspace.isOnOrMovingToCustomContent()) {
1090 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001091 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093 }
1094 };
1095
1096 public void resetQSBScroll() {
1097 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001098 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
1100
1101 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001102 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1103 // by a onResume or by scrolling otherwise.
1104 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001105
1106 // Custom content is completely hidden
1107 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001108
1109 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1110 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001111
1112 // Indicates whether the user is allowed to scroll away from the custom content.
1113 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001114 }
1115
Jorim Jaggid017f882014-01-14 17:08:48 -08001116 protected boolean hasSettings() {
1117 return false;
1118 }
1119
Adam Cohenbffe7452013-07-22 18:21:45 -07001120 public interface QSBScroller {
1121 public void setScrollY(int scrollY);
1122 }
1123
Winson Chung98ca0f72013-07-29 12:58:51 -07001124 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001125 CustomContentCallbacks callbacks, String description) {
1126 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001128 }
1129
Adam Cohenedb40762013-07-18 16:45:45 -07001130 // The custom content needs to offset its content to account for the QSB
1131 public int getTopOffsetForCustomContent() {
1132 return mWorkspace.getPaddingTop();
1133 }
1134
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001135 @Override
1136 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001137 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001138 if (mModel.isCurrentCallbacks(this)) {
1139 mModel.stopLoader();
1140 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001141 if (mAppsCustomizeContent != null) {
1142 mAppsCustomizeContent.surrender();
1143 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001144 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001145 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001147 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001148 @Override
1149 public void onWindowFocusChanged(boolean hasFocus) {
1150 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001151 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001152 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 private boolean acceptFilter() {
1155 final InputMethodManager inputManager = (InputMethodManager)
1156 getSystemService(Context.INPUT_METHOD_SERVICE);
1157 return !inputManager.isFullscreenMode();
1158 }
1159
1160 @Override
1161 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001162 final int uniChar = event.getUnicodeChar();
1163 final boolean handled = super.onKeyDown(keyCode, event);
1164 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1165 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1167 keyCode, event);
1168 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001170 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001171 // showSearchDialog()
1172 // If there are multiple keystrokes before the search dialog takes focus,
1173 // onSearchRequested() will be called for every keystroke,
1174 // but it is idempotent, so it's fine.
1175 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
Joe Onorato8a9625e2010-01-28 15:55:35 -08001179 // Eat the long press event so the keyboard doesn't come up.
1180 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1181 return true;
1182 }
1183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 return handled;
1185 }
1186
Karl Rosaen138a0412009-04-23 19:00:21 -07001187 private String getTypedText() {
1188 return mDefaultKeySsb.toString();
1189 }
1190
1191 private void clearTypedText() {
1192 mDefaultKeySsb.clear();
1193 mDefaultKeySsb.clearSpans();
1194 Selection.setSelection(mDefaultKeySsb, 0);
1195 }
1196
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001198 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1199 * State
1200 */
1201 private static State intToState(int stateOrdinal) {
1202 State state = State.WORKSPACE;
1203 final State[] stateValues = State.values();
1204 for (int i = 0; i < stateValues.length; i++) {
1205 if (stateValues[i].ordinal() == stateOrdinal) {
1206 state = stateValues[i];
1207 break;
1208 }
1209 }
1210 return state;
1211 }
1212
1213 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 * Restores the previous state, if it exists.
1215 *
1216 * @param savedState The previous state.
1217 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001218 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 private void restoreState(Bundle savedState) {
1220 if (savedState == null) {
1221 return;
1222 }
1223
Michael Jurka883f55b2010-10-21 15:47:14 -07001224 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001225 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001226 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001227 }
1228
Adam Cohen21cd0022013-10-09 18:57:02 -07001229 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1230 PagedView.INVALID_RESTORE_PAGE);
1231 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001232 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234
Winson Chung3d503fb2011-07-13 17:25:49 -07001235 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001236 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1239 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001240 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001241 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1242 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001243 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1244 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1245 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001246 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001247 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 mRestoring = true;
1249 }
1250
1251 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1252 if (renameFolder) {
1253 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001254 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 mRestoring = true;
1256 }
Winson Chunga12a2502010-12-20 14:41:35 -08001257
Winson Chung785d2eb2011-04-14 16:08:02 -07001258 // Restore the AppsCustomize tab
1259 if (mAppsCustomizeTabHost != null) {
1260 String curTab = savedState.getString("apps_customize_currentTab");
1261 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001262 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001264 mAppsCustomizeContent.loadAssociatedPages(
1265 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001266 }
1267
Winson Chung5afbf7b2011-07-25 11:53:08 -07001268 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1269 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001270 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001271 mItemIdToViewId = (HashMap<Integer, Integer>)
1272 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274
1275 /**
1276 * Finds all the views we need and configure them properly.
1277 */
1278 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001279 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001280
Craig Mautner360310b2012-10-26 15:13:08 -07001281 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001282 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001283 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1284 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001285 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001286 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001287
John Spurlock77e1f472013-09-11 10:09:51 -04001288 mLauncherView.setSystemUiVisibility(
1289 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001290 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
Winson Chung4c98d922011-05-31 16:50:48 -07001292 // Setup the drag layer
1293 mDragLayer.setup(this, dragController);
1294
Winson Chung3d503fb2011-07-13 17:25:49 -07001295 // Setup the hotseat
1296 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1297 if (mHotseat != null) {
1298 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001299 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001300 }
1301
Jorim Jaggid017f882014-01-14 17:08:48 -08001302 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001303 View widgetButton = findViewById(R.id.widget_button);
1304 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001305 @Override
1306 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001307 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001308 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001309 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001310 }
1311 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001312 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1313
1314 View wallpaperButton = findViewById(R.id.wallpaper_button);
1315 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001316 @Override
1317 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001318 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001319 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001320 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001321 }
1322 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001323 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1324
1325 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001326 if (hasSettings()) {
1327 settingsButton.setOnClickListener(new OnClickListener() {
1328 @Override
1329 public void onClick(View arg0) {
1330 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001331 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001332 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001333 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001334 });
1335 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1336 } else {
1337 settingsButton.setVisibility(View.GONE);
1338 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1339 lp.gravity = Gravity.END | Gravity.TOP;
1340 widgetButton.requestLayout();
1341 }
1342
Adam Cohendbdff6b2013-09-18 19:09:15 -07001343 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344
Winson Chung4c98d922011-05-31 16:50:48 -07001345 // Setup the workspace
1346 mWorkspace.setHapticFeedbackEnabled(false);
1347 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001348 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001349 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001350
Winson Chungf0ea4d32011-06-06 14:27:16 -07001351 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001352 mSearchDropTargetBar = (SearchDropTargetBar)
1353 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001354
Winson Chungf0ea4d32011-06-06 14:27:16 -07001355 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001356 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001357 mAppsCustomizeContent = (AppsCustomizePagedView)
1358 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1359 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001360
Winson Chung3d503fb2011-07-13 17:25:49 -07001361 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001362 dragController.setDragScoller(mWorkspace);
1363 dragController.setScrollView(mDragLayer);
1364 dragController.setMoveTarget(mWorkspace);
1365 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001366 if (mSearchDropTargetBar != null) {
1367 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001368 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001369
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001370 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001371 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001372 mWeightWatcher = new WeightWatcher(this);
1373 mWeightWatcher.setAlpha(0.5f);
1374 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001375 new FrameLayout.LayoutParams(
1376 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001377 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001378 Gravity.BOTTOM)
1379 );
Adam Cohen39a06042013-07-19 14:30:12 -07001380
1381 boolean show = shouldShowWeightWatcher();
1382 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001387 * Sets the all apps button. This method is called from {@link Hotseat}.
1388 */
1389 public void setAllAppsButton(View allAppsButton) {
1390 mAllAppsButton = allAppsButton;
1391 }
1392
1393 public View getAllAppsButton() {
1394 return mAllAppsButton;
1395 }
1396
1397 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 * Creates a view representing a shortcut.
1399 *
1400 * @param info The data structure describing the shortcut.
1401 *
1402 * @return A View inflated from R.layout.application.
1403 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001404 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001406 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
1409 /**
1410 * Creates a view representing a shortcut inflated from the specified resource.
1411 *
1412 * @param layoutResId The id of the XML layout used to create the shortcut.
1413 * @param parent The group the shortcut belongs to.
1414 * @param info The data structure describing the shortcut.
1415 *
1416 * @return A View inflated from layoutResId.
1417 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001418 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001419 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001420 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001422 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 return favorite;
1424 }
1425
1426 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 * Add a shortcut to the workspace.
1428 *
1429 * @param data The intent describing the shortcut.
1430 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001432 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001433 int cellY) {
1434 int[] cellXY = mTmpAddItemCellCoordinates;
1435 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001436 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001437
Michael Jurkad3ef3062010-11-23 16:23:58 -08001438 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001439
Adam Cohen558baaf2011-08-15 15:22:57 -07001440 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001441 if (info == null) {
1442 return;
1443 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001444 final View view = createShortcut(info);
1445
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 // First we check if we already know the exact location where we want to add this item.
1447 if (cellX >= 0 && cellY >= 0) {
1448 cellXY[0] = cellX;
1449 cellXY[1] = cellY;
1450 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001451
1452 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001453 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001454 true, null,null)) {
1455 return;
1456 }
1457 DragObject dragObject = new DragObject();
1458 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001459 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1460 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001461 return;
1462 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001464 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001466 foundCellSpan = (result != null);
1467 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001468 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001469 }
1470
1471 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001472 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001473 return;
1474 }
1475
Adam Cohendcd297f2013-06-18 13:13:40 -07001476 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477
1478 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001479 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001480 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482 }
1483
Adam Cohen2f093b62012-04-30 18:59:53 -07001484 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1485 int minHeight) {
1486 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001487 // We want to account for the extra amount of padding that we are adding to the widget
1488 // to ensure that it gets the full amount of space that it has requested
1489 int requiredWidth = minWidth + padding.left + padding.right;
1490 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001491 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001492 }
1493
Adam Cohen2f093b62012-04-30 18:59:53 -07001494 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1495 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001496 }
1497
Adam Cohen2f093b62012-04-30 18:59:53 -07001498 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1499 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001500 }
1501
Adam Cohen2f093b62012-04-30 18:59:53 -07001502 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1503 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001504 }
1505
Adam Cohen2f093b62012-04-30 18:59:53 -07001506 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1507 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001508 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001509 }
1510
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001514 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001518 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1519 if (appWidgetInfo == null) {
1520 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1521 }
Romain Guycbb89e42009-06-08 15:52:54 -07001522
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001525
Adam Cohen2f093b62012-04-30 18:59:53 -07001526 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1527 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001530 // We have saved the position to which the widget was dragged-- this really only matters
1531 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001532 int[] cellXY = mTmpAddItemCellCoordinates;
1533 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001534 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1537 cellXY[0] = mPendingAddInfo.cellX;
1538 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001539 spanXY[0] = mPendingAddInfo.spanX;
1540 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001541 foundCellSpan = true;
1542 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001543 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001545 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1546 spanXY[1], cellXY, finalSpan);
1547 spanXY[0] = finalSpan[0];
1548 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001549 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001550 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001551 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001552 }
1553
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001555 if (appWidgetId != -1) {
1556 // Deleting an app widget ID is a void call but writes to disk before returning
1557 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001558 new AsyncTask<Void, Void, Void>() {
1559 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001560 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001561 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001562 }
Michael Jurka43467462013-11-14 12:03:58 +01001563 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001564 }
Winson Chung93eef082012-03-23 15:59:27 -07001565 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001566 return;
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001569 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001570 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1571 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 launcherInfo.spanX = spanXY[0];
1573 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001574 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1575 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001576 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001579 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 if (hostView == null) {
1583 // Perform actual inflation because we're live
1584 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1585 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1586 } else {
1587 // The AppWidgetHostView has already been inflated and instantiated
1588 launcherInfo.hostView = hostView;
1589 }
Romain Guycbb89e42009-06-08 15:52:54 -07001590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001592 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001593 launcherInfo.notifyWidgetSizeChanged(this);
1594
Adam Cohendcd297f2013-06-18 13:13:40 -07001595 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001596 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001597
1598 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001600 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
Romain Guycbb89e42009-06-08 15:52:54 -07001602
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1604 @Override
1605 public void onReceive(Context context, Intent intent) {
1606 final String action = intent.getAction();
1607 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1608 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001609 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001611
Winson Chungc100e8e2011-08-09 16:02:43 -07001612 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001613 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001614 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001615 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001616 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1618 mUserPresent = true;
1619 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001620 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1621 mModel.resetLoadedState(false, true);
1622 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1623 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1624 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1625 mModel.resetLoadedState(false, true);
1626 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1627 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1628 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001629 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1630 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1631 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001632 }
1633 }
1634 };
1635
1636 @Override
1637 public void onAttachedToWindow() {
1638 super.onAttachedToWindow();
1639
1640 // Listen for broadcasts related to user-presence
1641 final IntentFilter filter = new IntentFilter();
1642 filter.addAction(Intent.ACTION_SCREEN_OFF);
1643 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001644 // For handling managed profiles
1645 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1646 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001647 if (ENABLE_DEBUG_INTENTS) {
1648 filter.addAction(DebugIntents.DELETE_DATABASE);
1649 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001652 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001653 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001654 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 mVisible = true;
1656 }
1657
Adam Cohen0b395352014-06-09 22:54:36 +00001658 /**
1659 * Sets up transparent navigation and status bars in LMP.
1660 * This method is a no-op for other platform versions.
1661 */
1662 @TargetApi(19)
1663 private void setupTransparentSystemBarsForLmp() {
1664 // TODO(sansid): use the APIs directly when compiling against L sdk.
1665 // Currently we use reflection to access the flags and the API to set the transparency
1666 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001667 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001668 try {
1669 getWindow().getAttributes().systemUiVisibility |=
1670 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1671 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1672 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1673 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1674 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1675 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1676 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1677 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1678
1679 Method setStatusBarColorMethod =
1680 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1681 Method setNavigationBarColorMethod =
1682 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1683 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1684 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1685 } catch (NoSuchFieldException e) {
1686 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1687 } catch (NoSuchMethodException ex) {
1688 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1689 } catch (IllegalAccessException e) {
1690 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1691 } catch (IllegalArgumentException e) {
1692 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1693 } catch (InvocationTargetException e) {
1694 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1695 } finally {}
1696 }
1697 }
1698
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 @Override
1700 public void onDetachedFromWindow() {
1701 super.onDetachedFromWindow();
1702 mVisible = false;
1703
Adam Cohend113e0c2010-11-11 10:48:05 -08001704 if (mAttached) {
1705 unregisterReceiver(mReceiver);
1706 mAttached = false;
1707 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 updateRunning();
1709 }
1710
1711 public void onWindowVisibilityChanged(int visibility) {
1712 mVisible = visibility == View.VISIBLE;
1713 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001714 // The following code used to be in onResume, but it turns out onResume is called when
1715 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1716 // is a more appropriate event to handle
1717 if (mVisible) {
1718 mAppsCustomizeTabHost.onWindowVisible();
1719 if (!mWorkspaceLoading) {
1720 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001721 // We want to let Launcher draw itself at least once before we force it to build
1722 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001723 // apps is nice and speedy.
1724 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001725 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001726 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001727 if (mStarted) return;
1728 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001729 // We delay the layer building a bit in order to give
1730 // other message processing a time to run. In particular
1731 // this avoids a delay in hiding the IME if it was
1732 // currently shown, because doing that may involve
1733 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001734 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001735 final ViewTreeObserver.OnDrawListener listener = this;
1736 mWorkspace.post(new Runnable() {
1737 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001738 if (mWorkspace != null &&
1739 mWorkspace.getViewTreeObserver() != null) {
1740 mWorkspace.getViewTreeObserver().
1741 removeOnDrawListener(listener);
1742 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 }
1744 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001745 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001746 }
1747 });
1748 }
1749 clearTypedText();
1750 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752
1753 private void sendAdvanceMessage(long delay) {
1754 mHandler.removeMessages(ADVANCE_MSG);
1755 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1756 mHandler.sendMessageDelayed(msg, delay);
1757 mAutoAdvanceSentTime = System.currentTimeMillis();
1758 }
1759
1760 private void updateRunning() {
1761 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1762 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1763 mAutoAdvanceRunning = autoAdvanceRunning;
1764 if (autoAdvanceRunning) {
1765 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1766 sendAdvanceMessage(delay);
1767 } else {
1768 if (!mWidgetsToAdvance.isEmpty()) {
1769 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1770 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1771 }
1772 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001773 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 }
1775 }
1776 }
1777
1778 private final Handler mHandler = new Handler() {
1779 @Override
1780 public void handleMessage(Message msg) {
1781 if (msg.what == ADVANCE_MSG) {
1782 int i = 0;
1783 for (View key: mWidgetsToAdvance.keySet()) {
1784 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1785 final int delay = mAdvanceStagger * i;
1786 if (v instanceof Advanceable) {
1787 postDelayed(new Runnable() {
1788 public void run() {
1789 ((Advanceable) v).advance();
1790 }
1791 }, delay);
1792 }
1793 i++;
1794 }
1795 sendAdvanceMessage(mAdvanceInterval);
1796 }
1797 }
1798 };
1799
1800 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001801 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1803 if (v instanceof Advanceable) {
1804 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001805 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001806 updateRunning();
1807 }
1808 }
1809
1810 void removeWidgetToAutoAdvance(View hostView) {
1811 if (mWidgetsToAdvance.containsKey(hostView)) {
1812 mWidgetsToAdvance.remove(hostView);
1813 updateRunning();
1814 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001815 }
1816
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001818 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 launcherInfo.hostView = null;
1820 }
1821
Winson Chung93eef082012-03-23 15:59:27 -07001822 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1823 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1824 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001825 }
1826
Winson Chunga6945242014-01-08 14:04:34 -08001827 public DragLayer getDragLayer() {
1828 return mDragLayer;
1829 }
1830
1831 public Workspace getWorkspace() {
1832 return mWorkspace;
1833 }
1834
1835 public Hotseat getHotseat() {
1836 return mHotseat;
1837 }
1838
Jorim Jaggid017f882014-01-14 17:08:48 -08001839 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001840 return mOverviewPanel;
1841 }
1842
1843 public SearchDropTargetBar getSearchBar() {
1844 return mSearchDropTargetBar;
1845 }
1846
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001847 public LauncherAppWidgetHost getAppWidgetHost() {
1848 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
Romain Guycbb89e42009-06-08 15:52:54 -07001850
Winson Chunga9abd0e2010-10-27 17:18:37 -07001851 public LauncherModel getModel() {
1852 return mModel;
1853 }
1854
Winson Chunga6945242014-01-08 14:04:34 -08001855 protected SharedPreferences getSharedPrefs() {
1856 return mSharedPrefs;
1857 }
1858
Bjorn Bringertc459e522013-06-07 19:36:01 +01001859 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001860 getWindow().closeAllPanels();
1861
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001862 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001863 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 @Override
1867 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001868 long startTime = 0;
1869 if (DEBUG_RESUME_TIME) {
1870 startTime = System.currentTimeMillis();
1871 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 super.onNewIntent(intent);
1873
1874 // Close the menu
1875 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001876 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001877 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001878
Adam Cohened307df2013-10-02 09:37:31 -07001879 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1880 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1881 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001882
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 if (mWorkspace == null) {
1884 // Can be cases where mWorkspace is null, this prevents a NPE
1885 return;
1886 }
1887 Folder openFolder = mWorkspace.getOpenFolder();
1888 // In all these cases, only animate if we're already on home
1889 mWorkspace.exitWidgetResizeMode();
1890 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001891 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001892 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001893 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001894
Adam Cohen6fecd412013-10-02 17:41:50 -07001895 closeFolder();
1896 exitSpringLoadedDragMode();
1897
1898 // If we are already on home, then just animate back to the workspace,
1899 // otherwise, just wait until onResume to set the state back to Workspace
1900 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001901 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 } else {
1903 mOnResumeState = State.WORKSPACE;
1904 }
1905
1906 final View v = getWindow().peekDecorView();
1907 if (v != null && v.getWindowToken() != null) {
1908 InputMethodManager imm = (InputMethodManager)getSystemService(
1909 INPUT_METHOD_SERVICE);
1910 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1911 }
1912
1913 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001914 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001915 mAppsCustomizeTabHost.reset();
1916 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001917
1918 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Adam Cohened307df2013-10-02 09:37:31 -07001920
Michael Jurka447bf842013-05-15 14:52:15 +02001921 if (DEBUG_RESUME_TIME) {
1922 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 }
1925
Adam Coppa120b8e2013-11-12 16:26:04 +00001926 /**
1927 * Override point for subclasses to prevent movement to the default screen when the home
1928 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1929 */
1930 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1931 return true;
1932 }
1933
1934 /**
1935 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1936 */
1937 protected void onHomeIntent() {
1938 // Do nothing
1939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001942 public void onRestoreInstanceState(Bundle state) {
1943 super.onRestoreInstanceState(state);
1944 for (int page: mSynchronouslyBoundPages) {
1945 mWorkspace.restoreInstanceStateForChild(page);
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
1949 @Override
1950 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001952 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1953 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001954 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001955 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956
Michael Jurka883f55b2010-10-21 15:47:14 -07001957 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001958 // We close any open folder since it will not be re-opened, and we need to make sure
1959 // this state is reflected.
1960 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mWaitingForResult) {
1964 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1967 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1969 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001971 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 if (mFolderInfo != null && mWaitingForResult) {
1975 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1976 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1977 }
Michael Jurka946ad472010-07-09 18:05:18 -07001978
Winson Chung785d2eb2011-04-14 16:08:02 -07001979 // Save the current AppsCustomize tab
1980 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001981 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1982 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001983 if (currentTabTag != null) {
1984 outState.putString("apps_customize_currentTab", currentTabTag);
1985 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001986 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1987 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001988 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001989 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 }
1991
1992 @Override
1993 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001995
Winson Chunge7a03942011-08-05 15:05:12 -07001996 // Remove all pending runnables
1997 mHandler.removeMessages(ADVANCE_MSG);
1998 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001999 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002000
Winson Chungcd2b0142011-06-08 16:02:26 -07002001 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002002 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002003
2004 // It's possible to receive onDestroy after a new Launcher activity has
2005 // been created. In this case, don't interfere with the new Launcher.
2006 if (mModel.isCurrentCallbacks(this)) {
2007 mModel.stopLoader();
2008 app.setLauncher(null);
2009 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002012 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002014 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002016 mAppWidgetHost = null;
2017
2018 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019
2020 TextKeyListener.getInstance().release();
2021
Winson Chung81b52252012-08-27 15:34:29 -07002022 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2023 // to prevent leaking Launcher activities on orientation change.
2024 if (mModel != null) {
2025 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2026 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002027
2028 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002029 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002030
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002031 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002032 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002033 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002034 mWorkspace = null;
2035 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002036
Sunny Goyale755d462014-07-22 13:48:29 -07002037 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002038 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040
Adam Cohena9cf38f2011-05-02 15:36:58 -07002041 public DragController getDragController() {
2042 return mDragController;
2043 }
2044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 @Override
2046 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002047 if (requestCode >= 0) {
2048 setWaitingForResult(true);
2049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 super.startActivityForResult(intent, requestCode);
2051 }
2052
Winson Chung70d72102011-08-12 11:24:35 -07002053 /**
2054 * Indicates that we want global search for this activity by setting the globalSearch
2055 * argument for {@link #startSearch} to true.
2056 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002058 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002060
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002061 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002062
Karl Rosaen138a0412009-04-23 19:00:21 -07002063 if (initialQuery == null) {
2064 // Use any text typed in the launcher as the initial query
2065 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 if (appSearchData == null) {
2068 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002069 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
Winson Chungadf0c182012-08-23 14:59:07 -07002071 Rect sourceBounds = new Rect();
2072 if (mSearchDropTargetBar != null) {
2073 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2074 }
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Ian Parkinson047036e2014-09-01 15:40:53 +01002076 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002077 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002078 if (clearTextImmediately) {
2079 clearTypedText();
2080 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 }
2082
Ian Parkinson047036e2014-09-01 15:40:53 +01002083 /**
2084 * Start a text search.
2085 *
2086 * @return {@code true} if the search will start immediately, so any further keypresses
2087 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2088 * to buffer keypresses.
2089 */
2090 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002091 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002092 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002093 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002094 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002095 }
2096
2097 /**
2098 * Starts the global search activity. This code is a copied from SearchManager
2099 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2104 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2105 if (globalSearchActivity == null) {
2106 Log.w(TAG, "No global search activity found.");
2107 return;
2108 }
2109 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2110 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2111 intent.setComponent(globalSearchActivity);
2112 // Make sure that we have a Bundle to put source in
2113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
2115 } else {
2116 appSearchData = new Bundle(appSearchData);
2117 }
2118 // Set source to package name of app that starts global search, if not set already.
2119 if (!appSearchData.containsKey("source")) {
2120 appSearchData.putString("source", getPackageName());
2121 }
2122 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2123 if (!TextUtils.isEmpty(initialQuery)) {
2124 intent.putExtra(SearchManager.QUERY, initialQuery);
2125 }
2126 if (selectInitialQuery) {
2127 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2128 }
2129 intent.setSourceBounds(sourceBounds);
2130 try {
2131 startActivity(intent);
2132 } catch (ActivityNotFoundException ex) {
2133 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136
Yura4f93ec62014-02-04 14:15:21 +00002137 public boolean isOnCustomContent() {
2138 return mWorkspace.isOnOrMovingToCustomContent();
2139 }
2140
Winson Chung70d72102011-08-12 11:24:35 -07002141 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002142 public boolean onPrepareOptionsMenu(Menu menu) {
2143 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002144 if (!isOnCustomContent()) {
2145 // Close any open folders
2146 closeFolder();
2147 // Stop resizing any widgets
2148 mWorkspace.exitWidgetResizeMode();
2149 if (!mWorkspace.isInOverviewMode()) {
2150 // Show the overview mode
2151 showOverviewMode(true);
2152 } else {
2153 showWorkspace(true);
2154 }
Winson Chunge0298742014-01-17 12:03:00 -08002155 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002156 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002159 @Override
2160 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002161 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002162 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002163 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002164 }
2165
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 public boolean isWorkspaceLocked() {
2167 return mWorkspaceLoading || mWaitingForResult;
2168 }
2169
Adam Cohen517a7f52014-03-01 12:12:59 -08002170 public boolean isWorkspaceLoading() {
2171 return mWorkspaceLoading;
2172 }
2173
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002174 private void setWorkspaceLoading(boolean value) {
2175 boolean isLocked = isWorkspaceLocked();
2176 mWorkspaceLoading = value;
2177 if (isLocked != isWorkspaceLocked()) {
2178 onWorkspaceLockedChanged();
2179 }
2180 }
2181
2182 private void setWaitingForResult(boolean value) {
2183 boolean isLocked = isWorkspaceLocked();
2184 mWaitingForResult = value;
2185 if (isLocked != isWorkspaceLocked()) {
2186 onWorkspaceLockedChanged();
2187 }
2188 }
2189
2190 protected void onWorkspaceLockedChanged() { }
2191
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002193 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002194 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002195 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2196 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002197 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002200
Adam Cohenad4e15c2013-10-17 16:21:35 -07002201 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2202 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2203 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2204 }
2205
2206 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2207 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2208 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002209 if (appWidgetInfo.configure != null) {
2210 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002211 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002212
Bjorn Bringert7984c942009-12-09 15:38:25 +00002213 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002214 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2215 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002218 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002219 Runnable onComplete = new Runnable() {
2220 @Override
2221 public void run() {
2222 // Exit spring loaded mode if necessary after adding the widget
2223 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2224 null);
2225 }
2226 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002228 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002229 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 }
2231 }
Romain Guycbb89e42009-06-08 15:52:54 -07002232
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002233 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002234 // Close any folders that may be open.
2235 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002236 mWorkspace.moveToCustomContentScreen(animate);
2237 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002238 /**
2239 * Process a shortcut drop.
2240 *
2241 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002242 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002243 * @param cell The cell it should be added to, optional
2244 * @param position The location on the screen where it was dropped, optional
2245 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002250 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002251 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252
2253 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.cellX = cell[0];
2255 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257
2258 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2259 createShortcutIntent.setComponent(componentName);
2260 processShortcut(createShortcutIntent);
2261 }
2262
Adam Cohenfbba09b2011-07-18 21:43:05 -07002263 /**
2264 * Process a widget drop.
2265 *
2266 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002267 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002268 * @param cell The cell it should be added to, optional
2269 * @param position The location on the screen where it was dropped, optional
2270 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002272 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002273 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002274 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002277 mPendingAddInfo.minSpanX = info.minSpanX;
2278 mPendingAddInfo.minSpanY = info.minSpanY;
2279
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = cell[0];
2282 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002284 if (span != null) {
2285 mPendingAddInfo.spanX = span[0];
2286 mPendingAddInfo.spanY = span[1];
2287 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288
Adam Cohened66b2b2012-01-23 17:28:51 -08002289 AppWidgetHostView hostView = info.boundWidget;
2290 int appWidgetId;
2291 if (hostView != null) {
2292 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002294 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 // In this case, we either need to start an activity to get permission to bind
2296 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002297 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002298 Bundle options = info.bindOptions;
2299
Sunny Goyalffe83f12014-08-14 17:39:34 -07002300 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2301 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002302 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002303 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002304 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002306 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2308 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002309 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2310 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002311 // TODO: we need to make sure that this accounts for the options bundle.
2312 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002313 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2314 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002315 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316 }
2317
Joe Onoratodeb98af2010-02-19 14:59:39 -08002318 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002319 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 }
2321
Winson Chung24ab2f12010-09-16 14:10:47 -07002322 void processWallpaper(Intent intent) {
2323 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2324 }
2325
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002328 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002329 folderInfo.title = getText(R.string.folder_name);
2330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002334 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335
2336 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 FolderIcon newFolder =
2338 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002341 // Force measure the new folder icon
2342 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2343 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002344 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 }
Romain Guycbb89e42009-06-08 15:52:54 -07002346
Joe Onorato9c1289c2009-08-17 11:03:03 -04002347 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002348 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002349 }
2350
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002351 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002352 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002353 }
2354
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002355 /**
2356 * Registers various content observers. The current implementation registers
2357 * only a favorites observer to keep track of the favorites applications.
2358 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002359 private void registerContentObservers() {
2360 ContentResolver resolver = getContentResolver();
2361 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2362 true, mWidgetObserver);
2363 }
2364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 @Override
2366 public boolean dispatchKeyEvent(KeyEvent event) {
2367 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2368 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002370 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002371 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002372 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002373 dumpState();
2374 return true;
2375 }
2376 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002377 }
2378 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2379 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002380 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 return true;
2382 }
2383 }
2384
2385 return super.dispatchKeyEvent(event);
2386 }
2387
Joe Onorato88ec0992009-11-19 13:16:06 -08002388 @Override
2389 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002390 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002391 if (mAppsCustomizeContent.getContentType() ==
2392 AppsCustomizePagedView.ContentType.Applications) {
2393 showWorkspace(true);
2394 } else {
2395 showOverviewMode(true);
2396 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002397 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002398 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002399 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002400 Folder openFolder = mWorkspace.getOpenFolder();
2401 if (openFolder.isEditingName()) {
2402 openFolder.dismissEditingName();
2403 } else {
2404 closeFolder();
2405 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002406 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002407 mWorkspace.exitWidgetResizeMode();
2408
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002409 // Back button is a no-op here, but give at least some feedback for the button press
2410 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002411 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002412 }
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002415 * Re-listen when widgets are reset.
2416 */
2417 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002418 if (mAppWidgetHost != null) {
2419 mAppWidgetHost.startListening();
2420 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002421 }
2422
2423 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 * Launches the intent referred by the clicked shortcut.
2425 *
2426 * @param v The view representing the clicked shortcut.
2427 */
2428 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002429 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2430 // view has detached (it's possible for this to happen if the view is removed mid touch).
2431 if (v.getWindowToken() == null) {
2432 return;
2433 }
2434
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002435 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002436 return;
2437 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002438
Adam Cohen1697b792013-09-17 19:08:21 -07002439 if (v instanceof Workspace) {
2440 if (mWorkspace.isInOverviewMode()) {
2441 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002442 }
2443 return;
2444 }
2445
2446 if (v instanceof CellLayout) {
2447 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002448 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002449 }
2450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002453 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002454 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002456 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002457 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002458 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002459 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002460 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002461 } else if (tag instanceof AppInfo) {
2462 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002463 } else if (tag instanceof LauncherAppWidgetInfo) {
2464 if (v instanceof PendingAppWidgetHostView) {
2465 onClickPendingWidget((PendingAppWidgetHostView) v);
2466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 }
2468 }
2469
Sunny Goyal508da152014-08-14 10:53:27 -07002470 public void onClickPagedViewIcon(View v) {
2471 startAppShortcutOrInfoActivity(v);
2472 }
2473
Michael Jurka0e260592010-06-30 17:07:39 -07002474 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002475 return false;
2476 }
2477
Michael Jurkaaf442092010-06-10 17:01:57 -07002478 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002479 * Event handler for the app widget view which has not fully restored.
2480 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002481 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2482 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002483 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002484 int widgetId = info.appWidgetId;
2485 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2486 if (appWidgetInfo != null) {
2487 mPendingAddWidgetInfo = appWidgetInfo;
2488 mPendingAddInfo.copyFrom(info);
2489 mPendingAddWidgetId = widgetId;
2490
Sunny Goyalffe83f12014-08-14 17:39:34 -07002491 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2492 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002493 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002494 } else if (info.installProgress < 0) {
2495 // The install has not been queued
2496 final String packageName = info.providerName.getPackageName();
2497 showBrokenAppInstallDialog(packageName,
2498 new DialogInterface.OnClickListener() {
2499 public void onClick(DialogInterface dialog, int id) {
2500 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2501 }
2502 });
2503 } else {
2504 // Download has started.
2505 final String packageName = info.providerName.getPackageName();
2506 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002507 }
2508 }
2509
2510 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002511 * Event handler for the search button
2512 *
2513 * @param v The view that was clicked.
2514 */
2515 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002516 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2517
Amith Yamasania135ba82011-08-09 17:42:01 -07002518 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002519 }
2520
2521 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002522 * Event handler for the voice button
2523 *
2524 * @param v The view that was clicked.
2525 */
2526 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002527 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002528
Bjorn Bringertc459e522013-06-07 19:36:01 +01002529 startVoice();
2530 }
2531
2532 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002533 try {
2534 final SearchManager searchManager =
2535 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2536 ComponentName activityName = searchManager.getGlobalSearchActivity();
2537 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002538 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002539 if (activityName != null) {
2540 intent.setPackage(activityName.getPackageName());
2541 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002542 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002543 } catch (ActivityNotFoundException e) {
2544 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002545 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002546 startActivitySafely(null, intent, "onClickVoiceButton");
2547 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002548 }
2549
2550 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002551 * Event handler for the "grid" button that appears on the home screen, which
2552 * enters all apps mode.
2553 *
2554 * @param v The view that was clicked.
2555 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002556 protected void onClickAllAppsButton(View v) {
2557 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2558 if (isAllAppsVisible()) {
2559 showWorkspace(true);
2560 } else {
2561 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2562 }
2563 }
2564
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002565 private void showBrokenAppInstallDialog(final String packageName,
2566 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyalf8763012014-09-23 16:22:10 -07002567 new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault))
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 .setTitle(R.string.abandoned_promises_title)
2569 .setMessage(R.string.abandoned_promise_explanation)
2570 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2571 .setNeutralButton(R.string.abandoned_clean_this,
2572 new DialogInterface.OnClickListener() {
2573 public void onClick(DialogInterface dialog, int id) {
2574 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2575 mWorkspace.removeAbandonedPromise(packageName, user);
2576 }
2577 })
2578 .create().show();
2579 return;
2580 }
2581
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002582 /**
2583 * Event handler for an app shortcut click.
2584 *
2585 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2586 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002587 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2589 Object tag = v.getTag();
2590 if (!(tag instanceof ShortcutInfo)) {
2591 throw new IllegalArgumentException("Input must be a Shortcut");
2592 }
2593
2594 // Open shortcut
2595 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2596 final Intent intent = shortcut.intent;
2597
2598 // Check for special shortcuts
2599 if (intent.getComponent() != null) {
2600 final String shortcutClass = intent.getComponent().getClassName();
2601
2602 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2603 MemoryDumpActivity.startDump(this);
2604 return;
2605 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2606 toggleShowWeightWatcher();
2607 return;
2608 }
2609 }
2610
Chris Wren40c5ed32014-06-24 18:24:23 -04002611 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002612 if ((v instanceof BubbleTextView)
2613 && shortcut.isPromise()
2614 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002615 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002616 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002617 new DialogInterface.OnClickListener() {
2618 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002619 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002621 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002622 return;
2623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002626 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 }
2628
Sunny Goyal508da152014-08-14 10:53:27 -07002629 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002631 final ShortcutInfo shortcut;
2632 final Intent intent;
2633 if (tag instanceof ShortcutInfo) {
2634 shortcut = (ShortcutInfo) tag;
2635 intent = shortcut.intent;
2636 int[] pos = new int[2];
2637 v.getLocationOnScreen(pos);
2638 intent.setSourceBounds(new Rect(pos[0], pos[1],
2639 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002640
Sunny Goyal508da152014-08-14 10:53:27 -07002641 } else if (tag instanceof AppInfo) {
2642 shortcut = null;
2643 intent = ((AppInfo) tag).intent;
2644 } else {
2645 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2646 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647
2648 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 mStats.recordLaunch(intent, shortcut);
2650
2651 if (success && v instanceof BubbleTextView) {
2652 mWaitingForResume = (BubbleTextView) v;
2653 mWaitingForResume.setStayPressed(true);
2654 }
2655 }
2656
2657 /**
2658 * Event handler for a folder icon click.
2659 *
2660 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2661 */
2662 protected void onClickFolderIcon(View v) {
2663 if (LOGD) Log.d(TAG, "onClickFolder");
2664 if (!(v instanceof FolderIcon)){
2665 throw new IllegalArgumentException("Input must be a FolderIcon");
2666 }
2667
2668 FolderIcon folderIcon = (FolderIcon) v;
2669 final FolderInfo info = folderIcon.getFolderInfo();
2670 Folder openFolder = mWorkspace.getFolderForTag(info);
2671
2672 // If the folder info reports that the associated folder is open, then verify that
2673 // it is actually opened. There have been a few instances where this gets out of sync.
2674 if (info.opened && openFolder == null) {
2675 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2676 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2677 info.opened = false;
2678 }
2679
2680 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2681 // Close any open folder
2682 closeFolder();
2683 // Open the requested folder
2684 openFolder(folderIcon);
2685 } else {
2686 // Find the open folder...
2687 int folderScreen;
2688 if (openFolder != null) {
2689 folderScreen = mWorkspace.getPageForView(openFolder);
2690 // .. and close it
2691 closeFolder(openFolder);
2692 if (folderScreen != mWorkspace.getCurrentPage()) {
2693 // Close any folder open on the current screen
2694 closeFolder();
2695 // Pull the folder onto this screen
2696 openFolder(folderIcon);
2697 }
2698 }
2699 }
Michael Jurka5130e402011-10-13 04:55:35 -07002700 }
2701
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002702 /**
2703 * Event handler for the (Add) Widgets button that appears after a long press
2704 * on the home screen.
2705 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002706 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002708 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2709 }
2710
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002711 /**
2712 * Event handler for the wallpaper picker button that appears after a long press
2713 * on the home screen.
2714 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002715 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2717 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2718 pickWallpaper.setComponent(getWallpaperPickerComponent());
2719 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2720 }
2721
2722 /**
2723 * Event handler for a click on the settings button that appears after a long press
2724 * on the home screen.
2725 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002726 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2728 }
2729
Michael Jurka5130e402011-10-13 04:55:35 -07002730 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002731 // Provide the same haptic feedback that the system offers for virtual keys.
2732 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002733 }
2734
Adam Cohen61f560d2013-09-30 15:58:20 -07002735 public void performHapticFeedbackOnTouchDown(View v) {
2736 // Provide the same haptic feedback that the system offers for virtual keys.
2737 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2738 }
2739
2740 public View.OnTouchListener getHapticFeedbackTouchListener() {
2741 if (mHapticFeedbackTouchListener == null) {
2742 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2743 @Override
2744 public boolean onTouch(View v, MotionEvent event) {
2745 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2746 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2747 }
2748 return false;
2749 }
2750 };
2751 }
2752 return mHapticFeedbackTouchListener;
2753 }
2754
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002755 public void onDragStarted(View view) {}
2756
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002757 /**
2758 * Called when the user stops interacting with the launcher.
2759 * This implies that the user is now on the homescreen and is not doing housekeeping.
2760 */
2761 protected void onInteractionEnd() {}
2762
2763 /**
2764 * Called when the user starts interacting with the launcher.
2765 * The possible interactions are:
2766 * - open all apps
2767 * - reorder an app shortcut, or a widget
2768 * - open the overview mode.
2769 * This is a good time to stop doing things that only make sense
2770 * when the user is on the homescreen and not doing housekeeping.
2771 */
2772 protected void onInteractionBegin() {}
2773
Kenny Guyf07af7b2014-07-31 11:39:16 +01002774 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002775 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002776 try {
2777 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2778 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2779 launcherApps.showAppDetailsForProfile(componentName, user);
2780 } catch (SecurityException e) {
2781 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2782 Log.e(TAG, "Launcher does not have permission to launch settings");
2783 } catch (ActivityNotFoundException e) {
2784 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2785 Log.e(TAG, "Unable to launch settings");
2786 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002787 }
2788
Michael Jurka1e2f4652013-07-08 18:03:46 -07002789 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002790 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2791 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002792 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002793 // System applications cannot be installed. For now, show a toast explaining that.
2794 // We may give them the option of disabling apps this way.
2795 int messageId = R.string.uninstall_system_app_text;
2796 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002797 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002798 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002799 String packageName = componentName.getPackageName();
2800 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002801 Intent intent = new Intent(
2802 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002803 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2804 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002805 if (user != null) {
2806 user.addToIntent(intent, Intent.EXTRA_USER);
2807 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002808 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002809 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002810 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002811 }
2812
Michael Jurka86a720e2012-05-09 11:23:23 -07002813 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002814 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002816 // Only launch using the new animation if the shortcut has not opted out (this is a
2817 // private contract between launcher and may be ignored in the future).
2818 boolean useLaunchAnimation = (v != null) &&
2819 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002820 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2821 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002822
Kenny Guy1317e2d2014-05-08 18:52:50 +01002823 UserHandleCompat user = null;
2824 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2825 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2826 user = userManager.getUserForSerialNumber(serialNumber);
2827 }
2828
2829 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002830 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002831 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002832 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2833 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002834 optsBundle = opts.toBundle();
2835 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002836
2837 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2838 // Could be launching some bookkeeping activity
2839 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002840 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002841 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002842 launcherApps.startActivityForProfile(intent.getComponent(), user,
2843 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002844 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002845 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 } catch (SecurityException e) {
2847 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002848 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002850 "or use the exported attribute for this activity. "
2851 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002853 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002855
Michael Jurka86a720e2012-05-09 11:23:23 -07002856 boolean startActivitySafely(View v, Intent intent, Object tag) {
2857 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002858 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2859 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2860 return false;
2861 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002862 try {
2863 success = startActivity(v, intent, tag);
2864 } catch (ActivityNotFoundException e) {
2865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2866 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2867 }
2868 return success;
2869 }
2870
Adam Cohen268c4752012-06-06 17:47:33 -07002871 /**
2872 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2873 * in the DragLayer in the exact absolute location of the original FolderIcon.
2874 */
2875 private void copyFolderIconToImage(FolderIcon fi) {
2876 final int width = fi.getMeasuredWidth();
2877 final int height = fi.getMeasuredHeight();
2878
2879 // Lazy load ImageView, Bitmap and Canvas
2880 if (mFolderIconImageView == null) {
2881 mFolderIconImageView = new ImageView(this);
2882 }
2883 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2884 mFolderIconBitmap.getHeight() != height) {
2885 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2886 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2887 }
2888
2889 DragLayer.LayoutParams lp;
2890 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2891 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2892 } else {
2893 lp = new DragLayer.LayoutParams(width, height);
2894 }
2895
Adam Cohen307fe232012-08-16 17:55:58 -07002896 // The layout from which the folder is being opened may be scaled, adjust the starting
2897 // view size by this scale factor.
2898 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002899 lp.customPosition = true;
2900 lp.x = mRectForFolderAnimation.left;
2901 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002902 lp.width = (int) (scale * width);
2903 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002904
2905 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2906 fi.draw(mFolderIconCanvas);
2907 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002908 if (fi.getFolder() != null) {
2909 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2910 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002911 }
Adam Cohen268c4752012-06-06 17:47:33 -07002912 // Just in case this image view is still in the drag layer from a previous animation,
2913 // we remove it and re-add it.
2914 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2915 mDragLayer.removeView(mFolderIconImageView);
2916 }
2917 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002918 if (fi.getFolder() != null) {
2919 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002920 }
Adam Cohen268c4752012-06-06 17:47:33 -07002921 }
2922
Adam Cohen2801caf2011-05-13 20:57:39 -07002923 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002924 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002925 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2926 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2927 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2928
Adam Cohenc51934b2011-07-26 21:07:43 -07002929 FolderInfo info = (FolderInfo) fi.getTag();
2930 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2931 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002932 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2933 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002934 }
2935
Adam Cohen268c4752012-06-06 17:47:33 -07002936 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2937 copyFolderIconToImage(fi);
2938 fi.setVisibility(View.INVISIBLE);
2939
Michael Jurka2ecf9952012-06-18 12:52:28 -07002940 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002941 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01002942 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002943 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
2944 }
2945 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07002946 oa.start();
2947 }
2948
Adam Cohen268c4752012-06-06 17:47:33 -07002949 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002950 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002951 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2952 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2953 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2954
Adam Cohen268c4752012-06-06 17:47:33 -07002955 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002956
Adam Cohen268c4752012-06-06 17:47:33 -07002957 // We remove and re-draw the FolderIcon in-case it has changed
2958 mDragLayer.removeView(mFolderIconImageView);
2959 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002960 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002961 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07002962 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002963 oa.addListener(new AnimatorListenerAdapter() {
2964 @Override
2965 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002966 if (cl != null) {
2967 cl.clearFolderLeaveBehind();
2968 // Remove the ImageView copy of the FolderIcon and make the original visible.
2969 mDragLayer.removeView(mFolderIconImageView);
2970 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002971 }
2972 }
2973 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002974 oa.start();
2975 }
2976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002978 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 * is animated relative to the specified View. If the View is null, no animation
2980 * is played.
2981 *
2982 * @param folderInfo The FolderInfo describing the folder to open.
2983 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002984 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002985 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002986 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987
Adam Cohena9cf38f2011-05-02 15:36:58 -07002988 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989
Adam Cohen4554ee12011-08-03 16:13:21 -07002990 // Just verify that the folder hasn't already been added to the DragLayer.
2991 // There was a one-off crash where the folder had a parent already.
2992 if (folder.getParent() == null) {
2993 mDragLayer.addView(folder);
2994 mDragController.addDropTarget((DropTarget) folder);
2995 } else {
2996 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2997 folder.getParent() + ").");
2998 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002999 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003000 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003001
3002 // Notify the accessibility manager that this folder "window" has appeared and occluded
3003 // the workspace items
3004 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3005 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003006 }
3007
3008 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003009 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003010 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003011 if (folder.isEditingName()) {
3012 folder.dismissEditingName();
3013 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003014 closeFolder(folder);
3015 }
3016 }
3017
3018 void closeFolder(Folder folder) {
3019 folder.getInfo().opened = false;
3020
3021 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3022 if (parent != null) {
3023 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3024 shrinkAndFadeInFolderIcon(fi);
3025 }
3026 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003027
3028 // Notify the accessibility manager that this folder "window" has disappeard and no
3029 // longer occludeds the workspace items
3030 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 }
3032
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003034 if (!isDraggingEnabled()) return false;
3035 if (isWorkspaceLocked()) return false;
3036 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037
Adam Cohen1697b792013-09-17 19:08:21 -07003038 if (v instanceof Workspace) {
3039 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003040 if (mWorkspace.enterOverviewMode()) {
3041 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3042 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3043 return true;
3044 } else {
3045 return false;
3046 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003047 } else {
3048 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003049 }
Adam Cohen1697b792013-09-17 19:08:21 -07003050 }
3051
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003052 CellLayout.CellInfo longClickCellInfo = null;
3053 View itemUnderLongClick = null;
3054 if (v.getTag() instanceof ItemInfo) {
3055 ItemInfo info = (ItemInfo) v.getTag();
3056 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3057 itemUnderLongClick = longClickCellInfo.cell;
3058 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 }
3060
Winson Chung3d503fb2011-07-13 17:25:49 -07003061 // The hotseat touch handling does not go through Workspace, and we always allow long press
3062 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003063 final boolean inHotseat = isHotseatLayout(v);
3064 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003065 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003066 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003067 // User long pressed on empty space
3068 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3069 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003070 if (mWorkspace.isInOverviewMode()) {
3071 mWorkspace.startReordering(v);
3072 } else {
3073 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003076 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3077 mHotseat.getOrderInHotseat(
3078 longClickCellInfo.cellX,
3079 longClickCellInfo.cellY));
3080 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003081 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003082 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 }
3084 }
3085 }
3086 return true;
3087 }
3088
Winson Chung3d503fb2011-07-13 17:25:49 -07003089 boolean isHotseatLayout(View layout) {
3090 return mHotseat != null && layout != null &&
3091 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3092 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003093
Winson Chung3d503fb2011-07-13 17:25:49 -07003094 /**
3095 * Returns the CellLayout of the specified container at the specified screen.
3096 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003097 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003098 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3099 if (mHotseat != null) {
3100 return mHotseat.getLayout();
3101 } else {
3102 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003103 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003104 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003105 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003106 }
3107 }
3108
Daniel Sandler843e8602010-06-07 14:59:01 -04003109 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003110 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003111 }
3112
Craig Mautner360310b2012-10-26 15:13:08 -07003113 private void setWorkspaceBackground(boolean workspace) {
3114 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003115 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003116 }
3117
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003118 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003119 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3120 int curflags = getWindow().getAttributes().flags
3121 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3122 if (wpflags != curflags) {
3123 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3124 }
Craig Mautner360310b2012-10-26 15:13:08 -07003125 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003126 }
3127
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003128 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3129 if (v instanceof LauncherTransitionable) {
3130 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3131 }
3132 }
3133
Michael Jurkabed61d22012-02-14 22:51:29 -08003134 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3135 if (v instanceof LauncherTransitionable) {
3136 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3137 }
Winson Chung70442722012-02-10 15:43:22 -08003138
3139 // Update the workspace transition step as well
3140 dispatchOnLauncherTransitionStep(v, 0f);
3141 }
3142
3143 private void dispatchOnLauncherTransitionStep(View v, float t) {
3144 if (v instanceof LauncherTransitionable) {
3145 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3146 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003147 }
3148
3149 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3150 if (v instanceof LauncherTransitionable) {
3151 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3152 }
Winson Chung70442722012-02-10 15:43:22 -08003153
3154 // Update the workspace transition step as well
3155 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003156 }
3157
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003158 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003159 * Things to test when changing the following seven functions.
3160 * - Home from workspace
3161 * - from center screen
3162 * - from other screens
3163 * - Home from all apps
3164 * - from center screen
3165 * - from other screens
3166 * - Back from all apps
3167 * - from center screen
3168 * - from other screens
3169 * - Launch app from workspace and quit
3170 * - with back
3171 * - with home
3172 * - Launch app from all apps and quit
3173 * - with back
3174 * - with home
3175 * - Go to a screen that's not the default, then all
3176 * apps, and launch and app, and go back
3177 * - with back
3178 * -with home
3179 * - On workspace, long press power and go back
3180 * - with back
3181 * - with home
3182 * - On all apps, long press power and go back
3183 * - with back
3184 * - with home
3185 * - On workspace, power off
3186 * - On all apps, power off
3187 * - Launch an app and turn off the screen while in that app
3188 * - Go back with home key
3189 * - Go back with back key TODO: make this not go to workspace
3190 * - From all apps
3191 * - From workspace
3192 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3193 * - From all apps
3194 * - From the center workspace
3195 * - From another workspace
3196 */
3197
3198 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003199 * Zoom the camera out from the workspace to reveal 'toView'.
3200 * Assumes that the view to show is anchored at either the very top or very bottom
3201 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003202 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003203 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003204 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3205 showAppsCustomizeHelper(animated, springLoaded, contentType);
3206 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003207
Winson Chungc58497e2013-09-03 17:48:37 -07003208 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3209 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003210 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003211 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003212 mStateAnimation.cancel();
3213 mStateAnimation = null;
3214 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003215
Kenny Guyd794a3f2014-09-16 15:17:58 +01003216 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003217
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003218 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003219
Winson Chungf0ea4d32011-06-06 14:27:16 -07003220 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3221 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003222 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003223 final int itemsAlphaStagger =
3224 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003225
Winson Chungf0ea4d32011-06-06 14:27:16 -07003226 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003227 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003228 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003229
Adam Cohen2854d252014-08-27 16:04:07 -07003230 final ArrayList<View> layerViews = new ArrayList<View>();
3231
Adam Cohen6c5891a2014-07-09 23:53:15 -07003232 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3233 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003234 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003235 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003236 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003237 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3238 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003239 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3240 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003241
Adam Cohena38dc902014-09-07 17:48:55 +02003242 // If for some reason our views aren't initialized, don't animate
3243 boolean initialized = getAllAppsButton() != null;
3244
3245 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003246 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003247 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3248 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003249
Adam Cohen9bfdb762014-07-21 17:44:06 -07003250 final View page = content.getPageAt(content.getCurrentPage());
3251 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003252
Adam Cohen63f1ec02014-08-12 09:23:13 -07003253 final float initialPanelAlpha = 1f;
3254
3255 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3256 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003257 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3258 } else {
3259 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3260 }
3261
Adam Cohen9bfdb762014-07-21 17:44:06 -07003262 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003263 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003264 revealView.setVisibility(View.VISIBLE);
3265 // We need to hide this view as the animation start will be posted.
3266 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003267
Adam Cohen9bfdb762014-07-21 17:44:06 -07003268 int width = revealView.getMeasuredWidth();
3269 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003270 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003271
Adam Cohen63f1ec02014-08-12 09:23:13 -07003272 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003273 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003274
Adam Cohen63f1ec02014-08-12 09:23:13 -07003275 // Get the y delta between the center of the page and the center of the all apps button
3276 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3277 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003278
Adam Cohen2854d252014-08-27 16:04:07 -07003279 float alpha = 0;
3280 float xDrift = 0;
3281 float yDrift = 0;
3282 if (material) {
3283 alpha = isWidgetTray ? 0.3f : 1f;
3284 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3285 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3286 } else {
3287 yDrift = 2 * height / 3;
3288 xDrift = 0;
3289 }
3290 final float initAlpha = alpha;
3291
Adam Cohen9bfdb762014-07-21 17:44:06 -07003292 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003293 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003294 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003295 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003296 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003297 PropertyValuesHolder panelDriftX =
3298 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003299
Adam Cohen2854d252014-08-27 16:04:07 -07003300 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3301 panelAlpha, panelDriftY, panelDriftX);
3302
Adam Cohen9bfdb762014-07-21 17:44:06 -07003303 panelAlphaAndDrift.setDuration(revealDuration);
3304 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003305
Adam Cohen9bfdb762014-07-21 17:44:06 -07003306 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003307
Adam Cohen4e243a22014-08-10 18:30:55 -07003308 if (page != null) {
3309 page.setVisibility(View.VISIBLE);
3310 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003311 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003312
Adam Cohen2854d252014-08-27 16:04:07 -07003313 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3314 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003315 pageDrift.setDuration(revealDuration);
3316 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003317 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003318 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003319
Adam Cohen63f1ec02014-08-12 09:23:13 -07003320 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003321 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003322 itemsAlpha.setDuration(revealDuration);
3323 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3324 itemsAlpha.setStartDelay(itemsAlphaStagger);
3325 mStateAnimation.play(itemsAlpha);
3326 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003327
Adam Cohen4e243a22014-08-10 18:30:55 -07003328 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3329 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003330 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003331 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003332 indicatorsAlpha.setDuration(revealDuration);
3333 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334
Adam Cohen9bfdb762014-07-21 17:44:06 -07003335 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003336 final View allApps = getAllAppsButton();
3337 int allAppsButtonSize = LauncherAppState.getInstance().
3338 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3339 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003340 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003341 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003342 reveal.setDuration(revealDuration);
3343 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003344
3345 reveal.addListener(new AnimatorListenerAdapter() {
3346 public void onAnimationStart(Animator animation) {
3347 if (!isWidgetTray) {
3348 allApps.setVisibility(View.INVISIBLE);
3349 }
3350 }
3351 public void onAnimationEnd(Animator animation) {
3352 if (!isWidgetTray) {
3353 allApps.setVisibility(View.VISIBLE);
3354 }
3355 }
3356 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003357 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003358 }
Michael Jurka1899a362011-11-03 13:50:45 -07003359
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3361 @Override
3362 public void onAnimationEnd(Animator animation) {
3363 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3364 dispatchOnLauncherTransitionEnd(toView, animated, false);
3365
3366 revealView.setVisibility(View.INVISIBLE);
3367 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003368 if (page != null) {
3369 page.setLayerType(View.LAYER_TYPE_NONE, null);
3370 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003371 content.setPageBackgroundsVisible(true);
3372
3373 // Hide the search bar
3374 if (mSearchDropTargetBar != null) {
3375 mSearchDropTargetBar.hideSearchBar(false);
3376 }
3377 }
3378
Adam Cohen9bfdb762014-07-21 17:44:06 -07003379 });
3380
Adam Cohen6c5891a2014-07-09 23:53:15 -07003381 if (workspaceAnim != null) {
3382 mStateAnimation.play(workspaceAnim);
3383 }
Adam Cohen2854d252014-08-27 16:04:07 -07003384
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003385 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3386 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003387 final AnimatorSet stateAnimation = mStateAnimation;
3388 final Runnable startAnimRunnable = new Runnable() {
3389 public void run() {
3390 // Check that mStateAnimation hasn't changed while
3391 // we waited for a layout/draw pass
3392 if (mStateAnimation != stateAnimation)
3393 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003394 dispatchOnLauncherTransitionStart(fromView, animated, false);
3395 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003396
3397 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003398 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003399 for (int i = 0; i < layerViews.size(); i++) {
3400 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003401 if (v != null) {
3402 boolean attached = true;
3403 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3404 attached = v.isAttachedToWindow();
3405 }
3406 if (attached) v.buildLayer();
3407 }
Adam Cohen2854d252014-08-27 16:04:07 -07003408 }
3409 }
3410 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003411 }
3412 };
Adam Cohen2854d252014-08-27 16:04:07 -07003413 toView.bringToFront();
3414 toView.setVisibility(View.VISIBLE);
3415 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003416 } else {
3417 toView.setTranslationX(0.0f);
3418 toView.setTranslationY(0.0f);
3419 toView.setScaleX(1.0f);
3420 toView.setScaleY(1.0f);
3421 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003422 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003423
Daniel Sandlere4f98912013-06-25 15:13:26 -04003424 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003425 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003426 if (mSearchDropTargetBar != null) {
3427 mSearchDropTargetBar.hideSearchBar(false);
3428 }
Michael Jurkaabded662011-03-04 12:06:57 -08003429 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003430 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003431 dispatchOnLauncherTransitionStart(fromView, animated, false);
3432 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003433 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003434 dispatchOnLauncherTransitionStart(toView, animated, false);
3435 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003436 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003437 }
3438
3439 /**
3440 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003441 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003442 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003443 */
Adam Cohened307df2013-10-02 09:37:31 -07003444 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003445 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003446
Michael Jurkab3e22d92011-10-31 15:58:33 -07003447 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003448 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003449 mStateAnimation.cancel();
3450 mStateAnimation = null;
3451 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003452
Kenny Guyd794a3f2014-09-16 15:17:58 +01003453 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003454 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003455
Winson Chungf0ea4d32011-06-06 14:27:16 -07003456 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003457 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003458 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003459 final int itemsAlphaStagger =
3460 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003461
Winson Chungf0ea4d32011-06-06 14:27:16 -07003462 final float scaleFactor = (float)
3463 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3464 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003465 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003466 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003467 final ArrayList<View> layerViews = new ArrayList<View>();
3468
Adam Cohened307df2013-10-02 09:37:31 -07003469 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003470 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003471 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003472 } else if (toState == Workspace.State.SPRING_LOADED ||
3473 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003474 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003475 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003476 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003477
Adam Cohena38dc902014-09-07 17:48:55 +02003478 // If for some reason our views aren't initialized, don't animate
3479 boolean initialized = getAllAppsButton() != null;
3480
3481 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003482 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003483 if (workspaceAnim != null) {
3484 mStateAnimation.play(workspaceAnim);
3485 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003486
Adam Cohen9bfdb762014-07-21 17:44:06 -07003487 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3488 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003489
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003491
3492 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3493 int count = content.getChildCount();
3494 for (int i = 0; i < count; i++) {
3495 View child = content.getChildAt(i);
3496 if (child != page) {
3497 child.setVisibility(View.INVISIBLE);
3498 }
3499 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003500 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003501
Adam Cohen2854d252014-08-27 16:04:07 -07003502 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3503 // don't perform all these no-op animations. In particularly, this was causing
3504 // the all-apps button to pop in and out.
3505 if (fromView.getVisibility() == View.VISIBLE) {
3506 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3507 final boolean isWidgetTray =
3508 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003509
Adam Cohen2854d252014-08-27 16:04:07 -07003510 if (isWidgetTray) {
3511 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3512 } else {
3513 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003514 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003515
Adam Cohen2854d252014-08-27 16:04:07 -07003516 int width = revealView.getMeasuredWidth();
3517 int height = revealView.getMeasuredHeight();
3518 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3519
3520 // Hide the real page background, and swap in the fake one
3521 revealView.setVisibility(View.VISIBLE);
3522 content.setPageBackgroundsVisible(false);
3523
3524 final View allAppsButton = getAllAppsButton();
3525 revealView.setTranslationY(0);
3526 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3527 allAppsButton, null);
3528
3529 float xDrift = 0;
3530 float yDrift = 0;
3531 if (material) {
3532 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3533 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3534 } else {
3535 yDrift = 5 * height / 4;
3536 xDrift = 0;
3537 }
3538
3539 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3540 TimeInterpolator decelerateInterpolator = material ?
3541 new LogDecelerateInterpolator(100, 0) :
3542 new LogDecelerateInterpolator(30, 0);
3543
3544 // The vertical motion of the apps panel should be delayed by one frame
3545 // from the conceal animation in order to give the right feel. We correpsondingly
3546 // shorten the duration so that the slide and conceal end at the same time.
3547 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3548 0, yDrift);
3549 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3550 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3551 panelDriftY.setInterpolator(decelerateInterpolator);
3552 mStateAnimation.play(panelDriftY);
3553
3554 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3555 0, xDrift);
3556 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3557 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3558 panelDriftX.setInterpolator(decelerateInterpolator);
3559 mStateAnimation.play(panelDriftX);
3560
3561 if (isWidgetTray || !material) {
3562 float finalAlpha = material ? 0.4f : 0f;
3563 revealView.setAlpha(1f);
3564 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3565 1f, finalAlpha);
3566 panelAlpha.setDuration(revealDuration);
3567 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3568 new AccelerateInterpolator(1.5f));
3569 mStateAnimation.play(panelAlpha);
3570 }
3571
3572 if (page != null) {
3573 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3574
3575 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3576 0, yDrift);
3577 page.setTranslationY(0);
3578 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3579 pageDrift.setInterpolator(decelerateInterpolator);
3580 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3581 mStateAnimation.play(pageDrift);
3582
3583 page.setAlpha(1f);
3584 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3585 itemsAlpha.setDuration(100);
3586 itemsAlpha.setInterpolator(decelerateInterpolator);
3587 mStateAnimation.play(itemsAlpha);
3588 }
3589
3590 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3591 pageIndicators.setAlpha(1f);
3592 ObjectAnimator indicatorsAlpha =
3593 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3594 indicatorsAlpha.setDuration(revealDuration);
3595 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3596 mStateAnimation.play(indicatorsAlpha);
3597
3598 width = revealView.getMeasuredWidth();
3599
3600 if (material) {
3601 if (!isWidgetTray) {
3602 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003603 }
Adam Cohen2854d252014-08-27 16:04:07 -07003604 int allAppsButtonSize = LauncherAppState.getInstance().
3605 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3606 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3607 Animator reveal =
3608 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3609 height / 2, revealRadius, finalRadius);
3610 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3611 reveal.setDuration(revealDuration);
3612 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003613
Adam Cohen2854d252014-08-27 16:04:07 -07003614 reveal.addListener(new AnimatorListenerAdapter() {
3615 public void onAnimationEnd(Animator animation) {
3616 revealView.setVisibility(View.INVISIBLE);
3617 if (!isWidgetTray) {
3618 allAppsButton.setVisibility(View.VISIBLE);
3619 }
3620 }
3621 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003622
Adam Cohen2854d252014-08-27 16:04:07 -07003623 mStateAnimation.play(reveal);
3624 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003625
Adam Cohen2854d252014-08-27 16:04:07 -07003626 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3627 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3628 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003629 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003630
3631 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003632 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003633 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003634 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003635 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3636 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003637 if (onCompleteRunnable != null) {
3638 onCompleteRunnable.run();
3639 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003640
3641 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003642 if (page != null) {
3643 page.setLayerType(View.LAYER_TYPE_NONE, null);
3644 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003645 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003646 // Unhide side pages
3647 int count = content.getChildCount();
3648 for (int i = 0; i < count; i++) {
3649 View child = content.getChildAt(i);
3650 child.setVisibility(View.VISIBLE);
3651 }
3652
3653 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003654 if (page != null) {
3655 page.setTranslationX(0);
3656 page.setTranslationY(0);
3657 page.setAlpha(1);
3658 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003659 content.setCurrentPage(content.getNextPage());
3660
Chet Haasebc2f0822012-10-26 17:59:53 -07003661 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003662 }
3663 });
3664
Adam Cohen2854d252014-08-27 16:04:07 -07003665 final AnimatorSet stateAnimation = mStateAnimation;
3666 final Runnable startAnimRunnable = new Runnable() {
3667 public void run() {
3668 // Check that mStateAnimation hasn't changed while
3669 // we waited for a layout/draw pass
3670 if (mStateAnimation != stateAnimation)
3671 return;
3672 dispatchOnLauncherTransitionStart(fromView, animated, false);
3673 dispatchOnLauncherTransitionStart(toView, animated, false);
3674
Kenny Guyd794a3f2014-09-16 15:17:58 +01003675 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003676 for (int i = 0; i < layerViews.size(); i++) {
3677 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003678 if (v != null) {
3679 boolean attached = true;
3680 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3681 attached = v.isAttachedToWindow();
3682 }
3683 if (attached) v.buildLayer();
3684 }
Adam Cohen2854d252014-08-27 16:04:07 -07003685 }
3686 }
3687 mStateAnimation.start();
3688 }
3689 };
3690 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003691 } else {
3692 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003693 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003694 dispatchOnLauncherTransitionStart(fromView, animated, true);
3695 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003696 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003697 dispatchOnLauncherTransitionStart(toView, animated, true);
3698 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003699 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003700 }
3701
Michael Jurkae326f182011-11-21 14:05:46 -08003702 @Override
3703 public void onTrimMemory(int level) {
3704 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003705 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003706 mAppsCustomizeTabHost.onTrimMemory();
3707 }
3708 }
3709
Adam Cohened307df2013-10-02 09:37:31 -07003710 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003711 showWorkspace(animated, null);
3712 }
3713
Adam Cohened307df2013-10-02 09:37:31 -07003714 protected void showWorkspace() {
3715 showWorkspace(true);
3716 }
3717
Adam Cohened66b2b2012-01-23 17:28:51 -08003718 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003719 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003720 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003721 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003722 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003723
Winson Chungc7d2b602012-05-16 17:02:20 -07003724 // Show the search bar (only animate if we were showing the drop target bar in spring
3725 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003726 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003727 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003728 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003729
Michael Jurkab3e22d92011-10-31 15:58:33 -07003730 // Set focus to the AppsCustomize button
3731 if (mAllAppsButton != null) {
3732 mAllAppsButton.requestFocus();
3733 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003734 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003735
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003736 // Change the state *after* we've called all the transition code
3737 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003738
Winson Chung5fb63472011-02-02 17:03:37 -08003739 // Resume the auto-advance of widgets
3740 mUserPresent = true;
3741 updateRunning();
3742
alanv1d4fde62012-10-17 13:15:47 -07003743 // Send an accessibility event to announce the context change
3744 getWindow().getDecorView()
3745 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003746
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003747 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003748 }
3749
Adam Cohened307df2013-10-02 09:37:31 -07003750 void showOverviewMode(boolean animated) {
3751 mWorkspace.setVisibility(View.VISIBLE);
3752 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3753 mState = State.WORKSPACE;
3754 onWorkspaceShown(animated);
3755 }
3756
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003757 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003758 }
3759
Winson Chung82963d52013-10-09 11:20:57 -07003760 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3761 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003762 if (mState != State.WORKSPACE) return;
3763
Winson Chung82963d52013-10-09 11:20:57 -07003764 if (resetPageToZero) {
3765 mAppsCustomizeTabHost.reset();
3766 }
Winson Chungc58497e2013-09-03 17:48:37 -07003767 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003768 mAppsCustomizeTabHost.post(new Runnable() {
3769 @Override
3770 public void run() {
3771 // We post this in-case the all apps view isn't yet constructed.
3772 mAppsCustomizeTabHost.requestFocus();
3773 }
3774 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003775
Michael Jurkab3e22d92011-10-31 15:58:33 -07003776 // Change the state *after* we've called all the transition code
3777 mState = State.APPS_CUSTOMIZE;
3778
3779 // Pause the auto-advance of widgets until we are out of AllApps
3780 mUserPresent = false;
3781 updateRunning();
3782 closeFolder();
3783
3784 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003785 getWindow().getDecorView()
3786 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003787 }
3788
Adam Cohen7777d962011-08-18 18:58:38 -07003789 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003790 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003791 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003792 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003793 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003794 }
Adam Cohen7777d962011-08-18 18:58:38 -07003795
Adam Cohenad4e15c2013-10-17 16:21:35 -07003796 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003797 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003798 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3799
Winson Chunge7a03942011-08-05 15:05:12 -07003800 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003801 @Override
3802 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003803 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003804 // Before we show workspace, hide all apps again because
3805 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3806 // clean up our state transition functions
3807 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003808 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003809 } else {
3810 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003811 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003812 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003813 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003814 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003815
Michael Jurkad3ef3062010-11-23 16:23:58 -08003816 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003817 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003818 final boolean animated = true;
3819 final boolean springLoaded = true;
3820 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003821 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003822 }
3823 // Otherwise, we are not in spring loaded mode, so don't do anything.
3824 }
3825
Michael Jurkab3e22d92011-10-31 15:58:33 -07003826 void lockAllApps() {
3827 // TODO
3828 }
3829
3830 void unlockAllApps() {
3831 // TODO
3832 }
3833
Winson Chungf0ea4d32011-06-06 14:27:16 -07003834 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003835 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003836 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003837 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003838 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003839 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003840 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003841 int duration = 0;
3842 if (mSearchDropTargetBar != null) {
3843 duration = mSearchDropTargetBar.getTransitionOutDuration();
3844 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003845 mHotseat.animate().alpha(0f).setDuration(duration);
3846 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003847 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003848 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003849 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003850 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003851 }
3852
Patrick Dubroy5f445422011-02-18 14:35:21 -08003853 /**
3854 * Add an item from all apps or customize onto the given workspace screen.
3855 * If layout is null, add to the current screen.
3856 */
3857 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003858 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003859 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003860 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003861 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003862
Winson Chungdff8ebb2011-09-08 17:25:31 -07003863 /** Maps the current orientation to an index for referencing orientation correct global icons */
3864 private int getCurrentOrientationIndexForGlobalIcons() {
3865 // default - 0, landscape - 1
3866 switch (getResources().getConfiguration().orientation) {
3867 case Configuration.ORIENTATION_LANDSCAPE:
3868 return 1;
3869 default:
3870 return 0;
3871 }
3872 }
3873
Michael Jurkaae65ee32012-04-30 11:45:54 -07003874 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003875 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003876 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003877 // Look for the toolbar icon specified in the activity meta-data
3878 Bundle metaData = packageManager.getActivityInfo(
3879 activityName, PackageManager.GET_META_DATA).metaData;
3880 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003881 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003882 if (iconResId != 0) {
3883 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003884 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003885 }
3886 }
3887 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003888 // This can happen if the activity defines an invalid drawable
3889 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3890 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003891 } catch (Resources.NotFoundException nfe) {
3892 // This can happen if the activity defines an invalid drawable
3893 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3894 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003895 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003896 return null;
3897 }
3898
3899 // if successful in getting icon, return it; otherwise, set button to use default drawable
3900 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003901 int buttonId, ComponentName activityName, int fallbackDrawableId,
3902 String toolbarResourceName) {
3903 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003904 Resources r = getResources();
3905 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3906 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003907
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003908 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003909 // If we were unable to find the icon via the meta-data, use a generic one
3910 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003911 toolbarIcon = r.getDrawable(fallbackDrawableId);
3912 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003913 if (button != null) {
3914 button.setCompoundDrawables(toolbarIcon, null, null, null);
3915 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003916 return null;
3917 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003918 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003919 if (button != null) {
3920 button.setCompoundDrawables(toolbarIcon, null, null, null);
3921 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003922 return toolbarIcon.getConstantState();
3923 }
3924 }
3925
3926 // if successful in getting icon, return it; otherwise, set button to use default drawable
3927 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003928 int buttonId, ComponentName activityName, int fallbackDrawableId,
3929 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003930 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003931 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003932
Michael Jurka19e0fc52011-07-22 18:00:21 -07003933 if (button != null) {
3934 // If we were unable to find the icon via the meta-data, use a
3935 // generic one
3936 if (toolbarIcon == null) {
3937 button.setImageResource(fallbackDrawableId);
3938 } else {
3939 button.setImageDrawable(toolbarIcon);
3940 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003941 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003942
3943 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3944
Michael Jurka0423dcf2010-10-05 14:56:18 -07003945 }
3946
Winson Chung1b884092012-06-08 17:13:02 -07003947 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003948 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003949 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003950 }
3951
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003952 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003953 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003954 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003955 }
3956
Winson Chungbb185bd2011-11-21 12:31:42 -08003957 private void invalidatePressedFocusedStates(View container, View button) {
3958 if (container instanceof HolographicLinearLayout) {
3959 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3960 layout.invalidatePressedFocusedStates();
3961 } else if (button instanceof HolographicImageView) {
3962 HolographicImageView view = (HolographicImageView) button;
3963 view.invalidatePressedFocusedStates();
3964 }
3965 }
3966
Cristina Stancu476493b2013-08-07 17:20:14 +01003967 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003968 if (mQsb == null) {
3969 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3970 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003971 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003972 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003973 }
3974
3975 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003976 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003977 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003978 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003979 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003980
Winson Chung1cad91e2011-05-25 17:41:01 -07003981 final SearchManager searchManager =
3982 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3983 ComponentName activityName = searchManager.getGlobalSearchActivity();
3984 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003985 int coi = getCurrentOrientationIndexForGlobalIcons();
3986 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003987 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3988 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3989 if (sGlobalSearchIcon[coi] == null) {
3990 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3991 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3992 TOOLBAR_ICON_METADATA_NAME);
3993 }
3994
Winson Chungc51db6a2011-10-05 11:44:49 -07003995 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3996 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003997 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003998 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003999 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004000 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004001 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4002 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004003 if (searchButton != null) searchButton.setVisibility(View.GONE);
4004 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004005 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004006 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004007 }
4008 }
4009
Cristina Stancu476493b2013-08-07 17:20:14 +01004010 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004011 final View searchButtonContainer = findViewById(R.id.search_button_container);
4012 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004013 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004014 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004015 }
4016
Cristina Stancu476493b2013-08-07 17:20:14 +01004017 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07004018 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004019 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004020
Winson Chungc51db6a2011-10-05 11:44:49 -07004021 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004022 final SearchManager searchManager =
4023 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4024 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4025
4026 ComponentName activityName = null;
4027 if (globalSearchActivity != null) {
4028 // Check if the global search activity handles voice search
4029 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4030 intent.setPackage(globalSearchActivity.getPackageName());
4031 activityName = intent.resolveActivity(getPackageManager());
4032 }
4033
4034 if (activityName == null) {
4035 // Fallback: check if an activity other than the global search activity
4036 // resolves this
4037 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4038 activityName = intent.resolveActivity(getPackageManager());
4039 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004040 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004041 int coi = getCurrentOrientationIndexForGlobalIcons();
4042 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004043 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4044 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4045 if (sVoiceSearchIcon[coi] == null) {
4046 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4047 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4048 TOOLBAR_ICON_METADATA_NAME);
4049 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004050 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004051 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004052 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004053 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004054 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004055 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004056 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004057 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004058 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004059 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004060 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004061 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004062
Cristina Stancu476493b2013-08-07 17:20:14 +01004063 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08004064 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4065 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004066 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004067 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004068 }
4069
Winson Chung5841efa2013-09-30 18:06:44 -07004070 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004071 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4072 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004073 boolean visible = !forceDisableVoiceButtonProxy &&
4074 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004075 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004076 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004077 }
4078 }
Winson Chung5841efa2013-09-30 18:06:44 -07004079
4080 /**
4081 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4082 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4083 */
4084 public void disableVoiceButtonProxy(boolean disabled) {
4085 updateVoiceButtonProxyVisible(disabled);
4086 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004087
Michael Jurkad7c28052012-04-27 15:43:36 -07004088 @Override
4089 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004090 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004091 final List<CharSequence> text = event.getText();
4092 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004093 // Populate event with a fake title based on the current state.
4094 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004095 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004096 } else {
4097 text.add(getString(R.string.all_apps_home_button_label));
4098 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004099 return result;
4100 }
4101
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004102 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004103 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004104 */
4105 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4106 @Override
4107 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004108 closeSystemDialogs();
4109 }
4110 }
4111
4112 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004113 * Receives notifications whenever the appwidgets are reset.
4114 */
4115 private class AppWidgetResetObserver extends ContentObserver {
4116 public AppWidgetResetObserver() {
4117 super(new Handler());
4118 }
4119
4120 @Override
4121 public void onChange(boolean selfChange) {
4122 onAppWidgetReset();
4123 }
4124 }
4125
4126 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004127 * If the activity is currently paused, signal that we need to run the passed Runnable
4128 * in onResume.
4129 *
4130 * This needs to be called from incoming places where resources might have been loaded
4131 * while we are paused. That is becaues the Configuration might be wrong
4132 * when we're not running, and if it comes back to what it was when we
4133 * were paused, we are not restarted.
4134 *
4135 * Implementation of the method from LauncherModel.Callbacks.
4136 *
4137 * @return true if we are currently paused. The caller might be able to
4138 * skip some work in that case since we will come back again.
4139 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004140 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004141 if (mPaused) {
4142 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004143 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004144 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004145 }
4146 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004147 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004148 return true;
4149 } else {
4150 return false;
4151 }
4152 }
4153
Michael Jurkac402cd92013-05-20 15:49:32 +02004154 private boolean waitUntilResume(Runnable run) {
4155 return waitUntilResume(run, false);
4156 }
4157
Michael Jurka1e2f4652013-07-08 18:03:46 -07004158 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004159 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004160 }
4161
Michael Jurka7607c2f2013-04-03 14:33:19 -07004162 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004163 * If the activity is currently paused, signal that we need to re-run the loader
4164 * in onResume.
4165 *
4166 * This needs to be called from incoming places where resources might have been loaded
4167 * while we are paused. That is becaues the Configuration might be wrong
4168 * when we're not running, and if it comes back to what it was when we
4169 * were paused, we are not restarted.
4170 *
4171 * Implementation of the method from LauncherModel.Callbacks.
4172 *
4173 * @return true if we are currently paused. The caller might be able to
4174 * skip some work in that case since we will come back again.
4175 */
4176 public boolean setLoadOnResume() {
4177 if (mPaused) {
4178 Log.i(TAG, "setLoadOnResume");
4179 mOnResumeNeedsLoad = true;
4180 return true;
4181 } else {
4182 return false;
4183 }
4184 }
4185
4186 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004188 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004189 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004190 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004191 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004192 } else {
4193 return SCREEN_COUNT / 2;
4194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004195 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004196
Joe Onorato9c1289c2009-08-17 11:03:03 -04004197 /**
4198 * Refreshes the shortcuts shown on the workspace.
4199 *
4200 * Implementation of the method from LauncherModel.Callbacks.
4201 */
4202 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004203 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004204
Michael Jurka7607c2f2013-04-03 14:33:19 -07004205 // If we're starting binding all over again, clear any bind calls we'd postponed in
4206 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4207 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004208 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004209
Winson Chungd64d1762013-08-20 14:37:16 -07004210 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004211 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004212 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004213
Michael Jurka05bf6442011-11-30 20:28:53 -08004214 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004215 if (mHotseat != null) {
4216 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004217 }
4218 }
4219
Adam Cohendcd297f2013-06-18 13:13:40 -07004220 @Override
4221 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004222 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004223
Adam Cohen5084cba2013-09-03 12:01:16 -07004224 // If there are no screens, we need to have an empty screen
4225 if (orderedScreenIds.size() == 0) {
4226 mWorkspace.addExtraEmptyScreen();
4227 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004228
4229 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004230 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004231 if (hasCustomContentToLeft()) {
4232 mWorkspace.createCustomContentContainer();
4233 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004234 }
Winson Chung64359a52013-07-08 17:17:08 -07004235 }
4236
4237 @Override
4238 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004239 // Log to disk
4240 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4241 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4242 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004243 int count = orderedScreenIds.size();
4244 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004245 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004246 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004247 }
4248
Adam Cohen39a06042013-07-19 14:30:12 -07004249 private boolean shouldShowWeightWatcher() {
4250 String spKey = LauncherAppState.getSharedPreferencesKey();
4251 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004252 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004253
4254 return show;
4255 }
4256
4257 private void toggleShowWeightWatcher() {
4258 String spKey = LauncherAppState.getSharedPreferencesKey();
4259 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4260 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4261
4262 show = !show;
4263
4264 SharedPreferences.Editor editor = sp.edit();
4265 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4266 editor.commit();
4267
4268 if (mWeightWatcher != null) {
4269 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4270 }
4271 }
4272
Winson Chungd64d1762013-08-20 14:37:16 -07004273 public void bindAppsAdded(final ArrayList<Long> newScreens,
4274 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004275 final ArrayList<ItemInfo> addAnimated,
4276 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004277 Runnable r = new Runnable() {
4278 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004279 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004280 }
4281 };
4282 if (waitUntilResume(r)) {
4283 return;
4284 }
4285
Winson Chungd64d1762013-08-20 14:37:16 -07004286 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004287 if (newScreens != null) {
4288 bindAddScreens(newScreens);
4289 }
Winson Chungd64d1762013-08-20 14:37:16 -07004290
4291 // We add the items without animation on non-visible pages, and with
4292 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004293 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004294 bindItems(addNotAnimated, 0,
4295 addNotAnimated.size(), false);
4296 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004297 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004298 bindItems(addAnimated, 0,
4299 addAnimated.size(), true);
4300 }
Winson Chungc58497e2013-09-03 17:48:37 -07004301
Winson Chung87412982013-10-03 18:34:14 -07004302 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004303 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004304
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004305 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004306 addedApps != null && mAppsCustomizeContent != null) {
4307 mAppsCustomizeContent.addApps(addedApps);
4308 }
Winson Chungd64d1762013-08-20 14:37:16 -07004309 }
4310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004311 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004312 * Bind the items start-end from the list.
4313 *
4314 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004315 */
Winson Chung64359a52013-07-08 17:17:08 -07004316 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4317 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004318 Runnable r = new Runnable() {
4319 public void run() {
4320 bindItems(shortcuts, start, end, forceAnimateIcons);
4321 }
4322 };
4323 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004324 return;
4325 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004326
Winson Chungf0c6ae02012-03-21 16:10:31 -07004327 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004328 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4329 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004330 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004331 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004332 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004333 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004334 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004335
4336 // Short circuit if we are loading dock items for a configuration which has no dock
4337 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4338 mHotseat == null) {
4339 continue;
4340 }
4341
Joe Onorato9c1289c2009-08-17 11:03:03 -04004342 switch (item.itemType) {
4343 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4344 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004345 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004346 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004347
Winson Chung64359a52013-07-08 17:17:08 -07004348 /*
4349 * TODO: FIX collision case
4350 */
Winson Chungce376632013-07-11 16:12:41 -07004351 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4352 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4353 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004354 View v = cl.getChildAt(item.cellX, item.cellY);
4355 Object tag = v.getTag();
4356 String desc = "Collision while binding workspace item: " + item
4357 + ". Collides with " + tag;
4358 if (LauncherAppState.isDogfoodBuild()) {
4359 throw (new RuntimeException(desc));
4360 } else {
4361 Log.d(TAG, desc);
4362 }
Winson Chungce376632013-07-11 16:12:41 -07004363 }
Winson Chung64359a52013-07-08 17:17:08 -07004364 }
4365
Adam Cohendcd297f2013-06-18 13:13:40 -07004366 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4367 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004368 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004369 // Animate all the applications up now
4370 shortcut.setAlpha(0f);
4371 shortcut.setScaleX(0f);
4372 shortcut.setScaleY(0f);
4373 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004374 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004375 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004376 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004377 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004378 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004379 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004380 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004381 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4382 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004383 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004384 default:
4385 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004387 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004388
Winson Chung997a9232013-07-24 15:33:46 -07004389 if (animateIcons) {
4390 // Animate to the correct page
4391 if (newShortcutsScreenId > -1) {
4392 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004393 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004394 final Runnable startBounceAnimRunnable = new Runnable() {
4395 public void run() {
4396 anim.playTogether(bounceAnims);
4397 anim.start();
4398 }
4399 };
Winson Chung997a9232013-07-24 15:33:46 -07004400 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004401 // We post the animation slightly delayed to prevent slowdowns
4402 // when we are loading right after we return to launcher.
4403 mWorkspace.postDelayed(new Runnable() {
4404 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004405 if (mWorkspace != null) {
4406 mWorkspace.snapToPage(newScreenIndex);
4407 mWorkspace.postDelayed(startBounceAnimRunnable,
4408 NEW_APPS_ANIMATION_DELAY);
4409 }
Winson Chung94d67682013-09-25 16:29:40 -07004410 }
4411 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004412 } else {
4413 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004414 }
4415 }
Winson Chung64359a52013-07-08 17:17:08 -07004416 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004417 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004418 }
4419
Joe Onorato9c1289c2009-08-17 11:03:03 -04004420 /**
4421 * Implementation of the method from LauncherModel.Callbacks.
4422 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004423 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004424 Runnable r = new Runnable() {
4425 public void run() {
4426 bindFolders(folders);
4427 }
4428 };
4429 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004430 return;
4431 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004432 sFolders.clear();
4433 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004434 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004435
4436 /**
4437 * Add the views for a widget to the workspace.
4438 *
4439 * Implementation of the method from LauncherModel.Callbacks.
4440 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004441 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004442 Runnable r = new Runnable() {
4443 public void run() {
4444 bindAppWidget(item);
4445 }
4446 };
4447 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004448 return;
4449 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004450
Daniel Sandler843e8602010-06-07 14:59:01 -04004451 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4452 if (DEBUG_WIDGETS) {
4453 Log.d(TAG, "bindAppWidget: " + item);
4454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004455 final Workspace workspace = mWorkspace;
4456
Sunny Goyalff572272014-07-23 13:58:07 -07004457 AppWidgetProviderInfo appWidgetInfo;
4458 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4459 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4460
4461 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4462 if (appWidgetInfo == null) {
4463 if (DEBUG_WIDGETS) {
4464 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4465 + " belongs to component " + item.providerName
4466 + ", as the povider is null");
4467 }
4468 LauncherModel.deleteItemFromDatabase(this, item);
4469 return;
4470 }
4471 // Note: This assumes that the id remap broadcast is received before this step.
4472 // If that is not the case, the id remap will be ignored and user may see the
4473 // click to setup view.
4474 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4475 pendingInfo.spanX = item.spanX;
4476 pendingInfo.spanY = item.spanY;
4477 pendingInfo.minSpanX = item.minSpanX;
4478 pendingInfo.minSpanY = item.minSpanY;
4479 Bundle options =
4480 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4481
Sunny Goyalff572272014-07-23 13:58:07 -07004482 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004483 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4484 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004485
4486 // TODO consider showing a permission dialog when the widget is clicked.
4487 if (!success) {
4488 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4489 if (DEBUG_WIDGETS) {
4490 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4491 + " belongs to component " + item.providerName
4492 + ", as the launcher is unable to bing a new widget id");
4493 }
4494 LauncherModel.deleteItemFromDatabase(this, item);
4495 return;
4496 }
4497
4498 item.appWidgetId = newWidgetId;
4499
4500 // If the widget has a configure activity, it is still needs to set it up, otherwise
4501 // the widget is ready to go.
4502 item.restoreStatus = (appWidgetInfo.configure == null)
4503 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4504 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4505
4506 LauncherModel.updateItemInDatabase(this, item);
4507 }
4508
Sunny Goyal651077b2014-06-30 14:15:31 -07004509 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4510 final int appWidgetId = item.appWidgetId;
4511 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4512 if (DEBUG_WIDGETS) {
4513 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4514 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004515
Sunny Goyal651077b2014-06-30 14:15:31 -07004516 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4517 } else {
4518 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004519 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4520 view.updateIcon(mIconCache);
4521 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004522 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004523 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004524 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525
Joe Onorato9c1289c2009-08-17 11:03:03 -04004526 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004527 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004528
Adam Cohendcd297f2013-06-18 13:13:40 -07004529 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004530 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004531 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4532
Joe Onorato9c1289c2009-08-17 11:03:03 -04004533 workspace.requestLayout();
4534
Daniel Sandler843e8602010-06-07 14:59:01 -04004535 if (DEBUG_WIDGETS) {
4536 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4537 + (SystemClock.uptimeMillis()-start) + "ms");
4538 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004539 }
4540
Sunny Goyalff572272014-07-23 13:58:07 -07004541 /**
4542 * Restores a pending widget.
4543 *
4544 * @param appWidgetId The app widget id
4545 * @param cellInfo The position on screen where to create the widget.
4546 */
4547 private void completeRestoreAppWidget(final int appWidgetId) {
4548 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4549 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4550 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4551 return;
4552 }
4553
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004554 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004555 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4556
4557 mWorkspace.reinflateWidgetsIfNecessary();
4558 LauncherModel.updateItemInDatabase(this, info);
4559 }
4560
Adam Cohen1462de32012-07-24 22:34:36 -07004561 public void onPageBoundSynchronously(int page) {
4562 mSynchronouslyBoundPages.add(page);
4563 }
4564
Joe Onorato9c1289c2009-08-17 11:03:03 -04004565 /**
4566 * Callback saying that there aren't any more items to bind.
4567 *
4568 * Implementation of the method from LauncherModel.Callbacks.
4569 */
Adam Cohene25af792013-06-06 23:08:25 -07004570 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004571 Runnable r = new Runnable() {
4572 public void run() {
4573 finishBindingItems(upgradePath);
4574 }
4575 };
4576 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004577 return;
4578 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004579 if (mSavedState != null) {
4580 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004581 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004583 mSavedState = null;
4584 }
4585
Adam Cohen1462de32012-07-24 22:34:36 -07004586 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004587
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004588 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004589 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004590
4591 // If we received the result of any pending adds while the loader was running (e.g. the
4592 // widget configuration forced an orientation change), process them now.
4593 if (sPendingAddItem != null) {
4594 final long screenId = completeAdd(sPendingAddItem);
4595
4596 // TODO: this moves the user to the page where the pending item was added. Ideally,
4597 // the screen would be guaranteed to exist after bind, and the page would be set through
4598 // the workspace restore process.
4599 mWorkspace.post(new Runnable() {
4600 @Override
4601 public void run() {
4602 mWorkspace.snapToScreenId(screenId);
4603 }
4604 });
4605 sPendingAddItem = null;
4606 }
4607
Adam Cohene25af792013-06-06 23:08:25 -07004608 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004609 mWorkspace.getUniqueComponents(true, null);
4610 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004611 }
Sunny Goyale755d462014-07-22 13:48:29 -07004612 PackageInstallerCompat.getInstance(this).onFinishBind();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004613 }
4614
Adam Cohen3ed316a2014-07-23 18:21:20 -07004615 private void sendLoadingCompleteBroadcastIfNecessary() {
4616 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4617 String permission =
4618 getResources().getString(R.string.receive_first_load_broadcast_permission);
4619 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4620 sendBroadcast(intent, permission);
4621 SharedPreferences.Editor editor = mSharedPrefs.edit();
4622 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4623 editor.apply();
4624 }
4625 }
4626
Winson Chunga0b7e862013-09-05 16:03:15 -07004627 public boolean isAllAppsButtonRank(int rank) {
4628 if (mHotseat != null) {
4629 return mHotseat.isAllAppsButtonRank(rank);
4630 }
4631 return false;
4632 }
4633
Winson Chunga2413752012-04-03 14:22:34 -07004634 private boolean canRunNewAppsAnimation() {
4635 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4636 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4637 }
4638
Winson Chungc9168342013-06-26 14:54:55 -07004639 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4640 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4641 PropertyValuesHolder.ofFloat("alpha", 1f),
4642 PropertyValuesHolder.ofFloat("scaleX", 1f),
4643 PropertyValuesHolder.ofFloat("scaleY", 1f));
4644 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4645 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4646 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4647 return bounceAnim;
4648 }
4649
Adam Cohen27772732013-11-11 14:00:56 +00004650 public boolean useVerticalBarLayout() {
4651 return LauncherAppState.getInstance().getDynamicGrid().
4652 getDeviceProfile().isVerticalBarLayout();
4653 }
4654
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004655 protected Rect getSearchBarBounds() {
4656 return LauncherAppState.getInstance().getDynamicGrid().
4657 getDeviceProfile().getSearchBarBounds();
4658 }
4659
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004660 @Override
4661 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004662 boolean searchVisible = updateGlobalSearchIcon();
4663 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004664 if (mSearchDropTargetBar != null) {
4665 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4666 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004667 }
4668
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004669 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004670 * Add the icons for all apps.
4671 *
4672 * Implementation of the method from LauncherModel.Callbacks.
4673 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004674 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004675 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004676 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4677 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4678 getHotseat().addAllAppsFolder(mIconCache, apps,
4679 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4680 }
4681 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004682 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004683 if (mAppsCustomizeContent != null) {
4684 mAppsCustomizeContent.onPackagesUpdated(
4685 LauncherModel.getSortedWidgetsAndShortcuts(this));
4686 }
Winson Chungc58497e2013-09-03 17:48:37 -07004687 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004688 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004689 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004690 mAppsCustomizeContent.onPackagesUpdated(
4691 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004692 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004693 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004694 }
4695
4696 /**
4697 * A package was updated.
4698 *
4699 * Implementation of the method from LauncherModel.Callbacks.
4700 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004701 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004702 Runnable r = new Runnable() {
4703 public void run() {
4704 bindAppsUpdated(apps);
4705 }
4706 };
4707 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004708 return;
4709 }
4710
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004711 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004712 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004713 }
Winson Chungc58497e2013-09-03 17:48:37 -07004714
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004715 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004716 mAppsCustomizeContent != null) {
4717 mAppsCustomizeContent.updateApps(apps);
4718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004719 }
4720
4721 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004722 * Update the state of a package, typically related to install state.
4723 *
4724 * Implementation of the method from LauncherModel.Callbacks.
4725 */
Sunny Goyale755d462014-07-22 13:48:29 -07004726 @Override
4727 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004728 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004729 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004730 }
4731 }
4732
4733 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004734 * Update the label and icon of all the icons in a package
4735 *
4736 * Implementation of the method from LauncherModel.Callbacks.
4737 */
4738 @Override
4739 public void updatePackageBadge(String packageName) {
4740 if (mWorkspace != null) {
4741 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4742 }
4743 }
4744
4745 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004746 * A package was uninstalled. We take both the super set of packageNames
4747 * in addition to specific applications to remove, the reason being that
4748 * this can be called when a package is updated as well. In that scenario,
4749 * we only remove specific components from the workspace, where as
4750 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004751 *
4752 * Implementation of the method from LauncherModel.Callbacks.
4753 */
Winson Chung83892cc2013-05-01 16:53:33 -07004754 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004755 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004756 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004757 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004758 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004759 }
Winson Chungd64d1762013-08-20 14:37:16 -07004760 };
4761 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004762 return;
4763 }
4764
Winson Chungdf95eb12013-10-16 14:57:07 -07004765 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004766 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004767 }
4768 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004769 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004770 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004771
Winson Chunga1820962011-10-03 16:31:06 -07004772 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004773 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004774
Winson Chungdf95eb12013-10-16 14:57:07 -07004775 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004776 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004777 mAppsCustomizeContent != null) {
4778 mAppsCustomizeContent.removeApps(appInfos);
4779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004780 }
Joe Onoratobe386092009-11-17 17:32:16 -08004781
4782 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004783 * A number of packages were updated.
4784 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004785 private ArrayList<Object> mWidgetsAndShortcuts;
4786 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004787 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004788 bindPackagesUpdated(mWidgetsAndShortcuts);
4789 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004790 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004791 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004792 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4793 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4794 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004795 return;
4796 }
4797
Winson Chung64359a52013-07-08 17:17:08 -07004798 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004799 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004800 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004801 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004802 }
4803
Winson Chung400438b2011-01-16 17:53:48 -08004804 private int mapConfigurationOriActivityInfoOri(int configOri) {
4805 final Display d = getWindowManager().getDefaultDisplay();
4806 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4807 switch (d.getRotation()) {
4808 case Surface.ROTATION_0:
4809 case Surface.ROTATION_180:
4810 // We are currently in the same basic orientation as the natural orientation
4811 naturalOri = configOri;
4812 break;
4813 case Surface.ROTATION_90:
4814 case Surface.ROTATION_270:
4815 // We are currently in the other basic orientation to the natural orientation
4816 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4817 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4818 break;
4819 }
4820
4821 int[] oriMap = {
4822 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4823 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4824 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4825 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4826 };
4827 // Since the map starts at portrait, we need to offset if this device's natural orientation
4828 // is landscape.
4829 int indexOffset = 0;
4830 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4831 indexOffset = 1;
4832 }
4833 return oriMap[(d.getRotation() + indexOffset) % 4];
4834 }
Adam Cohen446e9402011-09-15 18:21:21 -07004835
Winson Chung4b919f82012-05-01 10:44:08 -07004836 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004837 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004838 getResources().getBoolean(R.bool.allow_rotation);
4839 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004840 }
Winson Chung4b919f82012-05-01 10:44:08 -07004841 public void lockScreenOrientation() {
4842 if (isRotationEnabled()) {
4843 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4844 .getConfiguration().orientation));
4845 }
4846 }
4847 public void unlockScreenOrientation(boolean immediate) {
4848 if (isRotationEnabled()) {
4849 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004850 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004851 } else {
4852 mHandler.postDelayed(new Runnable() {
4853 public void run() {
4854 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4855 }
4856 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004857 }
Winson Chung4b919f82012-05-01 10:44:08 -07004858 }
Winson Chung400438b2011-01-16 17:53:48 -08004859 }
4860
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004861 /**
4862 * Called when the SearchBar hint should be changed.
4863 *
4864 * @param hint the hint to be displayed in the search bar.
4865 */
4866 protected void onSearchBarHintChanged(String hint) {
Sunny Goyal424418b2014-08-22 16:09:37 -07004867
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004868 }
4869
Winson Chunge43a1e72014-01-15 10:33:02 -08004870 protected boolean isLauncherPreinstalled() {
4871 PackageManager pm = getPackageManager();
4872 try {
4873 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4874 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4875 return true;
4876 } else {
4877 return false;
4878 }
4879 } catch (NameNotFoundException e) {
4880 e.printStackTrace();
4881 return false;
4882 }
4883 }
4884
Adam Cohenea90f832014-05-21 15:03:34 -07004885 /**
4886 * This method indicates whether or not we should suggest default wallpaper dimensions
4887 * when our wallpaper cropper was not yet used to set a wallpaper.
4888 */
4889 protected boolean overrideWallpaperDimensions() {
4890 return true;
4891 }
4892
Adam Cohen5eed5d82014-05-19 13:12:13 -07004893 protected boolean shouldClingFocusHotseatApp() {
4894 return false;
4895 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004896 protected String getFirstRunClingSearchBarHint() {
4897 return "";
4898 }
4899 protected String getFirstRunCustomContentHint() {
4900 return "";
4901 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004902 protected int getFirstRunFocusedHotseatAppDrawableId() {
4903 return -1;
4904 }
4905 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4906 return null;
4907 }
4908 protected int getFirstRunFocusedHotseatAppRank() {
4909 return -1;
4910 }
4911 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4912 return "";
4913 }
4914 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4915 return "";
4916 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004917
Adam Cohen432609a2014-03-13 17:03:22 -07004918 /**
4919 * To be overridden by subclasses to indicate that there is an activity to launch
4920 * before showing the standard launcher experience.
4921 */
4922 protected boolean hasFirstRunActivity() {
4923 return false;
4924 }
4925
4926 /**
4927 * To be overridden by subclasses to launch any first run activity
4928 */
4929 protected Intent getFirstRunActivity() {
4930 return null;
4931 }
4932
Winson Chunga6945242014-01-08 14:04:34 -08004933 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004934 return !ActivityManager.isRunningInTestHarness() &&
4935 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004936 }
4937
Adam Cohen4a9423d2014-03-28 14:22:31 -07004938 protected boolean hasRunFirstRunActivity() {
4939 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4940 }
4941
Adam Cohen432609a2014-03-13 17:03:22 -07004942 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004943 if (shouldRunFirstRunActivity() &&
4944 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004945 Intent firstRunIntent = getFirstRunActivity();
4946 if (firstRunIntent != null) {
4947 startActivity(firstRunIntent);
4948 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004949 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004950 }
4951 }
Adam Cohen432609a2014-03-13 17:03:22 -07004952 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004953 }
4954
4955 private void markFirstRunActivityShown() {
4956 SharedPreferences.Editor editor = mSharedPrefs.edit();
4957 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4958 editor.apply();
4959 }
4960
Adam Cohen432609a2014-03-13 17:03:22 -07004961 /**
4962 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4963 * screen that must be displayed and dismissed.
4964 */
4965 protected boolean hasDismissableIntroScreen() {
4966 return false;
4967 }
4968
4969 /**
4970 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4971 */
4972 protected View getIntroScreen() {
4973 return null;
4974 }
4975
4976 /**
4977 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4978 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4979 */
4980 private boolean shouldShowIntroScreen() {
4981 return hasDismissableIntroScreen() &&
4982 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4983 }
4984
4985 protected void showIntroScreen() {
4986 View introScreen = getIntroScreen();
4987 changeWallpaperVisiblity(false);
4988 if (introScreen != null) {
4989 mDragLayer.showOverlayView(introScreen);
4990 }
4991 }
4992
4993 public void dismissIntroScreen() {
4994 markIntroScreenDismissed();
4995 if (showFirstRunActivity()) {
4996 // We delay hiding the intro view until the first run activity is showing. This
4997 // avoids a blip.
4998 mWorkspace.postDelayed(new Runnable() {
4999 @Override
5000 public void run() {
5001 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005002 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005003 }
5004 }, ACTIVITY_START_DELAY);
5005 } else {
5006 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005007 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005008 }
5009 changeWallpaperVisiblity(true);
5010 }
5011
5012 private void markIntroScreenDismissed() {
5013 SharedPreferences.Editor editor = mSharedPrefs.edit();
5014 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5015 editor.apply();
5016 }
5017
Sunny Goyal88d60f12014-09-08 03:47:29 -07005018 private void showFirstRunClings() {
5019 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5020 // on the device, then we always show the first run cling experience (or if there is no
5021 // launcher2). Otherwise, we prompt the user upon started for migration
5022 LauncherClings launcherClings = new LauncherClings(this);
5023 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5024 if (mModel.canMigrateFromOldLauncherDb(this)) {
5025 launcherClings.showMigrationCling();
5026 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005027 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005028 }
5029 }
5030 }
5031
Winson Chunga6945242014-01-08 14:04:34 -08005032 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005033 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5034 if (mHotseat != null) mHotseat.setAlpha(1f);
5035 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5036 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005037 }
5038
5039 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005040 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5041 if (mHotseat != null) mHotseat.setAlpha(0f);
5042 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5043 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005044 }
5045
Mathew Inwood72fbec12013-11-19 15:45:07 +00005046 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005047 // Called from search suggestion, not supported in other profiles.
5048 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5049 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5050 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5051 myUser);
5052 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005053 return null;
5054 }
Kenny Guyed131872014-04-30 03:02:21 +01005055 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005056 }
5057
5058 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5059 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005060 // Called from search suggestion, not supported in other profiles.
5061 return createShortcutDragInfo(shortcutIntent, caption, icon,
5062 UserHandleCompat.myUserHandle());
5063 }
5064
5065 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5066 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005067 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005068 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005069 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005070 }
5071
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005072 protected void moveWorkspaceToDefaultScreen() {
5073 mWorkspace.moveToDefaultScreen(false);
5074 }
5075
Mathew Inwood72fbec12013-11-19 15:45:07 +00005076 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5077 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005078 mWorkspace.onExternalDragStartedWithItem(dragView);
5079 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005080 }
5081
Anjali Koppalf5d29132014-02-28 13:33:27 -08005082 @Override
5083 public void onPageSwitch(View newPage, int newPageIndex) {
5084 }
5085
Winson Chung80baf5a2010-08-09 16:03:15 -07005086 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005087 * Prints out out state for debugging.
5088 */
5089 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005090 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005091 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005092 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5093 Log.d(TAG, "mRestoring=" + mRestoring);
5094 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5095 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005096 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005097 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005098
Winson Chung785d2eb2011-04-14 16:08:02 -07005099 if (mAppsCustomizeContent != null) {
5100 mAppsCustomizeContent.dumpState();
5101 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005102 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005103 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005104
5105 @Override
5106 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5107 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005108 synchronized (sDumpLogs) {
5109 writer.println(" ");
5110 writer.println("Debug logs: ");
5111 for (int i = 0; i < sDumpLogs.size(); i++) {
5112 writer.println(" " + sDumpLogs.get(i));
5113 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005114 }
5115 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005116
5117 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005118 if (DEBUG_DUMP_LOG) {
5119 synchronized (sDumpLogs) {
5120 Log.d(TAG, "");
5121 Log.d(TAG, "*********************");
5122 Log.d(TAG, "Launcher debug logs: ");
5123 for (int i = 0; i < sDumpLogs.size(); i++) {
5124 Log.d(TAG, " " + sDumpLogs.get(i));
5125 }
5126 Log.d(TAG, "*********************");
5127 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005129 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005130 }
5131
5132 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005133 addDumpLog(tag, log, null, debugLog);
5134 }
5135
5136 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005137 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005138 if (e != null) {
5139 Log.d(tag, log, e);
5140 } else {
5141 Log.d(tag, log);
5142 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005143 }
Winson Chungede41292013-09-19 16:27:36 -07005144 if (DEBUG_DUMP_LOG) {
5145 sDateStamp.setTime(System.currentTimeMillis());
5146 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005147 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5148 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005149 }
Winson Chungede41292013-09-19 16:27:36 -07005150 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005151 }
5152
Winson Chungede41292013-09-19 16:27:36 -07005153 public void dumpLogsToLocalData() {
5154 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005155 new AsyncTask<Void, Void, Void>() {
5156 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005157 boolean success = false;
5158 sDateStamp.setTime(sRunStart);
5159 String FILENAME = sDateStamp.getMonth() + "-"
5160 + sDateStamp.getDay() + "_"
5161 + sDateStamp.getHours() + "-"
5162 + sDateStamp.getMinutes() + "_"
5163 + sDateStamp.getSeconds() + ".txt";
5164
5165 FileOutputStream fos = null;
5166 File outFile = null;
5167 try {
5168 outFile = new File(getFilesDir(), FILENAME);
5169 outFile.createNewFile();
5170 fos = new FileOutputStream(outFile);
5171 } catch (Exception e) {
5172 e.printStackTrace();
5173 }
5174 if (fos != null) {
5175 PrintWriter writer = new PrintWriter(fos);
5176
5177 writer.println(" ");
5178 writer.println("Debug logs: ");
5179 synchronized (sDumpLogs) {
5180 for (int i = 0; i < sDumpLogs.size(); i++) {
5181 writer.println(" " + sDumpLogs.get(i));
5182 }
5183 }
5184 writer.close();
5185 }
5186 try {
5187 if (fos != null) {
5188 fos.close();
5189 success = true;
5190 }
5191 } catch (IOException e) {
5192 e.printStackTrace();
5193 }
Michael Jurka43467462013-11-14 12:03:58 +01005194 return null;
Winson Chungede41292013-09-19 16:27:36 -07005195 }
Michael Jurka43467462013-11-14 12:03:58 +01005196 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005197 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005199}
Michael Jurka2763be32011-02-24 11:19:57 -08005200
Michael Jurkaabded662011-03-04 12:06:57 -08005201interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005202 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005203 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005204 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005205 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005206 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005207}
Dan Sandlerd5024042014-01-09 15:01:33 -05005208
5209interface DebugIntents {
5210 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5211 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005212}