blob: 34e8b783c43cc03c811e69a2368f865970f1f258 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070050import android.graphics.Bitmap;
51import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070052import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070070import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
94import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Daniel Sandler325dc232013-06-05 22:57:57 -040097import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700101import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000113import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
Winson Chunga6945242014-01-08 14:04:34 -0800115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100119public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Daniel Sandlerf061f822013-06-27 13:59:36 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400128 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700129 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int REQUEST_PICK_APPLICATION = 6;
134 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700136 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Michael Jurka8b805b12012-04-18 14:23:14 -0700138 private static final int REQUEST_BIND_APPWIDGET = 11;
139
Mathew Inwood876a8462013-06-14 14:12:41 +0100140 /**
141 * IntentStarter uses request codes starting with this. This must be greater than all activity
142 * request codes used internally.
143 */
144 protected static final int REQUEST_LAST = 100;
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
147
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800148 static final int SCREEN_COUNT = 5;
149 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800152 // To turn on these properties, type
153 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
154 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
155 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Winson Chung2672ff92012-05-04 16:22:30 -0700157 // The Intent extra that defines whether to ignore the launch animation
158 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400159 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
162 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700163 // Type: int
164 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700166 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
167 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
169 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700172 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700177 // Type: int
178 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
179 // Type: int
180 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
181 // Type: parcelable
182 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000183 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000185 // Type: int[]
186 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Adam Cohenb54a5982014-01-08 15:21:04 -0800188
189 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
190
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100191 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700192 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100193 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700194 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100195 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700196
Adam Cohen39a06042013-07-19 14:30:12 -0700197 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700198 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700199
Winson Chunga6945242014-01-08 14:04:34 -0800200 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
201
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700203 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700204 private State mState = State.WORKSPACE;
205 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700208 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
209 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800212 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000214 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
215 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
216
Winson Chunga2413752012-04-03 14:22:34 -0700217 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700218 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
219 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700220 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700221
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800222 private final BroadcastReceiver mCloseSystemDialogsReceiver
223 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800224 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private LayoutInflater mInflater;
227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700229 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800230 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800231 private DragLayer mDragLayer;
232 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700233 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800234 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700235
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700236 private AppWidgetManager mAppWidgetManager;
237 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Michael Jurkac9d95c52011-08-29 14:03:34 -0700239 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700240 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800241 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700242
Michael Jurka0280c3b2010-09-17 15:00:07 -0700243 private int[] mTmpAddItemCellCoordinates = new int[2];
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private FolderInfo mFolderInfo;
246
Winson Chung3d503fb2011-07-13 17:25:49 -0700247 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700248 private View mOverviewPanel;
249
Michael Jurka838a4ca2011-02-07 13:33:06 -0800250 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700251
Winson Chungc51db6a2011-10-05 11:44:49 -0700252 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700253 private AppsCustomizeTabHost mAppsCustomizeTabHost;
254 private AppsCustomizePagedView mAppsCustomizeContent;
255 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100256 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700259 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
260 // scroll issues (because the workspace may not have been measured yet) and extra work.
261 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
262 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
264 private SpannableStringBuilder mDefaultKeySsb = null;
265
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 private boolean mWorkspaceLoading = true;
267
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800268 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private boolean mRestoring;
270 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700271 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
Michael Jurka1e2f4652013-07-08 18:03:46 -0700273 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700274 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
275
Winson Chung4a2afa32012-07-19 14:53:05 -0700276 // Keep track of whether the user has left launcher
277 private static boolean sPausedFromUserAction = false;
278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedInstanceState;
280
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800282 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800283 private boolean mUserPresent = true;
284 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700285 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700288 private static LocaleConfiguration sLocaleConfiguration = null;
289
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700290 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700291
Adam Cohen61f560d2013-09-30 15:58:20 -0700292 private View.OnTouchListener mHapticFeedbackTouchListener;
293
Adam Cohended9f8d2010-11-03 13:25:16 -0700294 // Related to the auto-advancing of widgets
295 private final int ADVANCE_MSG = 1;
296 private final int mAdvanceInterval = 20000;
297 private final int mAdvanceStagger = 250;
298 private long mAutoAdvanceSentTime;
299 private long mAutoAdvanceTimeLeft = -1;
300 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
301 new HashMap<View, AppWidgetProviderInfo>();
302
Winson Chung400438b2011-01-16 17:53:48 -0800303 // Determines how long to wait after a rotation before restoring the screen orientation to
304 // match the sensor state.
305 private final int mRestoreScreenOrientationDelay = 500;
306
Michael Jurka4ef207b2010-11-29 17:05:45 -0800307 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700308 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
309 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
310 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800311
Winson Chungd64a6662013-09-30 11:06:59 -0700312 private Intent mAppMarketIntent = null;
313 private static final boolean DISABLE_MARKET_BUTTON = true;
314
Craig Mautner360310b2012-10-26 15:13:08 -0700315 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700316
Adam Cohen1462de32012-07-24 22:34:36 -0700317 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700318 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700319
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700320 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700321 static Date sDateStamp = new Date();
322 static DateFormat sDateFormat =
323 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
324 static long sRunStart = System.currentTimeMillis();
325 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700326
Winson Chung46353de2012-02-16 14:05:10 -0800327 // We only want to get the SharedPreferences once since it does an FS stat each time we get
328 // it from the context.
329 private SharedPreferences mSharedPrefs;
330
Adam Cohene25af792013-06-06 23:08:25 -0700331 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
332
Winson Chungf0c6ae02012-03-21 16:10:31 -0700333 // Holds the page that we need to animate to, and the icon views that we need to animate up
334 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700335 private ImageView mFolderIconImageView;
336 private Bitmap mFolderIconBitmap;
337 private Canvas mFolderIconCanvas;
338 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700339
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 private BubbleTextView mWaitingForResume;
341
Michael Jurkac1f5d262011-09-30 19:32:27 -0700342 private Runnable mBuildLayersRunnable = new Runnable() {
343 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700344 if (mWorkspace != null) {
345 mWorkspace.buildPageHardwareLayers();
346 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700347 }
348 };
349
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800350 private static ArrayList<PendingAddArguments> sPendingAddList
351 = new ArrayList<PendingAddArguments>();
352
Michael Jurka7267fa52013-09-26 15:29:57 -0700353 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800354
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800355 private static class PendingAddArguments {
356 int requestCode;
357 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700358 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700359 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800360 int cellX;
361 int cellY;
362 }
363
Daniel Sandlerff02d492013-08-05 02:12:05 -0400364 private Stats mStats;
365
Michael Jurka0a457bf2012-11-19 14:05:05 -0800366 private static boolean isPropertyEnabled(String propertyName) {
367 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700368 }
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 @Override
371 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700372 if (DEBUG_STRICT_MODE) {
373 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
374 .detectDiskReads()
375 .detectDiskWrites()
376 .detectNetwork() // or .detectAll() for all detectable problems
377 .penaltyLog()
378 .build());
379 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
380 .detectLeakedSqlLiteObjects()
381 .detectLeakedClosableObjects()
382 .penaltyLog()
383 .penaltyDeath()
384 .build());
385 }
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400388
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400389 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400390 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700391
392 // Determine the dynamic grid properties
393 Point smallestSize = new Point();
394 Point largestSize = new Point();
395 Point realSize = new Point();
396 Display display = getWindowManager().getDefaultDisplay();
397 display.getCurrentSizeRange(smallestSize, largestSize);
398 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700399 DisplayMetrics dm = new DisplayMetrics();
400 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700401
Winson Chung5f8afe62013-08-12 16:19:28 -0700402 // Lazy-initialize the dynamic grid
403 DeviceProfile grid = app.initDynamicGrid(this,
404 Math.min(smallestSize.x, smallestSize.y),
405 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700406 realSize.x, realSize.y,
407 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700408
409 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700411 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 mModel = app.setLauncher(this);
413 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700414 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400415 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800416 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Daniel Sandlerff02d492013-08-05 02:12:05 -0400419 mStats = new Stats(this);
420
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700421 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700422
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
424 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
427 // this also ensures that any synchronous binding below doesn't re-trigger another
428 // LauncherModel load.
429 mPaused = false;
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200432 android.os.Debug.startMethodTracing(
433 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
Adam Cohen53805212013-10-01 10:39:23 -0700436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100441 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800443 registerContentObservers();
444
Joe Onorato7c312c12009-08-13 21:36:53 -0700445 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 mSavedState = savedInstanceState;
448 restoreState(mSavedState);
449
450 if (PROFILE_STARTUP) {
451 android.os.Debug.stopMethodTracing();
452 }
453
454 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700455 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700456 // If the user leaves launcher, then we should just load items asynchronously when
457 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500458 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 } else {
460 // We only load the page synchronously if the user rotates (or triggers a
461 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800462 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465
466 // For handling default keys
467 mDefaultKeySsb = new SpannableStringBuilder();
468 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800469
470 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
471 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800472
Adam Cohenf9426d52012-06-04 17:26:21 -0700473 updateGlobalIcons();
474
475 // On large interfaces, we want the screen to auto-rotate based on the current orientation
476 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700477
Adam Cohenb54a5982014-01-08 15:21:04 -0800478 showFirstRunActivity();
Winson Chunga6945242014-01-08 14:04:34 -0800479 if (mModel.canMigrateFromOldLauncherDb()) {
480 mLauncherClings.showMigrationCling();
481 } else {
482 mLauncherClings.showFirstRunCling();
483 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700484 }
485
Winson Chung4a2afa32012-07-19 14:53:05 -0700486 protected void onUserLeaveHint() {
487 super.onUserLeaveHint();
488 sPausedFromUserAction = true;
489 }
490
Winson Chung98ca0f72013-07-29 12:58:51 -0700491 /** To be overriden by subclasses to hint to Launcher that we have custom content */
492 protected boolean hasCustomContentToLeft() {
493 return false;
494 }
495
Dave Hawkeya8881582013-09-17 15:55:33 -0600496 /**
497 * To be overridden by subclasses to create the custom content and call
498 * {@link #addToCustomContentPage}. This will only be invoked if
499 * {@link #hasCustomContentToLeft()} is {@code true}.
500 */
501 protected void addCustomContentToLeft() {
502 }
503
504 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800505 * To be overridden by subclasses to indicate that there is an activity to launch
506 * before showing the standard launcher experience.
507 */
508 protected boolean hasFirstRunActivity() {
509 return false;
510 }
511
512 /**
513 * To be overridden by subclasses to launch any first run activity
514 */
515 protected Intent getFirstRunActivity() {
516 return null;
517 }
518
519 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600520 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
521 * ensure the custom content page is added or removed if necessary.
522 */
523 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600524 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600525 // Not bound yet, wait for bindScreens to be called.
526 return;
527 }
528
529 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
530 // Create the custom content page and call the subclass to populate it.
531 mWorkspace.createCustomContentPage();
532 addCustomContentToLeft();
533 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
534 mWorkspace.removeCustomContentPage();
535 }
536 }
537
Adam Cohenf9426d52012-06-04 17:26:21 -0700538 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700539 boolean searchVisible = false;
540 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800541 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700542 int coi = getCurrentOrientationIndexForGlobalIcons();
543 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
544 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700545 if (!DISABLE_MARKET_BUTTON) {
546 updateAppMarketIcon();
547 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700548 searchVisible = updateGlobalSearchIcon();
549 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700550 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700551 if (sGlobalSearchIcon[coi] != null) {
552 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700553 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700554 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700555 if (sVoiceSearchIcon[coi] != null) {
556 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700557 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700558 }
Winson Chungd64a6662013-09-30 11:06:59 -0700559 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700560 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800561 }
Winson Chungadf0c182012-08-23 14:59:07 -0700562 if (mSearchDropTargetBar != null) {
563 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
Romain Guycbb89e42009-06-08 15:52:54 -0700566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700568 if (sLocaleConfiguration == null) {
569 new AsyncTask<Void, Void, LocaleConfiguration>() {
570 @Override
571 protected LocaleConfiguration doInBackground(Void... unused) {
572 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
573 readConfiguration(Launcher.this, localeConfiguration);
574 return localeConfiguration;
575 }
576
577 @Override
578 protected void onPostExecute(LocaleConfiguration result) {
579 sLocaleConfiguration = result;
580 checkForLocaleChange(); // recursive, but now with a locale configuration
581 }
582 }.execute();
583 return;
584 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 final Configuration configuration = getResources().getConfiguration();
587
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700588 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final String locale = configuration.locale.toString();
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final int mcc = configuration.mcc;
593
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 final int mnc = configuration.mnc;
596
Romain Guy84f296c2009-11-04 15:00:44 -0800597 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598
Romain Guy84f296c2009-11-04 15:00:44 -0800599 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700600 sLocaleConfiguration.locale = locale;
601 sLocaleConfiguration.mcc = mcc;
602 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700603
Joe Onorato0589f0f2010-02-08 13:44:00 -0800604 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605
606 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100607 new AsyncTask<Void, Void, Void>() {
608 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100610 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 }
Michael Jurka43467462013-11-14 12:03:58 +0100612 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700613 }
614 }
615
616 private static class LocaleConfiguration {
617 public String locale;
618 public int mcc = -1;
619 public int mnc = -1;
620 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700621
Romain Guy98d01652009-06-30 16:21:04 -0700622 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
623 DataInputStream in = null;
624 try {
625 in = new DataInputStream(context.openFileInput(PREFERENCES));
626 configuration.locale = in.readUTF();
627 configuration.mcc = in.readInt();
628 configuration.mnc = in.readInt();
629 } catch (FileNotFoundException e) {
630 // Ignore
631 } catch (IOException e) {
632 // Ignore
633 } finally {
634 if (in != null) {
635 try {
636 in.close();
637 } catch (IOException e) {
638 // Ignore
639 }
640 }
641 }
642 }
643
644 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
645 DataOutputStream out = null;
646 try {
647 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
648 out.writeUTF(configuration.locale);
649 out.writeInt(configuration.mcc);
650 out.writeInt(configuration.mnc);
651 out.flush();
652 } catch (FileNotFoundException e) {
653 // Ignore
654 } catch (IOException e) {
655 //noinspection ResultOfMethodCallIgnored
656 context.getFileStreamPath(PREFERENCES).delete();
657 } finally {
658 if (out != null) {
659 try {
660 out.close();
661 } catch (IOException e) {
662 // Ignore
663 }
664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666 }
667
Anton Hanssona2f665f2013-09-26 12:18:32 +0100668 public Stats getStats() {
669 return mStats;
670 }
671
Bjorn Bringertc459e522013-06-07 19:36:01 +0100672 public LayoutInflater getInflater() {
673 return mInflater;
674 }
675
Winson Chung36a62fe2012-05-06 18:04:42 -0700676 boolean isDraggingEnabled() {
677 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
678 // that is subsequently removed from the workspace in startBinding().
679 return !mModel.isLoadingWorkspace();
680 }
681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 static int getScreen() {
683 synchronized (sLock) {
684 return sScreen;
685 }
686 }
687
688 static void setScreen(int screen) {
689 synchronized (sLock) {
690 sScreen = screen;
691 }
692 }
693
Winson Chung557d6ed2011-07-08 15:34:52 -0700694 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000695 * Copied from View -- the View version of the method isn't called
696 * anywhere else in our process and only exists for API level 17+,
697 * so it's ok to keep our own version with no API requirement.
698 */
699 public static int generateViewId() {
700 for (;;) {
701 final int result = sNextGeneratedId.get();
702 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
703 int newValue = result + 1;
704 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
705 if (sNextGeneratedId.compareAndSet(result, newValue)) {
706 return result;
707 }
708 }
709 }
710
711 public int getViewIdForItem(ItemInfo info) {
712 // This cast is safe given the > 2B range for int.
713 int itemId = (int) info.id;
714 if (mItemIdToViewId.containsKey(itemId)) {
715 return mItemIdToViewId.get(itemId);
716 }
717 int viewId = generateViewId();
718 mItemIdToViewId.put(itemId, viewId);
719 return viewId;
720 }
721
722 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700723 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
724 * a configuration step, this allows the proper animations to run after other transitions.
725 */
726 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700727 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 switch (args.requestCode) {
729 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700730 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 break;
733 case REQUEST_PICK_SHORTCUT:
734 processShortcut(args.intent);
735 break;
736 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700737 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700739 result = true;
740 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 case REQUEST_CREATE_APPWIDGET:
742 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700743 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700744 result = true;
745 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 }
Michael Jurka27614d22012-04-02 06:40:22 -0700747 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
748 // if you turned the screen off and then back while in All Apps, Launcher would not
749 // return to the workspace. Clearing mAddInfo.container here fixes this issue
750 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 }
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 protected void onActivityResult(
756 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700757 // Reset the startActivity waiting flag
758 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800759 int pendingAddWidgetId = mPendingAddWidgetId;
760 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700761
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 Runnable exitSpringLoaded = new Runnable() {
763 @Override
764 public void run() {
765 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
766 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
767 }
768 };
769
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
773 if (resultCode == RESULT_CANCELED) {
774 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700775 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
776 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
779 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 }
781 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200782 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
783 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
784 mWorkspace.exitOverviewMode(false);
785 }
786 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200788
Adam Cohened66b2b2012-01-23 17:28:51 -0800789 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
790 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700791
Adam Cohened66b2b2012-01-23 17:28:51 -0800792 // We have special handling for widgets
793 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800794 final int appWidgetId;
795 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
796 : -1;
797 if (widgetId < 0) {
798 appWidgetId = pendingAddWidgetId;
799 } else {
800 appWidgetId = widgetId;
801 }
802
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 final int result;
804 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800805 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700806 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
807 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700808 result = RESULT_CANCELED;
809 completeTwoStageWidgetDrop(result, appWidgetId);
810 onComplete = new Runnable() {
811 @Override
812 public void run() {
813 exitSpringLoadedDragModeDelayed(false, 0, null);
814 }
815 };
Winson Chung5aaab772012-04-27 15:24:02 -0700816 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700817 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700818 final CellLayout dropLayout =
819 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
820 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700821 onComplete = new Runnable() {
822 @Override
823 public void run() {
824 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700825 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 }
827 };
Winson Chung5aaab772012-04-27 15:24:02 -0700828 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
830 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800831 return;
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 // The pattern used here is that a user PICKs a specific application,
835 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
838 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700839 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800840 final PendingAddArguments args = new PendingAddArguments();
841 args.requestCode = requestCode;
842 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700844 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 args.cellX = mPendingAddInfo.cellX;
846 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800847 if (isWorkspaceLocked()) {
848 sPendingAddList.add(args);
849 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700850 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700852 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
853 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700854 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700855 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800858 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800859 }
860
861 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700862 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700863 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800864 Runnable onCompleteRunnable = null;
865 int animationType = 0;
866
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700867 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 if (resultCode == RESULT_OK) {
869 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
870 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700871 mPendingAddWidgetInfo);
872 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 onCompleteRunnable = new Runnable() {
874 @Override
875 public void run() {
876 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700877 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
879 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 }
881 };
882 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800883 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700886 if (mDragLayer.getAnimatedView() != null) {
887 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
888 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
889 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 // The animated view may be null in the case of a rotation during widget configuration
892 onCompleteRunnable.run();
893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 }
895
896 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700897 protected void onStop() {
898 super.onStop();
899 FirstFrameAnimatorHelper.setIsVisible(false);
900 }
901
902 @Override
903 protected void onStart() {
904 super.onStart();
905 FirstFrameAnimatorHelper.setIsVisible(true);
906 }
907
908 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200910 long startTime = 0;
911 if (DEBUG_RESUME_TIME) {
912 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400913 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700916
Winson Chung4a2afa32012-07-19 14:53:05 -0700917 // Restore the previous launcher state
918 if (mOnResumeState == State.WORKSPACE) {
919 showWorkspace(false);
920 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800921 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700922 }
923 mOnResumeState = State.NONE;
924
Craig Mautner360310b2012-10-26 15:13:08 -0700925 // Background was set to gradient in onPause(), restore to black if in all apps.
926 setWorkspaceBackground(mState == State.WORKSPACE);
927
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800928 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700930 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400931 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500932 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400933 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700934 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700936 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200937 // We might have postponed some bind calls until onResume (see waitUntilResume) --
938 // execute them here
939 long startTimeCallbacks = 0;
940 if (DEBUG_RESUME_TIME) {
941 startTimeCallbacks = System.currentTimeMillis();
942 }
943
944 if (mAppsCustomizeContent != null) {
945 mAppsCustomizeContent.setBulkBind(true);
946 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700947 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
948 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200949 }
950 if (mAppsCustomizeContent != null) {
951 mAppsCustomizeContent.setBulkBind(false);
952 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700953 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200954 if (DEBUG_RESUME_TIME) {
955 Log.d(TAG, "Time spent processing callbacks in onResume: " +
956 (System.currentTimeMillis() - startTimeCallbacks));
957 }
Michael Jurka447bf842013-05-15 14:52:15 +0200958 }
Michael Jurka54554252013-08-01 12:52:23 +0200959 if (mOnResumeCallbacks.size() > 0) {
960 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
961 mOnResumeCallbacks.get(i).run();
962 }
963 mOnResumeCallbacks.clear();
964 }
Winson Chunge4e50662012-01-23 14:45:13 -0800965
966 // Reset the pressed state of icons that were locked in the press state while activities
967 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800968 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800969 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800970 mWaitingForResume.setStayPressed(false);
971 }
Winson Chunge4e50662012-01-23 14:45:13 -0800972 if (mAppsCustomizeContent != null) {
973 // Resets the previous all apps icon press state
974 mAppsCustomizeContent.resetDrawableState();
975 }
Winson Chung82963d52013-10-09 11:20:57 -0700976
Adam Cohen06dff352012-06-01 17:17:08 -0700977 // It is possible that widgets can receive updates while launcher is not in the foreground.
978 // Consequently, the widgets will be inflated in the orientation of the foreground activity
979 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
980 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700981 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700982
Winson Chung780fe592013-09-26 14:48:44 -0700983 // Process any items that were added while Launcher was away.
984 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
985
Winson Chung5841efa2013-09-30 18:06:44 -0700986 // Update the voice search button proxy
987 updateVoiceButtonProxyVisible(false);
988
Adam Cohenf9426d52012-06-04 17:26:21 -0700989 // Again, as with the above scenario, it's possible that one or more of the global icons
990 // were updated in the wrong orientation.
991 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200992 if (DEBUG_RESUME_TIME) {
993 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
994 }
Adam Cohen6fecd412013-10-02 17:41:50 -0700995
996 if (mWorkspace.getCustomContentCallbacks() != null) {
997 // If we are resuming and the custom content is the current page, we call onShow().
998 // It is also poassible that onShow will instead be called slightly after first layout
999 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1000 if (mWorkspace.isOnOrMovingToCustomContent()) {
1001 mWorkspace.getCustomContentCallbacks().onShow();
1002 }
1003 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001004 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001005 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001006 }
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001009 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001010 // Ensure that items added to Launcher are queued until Launcher returns
1011 InstallShortcutReceiver.enableInstallQueue();
1012
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001013 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001014 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001015 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001016 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001017
1018 // We call onHide() aggressively. The custom content callbacks should be able to
1019 // debounce excess onHide calls.
1020 if (mWorkspace.getCustomContentCallbacks() != null) {
1021 mWorkspace.getCustomContentCallbacks().onHide();
1022 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001023 }
Romain Guycbb89e42009-06-08 15:52:54 -07001024
Adam Cohen66a01fd2013-06-11 12:48:00 -07001025 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001026 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001027 addCustomContentToLeft();
1028 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001029 }
1030
Adam Cohenbffe7452013-07-22 18:21:45 -07001031 QSBScroller mQsbScroller = new QSBScroller() {
1032 int scrollY = 0;
1033
1034 @Override
1035 public void setScrollY(int scroll) {
1036 scrollY = scroll;
1037
1038 if (mWorkspace.isOnOrMovingToCustomContent()) {
1039 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001040 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001041 }
1042 }
1043 };
1044
1045 public void resetQSBScroll() {
1046 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001047 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 }
1049
1050 public interface CustomContentCallbacks {
1051 // Custom content is completely shown
1052 public void onShow();
1053
1054 // Custom content is completely hidden
1055 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001056
1057 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1058 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001059 }
1060
Adam Cohen30bacb22013-08-29 14:25:25 -07001061 protected void startSettings() {
1062 }
1063
Adam Cohenbffe7452013-07-22 18:21:45 -07001064 public interface QSBScroller {
1065 public void setScrollY(int scrollY);
1066 }
1067
Winson Chung98ca0f72013-07-29 12:58:51 -07001068 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001069 CustomContentCallbacks callbacks, String description) {
1070 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001072 }
1073
Adam Cohenedb40762013-07-18 16:45:45 -07001074 // The custom content needs to offset its content to account for the QSB
1075 public int getTopOffsetForCustomContent() {
1076 return mWorkspace.getPaddingTop();
1077 }
1078
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001079 @Override
1080 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001081 // Flag the loader to stop early before switching
1082 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001083 if (mAppsCustomizeContent != null) {
1084 mAppsCustomizeContent.surrender();
1085 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001086 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001087 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001089 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001090 @Override
1091 public void onWindowFocusChanged(boolean hasFocus) {
1092 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001093 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001094 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 private boolean acceptFilter() {
1097 final InputMethodManager inputManager = (InputMethodManager)
1098 getSystemService(Context.INPUT_METHOD_SERVICE);
1099 return !inputManager.isFullscreenMode();
1100 }
1101
1102 @Override
1103 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001104 final int uniChar = event.getUnicodeChar();
1105 final boolean handled = super.onKeyDown(keyCode, event);
1106 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1107 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1109 keyCode, event);
1110 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001111 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001112 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001113 // showSearchDialog()
1114 // If there are multiple keystrokes before the search dialog takes focus,
1115 // onSearchRequested() will be called for every keystroke,
1116 // but it is idempotent, so it's fine.
1117 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119 }
1120
Joe Onorato8a9625e2010-01-28 15:55:35 -08001121 // Eat the long press event so the keyboard doesn't come up.
1122 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1123 return true;
1124 }
1125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 return handled;
1127 }
1128
Karl Rosaen138a0412009-04-23 19:00:21 -07001129 private String getTypedText() {
1130 return mDefaultKeySsb.toString();
1131 }
1132
1133 private void clearTypedText() {
1134 mDefaultKeySsb.clear();
1135 mDefaultKeySsb.clearSpans();
1136 Selection.setSelection(mDefaultKeySsb, 0);
1137 }
1138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001140 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1141 * State
1142 */
1143 private static State intToState(int stateOrdinal) {
1144 State state = State.WORKSPACE;
1145 final State[] stateValues = State.values();
1146 for (int i = 0; i < stateValues.length; i++) {
1147 if (stateValues[i].ordinal() == stateOrdinal) {
1148 state = stateValues[i];
1149 break;
1150 }
1151 }
1152 return state;
1153 }
1154
1155 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 * Restores the previous state, if it exists.
1157 *
1158 * @param savedState The previous state.
1159 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001160 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 private void restoreState(Bundle savedState) {
1162 if (savedState == null) {
1163 return;
1164 }
1165
Michael Jurka883f55b2010-10-21 15:47:14 -07001166 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001167 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001168 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001169 }
1170
Adam Cohen21cd0022013-10-09 18:57:02 -07001171 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1172 PagedView.INVALID_RESTORE_PAGE);
1173 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001174 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176
Winson Chung3d503fb2011-07-13 17:25:49 -07001177 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001178 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1181 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1184 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001185 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1186 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1187 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001188 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001189 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 mRestoring = true;
1191 }
1192
1193 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1194 if (renameFolder) {
1195 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001196 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 mRestoring = true;
1198 }
Winson Chunga12a2502010-12-20 14:41:35 -08001199
Winson Chung785d2eb2011-04-14 16:08:02 -07001200 // Restore the AppsCustomize tab
1201 if (mAppsCustomizeTabHost != null) {
1202 String curTab = savedState.getString("apps_customize_currentTab");
1203 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001204 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001205 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001206 mAppsCustomizeContent.loadAssociatedPages(
1207 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001208 }
1209
Winson Chung5afbf7b2011-07-25 11:53:08 -07001210 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1211 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001213 mItemIdToViewId = (HashMap<Integer, Integer>)
1214 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216
1217 /**
1218 * Finds all the views we need and configure them properly.
1219 */
1220 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001221 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001222
Craig Mautner360310b2012-10-26 15:13:08 -07001223 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001224 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1225 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001226 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001227
John Spurlock77e1f472013-09-11 10:09:51 -04001228 mLauncherView.setSystemUiVisibility(
1229 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001230 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231
Winson Chung4c98d922011-05-31 16:50:48 -07001232 // Setup the drag layer
1233 mDragLayer.setup(this, dragController);
1234
Winson Chung3d503fb2011-07-13 17:25:49 -07001235 // Setup the hotseat
1236 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1237 if (mHotseat != null) {
1238 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001239 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001240 }
1241
Adam Cohenf358a4b2013-07-23 16:47:31 -07001242 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001243 View widgetButton = findViewById(R.id.widget_button);
1244 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001245 @Override
1246 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001247 if (!mWorkspace.isSwitchingState()) {
1248 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1249 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001250 }
1251 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001252 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1253
1254 View wallpaperButton = findViewById(R.id.wallpaper_button);
1255 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 @Override
1257 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001258 if (!mWorkspace.isSwitchingState()) {
1259 startWallpaper();
1260 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001261 }
1262 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001263 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1264
1265 View settingsButton = findViewById(R.id.settings_button);
1266 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001267 @Override
1268 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001269 if (!mWorkspace.isSwitchingState()) {
1270 startSettings();
1271 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001272 }
1273 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001274 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001275 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001276
Winson Chung4c98d922011-05-31 16:50:48 -07001277 // Setup the workspace
1278 mWorkspace.setHapticFeedbackEnabled(false);
1279 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001280 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001281 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001282
Winson Chungf0ea4d32011-06-06 14:27:16 -07001283 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001284 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001285
Winson Chungf0ea4d32011-06-06 14:27:16 -07001286 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001287 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001288 mAppsCustomizeContent = (AppsCustomizePagedView)
1289 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1290 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001291
Winson Chung3d503fb2011-07-13 17:25:49 -07001292 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001293 dragController.setDragScoller(mWorkspace);
1294 dragController.setScrollView(mDragLayer);
1295 dragController.setMoveTarget(mWorkspace);
1296 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001297 if (mSearchDropTargetBar != null) {
1298 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001299 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001300
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001301 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001302 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001303 mWeightWatcher = new WeightWatcher(this);
1304 mWeightWatcher.setAlpha(0.5f);
1305 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001306 new FrameLayout.LayoutParams(
1307 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001308 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001309 Gravity.BOTTOM)
1310 );
Adam Cohen39a06042013-07-19 14:30:12 -07001311
1312 boolean show = shouldShowWeightWatcher();
1313 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316
1317 /**
1318 * Creates a view representing a shortcut.
1319 *
1320 * @param info The data structure describing the shortcut.
1321 *
1322 * @return A View inflated from R.layout.application.
1323 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001324 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001326 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
1329 /**
1330 * Creates a view representing a shortcut inflated from the specified resource.
1331 *
1332 * @param layoutResId The id of the XML layout used to create the shortcut.
1333 * @param parent The group the shortcut belongs to.
1334 * @param info The data structure describing the shortcut.
1335 *
1336 * @return A View inflated from layoutResId.
1337 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001338 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001339 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1340 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 return favorite;
1343 }
1344
1345 /**
1346 * Add an application shortcut to the workspace.
1347 *
1348 * @param data The intent describing the application.
1349 * @param cellInfo The position on screen where to create the shortcut.
1350 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001351 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001353 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001354
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 // First we check if we already know the exact location where we want to add this item.
1356 if (cellX >= 0 && cellY >= 0) {
1357 cellXY[0] = cellX;
1358 cellXY[1] = cellY;
1359 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001360 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001361 return;
1362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001364 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001365
Romain Guy73b979d2009-06-09 12:57:21 -07001366 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001367 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001369 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001371 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001372 } else {
1373 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
1375 }
Romain Guycbb89e42009-06-08 15:52:54 -07001376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 /**
1378 * Add a shortcut to the workspace.
1379 *
1380 * @param data The intent describing the shortcut.
1381 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001383 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 int cellY) {
1385 int[] cellXY = mTmpAddItemCellCoordinates;
1386 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001387 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001388
Michael Jurkad3ef3062010-11-23 16:23:58 -08001389 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001390
Adam Cohen558baaf2011-08-15 15:22:57 -07001391 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001392 if (info == null) {
1393 return;
1394 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001395 final View view = createShortcut(info);
1396
Adam Cohenfbba09b2011-07-18 21:43:05 -07001397 // First we check if we already know the exact location where we want to add this item.
1398 if (cellX >= 0 && cellY >= 0) {
1399 cellXY[0] = cellX;
1400 cellXY[1] = cellY;
1401 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001402
1403 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001404 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001405 true, null,null)) {
1406 return;
1407 }
1408 DragObject dragObject = new DragObject();
1409 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001410 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1411 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001412 return;
1413 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001414 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001415 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001417 foundCellSpan = (result != null);
1418 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001419 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001420 }
1421
1422 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001423 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001424 return;
1425 }
1426
Adam Cohendcd297f2013-06-18 13:13:40 -07001427 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428
1429 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001430 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
1433 }
1434
Adam Cohen2f093b62012-04-30 18:59:53 -07001435 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1436 int minHeight) {
1437 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001438 // We want to account for the extra amount of padding that we are adding to the widget
1439 // to ensure that it gets the full amount of space that it has requested
1440 int requiredWidth = minWidth + padding.left + padding.right;
1441 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001442 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001443 }
1444
Adam Cohen2f093b62012-04-30 18:59:53 -07001445 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1446 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001447 }
1448
Adam Cohen2f093b62012-04-30 18:59:53 -07001449 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1450 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001451 }
1452
Adam Cohen2f093b62012-04-30 18:59:53 -07001453 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1454 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001455 }
1456
Adam Cohen2f093b62012-04-30 18:59:53 -07001457 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1458 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001459 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001463 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001465 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001466 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001468 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001469 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1470 if (appWidgetInfo == null) {
1471 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1472 }
Romain Guycbb89e42009-06-08 15:52:54 -07001473
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001474 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001475 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001476
Adam Cohen2f093b62012-04-30 18:59:53 -07001477 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1478 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001481 // We have saved the position to which the widget was dragged-- this really only matters
1482 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001483 int[] cellXY = mTmpAddItemCellCoordinates;
1484 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001485 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1488 cellXY[0] = mPendingAddInfo.cellX;
1489 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001490 spanXY[0] = mPendingAddInfo.spanX;
1491 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 foundCellSpan = true;
1493 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001494 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001496 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1497 spanXY[1], cellXY, finalSpan);
1498 spanXY[0] = finalSpan[0];
1499 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001502 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001503 }
1504
Michael Jurka0280c3b2010-09-17 15:00:07 -07001505 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001506 if (appWidgetId != -1) {
1507 // Deleting an app widget ID is a void call but writes to disk before returning
1508 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001509 new AsyncTask<Void, Void, Void>() {
1510 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001511 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001512 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001513 }
Michael Jurka43467462013-11-14 12:03:58 +01001514 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001515 }
Winson Chung93eef082012-03-23 15:59:27 -07001516 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001517 return;
1518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001520 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001521 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1522 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001523 launcherInfo.spanX = spanXY[0];
1524 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1526 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001527
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001529 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
1531 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 if (hostView == null) {
1533 // Perform actual inflation because we're live
1534 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1535 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1536 } else {
1537 // The AppWidgetHostView has already been inflated and instantiated
1538 launcherInfo.hostView = hostView;
1539 }
Romain Guycbb89e42009-06-08 15:52:54 -07001540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001542 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001543 launcherInfo.notifyWidgetSizeChanged(this);
1544
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001546 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001547
1548 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001550 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Adam Cohended9f8d2010-11-03 13:25:16 -07001553 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1554 @Override
1555 public void onReceive(Context context, Intent intent) {
1556 final String action = intent.getAction();
1557 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1558 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001559 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001560 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001561
Winson Chungc100e8e2011-08-09 16:02:43 -07001562 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001563 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001564 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001565 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001566 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001567 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1568 mUserPresent = true;
1569 updateRunning();
1570 }
1571 }
1572 };
1573
1574 @Override
1575 public void onAttachedToWindow() {
1576 super.onAttachedToWindow();
1577
1578 // Listen for broadcasts related to user-presence
1579 final IntentFilter filter = new IntentFilter();
1580 filter.addAction(Intent.ACTION_SCREEN_OFF);
1581 filter.addAction(Intent.ACTION_USER_PRESENT);
1582 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001583 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001584 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 mVisible = true;
1586 }
1587
1588 @Override
1589 public void onDetachedFromWindow() {
1590 super.onDetachedFromWindow();
1591 mVisible = false;
1592
Adam Cohend113e0c2010-11-11 10:48:05 -08001593 if (mAttached) {
1594 unregisterReceiver(mReceiver);
1595 mAttached = false;
1596 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 updateRunning();
1598 }
1599
1600 public void onWindowVisibilityChanged(int visibility) {
1601 mVisible = visibility == View.VISIBLE;
1602 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001603 // The following code used to be in onResume, but it turns out onResume is called when
1604 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1605 // is a more appropriate event to handle
1606 if (mVisible) {
1607 mAppsCustomizeTabHost.onWindowVisible();
1608 if (!mWorkspaceLoading) {
1609 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001610 // We want to let Launcher draw itself at least once before we force it to build
1611 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001612 // apps is nice and speedy.
1613 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001614 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001615 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001616 if (mStarted) return;
1617 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001618 // We delay the layer building a bit in order to give
1619 // other message processing a time to run. In particular
1620 // this avoids a delay in hiding the IME if it was
1621 // currently shown, because doing that may involve
1622 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001623 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001624 final ViewTreeObserver.OnDrawListener listener = this;
1625 mWorkspace.post(new Runnable() {
1626 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001627 if (mWorkspace != null &&
1628 mWorkspace.getViewTreeObserver() != null) {
1629 mWorkspace.getViewTreeObserver().
1630 removeOnDrawListener(listener);
1631 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001632 }
1633 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001634 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001635 }
1636 });
1637 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001638 // When Launcher comes back to foreground, a different Activity might be responsible for
1639 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001640 if (!DISABLE_MARKET_BUTTON) {
1641 updateAppMarketIcon();
1642 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001643 clearTypedText();
1644 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001645 }
1646
1647 private void sendAdvanceMessage(long delay) {
1648 mHandler.removeMessages(ADVANCE_MSG);
1649 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1650 mHandler.sendMessageDelayed(msg, delay);
1651 mAutoAdvanceSentTime = System.currentTimeMillis();
1652 }
1653
1654 private void updateRunning() {
1655 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1656 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1657 mAutoAdvanceRunning = autoAdvanceRunning;
1658 if (autoAdvanceRunning) {
1659 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1660 sendAdvanceMessage(delay);
1661 } else {
1662 if (!mWidgetsToAdvance.isEmpty()) {
1663 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1664 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1665 }
1666 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001667 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 }
1669 }
1670 }
1671
1672 private final Handler mHandler = new Handler() {
1673 @Override
1674 public void handleMessage(Message msg) {
1675 if (msg.what == ADVANCE_MSG) {
1676 int i = 0;
1677 for (View key: mWidgetsToAdvance.keySet()) {
1678 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1679 final int delay = mAdvanceStagger * i;
1680 if (v instanceof Advanceable) {
1681 postDelayed(new Runnable() {
1682 public void run() {
1683 ((Advanceable) v).advance();
1684 }
1685 }, delay);
1686 }
1687 i++;
1688 }
1689 sendAdvanceMessage(mAdvanceInterval);
1690 }
1691 }
1692 };
1693
1694 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001695 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1697 if (v instanceof Advanceable) {
1698 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001699 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 updateRunning();
1701 }
1702 }
1703
1704 void removeWidgetToAutoAdvance(View hostView) {
1705 if (mWidgetsToAdvance.containsKey(hostView)) {
1706 mWidgetsToAdvance.remove(hostView);
1707 updateRunning();
1708 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001709 }
1710
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001712 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001713 launcherInfo.hostView = null;
1714 }
1715
Winson Chung93eef082012-03-23 15:59:27 -07001716 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1717 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1718 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 }
1720
Winson Chunga6945242014-01-08 14:04:34 -08001721 public DragLayer getDragLayer() {
1722 return mDragLayer;
1723 }
1724
1725 public Workspace getWorkspace() {
1726 return mWorkspace;
1727 }
1728
1729 public Hotseat getHotseat() {
1730 return mHotseat;
1731 }
1732
1733 public View getOverviewPanel() {
1734 return mOverviewPanel;
1735 }
1736
1737 public SearchDropTargetBar getSearchBar() {
1738 return mSearchDropTargetBar;
1739 }
1740
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001741 public LauncherAppWidgetHost getAppWidgetHost() {
1742 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 }
Romain Guycbb89e42009-06-08 15:52:54 -07001744
Winson Chunga9abd0e2010-10-27 17:18:37 -07001745 public LauncherModel getModel() {
1746 return mModel;
1747 }
1748
Winson Chunga6945242014-01-08 14:04:34 -08001749 public LauncherClings getLauncherClings() {
1750 return mLauncherClings;
1751 }
1752
1753 protected SharedPreferences getSharedPrefs() {
1754 return mSharedPrefs;
1755 }
1756
Bjorn Bringertc459e522013-06-07 19:36:01 +01001757 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001758 getWindow().closeAllPanels();
1759
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001760 // Whatever we were doing is hereby canceled.
1761 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001762 }
1763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 @Override
1765 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001766 long startTime = 0;
1767 if (DEBUG_RESUME_TIME) {
1768 startTime = System.currentTimeMillis();
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 super.onNewIntent(intent);
1771
1772 // Close the menu
1773 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001774 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001775 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001776
Adam Cohened307df2013-10-02 09:37:31 -07001777 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1778 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1779 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001780
Adam Cohen6fecd412013-10-02 17:41:50 -07001781 if (mWorkspace == null) {
1782 // Can be cases where mWorkspace is null, this prevents a NPE
1783 return;
1784 }
1785 Folder openFolder = mWorkspace.getOpenFolder();
1786 // In all these cases, only animate if we're already on home
1787 mWorkspace.exitWidgetResizeMode();
1788 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001789 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001790 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001791 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001792
Adam Cohen6fecd412013-10-02 17:41:50 -07001793 closeFolder();
1794 exitSpringLoadedDragMode();
1795
1796 // If we are already on home, then just animate back to the workspace,
1797 // otherwise, just wait until onResume to set the state back to Workspace
1798 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001799 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001800 } else {
1801 mOnResumeState = State.WORKSPACE;
1802 }
1803
1804 final View v = getWindow().peekDecorView();
1805 if (v != null && v.getWindowToken() != null) {
1806 InputMethodManager imm = (InputMethodManager)getSystemService(
1807 INPUT_METHOD_SERVICE);
1808 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1809 }
1810
1811 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001812 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001813 mAppsCustomizeTabHost.reset();
1814 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001815
1816 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
Adam Cohened307df2013-10-02 09:37:31 -07001818
Michael Jurka447bf842013-05-15 14:52:15 +02001819 if (DEBUG_RESUME_TIME) {
1820 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 }
1823
Adam Coppa120b8e2013-11-12 16:26:04 +00001824 /**
1825 * Override point for subclasses to prevent movement to the default screen when the home
1826 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1827 */
1828 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1829 return true;
1830 }
1831
1832 /**
1833 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1834 */
1835 protected void onHomeIntent() {
1836 // Do nothing
1837 }
1838
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001840 public void onRestoreInstanceState(Bundle state) {
1841 super.onRestoreInstanceState(state);
1842 for (int page: mSynchronouslyBoundPages) {
1843 mWorkspace.restoreInstanceStateForChild(page);
1844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
1846
1847 @Override
1848 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001849 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001850 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1851 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001852 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001853 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854
Michael Jurka883f55b2010-10-21 15:47:14 -07001855 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001856 // We close any open folder since it will not be re-opened, and we need to make sure
1857 // this state is reflected.
1858 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
Adam Cohendcd297f2013-06-18 13:13:40 -07001860 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001861 mWaitingForResult) {
1862 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001863 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001864 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1865 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001866 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1867 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1868 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001869 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
1871
1872 if (mFolderInfo != null && mWaitingForResult) {
1873 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1874 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1875 }
Michael Jurka946ad472010-07-09 18:05:18 -07001876
Winson Chung785d2eb2011-04-14 16:08:02 -07001877 // Save the current AppsCustomize tab
1878 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001879 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1880 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001881 if (currentTabTag != null) {
1882 outState.putString("apps_customize_currentTab", currentTabTag);
1883 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001884 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1885 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001886 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001887 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
1889
1890 @Override
1891 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Winson Chunge7a03942011-08-05 15:05:12 -07001894 // Remove all pending runnables
1895 mHandler.removeMessages(ADVANCE_MSG);
1896 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001897 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001898
Winson Chungcd2b0142011-06-08 16:02:26 -07001899 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001900 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001901 mModel.stopLoader();
1902 app.setLauncher(null);
1903
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001905 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001907 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001909 mAppWidgetHost = null;
1910
1911 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912
1913 TextKeyListener.getInstance().release();
1914
Winson Chung81b52252012-08-27 15:34:29 -07001915 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1916 // to prevent leaking Launcher activities on orientation change.
1917 if (mModel != null) {
1918 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1919 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001920
1921 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001922 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001923
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001924 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001925 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001926 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001927 mWorkspace = null;
1928 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001929
Michael Jurka2ecf9952012-06-18 12:52:28 -07001930 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
Adam Cohena9cf38f2011-05-02 15:36:58 -07001933 public DragController getDragController() {
1934 return mDragController;
1935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 @Override
1938 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001939 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 super.startActivityForResult(intent, requestCode);
1941 }
1942
Winson Chung70d72102011-08-12 11:24:35 -07001943 /**
1944 * Indicates that we want global search for this activity by setting the globalSearch
1945 * argument for {@link #startSearch} to true.
1946 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001948 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001950
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001951 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001952
Karl Rosaen138a0412009-04-23 19:00:21 -07001953 if (initialQuery == null) {
1954 // Use any text typed in the launcher as the initial query
1955 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 if (appSearchData == null) {
1958 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001959 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
Winson Chungadf0c182012-08-23 14:59:07 -07001961 Rect sourceBounds = new Rect();
1962 if (mSearchDropTargetBar != null) {
1963 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1964 }
Romain Guycbb89e42009-06-08 15:52:54 -07001965
Bjorn Bringertc459e522013-06-07 19:36:01 +01001966 startSearch(initialQuery, selectInitialQuery,
1967 appSearchData, sourceBounds);
1968 }
1969
1970 public void startSearch(String initialQuery,
1971 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001972 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001973 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001974 }
1975
1976 /**
1977 * Starts the global search activity. This code is a copied from SearchManager
1978 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001979 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001980 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001981 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001982 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1983 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1984 if (globalSearchActivity == null) {
1985 Log.w(TAG, "No global search activity found.");
1986 return;
1987 }
1988 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1989 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1990 intent.setComponent(globalSearchActivity);
1991 // Make sure that we have a Bundle to put source in
1992 if (appSearchData == null) {
1993 appSearchData = new Bundle();
1994 } else {
1995 appSearchData = new Bundle(appSearchData);
1996 }
1997 // Set source to package name of app that starts global search, if not set already.
1998 if (!appSearchData.containsKey("source")) {
1999 appSearchData.putString("source", getPackageName());
2000 }
2001 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2002 if (!TextUtils.isEmpty(initialQuery)) {
2003 intent.putExtra(SearchManager.QUERY, initialQuery);
2004 }
2005 if (selectInitialQuery) {
2006 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2007 }
2008 intent.setSourceBounds(sourceBounds);
2009 try {
2010 startActivity(intent);
2011 } catch (ActivityNotFoundException ex) {
2012 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
Winson Chung70d72102011-08-12 11:24:35 -07002016 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002017 public boolean onPrepareOptionsMenu(Menu menu) {
2018 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002019 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002020 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002023 @Override
2024 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002025 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002026 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002027 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002028 }
2029
Joe Onorato9c1289c2009-08-17 11:03:03 -04002030 public boolean isWorkspaceLocked() {
2031 return mWorkspaceLoading || mWaitingForResult;
2032 }
2033
Michael Jurka0280c3b2010-09-17 15:00:07 -07002034 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002035 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002036 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002037 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2038 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002039 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002040 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002041 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002042
Adam Cohenad4e15c2013-10-17 16:21:35 -07002043 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2044 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2045 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2046 }
2047
2048 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2049 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2050 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002051 if (appWidgetInfo.configure != null) {
2052 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002053 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002054
Bjorn Bringert7984c942009-12-09 15:38:25 +00002055 // Launch over to configure widget, if needed
2056 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002057 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002058 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002059 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002061 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002062 Runnable onComplete = new Runnable() {
2063 @Override
2064 public void run() {
2065 // Exit spring loaded mode if necessary after adding the widget
2066 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2067 null);
2068 }
2069 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002070 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002071 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002072 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074 }
Romain Guycbb89e42009-06-08 15:52:54 -07002075
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002076 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002077 // Close any folders that may be open.
2078 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002079 mWorkspace.moveToCustomContentScreen(animate);
2080 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002081 /**
2082 * Process a shortcut drop.
2083 *
2084 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002085 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002086 * @param cell The cell it should be added to, optional
2087 * @param position The location on the screen where it was dropped, optional
2088 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002089 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002092 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002093 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002095
2096 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002097 mPendingAddInfo.cellX = cell[0];
2098 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002099 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002100
2101 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2102 createShortcutIntent.setComponent(componentName);
2103 processShortcut(createShortcutIntent);
2104 }
2105
Adam Cohenfbba09b2011-07-18 21:43:05 -07002106 /**
2107 * Process a widget drop.
2108 *
2109 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002110 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002111 * @param cell The cell it should be added to, optional
2112 * @param position The location on the screen where it was dropped, optional
2113 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002114 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002115 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002116 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002117 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002118 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002120 mPendingAddInfo.minSpanX = info.minSpanX;
2121 mPendingAddInfo.minSpanY = info.minSpanY;
2122
Adam Cohenfbba09b2011-07-18 21:43:05 -07002123 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 mPendingAddInfo.cellX = cell[0];
2125 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002126 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002127 if (span != null) {
2128 mPendingAddInfo.spanX = span[0];
2129 mPendingAddInfo.spanY = span[1];
2130 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002131
Adam Cohened66b2b2012-01-23 17:28:51 -08002132 AppWidgetHostView hostView = info.boundWidget;
2133 int appWidgetId;
2134 if (hostView != null) {
2135 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002136 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002137 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002138 // In this case, we either need to start an activity to get permission to bind
2139 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002140 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002141 Bundle options = info.bindOptions;
2142
2143 boolean success = false;
2144 if (options != null) {
2145 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2146 info.componentName, options);
2147 } else {
2148 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2149 info.componentName);
2150 }
2151 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002152 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002153 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002154 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002155 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2156 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2157 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002158 // TODO: we need to make sure that this accounts for the options bundle.
2159 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002160 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2161 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002162 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002163 }
2164
Joe Onoratodeb98af2010-02-19 14:59:39 -08002165 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002166 // Handle case where user selected "Applications"
2167 String applicationName = getResources().getString(R.string.group_applications);
2168 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002169
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002170 if (applicationName != null && applicationName.equals(shortcutName)) {
2171 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2172 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002173
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002174 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2175 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002176 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002177 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002178 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002179 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 }
2182
Winson Chung24ab2f12010-09-16 14:10:47 -07002183 void processWallpaper(Intent intent) {
2184 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2185 }
2186
Adam Cohendcd297f2013-06-18 13:13:40 -07002187 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002188 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002189 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 folderInfo.title = getText(R.string.folder_name);
2191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002193 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002194 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002195 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196
2197 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002198 FolderIcon newFolder =
2199 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002201 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002202 // Force measure the new folder icon
2203 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2204 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002205 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 }
Romain Guycbb89e42009-06-08 15:52:54 -07002207
Joe Onorato9c1289c2009-08-17 11:03:03 -04002208 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002209 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 }
2211
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002212 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002214 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002215 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002218 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002219 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002220 }
2221
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002222 /**
2223 * Registers various content observers. The current implementation registers
2224 * only a favorites observer to keep track of the favorites applications.
2225 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002226 private void registerContentObservers() {
2227 ContentResolver resolver = getContentResolver();
2228 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2229 true, mWidgetObserver);
2230 }
2231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 @Override
2233 public boolean dispatchKeyEvent(KeyEvent event) {
2234 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2235 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002237 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002238 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002239 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002240 dumpState();
2241 return true;
2242 }
2243 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002244 }
2245 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2246 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002247 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 return true;
2249 }
2250 }
2251
2252 return super.dispatchKeyEvent(event);
2253 }
2254
Joe Onorato88ec0992009-11-19 13:16:06 -08002255 @Override
2256 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002257 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002258 if (mAppsCustomizeContent.getContentType() ==
2259 AppsCustomizePagedView.ContentType.Applications) {
2260 showWorkspace(true);
2261 } else {
2262 showOverviewMode(true);
2263 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002264 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002265 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002266 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002267 Folder openFolder = mWorkspace.getOpenFolder();
2268 if (openFolder.isEditingName()) {
2269 openFolder.dismissEditingName();
2270 } else {
2271 closeFolder();
2272 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002273 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002274 mWorkspace.exitWidgetResizeMode();
2275
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002276 // Back button is a no-op here, but give at least some feedback for the button press
2277 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002278 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002279 }
2280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002282 * Re-listen when widgets are reset.
2283 */
2284 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002285 if (mAppWidgetHost != null) {
2286 mAppWidgetHost.startListening();
2287 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002288 }
2289
2290 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 * Launches the intent referred by the clicked shortcut.
2292 *
2293 * @param v The view representing the clicked shortcut.
2294 */
2295 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002296 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2297 // view has detached (it's possible for this to happen if the view is removed mid touch).
2298 if (v.getWindowToken() == null) {
2299 return;
2300 }
2301
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002302 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002303 return;
2304 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002305
Adam Cohen1697b792013-09-17 19:08:21 -07002306 if (v instanceof Workspace) {
2307 if (mWorkspace.isInOverviewMode()) {
2308 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002309 }
2310 return;
2311 }
2312
2313 if (v instanceof CellLayout) {
2314 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002315 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002316 }
2317 }
2318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002320 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002322 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2323 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002324
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002325 // Check for special shortcuts
2326 if (intent.getComponent() != null) {
2327 final String shortcutClass = intent.getComponent().getClassName();
2328
2329 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002330 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002331 return;
2332 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2333 MemoryDumpActivity.startDump(this);
2334 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002335 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2336 toggleShowWeightWatcher();
2337 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002338 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002339 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002340
2341 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002342 int[] pos = new int[2];
2343 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002344 intent.setSourceBounds(new Rect(pos[0], pos[1],
2345 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002346
2347 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002348
Daniel Sandlerff02d492013-08-05 02:12:05 -04002349 mStats.recordLaunch(intent, shortcut);
2350
Michael Jurkaddd62e92011-02-16 17:49:14 -08002351 if (success && v instanceof BubbleTextView) {
2352 mWaitingForResume = (BubbleTextView) v;
2353 mWaitingForResume.setStayPressed(true);
2354 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002356 if (v instanceof FolderIcon) {
2357 FolderIcon fi = (FolderIcon) v;
2358 handleFolderClick(fi);
2359 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002360 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002361 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002362 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002363 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002364 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367 }
2368
Michael Jurka0e260592010-06-30 17:07:39 -07002369 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002370 return false;
2371 }
2372
Michael Jurkaaf442092010-06-10 17:01:57 -07002373 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002374 * Event handler for the search button
2375 *
2376 * @param v The view that was clicked.
2377 */
2378 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002379 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2380
Amith Yamasania135ba82011-08-09 17:42:01 -07002381 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002382 }
2383
2384 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002385 * Event handler for the voice button
2386 *
2387 * @param v The view that was clicked.
2388 */
2389 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002390 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002391
Bjorn Bringertc459e522013-06-07 19:36:01 +01002392 startVoice();
2393 }
2394
2395 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002396 try {
2397 final SearchManager searchManager =
2398 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2399 ComponentName activityName = searchManager.getGlobalSearchActivity();
2400 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002401 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002402 if (activityName != null) {
2403 intent.setPackage(activityName.getPackageName());
2404 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002405 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002406 } catch (ActivityNotFoundException e) {
2407 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002408 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002409 startActivitySafely(null, intent, "onClickVoiceButton");
2410 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002411 }
2412
2413 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002414 * Event handler for the "grid" button that appears on the home screen, which
2415 * enters all apps mode.
2416 *
2417 * @param v The view that was clicked.
2418 */
2419 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002420 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002421 }
2422
2423 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002424 // Provide the same haptic feedback that the system offers for virtual keys.
2425 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002426 }
2427
Adam Cohen61f560d2013-09-30 15:58:20 -07002428 public void performHapticFeedbackOnTouchDown(View v) {
2429 // Provide the same haptic feedback that the system offers for virtual keys.
2430 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2431 }
2432
2433 public View.OnTouchListener getHapticFeedbackTouchListener() {
2434 if (mHapticFeedbackTouchListener == null) {
2435 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2436 @Override
2437 public boolean onTouch(View v, MotionEvent event) {
2438 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2439 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2440 }
2441 return false;
2442 }
2443 };
2444 }
2445 return mHapticFeedbackTouchListener;
2446 }
2447
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002448 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002449 if (!DISABLE_MARKET_BUTTON) {
2450 if (mAppMarketIntent != null) {
2451 startActivitySafely(v, mAppMarketIntent, "app market");
2452 } else {
2453 Log.e(TAG, "Invalid app market intent.");
2454 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002455 }
2456 }
2457
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002458 /**
2459 * Called when the user stops interacting with the launcher.
2460 * This implies that the user is now on the homescreen and is not doing housekeeping.
2461 */
2462 protected void onInteractionEnd() {}
2463
2464 /**
2465 * Called when the user starts interacting with the launcher.
2466 * The possible interactions are:
2467 * - open all apps
2468 * - reorder an app shortcut, or a widget
2469 * - open the overview mode.
2470 * This is a good time to stop doing things that only make sense
2471 * when the user is on the homescreen and not doing housekeeping.
2472 */
2473 protected void onInteractionBegin() {}
2474
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002475 void startApplicationDetailsActivity(ComponentName componentName) {
2476 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002477 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2478 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002479 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002480 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002481 }
2482
Michael Jurka1e2f4652013-07-08 18:03:46 -07002483 // returns true if the activity was started
2484 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002485 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002486 // System applications cannot be installed. For now, show a toast explaining that.
2487 // We may give them the option of disabling apps this way.
2488 int messageId = R.string.uninstall_system_app_text;
2489 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002490 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002491 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002492 String packageName = componentName.getPackageName();
2493 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002494 Intent intent = new Intent(
2495 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002496 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2497 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002498 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002499 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002500 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002501 }
2502
Michael Jurka86a720e2012-05-09 11:23:23 -07002503 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002507 // Only launch using the new animation if the shortcut has not opted out (this is a
2508 // private contract between launcher and may be ignored in the future).
2509 boolean useLaunchAnimation = (v != null) &&
2510 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2511 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002512 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2513 v.getMeasuredWidth(), v.getMeasuredHeight());
2514
2515 startActivity(intent, opts.toBundle());
2516 } else {
2517 startActivity(intent);
2518 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002519 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 } catch (SecurityException e) {
2521 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002522 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002524 "or use the exported attribute for this activity. "
2525 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002527 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002529
Michael Jurka86a720e2012-05-09 11:23:23 -07002530 boolean startActivitySafely(View v, Intent intent, Object tag) {
2531 boolean success = false;
2532 try {
2533 success = startActivity(v, intent, tag);
2534 } catch (ActivityNotFoundException e) {
2535 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2536 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2537 }
2538 return success;
2539 }
2540
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002542 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002543 Folder openFolder = mWorkspace.getFolderForTag(info);
2544
2545 // If the folder info reports that the associated folder is open, then verify that
2546 // it is actually opened. There have been a few instances where this gets out of sync.
2547 if (info.opened && openFolder == null) {
2548 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002549 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002550 info.opened = false;
2551 }
2552
Adam Cohenfb91f302012-06-11 15:45:18 -07002553 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 // Close any open folder
2555 closeFolder();
2556 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002557 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 } else {
2559 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 int folderScreen;
2561 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002562 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 // .. and close it
2564 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002565 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 // Close any folder open on the current screen
2567 closeFolder();
2568 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002569 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 }
2571 }
2572 }
2573 }
2574
Adam Cohen268c4752012-06-06 17:47:33 -07002575 /**
2576 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2577 * in the DragLayer in the exact absolute location of the original FolderIcon.
2578 */
2579 private void copyFolderIconToImage(FolderIcon fi) {
2580 final int width = fi.getMeasuredWidth();
2581 final int height = fi.getMeasuredHeight();
2582
2583 // Lazy load ImageView, Bitmap and Canvas
2584 if (mFolderIconImageView == null) {
2585 mFolderIconImageView = new ImageView(this);
2586 }
2587 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2588 mFolderIconBitmap.getHeight() != height) {
2589 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2590 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2591 }
2592
2593 DragLayer.LayoutParams lp;
2594 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2595 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2596 } else {
2597 lp = new DragLayer.LayoutParams(width, height);
2598 }
2599
Adam Cohen307fe232012-08-16 17:55:58 -07002600 // The layout from which the folder is being opened may be scaled, adjust the starting
2601 // view size by this scale factor.
2602 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002603 lp.customPosition = true;
2604 lp.x = mRectForFolderAnimation.left;
2605 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002606 lp.width = (int) (scale * width);
2607 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002608
2609 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2610 fi.draw(mFolderIconCanvas);
2611 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002612 if (fi.getFolder() != null) {
2613 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2614 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002615 }
Adam Cohen268c4752012-06-06 17:47:33 -07002616 // Just in case this image view is still in the drag layer from a previous animation,
2617 // we remove it and re-add it.
2618 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2619 mDragLayer.removeView(mFolderIconImageView);
2620 }
2621 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002622 if (fi.getFolder() != null) {
2623 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002624 }
Adam Cohen268c4752012-06-06 17:47:33 -07002625 }
2626
Adam Cohen2801caf2011-05-13 20:57:39 -07002627 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002628 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002629 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2630 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2631 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2632
Adam Cohenc51934b2011-07-26 21:07:43 -07002633 FolderInfo info = (FolderInfo) fi.getTag();
2634 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2635 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002636 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2637 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002638 }
2639
Adam Cohen268c4752012-06-06 17:47:33 -07002640 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2641 copyFolderIconToImage(fi);
2642 fi.setVisibility(View.INVISIBLE);
2643
Michael Jurka2ecf9952012-06-18 12:52:28 -07002644 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002645 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002646 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2647 oa.start();
2648 }
2649
Adam Cohen268c4752012-06-06 17:47:33 -07002650 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002651 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002652 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2653 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2654 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2655
Adam Cohen268c4752012-06-06 17:47:33 -07002656 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002657
Adam Cohen268c4752012-06-06 17:47:33 -07002658 // We remove and re-draw the FolderIcon in-case it has changed
2659 mDragLayer.removeView(mFolderIconImageView);
2660 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002661 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002662 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002663 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002664 oa.addListener(new AnimatorListenerAdapter() {
2665 @Override
2666 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002667 if (cl != null) {
2668 cl.clearFolderLeaveBehind();
2669 // Remove the ImageView copy of the FolderIcon and make the original visible.
2670 mDragLayer.removeView(mFolderIconImageView);
2671 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002672 }
2673 }
2674 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002675 oa.start();
2676 }
2677
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002679 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 * is animated relative to the specified View. If the View is null, no animation
2681 * is played.
2682 *
2683 * @param folderInfo The FolderInfo describing the folder to open.
2684 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002685 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002686 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002687 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688
Adam Cohena9cf38f2011-05-02 15:36:58 -07002689 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690
Adam Cohen4554ee12011-08-03 16:13:21 -07002691 // Just verify that the folder hasn't already been added to the DragLayer.
2692 // There was a one-off crash where the folder had a parent already.
2693 if (folder.getParent() == null) {
2694 mDragLayer.addView(folder);
2695 mDragController.addDropTarget((DropTarget) folder);
2696 } else {
2697 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2698 folder.getParent() + ").");
2699 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002700 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002701 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002702
2703 // Notify the accessibility manager that this folder "window" has appeared and occluded
2704 // the workspace items
2705 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2706 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002707 }
2708
2709 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002710 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002711 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002712 if (folder.isEditingName()) {
2713 folder.dismissEditingName();
2714 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002715 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002716
2717 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002718 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002719 }
2720 }
2721
2722 void closeFolder(Folder folder) {
2723 folder.getInfo().opened = false;
2724
2725 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2726 if (parent != null) {
2727 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2728 shrinkAndFadeInFolderIcon(fi);
2729 }
2730 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002731
2732 // Notify the accessibility manager that this folder "window" has disappeard and no
2733 // longer occludeds the workspace items
2734 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 }
2736
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002738 if (!isDraggingEnabled()) return false;
2739 if (isWorkspaceLocked()) return false;
2740 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741
Adam Cohen1697b792013-09-17 19:08:21 -07002742 if (v instanceof Workspace) {
2743 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002744 if (mWorkspace.enterOverviewMode()) {
2745 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2746 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2747 return true;
2748 } else {
2749 return false;
2750 }
Adam Cohen1697b792013-09-17 19:08:21 -07002751 }
Adam Cohen1697b792013-09-17 19:08:21 -07002752 }
2753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002755 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
2757
Michael Jurka0280c3b2010-09-17 15:00:07 -07002758 resetAddInfo();
2759 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002761 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 return true;
2763 }
2764
Winson Chung3d503fb2011-07-13 17:25:49 -07002765 // The hotseat touch handling does not go through Workspace, and we always allow long press
2766 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002767 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002768 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2769 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002770 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002771 // User long pressed on empty space
2772 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2773 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2774 // Disabling reordering until we sort out some issues.
2775 if (mWorkspace.isInOverviewMode()) {
2776 mWorkspace.startReordering(v);
2777 } else {
2778 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002781 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002783 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 }
2785 }
2786 }
2787 return true;
2788 }
2789
Winson Chung3d503fb2011-07-13 17:25:49 -07002790 boolean isHotseatLayout(View layout) {
2791 return mHotseat != null && layout != null &&
2792 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2793 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002794
Winson Chung3d503fb2011-07-13 17:25:49 -07002795 /**
2796 * Returns the CellLayout of the specified container at the specified screen.
2797 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002798 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002799 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2800 if (mHotseat != null) {
2801 return mHotseat.getLayout();
2802 } else {
2803 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002804 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002805 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002806 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002807 }
2808 }
2809
Daniel Sandler843e8602010-06-07 14:59:01 -04002810 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002811 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002812 }
2813
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002814 /**
2815 * Helper method for the cameraZoomIn/cameraZoomOut animations
2816 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002817 * @param scaleFactor The scale factor used for the zoom
2818 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002819 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002820 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002821 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002822 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002823
Craig Mautner360310b2012-10-26 15:13:08 -07002824 private void setWorkspaceBackground(boolean workspace) {
2825 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002826 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002827 }
2828
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002829 void updateWallpaperVisibility(boolean visible) {
2830 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2831 int curflags = getWindow().getAttributes().flags
2832 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2833 if (wpflags != curflags) {
2834 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2835 }
Craig Mautner360310b2012-10-26 15:13:08 -07002836 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002837 }
2838
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002839 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2840 if (v instanceof LauncherTransitionable) {
2841 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2842 }
2843 }
2844
Michael Jurkabed61d22012-02-14 22:51:29 -08002845 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2846 if (v instanceof LauncherTransitionable) {
2847 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2848 }
Winson Chung70442722012-02-10 15:43:22 -08002849
2850 // Update the workspace transition step as well
2851 dispatchOnLauncherTransitionStep(v, 0f);
2852 }
2853
2854 private void dispatchOnLauncherTransitionStep(View v, float t) {
2855 if (v instanceof LauncherTransitionable) {
2856 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2857 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 }
2859
2860 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2861 if (v instanceof LauncherTransitionable) {
2862 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2863 }
Winson Chung70442722012-02-10 15:43:22 -08002864
2865 // Update the workspace transition step as well
2866 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 }
2868
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002869 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002870 * Things to test when changing the following seven functions.
2871 * - Home from workspace
2872 * - from center screen
2873 * - from other screens
2874 * - Home from all apps
2875 * - from center screen
2876 * - from other screens
2877 * - Back from all apps
2878 * - from center screen
2879 * - from other screens
2880 * - Launch app from workspace and quit
2881 * - with back
2882 * - with home
2883 * - Launch app from all apps and quit
2884 * - with back
2885 * - with home
2886 * - Go to a screen that's not the default, then all
2887 * apps, and launch and app, and go back
2888 * - with back
2889 * -with home
2890 * - On workspace, long press power and go back
2891 * - with back
2892 * - with home
2893 * - On all apps, long press power and go back
2894 * - with back
2895 * - with home
2896 * - On workspace, power off
2897 * - On all apps, power off
2898 * - Launch an app and turn off the screen while in that app
2899 * - Go back with home key
2900 * - Go back with back key TODO: make this not go to workspace
2901 * - From all apps
2902 * - From workspace
2903 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2904 * - From all apps
2905 * - From the center workspace
2906 * - From another workspace
2907 */
2908
2909 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002910 * Zoom the camera out from the workspace to reveal 'toView'.
2911 * Assumes that the view to show is anchored at either the very top or very bottom
2912 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002913 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002914 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002915 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2916 showAppsCustomizeHelper(animated, springLoaded, contentType);
2917 }
2918 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2919 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002920 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002921 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002922 mStateAnimation.cancel();
2923 mStateAnimation = null;
2924 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002925 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002926
Winson Chungf0ea4d32011-06-06 14:27:16 -07002927 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2928 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2929 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002930 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002931 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002932 final int startDelay =
2933 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002934
Michael Jurkab3e22d92011-10-31 15:58:33 -07002935 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002936
Michael Jurkad74c9842011-07-10 12:44:21 -07002937 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002938 Animator workspaceAnim =
2939 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002940 if (!AppsCustomizePagedView.DISABLE_ALL_APPS
2941 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2942 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002943 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2944 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002945
2946 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002947 toView.setScaleX(scale);
2948 toView.setScaleY(scale);
2949 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2950 scaleAnim.
2951 scaleX(1f).scaleY(1f).
2952 setDuration(duration).
2953 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002954
Winson Chungf0ea4d32011-06-06 14:27:16 -07002955 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002956 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002957 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002958 .ofFloat(toView, "alpha", 0f, 1f)
2959 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002961 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2962 @Override
2963 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002964 if (animation == null) {
2965 throw new RuntimeException("animation is null");
2966 }
Winson Chung70442722012-02-10 15:43:22 -08002967 float t = (Float) animation.getAnimatedValue();
2968 dispatchOnLauncherTransitionStep(fromView, t);
2969 dispatchOnLauncherTransitionStep(toView, t);
2970 }
2971 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002972
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002973 // toView should appear right at the end of the workspace shrink
2974 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002975 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002976 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002977 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002978
2979 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002980 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002981 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002982 // Prepare the position
2983 toView.setTranslationX(0.0f);
2984 toView.setTranslationY(0.0f);
2985 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002986 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002987 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002988 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002989 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002990 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2991 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002992
Winson Chungc7d2b602012-05-16 17:02:20 -07002993 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002994 if (mSearchDropTargetBar != null) {
2995 mSearchDropTargetBar.hideSearchBar(false);
2996 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002997 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002998 });
2999
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003000 if (workspaceAnim != null) {
3001 mStateAnimation.play(workspaceAnim);
3002 }
Michael Jurka1899a362011-11-03 13:50:45 -07003003
3004 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003005
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003006 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3007 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003008
3009 // If any of the objects being animated haven't been measured/laid out
3010 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003011 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003012 (mWorkspace.getMeasuredWidth() == 0) ||
3013 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003014 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003015 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003016
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003017 final AnimatorSet stateAnimation = mStateAnimation;
3018 final Runnable startAnimRunnable = new Runnable() {
3019 public void run() {
3020 // Check that mStateAnimation hasn't changed while
3021 // we waited for a layout/draw pass
3022 if (mStateAnimation != stateAnimation)
3023 return;
3024 setPivotsForZoom(toView, scale);
3025 dispatchOnLauncherTransitionStart(fromView, animated, false);
3026 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003027 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003028 }
3029 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003030 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003031 final ViewTreeObserver observer = toView.getViewTreeObserver();
3032 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3033 public void onGlobalLayout() {
3034 startAnimRunnable.run();
3035 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3036 }
3037 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003038 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003039 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003040 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003041 } else {
3042 toView.setTranslationX(0.0f);
3043 toView.setTranslationY(0.0f);
3044 toView.setScaleX(1.0f);
3045 toView.setScaleY(1.0f);
3046 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003047 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003048
Daniel Sandlere4f98912013-06-25 15:13:26 -04003049 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003050 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003051 if (mSearchDropTargetBar != null) {
3052 mSearchDropTargetBar.hideSearchBar(false);
3053 }
Michael Jurkaabded662011-03-04 12:06:57 -08003054 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003055 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003056 dispatchOnLauncherTransitionStart(fromView, animated, false);
3057 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003058 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003059 dispatchOnLauncherTransitionStart(toView, animated, false);
3060 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003061 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003062 }
3063
3064 /**
3065 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003066 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003067 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003068 */
Adam Cohened307df2013-10-02 09:37:31 -07003069 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003070 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003071
Michael Jurkab3e22d92011-10-31 15:58:33 -07003072 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003073 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003074 mStateAnimation.cancel();
3075 mStateAnimation = null;
3076 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003077 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003078
Winson Chungf0ea4d32011-06-06 14:27:16 -07003079 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003080 final int fadeOutDuration =
3081 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003082 final float scaleFactor = (float)
3083 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3084 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003085 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003086 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003087 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003088 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3089 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003090 toState, animated, stagger, -1);
3091 } else if (toState == Workspace.State.SPRING_LOADED ||
3092 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003093 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003094 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003095 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003096
Michael Jurkab3e22d92011-10-31 15:58:33 -07003097 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003098 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003099 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003100 final LauncherViewPropertyAnimator scaleAnim =
3101 new LauncherViewPropertyAnimator(fromView);
3102 scaleAnim.
3103 scaleX(scaleFactor).scaleY(scaleFactor).
3104 setDuration(duration).
3105 setInterpolator(new Workspace.ZoomInInterpolator());
3106
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003107 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003108 .ofFloat(fromView, "alpha", 1f, 0f)
3109 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003110 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003111 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3112 @Override
3113 public void onAnimationUpdate(ValueAnimator animation) {
3114 float t = 1f - (Float) animation.getAnimatedValue();
3115 dispatchOnLauncherTransitionStep(fromView, t);
3116 dispatchOnLauncherTransitionStep(toView, t);
3117 }
3118 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003119
Michael Jurka2ecf9952012-06-18 12:52:28 -07003120 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003121
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003122 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3123 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003124 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003125
3126 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003127 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003128 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003129 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003130 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3131 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003132 if (onCompleteRunnable != null) {
3133 onCompleteRunnable.run();
3134 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003135 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003136 }
3137 });
3138
Winson Chungf0ea4d32011-06-06 14:27:16 -07003139 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003140 if (workspaceAnim != null) {
3141 mStateAnimation.play(workspaceAnim);
3142 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003143 dispatchOnLauncherTransitionStart(fromView, animated, true);
3144 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003145 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003146 } else {
3147 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003148 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003149 dispatchOnLauncherTransitionStart(fromView, animated, true);
3150 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003151 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003152 dispatchOnLauncherTransitionStart(toView, animated, true);
3153 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003154 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003155 }
3156
Michael Jurkae326f182011-11-21 14:05:46 -08003157 @Override
3158 public void onTrimMemory(int level) {
3159 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003160 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003161 mAppsCustomizeTabHost.onTrimMemory();
3162 }
3163 }
3164
Adam Cohened307df2013-10-02 09:37:31 -07003165 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003166 showWorkspace(animated, null);
3167 }
3168
Adam Cohened307df2013-10-02 09:37:31 -07003169 protected void showWorkspace() {
3170 showWorkspace(true);
3171 }
3172
Adam Cohened66b2b2012-01-23 17:28:51 -08003173 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003174 if (mWorkspace.isInOverviewMode()) {
3175 mWorkspace.exitOverviewMode(animated);
3176 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003177 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003178 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003179 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003180 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003181
Winson Chungc7d2b602012-05-16 17:02:20 -07003182 // Show the search bar (only animate if we were showing the drop target bar in spring
3183 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003184 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003185 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003186 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003187
Michael Jurkab3e22d92011-10-31 15:58:33 -07003188 // Set focus to the AppsCustomize button
3189 if (mAllAppsButton != null) {
3190 mAllAppsButton.requestFocus();
3191 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003192 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003193
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003194 // Change the state *after* we've called all the transition code
3195 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003196
Winson Chung5fb63472011-02-02 17:03:37 -08003197 // Resume the auto-advance of widgets
3198 mUserPresent = true;
3199 updateRunning();
3200
alanv1d4fde62012-10-17 13:15:47 -07003201 // Send an accessibility event to announce the context change
3202 getWindow().getDecorView()
3203 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003204
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003205 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003206 }
3207
Adam Cohened307df2013-10-02 09:37:31 -07003208 void showOverviewMode(boolean animated) {
3209 mWorkspace.setVisibility(View.VISIBLE);
3210 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3211 mState = State.WORKSPACE;
3212 onWorkspaceShown(animated);
3213 }
3214
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003215 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003216 }
3217
Winson Chung82963d52013-10-09 11:20:57 -07003218 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3219 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003220 if (mState != State.WORKSPACE) return;
3221
Winson Chung82963d52013-10-09 11:20:57 -07003222 if (resetPageToZero) {
3223 mAppsCustomizeTabHost.reset();
3224 }
Winson Chungc58497e2013-09-03 17:48:37 -07003225 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003226 mAppsCustomizeTabHost.requestFocus();
3227
Michael Jurkab3e22d92011-10-31 15:58:33 -07003228 // Change the state *after* we've called all the transition code
3229 mState = State.APPS_CUSTOMIZE;
3230
3231 // Pause the auto-advance of widgets until we are out of AllApps
3232 mUserPresent = false;
3233 updateRunning();
3234 closeFolder();
3235
3236 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003237 getWindow().getDecorView()
3238 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003239 }
3240
Adam Cohen7777d962011-08-18 18:58:38 -07003241 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003242 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003243 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003244 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003245 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003246 }
Adam Cohen7777d962011-08-18 18:58:38 -07003247
Adam Cohenad4e15c2013-10-17 16:21:35 -07003248 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003249 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003250 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3251
Winson Chunge7a03942011-08-05 15:05:12 -07003252 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003253 @Override
3254 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003255 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003256 // Before we show workspace, hide all apps again because
3257 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3258 // clean up our state transition functions
3259 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003260 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003261 } else {
3262 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003263 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003264 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003265 }, delay);
3266
Winson Chung557d6ed2011-07-08 15:34:52 -07003267 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003268
Michael Jurkad3ef3062010-11-23 16:23:58 -08003269 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003270 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 final boolean animated = true;
3272 final boolean springLoaded = true;
3273 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003274 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003275 }
3276 // Otherwise, we are not in spring loaded mode, so don't do anything.
3277 }
3278
Michael Jurkab3e22d92011-10-31 15:58:33 -07003279 void lockAllApps() {
3280 // TODO
3281 }
3282
3283 void unlockAllApps() {
3284 // TODO
3285 }
3286
Winson Chungf0ea4d32011-06-06 14:27:16 -07003287 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003288 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003289 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003290 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003291 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003292 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003293 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003294 int duration = 0;
3295 if (mSearchDropTargetBar != null) {
3296 duration = mSearchDropTargetBar.getTransitionInDuration();
3297 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003298 mHotseat.animate().alpha(1f).setDuration(duration);
3299 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003300 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003301 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003302 }
3303 }
3304 }
3305
3306 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003307 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003308 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003309 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003310 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003311 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003312 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003313 int duration = 0;
3314 if (mSearchDropTargetBar != null) {
3315 duration = mSearchDropTargetBar.getTransitionOutDuration();
3316 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003317 mHotseat.animate().alpha(0f).setDuration(duration);
3318 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003319 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003320 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003321 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003322 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003323 }
3324
Patrick Dubroy5f445422011-02-18 14:35:21 -08003325 /**
3326 * Add an item from all apps or customize onto the given workspace screen.
3327 * If layout is null, add to the current screen.
3328 */
3329 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003330 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003331 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003332 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003333 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003334
Winson Chungdff8ebb2011-09-08 17:25:31 -07003335 /** Maps the current orientation to an index for referencing orientation correct global icons */
3336 private int getCurrentOrientationIndexForGlobalIcons() {
3337 // default - 0, landscape - 1
3338 switch (getResources().getConfiguration().orientation) {
3339 case Configuration.ORIENTATION_LANDSCAPE:
3340 return 1;
3341 default:
3342 return 0;
3343 }
3344 }
3345
Michael Jurkaae65ee32012-04-30 11:45:54 -07003346 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003347 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003348 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003349 // Look for the toolbar icon specified in the activity meta-data
3350 Bundle metaData = packageManager.getActivityInfo(
3351 activityName, PackageManager.GET_META_DATA).metaData;
3352 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003353 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003354 if (iconResId != 0) {
3355 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003356 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003357 }
3358 }
3359 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003360 // This can happen if the activity defines an invalid drawable
3361 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3362 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003363 } catch (Resources.NotFoundException nfe) {
3364 // This can happen if the activity defines an invalid drawable
3365 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3366 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003367 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003368 return null;
3369 }
3370
3371 // if successful in getting icon, return it; otherwise, set button to use default drawable
3372 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003373 int buttonId, ComponentName activityName, int fallbackDrawableId,
3374 String toolbarResourceName) {
3375 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003376 Resources r = getResources();
3377 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3378 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003379
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003380 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003381 // If we were unable to find the icon via the meta-data, use a generic one
3382 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003383 toolbarIcon = r.getDrawable(fallbackDrawableId);
3384 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003385 if (button != null) {
3386 button.setCompoundDrawables(toolbarIcon, null, null, null);
3387 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003388 return null;
3389 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003390 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003391 if (button != null) {
3392 button.setCompoundDrawables(toolbarIcon, null, null, null);
3393 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003394 return toolbarIcon.getConstantState();
3395 }
3396 }
3397
3398 // if successful in getting icon, return it; otherwise, set button to use default drawable
3399 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003400 int buttonId, ComponentName activityName, int fallbackDrawableId,
3401 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003402 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003403 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003404
Michael Jurka19e0fc52011-07-22 18:00:21 -07003405 if (button != null) {
3406 // If we were unable to find the icon via the meta-data, use a
3407 // generic one
3408 if (toolbarIcon == null) {
3409 button.setImageResource(fallbackDrawableId);
3410 } else {
3411 button.setImageDrawable(toolbarIcon);
3412 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003413 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003414
3415 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3416
Michael Jurka0423dcf2010-10-05 14:56:18 -07003417 }
3418
Winson Chung1b884092012-06-08 17:13:02 -07003419 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003420 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003421 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003422 }
3423
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003424 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003425 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003426 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003427 }
3428
Winson Chungbb185bd2011-11-21 12:31:42 -08003429 private void invalidatePressedFocusedStates(View container, View button) {
3430 if (container instanceof HolographicLinearLayout) {
3431 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3432 layout.invalidatePressedFocusedStates();
3433 } else if (button instanceof HolographicImageView) {
3434 HolographicImageView view = (HolographicImageView) button;
3435 view.invalidatePressedFocusedStates();
3436 }
3437 }
3438
Cristina Stancu476493b2013-08-07 17:20:14 +01003439 public View getQsbBar() {
3440 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003441 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3442 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003443 }
3444 return mQsbBar;
3445 }
3446
3447 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003448 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003449 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003450 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003451 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003452
Winson Chung1cad91e2011-05-25 17:41:01 -07003453 final SearchManager searchManager =
3454 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3455 ComponentName activityName = searchManager.getGlobalSearchActivity();
3456 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003457 int coi = getCurrentOrientationIndexForGlobalIcons();
3458 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003459 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3460 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3461 if (sGlobalSearchIcon[coi] == null) {
3462 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3463 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3464 TOOLBAR_ICON_METADATA_NAME);
3465 }
3466
Winson Chungc51db6a2011-10-05 11:44:49 -07003467 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3468 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003469 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003470 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003471 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003472 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003473 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3474 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003475 if (searchButton != null) searchButton.setVisibility(View.GONE);
3476 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003477 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003478 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003479 }
3480 }
3481
Cristina Stancu476493b2013-08-07 17:20:14 +01003482 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003483 final View searchButtonContainer = findViewById(R.id.search_button_container);
3484 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003485 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003486 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003487 }
3488
Cristina Stancu476493b2013-08-07 17:20:14 +01003489 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003490 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003491 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003492
Winson Chungc51db6a2011-10-05 11:44:49 -07003493 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003494 final SearchManager searchManager =
3495 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3496 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3497
3498 ComponentName activityName = null;
3499 if (globalSearchActivity != null) {
3500 // Check if the global search activity handles voice search
3501 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3502 intent.setPackage(globalSearchActivity.getPackageName());
3503 activityName = intent.resolveActivity(getPackageManager());
3504 }
3505
3506 if (activityName == null) {
3507 // Fallback: check if an activity other than the global search activity
3508 // resolves this
3509 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3510 activityName = intent.resolveActivity(getPackageManager());
3511 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003512 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003513 int coi = getCurrentOrientationIndexForGlobalIcons();
3514 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003515 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3516 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3517 if (sVoiceSearchIcon[coi] == null) {
3518 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3519 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3520 TOOLBAR_ICON_METADATA_NAME);
3521 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003522 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003523 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003524 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003525 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003526 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003527 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003528 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003529 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003530 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003531 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003532 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003533 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003534
Cristina Stancu476493b2013-08-07 17:20:14 +01003535 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003536 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3537 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003538 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003539 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003540 }
3541
Winson Chung5841efa2013-09-30 18:06:44 -07003542 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003543 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3544 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003545 boolean visible = !forceDisableVoiceButtonProxy &&
3546 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003547 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003548 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003549 }
3550 }
Winson Chung5841efa2013-09-30 18:06:44 -07003551
3552 /**
3553 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3554 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3555 */
3556 public void disableVoiceButtonProxy(boolean disabled) {
3557 updateVoiceButtonProxyVisible(disabled);
3558 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003559 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003560 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003561 */
3562 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003563 if (!DISABLE_MARKET_BUTTON) {
3564 final View marketButton = findViewById(R.id.market_button);
3565 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3566 // Find the app market activity by resolving an intent.
3567 // (If multiple app markets are installed, it will return the ResolverActivity.)
3568 ComponentName activityName = intent.resolveActivity(getPackageManager());
3569 if (activityName != null) {
3570 int coi = getCurrentOrientationIndexForGlobalIcons();
3571 mAppMarketIntent = intent;
3572 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3573 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3574 TOOLBAR_ICON_METADATA_NAME);
3575 marketButton.setVisibility(View.VISIBLE);
3576 } else {
3577 // We should hide and disable the view so that we don't try and restore the visibility
3578 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3579 marketButton.setVisibility(View.GONE);
3580 marketButton.setEnabled(false);
3581 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003582 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003583 }
3584
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003585 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003586 if (!DISABLE_MARKET_BUTTON) {
3587 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3588 Resources r = getResources();
3589 Drawable marketIconDrawable = d.newDrawable(r);
3590 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3591 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3592 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003593
Winson Chungd64a6662013-09-30 11:06:59 -07003594 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3595 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003596 }
3597
Michael Jurkad7c28052012-04-27 15:43:36 -07003598 @Override
3599 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003600 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003601 final List<CharSequence> text = event.getText();
3602 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003603 // Populate event with a fake title based on the current state.
3604 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003605 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003606 } else {
3607 text.add(getString(R.string.all_apps_home_button_label));
3608 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003609 return result;
3610 }
3611
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003612 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003613 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003614 */
3615 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3616 @Override
3617 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003618 closeSystemDialogs();
3619 }
3620 }
3621
3622 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003623 * Receives notifications whenever the appwidgets are reset.
3624 */
3625 private class AppWidgetResetObserver extends ContentObserver {
3626 public AppWidgetResetObserver() {
3627 super(new Handler());
3628 }
3629
3630 @Override
3631 public void onChange(boolean selfChange) {
3632 onAppWidgetReset();
3633 }
3634 }
3635
3636 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003637 * If the activity is currently paused, signal that we need to run the passed Runnable
3638 * in onResume.
3639 *
3640 * This needs to be called from incoming places where resources might have been loaded
3641 * while we are paused. That is becaues the Configuration might be wrong
3642 * when we're not running, and if it comes back to what it was when we
3643 * were paused, we are not restarted.
3644 *
3645 * Implementation of the method from LauncherModel.Callbacks.
3646 *
3647 * @return true if we are currently paused. The caller might be able to
3648 * skip some work in that case since we will come back again.
3649 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003650 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003651 if (mPaused) {
3652 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003653 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003654 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003655 }
3656 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003657 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003658 return true;
3659 } else {
3660 return false;
3661 }
3662 }
3663
Michael Jurkac402cd92013-05-20 15:49:32 +02003664 private boolean waitUntilResume(Runnable run) {
3665 return waitUntilResume(run, false);
3666 }
3667
Michael Jurka1e2f4652013-07-08 18:03:46 -07003668 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003669 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003670 }
3671
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003673 * If the activity is currently paused, signal that we need to re-run the loader
3674 * in onResume.
3675 *
3676 * This needs to be called from incoming places where resources might have been loaded
3677 * while we are paused. That is becaues the Configuration might be wrong
3678 * when we're not running, and if it comes back to what it was when we
3679 * were paused, we are not restarted.
3680 *
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 *
3683 * @return true if we are currently paused. The caller might be able to
3684 * skip some work in that case since we will come back again.
3685 */
3686 public boolean setLoadOnResume() {
3687 if (mPaused) {
3688 Log.i(TAG, "setLoadOnResume");
3689 mOnResumeNeedsLoad = true;
3690 return true;
3691 } else {
3692 return false;
3693 }
3694 }
3695
3696 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003700 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003701 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003702 } else {
3703 return SCREEN_COUNT / 2;
3704 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003706
Joe Onorato9c1289c2009-08-17 11:03:03 -04003707 /**
3708 * Refreshes the shortcuts shown on the workspace.
3709 *
3710 * Implementation of the method from LauncherModel.Callbacks.
3711 */
3712 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003713 // If we're starting binding all over again, clear any bind calls we'd postponed in
3714 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3715 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003716 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003717
Winson Chungd64d1762013-08-20 14:37:16 -07003718 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003719 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003720 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003721
Michael Jurka05bf6442011-11-30 20:28:53 -08003722 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003723 if (mHotseat != null) {
3724 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003725 }
3726 }
3727
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 @Override
3729 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003730 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003731
Adam Cohen5084cba2013-09-03 12:01:16 -07003732 // If there are no screens, we need to have an empty screen
3733 if (orderedScreenIds.size() == 0) {
3734 mWorkspace.addExtraEmptyScreen();
3735 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003736
3737 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003738 // setCurrentPage() so ensure that all pages are added before calling this).
3739 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003740 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3741 mWorkspace.createCustomContentPage();
3742 }
Winson Chung64359a52013-07-08 17:17:08 -07003743 }
3744
3745 @Override
3746 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003747 // Log to disk
3748 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3749 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3750 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003751 int count = orderedScreenIds.size();
3752 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003753 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003754 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003755 }
3756
Adam Cohen39a06042013-07-19 14:30:12 -07003757 private boolean shouldShowWeightWatcher() {
3758 String spKey = LauncherAppState.getSharedPreferencesKey();
3759 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003760 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003761
3762 return show;
3763 }
3764
3765 private void toggleShowWeightWatcher() {
3766 String spKey = LauncherAppState.getSharedPreferencesKey();
3767 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3768 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3769
3770 show = !show;
3771
3772 SharedPreferences.Editor editor = sp.edit();
3773 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3774 editor.commit();
3775
3776 if (mWeightWatcher != null) {
3777 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3778 }
3779 }
3780
Winson Chungd64d1762013-08-20 14:37:16 -07003781 public void bindAppsAdded(final ArrayList<Long> newScreens,
3782 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003783 final ArrayList<ItemInfo> addAnimated,
3784 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003785 Runnable r = new Runnable() {
3786 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003787 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003788 }
3789 };
3790 if (waitUntilResume(r)) {
3791 return;
3792 }
3793
Winson Chungd64d1762013-08-20 14:37:16 -07003794 // Add the new screens
3795 bindAddScreens(newScreens);
3796
3797 // We add the items without animation on non-visible pages, and with
3798 // animations on the new page (which we will try and snap to).
3799 if (!addNotAnimated.isEmpty()) {
3800 bindItems(addNotAnimated, 0,
3801 addNotAnimated.size(), false);
3802 }
3803 if (!addAnimated.isEmpty()) {
3804 bindItems(addAnimated, 0,
3805 addAnimated.size(), true);
3806 }
Winson Chungc58497e2013-09-03 17:48:37 -07003807
Winson Chung87412982013-10-03 18:34:14 -07003808 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003809 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003810
Winson Chungc58497e2013-09-03 17:48:37 -07003811 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
3812 addedApps != null && mAppsCustomizeContent != null) {
3813 mAppsCustomizeContent.addApps(addedApps);
3814 }
Winson Chungd64d1762013-08-20 14:37:16 -07003815 }
3816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003817 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 * Bind the items start-end from the list.
3819 *
3820 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 */
Winson Chung64359a52013-07-08 17:17:08 -07003822 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3823 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003824 Runnable r = new Runnable() {
3825 public void run() {
3826 bindItems(shortcuts, start, end, forceAnimateIcons);
3827 }
3828 };
3829 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003830 return;
3831 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003832
Winson Chungf0c6ae02012-03-21 16:10:31 -07003833 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003834 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3835 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003836 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003837 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003838 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003839 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003841
3842 // Short circuit if we are loading dock items for a configuration which has no dock
3843 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3844 mHotseat == null) {
3845 continue;
3846 }
3847
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 switch (item.itemType) {
3849 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3850 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003851 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003852 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003853
Winson Chung64359a52013-07-08 17:17:08 -07003854 /*
3855 * TODO: FIX collision case
3856 */
Winson Chungce376632013-07-11 16:12:41 -07003857 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3858 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3859 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3860 throw new RuntimeException("OCCUPIED");
3861 }
Winson Chung64359a52013-07-08 17:17:08 -07003862 }
3863
Adam Cohendcd297f2013-06-18 13:13:40 -07003864 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3865 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003866 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003867 // Animate all the applications up now
3868 shortcut.setAlpha(0f);
3869 shortcut.setScaleX(0f);
3870 shortcut.setScaleY(0f);
3871 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003872 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003873 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003875 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003876 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003877 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003878 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003879 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3880 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003882 default:
3883 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003885 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003886
Winson Chung997a9232013-07-24 15:33:46 -07003887 if (animateIcons) {
3888 // Animate to the correct page
3889 if (newShortcutsScreenId > -1) {
3890 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003891 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003892 final Runnable startBounceAnimRunnable = new Runnable() {
3893 public void run() {
3894 anim.playTogether(bounceAnims);
3895 anim.start();
3896 }
3897 };
Winson Chung997a9232013-07-24 15:33:46 -07003898 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003899 // We post the animation slightly delayed to prevent slowdowns
3900 // when we are loading right after we return to launcher.
3901 mWorkspace.postDelayed(new Runnable() {
3902 public void run() {
3903 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003904 mWorkspace.postDelayed(startBounceAnimRunnable,
3905 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003906 }
3907 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003908 } else {
3909 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003910 }
3911 }
Winson Chung64359a52013-07-08 17:17:08 -07003912 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003914 }
3915
Joe Onorato9c1289c2009-08-17 11:03:03 -04003916 /**
3917 * Implementation of the method from LauncherModel.Callbacks.
3918 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003919 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003920 Runnable r = new Runnable() {
3921 public void run() {
3922 bindFolders(folders);
3923 }
3924 };
3925 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003926 return;
3927 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003928 sFolders.clear();
3929 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931
3932 /**
3933 * Add the views for a widget to the workspace.
3934 *
3935 * Implementation of the method from LauncherModel.Callbacks.
3936 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003937 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003938 Runnable r = new Runnable() {
3939 public void run() {
3940 bindAppWidget(item);
3941 }
3942 };
3943 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003944 return;
3945 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003946
Daniel Sandler843e8602010-06-07 14:59:01 -04003947 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3948 if (DEBUG_WIDGETS) {
3949 Log.d(TAG, "bindAppWidget: " + item);
3950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 final Workspace workspace = mWorkspace;
3952
3953 final int appWidgetId = item.appWidgetId;
3954 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003955 if (DEBUG_WIDGETS) {
3956 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3957 }
3958
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3960
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003962 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963
Adam Cohendcd297f2013-06-18 13:13:40 -07003964 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003966 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3967
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 workspace.requestLayout();
3969
Daniel Sandler843e8602010-06-07 14:59:01 -04003970 if (DEBUG_WIDGETS) {
3971 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3972 + (SystemClock.uptimeMillis()-start) + "ms");
3973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003974 }
3975
Adam Cohen1462de32012-07-24 22:34:36 -07003976 public void onPageBoundSynchronously(int page) {
3977 mSynchronouslyBoundPages.add(page);
3978 }
3979
Joe Onorato9c1289c2009-08-17 11:03:03 -04003980 /**
3981 * Callback saying that there aren't any more items to bind.
3982 *
3983 * Implementation of the method from LauncherModel.Callbacks.
3984 */
Adam Cohene25af792013-06-06 23:08:25 -07003985 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003986 Runnable r = new Runnable() {
3987 public void run() {
3988 finishBindingItems(upgradePath);
3989 }
3990 };
3991 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003992 return;
3993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 if (mSavedState != null) {
3995 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003996 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 mSavedState = null;
3999 }
4000
Adam Cohen1462de32012-07-24 22:34:36 -07004001 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004003 // If we received the result of any pending adds while the loader was running (e.g. the
4004 // widget configuration forced an orientation change), process them now.
4005 for (int i = 0; i < sPendingAddList.size(); i++) {
4006 completeAdd(sPendingAddList.get(i));
4007 }
4008 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009
Winson Chungc51db6a2011-10-05 11:44:49 -07004010 // Update the market app icon as necessary (the other icons will be managed in response to
4011 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004012 if (!DISABLE_MARKET_BUTTON) {
4013 updateAppMarketIcon();
4014 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004015
Winson Chungf0c6ae02012-03-21 16:10:31 -07004016 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004017 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004018 mWorkspace.getUniqueComponents(true, null);
4019 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004020 }
Adam Cohen99894d92013-06-14 11:22:59 -07004021
Adam Cohen66a01fd2013-06-11 12:48:00 -07004022 mWorkspace.post(new Runnable() {
4023 @Override
4024 public void run() {
4025 onFinishBindingItems();
4026 }
4027 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004028 }
4029
Winson Chunga0b7e862013-09-05 16:03:15 -07004030 public boolean isAllAppsButtonRank(int rank) {
4031 if (mHotseat != null) {
4032 return mHotseat.isAllAppsButtonRank(rank);
4033 }
4034 return false;
4035 }
4036
Winson Chunga2413752012-04-03 14:22:34 -07004037 private boolean canRunNewAppsAnimation() {
4038 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4039 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4040 }
4041
Winson Chungc9168342013-06-26 14:54:55 -07004042 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4043 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4044 PropertyValuesHolder.ofFloat("alpha", 1f),
4045 PropertyValuesHolder.ofFloat("scaleX", 1f),
4046 PropertyValuesHolder.ofFloat("scaleY", 1f));
4047 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4048 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4049 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4050 return bounceAnim;
4051 }
4052
Adam Cohen27772732013-11-11 14:00:56 +00004053 public boolean useVerticalBarLayout() {
4054 return LauncherAppState.getInstance().getDynamicGrid().
4055 getDeviceProfile().isVerticalBarLayout();
4056 }
4057
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004058 protected Rect getSearchBarBounds() {
4059 return LauncherAppState.getInstance().getDynamicGrid().
4060 getDeviceProfile().getSearchBarBounds();
4061 }
4062
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004063 @Override
4064 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004065 boolean searchVisible = updateGlobalSearchIcon();
4066 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004067 if (mSearchDropTargetBar != null) {
4068 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4069 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004070 }
4071
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004072 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 * Add the icons for all apps.
4074 *
4075 * Implementation of the method from LauncherModel.Callbacks.
4076 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004077 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungc58497e2013-09-03 17:48:37 -07004078 if (AppsCustomizePagedView.DISABLE_ALL_APPS) {
4079 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4080 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4081 getHotseat().addAllAppsFolder(mIconCache, apps,
4082 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4083 }
4084 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004085 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004086 if (mAppsCustomizeContent != null) {
4087 mAppsCustomizeContent.onPackagesUpdated(
4088 LauncherModel.getSortedWidgetsAndShortcuts(this));
4089 }
Winson Chungc58497e2013-09-03 17:48:37 -07004090 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004091 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004092 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004093 mAppsCustomizeContent.onPackagesUpdated(
4094 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004095 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004096 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004097 }
4098
4099 /**
4100 * A package was updated.
4101 *
4102 * Implementation of the method from LauncherModel.Callbacks.
4103 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004104 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004105 Runnable r = new Runnable() {
4106 public void run() {
4107 bindAppsUpdated(apps);
4108 }
4109 };
4110 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004111 return;
4112 }
4113
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004114 if (mWorkspace != null) {
4115 mWorkspace.updateShortcuts(apps);
4116 }
Winson Chungc58497e2013-09-03 17:48:37 -07004117
4118 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4119 mAppsCustomizeContent != null) {
4120 mAppsCustomizeContent.updateApps(apps);
4121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 }
4123
4124 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004125 * A package was uninstalled. We take both the super set of packageNames
4126 * in addition to specific applications to remove, the reason being that
4127 * this can be called when a package is updated as well. In that scenario,
4128 * we only remove specific components from the workspace, where as
4129 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 *
4131 * Implementation of the method from LauncherModel.Callbacks.
4132 */
Winson Chung83892cc2013-05-01 16:53:33 -07004133 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004134 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004135 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004136 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004137 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004138 }
Winson Chungd64d1762013-08-20 14:37:16 -07004139 };
4140 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004141 return;
4142 }
4143
Winson Chungdf95eb12013-10-16 14:57:07 -07004144 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004145 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004146 }
4147 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004148 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004149 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004150
Winson Chunga1820962011-10-03 16:31:06 -07004151 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004152 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004153
Winson Chungdf95eb12013-10-16 14:57:07 -07004154 // Update AllApps
Winson Chungc58497e2013-09-03 17:48:37 -07004155 if (!AppsCustomizePagedView.DISABLE_ALL_APPS &&
4156 mAppsCustomizeContent != null) {
4157 mAppsCustomizeContent.removeApps(appInfos);
4158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004159 }
Joe Onoratobe386092009-11-17 17:32:16 -08004160
4161 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004162 * A number of packages were updated.
4163 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004164 private ArrayList<Object> mWidgetsAndShortcuts;
4165 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004166 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004167 bindPackagesUpdated(mWidgetsAndShortcuts);
4168 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004169 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004170 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004171 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4172 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4173 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004174 return;
4175 }
4176
Winson Chung64359a52013-07-08 17:17:08 -07004177 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004178 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004179 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004180 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004181 }
4182
Winson Chung400438b2011-01-16 17:53:48 -08004183 private int mapConfigurationOriActivityInfoOri(int configOri) {
4184 final Display d = getWindowManager().getDefaultDisplay();
4185 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4186 switch (d.getRotation()) {
4187 case Surface.ROTATION_0:
4188 case Surface.ROTATION_180:
4189 // We are currently in the same basic orientation as the natural orientation
4190 naturalOri = configOri;
4191 break;
4192 case Surface.ROTATION_90:
4193 case Surface.ROTATION_270:
4194 // We are currently in the other basic orientation to the natural orientation
4195 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4196 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4197 break;
4198 }
4199
4200 int[] oriMap = {
4201 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4202 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4203 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4204 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4205 };
4206 // Since the map starts at portrait, we need to offset if this device's natural orientation
4207 // is landscape.
4208 int indexOffset = 0;
4209 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4210 indexOffset = 1;
4211 }
4212 return oriMap[(d.getRotation() + indexOffset) % 4];
4213 }
Adam Cohen446e9402011-09-15 18:21:21 -07004214
Winson Chung4b919f82012-05-01 10:44:08 -07004215 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004216 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004217 getResources().getBoolean(R.bool.allow_rotation);
4218 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004219 }
Winson Chung4b919f82012-05-01 10:44:08 -07004220 public void lockScreenOrientation() {
4221 if (isRotationEnabled()) {
4222 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4223 .getConfiguration().orientation));
4224 }
4225 }
4226 public void unlockScreenOrientation(boolean immediate) {
4227 if (isRotationEnabled()) {
4228 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004229 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004230 } else {
4231 mHandler.postDelayed(new Runnable() {
4232 public void run() {
4233 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4234 }
4235 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004236 }
Winson Chung4b919f82012-05-01 10:44:08 -07004237 }
Winson Chung400438b2011-01-16 17:53:48 -08004238 }
4239
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004240 /**
4241 * Called when the SearchBar hint should be changed.
4242 *
4243 * @param hint the hint to be displayed in the search bar.
4244 */
4245 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004246 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004247 }
4248
Winson Chunge6eabff2013-09-24 11:01:23 -07004249 protected String getFirstRunClingSearchBarHint() {
4250 return "";
4251 }
4252 protected String getFirstRunCustomContentHint() {
4253 return "";
4254 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004255 protected int getFirstRunFocusedHotseatAppDrawableId() {
4256 return -1;
4257 }
4258 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4259 return null;
4260 }
4261 protected int getFirstRunFocusedHotseatAppRank() {
4262 return -1;
4263 }
4264 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4265 return "";
4266 }
4267 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4268 return "";
4269 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004270
Winson Chungaf40f202013-09-18 18:26:31 -07004271 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004272 mLauncherClings.dismissFirstRunCling(v);
4273 }
4274 public void dismissMigrationClingCopyApps(View v) {
4275 mLauncherClings.dismissMigrationClingCopyApps(v);
4276 }
4277 public void dismissMigrationClingUseDefault(View v) {
4278 mLauncherClings.dismissMigrationClingUseDefault(v);
4279 }
4280 public void dismissMigrationWorkspaceCling(View v) {
4281 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004282 }
Winson Chung82f55532011-08-09 14:14:23 -07004283 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004284 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004285 }
4286 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004287 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004288 }
4289
Winson Chunga6945242014-01-08 14:04:34 -08004290 private boolean shouldRunFirstRunActivity() {
4291 return !ActivityManager.isRunningInTestHarness();
4292 }
4293
4294 public void showFirstRunActivity() {
4295 if (shouldRunFirstRunActivity() && hasFirstRunActivity()
4296 && !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false)) {
4297 Intent firstRunIntent = getFirstRunActivity();
4298 if (firstRunIntent != null) {
4299 startActivity(firstRunIntent);
4300 markFirstRunActivityShown();
4301 }
4302 }
4303 }
4304
4305 private void markFirstRunActivityShown() {
4306 SharedPreferences.Editor editor = mSharedPrefs.edit();
4307 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4308 editor.apply();
4309 }
4310
4311 void showWorkspaceSearchAndHotseat() {
4312 mWorkspace.setAlpha(1f);
4313 mHotseat.setAlpha(1f);
4314 mPageIndicators.setAlpha(1f);
4315 mSearchDropTargetBar.showSearchBar(false);
4316 }
4317
4318 void hideWorkspaceSearchAndHotseat() {
4319 mWorkspace.setAlpha(0f);
4320 mHotseat.setAlpha(0f);
4321 mPageIndicators.setAlpha(0f);
4322 mSearchDropTargetBar.hideSearchBar(false);
4323 }
4324
4325
Mathew Inwood72fbec12013-11-19 15:45:07 +00004326 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4327 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4328 if (ri == null) {
4329 return null;
4330 }
4331 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4332 }
4333
4334 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4335 Bitmap icon) {
4336 return new ShortcutInfo(shortcutIntent, caption, icon);
4337 }
4338
4339 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4340 dragView.setTag(dragInfo);
4341 mWorkspace.onDragStartedWithItem(dragView);
4342 mWorkspace.beginDragShared(dragView, source);
4343 }
4344
Winson Chung80baf5a2010-08-09 16:03:15 -07004345 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004346 * Prints out out state for debugging.
4347 */
4348 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004349 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004350 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004351 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4352 Log.d(TAG, "mRestoring=" + mRestoring);
4353 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4354 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004355 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004356 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004357
Winson Chung785d2eb2011-04-14 16:08:02 -07004358 if (mAppsCustomizeContent != null) {
4359 mAppsCustomizeContent.dumpState();
4360 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004361 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004362 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004363
4364 @Override
4365 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4366 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004367 synchronized (sDumpLogs) {
4368 writer.println(" ");
4369 writer.println("Debug logs: ");
4370 for (int i = 0; i < sDumpLogs.size(); i++) {
4371 writer.println(" " + sDumpLogs.get(i));
4372 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004373 }
4374 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004375
4376 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004377 if (DEBUG_DUMP_LOG) {
4378 synchronized (sDumpLogs) {
4379 Log.d(TAG, "");
4380 Log.d(TAG, "*********************");
4381 Log.d(TAG, "Launcher debug logs: ");
4382 for (int i = 0; i < sDumpLogs.size(); i++) {
4383 Log.d(TAG, " " + sDumpLogs.get(i));
4384 }
4385 Log.d(TAG, "*********************");
4386 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004387 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004388 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004389 }
4390
4391 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004392 addDumpLog(tag, log, null, debugLog);
4393 }
4394
4395 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004396 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004397 if (e != null) {
4398 Log.d(tag, log, e);
4399 } else {
4400 Log.d(tag, log);
4401 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004402 }
Winson Chungede41292013-09-19 16:27:36 -07004403 if (DEBUG_DUMP_LOG) {
4404 sDateStamp.setTime(System.currentTimeMillis());
4405 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004406 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4407 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004408 }
Winson Chungede41292013-09-19 16:27:36 -07004409 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004410 }
4411
Winson Chungede41292013-09-19 16:27:36 -07004412 public void dumpLogsToLocalData() {
4413 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004414 new AsyncTask<Void, Void, Void>() {
4415 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004416 boolean success = false;
4417 sDateStamp.setTime(sRunStart);
4418 String FILENAME = sDateStamp.getMonth() + "-"
4419 + sDateStamp.getDay() + "_"
4420 + sDateStamp.getHours() + "-"
4421 + sDateStamp.getMinutes() + "_"
4422 + sDateStamp.getSeconds() + ".txt";
4423
4424 FileOutputStream fos = null;
4425 File outFile = null;
4426 try {
4427 outFile = new File(getFilesDir(), FILENAME);
4428 outFile.createNewFile();
4429 fos = new FileOutputStream(outFile);
4430 } catch (Exception e) {
4431 e.printStackTrace();
4432 }
4433 if (fos != null) {
4434 PrintWriter writer = new PrintWriter(fos);
4435
4436 writer.println(" ");
4437 writer.println("Debug logs: ");
4438 synchronized (sDumpLogs) {
4439 for (int i = 0; i < sDumpLogs.size(); i++) {
4440 writer.println(" " + sDumpLogs.get(i));
4441 }
4442 }
4443 writer.close();
4444 }
4445 try {
4446 if (fos != null) {
4447 fos.close();
4448 success = true;
4449 }
4450 } catch (IOException e) {
4451 e.printStackTrace();
4452 }
Michael Jurka43467462013-11-14 12:03:58 +01004453 return null;
Winson Chungede41292013-09-19 16:27:36 -07004454 }
Michael Jurka43467462013-11-14 12:03:58 +01004455 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004456 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004458}
Michael Jurka2763be32011-02-24 11:19:57 -08004459
Michael Jurkaabded662011-03-04 12:06:57 -08004460interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004461 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004462 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004463 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004464 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004465 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004466}