blob: 7944173355644074fda9d845ba2b43d23ddc3576 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700102import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700105import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700110import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000114import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
Winson Chunga6945242014-01-08 14:04:34 -0800116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
Dan Sandlerd5024042014-01-09 15:01:33 -0500132 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 private static final int REQUEST_BIND_APPWIDGET = 11;
142
Mathew Inwood876a8462013-06-14 14:12:41 +0100143 /**
144 * IntentStarter uses request codes starting with this. This must be greater than all activity
145 * request codes used internally.
146 */
147 protected static final int REQUEST_LAST = 100;
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Romain Guy98d01652009-06-30 16:21:04 -0700154 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800155 // To turn on these properties, type
156 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
157 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
158 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800159 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000187 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800188 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000189 // Type: int[]
190 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Adam Cohenb54a5982014-01-08 15:21:04 -0800192
193 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
194
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100195 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700196 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100197 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700198 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100199 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700200
Adam Cohen39a06042013-07-19 14:30:12 -0700201 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700202 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700207 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700208 private State mState = State.WORKSPACE;
209 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700214 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800217 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000219 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
220 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
221
Winson Chunga2413752012-04-03 14:22:34 -0700222 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700223 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
224 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700225 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700226
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800227 private final BroadcastReceiver mCloseSystemDialogsReceiver
228 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800229 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private LayoutInflater mInflater;
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800235 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800236 private DragLayer mDragLayer;
237 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700238 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800239 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700240
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 private AppWidgetManager mAppWidgetManager;
242 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700243
Michael Jurkac9d95c52011-08-29 14:03:34 -0700244 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800246 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700247
Michael Jurka0280c3b2010-09-17 15:00:07 -0700248 private int[] mTmpAddItemCellCoordinates = new int[2];
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private FolderInfo mFolderInfo;
251
Winson Chung3d503fb2011-07-13 17:25:49 -0700252 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800253 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700254
Michael Jurka838a4ca2011-02-07 13:33:06 -0800255 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700256
Winson Chungc51db6a2011-10-05 11:44:49 -0700257 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700258 private AppsCustomizeTabHost mAppsCustomizeTabHost;
259 private AppsCustomizePagedView mAppsCustomizeContent;
260 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800261 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700264 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
265 // scroll issues (because the workspace may not have been measured yet) and extra work.
266 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
267 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268
269 private SpannableStringBuilder mDefaultKeySsb = null;
270
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271 private boolean mWorkspaceLoading = true;
272
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800273 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private boolean mRestoring;
275 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700276 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Michael Jurka1e2f4652013-07-08 18:03:46 -0700278 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700279 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
280
Winson Chung4a2afa32012-07-19 14:53:05 -0700281 // Keep track of whether the user has left launcher
282 private static boolean sPausedFromUserAction = false;
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private Bundle mSavedInstanceState;
285
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800287 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800288 private boolean mUserPresent = true;
289 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700290 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800291 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700293 private static LocaleConfiguration sLocaleConfiguration = null;
294
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700295 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Adam Cohen61f560d2013-09-30 15:58:20 -0700297 private View.OnTouchListener mHapticFeedbackTouchListener;
298
Adam Cohended9f8d2010-11-03 13:25:16 -0700299 // Related to the auto-advancing of widgets
300 private final int ADVANCE_MSG = 1;
301 private final int mAdvanceInterval = 20000;
302 private final int mAdvanceStagger = 250;
303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
305 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
306 new HashMap<View, AppWidgetProviderInfo>();
307
Winson Chung400438b2011-01-16 17:53:48 -0800308 // Determines how long to wait after a rotation before restoring the screen orientation to
309 // match the sensor state.
310 private final int mRestoreScreenOrientationDelay = 500;
311
Michael Jurka4ef207b2010-11-29 17:05:45 -0800312 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700313 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
314 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
315 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316
Winson Chungd64a6662013-09-30 11:06:59 -0700317 private Intent mAppMarketIntent = null;
318 private static final boolean DISABLE_MARKET_BUTTON = true;
319
Craig Mautner360310b2012-10-26 15:13:08 -0700320 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700321
Adam Cohen1462de32012-07-24 22:34:36 -0700322 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700324
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700326 static Date sDateStamp = new Date();
327 static DateFormat sDateFormat =
328 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
329 static long sRunStart = System.currentTimeMillis();
330 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331
Winson Chung46353de2012-02-16 14:05:10 -0800332 // We only want to get the SharedPreferences once since it does an FS stat each time we get
333 // it from the context.
334 private SharedPreferences mSharedPrefs;
335
Adam Cohene25af792013-06-06 23:08:25 -0700336 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700340 private ImageView mFolderIconImageView;
341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Michael Jurkaddd62e92011-02-16 17:49:14 -0800345 private BubbleTextView mWaitingForResume;
346
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 private Runnable mBuildLayersRunnable = new Runnable() {
348 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700349 if (mWorkspace != null) {
350 mWorkspace.buildPageHardwareLayers();
351 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700352 }
353 };
354
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 private static ArrayList<PendingAddArguments> sPendingAddList
356 = new ArrayList<PendingAddArguments>();
357
Michael Jurka7267fa52013-09-26 15:29:57 -0700358 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800359
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 private static class PendingAddArguments {
361 int requestCode;
362 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700363 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700364 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365 int cellX;
366 int cellY;
367 }
368
Daniel Sandlerff02d492013-08-05 02:12:05 -0400369 private Stats mStats;
370
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800371 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800372 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700373 }
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 @Override
376 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700377 if (DEBUG_STRICT_MODE) {
378 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
379 .detectDiskReads()
380 .detectDiskWrites()
381 .detectNetwork() // or .detectAll() for all detectable problems
382 .penaltyLog()
383 .build());
384 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
385 .detectLeakedSqlLiteObjects()
386 .detectLeakedClosableObjects()
387 .penaltyLog()
388 .penaltyDeath()
389 .build());
390 }
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400393
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400394 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400395 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700396
397 // Determine the dynamic grid properties
398 Point smallestSize = new Point();
399 Point largestSize = new Point();
400 Point realSize = new Point();
401 Display display = getWindowManager().getDefaultDisplay();
402 display.getCurrentSizeRange(smallestSize, largestSize);
403 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700404 DisplayMetrics dm = new DisplayMetrics();
405 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700406
Winson Chung5f8afe62013-08-12 16:19:28 -0700407 // Lazy-initialize the dynamic grid
408 DeviceProfile grid = app.initDynamicGrid(this,
409 Math.min(smallestSize.x, smallestSize.y),
410 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700411 realSize.x, realSize.y,
412 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700413
414 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400415 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700416 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800417 mModel = app.setLauncher(this);
418 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700419 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400420 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800421 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700423
Daniel Sandlerff02d492013-08-05 02:12:05 -0400424 mStats = new Stats(this);
425
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
429 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700430
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700431 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
432 // this also ensures that any synchronous binding below doesn't re-trigger another
433 // LauncherModel load.
434 mPaused = false;
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200437 android.os.Debug.startMethodTracing(
438 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
440
Adam Cohen53805212013-10-01 10:39:23 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100446 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800448 registerContentObservers();
449
Joe Onorato7c312c12009-08-13 21:36:53 -0700450 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mSavedState = savedInstanceState;
453 restoreState(mSavedState);
454
455 if (PROFILE_STARTUP) {
456 android.os.Debug.stopMethodTracing();
457 }
458
459 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700460 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 // If the user leaves launcher, then we should just load items asynchronously when
462 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500463 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 } else {
465 // We only load the page synchronously if the user rotates (or triggers a
466 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800467 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
476 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800477
Adam Cohenf9426d52012-06-04 17:26:21 -0700478 updateGlobalIcons();
479
480 // On large interfaces, we want the screen to auto-rotate based on the current orientation
481 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700482
Winson Chunge43a1e72014-01-15 10:33:02 -0800483 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
484 // on the device, then we always show the first run cling experience (or if there is no
485 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800486 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800487 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
488 if (mModel.canMigrateFromOldLauncherDb(this)) {
489 mLauncherClings.showMigrationCling();
490 } else {
491 mLauncherClings.showFirstRunCling();
492 }
Winson Chunga6945242014-01-08 14:04:34 -0800493 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800494 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800495 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700496 }
497
Winson Chung4a2afa32012-07-19 14:53:05 -0700498 protected void onUserLeaveHint() {
499 super.onUserLeaveHint();
500 sPausedFromUserAction = true;
501 }
502
Winson Chung98ca0f72013-07-29 12:58:51 -0700503 /** To be overriden by subclasses to hint to Launcher that we have custom content */
504 protected boolean hasCustomContentToLeft() {
505 return false;
506 }
507
Dave Hawkeya8881582013-09-17 15:55:33 -0600508 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500509 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600510 * {@link #addToCustomContentPage}. This will only be invoked if
511 * {@link #hasCustomContentToLeft()} is {@code true}.
512 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500513 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600514 }
515
516 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800517 * To be overridden by subclasses to indicate that there is an activity to launch
518 * before showing the standard launcher experience.
519 */
520 protected boolean hasFirstRunActivity() {
521 return false;
522 }
523
524 /**
525 * To be overridden by subclasses to launch any first run activity
526 */
527 protected Intent getFirstRunActivity() {
528 return null;
529 }
530
531 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600532 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
533 * ensure the custom content page is added or removed if necessary.
534 */
535 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600536 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600537 // Not bound yet, wait for bindScreens to be called.
538 return;
539 }
540
541 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
542 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500543 mWorkspace.createCustomContentContainer();
544 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600545 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
546 mWorkspace.removeCustomContentPage();
547 }
548 }
549
Adam Cohenf9426d52012-06-04 17:26:21 -0700550 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700551 boolean searchVisible = false;
552 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800553 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700554 int coi = getCurrentOrientationIndexForGlobalIcons();
555 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
556 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700557 if (!DISABLE_MARKET_BUTTON) {
558 updateAppMarketIcon();
559 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700560 searchVisible = updateGlobalSearchIcon();
561 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700562 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700563 if (sGlobalSearchIcon[coi] != null) {
564 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700565 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700566 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700567 if (sVoiceSearchIcon[coi] != null) {
568 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700569 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700570 }
Winson Chungd64a6662013-09-30 11:06:59 -0700571 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700572 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800573 }
Winson Chungadf0c182012-08-23 14:59:07 -0700574 if (mSearchDropTargetBar != null) {
575 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
Romain Guycbb89e42009-06-08 15:52:54 -0700578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580 if (sLocaleConfiguration == null) {
581 new AsyncTask<Void, Void, LocaleConfiguration>() {
582 @Override
583 protected LocaleConfiguration doInBackground(Void... unused) {
584 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
585 readConfiguration(Launcher.this, localeConfiguration);
586 return localeConfiguration;
587 }
588
589 @Override
590 protected void onPostExecute(LocaleConfiguration result) {
591 sLocaleConfiguration = result;
592 checkForLocaleChange(); // recursive, but now with a locale configuration
593 }
594 }.execute();
595 return;
596 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 final Configuration configuration = getResources().getConfiguration();
599
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700600 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 final String locale = configuration.locale.toString();
602
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final int mcc = configuration.mcc;
605
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final int mnc = configuration.mnc;
608
Romain Guy84f296c2009-11-04 15:00:44 -0800609 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610
Romain Guy84f296c2009-11-04 15:00:44 -0800611 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 sLocaleConfiguration.locale = locale;
613 sLocaleConfiguration.mcc = mcc;
614 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700615
Joe Onorato0589f0f2010-02-08 13:44:00 -0800616 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700617
618 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100619 new AsyncTask<Void, Void, Void>() {
620 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100622 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623 }
Michael Jurka43467462013-11-14 12:03:58 +0100624 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700625 }
626 }
627
628 private static class LocaleConfiguration {
629 public String locale;
630 public int mcc = -1;
631 public int mnc = -1;
632 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700633
Romain Guy98d01652009-06-30 16:21:04 -0700634 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
635 DataInputStream in = null;
636 try {
637 in = new DataInputStream(context.openFileInput(PREFERENCES));
638 configuration.locale = in.readUTF();
639 configuration.mcc = in.readInt();
640 configuration.mnc = in.readInt();
641 } catch (FileNotFoundException e) {
642 // Ignore
643 } catch (IOException e) {
644 // Ignore
645 } finally {
646 if (in != null) {
647 try {
648 in.close();
649 } catch (IOException e) {
650 // Ignore
651 }
652 }
653 }
654 }
655
656 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
657 DataOutputStream out = null;
658 try {
659 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
660 out.writeUTF(configuration.locale);
661 out.writeInt(configuration.mcc);
662 out.writeInt(configuration.mnc);
663 out.flush();
664 } catch (FileNotFoundException e) {
665 // Ignore
666 } catch (IOException e) {
667 //noinspection ResultOfMethodCallIgnored
668 context.getFileStreamPath(PREFERENCES).delete();
669 } finally {
670 if (out != null) {
671 try {
672 out.close();
673 } catch (IOException e) {
674 // Ignore
675 }
676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
679
Anton Hanssona2f665f2013-09-26 12:18:32 +0100680 public Stats getStats() {
681 return mStats;
682 }
683
Bjorn Bringertc459e522013-06-07 19:36:01 +0100684 public LayoutInflater getInflater() {
685 return mInflater;
686 }
687
Winson Chung36a62fe2012-05-06 18:04:42 -0700688 boolean isDraggingEnabled() {
689 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
690 // that is subsequently removed from the workspace in startBinding().
691 return !mModel.isLoadingWorkspace();
692 }
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 static int getScreen() {
695 synchronized (sLock) {
696 return sScreen;
697 }
698 }
699
700 static void setScreen(int screen) {
701 synchronized (sLock) {
702 sScreen = screen;
703 }
704 }
705
Winson Chung557d6ed2011-07-08 15:34:52 -0700706 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000707 * Copied from View -- the View version of the method isn't called
708 * anywhere else in our process and only exists for API level 17+,
709 * so it's ok to keep our own version with no API requirement.
710 */
711 public static int generateViewId() {
712 for (;;) {
713 final int result = sNextGeneratedId.get();
714 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
715 int newValue = result + 1;
716 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
717 if (sNextGeneratedId.compareAndSet(result, newValue)) {
718 return result;
719 }
720 }
721 }
722
723 public int getViewIdForItem(ItemInfo info) {
724 // This cast is safe given the > 2B range for int.
725 int itemId = (int) info.id;
726 if (mItemIdToViewId.containsKey(itemId)) {
727 return mItemIdToViewId.get(itemId);
728 }
729 int viewId = generateViewId();
730 mItemIdToViewId.put(itemId, viewId);
731 return viewId;
732 }
733
734 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700735 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
736 * a configuration step, this allows the proper animations to run after other transitions.
737 */
738 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700739 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 switch (args.requestCode) {
741 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700742 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 break;
745 case REQUEST_PICK_SHORTCUT:
746 processShortcut(args.intent);
747 break;
748 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700749 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700750 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 result = true;
752 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 case REQUEST_CREATE_APPWIDGET:
754 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700755 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700756 result = true;
757 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 }
Michael Jurka27614d22012-04-02 06:40:22 -0700759 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
760 // if you turned the screen off and then back while in All Apps, Launcher would not
761 // return to the workspace. Clearing mAddInfo.container here fixes this issue
762 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700763 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800764 }
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 protected void onActivityResult(
768 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700769 // Reset the startActivity waiting flag
770 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800771 int pendingAddWidgetId = mPendingAddWidgetId;
772 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700773
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 Runnable exitSpringLoaded = new Runnable() {
775 @Override
776 public void run() {
777 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
778 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
779 }
780 };
781
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
785 if (resultCode == RESULT_CANCELED) {
786 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
788 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
791 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700792 }
793 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200794 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
795 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
796 mWorkspace.exitOverviewMode(false);
797 }
798 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200800
Adam Cohened66b2b2012-01-23 17:28:51 -0800801 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
802 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700803
Adam Cohened66b2b2012-01-23 17:28:51 -0800804 // We have special handling for widgets
805 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800806 final int appWidgetId;
807 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
808 : -1;
809 if (widgetId < 0) {
810 appWidgetId = pendingAddWidgetId;
811 } else {
812 appWidgetId = widgetId;
813 }
814
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 final int result;
816 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800817 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700818 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
819 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 result = RESULT_CANCELED;
821 completeTwoStageWidgetDrop(result, appWidgetId);
822 onComplete = new Runnable() {
823 @Override
824 public void run() {
825 exitSpringLoadedDragModeDelayed(false, 0, null);
826 }
827 };
Winson Chung5aaab772012-04-27 15:24:02 -0700828 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700830 final CellLayout dropLayout =
831 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
832 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 onComplete = new Runnable() {
834 @Override
835 public void run() {
836 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700837 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700838 }
839 };
Winson Chung5aaab772012-04-27 15:24:02 -0700840 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700841 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
842 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 return;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // The pattern used here is that a user PICKs a specific application,
847 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
850 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700851 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800852 final PendingAddArguments args = new PendingAddArguments();
853 args.requestCode = requestCode;
854 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700856 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 args.cellX = mPendingAddInfo.cellX;
858 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800859 if (isWorkspaceLocked()) {
860 sPendingAddList.add(args);
861 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700862 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
865 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700866 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700867 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 }
872
873 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700874 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700875 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 Runnable onCompleteRunnable = null;
877 int animationType = 0;
878
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700879 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 if (resultCode == RESULT_OK) {
881 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
882 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700883 mPendingAddWidgetInfo);
884 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 onCompleteRunnable = new Runnable() {
886 @Override
887 public void run() {
888 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700889 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
891 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
893 };
894 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800895 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800897 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 if (mDragLayer.getAnimatedView() != null) {
899 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
900 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
901 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700902 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 // The animated view may be null in the case of a rotation during widget configuration
904 onCompleteRunnable.run();
905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907
908 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700909 protected void onStop() {
910 super.onStop();
911 FirstFrameAnimatorHelper.setIsVisible(false);
912 }
913
914 @Override
915 protected void onStart() {
916 super.onStart();
917 FirstFrameAnimatorHelper.setIsVisible(true);
918 }
919
920 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200922 long startTime = 0;
923 if (DEBUG_RESUME_TIME) {
924 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400925 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700928
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 // Restore the previous launcher state
930 if (mOnResumeState == State.WORKSPACE) {
931 showWorkspace(false);
932 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800933 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700934 }
935 mOnResumeState = State.NONE;
936
Craig Mautner360310b2012-10-26 15:13:08 -0700937 // Background was set to gradient in onPause(), restore to black if in all apps.
938 setWorkspaceBackground(mState == State.WORKSPACE);
939
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800940 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700941 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700942 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400943 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500944 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700946 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700948 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200949 // We might have postponed some bind calls until onResume (see waitUntilResume) --
950 // execute them here
951 long startTimeCallbacks = 0;
952 if (DEBUG_RESUME_TIME) {
953 startTimeCallbacks = System.currentTimeMillis();
954 }
955
956 if (mAppsCustomizeContent != null) {
957 mAppsCustomizeContent.setBulkBind(true);
958 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700959 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
960 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200961 }
962 if (mAppsCustomizeContent != null) {
963 mAppsCustomizeContent.setBulkBind(false);
964 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700965 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200966 if (DEBUG_RESUME_TIME) {
967 Log.d(TAG, "Time spent processing callbacks in onResume: " +
968 (System.currentTimeMillis() - startTimeCallbacks));
969 }
Michael Jurka447bf842013-05-15 14:52:15 +0200970 }
Michael Jurka54554252013-08-01 12:52:23 +0200971 if (mOnResumeCallbacks.size() > 0) {
972 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
973 mOnResumeCallbacks.get(i).run();
974 }
975 mOnResumeCallbacks.clear();
976 }
Winson Chunge4e50662012-01-23 14:45:13 -0800977
978 // Reset the pressed state of icons that were locked in the press state while activities
979 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800980 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800981 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800982 mWaitingForResume.setStayPressed(false);
983 }
Winson Chunge4e50662012-01-23 14:45:13 -0800984 if (mAppsCustomizeContent != null) {
985 // Resets the previous all apps icon press state
986 mAppsCustomizeContent.resetDrawableState();
987 }
Winson Chung82963d52013-10-09 11:20:57 -0700988
Adam Cohen06dff352012-06-01 17:17:08 -0700989 // It is possible that widgets can receive updates while launcher is not in the foreground.
990 // Consequently, the widgets will be inflated in the orientation of the foreground activity
991 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
992 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700993 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700994
Winson Chung780fe592013-09-26 14:48:44 -0700995 // Process any items that were added while Launcher was away.
996 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
997
Winson Chung5841efa2013-09-30 18:06:44 -0700998 // Update the voice search button proxy
999 updateVoiceButtonProxyVisible(false);
1000
Adam Cohenf9426d52012-06-04 17:26:21 -07001001 // Again, as with the above scenario, it's possible that one or more of the global icons
1002 // were updated in the wrong orientation.
1003 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001004 if (DEBUG_RESUME_TIME) {
1005 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1006 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001007
1008 if (mWorkspace.getCustomContentCallbacks() != null) {
1009 // If we are resuming and the custom content is the current page, we call onShow().
1010 // It is also poassible that onShow will instead be called slightly after first layout
1011 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1012 if (mWorkspace.isOnOrMovingToCustomContent()) {
1013 mWorkspace.getCustomContentCallbacks().onShow();
1014 }
1015 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001016 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001017 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001018 }
1019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001021 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001022 // Ensure that items added to Launcher are queued until Launcher returns
1023 InstallShortcutReceiver.enableInstallQueue();
1024
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001025 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001026 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001027 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001028 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001029
1030 // We call onHide() aggressively. The custom content callbacks should be able to
1031 // debounce excess onHide calls.
1032 if (mWorkspace.getCustomContentCallbacks() != null) {
1033 mWorkspace.getCustomContentCallbacks().onHide();
1034 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
Adam Cohenbffe7452013-07-22 18:21:45 -07001037 QSBScroller mQsbScroller = new QSBScroller() {
1038 int scrollY = 0;
1039
1040 @Override
1041 public void setScrollY(int scroll) {
1042 scrollY = scroll;
1043
1044 if (mWorkspace.isOnOrMovingToCustomContent()) {
1045 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001046 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001047 }
1048 }
1049 };
1050
1051 public void resetQSBScroll() {
1052 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001053 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001054 }
1055
1056 public interface CustomContentCallbacks {
1057 // Custom content is completely shown
1058 public void onShow();
1059
1060 // Custom content is completely hidden
1061 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001062
1063 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1064 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001065 }
1066
Jorim Jaggid017f882014-01-14 17:08:48 -08001067 protected boolean hasSettings() {
1068 return false;
1069 }
1070
Adam Cohen30bacb22013-08-29 14:25:25 -07001071 protected void startSettings() {
1072 }
1073
Adam Cohenbffe7452013-07-22 18:21:45 -07001074 public interface QSBScroller {
1075 public void setScrollY(int scrollY);
1076 }
1077
Winson Chung98ca0f72013-07-29 12:58:51 -07001078 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001079 CustomContentCallbacks callbacks, String description) {
1080 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001081 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001082 }
1083
Adam Cohenedb40762013-07-18 16:45:45 -07001084 // The custom content needs to offset its content to account for the QSB
1085 public int getTopOffsetForCustomContent() {
1086 return mWorkspace.getPaddingTop();
1087 }
1088
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001089 @Override
1090 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 // Flag the loader to stop early before switching
1092 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001093 if (mAppsCustomizeContent != null) {
1094 mAppsCustomizeContent.surrender();
1095 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001096 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001097 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001098
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001099 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001100 @Override
1101 public void onWindowFocusChanged(boolean hasFocus) {
1102 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001103 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001104 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 private boolean acceptFilter() {
1107 final InputMethodManager inputManager = (InputMethodManager)
1108 getSystemService(Context.INPUT_METHOD_SERVICE);
1109 return !inputManager.isFullscreenMode();
1110 }
1111
1112 @Override
1113 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001114 final int uniChar = event.getUnicodeChar();
1115 final boolean handled = super.onKeyDown(keyCode, event);
1116 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1117 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1119 keyCode, event);
1120 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001121 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001122 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001123 // showSearchDialog()
1124 // If there are multiple keystrokes before the search dialog takes focus,
1125 // onSearchRequested() will be called for every keystroke,
1126 // but it is idempotent, so it's fine.
1127 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129 }
1130
Joe Onorato8a9625e2010-01-28 15:55:35 -08001131 // Eat the long press event so the keyboard doesn't come up.
1132 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1133 return true;
1134 }
1135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 return handled;
1137 }
1138
Karl Rosaen138a0412009-04-23 19:00:21 -07001139 private String getTypedText() {
1140 return mDefaultKeySsb.toString();
1141 }
1142
1143 private void clearTypedText() {
1144 mDefaultKeySsb.clear();
1145 mDefaultKeySsb.clearSpans();
1146 Selection.setSelection(mDefaultKeySsb, 0);
1147 }
1148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001150 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1151 * State
1152 */
1153 private static State intToState(int stateOrdinal) {
1154 State state = State.WORKSPACE;
1155 final State[] stateValues = State.values();
1156 for (int i = 0; i < stateValues.length; i++) {
1157 if (stateValues[i].ordinal() == stateOrdinal) {
1158 state = stateValues[i];
1159 break;
1160 }
1161 }
1162 return state;
1163 }
1164
1165 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 * Restores the previous state, if it exists.
1167 *
1168 * @param savedState The previous state.
1169 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001170 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 private void restoreState(Bundle savedState) {
1172 if (savedState == null) {
1173 return;
1174 }
1175
Michael Jurka883f55b2010-10-21 15:47:14 -07001176 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001177 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001178 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001179 }
1180
Adam Cohen21cd0022013-10-09 18:57:02 -07001181 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1182 PagedView.INVALID_RESTORE_PAGE);
1183 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001184 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 }
1186
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001188 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1191 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001192 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001193 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1194 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001195 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1196 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1197 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001198 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001199 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 mRestoring = true;
1201 }
1202
1203 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1204 if (renameFolder) {
1205 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001206 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 mRestoring = true;
1208 }
Winson Chunga12a2502010-12-20 14:41:35 -08001209
Winson Chung785d2eb2011-04-14 16:08:02 -07001210 // Restore the AppsCustomize tab
1211 if (mAppsCustomizeTabHost != null) {
1212 String curTab = savedState.getString("apps_customize_currentTab");
1213 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001214 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001216 mAppsCustomizeContent.loadAssociatedPages(
1217 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001218 }
1219
Winson Chung5afbf7b2011-07-25 11:53:08 -07001220 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1221 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001222 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001223 mItemIdToViewId = (HashMap<Integer, Integer>)
1224 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226
1227 /**
1228 * Finds all the views we need and configure them properly.
1229 */
1230 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001231 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001232
Craig Mautner360310b2012-10-26 15:13:08 -07001233 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001234 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1235 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001236 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001237
John Spurlock77e1f472013-09-11 10:09:51 -04001238 mLauncherView.setSystemUiVisibility(
1239 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001240 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
Winson Chung4c98d922011-05-31 16:50:48 -07001242 // Setup the drag layer
1243 mDragLayer.setup(this, dragController);
1244
Winson Chung3d503fb2011-07-13 17:25:49 -07001245 // Setup the hotseat
1246 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1247 if (mHotseat != null) {
1248 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001249 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 }
1251
Jorim Jaggid017f882014-01-14 17:08:48 -08001252 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001253 View widgetButton = findViewById(R.id.widget_button);
1254 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001255 @Override
1256 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001257 if (!mWorkspace.isSwitchingState()) {
1258 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1259 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 }
1261 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001262 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1263
1264 View wallpaperButton = findViewById(R.id.wallpaper_button);
1265 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001266 @Override
1267 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001268 if (!mWorkspace.isSwitchingState()) {
1269 startWallpaper();
1270 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001271 }
1272 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001273 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1274
1275 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001276 if (hasSettings()) {
1277 settingsButton.setOnClickListener(new OnClickListener() {
1278 @Override
1279 public void onClick(View arg0) {
1280 if (!mWorkspace.isSwitchingState()) {
1281 startSettings();
1282 }
1283 }
1284 });
1285 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1286 } else {
1287 settingsButton.setVisibility(View.GONE);
1288 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1289 lp.gravity = Gravity.END | Gravity.TOP;
1290 widgetButton.requestLayout();
1291 }
1292
Adam Cohendbdff6b2013-09-18 19:09:15 -07001293 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001294
Winson Chung4c98d922011-05-31 16:50:48 -07001295 // Setup the workspace
1296 mWorkspace.setHapticFeedbackEnabled(false);
1297 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001298 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001299 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001300
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001302 mSearchDropTargetBar = (SearchDropTargetBar)
1303 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001304
Winson Chungf0ea4d32011-06-06 14:27:16 -07001305 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001306 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001307 mAppsCustomizeContent = (AppsCustomizePagedView)
1308 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1309 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001312 dragController.setDragScoller(mWorkspace);
1313 dragController.setScrollView(mDragLayer);
1314 dragController.setMoveTarget(mWorkspace);
1315 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001316 if (mSearchDropTargetBar != null) {
1317 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001318 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001319
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001320 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001321 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001322 mWeightWatcher = new WeightWatcher(this);
1323 mWeightWatcher.setAlpha(0.5f);
1324 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001325 new FrameLayout.LayoutParams(
1326 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001327 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001328 Gravity.BOTTOM)
1329 );
Adam Cohen39a06042013-07-19 14:30:12 -07001330
1331 boolean show = shouldShowWeightWatcher();
1332 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
1336 /**
1337 * Creates a view representing a shortcut.
1338 *
1339 * @param info The data structure describing the shortcut.
1340 *
1341 * @return A View inflated from R.layout.application.
1342 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001343 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001345 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 /**
1349 * Creates a view representing a shortcut inflated from the specified resource.
1350 *
1351 * @param layoutResId The id of the XML layout used to create the shortcut.
1352 * @param parent The group the shortcut belongs to.
1353 * @param info The data structure describing the shortcut.
1354 *
1355 * @return A View inflated from layoutResId.
1356 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001357 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001358 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1359 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 return favorite;
1362 }
1363
1364 /**
1365 * Add an application shortcut to the workspace.
1366 *
1367 * @param data The intent describing the application.
1368 * @param cellInfo The position on screen where to create the shortcut.
1369 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001373
Adam Cohenfbba09b2011-07-18 21:43:05 -07001374 // First we check if we already know the exact location where we want to add this item.
1375 if (cellX >= 0 && cellY >= 0) {
1376 cellXY[0] = cellX;
1377 cellXY[1] = cellY;
1378 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001379 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001380 return;
1381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001383 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001384
Romain Guy73b979d2009-06-09 12:57:21 -07001385 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001386 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001388 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001389 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001390 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001391 } else {
1392 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394 }
Romain Guycbb89e42009-06-08 15:52:54 -07001395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 /**
1397 * Add a shortcut to the workspace.
1398 *
1399 * @param data The intent describing the shortcut.
1400 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001403 int cellY) {
1404 int[] cellXY = mTmpAddItemCellCoordinates;
1405 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001406 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001407
Michael Jurkad3ef3062010-11-23 16:23:58 -08001408 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001409
Adam Cohen558baaf2011-08-15 15:22:57 -07001410 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001411 if (info == null) {
1412 return;
1413 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001414 final View view = createShortcut(info);
1415
Adam Cohenfbba09b2011-07-18 21:43:05 -07001416 // First we check if we already know the exact location where we want to add this item.
1417 if (cellX >= 0 && cellY >= 0) {
1418 cellXY[0] = cellX;
1419 cellXY[1] = cellY;
1420 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001421
1422 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001423 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001424 true, null,null)) {
1425 return;
1426 }
1427 DragObject dragObject = new DragObject();
1428 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001429 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1430 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001431 return;
1432 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001433 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001434 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001436 foundCellSpan = (result != null);
1437 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001439 }
1440
1441 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001442 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001443 return;
1444 }
1445
Adam Cohendcd297f2013-06-18 13:13:40 -07001446 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001449 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001450 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452 }
1453
Adam Cohen2f093b62012-04-30 18:59:53 -07001454 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1455 int minHeight) {
1456 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001457 // We want to account for the extra amount of padding that we are adding to the widget
1458 // to ensure that it gets the full amount of space that it has requested
1459 int requiredWidth = minWidth + padding.left + padding.right;
1460 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001461 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001462 }
1463
Adam Cohen2f093b62012-04-30 18:59:53 -07001464 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1465 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1469 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001470 }
1471
Adam Cohen2f093b62012-04-30 18:59:53 -07001472 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1473 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001474 }
1475
Adam Cohen2f093b62012-04-30 18:59:53 -07001476 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1477 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001478 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001482 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001484 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001485 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001487 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001488 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1489 if (appWidgetInfo == null) {
1490 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1491 }
Romain Guycbb89e42009-06-08 15:52:54 -07001492
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001493 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001495
Adam Cohen2f093b62012-04-30 18:59:53 -07001496 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1497 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001500 // We have saved the position to which the widget was dragged-- this really only matters
1501 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 int[] cellXY = mTmpAddItemCellCoordinates;
1503 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001504 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1507 cellXY[0] = mPendingAddInfo.cellX;
1508 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001509 spanXY[0] = mPendingAddInfo.spanX;
1510 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 foundCellSpan = true;
1512 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001515 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1516 spanXY[1], cellXY, finalSpan);
1517 spanXY[0] = finalSpan[0];
1518 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001521 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001522 }
1523
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001525 if (appWidgetId != -1) {
1526 // Deleting an app widget ID is a void call but writes to disk before returning
1527 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001528 new AsyncTask<Void, Void, Void>() {
1529 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001530 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001531 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001532 }
Michael Jurka43467462013-11-14 12:03:58 +01001533 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001534 }
Winson Chung93eef082012-03-23 15:59:27 -07001535 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001536 return;
1537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001539 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001540 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1541 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 launcherInfo.spanX = spanXY[0];
1543 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001544 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1545 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 if (hostView == null) {
1552 // Perform actual inflation because we're live
1553 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1554 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1555 } else {
1556 // The AppWidgetHostView has already been inflated and instantiated
1557 launcherInfo.hostView = hostView;
1558 }
Romain Guycbb89e42009-06-08 15:52:54 -07001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001561 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001562 launcherInfo.notifyWidgetSizeChanged(this);
1563
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001566
1567 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001569 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Adam Cohended9f8d2010-11-03 13:25:16 -07001572 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1573 @Override
1574 public void onReceive(Context context, Intent intent) {
1575 final String action = intent.getAction();
1576 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1577 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001578 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001579 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001580
Winson Chungc100e8e2011-08-09 16:02:43 -07001581 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001582 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001583 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001584 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001586 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1587 mUserPresent = true;
1588 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001589 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1590 mModel.resetLoadedState(false, true);
1591 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1592 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1593 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1594 mModel.resetLoadedState(false, true);
1595 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1596 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1597 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001598 }
1599 }
1600 };
1601
1602 @Override
1603 public void onAttachedToWindow() {
1604 super.onAttachedToWindow();
1605
1606 // Listen for broadcasts related to user-presence
1607 final IntentFilter filter = new IntentFilter();
1608 filter.addAction(Intent.ACTION_SCREEN_OFF);
1609 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001610 if (ENABLE_DEBUG_INTENTS) {
1611 filter.addAction(DebugIntents.DELETE_DATABASE);
1612 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1613 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001614 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001615 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001616 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001617 mVisible = true;
1618 }
1619
1620 @Override
1621 public void onDetachedFromWindow() {
1622 super.onDetachedFromWindow();
1623 mVisible = false;
1624
Adam Cohend113e0c2010-11-11 10:48:05 -08001625 if (mAttached) {
1626 unregisterReceiver(mReceiver);
1627 mAttached = false;
1628 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001629 updateRunning();
1630 }
1631
1632 public void onWindowVisibilityChanged(int visibility) {
1633 mVisible = visibility == View.VISIBLE;
1634 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001635 // The following code used to be in onResume, but it turns out onResume is called when
1636 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1637 // is a more appropriate event to handle
1638 if (mVisible) {
1639 mAppsCustomizeTabHost.onWindowVisible();
1640 if (!mWorkspaceLoading) {
1641 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001642 // We want to let Launcher draw itself at least once before we force it to build
1643 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001644 // apps is nice and speedy.
1645 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001646 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001647 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001648 if (mStarted) return;
1649 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001650 // We delay the layer building a bit in order to give
1651 // other message processing a time to run. In particular
1652 // this avoids a delay in hiding the IME if it was
1653 // currently shown, because doing that may involve
1654 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001655 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001656 final ViewTreeObserver.OnDrawListener listener = this;
1657 mWorkspace.post(new Runnable() {
1658 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001659 if (mWorkspace != null &&
1660 mWorkspace.getViewTreeObserver() != null) {
1661 mWorkspace.getViewTreeObserver().
1662 removeOnDrawListener(listener);
1663 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001664 }
1665 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001666 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001667 }
1668 });
1669 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001670 // When Launcher comes back to foreground, a different Activity might be responsible for
1671 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001672 if (!DISABLE_MARKET_BUTTON) {
1673 updateAppMarketIcon();
1674 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001675 clearTypedText();
1676 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 }
1678
1679 private void sendAdvanceMessage(long delay) {
1680 mHandler.removeMessages(ADVANCE_MSG);
1681 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1682 mHandler.sendMessageDelayed(msg, delay);
1683 mAutoAdvanceSentTime = System.currentTimeMillis();
1684 }
1685
1686 private void updateRunning() {
1687 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1688 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1689 mAutoAdvanceRunning = autoAdvanceRunning;
1690 if (autoAdvanceRunning) {
1691 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1692 sendAdvanceMessage(delay);
1693 } else {
1694 if (!mWidgetsToAdvance.isEmpty()) {
1695 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1696 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1697 }
1698 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001699 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 }
1701 }
1702 }
1703
1704 private final Handler mHandler = new Handler() {
1705 @Override
1706 public void handleMessage(Message msg) {
1707 if (msg.what == ADVANCE_MSG) {
1708 int i = 0;
1709 for (View key: mWidgetsToAdvance.keySet()) {
1710 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1711 final int delay = mAdvanceStagger * i;
1712 if (v instanceof Advanceable) {
1713 postDelayed(new Runnable() {
1714 public void run() {
1715 ((Advanceable) v).advance();
1716 }
1717 }, delay);
1718 }
1719 i++;
1720 }
1721 sendAdvanceMessage(mAdvanceInterval);
1722 }
1723 }
1724 };
1725
1726 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001727 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1729 if (v instanceof Advanceable) {
1730 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001731 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 updateRunning();
1733 }
1734 }
1735
1736 void removeWidgetToAutoAdvance(View hostView) {
1737 if (mWidgetsToAdvance.containsKey(hostView)) {
1738 mWidgetsToAdvance.remove(hostView);
1739 updateRunning();
1740 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001741 }
1742
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001745 launcherInfo.hostView = null;
1746 }
1747
Winson Chung93eef082012-03-23 15:59:27 -07001748 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1749 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1750 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 }
1752
Winson Chunga6945242014-01-08 14:04:34 -08001753 public DragLayer getDragLayer() {
1754 return mDragLayer;
1755 }
1756
1757 public Workspace getWorkspace() {
1758 return mWorkspace;
1759 }
1760
1761 public Hotseat getHotseat() {
1762 return mHotseat;
1763 }
1764
Jorim Jaggid017f882014-01-14 17:08:48 -08001765 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001766 return mOverviewPanel;
1767 }
1768
1769 public SearchDropTargetBar getSearchBar() {
1770 return mSearchDropTargetBar;
1771 }
1772
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001773 public LauncherAppWidgetHost getAppWidgetHost() {
1774 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Romain Guycbb89e42009-06-08 15:52:54 -07001776
Winson Chunga9abd0e2010-10-27 17:18:37 -07001777 public LauncherModel getModel() {
1778 return mModel;
1779 }
1780
Winson Chunga6945242014-01-08 14:04:34 -08001781 public LauncherClings getLauncherClings() {
1782 return mLauncherClings;
1783 }
1784
1785 protected SharedPreferences getSharedPrefs() {
1786 return mSharedPrefs;
1787 }
1788
Bjorn Bringertc459e522013-06-07 19:36:01 +01001789 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001790 getWindow().closeAllPanels();
1791
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001792 // Whatever we were doing is hereby canceled.
1793 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001794 }
1795
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 @Override
1797 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001798 long startTime = 0;
1799 if (DEBUG_RESUME_TIME) {
1800 startTime = System.currentTimeMillis();
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 super.onNewIntent(intent);
1803
1804 // Close the menu
1805 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001806 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001807 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001808
Adam Cohened307df2013-10-02 09:37:31 -07001809 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1810 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1811 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001812
Adam Cohen6fecd412013-10-02 17:41:50 -07001813 if (mWorkspace == null) {
1814 // Can be cases where mWorkspace is null, this prevents a NPE
1815 return;
1816 }
1817 Folder openFolder = mWorkspace.getOpenFolder();
1818 // In all these cases, only animate if we're already on home
1819 mWorkspace.exitWidgetResizeMode();
1820 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001821 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001822 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001823 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001824
Adam Cohen6fecd412013-10-02 17:41:50 -07001825 closeFolder();
1826 exitSpringLoadedDragMode();
1827
1828 // If we are already on home, then just animate back to the workspace,
1829 // otherwise, just wait until onResume to set the state back to Workspace
1830 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001831 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001832 } else {
1833 mOnResumeState = State.WORKSPACE;
1834 }
1835
1836 final View v = getWindow().peekDecorView();
1837 if (v != null && v.getWindowToken() != null) {
1838 InputMethodManager imm = (InputMethodManager)getSystemService(
1839 INPUT_METHOD_SERVICE);
1840 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1841 }
1842
1843 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001844 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001845 mAppsCustomizeTabHost.reset();
1846 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001847
1848 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 }
Adam Cohened307df2013-10-02 09:37:31 -07001850
Michael Jurka447bf842013-05-15 14:52:15 +02001851 if (DEBUG_RESUME_TIME) {
1852 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
1855
Adam Coppa120b8e2013-11-12 16:26:04 +00001856 /**
1857 * Override point for subclasses to prevent movement to the default screen when the home
1858 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1859 */
1860 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1861 return true;
1862 }
1863
1864 /**
1865 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1866 */
1867 protected void onHomeIntent() {
1868 // Do nothing
1869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001872 public void onRestoreInstanceState(Bundle state) {
1873 super.onRestoreInstanceState(state);
1874 for (int page: mSynchronouslyBoundPages) {
1875 mWorkspace.restoreInstanceStateForChild(page);
1876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878
1879 @Override
1880 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001881 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001882 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1883 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001884 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001885 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886
Michael Jurka883f55b2010-10-21 15:47:14 -07001887 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001888 // We close any open folder since it will not be re-opened, and we need to make sure
1889 // this state is reflected.
1890 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
Adam Cohendcd297f2013-06-18 13:13:40 -07001892 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001893 mWaitingForResult) {
1894 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001895 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1897 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001898 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1899 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1900 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001901 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903
1904 if (mFolderInfo != null && mWaitingForResult) {
1905 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1906 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1907 }
Michael Jurka946ad472010-07-09 18:05:18 -07001908
Winson Chung785d2eb2011-04-14 16:08:02 -07001909 // Save the current AppsCustomize tab
1910 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001911 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1912 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001913 if (currentTabTag != null) {
1914 outState.putString("apps_customize_currentTab", currentTabTag);
1915 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001916 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1917 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001918 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001919 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 }
1921
1922 @Override
1923 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001925
Winson Chunge7a03942011-08-05 15:05:12 -07001926 // Remove all pending runnables
1927 mHandler.removeMessages(ADVANCE_MSG);
1928 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001929 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001930
Winson Chungcd2b0142011-06-08 16:02:26 -07001931 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001932 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001933 mModel.stopLoader();
1934 app.setLauncher(null);
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001937 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001939 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001941 mAppWidgetHost = null;
1942
1943 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
1945 TextKeyListener.getInstance().release();
1946
Winson Chung81b52252012-08-27 15:34:29 -07001947 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1948 // to prevent leaking Launcher activities on orientation change.
1949 if (mModel != null) {
1950 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1951 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001952
1953 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001954 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001955
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001956 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001957 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001958 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001959 mWorkspace = null;
1960 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001961
Michael Jurka2ecf9952012-06-18 12:52:28 -07001962 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
Adam Cohena9cf38f2011-05-02 15:36:58 -07001965 public DragController getDragController() {
1966 return mDragController;
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
1970 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001971 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 super.startActivityForResult(intent, requestCode);
1973 }
1974
Winson Chung70d72102011-08-12 11:24:35 -07001975 /**
1976 * Indicates that we want global search for this activity by setting the globalSearch
1977 * argument for {@link #startSearch} to true.
1978 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001980 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001982
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001983 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Karl Rosaen138a0412009-04-23 19:00:21 -07001985 if (initialQuery == null) {
1986 // Use any text typed in the launcher as the initial query
1987 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 if (appSearchData == null) {
1990 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001991 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Winson Chungadf0c182012-08-23 14:59:07 -07001993 Rect sourceBounds = new Rect();
1994 if (mSearchDropTargetBar != null) {
1995 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1996 }
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Bjorn Bringertc459e522013-06-07 19:36:01 +01001998 startSearch(initialQuery, selectInitialQuery,
1999 appSearchData, sourceBounds);
2000 }
2001
2002 public void startSearch(String initialQuery,
2003 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002004 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002005 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002006 }
2007
2008 /**
2009 * Starts the global search activity. This code is a copied from SearchManager
2010 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002011 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002012 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002013 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002014 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2015 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2016 if (globalSearchActivity == null) {
2017 Log.w(TAG, "No global search activity found.");
2018 return;
2019 }
2020 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2021 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2022 intent.setComponent(globalSearchActivity);
2023 // Make sure that we have a Bundle to put source in
2024 if (appSearchData == null) {
2025 appSearchData = new Bundle();
2026 } else {
2027 appSearchData = new Bundle(appSearchData);
2028 }
2029 // Set source to package name of app that starts global search, if not set already.
2030 if (!appSearchData.containsKey("source")) {
2031 appSearchData.putString("source", getPackageName());
2032 }
2033 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2034 if (!TextUtils.isEmpty(initialQuery)) {
2035 intent.putExtra(SearchManager.QUERY, initialQuery);
2036 }
2037 if (selectInitialQuery) {
2038 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2039 }
2040 intent.setSourceBounds(sourceBounds);
2041 try {
2042 startActivity(intent);
2043 } catch (ActivityNotFoundException ex) {
2044 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Yura4f93ec62014-02-04 14:15:21 +00002048 public boolean isOnCustomContent() {
2049 return mWorkspace.isOnOrMovingToCustomContent();
2050 }
2051
Winson Chung70d72102011-08-12 11:24:35 -07002052 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002053 public boolean onPrepareOptionsMenu(Menu menu) {
2054 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002055 if (!isOnCustomContent()) {
2056 // Close any open folders
2057 closeFolder();
2058 // Stop resizing any widgets
2059 mWorkspace.exitWidgetResizeMode();
2060 if (!mWorkspace.isInOverviewMode()) {
2061 // Show the overview mode
2062 showOverviewMode(true);
2063 } else {
2064 showWorkspace(true);
2065 }
Winson Chunge0298742014-01-17 12:03:00 -08002066 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002067 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002070 @Override
2071 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002072 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002073 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002075 }
2076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 public boolean isWorkspaceLocked() {
2078 return mWorkspaceLoading || mWaitingForResult;
2079 }
2080
Adam Cohen517a7f52014-03-01 12:12:59 -08002081 public boolean isWorkspaceLoading() {
2082 return mWorkspaceLoading;
2083 }
2084
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002087 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002088 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2089 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002090 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002092 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002093
Adam Cohenad4e15c2013-10-17 16:21:35 -07002094 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2095 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2096 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2097 }
2098
2099 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2100 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2101 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002102 if (appWidgetInfo.configure != null) {
2103 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002104 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002105
Bjorn Bringert7984c942009-12-09 15:38:25 +00002106 // Launch over to configure widget, if needed
2107 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002108 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002109 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002110 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002112 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002113 Runnable onComplete = new Runnable() {
2114 @Override
2115 public void run() {
2116 // Exit spring loaded mode if necessary after adding the widget
2117 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2118 null);
2119 }
2120 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002121 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002122 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002123 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125 }
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002127 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002128 // Close any folders that may be open.
2129 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002130 mWorkspace.moveToCustomContentScreen(animate);
2131 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002132 /**
2133 * Process a shortcut drop.
2134 *
2135 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002136 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002137 * @param cell The cell it should be added to, optional
2138 * @param position The location on the screen where it was dropped, optional
2139 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002140 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002141 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002142 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002144 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002146
2147 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002148 mPendingAddInfo.cellX = cell[0];
2149 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002150 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002151
2152 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2153 createShortcutIntent.setComponent(componentName);
2154 processShortcut(createShortcutIntent);
2155 }
2156
Adam Cohenfbba09b2011-07-18 21:43:05 -07002157 /**
2158 * Process a widget drop.
2159 *
2160 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002161 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002162 * @param cell The cell it should be added to, optional
2163 * @param position The location on the screen where it was dropped, optional
2164 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002166 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002167 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002171 mPendingAddInfo.minSpanX = info.minSpanX;
2172 mPendingAddInfo.minSpanY = info.minSpanY;
2173
Adam Cohenfbba09b2011-07-18 21:43:05 -07002174 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002175 mPendingAddInfo.cellX = cell[0];
2176 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002177 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002178 if (span != null) {
2179 mPendingAddInfo.spanX = span[0];
2180 mPendingAddInfo.spanY = span[1];
2181 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002182
Adam Cohened66b2b2012-01-23 17:28:51 -08002183 AppWidgetHostView hostView = info.boundWidget;
2184 int appWidgetId;
2185 if (hostView != null) {
2186 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002187 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002188 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002189 // In this case, we either need to start an activity to get permission to bind
2190 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002191 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002192 Bundle options = info.bindOptions;
2193
2194 boolean success = false;
2195 if (options != null) {
2196 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2197 info.componentName, options);
2198 } else {
2199 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2200 info.componentName);
2201 }
2202 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002204 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002205 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002206 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2207 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2208 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002209 // TODO: we need to make sure that this accounts for the options bundle.
2210 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002211 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2212 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002213 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002214 }
2215
Joe Onoratodeb98af2010-02-19 14:59:39 -08002216 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 // Handle case where user selected "Applications"
2218 String applicationName = getResources().getString(R.string.group_applications);
2219 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002220
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002221 if (applicationName != null && applicationName.equals(shortcutName)) {
2222 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2223 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002224
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002225 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2226 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002227 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002228 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002229 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002230 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233
Winson Chung24ab2f12010-09-16 14:10:47 -07002234 void processWallpaper(Intent intent) {
2235 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2236 }
2237
Adam Cohendcd297f2013-06-18 13:13:40 -07002238 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002240 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 folderInfo.title = getText(R.string.folder_name);
2242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002245 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002246 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247
2248 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 FolderIcon newFolder =
2250 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002253 // Force measure the new folder icon
2254 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2255 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002256 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
Romain Guycbb89e42009-06-08 15:52:54 -07002258
Joe Onorato9c1289c2009-08-17 11:03:03 -04002259 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002260 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002261 }
2262
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002263 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002265 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002266 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002269 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002270 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002271 }
2272
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002273 /**
2274 * Registers various content observers. The current implementation registers
2275 * only a favorites observer to keep track of the favorites applications.
2276 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002277 private void registerContentObservers() {
2278 ContentResolver resolver = getContentResolver();
2279 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2280 true, mWidgetObserver);
2281 }
2282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 @Override
2284 public boolean dispatchKeyEvent(KeyEvent event) {
2285 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2286 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002288 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002289 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002290 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002291 dumpState();
2292 return true;
2293 }
2294 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002295 }
2296 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2297 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002298 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 return true;
2300 }
2301 }
2302
2303 return super.dispatchKeyEvent(event);
2304 }
2305
Joe Onorato88ec0992009-11-19 13:16:06 -08002306 @Override
2307 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002308 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002309 if (mAppsCustomizeContent.getContentType() ==
2310 AppsCustomizePagedView.ContentType.Applications) {
2311 showWorkspace(true);
2312 } else {
2313 showOverviewMode(true);
2314 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002315 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002316 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002317 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002318 Folder openFolder = mWorkspace.getOpenFolder();
2319 if (openFolder.isEditingName()) {
2320 openFolder.dismissEditingName();
2321 } else {
2322 closeFolder();
2323 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002324 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002325 mWorkspace.exitWidgetResizeMode();
2326
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002327 // Back button is a no-op here, but give at least some feedback for the button press
2328 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002329 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002330 }
2331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002333 * Re-listen when widgets are reset.
2334 */
2335 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002336 if (mAppWidgetHost != null) {
2337 mAppWidgetHost.startListening();
2338 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002339 }
2340
2341 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 * Launches the intent referred by the clicked shortcut.
2343 *
2344 * @param v The view representing the clicked shortcut.
2345 */
2346 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002347 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2348 // view has detached (it's possible for this to happen if the view is removed mid touch).
2349 if (v.getWindowToken() == null) {
2350 return;
2351 }
2352
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002353 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002354 return;
2355 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002356
Adam Cohen1697b792013-09-17 19:08:21 -07002357 if (v instanceof Workspace) {
2358 if (mWorkspace.isInOverviewMode()) {
2359 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002360 }
2361 return;
2362 }
2363
2364 if (v instanceof CellLayout) {
2365 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002366 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002367 }
2368 }
2369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002371 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002373 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2374 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002375
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002376 // Check for special shortcuts
2377 if (intent.getComponent() != null) {
2378 final String shortcutClass = intent.getComponent().getClassName();
2379
2380 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002381 onClickAddWidgetButton();
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002382 return;
2383 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2384 MemoryDumpActivity.startDump(this);
2385 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002386 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2387 toggleShowWeightWatcher();
2388 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002389 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002390 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002391
2392 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002393 int[] pos = new int[2];
2394 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002395 intent.setSourceBounds(new Rect(pos[0], pos[1],
2396 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002397
2398 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002399
Daniel Sandlerff02d492013-08-05 02:12:05 -04002400 mStats.recordLaunch(intent, shortcut);
2401
Michael Jurkaddd62e92011-02-16 17:49:14 -08002402 if (success && v instanceof BubbleTextView) {
2403 mWaitingForResume = (BubbleTextView) v;
2404 mWaitingForResume.setStayPressed(true);
2405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002407 if (v instanceof FolderIcon) {
2408 FolderIcon fi = (FolderIcon) v;
2409 handleFolderClick(fi);
2410 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002411 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002412 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002413 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002414 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002415 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
2418 }
2419
Michael Jurka0e260592010-06-30 17:07:39 -07002420 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002421 return false;
2422 }
2423
Michael Jurkaaf442092010-06-10 17:01:57 -07002424 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002425 * Event handler for the search button
2426 *
2427 * @param v The view that was clicked.
2428 */
2429 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002430 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2431
Amith Yamasania135ba82011-08-09 17:42:01 -07002432 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002433 }
2434
2435 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002436 * Event handler for the voice button
2437 *
2438 * @param v The view that was clicked.
2439 */
2440 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002441 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002442
Bjorn Bringertc459e522013-06-07 19:36:01 +01002443 startVoice();
2444 }
2445
2446 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002447 try {
2448 final SearchManager searchManager =
2449 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2450 ComponentName activityName = searchManager.getGlobalSearchActivity();
2451 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002452 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002453 if (activityName != null) {
2454 intent.setPackage(activityName.getPackageName());
2455 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002456 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002457 } catch (ActivityNotFoundException e) {
2458 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002459 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002460 startActivitySafely(null, intent, "onClickVoiceButton");
2461 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002462 }
2463
2464 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002465 * Event handler for the "grid" button that appears on the home screen, which
2466 * enters all apps mode.
2467 *
2468 * @param v The view that was clicked.
2469 */
2470 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002471 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002472 }
2473
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002474 /**
2475 * Event handler for the (Add) Widgets button that appears after a long press
2476 * on the home screen.
2477 */
2478 protected void onClickAddWidgetButton() {
2479 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2480 }
2481
Michael Jurka5130e402011-10-13 04:55:35 -07002482 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002483 // Provide the same haptic feedback that the system offers for virtual keys.
2484 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002485 }
2486
Adam Cohen61f560d2013-09-30 15:58:20 -07002487 public void performHapticFeedbackOnTouchDown(View v) {
2488 // Provide the same haptic feedback that the system offers for virtual keys.
2489 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2490 }
2491
2492 public View.OnTouchListener getHapticFeedbackTouchListener() {
2493 if (mHapticFeedbackTouchListener == null) {
2494 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2495 @Override
2496 public boolean onTouch(View v, MotionEvent event) {
2497 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2498 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2499 }
2500 return false;
2501 }
2502 };
2503 }
2504 return mHapticFeedbackTouchListener;
2505 }
2506
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002507 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002508 if (!DISABLE_MARKET_BUTTON) {
2509 if (mAppMarketIntent != null) {
2510 startActivitySafely(v, mAppMarketIntent, "app market");
2511 } else {
2512 Log.e(TAG, "Invalid app market intent.");
2513 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002514 }
2515 }
2516
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002517 /**
2518 * Called when the user stops interacting with the launcher.
2519 * This implies that the user is now on the homescreen and is not doing housekeeping.
2520 */
2521 protected void onInteractionEnd() {}
2522
2523 /**
2524 * Called when the user starts interacting with the launcher.
2525 * The possible interactions are:
2526 * - open all apps
2527 * - reorder an app shortcut, or a widget
2528 * - open the overview mode.
2529 * This is a good time to stop doing things that only make sense
2530 * when the user is on the homescreen and not doing housekeeping.
2531 */
2532 protected void onInteractionBegin() {}
2533
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002534 void startApplicationDetailsActivity(ComponentName componentName) {
2535 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002536 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2537 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002538 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2539 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002540 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002541 }
2542
Michael Jurka1e2f4652013-07-08 18:03:46 -07002543 // returns true if the activity was started
2544 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002545 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002546 // System applications cannot be installed. For now, show a toast explaining that.
2547 // We may give them the option of disabling apps this way.
2548 int messageId = R.string.uninstall_system_app_text;
2549 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002550 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002551 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002552 String packageName = componentName.getPackageName();
2553 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002554 Intent intent = new Intent(
2555 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002556 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2557 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002558 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002559 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002560 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002561 }
2562
Michael Jurka86a720e2012-05-09 11:23:23 -07002563 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002565
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002567 // Only launch using the new animation if the shortcut has not opted out (this is a
2568 // private contract between launcher and may be ignored in the future).
2569 boolean useLaunchAnimation = (v != null) &&
2570 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2571 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002572 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2573 v.getMeasuredWidth(), v.getMeasuredHeight());
2574
2575 startActivity(intent, opts.toBundle());
2576 } else {
2577 startActivity(intent);
2578 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002579 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 } catch (SecurityException e) {
2581 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002582 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002584 "or use the exported attribute for this activity. "
2585 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002586 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002587 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002589
Michael Jurka86a720e2012-05-09 11:23:23 -07002590 boolean startActivitySafely(View v, Intent intent, Object tag) {
2591 boolean success = false;
2592 try {
2593 success = startActivity(v, intent, tag);
2594 } catch (ActivityNotFoundException e) {
2595 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2596 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2597 }
2598 return success;
2599 }
2600
Adam Cohena9cf38f2011-05-02 15:36:58 -07002601 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002602 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002603 Folder openFolder = mWorkspace.getFolderForTag(info);
2604
2605 // If the folder info reports that the associated folder is open, then verify that
2606 // it is actually opened. There have been a few instances where this gets out of sync.
2607 if (info.opened && openFolder == null) {
2608 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002609 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002610 info.opened = false;
2611 }
2612
Adam Cohenfb91f302012-06-11 15:45:18 -07002613 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 // Close any open folder
2615 closeFolder();
2616 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002617 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002618 } else {
2619 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002620 int folderScreen;
2621 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002622 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 // .. and close it
2624 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002625 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 // Close any folder open on the current screen
2627 closeFolder();
2628 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002629 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002630 }
2631 }
2632 }
2633 }
2634
Adam Cohen268c4752012-06-06 17:47:33 -07002635 /**
2636 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2637 * in the DragLayer in the exact absolute location of the original FolderIcon.
2638 */
2639 private void copyFolderIconToImage(FolderIcon fi) {
2640 final int width = fi.getMeasuredWidth();
2641 final int height = fi.getMeasuredHeight();
2642
2643 // Lazy load ImageView, Bitmap and Canvas
2644 if (mFolderIconImageView == null) {
2645 mFolderIconImageView = new ImageView(this);
2646 }
2647 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2648 mFolderIconBitmap.getHeight() != height) {
2649 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2650 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2651 }
2652
2653 DragLayer.LayoutParams lp;
2654 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2655 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2656 } else {
2657 lp = new DragLayer.LayoutParams(width, height);
2658 }
2659
Adam Cohen307fe232012-08-16 17:55:58 -07002660 // The layout from which the folder is being opened may be scaled, adjust the starting
2661 // view size by this scale factor.
2662 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002663 lp.customPosition = true;
2664 lp.x = mRectForFolderAnimation.left;
2665 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002666 lp.width = (int) (scale * width);
2667 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002668
2669 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2670 fi.draw(mFolderIconCanvas);
2671 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002672 if (fi.getFolder() != null) {
2673 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2674 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002675 }
Adam Cohen268c4752012-06-06 17:47:33 -07002676 // Just in case this image view is still in the drag layer from a previous animation,
2677 // we remove it and re-add it.
2678 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2679 mDragLayer.removeView(mFolderIconImageView);
2680 }
2681 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002682 if (fi.getFolder() != null) {
2683 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002684 }
Adam Cohen268c4752012-06-06 17:47:33 -07002685 }
2686
Adam Cohen2801caf2011-05-13 20:57:39 -07002687 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002688 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002689 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2690 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2691 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2692
Adam Cohenc51934b2011-07-26 21:07:43 -07002693 FolderInfo info = (FolderInfo) fi.getTag();
2694 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2695 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002696 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2697 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002698 }
2699
Adam Cohen268c4752012-06-06 17:47:33 -07002700 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2701 copyFolderIconToImage(fi);
2702 fi.setVisibility(View.INVISIBLE);
2703
Michael Jurka2ecf9952012-06-18 12:52:28 -07002704 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002705 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002706 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2707 oa.start();
2708 }
2709
Adam Cohen268c4752012-06-06 17:47:33 -07002710 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002711 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002712 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2713 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2714 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2715
Adam Cohen268c4752012-06-06 17:47:33 -07002716 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002717
Adam Cohen268c4752012-06-06 17:47:33 -07002718 // We remove and re-draw the FolderIcon in-case it has changed
2719 mDragLayer.removeView(mFolderIconImageView);
2720 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002721 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002722 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002723 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002724 oa.addListener(new AnimatorListenerAdapter() {
2725 @Override
2726 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002727 if (cl != null) {
2728 cl.clearFolderLeaveBehind();
2729 // Remove the ImageView copy of the FolderIcon and make the original visible.
2730 mDragLayer.removeView(mFolderIconImageView);
2731 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002732 }
2733 }
2734 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002735 oa.start();
2736 }
2737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002739 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 * is animated relative to the specified View. If the View is null, no animation
2741 * is played.
2742 *
2743 * @param folderInfo The FolderInfo describing the folder to open.
2744 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002745 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002746 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002747 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748
Adam Cohena9cf38f2011-05-02 15:36:58 -07002749 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750
Adam Cohen4554ee12011-08-03 16:13:21 -07002751 // Just verify that the folder hasn't already been added to the DragLayer.
2752 // There was a one-off crash where the folder had a parent already.
2753 if (folder.getParent() == null) {
2754 mDragLayer.addView(folder);
2755 mDragController.addDropTarget((DropTarget) folder);
2756 } else {
2757 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2758 folder.getParent() + ").");
2759 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002760 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002761 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002762
2763 // Notify the accessibility manager that this folder "window" has appeared and occluded
2764 // the workspace items
2765 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2766 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002767 }
2768
2769 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002770 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002771 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002772 if (folder.isEditingName()) {
2773 folder.dismissEditingName();
2774 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002775 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002776
2777 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002778 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002779 }
2780 }
2781
2782 void closeFolder(Folder folder) {
2783 folder.getInfo().opened = false;
2784
2785 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2786 if (parent != null) {
2787 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2788 shrinkAndFadeInFolderIcon(fi);
2789 }
2790 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002791
2792 // Notify the accessibility manager that this folder "window" has disappeard and no
2793 // longer occludeds the workspace items
2794 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002795 }
2796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002798 if (!isDraggingEnabled()) return false;
2799 if (isWorkspaceLocked()) return false;
2800 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801
Adam Cohen1697b792013-09-17 19:08:21 -07002802 if (v instanceof Workspace) {
2803 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002804 if (mWorkspace.enterOverviewMode()) {
2805 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2806 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2807 return true;
2808 } else {
2809 return false;
2810 }
Adam Cohen1697b792013-09-17 19:08:21 -07002811 }
Adam Cohen1697b792013-09-17 19:08:21 -07002812 }
2813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002815 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816 }
2817
Michael Jurka0280c3b2010-09-17 15:00:07 -07002818 resetAddInfo();
2819 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002820 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002821 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002822 return true;
2823 }
2824
Winson Chung3d503fb2011-07-13 17:25:49 -07002825 // The hotseat touch handling does not go through Workspace, and we always allow long press
2826 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002827 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002828 final boolean inHotseat = isHotseatLayout(v);
2829 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002830 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002831 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002832 // User long pressed on empty space
2833 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2834 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2835 // Disabling reordering until we sort out some issues.
2836 if (mWorkspace.isInOverviewMode()) {
2837 mWorkspace.startReordering(v);
2838 } else {
2839 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002842 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2843 mHotseat.getOrderInHotseat(
2844 longClickCellInfo.cellX,
2845 longClickCellInfo.cellY));
2846 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002848 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 }
2850 }
2851 }
2852 return true;
2853 }
2854
Winson Chung3d503fb2011-07-13 17:25:49 -07002855 boolean isHotseatLayout(View layout) {
2856 return mHotseat != null && layout != null &&
2857 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2858 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002859
Winson Chung3d503fb2011-07-13 17:25:49 -07002860 /**
2861 * Returns the CellLayout of the specified container at the specified screen.
2862 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002863 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002864 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2865 if (mHotseat != null) {
2866 return mHotseat.getLayout();
2867 } else {
2868 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002869 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002870 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002871 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002872 }
2873 }
2874
Daniel Sandler843e8602010-06-07 14:59:01 -04002875 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002876 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002877 }
2878
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002879 /**
2880 * Helper method for the cameraZoomIn/cameraZoomOut animations
2881 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002882 * @param scaleFactor The scale factor used for the zoom
2883 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002884 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002885 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002886 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002887 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002888
Craig Mautner360310b2012-10-26 15:13:08 -07002889 private void setWorkspaceBackground(boolean workspace) {
2890 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002891 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002892 }
2893
Selim Cinekf3a8e8c2014-01-16 10:38:38 -08002894 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002895 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2896 int curflags = getWindow().getAttributes().flags
2897 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2898 if (wpflags != curflags) {
2899 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2900 }
Craig Mautner360310b2012-10-26 15:13:08 -07002901 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002902 }
2903
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002904 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2905 if (v instanceof LauncherTransitionable) {
2906 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2907 }
2908 }
2909
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2911 if (v instanceof LauncherTransitionable) {
2912 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2913 }
Winson Chung70442722012-02-10 15:43:22 -08002914
2915 // Update the workspace transition step as well
2916 dispatchOnLauncherTransitionStep(v, 0f);
2917 }
2918
2919 private void dispatchOnLauncherTransitionStep(View v, float t) {
2920 if (v instanceof LauncherTransitionable) {
2921 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2922 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002923 }
2924
2925 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2926 if (v instanceof LauncherTransitionable) {
2927 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2928 }
Winson Chung70442722012-02-10 15:43:22 -08002929
2930 // Update the workspace transition step as well
2931 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002932 }
2933
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002934 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002935 * Things to test when changing the following seven functions.
2936 * - Home from workspace
2937 * - from center screen
2938 * - from other screens
2939 * - Home from all apps
2940 * - from center screen
2941 * - from other screens
2942 * - Back from all apps
2943 * - from center screen
2944 * - from other screens
2945 * - Launch app from workspace and quit
2946 * - with back
2947 * - with home
2948 * - Launch app from all apps and quit
2949 * - with back
2950 * - with home
2951 * - Go to a screen that's not the default, then all
2952 * apps, and launch and app, and go back
2953 * - with back
2954 * -with home
2955 * - On workspace, long press power and go back
2956 * - with back
2957 * - with home
2958 * - On all apps, long press power and go back
2959 * - with back
2960 * - with home
2961 * - On workspace, power off
2962 * - On all apps, power off
2963 * - Launch an app and turn off the screen while in that app
2964 * - Go back with home key
2965 * - Go back with back key TODO: make this not go to workspace
2966 * - From all apps
2967 * - From workspace
2968 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2969 * - From all apps
2970 * - From the center workspace
2971 * - From another workspace
2972 */
2973
2974 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002975 * Zoom the camera out from the workspace to reveal 'toView'.
2976 * Assumes that the view to show is anchored at either the very top or very bottom
2977 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002978 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002979 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002980 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2981 showAppsCustomizeHelper(animated, springLoaded, contentType);
2982 }
2983 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2984 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002985 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002986 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 mStateAnimation.cancel();
2988 mStateAnimation = null;
2989 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002990 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002991
Winson Chungf0ea4d32011-06-06 14:27:16 -07002992 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2993 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2994 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002995 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002996 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002997 final int startDelay =
2998 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002999
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003001
Michael Jurkad74c9842011-07-10 12:44:21 -07003002 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003003 Animator workspaceAnim =
3004 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003005 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003006 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3007 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003008 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3009 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003010
3011 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003012 toView.setScaleX(scale);
3013 toView.setScaleY(scale);
3014 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3015 scaleAnim.
3016 scaleX(1f).scaleY(1f).
3017 setDuration(duration).
3018 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003019
Winson Chungf0ea4d32011-06-06 14:27:16 -07003020 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003021 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003022 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003023 .ofFloat(toView, "alpha", 0f, 1f)
3024 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003026 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3027 @Override
3028 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003029 if (animation == null) {
3030 throw new RuntimeException("animation is null");
3031 }
Winson Chung70442722012-02-10 15:43:22 -08003032 float t = (Float) animation.getAnimatedValue();
3033 dispatchOnLauncherTransitionStep(fromView, t);
3034 dispatchOnLauncherTransitionStep(toView, t);
3035 }
3036 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003037
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003038 // toView should appear right at the end of the workspace shrink
3039 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003040 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003041 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003042 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003043
3044 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003045 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003046 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003047 // Prepare the position
3048 toView.setTranslationX(0.0f);
3049 toView.setTranslationY(0.0f);
3050 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003051 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003052 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003053 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003054 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003055 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3056 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003057
Winson Chungc7d2b602012-05-16 17:02:20 -07003058 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003059 if (mSearchDropTargetBar != null) {
3060 mSearchDropTargetBar.hideSearchBar(false);
3061 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003062 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003063 });
3064
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003065 if (workspaceAnim != null) {
3066 mStateAnimation.play(workspaceAnim);
3067 }
Michael Jurka1899a362011-11-03 13:50:45 -07003068
3069 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003070
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003071 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3072 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003073
3074 // If any of the objects being animated haven't been measured/laid out
3075 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003076 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003077 (mWorkspace.getMeasuredWidth() == 0) ||
3078 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003079 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003080 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003081
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003082 final AnimatorSet stateAnimation = mStateAnimation;
3083 final Runnable startAnimRunnable = new Runnable() {
3084 public void run() {
3085 // Check that mStateAnimation hasn't changed while
3086 // we waited for a layout/draw pass
3087 if (mStateAnimation != stateAnimation)
3088 return;
3089 setPivotsForZoom(toView, scale);
3090 dispatchOnLauncherTransitionStart(fromView, animated, false);
3091 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003092 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003093 }
3094 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003095 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003096 final ViewTreeObserver observer = toView.getViewTreeObserver();
3097 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3098 public void onGlobalLayout() {
3099 startAnimRunnable.run();
3100 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3101 }
3102 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003103 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003104 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003105 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003106 } else {
3107 toView.setTranslationX(0.0f);
3108 toView.setTranslationY(0.0f);
3109 toView.setScaleX(1.0f);
3110 toView.setScaleY(1.0f);
3111 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003112 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003113
Daniel Sandlere4f98912013-06-25 15:13:26 -04003114 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003115 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003116 if (mSearchDropTargetBar != null) {
3117 mSearchDropTargetBar.hideSearchBar(false);
3118 }
Michael Jurkaabded662011-03-04 12:06:57 -08003119 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003120 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003121 dispatchOnLauncherTransitionStart(fromView, animated, false);
3122 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003123 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003124 dispatchOnLauncherTransitionStart(toView, animated, false);
3125 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003126 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003127 }
3128
3129 /**
3130 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003131 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003132 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003133 */
Adam Cohened307df2013-10-02 09:37:31 -07003134 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003135 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003136
Michael Jurkab3e22d92011-10-31 15:58:33 -07003137 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003138 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003139 mStateAnimation.cancel();
3140 mStateAnimation = null;
3141 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003142 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003143
Winson Chungf0ea4d32011-06-06 14:27:16 -07003144 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003145 final int fadeOutDuration =
3146 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003147 final float scaleFactor = (float)
3148 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3149 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003150 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003151 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003152 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003153 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3154 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003155 toState, animated, stagger, -1);
3156 } else if (toState == Workspace.State.SPRING_LOADED ||
3157 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003158 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003159 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003160 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003161
Michael Jurkab3e22d92011-10-31 15:58:33 -07003162 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003163 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003164 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003165 final LauncherViewPropertyAnimator scaleAnim =
3166 new LauncherViewPropertyAnimator(fromView);
3167 scaleAnim.
3168 scaleX(scaleFactor).scaleY(scaleFactor).
3169 setDuration(duration).
3170 setInterpolator(new Workspace.ZoomInInterpolator());
3171
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003172 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003173 .ofFloat(fromView, "alpha", 1f, 0f)
3174 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003175 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003176 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3177 @Override
3178 public void onAnimationUpdate(ValueAnimator animation) {
3179 float t = 1f - (Float) animation.getAnimatedValue();
3180 dispatchOnLauncherTransitionStep(fromView, t);
3181 dispatchOnLauncherTransitionStep(toView, t);
3182 }
3183 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003184
Michael Jurka2ecf9952012-06-18 12:52:28 -07003185 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003186
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003187 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3188 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003189 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003190
3191 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003192 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003193 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003194 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003195 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3196 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003197 if (onCompleteRunnable != null) {
3198 onCompleteRunnable.run();
3199 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003200 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003201 }
3202 });
3203
Winson Chungf0ea4d32011-06-06 14:27:16 -07003204 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003205 if (workspaceAnim != null) {
3206 mStateAnimation.play(workspaceAnim);
3207 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003208 dispatchOnLauncherTransitionStart(fromView, animated, true);
3209 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003210 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003211 } else {
3212 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003213 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003214 dispatchOnLauncherTransitionStart(fromView, animated, true);
3215 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003216 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003217 dispatchOnLauncherTransitionStart(toView, animated, true);
3218 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003219 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003220 }
3221
Michael Jurkae326f182011-11-21 14:05:46 -08003222 @Override
3223 public void onTrimMemory(int level) {
3224 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003225 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003226 mAppsCustomizeTabHost.onTrimMemory();
3227 }
3228 }
3229
Adam Cohened307df2013-10-02 09:37:31 -07003230 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003231 showWorkspace(animated, null);
3232 }
3233
Adam Cohened307df2013-10-02 09:37:31 -07003234 protected void showWorkspace() {
3235 showWorkspace(true);
3236 }
3237
Adam Cohened66b2b2012-01-23 17:28:51 -08003238 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003239 if (mWorkspace.isInOverviewMode()) {
3240 mWorkspace.exitOverviewMode(animated);
3241 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003242 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003243 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003244 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003245 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003246
Winson Chungc7d2b602012-05-16 17:02:20 -07003247 // Show the search bar (only animate if we were showing the drop target bar in spring
3248 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003249 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003250 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003251 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003252
Michael Jurkab3e22d92011-10-31 15:58:33 -07003253 // Set focus to the AppsCustomize button
3254 if (mAllAppsButton != null) {
3255 mAllAppsButton.requestFocus();
3256 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003257 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003258
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003259 // Change the state *after* we've called all the transition code
3260 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003261
Winson Chung5fb63472011-02-02 17:03:37 -08003262 // Resume the auto-advance of widgets
3263 mUserPresent = true;
3264 updateRunning();
3265
alanv1d4fde62012-10-17 13:15:47 -07003266 // Send an accessibility event to announce the context change
3267 getWindow().getDecorView()
3268 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003269
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003270 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003271 }
3272
Adam Cohened307df2013-10-02 09:37:31 -07003273 void showOverviewMode(boolean animated) {
3274 mWorkspace.setVisibility(View.VISIBLE);
3275 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3276 mState = State.WORKSPACE;
3277 onWorkspaceShown(animated);
3278 }
3279
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003280 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003281 }
3282
Winson Chung82963d52013-10-09 11:20:57 -07003283 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3284 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003285 if (mState != State.WORKSPACE) return;
3286
Winson Chung82963d52013-10-09 11:20:57 -07003287 if (resetPageToZero) {
3288 mAppsCustomizeTabHost.reset();
3289 }
Winson Chungc58497e2013-09-03 17:48:37 -07003290 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003291 mAppsCustomizeTabHost.requestFocus();
3292
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293 // Change the state *after* we've called all the transition code
3294 mState = State.APPS_CUSTOMIZE;
3295
3296 // Pause the auto-advance of widgets until we are out of AllApps
3297 mUserPresent = false;
3298 updateRunning();
3299 closeFolder();
3300
3301 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003302 getWindow().getDecorView()
3303 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003304 }
3305
Adam Cohen7777d962011-08-18 18:58:38 -07003306 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003307 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003308 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003309 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003310 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003311 }
Adam Cohen7777d962011-08-18 18:58:38 -07003312
Adam Cohenad4e15c2013-10-17 16:21:35 -07003313 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003314 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003315 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3316
Winson Chunge7a03942011-08-05 15:05:12 -07003317 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003318 @Override
3319 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003320 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003321 // Before we show workspace, hide all apps again because
3322 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3323 // clean up our state transition functions
3324 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003325 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003326 } else {
3327 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003328 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003329 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003330 }, delay);
3331
Winson Chung557d6ed2011-07-08 15:34:52 -07003332 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003333
Michael Jurkad3ef3062010-11-23 16:23:58 -08003334 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003335 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 final boolean animated = true;
3337 final boolean springLoaded = true;
3338 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003339 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003340 }
3341 // Otherwise, we are not in spring loaded mode, so don't do anything.
3342 }
3343
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 void lockAllApps() {
3345 // TODO
3346 }
3347
3348 void unlockAllApps() {
3349 // TODO
3350 }
3351
Winson Chungf0ea4d32011-06-06 14:27:16 -07003352 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003353 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003354 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003355 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003356 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003357 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003358 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003359 int duration = 0;
3360 if (mSearchDropTargetBar != null) {
3361 duration = mSearchDropTargetBar.getTransitionInDuration();
3362 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003363 mHotseat.animate().alpha(1f).setDuration(duration);
3364 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003365 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003366 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003367 }
3368 }
3369 }
3370
3371 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003372 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003373 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003374 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003375 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003376 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003377 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003378 int duration = 0;
3379 if (mSearchDropTargetBar != null) {
3380 duration = mSearchDropTargetBar.getTransitionOutDuration();
3381 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003382 mHotseat.animate().alpha(0f).setDuration(duration);
3383 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003384 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003385 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003386 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003387 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003388 }
3389
Patrick Dubroy5f445422011-02-18 14:35:21 -08003390 /**
3391 * Add an item from all apps or customize onto the given workspace screen.
3392 * If layout is null, add to the current screen.
3393 */
3394 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003395 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003396 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003397 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003398 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003399
Winson Chungdff8ebb2011-09-08 17:25:31 -07003400 /** Maps the current orientation to an index for referencing orientation correct global icons */
3401 private int getCurrentOrientationIndexForGlobalIcons() {
3402 // default - 0, landscape - 1
3403 switch (getResources().getConfiguration().orientation) {
3404 case Configuration.ORIENTATION_LANDSCAPE:
3405 return 1;
3406 default:
3407 return 0;
3408 }
3409 }
3410
Michael Jurkaae65ee32012-04-30 11:45:54 -07003411 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003412 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003413 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003414 // Look for the toolbar icon specified in the activity meta-data
3415 Bundle metaData = packageManager.getActivityInfo(
3416 activityName, PackageManager.GET_META_DATA).metaData;
3417 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003418 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003419 if (iconResId != 0) {
3420 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003421 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003422 }
3423 }
3424 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003425 // This can happen if the activity defines an invalid drawable
3426 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3427 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003428 } catch (Resources.NotFoundException nfe) {
3429 // This can happen if the activity defines an invalid drawable
3430 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3431 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003432 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003433 return null;
3434 }
3435
3436 // if successful in getting icon, return it; otherwise, set button to use default drawable
3437 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003438 int buttonId, ComponentName activityName, int fallbackDrawableId,
3439 String toolbarResourceName) {
3440 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003441 Resources r = getResources();
3442 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3443 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003444
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003445 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003446 // If we were unable to find the icon via the meta-data, use a generic one
3447 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003448 toolbarIcon = r.getDrawable(fallbackDrawableId);
3449 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003450 if (button != null) {
3451 button.setCompoundDrawables(toolbarIcon, null, null, null);
3452 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003453 return null;
3454 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003455 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003456 if (button != null) {
3457 button.setCompoundDrawables(toolbarIcon, null, null, null);
3458 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003459 return toolbarIcon.getConstantState();
3460 }
3461 }
3462
3463 // if successful in getting icon, return it; otherwise, set button to use default drawable
3464 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003465 int buttonId, ComponentName activityName, int fallbackDrawableId,
3466 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003467 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003468 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003469
Michael Jurka19e0fc52011-07-22 18:00:21 -07003470 if (button != null) {
3471 // If we were unable to find the icon via the meta-data, use a
3472 // generic one
3473 if (toolbarIcon == null) {
3474 button.setImageResource(fallbackDrawableId);
3475 } else {
3476 button.setImageDrawable(toolbarIcon);
3477 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003478 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003479
3480 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3481
Michael Jurka0423dcf2010-10-05 14:56:18 -07003482 }
3483
Winson Chung1b884092012-06-08 17:13:02 -07003484 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003485 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003486 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003487 }
3488
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003489 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003490 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003491 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003492 }
3493
Winson Chungbb185bd2011-11-21 12:31:42 -08003494 private void invalidatePressedFocusedStates(View container, View button) {
3495 if (container instanceof HolographicLinearLayout) {
3496 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3497 layout.invalidatePressedFocusedStates();
3498 } else if (button instanceof HolographicImageView) {
3499 HolographicImageView view = (HolographicImageView) button;
3500 view.invalidatePressedFocusedStates();
3501 }
3502 }
3503
Cristina Stancu476493b2013-08-07 17:20:14 +01003504 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003505 if (mQsb == null) {
3506 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3507 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003508 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003509 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003510 }
3511
3512 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003513 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003514 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003515 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003516 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003517
Winson Chung1cad91e2011-05-25 17:41:01 -07003518 final SearchManager searchManager =
3519 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3520 ComponentName activityName = searchManager.getGlobalSearchActivity();
3521 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003522 int coi = getCurrentOrientationIndexForGlobalIcons();
3523 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003524 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3525 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3526 if (sGlobalSearchIcon[coi] == null) {
3527 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3528 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3529 TOOLBAR_ICON_METADATA_NAME);
3530 }
3531
Winson Chungc51db6a2011-10-05 11:44:49 -07003532 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3533 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003534 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003535 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003536 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003537 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003538 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3539 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003540 if (searchButton != null) searchButton.setVisibility(View.GONE);
3541 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003542 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003543 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003544 }
3545 }
3546
Cristina Stancu476493b2013-08-07 17:20:14 +01003547 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003548 final View searchButtonContainer = findViewById(R.id.search_button_container);
3549 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003550 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003551 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003552 }
3553
Cristina Stancu476493b2013-08-07 17:20:14 +01003554 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003555 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003556 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003557
Winson Chungc51db6a2011-10-05 11:44:49 -07003558 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003559 final SearchManager searchManager =
3560 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3561 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3562
3563 ComponentName activityName = null;
3564 if (globalSearchActivity != null) {
3565 // Check if the global search activity handles voice search
3566 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3567 intent.setPackage(globalSearchActivity.getPackageName());
3568 activityName = intent.resolveActivity(getPackageManager());
3569 }
3570
3571 if (activityName == null) {
3572 // Fallback: check if an activity other than the global search activity
3573 // resolves this
3574 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3575 activityName = intent.resolveActivity(getPackageManager());
3576 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003577 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003578 int coi = getCurrentOrientationIndexForGlobalIcons();
3579 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003580 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3581 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3582 if (sVoiceSearchIcon[coi] == null) {
3583 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3584 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3585 TOOLBAR_ICON_METADATA_NAME);
3586 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003587 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003588 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003589 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003590 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003591 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003592 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003593 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003594 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003595 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003596 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003597 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003598 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003599
Cristina Stancu476493b2013-08-07 17:20:14 +01003600 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003601 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3602 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003603 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003604 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003605 }
3606
Winson Chung5841efa2013-09-30 18:06:44 -07003607 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003608 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3609 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003610 boolean visible = !forceDisableVoiceButtonProxy &&
3611 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003612 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003613 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003614 }
3615 }
Winson Chung5841efa2013-09-30 18:06:44 -07003616
3617 /**
3618 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3619 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3620 */
3621 public void disableVoiceButtonProxy(boolean disabled) {
3622 updateVoiceButtonProxyVisible(disabled);
3623 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003624 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003625 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003626 */
3627 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003628 if (!DISABLE_MARKET_BUTTON) {
3629 final View marketButton = findViewById(R.id.market_button);
3630 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3631 // Find the app market activity by resolving an intent.
3632 // (If multiple app markets are installed, it will return the ResolverActivity.)
3633 ComponentName activityName = intent.resolveActivity(getPackageManager());
3634 if (activityName != null) {
3635 int coi = getCurrentOrientationIndexForGlobalIcons();
3636 mAppMarketIntent = intent;
3637 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3638 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3639 TOOLBAR_ICON_METADATA_NAME);
3640 marketButton.setVisibility(View.VISIBLE);
3641 } else {
3642 // We should hide and disable the view so that we don't try and restore the visibility
3643 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3644 marketButton.setVisibility(View.GONE);
3645 marketButton.setEnabled(false);
3646 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003647 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003648 }
3649
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003650 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003651 if (!DISABLE_MARKET_BUTTON) {
3652 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3653 Resources r = getResources();
3654 Drawable marketIconDrawable = d.newDrawable(r);
3655 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3656 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3657 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003658
Winson Chungd64a6662013-09-30 11:06:59 -07003659 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3660 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003661 }
3662
Michael Jurkad7c28052012-04-27 15:43:36 -07003663 @Override
3664 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003665 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003666 final List<CharSequence> text = event.getText();
3667 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003668 // Populate event with a fake title based on the current state.
3669 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003670 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003671 } else {
3672 text.add(getString(R.string.all_apps_home_button_label));
3673 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003674 return result;
3675 }
3676
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003677 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003678 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003679 */
3680 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3681 @Override
3682 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003683 closeSystemDialogs();
3684 }
3685 }
3686
3687 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003688 * Receives notifications whenever the appwidgets are reset.
3689 */
3690 private class AppWidgetResetObserver extends ContentObserver {
3691 public AppWidgetResetObserver() {
3692 super(new Handler());
3693 }
3694
3695 @Override
3696 public void onChange(boolean selfChange) {
3697 onAppWidgetReset();
3698 }
3699 }
3700
3701 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 * If the activity is currently paused, signal that we need to run the passed Runnable
3703 * in onResume.
3704 *
3705 * This needs to be called from incoming places where resources might have been loaded
3706 * while we are paused. That is becaues the Configuration might be wrong
3707 * when we're not running, and if it comes back to what it was when we
3708 * were paused, we are not restarted.
3709 *
3710 * Implementation of the method from LauncherModel.Callbacks.
3711 *
3712 * @return true if we are currently paused. The caller might be able to
3713 * skip some work in that case since we will come back again.
3714 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003715 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003716 if (mPaused) {
3717 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003718 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003719 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003720 }
3721 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003722 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003723 return true;
3724 } else {
3725 return false;
3726 }
3727 }
3728
Michael Jurkac402cd92013-05-20 15:49:32 +02003729 private boolean waitUntilResume(Runnable run) {
3730 return waitUntilResume(run, false);
3731 }
3732
Michael Jurka1e2f4652013-07-08 18:03:46 -07003733 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003734 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003735 }
3736
Michael Jurka7607c2f2013-04-03 14:33:19 -07003737 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003738 * If the activity is currently paused, signal that we need to re-run the loader
3739 * in onResume.
3740 *
3741 * This needs to be called from incoming places where resources might have been loaded
3742 * while we are paused. That is becaues the Configuration might be wrong
3743 * when we're not running, and if it comes back to what it was when we
3744 * were paused, we are not restarted.
3745 *
3746 * Implementation of the method from LauncherModel.Callbacks.
3747 *
3748 * @return true if we are currently paused. The caller might be able to
3749 * skip some work in that case since we will come back again.
3750 */
3751 public boolean setLoadOnResume() {
3752 if (mPaused) {
3753 Log.i(TAG, "setLoadOnResume");
3754 mOnResumeNeedsLoad = true;
3755 return true;
3756 } else {
3757 return false;
3758 }
3759 }
3760
3761 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003763 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003765 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003766 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003767 } else {
3768 return SCREEN_COUNT / 2;
3769 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003770 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003771
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 /**
3773 * Refreshes the shortcuts shown on the workspace.
3774 *
3775 * Implementation of the method from LauncherModel.Callbacks.
3776 */
3777 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003778 mWorkspaceLoading = true;
3779
Michael Jurka7607c2f2013-04-03 14:33:19 -07003780 // If we're starting binding all over again, clear any bind calls we'd postponed in
3781 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3782 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003783 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003784
Winson Chungd64d1762013-08-20 14:37:16 -07003785 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003786 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003787 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003788
Michael Jurka05bf6442011-11-30 20:28:53 -08003789 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003790 if (mHotseat != null) {
3791 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003792 }
3793 }
3794
Adam Cohendcd297f2013-06-18 13:13:40 -07003795 @Override
3796 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003797 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003798
Adam Cohen5084cba2013-09-03 12:01:16 -07003799 // If there are no screens, we need to have an empty screen
3800 if (orderedScreenIds.size() == 0) {
3801 mWorkspace.addExtraEmptyScreen();
3802 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003803
3804 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003805 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003806 if (hasCustomContentToLeft()) {
3807 mWorkspace.createCustomContentContainer();
3808 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003809 }
Winson Chung64359a52013-07-08 17:17:08 -07003810 }
3811
3812 @Override
3813 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003814 // Log to disk
3815 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3816 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3817 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003818 int count = orderedScreenIds.size();
3819 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003820 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003821 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003822 }
3823
Adam Cohen39a06042013-07-19 14:30:12 -07003824 private boolean shouldShowWeightWatcher() {
3825 String spKey = LauncherAppState.getSharedPreferencesKey();
3826 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003827 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003828
3829 return show;
3830 }
3831
3832 private void toggleShowWeightWatcher() {
3833 String spKey = LauncherAppState.getSharedPreferencesKey();
3834 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3835 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3836
3837 show = !show;
3838
3839 SharedPreferences.Editor editor = sp.edit();
3840 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3841 editor.commit();
3842
3843 if (mWeightWatcher != null) {
3844 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3845 }
3846 }
3847
Winson Chungd64d1762013-08-20 14:37:16 -07003848 public void bindAppsAdded(final ArrayList<Long> newScreens,
3849 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003850 final ArrayList<ItemInfo> addAnimated,
3851 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003852 Runnable r = new Runnable() {
3853 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003854 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003855 }
3856 };
3857 if (waitUntilResume(r)) {
3858 return;
3859 }
3860
Winson Chungd64d1762013-08-20 14:37:16 -07003861 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003862 if (newScreens != null) {
3863 bindAddScreens(newScreens);
3864 }
Winson Chungd64d1762013-08-20 14:37:16 -07003865
3866 // We add the items without animation on non-visible pages, and with
3867 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003868 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003869 bindItems(addNotAnimated, 0,
3870 addNotAnimated.size(), false);
3871 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003872 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003873 bindItems(addAnimated, 0,
3874 addAnimated.size(), true);
3875 }
Winson Chungc58497e2013-09-03 17:48:37 -07003876
Winson Chung87412982013-10-03 18:34:14 -07003877 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003878 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003879
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003880 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003881 addedApps != null && mAppsCustomizeContent != null) {
3882 mAppsCustomizeContent.addApps(addedApps);
3883 }
Winson Chungd64d1762013-08-20 14:37:16 -07003884 }
3885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003886 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 * Bind the items start-end from the list.
3888 *
3889 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003890 */
Winson Chung64359a52013-07-08 17:17:08 -07003891 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3892 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003893 Runnable r = new Runnable() {
3894 public void run() {
3895 bindItems(shortcuts, start, end, forceAnimateIcons);
3896 }
3897 };
3898 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return;
3900 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003901
Winson Chungf0c6ae02012-03-21 16:10:31 -07003902 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003903 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3904 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003905 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003906 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003907 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003908 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003909 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003910
3911 // Short circuit if we are loading dock items for a configuration which has no dock
3912 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3913 mHotseat == null) {
3914 continue;
3915 }
3916
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 switch (item.itemType) {
3918 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3919 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003921 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003922
Winson Chung64359a52013-07-08 17:17:08 -07003923 /*
3924 * TODO: FIX collision case
3925 */
Winson Chungce376632013-07-11 16:12:41 -07003926 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3927 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3928 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3929 throw new RuntimeException("OCCUPIED");
3930 }
Winson Chung64359a52013-07-08 17:17:08 -07003931 }
3932
Adam Cohendcd297f2013-06-18 13:13:40 -07003933 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3934 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003935 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003936 // Animate all the applications up now
3937 shortcut.setAlpha(0f);
3938 shortcut.setScaleX(0f);
3939 shortcut.setScaleY(0f);
3940 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003941 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003943 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003944 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003945 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003946 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003947 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003948 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3949 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003951 default:
3952 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003954 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003955
Winson Chung997a9232013-07-24 15:33:46 -07003956 if (animateIcons) {
3957 // Animate to the correct page
3958 if (newShortcutsScreenId > -1) {
3959 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003960 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003961 final Runnable startBounceAnimRunnable = new Runnable() {
3962 public void run() {
3963 anim.playTogether(bounceAnims);
3964 anim.start();
3965 }
3966 };
Winson Chung997a9232013-07-24 15:33:46 -07003967 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003968 // We post the animation slightly delayed to prevent slowdowns
3969 // when we are loading right after we return to launcher.
3970 mWorkspace.postDelayed(new Runnable() {
3971 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003972 if (mWorkspace != null) {
3973 mWorkspace.snapToPage(newScreenIndex);
3974 mWorkspace.postDelayed(startBounceAnimRunnable,
3975 NEW_APPS_ANIMATION_DELAY);
3976 }
Winson Chung94d67682013-09-25 16:29:40 -07003977 }
3978 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003979 } else {
3980 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003981 }
3982 }
Winson Chung64359a52013-07-08 17:17:08 -07003983 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003985 }
3986
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 /**
3988 * Implementation of the method from LauncherModel.Callbacks.
3989 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003990 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003991 Runnable r = new Runnable() {
3992 public void run() {
3993 bindFolders(folders);
3994 }
3995 };
3996 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003997 return;
3998 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003999 sFolders.clear();
4000 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002
4003 /**
4004 * Add the views for a widget to the workspace.
4005 *
4006 * Implementation of the method from LauncherModel.Callbacks.
4007 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004008 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004009 Runnable r = new Runnable() {
4010 public void run() {
4011 bindAppWidget(item);
4012 }
4013 };
4014 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004015 return;
4016 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004017
Daniel Sandler843e8602010-06-07 14:59:01 -04004018 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4019 if (DEBUG_WIDGETS) {
4020 Log.d(TAG, "bindAppWidget: " + item);
4021 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004022 final Workspace workspace = mWorkspace;
4023
4024 final int appWidgetId = item.appWidgetId;
4025 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004026 if (DEBUG_WIDGETS) {
4027 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4028 }
4029
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4031
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004033 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034
Adam Cohendcd297f2013-06-18 13:13:40 -07004035 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004037 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4038
Joe Onorato9c1289c2009-08-17 11:03:03 -04004039 workspace.requestLayout();
4040
Daniel Sandler843e8602010-06-07 14:59:01 -04004041 if (DEBUG_WIDGETS) {
4042 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4043 + (SystemClock.uptimeMillis()-start) + "ms");
4044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 }
4046
Adam Cohen1462de32012-07-24 22:34:36 -07004047 public void onPageBoundSynchronously(int page) {
4048 mSynchronouslyBoundPages.add(page);
4049 }
4050
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 /**
4052 * Callback saying that there aren't any more items to bind.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Adam Cohene25af792013-06-06 23:08:25 -07004056 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004057 Runnable r = new Runnable() {
4058 public void run() {
4059 finishBindingItems(upgradePath);
4060 }
4061 };
4062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004063 return;
4064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 if (mSavedState != null) {
4066 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004067 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004069 mSavedState = null;
4070 }
4071
Adam Cohen1462de32012-07-24 22:34:36 -07004072 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004074 // If we received the result of any pending adds while the loader was running (e.g. the
4075 // widget configuration forced an orientation change), process them now.
4076 for (int i = 0; i < sPendingAddList.size(); i++) {
4077 completeAdd(sPendingAddList.get(i));
4078 }
4079 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080
Winson Chungc51db6a2011-10-05 11:44:49 -07004081 // Update the market app icon as necessary (the other icons will be managed in response to
4082 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004083 if (!DISABLE_MARKET_BUTTON) {
4084 updateAppMarketIcon();
4085 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004086
Winson Chungf0c6ae02012-03-21 16:10:31 -07004087 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004088 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004089 mWorkspace.getUniqueComponents(true, null);
4090 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004091 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004092 }
4093
Winson Chunga0b7e862013-09-05 16:03:15 -07004094 public boolean isAllAppsButtonRank(int rank) {
4095 if (mHotseat != null) {
4096 return mHotseat.isAllAppsButtonRank(rank);
4097 }
4098 return false;
4099 }
4100
Winson Chunga2413752012-04-03 14:22:34 -07004101 private boolean canRunNewAppsAnimation() {
4102 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4103 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4104 }
4105
Winson Chungc9168342013-06-26 14:54:55 -07004106 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4107 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4108 PropertyValuesHolder.ofFloat("alpha", 1f),
4109 PropertyValuesHolder.ofFloat("scaleX", 1f),
4110 PropertyValuesHolder.ofFloat("scaleY", 1f));
4111 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4112 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4113 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4114 return bounceAnim;
4115 }
4116
Adam Cohen27772732013-11-11 14:00:56 +00004117 public boolean useVerticalBarLayout() {
4118 return LauncherAppState.getInstance().getDynamicGrid().
4119 getDeviceProfile().isVerticalBarLayout();
4120 }
4121
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004122 protected Rect getSearchBarBounds() {
4123 return LauncherAppState.getInstance().getDynamicGrid().
4124 getDeviceProfile().getSearchBarBounds();
4125 }
4126
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004127 @Override
4128 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004129 boolean searchVisible = updateGlobalSearchIcon();
4130 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004131 if (mSearchDropTargetBar != null) {
4132 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4133 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004134 }
4135
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004136 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137 * Add the icons for all apps.
4138 *
4139 * Implementation of the method from LauncherModel.Callbacks.
4140 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004141 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004142 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004143 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4144 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4145 getHotseat().addAllAppsFolder(mIconCache, apps,
4146 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4147 }
4148 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004149 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004150 if (mAppsCustomizeContent != null) {
4151 mAppsCustomizeContent.onPackagesUpdated(
4152 LauncherModel.getSortedWidgetsAndShortcuts(this));
4153 }
Winson Chungc58497e2013-09-03 17:48:37 -07004154 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004155 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004156 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004157 mAppsCustomizeContent.onPackagesUpdated(
4158 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004159 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004161 }
4162
4163 /**
4164 * A package was updated.
4165 *
4166 * Implementation of the method from LauncherModel.Callbacks.
4167 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004168 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004169 Runnable r = new Runnable() {
4170 public void run() {
4171 bindAppsUpdated(apps);
4172 }
4173 };
4174 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004175 return;
4176 }
4177
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004178 if (mWorkspace != null) {
4179 mWorkspace.updateShortcuts(apps);
4180 }
Winson Chungc58497e2013-09-03 17:48:37 -07004181
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004182 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004183 mAppsCustomizeContent != null) {
4184 mAppsCustomizeContent.updateApps(apps);
4185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 }
4187
4188 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004189 * A package was uninstalled. We take both the super set of packageNames
4190 * in addition to specific applications to remove, the reason being that
4191 * this can be called when a package is updated as well. In that scenario,
4192 * we only remove specific components from the workspace, where as
4193 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004194 *
4195 * Implementation of the method from LauncherModel.Callbacks.
4196 */
Winson Chung83892cc2013-05-01 16:53:33 -07004197 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004198 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004199 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004200 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004201 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004202 }
Winson Chungd64d1762013-08-20 14:37:16 -07004203 };
4204 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004205 return;
4206 }
4207
Winson Chungdf95eb12013-10-16 14:57:07 -07004208 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004209 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004210 }
4211 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004212 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004213 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004214
Winson Chunga1820962011-10-03 16:31:06 -07004215 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004216 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004217
Winson Chungdf95eb12013-10-16 14:57:07 -07004218 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004219 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004220 mAppsCustomizeContent != null) {
4221 mAppsCustomizeContent.removeApps(appInfos);
4222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004223 }
Joe Onoratobe386092009-11-17 17:32:16 -08004224
4225 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004226 * A number of packages were updated.
4227 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004228 private ArrayList<Object> mWidgetsAndShortcuts;
4229 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004230 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004231 bindPackagesUpdated(mWidgetsAndShortcuts);
4232 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004233 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004234 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004235 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4236 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4237 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004238 return;
4239 }
4240
Winson Chung64359a52013-07-08 17:17:08 -07004241 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004242 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004243 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004244 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004245 }
4246
Winson Chung400438b2011-01-16 17:53:48 -08004247 private int mapConfigurationOriActivityInfoOri(int configOri) {
4248 final Display d = getWindowManager().getDefaultDisplay();
4249 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4250 switch (d.getRotation()) {
4251 case Surface.ROTATION_0:
4252 case Surface.ROTATION_180:
4253 // We are currently in the same basic orientation as the natural orientation
4254 naturalOri = configOri;
4255 break;
4256 case Surface.ROTATION_90:
4257 case Surface.ROTATION_270:
4258 // We are currently in the other basic orientation to the natural orientation
4259 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4260 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4261 break;
4262 }
4263
4264 int[] oriMap = {
4265 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4266 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4267 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4268 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4269 };
4270 // Since the map starts at portrait, we need to offset if this device's natural orientation
4271 // is landscape.
4272 int indexOffset = 0;
4273 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4274 indexOffset = 1;
4275 }
4276 return oriMap[(d.getRotation() + indexOffset) % 4];
4277 }
Adam Cohen446e9402011-09-15 18:21:21 -07004278
Winson Chung4b919f82012-05-01 10:44:08 -07004279 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004280 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004281 getResources().getBoolean(R.bool.allow_rotation);
4282 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004283 }
Winson Chung4b919f82012-05-01 10:44:08 -07004284 public void lockScreenOrientation() {
4285 if (isRotationEnabled()) {
4286 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4287 .getConfiguration().orientation));
4288 }
4289 }
4290 public void unlockScreenOrientation(boolean immediate) {
4291 if (isRotationEnabled()) {
4292 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004293 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004294 } else {
4295 mHandler.postDelayed(new Runnable() {
4296 public void run() {
4297 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4298 }
4299 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004300 }
Winson Chung4b919f82012-05-01 10:44:08 -07004301 }
Winson Chung400438b2011-01-16 17:53:48 -08004302 }
4303
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004304 /**
4305 * Called when the SearchBar hint should be changed.
4306 *
4307 * @param hint the hint to be displayed in the search bar.
4308 */
4309 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004310 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004311 }
4312
Winson Chunge43a1e72014-01-15 10:33:02 -08004313 protected boolean isLauncherPreinstalled() {
4314 PackageManager pm = getPackageManager();
4315 try {
4316 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4317 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4318 return true;
4319 } else {
4320 return false;
4321 }
4322 } catch (NameNotFoundException e) {
4323 e.printStackTrace();
4324 return false;
4325 }
4326 }
4327
Winson Chunge6eabff2013-09-24 11:01:23 -07004328 protected String getFirstRunClingSearchBarHint() {
4329 return "";
4330 }
4331 protected String getFirstRunCustomContentHint() {
4332 return "";
4333 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004334 protected int getFirstRunFocusedHotseatAppDrawableId() {
4335 return -1;
4336 }
4337 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4338 return null;
4339 }
4340 protected int getFirstRunFocusedHotseatAppRank() {
4341 return -1;
4342 }
4343 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4344 return "";
4345 }
4346 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4347 return "";
4348 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004349
Winson Chungaf40f202013-09-18 18:26:31 -07004350 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004351 mLauncherClings.dismissFirstRunCling(v);
4352 }
4353 public void dismissMigrationClingCopyApps(View v) {
4354 mLauncherClings.dismissMigrationClingCopyApps(v);
4355 }
4356 public void dismissMigrationClingUseDefault(View v) {
4357 mLauncherClings.dismissMigrationClingUseDefault(v);
4358 }
4359 public void dismissMigrationWorkspaceCling(View v) {
4360 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004361 }
Winson Chung82f55532011-08-09 14:14:23 -07004362 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004363 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004364 }
4365 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004366 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004367 }
4368
Winson Chunga6945242014-01-08 14:04:34 -08004369 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004370 return !ActivityManager.isRunningInTestHarness() &&
4371 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004372 }
4373
4374 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004375 if (shouldRunFirstRunActivity() &&
4376 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004377 Intent firstRunIntent = getFirstRunActivity();
4378 if (firstRunIntent != null) {
4379 startActivity(firstRunIntent);
4380 markFirstRunActivityShown();
4381 }
4382 }
4383 }
4384
4385 private void markFirstRunActivityShown() {
4386 SharedPreferences.Editor editor = mSharedPrefs.edit();
4387 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4388 editor.apply();
4389 }
4390
4391 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004392 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4393 if (mHotseat != null) mHotseat.setAlpha(1f);
4394 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4395 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004396 }
4397
4398 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004399 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4400 if (mHotseat != null) mHotseat.setAlpha(0f);
4401 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4402 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004403 }
4404
4405
Mathew Inwood72fbec12013-11-19 15:45:07 +00004406 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4407 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4408 if (ri == null) {
4409 return null;
4410 }
4411 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4412 }
4413
4414 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4415 Bitmap icon) {
4416 return new ShortcutInfo(shortcutIntent, caption, icon);
4417 }
4418
4419 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4420 dragView.setTag(dragInfo);
4421 mWorkspace.onDragStartedWithItem(dragView);
4422 mWorkspace.beginDragShared(dragView, source);
4423 }
4424
Winson Chung80baf5a2010-08-09 16:03:15 -07004425 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004426 * Prints out out state for debugging.
4427 */
4428 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004429 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004430 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004431 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4432 Log.d(TAG, "mRestoring=" + mRestoring);
4433 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4434 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004435 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004436 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004437
Winson Chung785d2eb2011-04-14 16:08:02 -07004438 if (mAppsCustomizeContent != null) {
4439 mAppsCustomizeContent.dumpState();
4440 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004441 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004442 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004443
4444 @Override
4445 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4446 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004447 synchronized (sDumpLogs) {
4448 writer.println(" ");
4449 writer.println("Debug logs: ");
4450 for (int i = 0; i < sDumpLogs.size(); i++) {
4451 writer.println(" " + sDumpLogs.get(i));
4452 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004453 }
4454 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004455
4456 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004457 if (DEBUG_DUMP_LOG) {
4458 synchronized (sDumpLogs) {
4459 Log.d(TAG, "");
4460 Log.d(TAG, "*********************");
4461 Log.d(TAG, "Launcher debug logs: ");
4462 for (int i = 0; i < sDumpLogs.size(); i++) {
4463 Log.d(TAG, " " + sDumpLogs.get(i));
4464 }
4465 Log.d(TAG, "*********************");
4466 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004467 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004468 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004469 }
4470
4471 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004472 addDumpLog(tag, log, null, debugLog);
4473 }
4474
4475 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004476 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004477 if (e != null) {
4478 Log.d(tag, log, e);
4479 } else {
4480 Log.d(tag, log);
4481 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004482 }
Winson Chungede41292013-09-19 16:27:36 -07004483 if (DEBUG_DUMP_LOG) {
4484 sDateStamp.setTime(System.currentTimeMillis());
4485 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004486 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4487 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004488 }
Winson Chungede41292013-09-19 16:27:36 -07004489 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004490 }
4491
Winson Chungede41292013-09-19 16:27:36 -07004492 public void dumpLogsToLocalData() {
4493 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004494 new AsyncTask<Void, Void, Void>() {
4495 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004496 boolean success = false;
4497 sDateStamp.setTime(sRunStart);
4498 String FILENAME = sDateStamp.getMonth() + "-"
4499 + sDateStamp.getDay() + "_"
4500 + sDateStamp.getHours() + "-"
4501 + sDateStamp.getMinutes() + "_"
4502 + sDateStamp.getSeconds() + ".txt";
4503
4504 FileOutputStream fos = null;
4505 File outFile = null;
4506 try {
4507 outFile = new File(getFilesDir(), FILENAME);
4508 outFile.createNewFile();
4509 fos = new FileOutputStream(outFile);
4510 } catch (Exception e) {
4511 e.printStackTrace();
4512 }
4513 if (fos != null) {
4514 PrintWriter writer = new PrintWriter(fos);
4515
4516 writer.println(" ");
4517 writer.println("Debug logs: ");
4518 synchronized (sDumpLogs) {
4519 for (int i = 0; i < sDumpLogs.size(); i++) {
4520 writer.println(" " + sDumpLogs.get(i));
4521 }
4522 }
4523 writer.close();
4524 }
4525 try {
4526 if (fos != null) {
4527 fos.close();
4528 success = true;
4529 }
4530 } catch (IOException e) {
4531 e.printStackTrace();
4532 }
Michael Jurka43467462013-11-14 12:03:58 +01004533 return null;
Winson Chungede41292013-09-19 16:27:36 -07004534 }
Michael Jurka43467462013-11-14 12:03:58 +01004535 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004536 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004538}
Michael Jurka2763be32011-02-24 11:19:57 -08004539
Michael Jurkaabded662011-03-04 12:06:57 -08004540interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004541 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004542 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004543 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004544 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004545 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004546}
Dan Sandlerd5024042014-01-09 15:01:33 -05004547
4548interface DebugIntents {
4549 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4550 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004551}