blob: eabffcb4144f7c478decda2524b20f9ec4a874b4 [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;
213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800216 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000218 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
219 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
220
Winson Chunga2413752012-04-03 14:22:34 -0700221 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700222 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
223 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700224 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700225
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800226 private final BroadcastReceiver mCloseSystemDialogsReceiver
227 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private LayoutInflater mInflater;
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800234 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800235 private DragLayer mDragLayer;
236 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700237 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800238 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 private AppWidgetManager mAppWidgetManager;
241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Michael Jurkac9d95c52011-08-29 14:03:34 -0700243 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700244 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private FolderInfo mFolderInfo;
250
Winson Chung3d503fb2011-07-13 17:25:49 -0700251 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700255
Winson Chungc51db6a2011-10-05 11:44:49 -0700256 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700257 private AppsCustomizeTabHost mAppsCustomizeTabHost;
258 private AppsCustomizePagedView mAppsCustomizeContent;
259 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800260 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 private boolean mWorkspaceLoading = true;
271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
279
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // Keep track of whether the user has left launcher
281 private static boolean sPausedFromUserAction = false;
282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private Bundle mSavedInstanceState;
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mUserPresent = true;
288 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700289 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700292 private static LocaleConfiguration sLocaleConfiguration = null;
293
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700294 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700295
Adam Cohen61f560d2013-09-30 15:58:20 -0700296 private View.OnTouchListener mHapticFeedbackTouchListener;
297
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 // Related to the auto-advancing of widgets
299 private final int ADVANCE_MSG = 1;
300 private final int mAdvanceInterval = 20000;
301 private final int mAdvanceStagger = 250;
302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
304 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
305 new HashMap<View, AppWidgetProviderInfo>();
306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
309 private final int mRestoreScreenOrientationDelay = 500;
310
Michael Jurka4ef207b2010-11-29 17:05:45 -0800311 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700312 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
313 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
314 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800315
Winson Chungd64a6662013-09-30 11:06:59 -0700316 private Intent mAppMarketIntent = null;
317 private static final boolean DISABLE_MARKET_BUTTON = true;
318
Craig Mautner360310b2012-10-26 15:13:08 -0700319 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700320
Adam Cohen1462de32012-07-24 22:34:36 -0700321 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700323
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700324 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700325 static Date sDateStamp = new Date();
326 static DateFormat sDateFormat =
327 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
328 static long sRunStart = System.currentTimeMillis();
329 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330
Winson Chung46353de2012-02-16 14:05:10 -0800331 // We only want to get the SharedPreferences once since it does an FS stat each time we get
332 // it from the context.
333 private SharedPreferences mSharedPrefs;
334
Adam Cohene25af792013-06-06 23:08:25 -0700335 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
336
Winson Chungf0c6ae02012-03-21 16:10:31 -0700337 // Holds the page that we need to animate to, and the icon views that we need to animate up
338 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700339 private ImageView mFolderIconImageView;
340 private Bitmap mFolderIconBitmap;
341 private Canvas mFolderIconCanvas;
342 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700343
Michael Jurkaddd62e92011-02-16 17:49:14 -0800344 private BubbleTextView mWaitingForResume;
345
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 private Runnable mBuildLayersRunnable = new Runnable() {
347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 private static ArrayList<PendingAddArguments> sPendingAddList
355 = new ArrayList<PendingAddArguments>();
356
Michael Jurka7267fa52013-09-26 15:29:57 -0700357 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800358
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 private static class PendingAddArguments {
360 int requestCode;
361 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700362 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700363 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int cellX;
365 int cellY;
366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
369
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800370 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800371 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700376 if (DEBUG_STRICT_MODE) {
377 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378 .detectDiskReads()
379 .detectDiskWrites()
380 .detectNetwork() // or .detectAll() for all detectable problems
381 .penaltyLog()
382 .build());
383 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384 .detectLeakedSqlLiteObjects()
385 .detectLeakedClosableObjects()
386 .penaltyLog()
387 .penaltyDeath()
388 .build());
389 }
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400392
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400393 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400394 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700395
396 // Determine the dynamic grid properties
397 Point smallestSize = new Point();
398 Point largestSize = new Point();
399 Point realSize = new Point();
400 Display display = getWindowManager().getDefaultDisplay();
401 display.getCurrentSizeRange(smallestSize, largestSize);
402 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700403 DisplayMetrics dm = new DisplayMetrics();
404 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700405
Winson Chung5f8afe62013-08-12 16:19:28 -0700406 // Lazy-initialize the dynamic grid
407 DeviceProfile grid = app.initDynamicGrid(this,
408 Math.min(smallestSize.x, smallestSize.y),
409 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700410 realSize.x, realSize.y,
411 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700412
413 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700415 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mModel = app.setLauncher(this);
417 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700418 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400419 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800420 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Daniel Sandlerff02d492013-08-05 02:12:05 -0400423 mStats = new Stats(this);
424
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
Adam Cohen53805212013-10-01 10:39:23 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100445 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800447 registerContentObservers();
448
Joe Onorato7c312c12009-08-13 21:36:53 -0700449 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mSavedState = savedInstanceState;
452 restoreState(mSavedState);
453
454 if (PROFILE_STARTUP) {
455 android.os.Debug.stopMethodTracing();
456 }
457
458 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700459 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 // If the user leaves launcher, then we should just load items asynchronously when
461 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500462 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 } else {
464 // We only load the page synchronously if the user rotates (or triggers a
465 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800466 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469
470 // For handling default keys
471 mDefaultKeySsb = new SpannableStringBuilder();
472 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800473
474 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
475 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Adam Cohenf9426d52012-06-04 17:26:21 -0700477 updateGlobalIcons();
478
479 // On large interfaces, we want the screen to auto-rotate based on the current orientation
480 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700481
Winson Chunge43a1e72014-01-15 10:33:02 -0800482 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
483 // on the device, then we always show the first run cling experience (or if there is no
484 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800485 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800486 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
487 if (mModel.canMigrateFromOldLauncherDb(this)) {
488 mLauncherClings.showMigrationCling();
489 } else {
490 mLauncherClings.showFirstRunCling();
491 }
Winson Chunga6945242014-01-08 14:04:34 -0800492 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800493 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800494 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 }
496
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 protected void onUserLeaveHint() {
498 super.onUserLeaveHint();
499 sPausedFromUserAction = true;
500 }
501
Winson Chung98ca0f72013-07-29 12:58:51 -0700502 /** To be overriden by subclasses to hint to Launcher that we have custom content */
503 protected boolean hasCustomContentToLeft() {
504 return false;
505 }
506
Dave Hawkeya8881582013-09-17 15:55:33 -0600507 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500508 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600509 * {@link #addToCustomContentPage}. This will only be invoked if
510 * {@link #hasCustomContentToLeft()} is {@code true}.
511 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500512 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600513 }
514
515 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800516 * To be overridden by subclasses to indicate that there is an activity to launch
517 * before showing the standard launcher experience.
518 */
519 protected boolean hasFirstRunActivity() {
520 return false;
521 }
522
523 /**
524 * To be overridden by subclasses to launch any first run activity
525 */
526 protected Intent getFirstRunActivity() {
527 return null;
528 }
529
530 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532 * ensure the custom content page is added or removed if necessary.
533 */
534 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600535 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 // Not bound yet, wait for bindScreens to be called.
537 return;
538 }
539
540 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 mWorkspace.createCustomContentContainer();
543 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545 mWorkspace.removeCustomContentPage();
546 }
547 }
548
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 boolean searchVisible = false;
551 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800552 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 int coi = getCurrentOrientationIndexForGlobalIcons();
554 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700556 if (!DISABLE_MARKET_BUTTON) {
557 updateAppMarketIcon();
558 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700559 searchVisible = updateGlobalSearchIcon();
560 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sGlobalSearchIcon[coi] != null) {
563 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700566 if (sVoiceSearchIcon[coi] != null) {
567 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700568 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 }
Winson Chungd64a6662013-09-30 11:06:59 -0700570 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 }
Winson Chungadf0c182012-08-23 14:59:07 -0700573 if (mSearchDropTargetBar != null) {
574 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 if (sLocaleConfiguration == null) {
580 new AsyncTask<Void, Void, LocaleConfiguration>() {
581 @Override
582 protected LocaleConfiguration doInBackground(Void... unused) {
583 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584 readConfiguration(Launcher.this, localeConfiguration);
585 return localeConfiguration;
586 }
587
588 @Override
589 protected void onPostExecute(LocaleConfiguration result) {
590 sLocaleConfiguration = result;
591 checkForLocaleChange(); // recursive, but now with a locale configuration
592 }
593 }.execute();
594 return;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final Configuration configuration = getResources().getConfiguration();
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final String locale = configuration.locale.toString();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final int mcc = configuration.mcc;
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mnc = configuration.mnc;
607
Romain Guy84f296c2009-11-04 15:00:44 -0800608 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 sLocaleConfiguration.locale = locale;
612 sLocaleConfiguration.mcc = mcc;
613 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700614
Joe Onorato0589f0f2010-02-08 13:44:00 -0800615 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616
617 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100618 new AsyncTask<Void, Void, Void>() {
619 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100621 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 }
Michael Jurka43467462013-11-14 12:03:58 +0100623 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700624 }
625 }
626
627 private static class LocaleConfiguration {
628 public String locale;
629 public int mcc = -1;
630 public int mnc = -1;
631 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700632
Romain Guy98d01652009-06-30 16:21:04 -0700633 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634 DataInputStream in = null;
635 try {
636 in = new DataInputStream(context.openFileInput(PREFERENCES));
637 configuration.locale = in.readUTF();
638 configuration.mcc = in.readInt();
639 configuration.mnc = in.readInt();
640 } catch (FileNotFoundException e) {
641 // Ignore
642 } catch (IOException e) {
643 // Ignore
644 } finally {
645 if (in != null) {
646 try {
647 in.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
652 }
653 }
654
655 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656 DataOutputStream out = null;
657 try {
658 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659 out.writeUTF(configuration.locale);
660 out.writeInt(configuration.mcc);
661 out.writeInt(configuration.mnc);
662 out.flush();
663 } catch (FileNotFoundException e) {
664 // Ignore
665 } catch (IOException e) {
666 //noinspection ResultOfMethodCallIgnored
667 context.getFileStreamPath(PREFERENCES).delete();
668 } finally {
669 if (out != null) {
670 try {
671 out.close();
672 } catch (IOException e) {
673 // Ignore
674 }
675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Winson Chung36a62fe2012-05-06 18:04:42 -0700687 boolean isDraggingEnabled() {
688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 static int getScreen() {
694 synchronized (sLock) {
695 return sScreen;
696 }
697 }
698
699 static void setScreen(int screen) {
700 synchronized (sLock) {
701 sScreen = screen;
702 }
703 }
704
Winson Chung557d6ed2011-07-08 15:34:52 -0700705 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 * Copied from View -- the View version of the method isn't called
707 * anywhere else in our process and only exists for API level 17+,
708 * so it's ok to keep our own version with no API requirement.
709 */
710 public static int generateViewId() {
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
719 }
720 }
721
722 public int getViewIdForItem(ItemInfo info) {
723 // This cast is safe given the > 2B range for int.
724 int itemId = (int) info.id;
725 if (mItemIdToViewId.containsKey(itemId)) {
726 return mItemIdToViewId.get(itemId);
727 }
728 int viewId = generateViewId();
729 mItemIdToViewId.put(itemId, viewId);
730 return viewId;
731 }
732
733 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735 * a configuration step, this allows the proper animations to run after other transitions.
736 */
737 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
740 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 break;
744 case REQUEST_PICK_SHORTCUT:
745 processShortcut(args.intent);
746 break;
747 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700748 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700750 result = true;
751 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 case REQUEST_CREATE_APPWIDGET:
753 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700754 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700755 result = true;
756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
Michael Jurka27614d22012-04-02 06:40:22 -0700758 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
759 // if you turned the screen off and then back while in All Apps, Launcher would not
760 // return to the workspace. Clearing mAddInfo.container here fixes this issue
761 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700762 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 }
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 protected void onActivityResult(
767 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700768 // Reset the startActivity waiting flag
769 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 int pendingAddWidgetId = mPendingAddWidgetId;
771 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700772
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 Runnable exitSpringLoaded = new Runnable() {
774 @Override
775 public void run() {
776 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
777 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
778 }
779 };
780
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
784 if (resultCode == RESULT_CANCELED) {
785 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
790 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 }
792 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200793 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
794 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
795 mWorkspace.exitOverviewMode(false);
796 }
797 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700798 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200799
Adam Cohened66b2b2012-01-23 17:28:51 -0800800 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
801 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700802
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 // We have special handling for widgets
804 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800805 final int appWidgetId;
806 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
807 : -1;
808 if (widgetId < 0) {
809 appWidgetId = pendingAddWidgetId;
810 } else {
811 appWidgetId = widgetId;
812 }
813
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 final int result;
815 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800816 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700817 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
818 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 result = RESULT_CANCELED;
820 completeTwoStageWidgetDrop(result, appWidgetId);
821 onComplete = new Runnable() {
822 @Override
823 public void run() {
824 exitSpringLoadedDragModeDelayed(false, 0, null);
825 }
826 };
Winson Chung5aaab772012-04-27 15:24:02 -0700827 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700829 final CellLayout dropLayout =
830 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
831 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700832 onComplete = new Runnable() {
833 @Override
834 public void run() {
835 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700836 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 }
838 };
Winson Chung5aaab772012-04-27 15:24:02 -0700839 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700840 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
841 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 return;
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 // The pattern used here is that a user PICKs a specific application,
846 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
849 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700850 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800851 final PendingAddArguments args = new PendingAddArguments();
852 args.requestCode = requestCode;
853 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 args.cellX = mPendingAddInfo.cellX;
857 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800858 if (isWorkspaceLocked()) {
859 sPendingAddList.add(args);
860 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700861 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
864 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700865 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
872 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700873 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 Runnable onCompleteRunnable = null;
876 int animationType = 0;
877
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 if (resultCode == RESULT_OK) {
880 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
881 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700882 mPendingAddWidgetInfo);
883 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 onCompleteRunnable = new Runnable() {
885 @Override
886 public void run() {
887 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
890 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892 };
893 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800894 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 if (mDragLayer.getAnimatedView() != null) {
898 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
899 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
900 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 // The animated view may be null in the case of a rotation during widget configuration
903 onCompleteRunnable.run();
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700908 protected void onStop() {
909 super.onStop();
910 FirstFrameAnimatorHelper.setIsVisible(false);
911 }
912
913 @Override
914 protected void onStart() {
915 super.onStart();
916 FirstFrameAnimatorHelper.setIsVisible(true);
917 }
918
919 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200921 long startTime = 0;
922 if (DEBUG_RESUME_TIME) {
923 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400924 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700927
Winson Chung4a2afa32012-07-19 14:53:05 -0700928 // Restore the previous launcher state
929 if (mOnResumeState == State.WORKSPACE) {
930 showWorkspace(false);
931 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800932 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700933 }
934 mOnResumeState = State.NONE;
935
Craig Mautner360310b2012-10-26 15:13:08 -0700936 // Background was set to gradient in onPause(), restore to black if in all apps.
937 setWorkspaceBackground(mState == State.WORKSPACE);
938
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800939 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700940 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700941 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500943 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700945 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700947 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200948 // We might have postponed some bind calls until onResume (see waitUntilResume) --
949 // execute them here
950 long startTimeCallbacks = 0;
951 if (DEBUG_RESUME_TIME) {
952 startTimeCallbacks = System.currentTimeMillis();
953 }
954
955 if (mAppsCustomizeContent != null) {
956 mAppsCustomizeContent.setBulkBind(true);
957 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700958 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
959 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200960 }
961 if (mAppsCustomizeContent != null) {
962 mAppsCustomizeContent.setBulkBind(false);
963 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700964 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200965 if (DEBUG_RESUME_TIME) {
966 Log.d(TAG, "Time spent processing callbacks in onResume: " +
967 (System.currentTimeMillis() - startTimeCallbacks));
968 }
Michael Jurka447bf842013-05-15 14:52:15 +0200969 }
Michael Jurka54554252013-08-01 12:52:23 +0200970 if (mOnResumeCallbacks.size() > 0) {
971 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
972 mOnResumeCallbacks.get(i).run();
973 }
974 mOnResumeCallbacks.clear();
975 }
Winson Chunge4e50662012-01-23 14:45:13 -0800976
977 // Reset the pressed state of icons that were locked in the press state while activities
978 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800979 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800980 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800981 mWaitingForResume.setStayPressed(false);
982 }
Winson Chunge4e50662012-01-23 14:45:13 -0800983 if (mAppsCustomizeContent != null) {
984 // Resets the previous all apps icon press state
985 mAppsCustomizeContent.resetDrawableState();
986 }
Winson Chung82963d52013-10-09 11:20:57 -0700987
Adam Cohen06dff352012-06-01 17:17:08 -0700988 // It is possible that widgets can receive updates while launcher is not in the foreground.
989 // Consequently, the widgets will be inflated in the orientation of the foreground activity
990 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
991 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700992 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700993
Winson Chung780fe592013-09-26 14:48:44 -0700994 // Process any items that were added while Launcher was away.
995 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
996
Winson Chung5841efa2013-09-30 18:06:44 -0700997 // Update the voice search button proxy
998 updateVoiceButtonProxyVisible(false);
999
Adam Cohenf9426d52012-06-04 17:26:21 -07001000 // Again, as with the above scenario, it's possible that one or more of the global icons
1001 // were updated in the wrong orientation.
1002 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001003 if (DEBUG_RESUME_TIME) {
1004 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1005 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001006
1007 if (mWorkspace.getCustomContentCallbacks() != null) {
1008 // If we are resuming and the custom content is the current page, we call onShow().
1009 // It is also poassible that onShow will instead be called slightly after first layout
1010 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1011 if (mWorkspace.isOnOrMovingToCustomContent()) {
1012 mWorkspace.getCustomContentCallbacks().onShow();
1013 }
1014 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001015 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001016 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001017 }
1018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001021 // Ensure that items added to Launcher are queued until Launcher returns
1022 InstallShortcutReceiver.enableInstallQueue();
1023
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001024 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001026 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001027 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001028
1029 // We call onHide() aggressively. The custom content callbacks should be able to
1030 // debounce excess onHide calls.
1031 if (mWorkspace.getCustomContentCallbacks() != null) {
1032 mWorkspace.getCustomContentCallbacks().onHide();
1033 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 }
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Adam Cohenbffe7452013-07-22 18:21:45 -07001036 QSBScroller mQsbScroller = new QSBScroller() {
1037 int scrollY = 0;
1038
1039 @Override
1040 public void setScrollY(int scroll) {
1041 scrollY = scroll;
1042
1043 if (mWorkspace.isOnOrMovingToCustomContent()) {
1044 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001045 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001046 }
1047 }
1048 };
1049
1050 public void resetQSBScroll() {
1051 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001052 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001053 }
1054
1055 public interface CustomContentCallbacks {
1056 // Custom content is completely shown
1057 public void onShow();
1058
1059 // Custom content is completely hidden
1060 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001061
1062 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1063 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001064 }
1065
Jorim Jaggid017f882014-01-14 17:08:48 -08001066 protected boolean hasSettings() {
1067 return false;
1068 }
1069
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 public interface QSBScroller {
1071 public void setScrollY(int scrollY);
1072 }
1073
Winson Chung98ca0f72013-07-29 12:58:51 -07001074 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001075 CustomContentCallbacks callbacks, String description) {
1076 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001078 }
1079
Adam Cohenedb40762013-07-18 16:45:45 -07001080 // The custom content needs to offset its content to account for the QSB
1081 public int getTopOffsetForCustomContent() {
1082 return mWorkspace.getPaddingTop();
1083 }
1084
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001085 @Override
1086 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001087 // Flag the loader to stop early before switching
1088 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001089 if (mAppsCustomizeContent != null) {
1090 mAppsCustomizeContent.surrender();
1091 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001092 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001093 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001095 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001096 @Override
1097 public void onWindowFocusChanged(boolean hasFocus) {
1098 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001099 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001100 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 private boolean acceptFilter() {
1103 final InputMethodManager inputManager = (InputMethodManager)
1104 getSystemService(Context.INPUT_METHOD_SERVICE);
1105 return !inputManager.isFullscreenMode();
1106 }
1107
1108 @Override
1109 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001110 final int uniChar = event.getUnicodeChar();
1111 final boolean handled = super.onKeyDown(keyCode, event);
1112 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1113 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1115 keyCode, event);
1116 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001117 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001118 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001119 // showSearchDialog()
1120 // If there are multiple keystrokes before the search dialog takes focus,
1121 // onSearchRequested() will be called for every keystroke,
1122 // but it is idempotent, so it's fine.
1123 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125 }
1126
Joe Onorato8a9625e2010-01-28 15:55:35 -08001127 // Eat the long press event so the keyboard doesn't come up.
1128 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1129 return true;
1130 }
1131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 return handled;
1133 }
1134
Karl Rosaen138a0412009-04-23 19:00:21 -07001135 private String getTypedText() {
1136 return mDefaultKeySsb.toString();
1137 }
1138
1139 private void clearTypedText() {
1140 mDefaultKeySsb.clear();
1141 mDefaultKeySsb.clearSpans();
1142 Selection.setSelection(mDefaultKeySsb, 0);
1143 }
1144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001146 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1147 * State
1148 */
1149 private static State intToState(int stateOrdinal) {
1150 State state = State.WORKSPACE;
1151 final State[] stateValues = State.values();
1152 for (int i = 0; i < stateValues.length; i++) {
1153 if (stateValues[i].ordinal() == stateOrdinal) {
1154 state = stateValues[i];
1155 break;
1156 }
1157 }
1158 return state;
1159 }
1160
1161 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 * Restores the previous state, if it exists.
1163 *
1164 * @param savedState The previous state.
1165 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001166 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 private void restoreState(Bundle savedState) {
1168 if (savedState == null) {
1169 return;
1170 }
1171
Michael Jurka883f55b2010-10-21 15:47:14 -07001172 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001173 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001174 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001175 }
1176
Adam Cohen21cd0022013-10-09 18:57:02 -07001177 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1178 PagedView.INVALID_RESTORE_PAGE);
1179 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001180 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001184 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1187 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001188 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1190 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001191 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1192 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1193 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001194 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001195 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 mRestoring = true;
1197 }
1198
1199 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1200 if (renameFolder) {
1201 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001202 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 mRestoring = true;
1204 }
Winson Chunga12a2502010-12-20 14:41:35 -08001205
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 // Restore the AppsCustomize tab
1207 if (mAppsCustomizeTabHost != null) {
1208 String curTab = savedState.getString("apps_customize_currentTab");
1209 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001210 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001212 mAppsCustomizeContent.loadAssociatedPages(
1213 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
1215
Winson Chung5afbf7b2011-07-25 11:53:08 -07001216 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1217 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001218 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001219 mItemIdToViewId = (HashMap<Integer, Integer>)
1220 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222
1223 /**
1224 * Finds all the views we need and configure them properly.
1225 */
1226 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001227 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001228
Craig Mautner360310b2012-10-26 15:13:08 -07001229 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001230 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1231 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001232 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001233
John Spurlock77e1f472013-09-11 10:09:51 -04001234 mLauncherView.setSystemUiVisibility(
1235 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001236 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
Winson Chung4c98d922011-05-31 16:50:48 -07001238 // Setup the drag layer
1239 mDragLayer.setup(this, dragController);
1240
Winson Chung3d503fb2011-07-13 17:25:49 -07001241 // Setup the hotseat
1242 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1243 if (mHotseat != null) {
1244 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001245 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001246 }
1247
Jorim Jaggid017f882014-01-14 17:08:48 -08001248 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001249 View widgetButton = findViewById(R.id.widget_button);
1250 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 @Override
1252 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001253 if (!mWorkspace.isSwitchingState()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001254 onClickAddWidgetButton();
Winson Chung8e15fdf2013-11-11 15:47:45 -08001255 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 }
1257 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001258 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1259
1260 View wallpaperButton = findViewById(R.id.wallpaper_button);
1261 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 @Override
1263 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001264 if (!mWorkspace.isSwitchingState()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001265 onClickWallpaperPicker();
Winson Chung8e15fdf2013-11-11 15:47:45 -08001266 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001267 }
1268 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001269 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1270
1271 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001272 if (hasSettings()) {
1273 settingsButton.setOnClickListener(new OnClickListener() {
1274 @Override
1275 public void onClick(View arg0) {
1276 if (!mWorkspace.isSwitchingState()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001277 onClickSettingsButton();
Jorim Jaggid017f882014-01-14 17:08:48 -08001278 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001279 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001280 });
1281 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1282 } else {
1283 settingsButton.setVisibility(View.GONE);
1284 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1285 lp.gravity = Gravity.END | Gravity.TOP;
1286 widgetButton.requestLayout();
1287 }
1288
Adam Cohendbdff6b2013-09-18 19:09:15 -07001289 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001290
Winson Chung4c98d922011-05-31 16:50:48 -07001291 // Setup the workspace
1292 mWorkspace.setHapticFeedbackEnabled(false);
1293 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001294 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001295 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001296
Winson Chungf0ea4d32011-06-06 14:27:16 -07001297 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001298 mSearchDropTargetBar = (SearchDropTargetBar)
1299 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001300
Winson Chungf0ea4d32011-06-06 14:27:16 -07001301 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001302 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001303 mAppsCustomizeContent = (AppsCustomizePagedView)
1304 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1305 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001306
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001308 dragController.setDragScoller(mWorkspace);
1309 dragController.setScrollView(mDragLayer);
1310 dragController.setMoveTarget(mWorkspace);
1311 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001312 if (mSearchDropTargetBar != null) {
1313 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001314 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001315
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001316 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001317 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001318 mWeightWatcher = new WeightWatcher(this);
1319 mWeightWatcher.setAlpha(0.5f);
1320 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001321 new FrameLayout.LayoutParams(
1322 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001323 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001324 Gravity.BOTTOM)
1325 );
Adam Cohen39a06042013-07-19 14:30:12 -07001326
1327 boolean show = shouldShowWeightWatcher();
1328 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 /**
1333 * Creates a view representing a shortcut.
1334 *
1335 * @param info The data structure describing the shortcut.
1336 *
1337 * @return A View inflated from R.layout.application.
1338 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001339 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001341 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 }
1343
1344 /**
1345 * Creates a view representing a shortcut inflated from the specified resource.
1346 *
1347 * @param layoutResId The id of the XML layout used to create the shortcut.
1348 * @param parent The group the shortcut belongs to.
1349 * @param info The data structure describing the shortcut.
1350 *
1351 * @return A View inflated from layoutResId.
1352 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001353 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001354 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1355 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 return favorite;
1358 }
1359
1360 /**
1361 * Add an application shortcut to the workspace.
1362 *
1363 * @param data The intent describing the application.
1364 * @param cellInfo The position on screen where to create the shortcut.
1365 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001368 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369
Adam Cohenfbba09b2011-07-18 21:43:05 -07001370 // First we check if we already know the exact location where we want to add this item.
1371 if (cellX >= 0 && cellY >= 0) {
1372 cellXY[0] = cellX;
1373 cellXY[1] = cellY;
1374 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001375 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001376 return;
1377 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001379 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001380
Romain Guy73b979d2009-06-09 12:57:21 -07001381 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001382 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001384 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001385 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001386 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001387 } else {
1388 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390 }
Romain Guycbb89e42009-06-08 15:52:54 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 /**
1393 * Add a shortcut to the workspace.
1394 *
1395 * @param data The intent describing the shortcut.
1396 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001398 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 int cellY) {
1400 int[] cellXY = mTmpAddItemCellCoordinates;
1401 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001402 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Michael Jurkad3ef3062010-11-23 16:23:58 -08001404 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001405
Adam Cohen558baaf2011-08-15 15:22:57 -07001406 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001407 if (info == null) {
1408 return;
1409 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001410 final View view = createShortcut(info);
1411
Adam Cohenfbba09b2011-07-18 21:43:05 -07001412 // First we check if we already know the exact location where we want to add this item.
1413 if (cellX >= 0 && cellY >= 0) {
1414 cellXY[0] = cellX;
1415 cellXY[1] = cellY;
1416 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001417
1418 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001419 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001420 true, null,null)) {
1421 return;
1422 }
1423 DragObject dragObject = new DragObject();
1424 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001425 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1426 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001427 return;
1428 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001429 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001430 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001432 foundCellSpan = (result != null);
1433 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001434 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001435 }
1436
1437 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001438 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439 return;
1440 }
1441
Adam Cohendcd297f2013-06-18 13:13:40 -07001442 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443
1444 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001445 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448 }
1449
Adam Cohen2f093b62012-04-30 18:59:53 -07001450 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1451 int minHeight) {
1452 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001453 // We want to account for the extra amount of padding that we are adding to the widget
1454 // to ensure that it gets the full amount of space that it has requested
1455 int requiredWidth = minWidth + padding.left + padding.right;
1456 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001457 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001458 }
1459
Adam Cohen2f093b62012-04-30 18:59:53 -07001460 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1461 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001462 }
1463
Adam Cohen2f093b62012-04-30 18:59:53 -07001464 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1465 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1469 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001470 }
1471
Adam Cohen2f093b62012-04-30 18:59:53 -07001472 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1473 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001474 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001478 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001480 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001481 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001483 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001484 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1485 if (appWidgetInfo == null) {
1486 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1487 }
Romain Guycbb89e42009-06-08 15:52:54 -07001488
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001489 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001490 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001491
Adam Cohen2f093b62012-04-30 18:59:53 -07001492 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1493 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001496 // We have saved the position to which the widget was dragged-- this really only matters
1497 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int[] cellXY = mTmpAddItemCellCoordinates;
1499 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001500 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1503 cellXY[0] = mPendingAddInfo.cellX;
1504 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001505 spanXY[0] = mPendingAddInfo.spanX;
1506 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 foundCellSpan = true;
1508 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001511 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1512 spanXY[1], cellXY, finalSpan);
1513 spanXY[0] = finalSpan[0];
1514 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001515 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001517 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001518 }
1519
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001521 if (appWidgetId != -1) {
1522 // Deleting an app widget ID is a void call but writes to disk before returning
1523 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001524 new AsyncTask<Void, Void, Void>() {
1525 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001526 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001527 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001528 }
Michael Jurka43467462013-11-14 12:03:58 +01001529 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001530 }
Winson Chung93eef082012-03-23 15:59:27 -07001531 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001532 return;
1533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001535 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001536 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1537 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 launcherInfo.spanX = spanXY[0];
1539 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001540 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1541 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 if (hostView == null) {
1548 // Perform actual inflation because we're live
1549 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1550 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1551 } else {
1552 // The AppWidgetHostView has already been inflated and instantiated
1553 launcherInfo.hostView = hostView;
1554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001557 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001558 launcherInfo.notifyWidgetSizeChanged(this);
1559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001561 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001562
1563 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001565 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Romain Guycbb89e42009-06-08 15:52:54 -07001567
Adam Cohended9f8d2010-11-03 13:25:16 -07001568 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1569 @Override
1570 public void onReceive(Context context, Intent intent) {
1571 final String action = intent.getAction();
1572 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1573 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001574 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001575 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001576
Winson Chungc100e8e2011-08-09 16:02:43 -07001577 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001578 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001579 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001580 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001582 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1583 mUserPresent = true;
1584 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001585 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1586 mModel.resetLoadedState(false, true);
1587 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1588 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1589 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1590 mModel.resetLoadedState(false, true);
1591 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1592 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1593 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001594 }
1595 }
1596 };
1597
1598 @Override
1599 public void onAttachedToWindow() {
1600 super.onAttachedToWindow();
1601
1602 // Listen for broadcasts related to user-presence
1603 final IntentFilter filter = new IntentFilter();
1604 filter.addAction(Intent.ACTION_SCREEN_OFF);
1605 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001606 if (ENABLE_DEBUG_INTENTS) {
1607 filter.addAction(DebugIntents.DELETE_DATABASE);
1608 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1609 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001610 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001611 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001612 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001613 mVisible = true;
1614 }
1615
1616 @Override
1617 public void onDetachedFromWindow() {
1618 super.onDetachedFromWindow();
1619 mVisible = false;
1620
Adam Cohend113e0c2010-11-11 10:48:05 -08001621 if (mAttached) {
1622 unregisterReceiver(mReceiver);
1623 mAttached = false;
1624 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001625 updateRunning();
1626 }
1627
1628 public void onWindowVisibilityChanged(int visibility) {
1629 mVisible = visibility == View.VISIBLE;
1630 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001631 // The following code used to be in onResume, but it turns out onResume is called when
1632 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1633 // is a more appropriate event to handle
1634 if (mVisible) {
1635 mAppsCustomizeTabHost.onWindowVisible();
1636 if (!mWorkspaceLoading) {
1637 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001638 // We want to let Launcher draw itself at least once before we force it to build
1639 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001640 // apps is nice and speedy.
1641 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001642 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001643 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001644 if (mStarted) return;
1645 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001646 // We delay the layer building a bit in order to give
1647 // other message processing a time to run. In particular
1648 // this avoids a delay in hiding the IME if it was
1649 // currently shown, because doing that may involve
1650 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001651 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001652 final ViewTreeObserver.OnDrawListener listener = this;
1653 mWorkspace.post(new Runnable() {
1654 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001655 if (mWorkspace != null &&
1656 mWorkspace.getViewTreeObserver() != null) {
1657 mWorkspace.getViewTreeObserver().
1658 removeOnDrawListener(listener);
1659 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001660 }
1661 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001662 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001663 }
1664 });
1665 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001666 // When Launcher comes back to foreground, a different Activity might be responsible for
1667 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001668 if (!DISABLE_MARKET_BUTTON) {
1669 updateAppMarketIcon();
1670 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001671 clearTypedText();
1672 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674
1675 private void sendAdvanceMessage(long delay) {
1676 mHandler.removeMessages(ADVANCE_MSG);
1677 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1678 mHandler.sendMessageDelayed(msg, delay);
1679 mAutoAdvanceSentTime = System.currentTimeMillis();
1680 }
1681
1682 private void updateRunning() {
1683 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1684 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1685 mAutoAdvanceRunning = autoAdvanceRunning;
1686 if (autoAdvanceRunning) {
1687 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1688 sendAdvanceMessage(delay);
1689 } else {
1690 if (!mWidgetsToAdvance.isEmpty()) {
1691 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1692 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1693 }
1694 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001695 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 }
1697 }
1698 }
1699
1700 private final Handler mHandler = new Handler() {
1701 @Override
1702 public void handleMessage(Message msg) {
1703 if (msg.what == ADVANCE_MSG) {
1704 int i = 0;
1705 for (View key: mWidgetsToAdvance.keySet()) {
1706 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1707 final int delay = mAdvanceStagger * i;
1708 if (v instanceof Advanceable) {
1709 postDelayed(new Runnable() {
1710 public void run() {
1711 ((Advanceable) v).advance();
1712 }
1713 }, delay);
1714 }
1715 i++;
1716 }
1717 sendAdvanceMessage(mAdvanceInterval);
1718 }
1719 }
1720 };
1721
1722 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001723 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1725 if (v instanceof Advanceable) {
1726 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001727 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 updateRunning();
1729 }
1730 }
1731
1732 void removeWidgetToAutoAdvance(View hostView) {
1733 if (mWidgetsToAdvance.containsKey(hostView)) {
1734 mWidgetsToAdvance.remove(hostView);
1735 updateRunning();
1736 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 }
1738
Joe Onorato9c1289c2009-08-17 11:03:03 -04001739 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001741 launcherInfo.hostView = null;
1742 }
1743
Winson Chung93eef082012-03-23 15:59:27 -07001744 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1745 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1746 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 }
1748
Winson Chunga6945242014-01-08 14:04:34 -08001749 public DragLayer getDragLayer() {
1750 return mDragLayer;
1751 }
1752
1753 public Workspace getWorkspace() {
1754 return mWorkspace;
1755 }
1756
1757 public Hotseat getHotseat() {
1758 return mHotseat;
1759 }
1760
Jorim Jaggid017f882014-01-14 17:08:48 -08001761 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001762 return mOverviewPanel;
1763 }
1764
1765 public SearchDropTargetBar getSearchBar() {
1766 return mSearchDropTargetBar;
1767 }
1768
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001769 public LauncherAppWidgetHost getAppWidgetHost() {
1770 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Winson Chunga9abd0e2010-10-27 17:18:37 -07001773 public LauncherModel getModel() {
1774 return mModel;
1775 }
1776
Winson Chunga6945242014-01-08 14:04:34 -08001777 public LauncherClings getLauncherClings() {
1778 return mLauncherClings;
1779 }
1780
1781 protected SharedPreferences getSharedPrefs() {
1782 return mSharedPrefs;
1783 }
1784
Bjorn Bringertc459e522013-06-07 19:36:01 +01001785 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001786 getWindow().closeAllPanels();
1787
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001788 // Whatever we were doing is hereby canceled.
1789 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001790 }
1791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 @Override
1793 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001794 long startTime = 0;
1795 if (DEBUG_RESUME_TIME) {
1796 startTime = System.currentTimeMillis();
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 super.onNewIntent(intent);
1799
1800 // Close the menu
1801 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001802 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001803 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001804
Adam Cohened307df2013-10-02 09:37:31 -07001805 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1806 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1807 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001808
Adam Cohen6fecd412013-10-02 17:41:50 -07001809 if (mWorkspace == null) {
1810 // Can be cases where mWorkspace is null, this prevents a NPE
1811 return;
1812 }
1813 Folder openFolder = mWorkspace.getOpenFolder();
1814 // In all these cases, only animate if we're already on home
1815 mWorkspace.exitWidgetResizeMode();
1816 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001817 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001818 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001819 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001820
Adam Cohen6fecd412013-10-02 17:41:50 -07001821 closeFolder();
1822 exitSpringLoadedDragMode();
1823
1824 // If we are already on home, then just animate back to the workspace,
1825 // otherwise, just wait until onResume to set the state back to Workspace
1826 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001827 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001828 } else {
1829 mOnResumeState = State.WORKSPACE;
1830 }
1831
1832 final View v = getWindow().peekDecorView();
1833 if (v != null && v.getWindowToken() != null) {
1834 InputMethodManager imm = (InputMethodManager)getSystemService(
1835 INPUT_METHOD_SERVICE);
1836 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1837 }
1838
1839 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001840 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001841 mAppsCustomizeTabHost.reset();
1842 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001843
1844 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Adam Cohened307df2013-10-02 09:37:31 -07001846
Michael Jurka447bf842013-05-15 14:52:15 +02001847 if (DEBUG_RESUME_TIME) {
1848 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1849 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 }
1851
Adam Coppa120b8e2013-11-12 16:26:04 +00001852 /**
1853 * Override point for subclasses to prevent movement to the default screen when the home
1854 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1855 */
1856 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1857 return true;
1858 }
1859
1860 /**
1861 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1862 */
1863 protected void onHomeIntent() {
1864 // Do nothing
1865 }
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001868 public void onRestoreInstanceState(Bundle state) {
1869 super.onRestoreInstanceState(state);
1870 for (int page: mSynchronouslyBoundPages) {
1871 mWorkspace.restoreInstanceStateForChild(page);
1872 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 }
1874
1875 @Override
1876 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001877 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001878 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1879 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001880 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001881 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882
Michael Jurka883f55b2010-10-21 15:47:14 -07001883 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001884 // We close any open folder since it will not be re-opened, and we need to make sure
1885 // this state is reflected.
1886 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887
Adam Cohendcd297f2013-06-18 13:13:40 -07001888 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001889 mWaitingForResult) {
1890 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001891 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1893 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001894 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1895 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1896 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001897 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
1900 if (mFolderInfo != null && mWaitingForResult) {
1901 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1902 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1903 }
Michael Jurka946ad472010-07-09 18:05:18 -07001904
Winson Chung785d2eb2011-04-14 16:08:02 -07001905 // Save the current AppsCustomize tab
1906 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001907 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1908 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001909 if (currentTabTag != null) {
1910 outState.putString("apps_customize_currentTab", currentTabTag);
1911 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001912 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1913 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001914 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001915 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
1918 @Override
1919 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001921
Winson Chunge7a03942011-08-05 15:05:12 -07001922 // Remove all pending runnables
1923 mHandler.removeMessages(ADVANCE_MSG);
1924 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001925 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001926
Winson Chungcd2b0142011-06-08 16:02:26 -07001927 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001928 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001929 mModel.stopLoader();
1930 app.setLauncher(null);
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001933 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001935 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001937 mAppWidgetHost = null;
1938
1939 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940
1941 TextKeyListener.getInstance().release();
1942
Winson Chung81b52252012-08-27 15:34:29 -07001943 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1944 // to prevent leaking Launcher activities on orientation change.
1945 if (mModel != null) {
1946 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1947 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001948
1949 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001950 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001951
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001952 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001953 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001954 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001955 mWorkspace = null;
1956 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001957
Michael Jurka2ecf9952012-06-18 12:52:28 -07001958 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
Adam Cohena9cf38f2011-05-02 15:36:58 -07001961 public DragController getDragController() {
1962 return mDragController;
1963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 @Override
1966 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001967 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 super.startActivityForResult(intent, requestCode);
1969 }
1970
Winson Chung70d72102011-08-12 11:24:35 -07001971 /**
1972 * Indicates that we want global search for this activity by setting the globalSearch
1973 * argument for {@link #startSearch} to true.
1974 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001976 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001978
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001979 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Karl Rosaen138a0412009-04-23 19:00:21 -07001981 if (initialQuery == null) {
1982 // Use any text typed in the launcher as the initial query
1983 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 if (appSearchData == null) {
1986 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001987 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
Winson Chungadf0c182012-08-23 14:59:07 -07001989 Rect sourceBounds = new Rect();
1990 if (mSearchDropTargetBar != null) {
1991 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1992 }
Romain Guycbb89e42009-06-08 15:52:54 -07001993
Bjorn Bringertc459e522013-06-07 19:36:01 +01001994 startSearch(initialQuery, selectInitialQuery,
1995 appSearchData, sourceBounds);
1996 }
1997
1998 public void startSearch(String initialQuery,
1999 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002000 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002001 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002002 }
2003
2004 /**
2005 * Starts the global search activity. This code is a copied from SearchManager
2006 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002007 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002008 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002009 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002010 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2011 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2012 if (globalSearchActivity == null) {
2013 Log.w(TAG, "No global search activity found.");
2014 return;
2015 }
2016 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2017 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2018 intent.setComponent(globalSearchActivity);
2019 // Make sure that we have a Bundle to put source in
2020 if (appSearchData == null) {
2021 appSearchData = new Bundle();
2022 } else {
2023 appSearchData = new Bundle(appSearchData);
2024 }
2025 // Set source to package name of app that starts global search, if not set already.
2026 if (!appSearchData.containsKey("source")) {
2027 appSearchData.putString("source", getPackageName());
2028 }
2029 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2030 if (!TextUtils.isEmpty(initialQuery)) {
2031 intent.putExtra(SearchManager.QUERY, initialQuery);
2032 }
2033 if (selectInitialQuery) {
2034 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2035 }
2036 intent.setSourceBounds(sourceBounds);
2037 try {
2038 startActivity(intent);
2039 } catch (ActivityNotFoundException ex) {
2040 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043
Yura4f93ec62014-02-04 14:15:21 +00002044 public boolean isOnCustomContent() {
2045 return mWorkspace.isOnOrMovingToCustomContent();
2046 }
2047
Winson Chung70d72102011-08-12 11:24:35 -07002048 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002049 public boolean onPrepareOptionsMenu(Menu menu) {
2050 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002051 if (!isOnCustomContent()) {
2052 // Close any open folders
2053 closeFolder();
2054 // Stop resizing any widgets
2055 mWorkspace.exitWidgetResizeMode();
2056 if (!mWorkspace.isInOverviewMode()) {
2057 // Show the overview mode
2058 showOverviewMode(true);
2059 } else {
2060 showWorkspace(true);
2061 }
Winson Chunge0298742014-01-17 12:03:00 -08002062 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002063 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002066 @Override
2067 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002068 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002069 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002071 }
2072
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 public boolean isWorkspaceLocked() {
2074 return mWorkspaceLoading || mWaitingForResult;
2075 }
2076
Michael Jurka0280c3b2010-09-17 15:00:07 -07002077 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002079 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002080 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2081 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002082 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002084 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002085
Adam Cohenad4e15c2013-10-17 16:21:35 -07002086 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2087 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2088 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2089 }
2090
2091 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2092 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2093 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002094 if (appWidgetInfo.configure != null) {
2095 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002096 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002097
Bjorn Bringert7984c942009-12-09 15:38:25 +00002098 // Launch over to configure widget, if needed
2099 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002100 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002101 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002102 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002104 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002105 Runnable onComplete = new Runnable() {
2106 @Override
2107 public void run() {
2108 // Exit spring loaded mode if necessary after adding the widget
2109 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2110 null);
2111 }
2112 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002113 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002114 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002115 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117 }
Romain Guycbb89e42009-06-08 15:52:54 -07002118
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002119 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002120 // Close any folders that may be open.
2121 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002122 mWorkspace.moveToCustomContentScreen(animate);
2123 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002124 /**
2125 * Process a shortcut drop.
2126 *
2127 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002128 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002129 * @param cell The cell it should be added to, optional
2130 * @param position The location on the screen where it was dropped, optional
2131 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002132 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002133 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002134 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002135 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002136 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002137 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002138
2139 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 mPendingAddInfo.cellX = cell[0];
2141 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002142 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002143
2144 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2145 createShortcutIntent.setComponent(componentName);
2146 processShortcut(createShortcutIntent);
2147 }
2148
Adam Cohenfbba09b2011-07-18 21:43:05 -07002149 /**
2150 * Process a widget drop.
2151 *
2152 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002153 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002154 * @param cell The cell it should be added to, optional
2155 * @param position The location on the screen where it was dropped, optional
2156 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002157 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002158 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002159 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002161 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002162 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002163 mPendingAddInfo.minSpanX = info.minSpanX;
2164 mPendingAddInfo.minSpanY = info.minSpanY;
2165
Adam Cohenfbba09b2011-07-18 21:43:05 -07002166 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 mPendingAddInfo.cellX = cell[0];
2168 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002169 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002170 if (span != null) {
2171 mPendingAddInfo.spanX = span[0];
2172 mPendingAddInfo.spanY = span[1];
2173 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002174
Adam Cohened66b2b2012-01-23 17:28:51 -08002175 AppWidgetHostView hostView = info.boundWidget;
2176 int appWidgetId;
2177 if (hostView != null) {
2178 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002179 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002180 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002181 // In this case, we either need to start an activity to get permission to bind
2182 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002183 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002184 Bundle options = info.bindOptions;
2185
2186 boolean success = false;
2187 if (options != null) {
2188 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2189 info.componentName, options);
2190 } else {
2191 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2192 info.componentName);
2193 }
2194 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002195 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002196 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002197 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002198 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2199 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2200 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002201 // TODO: we need to make sure that this accounts for the options bundle.
2202 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002203 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2204 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002205 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002206 }
2207
Joe Onoratodeb98af2010-02-19 14:59:39 -08002208 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002209 // Handle case where user selected "Applications"
2210 String applicationName = getResources().getString(R.string.group_applications);
2211 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002212
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002213 if (applicationName != null && applicationName.equals(shortcutName)) {
2214 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2215 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002216
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002217 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2218 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002219 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002220 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002221 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002222 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 }
2225
Winson Chung24ab2f12010-09-16 14:10:47 -07002226 void processWallpaper(Intent intent) {
2227 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2228 }
2229
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002232 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 folderInfo.title = getText(R.string.folder_name);
2234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002238 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239
2240 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 FolderIcon newFolder =
2242 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002245 // Force measure the new folder icon
2246 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2247 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002248 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
Romain Guycbb89e42009-06-08 15:52:54 -07002250
Joe Onorato9c1289c2009-08-17 11:03:03 -04002251 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002252 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002253 }
2254
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002255 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002256 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002257 }
2258
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002259 /**
2260 * Registers various content observers. The current implementation registers
2261 * only a favorites observer to keep track of the favorites applications.
2262 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002263 private void registerContentObservers() {
2264 ContentResolver resolver = getContentResolver();
2265 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2266 true, mWidgetObserver);
2267 }
2268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 @Override
2270 public boolean dispatchKeyEvent(KeyEvent event) {
2271 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2272 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002274 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002275 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002276 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002277 dumpState();
2278 return true;
2279 }
2280 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002281 }
2282 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2283 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002284 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 return true;
2286 }
2287 }
2288
2289 return super.dispatchKeyEvent(event);
2290 }
2291
Joe Onorato88ec0992009-11-19 13:16:06 -08002292 @Override
2293 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002294 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002295 if (mAppsCustomizeContent.getContentType() ==
2296 AppsCustomizePagedView.ContentType.Applications) {
2297 showWorkspace(true);
2298 } else {
2299 showOverviewMode(true);
2300 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002301 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002302 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002303 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002304 Folder openFolder = mWorkspace.getOpenFolder();
2305 if (openFolder.isEditingName()) {
2306 openFolder.dismissEditingName();
2307 } else {
2308 closeFolder();
2309 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002310 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002311 mWorkspace.exitWidgetResizeMode();
2312
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002313 // Back button is a no-op here, but give at least some feedback for the button press
2314 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002315 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002316 }
2317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002319 * Re-listen when widgets are reset.
2320 */
2321 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002322 if (mAppWidgetHost != null) {
2323 mAppWidgetHost.startListening();
2324 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002325 }
2326
2327 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 * Launches the intent referred by the clicked shortcut.
2329 *
2330 * @param v The view representing the clicked shortcut.
2331 */
2332 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002333 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2334 // view has detached (it's possible for this to happen if the view is removed mid touch).
2335 if (v.getWindowToken() == null) {
2336 return;
2337 }
2338
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002339 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002340 return;
2341 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002342
Adam Cohen1697b792013-09-17 19:08:21 -07002343 if (v instanceof Workspace) {
2344 if (mWorkspace.isInOverviewMode()) {
2345 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002346 }
2347 return;
2348 }
2349
2350 if (v instanceof CellLayout) {
2351 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002352 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002353 }
2354 }
2355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002357 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002358 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002360 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002361 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002362 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002364 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
2366 }
2367
Michael Jurka0e260592010-06-30 17:07:39 -07002368 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002369 return false;
2370 }
2371
Michael Jurkaaf442092010-06-10 17:01:57 -07002372 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002373 * Event handler for the search button
2374 *
2375 * @param v The view that was clicked.
2376 */
2377 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002378 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2379
Amith Yamasania135ba82011-08-09 17:42:01 -07002380 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002381 }
2382
2383 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002384 * Event handler for the voice button
2385 *
2386 * @param v The view that was clicked.
2387 */
2388 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002389 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002390
Bjorn Bringertc459e522013-06-07 19:36:01 +01002391 startVoice();
2392 }
2393
2394 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002395 try {
2396 final SearchManager searchManager =
2397 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2398 ComponentName activityName = searchManager.getGlobalSearchActivity();
2399 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002400 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002401 if (activityName != null) {
2402 intent.setPackage(activityName.getPackageName());
2403 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002404 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002405 } catch (ActivityNotFoundException e) {
2406 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002407 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002408 startActivitySafely(null, intent, "onClickVoiceButton");
2409 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002410 }
2411
2412 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002413 * Event handler for the "grid" button that appears on the home screen, which
2414 * enters all apps mode.
2415 *
2416 * @param v The view that was clicked.
2417 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002418 protected void onClickAllAppsButton(View v) {
2419 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2420 if (isAllAppsVisible()) {
2421 showWorkspace(true);
2422 } else {
2423 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2424 }
2425 }
2426
2427 /**
2428 * Event handler for an app shortcut click.
2429 *
2430 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2431 */
2432 protected void onClickAppShortcut(View v) {
2433 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2434 Object tag = v.getTag();
2435 if (!(tag instanceof ShortcutInfo)) {
2436 throw new IllegalArgumentException("Input must be a Shortcut");
2437 }
2438
2439 // Open shortcut
2440 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2441 final Intent intent = shortcut.intent;
2442
2443 // Check for special shortcuts
2444 if (intent.getComponent() != null) {
2445 final String shortcutClass = intent.getComponent().getClassName();
2446
2447 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2448 MemoryDumpActivity.startDump(this);
2449 return;
2450 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2451 toggleShowWeightWatcher();
2452 return;
2453 }
2454 }
2455
2456 // Start activities
2457 int[] pos = new int[2];
2458 v.getLocationOnScreen(pos);
2459 intent.setSourceBounds(new Rect(pos[0], pos[1],
2460 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2461
2462 boolean success = startActivitySafely(v, intent, tag);
2463
2464 mStats.recordLaunch(intent, shortcut);
2465
2466 if (success && v instanceof BubbleTextView) {
2467 mWaitingForResume = (BubbleTextView) v;
2468 mWaitingForResume.setStayPressed(true);
2469 }
2470 }
2471
2472 /**
2473 * Event handler for a folder icon click.
2474 *
2475 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2476 */
2477 protected void onClickFolderIcon(View v) {
2478 if (LOGD) Log.d(TAG, "onClickFolder");
2479 if (!(v instanceof FolderIcon)){
2480 throw new IllegalArgumentException("Input must be a FolderIcon");
2481 }
2482
2483 FolderIcon folderIcon = (FolderIcon) v;
2484 final FolderInfo info = folderIcon.getFolderInfo();
2485 Folder openFolder = mWorkspace.getFolderForTag(info);
2486
2487 // If the folder info reports that the associated folder is open, then verify that
2488 // it is actually opened. There have been a few instances where this gets out of sync.
2489 if (info.opened && openFolder == null) {
2490 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2491 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2492 info.opened = false;
2493 }
2494
2495 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2496 // Close any open folder
2497 closeFolder();
2498 // Open the requested folder
2499 openFolder(folderIcon);
2500 } else {
2501 // Find the open folder...
2502 int folderScreen;
2503 if (openFolder != null) {
2504 folderScreen = mWorkspace.getPageForView(openFolder);
2505 // .. and close it
2506 closeFolder(openFolder);
2507 if (folderScreen != mWorkspace.getCurrentPage()) {
2508 // Close any folder open on the current screen
2509 closeFolder();
2510 // Pull the folder onto this screen
2511 openFolder(folderIcon);
2512 }
2513 }
2514 }
Michael Jurka5130e402011-10-13 04:55:35 -07002515 }
2516
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002517 /**
2518 * Event handler for the (Add) Widgets button that appears after a long press
2519 * on the home screen.
2520 */
2521 protected void onClickAddWidgetButton() {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002523 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2524 }
2525
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 /**
2527 * Event handler for the wallpaper picker button that appears after a long press
2528 * on the home screen.
2529 */
2530 protected void onClickWallpaperPicker() {
2531 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2532 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2533 pickWallpaper.setComponent(getWallpaperPickerComponent());
2534 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2535 }
2536
2537 /**
2538 * Event handler for a click on the settings button that appears after a long press
2539 * on the home screen.
2540 */
2541 protected void onClickSettingsButton() {
2542 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2543 }
2544
Michael Jurka5130e402011-10-13 04:55:35 -07002545 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002546 // Provide the same haptic feedback that the system offers for virtual keys.
2547 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002548 }
2549
Adam Cohen61f560d2013-09-30 15:58:20 -07002550 public void performHapticFeedbackOnTouchDown(View v) {
2551 // Provide the same haptic feedback that the system offers for virtual keys.
2552 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2553 }
2554
2555 public View.OnTouchListener getHapticFeedbackTouchListener() {
2556 if (mHapticFeedbackTouchListener == null) {
2557 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2558 @Override
2559 public boolean onTouch(View v, MotionEvent event) {
2560 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2561 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2562 }
2563 return false;
2564 }
2565 };
2566 }
2567 return mHapticFeedbackTouchListener;
2568 }
2569
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002570 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002571 if (!DISABLE_MARKET_BUTTON) {
2572 if (mAppMarketIntent != null) {
2573 startActivitySafely(v, mAppMarketIntent, "app market");
2574 } else {
2575 Log.e(TAG, "Invalid app market intent.");
2576 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002577 }
2578 }
2579
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002580 /**
2581 * Called when the user stops interacting with the launcher.
2582 * This implies that the user is now on the homescreen and is not doing housekeeping.
2583 */
2584 protected void onInteractionEnd() {}
2585
2586 /**
2587 * Called when the user starts interacting with the launcher.
2588 * The possible interactions are:
2589 * - open all apps
2590 * - reorder an app shortcut, or a widget
2591 * - open the overview mode.
2592 * This is a good time to stop doing things that only make sense
2593 * when the user is on the homescreen and not doing housekeeping.
2594 */
2595 protected void onInteractionBegin() {}
2596
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002597 void startApplicationDetailsActivity(ComponentName componentName) {
2598 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002599 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2600 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002601 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2602 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002603 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002604 }
2605
Michael Jurka1e2f4652013-07-08 18:03:46 -07002606 // returns true if the activity was started
2607 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002608 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002609 // System applications cannot be installed. For now, show a toast explaining that.
2610 // We may give them the option of disabling apps this way.
2611 int messageId = R.string.uninstall_system_app_text;
2612 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002613 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002614 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002615 String packageName = componentName.getPackageName();
2616 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002617 Intent intent = new Intent(
2618 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002619 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2620 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002621 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002622 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002623 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002624 }
2625
Michael Jurka86a720e2012-05-09 11:23:23 -07002626 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002630 // Only launch using the new animation if the shortcut has not opted out (this is a
2631 // private contract between launcher and may be ignored in the future).
2632 boolean useLaunchAnimation = (v != null) &&
2633 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2634 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002635 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2636 v.getMeasuredWidth(), v.getMeasuredHeight());
2637
2638 startActivity(intent, opts.toBundle());
2639 } else {
2640 startActivity(intent);
2641 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002642 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 } catch (SecurityException e) {
2644 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002645 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002647 "or use the exported attribute for this activity. "
2648 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002650 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002652
Michael Jurka86a720e2012-05-09 11:23:23 -07002653 boolean startActivitySafely(View v, Intent intent, Object tag) {
2654 boolean success = false;
2655 try {
2656 success = startActivity(v, intent, tag);
2657 } catch (ActivityNotFoundException e) {
2658 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2659 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2660 }
2661 return success;
2662 }
2663
Adam Cohen268c4752012-06-06 17:47:33 -07002664 /**
2665 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2666 * in the DragLayer in the exact absolute location of the original FolderIcon.
2667 */
2668 private void copyFolderIconToImage(FolderIcon fi) {
2669 final int width = fi.getMeasuredWidth();
2670 final int height = fi.getMeasuredHeight();
2671
2672 // Lazy load ImageView, Bitmap and Canvas
2673 if (mFolderIconImageView == null) {
2674 mFolderIconImageView = new ImageView(this);
2675 }
2676 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2677 mFolderIconBitmap.getHeight() != height) {
2678 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2679 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2680 }
2681
2682 DragLayer.LayoutParams lp;
2683 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2684 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2685 } else {
2686 lp = new DragLayer.LayoutParams(width, height);
2687 }
2688
Adam Cohen307fe232012-08-16 17:55:58 -07002689 // The layout from which the folder is being opened may be scaled, adjust the starting
2690 // view size by this scale factor.
2691 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002692 lp.customPosition = true;
2693 lp.x = mRectForFolderAnimation.left;
2694 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002695 lp.width = (int) (scale * width);
2696 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002697
2698 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2699 fi.draw(mFolderIconCanvas);
2700 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002701 if (fi.getFolder() != null) {
2702 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2703 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002704 }
Adam Cohen268c4752012-06-06 17:47:33 -07002705 // Just in case this image view is still in the drag layer from a previous animation,
2706 // we remove it and re-add it.
2707 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2708 mDragLayer.removeView(mFolderIconImageView);
2709 }
2710 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002711 if (fi.getFolder() != null) {
2712 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002713 }
Adam Cohen268c4752012-06-06 17:47:33 -07002714 }
2715
Adam Cohen2801caf2011-05-13 20:57:39 -07002716 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002717 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002718 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2719 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2720 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2721
Adam Cohenc51934b2011-07-26 21:07:43 -07002722 FolderInfo info = (FolderInfo) fi.getTag();
2723 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2724 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002725 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2726 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002727 }
2728
Adam Cohen268c4752012-06-06 17:47:33 -07002729 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2730 copyFolderIconToImage(fi);
2731 fi.setVisibility(View.INVISIBLE);
2732
Michael Jurka2ecf9952012-06-18 12:52:28 -07002733 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002734 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002735 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2736 oa.start();
2737 }
2738
Adam Cohen268c4752012-06-06 17:47:33 -07002739 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002740 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002741 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2742 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2743 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2744
Adam Cohen268c4752012-06-06 17:47:33 -07002745 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002746
Adam Cohen268c4752012-06-06 17:47:33 -07002747 // We remove and re-draw the FolderIcon in-case it has changed
2748 mDragLayer.removeView(mFolderIconImageView);
2749 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002750 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002751 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002752 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002753 oa.addListener(new AnimatorListenerAdapter() {
2754 @Override
2755 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002756 if (cl != null) {
2757 cl.clearFolderLeaveBehind();
2758 // Remove the ImageView copy of the FolderIcon and make the original visible.
2759 mDragLayer.removeView(mFolderIconImageView);
2760 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002761 }
2762 }
2763 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002764 oa.start();
2765 }
2766
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002768 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 * is animated relative to the specified View. If the View is null, no animation
2770 * is played.
2771 *
2772 * @param folderInfo The FolderInfo describing the folder to open.
2773 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002774 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002775 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002776 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002777
Adam Cohena9cf38f2011-05-02 15:36:58 -07002778 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779
Adam Cohen4554ee12011-08-03 16:13:21 -07002780 // Just verify that the folder hasn't already been added to the DragLayer.
2781 // There was a one-off crash where the folder had a parent already.
2782 if (folder.getParent() == null) {
2783 mDragLayer.addView(folder);
2784 mDragController.addDropTarget((DropTarget) folder);
2785 } else {
2786 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2787 folder.getParent() + ").");
2788 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002789 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002790 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002791
2792 // Notify the accessibility manager that this folder "window" has appeared and occluded
2793 // the workspace items
2794 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2795 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002796 }
2797
2798 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002799 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002800 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002801 if (folder.isEditingName()) {
2802 folder.dismissEditingName();
2803 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002804 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002805
2806 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002807 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002808 }
2809 }
2810
2811 void closeFolder(Folder folder) {
2812 folder.getInfo().opened = false;
2813
2814 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2815 if (parent != null) {
2816 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2817 shrinkAndFadeInFolderIcon(fi);
2818 }
2819 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002820
2821 // Notify the accessibility manager that this folder "window" has disappeard and no
2822 // longer occludeds the workspace items
2823 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 }
2825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002827 if (!isDraggingEnabled()) return false;
2828 if (isWorkspaceLocked()) return false;
2829 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002830
Adam Cohen1697b792013-09-17 19:08:21 -07002831 if (v instanceof Workspace) {
2832 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002833 if (mWorkspace.enterOverviewMode()) {
2834 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2835 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2836 return true;
2837 } else {
2838 return false;
2839 }
Adam Cohen1697b792013-09-17 19:08:21 -07002840 }
Adam Cohen1697b792013-09-17 19:08:21 -07002841 }
2842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002844 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846
Michael Jurka0280c3b2010-09-17 15:00:07 -07002847 resetAddInfo();
2848 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002850 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 return true;
2852 }
2853
Winson Chung3d503fb2011-07-13 17:25:49 -07002854 // The hotseat touch handling does not go through Workspace, and we always allow long press
2855 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002856 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002857 final boolean inHotseat = isHotseatLayout(v);
2858 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002859 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002860 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002861 // User long pressed on empty space
2862 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2863 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2864 // Disabling reordering until we sort out some issues.
2865 if (mWorkspace.isInOverviewMode()) {
2866 mWorkspace.startReordering(v);
2867 } else {
2868 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002871 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2872 mHotseat.getOrderInHotseat(
2873 longClickCellInfo.cellX,
2874 longClickCellInfo.cellY));
2875 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002877 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 }
2879 }
2880 }
2881 return true;
2882 }
2883
Winson Chung3d503fb2011-07-13 17:25:49 -07002884 boolean isHotseatLayout(View layout) {
2885 return mHotseat != null && layout != null &&
2886 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2887 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002888
Winson Chung3d503fb2011-07-13 17:25:49 -07002889 /**
2890 * Returns the CellLayout of the specified container at the specified screen.
2891 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002892 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002893 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2894 if (mHotseat != null) {
2895 return mHotseat.getLayout();
2896 } else {
2897 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002898 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002899 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002900 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002901 }
2902 }
2903
Daniel Sandler843e8602010-06-07 14:59:01 -04002904 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002905 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002906 }
2907
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002908 /**
2909 * Helper method for the cameraZoomIn/cameraZoomOut animations
2910 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002911 * @param scaleFactor The scale factor used for the zoom
2912 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002913 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002914 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002915 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002916 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002917
Craig Mautner360310b2012-10-26 15:13:08 -07002918 private void setWorkspaceBackground(boolean workspace) {
2919 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002920 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002921 }
2922
Selim Cinekf3a8e8c2014-01-16 10:38:38 -08002923 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002924 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2925 int curflags = getWindow().getAttributes().flags
2926 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2927 if (wpflags != curflags) {
2928 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2929 }
Craig Mautner360310b2012-10-26 15:13:08 -07002930 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002931 }
2932
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002933 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2934 if (v instanceof LauncherTransitionable) {
2935 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2936 }
2937 }
2938
Michael Jurkabed61d22012-02-14 22:51:29 -08002939 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2940 if (v instanceof LauncherTransitionable) {
2941 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2942 }
Winson Chung70442722012-02-10 15:43:22 -08002943
2944 // Update the workspace transition step as well
2945 dispatchOnLauncherTransitionStep(v, 0f);
2946 }
2947
2948 private void dispatchOnLauncherTransitionStep(View v, float t) {
2949 if (v instanceof LauncherTransitionable) {
2950 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2951 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002952 }
2953
2954 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2955 if (v instanceof LauncherTransitionable) {
2956 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2957 }
Winson Chung70442722012-02-10 15:43:22 -08002958
2959 // Update the workspace transition step as well
2960 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002961 }
2962
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002963 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002964 * Things to test when changing the following seven functions.
2965 * - Home from workspace
2966 * - from center screen
2967 * - from other screens
2968 * - Home from all apps
2969 * - from center screen
2970 * - from other screens
2971 * - Back from all apps
2972 * - from center screen
2973 * - from other screens
2974 * - Launch app from workspace and quit
2975 * - with back
2976 * - with home
2977 * - Launch app from all apps and quit
2978 * - with back
2979 * - with home
2980 * - Go to a screen that's not the default, then all
2981 * apps, and launch and app, and go back
2982 * - with back
2983 * -with home
2984 * - On workspace, long press power and go back
2985 * - with back
2986 * - with home
2987 * - On all apps, long press power and go back
2988 * - with back
2989 * - with home
2990 * - On workspace, power off
2991 * - On all apps, power off
2992 * - Launch an app and turn off the screen while in that app
2993 * - Go back with home key
2994 * - Go back with back key TODO: make this not go to workspace
2995 * - From all apps
2996 * - From workspace
2997 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2998 * - From all apps
2999 * - From the center workspace
3000 * - From another workspace
3001 */
3002
3003 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003004 * Zoom the camera out from the workspace to reveal 'toView'.
3005 * Assumes that the view to show is anchored at either the very top or very bottom
3006 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003007 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003008 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003009 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3010 showAppsCustomizeHelper(animated, springLoaded, contentType);
3011 }
3012 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3013 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003014 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003015 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003016 mStateAnimation.cancel();
3017 mStateAnimation = null;
3018 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003019 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003020
Winson Chungf0ea4d32011-06-06 14:27:16 -07003021 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3022 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3023 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003024 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003025 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003026 final int startDelay =
3027 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003028
Michael Jurkab3e22d92011-10-31 15:58:33 -07003029 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003030
Michael Jurkad74c9842011-07-10 12:44:21 -07003031 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003032 Animator workspaceAnim =
3033 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003034 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003035 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3036 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003037 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3038 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003039
3040 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003041 toView.setScaleX(scale);
3042 toView.setScaleY(scale);
3043 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3044 scaleAnim.
3045 scaleX(1f).scaleY(1f).
3046 setDuration(duration).
3047 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003048
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003050 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003051 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003052 .ofFloat(toView, "alpha", 0f, 1f)
3053 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003054 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003055 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3056 @Override
3057 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003058 if (animation == null) {
3059 throw new RuntimeException("animation is null");
3060 }
Winson Chung70442722012-02-10 15:43:22 -08003061 float t = (Float) animation.getAnimatedValue();
3062 dispatchOnLauncherTransitionStep(fromView, t);
3063 dispatchOnLauncherTransitionStep(toView, t);
3064 }
3065 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003066
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003067 // toView should appear right at the end of the workspace shrink
3068 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003069 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003070 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003071 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003072
3073 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003074 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003075 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003076 // Prepare the position
3077 toView.setTranslationX(0.0f);
3078 toView.setTranslationY(0.0f);
3079 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003080 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003081 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003082 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003083 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003084 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3085 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003086
Winson Chungc7d2b602012-05-16 17:02:20 -07003087 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003088 if (mSearchDropTargetBar != null) {
3089 mSearchDropTargetBar.hideSearchBar(false);
3090 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003091 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003092 });
3093
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003094 if (workspaceAnim != null) {
3095 mStateAnimation.play(workspaceAnim);
3096 }
Michael Jurka1899a362011-11-03 13:50:45 -07003097
3098 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003099
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003100 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3101 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003102
3103 // If any of the objects being animated haven't been measured/laid out
3104 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003105 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003106 (mWorkspace.getMeasuredWidth() == 0) ||
3107 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003108 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003109 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003110
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003111 final AnimatorSet stateAnimation = mStateAnimation;
3112 final Runnable startAnimRunnable = new Runnable() {
3113 public void run() {
3114 // Check that mStateAnimation hasn't changed while
3115 // we waited for a layout/draw pass
3116 if (mStateAnimation != stateAnimation)
3117 return;
3118 setPivotsForZoom(toView, scale);
3119 dispatchOnLauncherTransitionStart(fromView, animated, false);
3120 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003121 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003122 }
3123 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003124 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003125 final ViewTreeObserver observer = toView.getViewTreeObserver();
3126 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3127 public void onGlobalLayout() {
3128 startAnimRunnable.run();
3129 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3130 }
3131 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003132 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003133 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003134 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003135 } else {
3136 toView.setTranslationX(0.0f);
3137 toView.setTranslationY(0.0f);
3138 toView.setScaleX(1.0f);
3139 toView.setScaleY(1.0f);
3140 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003141 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003142
Daniel Sandlere4f98912013-06-25 15:13:26 -04003143 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003144 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003145 if (mSearchDropTargetBar != null) {
3146 mSearchDropTargetBar.hideSearchBar(false);
3147 }
Michael Jurkaabded662011-03-04 12:06:57 -08003148 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003149 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003150 dispatchOnLauncherTransitionStart(fromView, animated, false);
3151 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003152 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003153 dispatchOnLauncherTransitionStart(toView, animated, false);
3154 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003155 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003156 }
3157
3158 /**
3159 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003160 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003161 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003162 */
Adam Cohened307df2013-10-02 09:37:31 -07003163 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003164 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003165
Michael Jurkab3e22d92011-10-31 15:58:33 -07003166 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003167 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003168 mStateAnimation.cancel();
3169 mStateAnimation = null;
3170 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003171 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003172
Winson Chungf0ea4d32011-06-06 14:27:16 -07003173 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003174 final int fadeOutDuration =
3175 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003176 final float scaleFactor = (float)
3177 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3178 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003179 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003180 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003181 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003182 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3183 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003184 toState, animated, stagger, -1);
3185 } else if (toState == Workspace.State.SPRING_LOADED ||
3186 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003187 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003188 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003189 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003190
Michael Jurkab3e22d92011-10-31 15:58:33 -07003191 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003192 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003193 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003194 final LauncherViewPropertyAnimator scaleAnim =
3195 new LauncherViewPropertyAnimator(fromView);
3196 scaleAnim.
3197 scaleX(scaleFactor).scaleY(scaleFactor).
3198 setDuration(duration).
3199 setInterpolator(new Workspace.ZoomInInterpolator());
3200
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003201 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003202 .ofFloat(fromView, "alpha", 1f, 0f)
3203 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003204 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003205 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3206 @Override
3207 public void onAnimationUpdate(ValueAnimator animation) {
3208 float t = 1f - (Float) animation.getAnimatedValue();
3209 dispatchOnLauncherTransitionStep(fromView, t);
3210 dispatchOnLauncherTransitionStep(toView, t);
3211 }
3212 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003213
Michael Jurka2ecf9952012-06-18 12:52:28 -07003214 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003215
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003216 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3217 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003218 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003219
3220 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003221 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003222 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003223 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003224 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3225 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003226 if (onCompleteRunnable != null) {
3227 onCompleteRunnable.run();
3228 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003229 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003230 }
3231 });
3232
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003234 if (workspaceAnim != null) {
3235 mStateAnimation.play(workspaceAnim);
3236 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003237 dispatchOnLauncherTransitionStart(fromView, animated, true);
3238 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003239 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003240 } else {
3241 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003242 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003243 dispatchOnLauncherTransitionStart(fromView, animated, true);
3244 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003245 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003246 dispatchOnLauncherTransitionStart(toView, animated, true);
3247 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003248 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003249 }
3250
Michael Jurkae326f182011-11-21 14:05:46 -08003251 @Override
3252 public void onTrimMemory(int level) {
3253 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003254 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003255 mAppsCustomizeTabHost.onTrimMemory();
3256 }
3257 }
3258
Adam Cohened307df2013-10-02 09:37:31 -07003259 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003260 showWorkspace(animated, null);
3261 }
3262
Adam Cohened307df2013-10-02 09:37:31 -07003263 protected void showWorkspace() {
3264 showWorkspace(true);
3265 }
3266
Adam Cohened66b2b2012-01-23 17:28:51 -08003267 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003268 if (mWorkspace.isInOverviewMode()) {
3269 mWorkspace.exitOverviewMode(animated);
3270 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003272 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003274 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275
Winson Chungc7d2b602012-05-16 17:02:20 -07003276 // Show the search bar (only animate if we were showing the drop target bar in spring
3277 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003278 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003279 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003280 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003281
Michael Jurkab3e22d92011-10-31 15:58:33 -07003282 // Set focus to the AppsCustomize button
3283 if (mAllAppsButton != null) {
3284 mAllAppsButton.requestFocus();
3285 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003286 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003287
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003288 // Change the state *after* we've called all the transition code
3289 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003290
Winson Chung5fb63472011-02-02 17:03:37 -08003291 // Resume the auto-advance of widgets
3292 mUserPresent = true;
3293 updateRunning();
3294
alanv1d4fde62012-10-17 13:15:47 -07003295 // Send an accessibility event to announce the context change
3296 getWindow().getDecorView()
3297 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003298
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003299 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003300 }
3301
Adam Cohened307df2013-10-02 09:37:31 -07003302 void showOverviewMode(boolean animated) {
3303 mWorkspace.setVisibility(View.VISIBLE);
3304 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3305 mState = State.WORKSPACE;
3306 onWorkspaceShown(animated);
3307 }
3308
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003309 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003310 }
3311
Winson Chung82963d52013-10-09 11:20:57 -07003312 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3313 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003314 if (mState != State.WORKSPACE) return;
3315
Winson Chung82963d52013-10-09 11:20:57 -07003316 if (resetPageToZero) {
3317 mAppsCustomizeTabHost.reset();
3318 }
Winson Chungc58497e2013-09-03 17:48:37 -07003319 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320 mAppsCustomizeTabHost.requestFocus();
3321
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 // Change the state *after* we've called all the transition code
3323 mState = State.APPS_CUSTOMIZE;
3324
3325 // Pause the auto-advance of widgets until we are out of AllApps
3326 mUserPresent = false;
3327 updateRunning();
3328 closeFolder();
3329
3330 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003331 getWindow().getDecorView()
3332 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003333 }
3334
Adam Cohen7777d962011-08-18 18:58:38 -07003335 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003336 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003337 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003338 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003339 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003340 }
Adam Cohen7777d962011-08-18 18:58:38 -07003341
Adam Cohenad4e15c2013-10-17 16:21:35 -07003342 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003343 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003344 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3345
Winson Chunge7a03942011-08-05 15:05:12 -07003346 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003347 @Override
3348 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003349 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003350 // Before we show workspace, hide all apps again because
3351 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3352 // clean up our state transition functions
3353 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003354 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003355 } else {
3356 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003357 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003358 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003359 }, delay);
3360
Winson Chung557d6ed2011-07-08 15:34:52 -07003361 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003362
Michael Jurkad3ef3062010-11-23 16:23:58 -08003363 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003364 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365 final boolean animated = true;
3366 final boolean springLoaded = true;
3367 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003368 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003369 }
3370 // Otherwise, we are not in spring loaded mode, so don't do anything.
3371 }
3372
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373 void lockAllApps() {
3374 // TODO
3375 }
3376
3377 void unlockAllApps() {
3378 // TODO
3379 }
3380
Winson Chungf0ea4d32011-06-06 14:27:16 -07003381 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003382 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003383 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003384 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003385 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003386 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003387 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003388 int duration = 0;
3389 if (mSearchDropTargetBar != null) {
3390 duration = mSearchDropTargetBar.getTransitionInDuration();
3391 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003392 mHotseat.animate().alpha(1f).setDuration(duration);
3393 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003394 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003395 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003396 }
3397 }
3398 }
3399
3400 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003401 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003402 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003403 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003404 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003405 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003406 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003407 int duration = 0;
3408 if (mSearchDropTargetBar != null) {
3409 duration = mSearchDropTargetBar.getTransitionOutDuration();
3410 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003411 mHotseat.animate().alpha(0f).setDuration(duration);
3412 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003413 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003414 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003415 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003416 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003417 }
3418
Patrick Dubroy5f445422011-02-18 14:35:21 -08003419 /**
3420 * Add an item from all apps or customize onto the given workspace screen.
3421 * If layout is null, add to the current screen.
3422 */
3423 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003424 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003425 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003426 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003427 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003428
Winson Chungdff8ebb2011-09-08 17:25:31 -07003429 /** Maps the current orientation to an index for referencing orientation correct global icons */
3430 private int getCurrentOrientationIndexForGlobalIcons() {
3431 // default - 0, landscape - 1
3432 switch (getResources().getConfiguration().orientation) {
3433 case Configuration.ORIENTATION_LANDSCAPE:
3434 return 1;
3435 default:
3436 return 0;
3437 }
3438 }
3439
Michael Jurkaae65ee32012-04-30 11:45:54 -07003440 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003441 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003442 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003443 // Look for the toolbar icon specified in the activity meta-data
3444 Bundle metaData = packageManager.getActivityInfo(
3445 activityName, PackageManager.GET_META_DATA).metaData;
3446 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003447 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003448 if (iconResId != 0) {
3449 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003450 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003451 }
3452 }
3453 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003454 // This can happen if the activity defines an invalid drawable
3455 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3456 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003457 } catch (Resources.NotFoundException nfe) {
3458 // This can happen if the activity defines an invalid drawable
3459 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3460 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003461 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003462 return null;
3463 }
3464
3465 // if successful in getting icon, return it; otherwise, set button to use default drawable
3466 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003467 int buttonId, ComponentName activityName, int fallbackDrawableId,
3468 String toolbarResourceName) {
3469 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003470 Resources r = getResources();
3471 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3472 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003473
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003474 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003475 // If we were unable to find the icon via the meta-data, use a generic one
3476 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003477 toolbarIcon = r.getDrawable(fallbackDrawableId);
3478 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003479 if (button != null) {
3480 button.setCompoundDrawables(toolbarIcon, null, null, null);
3481 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003482 return null;
3483 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003484 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003485 if (button != null) {
3486 button.setCompoundDrawables(toolbarIcon, null, null, null);
3487 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003488 return toolbarIcon.getConstantState();
3489 }
3490 }
3491
3492 // if successful in getting icon, return it; otherwise, set button to use default drawable
3493 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003494 int buttonId, ComponentName activityName, int fallbackDrawableId,
3495 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003496 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003497 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003498
Michael Jurka19e0fc52011-07-22 18:00:21 -07003499 if (button != null) {
3500 // If we were unable to find the icon via the meta-data, use a
3501 // generic one
3502 if (toolbarIcon == null) {
3503 button.setImageResource(fallbackDrawableId);
3504 } else {
3505 button.setImageDrawable(toolbarIcon);
3506 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003507 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003508
3509 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3510
Michael Jurka0423dcf2010-10-05 14:56:18 -07003511 }
3512
Winson Chung1b884092012-06-08 17:13:02 -07003513 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003514 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003515 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003516 }
3517
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003518 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003519 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003520 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003521 }
3522
Winson Chungbb185bd2011-11-21 12:31:42 -08003523 private void invalidatePressedFocusedStates(View container, View button) {
3524 if (container instanceof HolographicLinearLayout) {
3525 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3526 layout.invalidatePressedFocusedStates();
3527 } else if (button instanceof HolographicImageView) {
3528 HolographicImageView view = (HolographicImageView) button;
3529 view.invalidatePressedFocusedStates();
3530 }
3531 }
3532
Cristina Stancu476493b2013-08-07 17:20:14 +01003533 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003534 if (mQsb == null) {
3535 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3536 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003537 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003538 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003539 }
3540
3541 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003542 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003543 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003544 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003545 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003546
Winson Chung1cad91e2011-05-25 17:41:01 -07003547 final SearchManager searchManager =
3548 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3549 ComponentName activityName = searchManager.getGlobalSearchActivity();
3550 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003551 int coi = getCurrentOrientationIndexForGlobalIcons();
3552 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003553 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3554 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3555 if (sGlobalSearchIcon[coi] == null) {
3556 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3557 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3558 TOOLBAR_ICON_METADATA_NAME);
3559 }
3560
Winson Chungc51db6a2011-10-05 11:44:49 -07003561 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3562 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003563 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003564 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003565 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003566 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003567 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3568 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003569 if (searchButton != null) searchButton.setVisibility(View.GONE);
3570 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003571 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003572 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003573 }
3574 }
3575
Cristina Stancu476493b2013-08-07 17:20:14 +01003576 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003577 final View searchButtonContainer = findViewById(R.id.search_button_container);
3578 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003579 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003580 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003581 }
3582
Cristina Stancu476493b2013-08-07 17:20:14 +01003583 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003584 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003585 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003586
Winson Chungc51db6a2011-10-05 11:44:49 -07003587 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003588 final SearchManager searchManager =
3589 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3590 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3591
3592 ComponentName activityName = null;
3593 if (globalSearchActivity != null) {
3594 // Check if the global search activity handles voice search
3595 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3596 intent.setPackage(globalSearchActivity.getPackageName());
3597 activityName = intent.resolveActivity(getPackageManager());
3598 }
3599
3600 if (activityName == null) {
3601 // Fallback: check if an activity other than the global search activity
3602 // resolves this
3603 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3604 activityName = intent.resolveActivity(getPackageManager());
3605 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003606 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003607 int coi = getCurrentOrientationIndexForGlobalIcons();
3608 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003609 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3610 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3611 if (sVoiceSearchIcon[coi] == null) {
3612 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3613 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3614 TOOLBAR_ICON_METADATA_NAME);
3615 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003616 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003617 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003618 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003619 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003620 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003621 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003622 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003623 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003624 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003625 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003626 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003627 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003628
Cristina Stancu476493b2013-08-07 17:20:14 +01003629 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003630 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3631 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003632 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003633 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003634 }
3635
Winson Chung5841efa2013-09-30 18:06:44 -07003636 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003637 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3638 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003639 boolean visible = !forceDisableVoiceButtonProxy &&
3640 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003641 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003642 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003643 }
3644 }
Winson Chung5841efa2013-09-30 18:06:44 -07003645
3646 /**
3647 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3648 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3649 */
3650 public void disableVoiceButtonProxy(boolean disabled) {
3651 updateVoiceButtonProxyVisible(disabled);
3652 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003653 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003654 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003655 */
3656 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003657 if (!DISABLE_MARKET_BUTTON) {
3658 final View marketButton = findViewById(R.id.market_button);
3659 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3660 // Find the app market activity by resolving an intent.
3661 // (If multiple app markets are installed, it will return the ResolverActivity.)
3662 ComponentName activityName = intent.resolveActivity(getPackageManager());
3663 if (activityName != null) {
3664 int coi = getCurrentOrientationIndexForGlobalIcons();
3665 mAppMarketIntent = intent;
3666 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3667 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3668 TOOLBAR_ICON_METADATA_NAME);
3669 marketButton.setVisibility(View.VISIBLE);
3670 } else {
3671 // We should hide and disable the view so that we don't try and restore the visibility
3672 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3673 marketButton.setVisibility(View.GONE);
3674 marketButton.setEnabled(false);
3675 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003676 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003677 }
3678
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003679 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003680 if (!DISABLE_MARKET_BUTTON) {
3681 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3682 Resources r = getResources();
3683 Drawable marketIconDrawable = d.newDrawable(r);
3684 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3685 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3686 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003687
Winson Chungd64a6662013-09-30 11:06:59 -07003688 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3689 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003690 }
3691
Michael Jurkad7c28052012-04-27 15:43:36 -07003692 @Override
3693 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003694 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003695 final List<CharSequence> text = event.getText();
3696 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003697 // Populate event with a fake title based on the current state.
3698 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003699 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003700 } else {
3701 text.add(getString(R.string.all_apps_home_button_label));
3702 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003703 return result;
3704 }
3705
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003706 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003707 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003708 */
3709 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3710 @Override
3711 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003712 closeSystemDialogs();
3713 }
3714 }
3715
3716 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003717 * Receives notifications whenever the appwidgets are reset.
3718 */
3719 private class AppWidgetResetObserver extends ContentObserver {
3720 public AppWidgetResetObserver() {
3721 super(new Handler());
3722 }
3723
3724 @Override
3725 public void onChange(boolean selfChange) {
3726 onAppWidgetReset();
3727 }
3728 }
3729
3730 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003731 * If the activity is currently paused, signal that we need to run the passed Runnable
3732 * in onResume.
3733 *
3734 * This needs to be called from incoming places where resources might have been loaded
3735 * while we are paused. That is becaues the Configuration might be wrong
3736 * when we're not running, and if it comes back to what it was when we
3737 * were paused, we are not restarted.
3738 *
3739 * Implementation of the method from LauncherModel.Callbacks.
3740 *
3741 * @return true if we are currently paused. The caller might be able to
3742 * skip some work in that case since we will come back again.
3743 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003744 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003745 if (mPaused) {
3746 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003747 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003748 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003749 }
3750 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003751 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003752 return true;
3753 } else {
3754 return false;
3755 }
3756 }
3757
Michael Jurkac402cd92013-05-20 15:49:32 +02003758 private boolean waitUntilResume(Runnable run) {
3759 return waitUntilResume(run, false);
3760 }
3761
Michael Jurka1e2f4652013-07-08 18:03:46 -07003762 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003763 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003764 }
3765
Michael Jurka7607c2f2013-04-03 14:33:19 -07003766 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003767 * If the activity is currently paused, signal that we need to re-run the loader
3768 * in onResume.
3769 *
3770 * This needs to be called from incoming places where resources might have been loaded
3771 * while we are paused. That is becaues the Configuration might be wrong
3772 * when we're not running, and if it comes back to what it was when we
3773 * were paused, we are not restarted.
3774 *
3775 * Implementation of the method from LauncherModel.Callbacks.
3776 *
3777 * @return true if we are currently paused. The caller might be able to
3778 * skip some work in that case since we will come back again.
3779 */
3780 public boolean setLoadOnResume() {
3781 if (mPaused) {
3782 Log.i(TAG, "setLoadOnResume");
3783 mOnResumeNeedsLoad = true;
3784 return true;
3785 } else {
3786 return false;
3787 }
3788 }
3789
3790 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003792 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003794 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003795 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003796 } else {
3797 return SCREEN_COUNT / 2;
3798 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003799 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003800
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 /**
3802 * Refreshes the shortcuts shown on the workspace.
3803 *
3804 * Implementation of the method from LauncherModel.Callbacks.
3805 */
3806 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003807 // If we're starting binding all over again, clear any bind calls we'd postponed in
3808 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3809 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003810 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003811
Winson Chungd64d1762013-08-20 14:37:16 -07003812 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003813 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003814 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003815
Michael Jurka05bf6442011-11-30 20:28:53 -08003816 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003817 if (mHotseat != null) {
3818 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003819 }
3820 }
3821
Adam Cohendcd297f2013-06-18 13:13:40 -07003822 @Override
3823 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003824 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003825
Adam Cohen5084cba2013-09-03 12:01:16 -07003826 // If there are no screens, we need to have an empty screen
3827 if (orderedScreenIds.size() == 0) {
3828 mWorkspace.addExtraEmptyScreen();
3829 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003830
3831 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003832 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003833 if (hasCustomContentToLeft()) {
3834 mWorkspace.createCustomContentContainer();
3835 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003836 }
Winson Chung64359a52013-07-08 17:17:08 -07003837 }
3838
3839 @Override
3840 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003841 // Log to disk
3842 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3843 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3844 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003845 int count = orderedScreenIds.size();
3846 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003847 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003848 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003849 }
3850
Adam Cohen39a06042013-07-19 14:30:12 -07003851 private boolean shouldShowWeightWatcher() {
3852 String spKey = LauncherAppState.getSharedPreferencesKey();
3853 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003854 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003855
3856 return show;
3857 }
3858
3859 private void toggleShowWeightWatcher() {
3860 String spKey = LauncherAppState.getSharedPreferencesKey();
3861 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3862 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3863
3864 show = !show;
3865
3866 SharedPreferences.Editor editor = sp.edit();
3867 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3868 editor.commit();
3869
3870 if (mWeightWatcher != null) {
3871 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3872 }
3873 }
3874
Winson Chungd64d1762013-08-20 14:37:16 -07003875 public void bindAppsAdded(final ArrayList<Long> newScreens,
3876 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003877 final ArrayList<ItemInfo> addAnimated,
3878 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003879 Runnable r = new Runnable() {
3880 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003881 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003882 }
3883 };
3884 if (waitUntilResume(r)) {
3885 return;
3886 }
3887
Winson Chungd64d1762013-08-20 14:37:16 -07003888 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003889 if (newScreens != null) {
3890 bindAddScreens(newScreens);
3891 }
Winson Chungd64d1762013-08-20 14:37:16 -07003892
3893 // We add the items without animation on non-visible pages, and with
3894 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003895 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003896 bindItems(addNotAnimated, 0,
3897 addNotAnimated.size(), false);
3898 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003899 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003900 bindItems(addAnimated, 0,
3901 addAnimated.size(), true);
3902 }
Winson Chungc58497e2013-09-03 17:48:37 -07003903
Winson Chung87412982013-10-03 18:34:14 -07003904 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003905 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003906
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003907 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003908 addedApps != null && mAppsCustomizeContent != null) {
3909 mAppsCustomizeContent.addApps(addedApps);
3910 }
Winson Chungd64d1762013-08-20 14:37:16 -07003911 }
3912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003913 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003914 * Bind the items start-end from the list.
3915 *
3916 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003917 */
Winson Chung64359a52013-07-08 17:17:08 -07003918 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3919 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003920 Runnable r = new Runnable() {
3921 public void run() {
3922 bindItems(shortcuts, start, end, forceAnimateIcons);
3923 }
3924 };
3925 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003926 return;
3927 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003928
Winson Chungf0c6ae02012-03-21 16:10:31 -07003929 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003930 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3931 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003932 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003933 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003934 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003935 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003937
3938 // Short circuit if we are loading dock items for a configuration which has no dock
3939 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3940 mHotseat == null) {
3941 continue;
3942 }
3943
Joe Onorato9c1289c2009-08-17 11:03:03 -04003944 switch (item.itemType) {
3945 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3946 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003947 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003948 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003949
Winson Chung64359a52013-07-08 17:17:08 -07003950 /*
3951 * TODO: FIX collision case
3952 */
Winson Chungce376632013-07-11 16:12:41 -07003953 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3954 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3955 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3956 throw new RuntimeException("OCCUPIED");
3957 }
Winson Chung64359a52013-07-08 17:17:08 -07003958 }
3959
Adam Cohendcd297f2013-06-18 13:13:40 -07003960 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3961 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003962 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003963 // Animate all the applications up now
3964 shortcut.setAlpha(0f);
3965 shortcut.setScaleX(0f);
3966 shortcut.setScaleY(0f);
3967 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003968 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003971 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003972 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003973 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003974 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003975 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3976 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003977 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003978 default:
3979 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003981 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003982
Winson Chung997a9232013-07-24 15:33:46 -07003983 if (animateIcons) {
3984 // Animate to the correct page
3985 if (newShortcutsScreenId > -1) {
3986 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003987 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003988 final Runnable startBounceAnimRunnable = new Runnable() {
3989 public void run() {
3990 anim.playTogether(bounceAnims);
3991 anim.start();
3992 }
3993 };
Winson Chung997a9232013-07-24 15:33:46 -07003994 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003995 // We post the animation slightly delayed to prevent slowdowns
3996 // when we are loading right after we return to launcher.
3997 mWorkspace.postDelayed(new Runnable() {
3998 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003999 if (mWorkspace != null) {
4000 mWorkspace.snapToPage(newScreenIndex);
4001 mWorkspace.postDelayed(startBounceAnimRunnable,
4002 NEW_APPS_ANIMATION_DELAY);
4003 }
Winson Chung94d67682013-09-25 16:29:40 -07004004 }
4005 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004006 } else {
4007 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004008 }
4009 }
Winson Chung64359a52013-07-08 17:17:08 -07004010 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004011 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004012 }
4013
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 /**
4015 * Implementation of the method from LauncherModel.Callbacks.
4016 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004017 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004018 Runnable r = new Runnable() {
4019 public void run() {
4020 bindFolders(folders);
4021 }
4022 };
4023 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004024 return;
4025 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004026 sFolders.clear();
4027 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029
4030 /**
4031 * Add the views for a widget to the workspace.
4032 *
4033 * Implementation of the method from LauncherModel.Callbacks.
4034 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004035 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004036 Runnable r = new Runnable() {
4037 public void run() {
4038 bindAppWidget(item);
4039 }
4040 };
4041 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004042 return;
4043 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004044
Daniel Sandler843e8602010-06-07 14:59:01 -04004045 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4046 if (DEBUG_WIDGETS) {
4047 Log.d(TAG, "bindAppWidget: " + item);
4048 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004049 final Workspace workspace = mWorkspace;
4050
4051 final int appWidgetId = item.appWidgetId;
4052 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004053 if (DEBUG_WIDGETS) {
4054 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4055 }
4056
Joe Onorato9c1289c2009-08-17 11:03:03 -04004057 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4058
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004060 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061
Adam Cohendcd297f2013-06-18 13:13:40 -07004062 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004064 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4065
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 workspace.requestLayout();
4067
Daniel Sandler843e8602010-06-07 14:59:01 -04004068 if (DEBUG_WIDGETS) {
4069 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4070 + (SystemClock.uptimeMillis()-start) + "ms");
4071 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 }
4073
Adam Cohen1462de32012-07-24 22:34:36 -07004074 public void onPageBoundSynchronously(int page) {
4075 mSynchronouslyBoundPages.add(page);
4076 }
4077
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 /**
4079 * Callback saying that there aren't any more items to bind.
4080 *
4081 * Implementation of the method from LauncherModel.Callbacks.
4082 */
Adam Cohene25af792013-06-06 23:08:25 -07004083 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004084 Runnable r = new Runnable() {
4085 public void run() {
4086 finishBindingItems(upgradePath);
4087 }
4088 };
4089 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004090 return;
4091 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004092 if (mSavedState != null) {
4093 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004094 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 mSavedState = null;
4097 }
4098
Adam Cohen1462de32012-07-24 22:34:36 -07004099 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004101 // If we received the result of any pending adds while the loader was running (e.g. the
4102 // widget configuration forced an orientation change), process them now.
4103 for (int i = 0; i < sPendingAddList.size(); i++) {
4104 completeAdd(sPendingAddList.get(i));
4105 }
4106 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107
Winson Chungc51db6a2011-10-05 11:44:49 -07004108 // Update the market app icon as necessary (the other icons will be managed in response to
4109 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004110 if (!DISABLE_MARKET_BUTTON) {
4111 updateAppMarketIcon();
4112 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004113
Winson Chungf0c6ae02012-03-21 16:10:31 -07004114 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004115 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004116 mWorkspace.getUniqueComponents(true, null);
4117 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004118 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004119 }
4120
Winson Chunga0b7e862013-09-05 16:03:15 -07004121 public boolean isAllAppsButtonRank(int rank) {
4122 if (mHotseat != null) {
4123 return mHotseat.isAllAppsButtonRank(rank);
4124 }
4125 return false;
4126 }
4127
Winson Chunga2413752012-04-03 14:22:34 -07004128 private boolean canRunNewAppsAnimation() {
4129 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4130 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4131 }
4132
Winson Chungc9168342013-06-26 14:54:55 -07004133 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4134 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4135 PropertyValuesHolder.ofFloat("alpha", 1f),
4136 PropertyValuesHolder.ofFloat("scaleX", 1f),
4137 PropertyValuesHolder.ofFloat("scaleY", 1f));
4138 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4139 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4140 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4141 return bounceAnim;
4142 }
4143
Adam Cohen27772732013-11-11 14:00:56 +00004144 public boolean useVerticalBarLayout() {
4145 return LauncherAppState.getInstance().getDynamicGrid().
4146 getDeviceProfile().isVerticalBarLayout();
4147 }
4148
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004149 protected Rect getSearchBarBounds() {
4150 return LauncherAppState.getInstance().getDynamicGrid().
4151 getDeviceProfile().getSearchBarBounds();
4152 }
4153
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004154 @Override
4155 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004156 boolean searchVisible = updateGlobalSearchIcon();
4157 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004158 if (mSearchDropTargetBar != null) {
4159 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4160 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004161 }
4162
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004163 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164 * Add the icons for all apps.
4165 *
4166 * Implementation of the method from LauncherModel.Callbacks.
4167 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004168 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004169 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004170 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4171 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4172 getHotseat().addAllAppsFolder(mIconCache, apps,
4173 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4174 }
4175 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004176 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004177 if (mAppsCustomizeContent != null) {
4178 mAppsCustomizeContent.onPackagesUpdated(
4179 LauncherModel.getSortedWidgetsAndShortcuts(this));
4180 }
Winson Chungc58497e2013-09-03 17:48:37 -07004181 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004182 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004183 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004184 mAppsCustomizeContent.onPackagesUpdated(
4185 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004186 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 }
4189
4190 /**
4191 * A package was updated.
4192 *
4193 * Implementation of the method from LauncherModel.Callbacks.
4194 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004195 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004196 Runnable r = new Runnable() {
4197 public void run() {
4198 bindAppsUpdated(apps);
4199 }
4200 };
4201 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004202 return;
4203 }
4204
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004205 if (mWorkspace != null) {
4206 mWorkspace.updateShortcuts(apps);
4207 }
Winson Chungc58497e2013-09-03 17:48:37 -07004208
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004209 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004210 mAppsCustomizeContent != null) {
4211 mAppsCustomizeContent.updateApps(apps);
4212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004213 }
4214
4215 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004216 * A package was uninstalled. We take both the super set of packageNames
4217 * in addition to specific applications to remove, the reason being that
4218 * this can be called when a package is updated as well. In that scenario,
4219 * we only remove specific components from the workspace, where as
4220 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004221 *
4222 * Implementation of the method from LauncherModel.Callbacks.
4223 */
Winson Chung83892cc2013-05-01 16:53:33 -07004224 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004225 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004226 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004227 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004228 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004229 }
Winson Chungd64d1762013-08-20 14:37:16 -07004230 };
4231 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004232 return;
4233 }
4234
Winson Chungdf95eb12013-10-16 14:57:07 -07004235 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004236 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004237 }
4238 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004239 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004240 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004241
Winson Chunga1820962011-10-03 16:31:06 -07004242 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004243 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004244
Winson Chungdf95eb12013-10-16 14:57:07 -07004245 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004246 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004247 mAppsCustomizeContent != null) {
4248 mAppsCustomizeContent.removeApps(appInfos);
4249 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004250 }
Joe Onoratobe386092009-11-17 17:32:16 -08004251
4252 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004253 * A number of packages were updated.
4254 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004255 private ArrayList<Object> mWidgetsAndShortcuts;
4256 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004257 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004258 bindPackagesUpdated(mWidgetsAndShortcuts);
4259 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004260 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004261 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004262 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4263 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4264 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004265 return;
4266 }
4267
Winson Chung64359a52013-07-08 17:17:08 -07004268 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004269 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004270 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004271 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004272 }
4273
Winson Chung400438b2011-01-16 17:53:48 -08004274 private int mapConfigurationOriActivityInfoOri(int configOri) {
4275 final Display d = getWindowManager().getDefaultDisplay();
4276 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4277 switch (d.getRotation()) {
4278 case Surface.ROTATION_0:
4279 case Surface.ROTATION_180:
4280 // We are currently in the same basic orientation as the natural orientation
4281 naturalOri = configOri;
4282 break;
4283 case Surface.ROTATION_90:
4284 case Surface.ROTATION_270:
4285 // We are currently in the other basic orientation to the natural orientation
4286 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4287 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4288 break;
4289 }
4290
4291 int[] oriMap = {
4292 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4293 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4294 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4295 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4296 };
4297 // Since the map starts at portrait, we need to offset if this device's natural orientation
4298 // is landscape.
4299 int indexOffset = 0;
4300 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4301 indexOffset = 1;
4302 }
4303 return oriMap[(d.getRotation() + indexOffset) % 4];
4304 }
Adam Cohen446e9402011-09-15 18:21:21 -07004305
Winson Chung4b919f82012-05-01 10:44:08 -07004306 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004307 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004308 getResources().getBoolean(R.bool.allow_rotation);
4309 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004310 }
Winson Chung4b919f82012-05-01 10:44:08 -07004311 public void lockScreenOrientation() {
4312 if (isRotationEnabled()) {
4313 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4314 .getConfiguration().orientation));
4315 }
4316 }
4317 public void unlockScreenOrientation(boolean immediate) {
4318 if (isRotationEnabled()) {
4319 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004320 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004321 } else {
4322 mHandler.postDelayed(new Runnable() {
4323 public void run() {
4324 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4325 }
4326 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004327 }
Winson Chung4b919f82012-05-01 10:44:08 -07004328 }
Winson Chung400438b2011-01-16 17:53:48 -08004329 }
4330
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004331 /**
4332 * Called when the SearchBar hint should be changed.
4333 *
4334 * @param hint the hint to be displayed in the search bar.
4335 */
4336 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004337 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004338 }
4339
Winson Chunge43a1e72014-01-15 10:33:02 -08004340 protected boolean isLauncherPreinstalled() {
4341 PackageManager pm = getPackageManager();
4342 try {
4343 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4344 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4345 return true;
4346 } else {
4347 return false;
4348 }
4349 } catch (NameNotFoundException e) {
4350 e.printStackTrace();
4351 return false;
4352 }
4353 }
4354
Winson Chunge6eabff2013-09-24 11:01:23 -07004355 protected String getFirstRunClingSearchBarHint() {
4356 return "";
4357 }
4358 protected String getFirstRunCustomContentHint() {
4359 return "";
4360 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004361 protected int getFirstRunFocusedHotseatAppDrawableId() {
4362 return -1;
4363 }
4364 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4365 return null;
4366 }
4367 protected int getFirstRunFocusedHotseatAppRank() {
4368 return -1;
4369 }
4370 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4371 return "";
4372 }
4373 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4374 return "";
4375 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004376
Winson Chungaf40f202013-09-18 18:26:31 -07004377 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004378 mLauncherClings.dismissFirstRunCling(v);
4379 }
4380 public void dismissMigrationClingCopyApps(View v) {
4381 mLauncherClings.dismissMigrationClingCopyApps(v);
4382 }
4383 public void dismissMigrationClingUseDefault(View v) {
4384 mLauncherClings.dismissMigrationClingUseDefault(v);
4385 }
4386 public void dismissMigrationWorkspaceCling(View v) {
4387 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004388 }
Winson Chung82f55532011-08-09 14:14:23 -07004389 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004390 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004391 }
4392 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004393 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004394 }
4395
Winson Chunga6945242014-01-08 14:04:34 -08004396 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004397 return !ActivityManager.isRunningInTestHarness() &&
4398 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004399 }
4400
4401 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004402 if (shouldRunFirstRunActivity() &&
4403 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004404 Intent firstRunIntent = getFirstRunActivity();
4405 if (firstRunIntent != null) {
4406 startActivity(firstRunIntent);
4407 markFirstRunActivityShown();
4408 }
4409 }
4410 }
4411
4412 private void markFirstRunActivityShown() {
4413 SharedPreferences.Editor editor = mSharedPrefs.edit();
4414 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4415 editor.apply();
4416 }
4417
4418 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004419 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4420 if (mHotseat != null) mHotseat.setAlpha(1f);
4421 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4422 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004423 }
4424
4425 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004426 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4427 if (mHotseat != null) mHotseat.setAlpha(0f);
4428 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4429 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004430 }
4431
4432
Mathew Inwood72fbec12013-11-19 15:45:07 +00004433 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4434 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4435 if (ri == null) {
4436 return null;
4437 }
4438 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4439 }
4440
4441 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4442 Bitmap icon) {
4443 return new ShortcutInfo(shortcutIntent, caption, icon);
4444 }
4445
4446 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4447 dragView.setTag(dragInfo);
4448 mWorkspace.onDragStartedWithItem(dragView);
4449 mWorkspace.beginDragShared(dragView, source);
4450 }
4451
Winson Chung80baf5a2010-08-09 16:03:15 -07004452 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004453 * Prints out out state for debugging.
4454 */
4455 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004456 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004457 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004458 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4459 Log.d(TAG, "mRestoring=" + mRestoring);
4460 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4461 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004462 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004463 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004464
Winson Chung785d2eb2011-04-14 16:08:02 -07004465 if (mAppsCustomizeContent != null) {
4466 mAppsCustomizeContent.dumpState();
4467 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004468 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004469 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004470
4471 @Override
4472 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4473 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004474 synchronized (sDumpLogs) {
4475 writer.println(" ");
4476 writer.println("Debug logs: ");
4477 for (int i = 0; i < sDumpLogs.size(); i++) {
4478 writer.println(" " + sDumpLogs.get(i));
4479 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004480 }
4481 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004482
4483 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004484 if (DEBUG_DUMP_LOG) {
4485 synchronized (sDumpLogs) {
4486 Log.d(TAG, "");
4487 Log.d(TAG, "*********************");
4488 Log.d(TAG, "Launcher debug logs: ");
4489 for (int i = 0; i < sDumpLogs.size(); i++) {
4490 Log.d(TAG, " " + sDumpLogs.get(i));
4491 }
4492 Log.d(TAG, "*********************");
4493 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004494 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004495 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004496 }
4497
4498 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004499 addDumpLog(tag, log, null, debugLog);
4500 }
4501
4502 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004503 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004504 if (e != null) {
4505 Log.d(tag, log, e);
4506 } else {
4507 Log.d(tag, log);
4508 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004509 }
Winson Chungede41292013-09-19 16:27:36 -07004510 if (DEBUG_DUMP_LOG) {
4511 sDateStamp.setTime(System.currentTimeMillis());
4512 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004513 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4514 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004515 }
Winson Chungede41292013-09-19 16:27:36 -07004516 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004517 }
4518
Winson Chungede41292013-09-19 16:27:36 -07004519 public void dumpLogsToLocalData() {
4520 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004521 new AsyncTask<Void, Void, Void>() {
4522 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004523 boolean success = false;
4524 sDateStamp.setTime(sRunStart);
4525 String FILENAME = sDateStamp.getMonth() + "-"
4526 + sDateStamp.getDay() + "_"
4527 + sDateStamp.getHours() + "-"
4528 + sDateStamp.getMinutes() + "_"
4529 + sDateStamp.getSeconds() + ".txt";
4530
4531 FileOutputStream fos = null;
4532 File outFile = null;
4533 try {
4534 outFile = new File(getFilesDir(), FILENAME);
4535 outFile.createNewFile();
4536 fos = new FileOutputStream(outFile);
4537 } catch (Exception e) {
4538 e.printStackTrace();
4539 }
4540 if (fos != null) {
4541 PrintWriter writer = new PrintWriter(fos);
4542
4543 writer.println(" ");
4544 writer.println("Debug logs: ");
4545 synchronized (sDumpLogs) {
4546 for (int i = 0; i < sDumpLogs.size(); i++) {
4547 writer.println(" " + sDumpLogs.get(i));
4548 }
4549 }
4550 writer.close();
4551 }
4552 try {
4553 if (fos != null) {
4554 fos.close();
4555 success = true;
4556 }
4557 } catch (IOException e) {
4558 e.printStackTrace();
4559 }
Michael Jurka43467462013-11-14 12:03:58 +01004560 return null;
Winson Chungede41292013-09-19 16:27:36 -07004561 }
Michael Jurka43467462013-11-14 12:03:58 +01004562 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004563 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004565}
Michael Jurka2763be32011-02-24 11:19:57 -08004566
Michael Jurkaabded662011-03-04 12:06:57 -08004567interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004568 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004569 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004570 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004571 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004572 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004573}
Dan Sandlerd5024042014-01-09 15:01:33 -05004574
4575interface DebugIntents {
4576 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4577 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004578}