blob: db36030f32bc6f2ed24c45353aa97f72e33b4c6c [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
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000048import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070072import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -070090import android.view.accessibility.AccessibilityManager;
Adam Cohencff6af82011-09-13 14:51:53 -070091import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700104import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700115import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000116import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118/**
119 * Default launcher application.
120 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100121public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700122 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700123 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800124 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400128 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700129 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700131 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700132
Dan Sandlerd5024042014-01-09 15:01:33 -0500133 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800160 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chung2672ff92012-05-04 16:22:30 -0700162 // The Intent extra that defines whether to ignore the launch animation
163 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400164 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
172 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: boolean
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
180 // Type: long
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
186 // Type: parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: int[]
191 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700199 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700201
Adam Cohen39a06042013-07-19 14:30:12 -0700202 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700203 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700204
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700205 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700206 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700207 private State mState = State.WORKSPACE;
208 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700209
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700211 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
212 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Winson Chungaf40f202013-09-18 18:26:31 -0700213 private static final int SHOW_CLING_DURATION = 250;
214 private static final int DISMISS_CLING_DURATION = 200;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800217 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000219 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
220 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
221
Winson Chunga2413752012-04-03 14:22:34 -0700222 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700223 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
224 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700225 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700226
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800227 private final BroadcastReceiver mCloseSystemDialogsReceiver
228 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800229 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private LayoutInflater mInflater;
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700234 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800235 private DragLayer mDragLayer;
236 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700237 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 private AppWidgetManager mAppWidgetManager;
240 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Michael Jurkac9d95c52011-08-29 14:03:34 -0700242 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700243 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800244 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700245
Michael Jurka0280c3b2010-09-17 15:00:07 -0700246 private int[] mTmpAddItemCellCoordinates = new int[2];
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private FolderInfo mFolderInfo;
249
Winson Chung3d503fb2011-07-13 17:25:49 -0700250 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700251 private View mOverviewPanel;
252
Michael Jurka838a4ca2011-02-07 13:33:06 -0800253 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700254
Winson Chungc51db6a2011-10-05 11:44:49 -0700255 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700256 private AppsCustomizeTabHost mAppsCustomizeTabHost;
257 private AppsCustomizePagedView mAppsCustomizeContent;
258 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100259 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700262 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
263 // scroll issues (because the workspace may not have been measured yet) and extra work.
264 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
265 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266
267 private SpannableStringBuilder mDefaultKeySsb = null;
268
Joe Onorato9c1289c2009-08-17 11:03:03 -0400269 private boolean mWorkspaceLoading = true;
270
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800271 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private boolean mRestoring;
273 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700274 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Michael Jurka1e2f4652013-07-08 18:03:46 -0700276 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700277 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
278
Winson Chung4a2afa32012-07-19 14:53:05 -0700279 // Keep track of whether the user has left launcher
280 private static boolean sPausedFromUserAction = false;
281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedInstanceState;
283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800285 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800286 private boolean mUserPresent = true;
287 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700288 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mAttached = false;
Winson Chungfa545132013-09-26 17:45:32 -0700290 private static final boolean DISABLE_CLINGS = false;
Winson Chunge6eabff2013-09-24 11:01:23 -0700291 private static final boolean DISABLE_CUSTOM_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700293 private static LocaleConfiguration sLocaleConfiguration = null;
294
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700295 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Adam Cohen61f560d2013-09-30 15:58:20 -0700297 private View.OnTouchListener mHapticFeedbackTouchListener;
298
Adam Cohended9f8d2010-11-03 13:25:16 -0700299 // Related to the auto-advancing of widgets
300 private final int ADVANCE_MSG = 1;
301 private final int mAdvanceInterval = 20000;
302 private final int mAdvanceStagger = 250;
303 private long mAutoAdvanceSentTime;
304 private long mAutoAdvanceTimeLeft = -1;
305 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
306 new HashMap<View, AppWidgetProviderInfo>();
307
Winson Chung400438b2011-01-16 17:53:48 -0800308 // Determines how long to wait after a rotation before restoring the screen orientation to
309 // match the sensor state.
310 private final int mRestoreScreenOrientationDelay = 500;
311
Michael Jurka4ef207b2010-11-29 17:05:45 -0800312 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700313 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
314 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
315 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316
Winson Chungd64a6662013-09-30 11:06:59 -0700317 private Intent mAppMarketIntent = null;
318 private static final boolean DISABLE_MARKET_BUTTON = true;
319
Craig Mautner360310b2012-10-26 15:13:08 -0700320 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700321
Adam Cohen1462de32012-07-24 22:34:36 -0700322 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700324
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700326 static Date sDateStamp = new Date();
327 static DateFormat sDateFormat =
328 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
329 static long sRunStart = System.currentTimeMillis();
330 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331
Winson Chung46353de2012-02-16 14:05:10 -0800332 // We only want to get the SharedPreferences once since it does an FS stat each time we get
333 // it from the context.
334 private SharedPreferences mSharedPrefs;
335
Adam Cohene25af792013-06-06 23:08:25 -0700336 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700340 private ImageView mFolderIconImageView;
341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Michael Jurkaddd62e92011-02-16 17:49:14 -0800345 private BubbleTextView mWaitingForResume;
346
Michael Jurka22143132012-10-04 17:42:38 +0200347 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
348 = new HideFromAccessibilityHelper();
349
Michael Jurkac1f5d262011-09-30 19:32:27 -0700350 private Runnable mBuildLayersRunnable = new Runnable() {
351 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700352 if (mWorkspace != null) {
353 mWorkspace.buildPageHardwareLayers();
354 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 }
356 };
357
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800358 private static ArrayList<PendingAddArguments> sPendingAddList
359 = new ArrayList<PendingAddArguments>();
360
Michael Jurka7267fa52013-09-26 15:29:57 -0700361 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800362
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363 private static class PendingAddArguments {
364 int requestCode;
365 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700366 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700367 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 int cellX;
369 int cellY;
370 }
371
Daniel Sandlerff02d492013-08-05 02:12:05 -0400372 private Stats mStats;
373
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800374 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800375 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700376 }
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 @Override
379 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700380 if (DEBUG_STRICT_MODE) {
381 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
382 .detectDiskReads()
383 .detectDiskWrites()
384 .detectNetwork() // or .detectAll() for all detectable problems
385 .penaltyLog()
386 .build());
387 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
388 .detectLeakedSqlLiteObjects()
389 .detectLeakedClosableObjects()
390 .penaltyLog()
391 .penaltyDeath()
392 .build());
393 }
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400396
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400397 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400398 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700399
400 // Determine the dynamic grid properties
401 Point smallestSize = new Point();
402 Point largestSize = new Point();
403 Point realSize = new Point();
404 Display display = getWindowManager().getDefaultDisplay();
405 display.getCurrentSizeRange(smallestSize, largestSize);
406 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700407 DisplayMetrics dm = new DisplayMetrics();
408 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 // Lazy-initialize the dynamic grid
411 DeviceProfile grid = app.initDynamicGrid(this,
412 Math.min(smallestSize.x, smallestSize.y),
413 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700414 realSize.x, realSize.y,
415 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700416
417 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700419 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700422 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Daniel Sandlerff02d492013-08-05 02:12:05 -0400426 mStats = new Stats(this);
427
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700428 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200439 android.os.Debug.startMethodTracing(
440 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
Adam Cohen53805212013-10-01 10:39:23 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100448 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800450 registerContentObservers();
451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500465 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800469 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Adam Cohenf9426d52012-06-04 17:26:21 -0700480 updateGlobalIcons();
481
482 // On large interfaces, we want the screen to auto-rotate based on the current orientation
483 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700484
Adam Cohenb54a5982014-01-08 15:21:04 -0800485 showFirstRunActivity();
Winson Chungaf40f202013-09-18 18:26:31 -0700486 showFirstRunCling();
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 }
488
Winson Chung4a2afa32012-07-19 14:53:05 -0700489 protected void onUserLeaveHint() {
490 super.onUserLeaveHint();
491 sPausedFromUserAction = true;
492 }
493
Winson Chung98ca0f72013-07-29 12:58:51 -0700494 /** To be overriden by subclasses to hint to Launcher that we have custom content */
495 protected boolean hasCustomContentToLeft() {
496 return false;
497 }
498
Dave Hawkeya8881582013-09-17 15:55:33 -0600499 /**
500 * To be overridden by subclasses to create the custom content and call
501 * {@link #addToCustomContentPage}. This will only be invoked if
502 * {@link #hasCustomContentToLeft()} is {@code true}.
503 */
504 protected void addCustomContentToLeft() {
505 }
506
507 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800508 * To be overridden by subclasses to indicate that there is an activity to launch
509 * before showing the standard launcher experience.
510 */
511 protected boolean hasFirstRunActivity() {
512 return false;
513 }
514
515 /**
516 * To be overridden by subclasses to launch any first run activity
517 */
518 protected Intent getFirstRunActivity() {
519 return null;
520 }
521
522 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600523 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
524 * ensure the custom content page is added or removed if necessary.
525 */
526 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600527 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 // Not bound yet, wait for bindScreens to be called.
529 return;
530 }
531
532 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
533 // Create the custom content page and call the subclass to populate it.
534 mWorkspace.createCustomContentPage();
535 addCustomContentToLeft();
536 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
537 mWorkspace.removeCustomContentPage();
538 }
539 }
540
Adam Cohenf9426d52012-06-04 17:26:21 -0700541 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700542 boolean searchVisible = false;
543 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800544 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700545 int coi = getCurrentOrientationIndexForGlobalIcons();
546 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
547 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700548 if (!DISABLE_MARKET_BUTTON) {
549 updateAppMarketIcon();
550 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700551 searchVisible = updateGlobalSearchIcon();
552 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700553 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700554 if (sGlobalSearchIcon[coi] != null) {
555 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700556 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700557 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700558 if (sVoiceSearchIcon[coi] != null) {
559 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700560 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungd64a6662013-09-30 11:06:59 -0700562 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700563 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800564 }
Winson Chungadf0c182012-08-23 14:59:07 -0700565 if (mSearchDropTargetBar != null) {
566 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
Romain Guycbb89e42009-06-08 15:52:54 -0700569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700571 if (sLocaleConfiguration == null) {
572 new AsyncTask<Void, Void, LocaleConfiguration>() {
573 @Override
574 protected LocaleConfiguration doInBackground(Void... unused) {
575 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
576 readConfiguration(Launcher.this, localeConfiguration);
577 return localeConfiguration;
578 }
579
580 @Override
581 protected void onPostExecute(LocaleConfiguration result) {
582 sLocaleConfiguration = result;
583 checkForLocaleChange(); // recursive, but now with a locale configuration
584 }
585 }.execute();
586 return;
587 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final Configuration configuration = getResources().getConfiguration();
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final String locale = configuration.locale.toString();
593
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 final int mcc = configuration.mcc;
596
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 final int mnc = configuration.mnc;
599
Romain Guy84f296c2009-11-04 15:00:44 -0800600 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601
Romain Guy84f296c2009-11-04 15:00:44 -0800602 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 sLocaleConfiguration.locale = locale;
604 sLocaleConfiguration.mcc = mcc;
605 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700606
Joe Onorato0589f0f2010-02-08 13:44:00 -0800607 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608
609 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100610 new AsyncTask<Void, Void, Void>() {
611 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100613 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 }
Michael Jurka43467462013-11-14 12:03:58 +0100615 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700616 }
617 }
618
619 private static class LocaleConfiguration {
620 public String locale;
621 public int mcc = -1;
622 public int mnc = -1;
623 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700624
Romain Guy98d01652009-06-30 16:21:04 -0700625 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
626 DataInputStream in = null;
627 try {
628 in = new DataInputStream(context.openFileInput(PREFERENCES));
629 configuration.locale = in.readUTF();
630 configuration.mcc = in.readInt();
631 configuration.mnc = in.readInt();
632 } catch (FileNotFoundException e) {
633 // Ignore
634 } catch (IOException e) {
635 // Ignore
636 } finally {
637 if (in != null) {
638 try {
639 in.close();
640 } catch (IOException e) {
641 // Ignore
642 }
643 }
644 }
645 }
646
647 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
648 DataOutputStream out = null;
649 try {
650 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
651 out.writeUTF(configuration.locale);
652 out.writeInt(configuration.mcc);
653 out.writeInt(configuration.mnc);
654 out.flush();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 //noinspection ResultOfMethodCallIgnored
659 context.getFileStreamPath(PREFERENCES).delete();
660 } finally {
661 if (out != null) {
662 try {
663 out.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Anton Hanssona2f665f2013-09-26 12:18:32 +0100671 public Stats getStats() {
672 return mStats;
673 }
674
Bjorn Bringertc459e522013-06-07 19:36:01 +0100675 public LayoutInflater getInflater() {
676 return mInflater;
677 }
678
Adam Cohen716b51e2011-06-30 12:09:54 -0700679 public DragLayer getDragLayer() {
680 return mDragLayer;
681 }
682
Winson Chung36a62fe2012-05-06 18:04:42 -0700683 boolean isDraggingEnabled() {
684 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
685 // that is subsequently removed from the workspace in startBinding().
686 return !mModel.isLoadingWorkspace();
687 }
688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 static int getScreen() {
690 synchronized (sLock) {
691 return sScreen;
692 }
693 }
694
695 static void setScreen(int screen) {
696 synchronized (sLock) {
697 sScreen = screen;
698 }
699 }
700
Winson Chung557d6ed2011-07-08 15:34:52 -0700701 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000702 * Copied from View -- the View version of the method isn't called
703 * anywhere else in our process and only exists for API level 17+,
704 * so it's ok to keep our own version with no API requirement.
705 */
706 public static int generateViewId() {
707 for (;;) {
708 final int result = sNextGeneratedId.get();
709 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
710 int newValue = result + 1;
711 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
712 if (sNextGeneratedId.compareAndSet(result, newValue)) {
713 return result;
714 }
715 }
716 }
717
718 public int getViewIdForItem(ItemInfo info) {
719 // This cast is safe given the > 2B range for int.
720 int itemId = (int) info.id;
721 if (mItemIdToViewId.containsKey(itemId)) {
722 return mItemIdToViewId.get(itemId);
723 }
724 int viewId = generateViewId();
725 mItemIdToViewId.put(itemId, viewId);
726 return viewId;
727 }
728
729 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700730 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
731 * a configuration step, this allows the proper animations to run after other transitions.
732 */
733 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700734 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 switch (args.requestCode) {
736 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700737 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 break;
740 case REQUEST_PICK_SHORTCUT:
741 processShortcut(args.intent);
742 break;
743 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700744 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700745 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 result = true;
747 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 case REQUEST_CREATE_APPWIDGET:
749 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700750 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700751 result = true;
752 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 }
Michael Jurka27614d22012-04-02 06:40:22 -0700754 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
755 // if you turned the screen off and then back while in All Apps, Launcher would not
756 // return to the workspace. Clearing mAddInfo.container here fixes this issue
757 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 protected void onActivityResult(
763 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700764 // Reset the startActivity waiting flag
765 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800766 int pendingAddWidgetId = mPendingAddWidgetId;
767 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700768
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 Runnable exitSpringLoaded = new Runnable() {
770 @Override
771 public void run() {
772 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
773 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
774 }
775 };
776
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
780 if (resultCode == RESULT_CANCELED) {
781 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
783 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700784 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700785 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
786 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700787 }
788 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
790 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
791 mWorkspace.exitOverviewMode(false);
792 }
793 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200795
Adam Cohened66b2b2012-01-23 17:28:51 -0800796 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
797 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700798
Adam Cohened66b2b2012-01-23 17:28:51 -0800799 // We have special handling for widgets
800 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800801 final int appWidgetId;
802 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
803 : -1;
804 if (widgetId < 0) {
805 appWidgetId = pendingAddWidgetId;
806 } else {
807 appWidgetId = widgetId;
808 }
809
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 final int result;
811 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800812 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700813 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
814 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700815 result = RESULT_CANCELED;
816 completeTwoStageWidgetDrop(result, appWidgetId);
817 onComplete = new Runnable() {
818 @Override
819 public void run() {
820 exitSpringLoadedDragModeDelayed(false, 0, null);
821 }
822 };
Winson Chung5aaab772012-04-27 15:24:02 -0700823 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700825 final CellLayout dropLayout =
826 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
827 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700832 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 }
834 };
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
837 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800838 return;
839 }
840
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 // The pattern used here is that a user PICKs a specific application,
842 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
845 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700846 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800847 final PendingAddArguments args = new PendingAddArguments();
848 args.requestCode = requestCode;
849 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700851 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 args.cellX = mPendingAddInfo.cellX;
853 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
855 sPendingAddList.add(args);
856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700859 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700862 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 }
867
868 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700869 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700870 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800871 Runnable onCompleteRunnable = null;
872 int animationType = 0;
873
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700874 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 if (resultCode == RESULT_OK) {
876 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
877 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 mPendingAddWidgetInfo);
879 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 onCompleteRunnable = new Runnable() {
881 @Override
882 public void run() {
883 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700884 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700885 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
886 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 }
888 };
889 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800890 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700893 if (mDragLayer.getAnimatedView() != null) {
894 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
895 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
896 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700897 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898 // The animated view may be null in the case of a rotation during widget configuration
899 onCompleteRunnable.run();
900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902
903 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700904 protected void onStop() {
905 super.onStop();
906 FirstFrameAnimatorHelper.setIsVisible(false);
907 }
908
909 @Override
910 protected void onStart() {
911 super.onStart();
912 FirstFrameAnimatorHelper.setIsVisible(true);
913 }
914
915 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200917 long startTime = 0;
918 if (DEBUG_RESUME_TIME) {
919 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400920 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700923
Winson Chung4a2afa32012-07-19 14:53:05 -0700924 // Restore the previous launcher state
925 if (mOnResumeState == State.WORKSPACE) {
926 showWorkspace(false);
927 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800928 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700929 }
930 mOnResumeState = State.NONE;
931
Craig Mautner360310b2012-10-26 15:13:08 -0700932 // Background was set to gradient in onPause(), restore to black if in all apps.
933 setWorkspaceBackground(mState == State.WORKSPACE);
934
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800935 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700936 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700937 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400938 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500939 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400940 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700941 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700943 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200944 // We might have postponed some bind calls until onResume (see waitUntilResume) --
945 // execute them here
946 long startTimeCallbacks = 0;
947 if (DEBUG_RESUME_TIME) {
948 startTimeCallbacks = System.currentTimeMillis();
949 }
950
951 if (mAppsCustomizeContent != null) {
952 mAppsCustomizeContent.setBulkBind(true);
953 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700954 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
955 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200956 }
957 if (mAppsCustomizeContent != null) {
958 mAppsCustomizeContent.setBulkBind(false);
959 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700960 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200961 if (DEBUG_RESUME_TIME) {
962 Log.d(TAG, "Time spent processing callbacks in onResume: " +
963 (System.currentTimeMillis() - startTimeCallbacks));
964 }
Michael Jurka447bf842013-05-15 14:52:15 +0200965 }
Michael Jurka54554252013-08-01 12:52:23 +0200966 if (mOnResumeCallbacks.size() > 0) {
967 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
968 mOnResumeCallbacks.get(i).run();
969 }
970 mOnResumeCallbacks.clear();
971 }
Winson Chunge4e50662012-01-23 14:45:13 -0800972
973 // Reset the pressed state of icons that were locked in the press state while activities
974 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800975 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800976 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800977 mWaitingForResume.setStayPressed(false);
978 }
Winson Chunge4e50662012-01-23 14:45:13 -0800979 if (mAppsCustomizeContent != null) {
980 // Resets the previous all apps icon press state
981 mAppsCustomizeContent.resetDrawableState();
982 }
Winson Chung82963d52013-10-09 11:20:57 -0700983
Adam Cohen06dff352012-06-01 17:17:08 -0700984 // It is possible that widgets can receive updates while launcher is not in the foreground.
985 // Consequently, the widgets will be inflated in the orientation of the foreground activity
986 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
987 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700988 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700989
Winson Chung780fe592013-09-26 14:48:44 -0700990 // Process any items that were added while Launcher was away.
991 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
992
Winson Chung5841efa2013-09-30 18:06:44 -0700993 // Update the voice search button proxy
994 updateVoiceButtonProxyVisible(false);
995
Adam Cohenf9426d52012-06-04 17:26:21 -0700996 // Again, as with the above scenario, it's possible that one or more of the global icons
997 // were updated in the wrong orientation.
998 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200999 if (DEBUG_RESUME_TIME) {
1000 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1001 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001002
1003 if (mWorkspace.getCustomContentCallbacks() != null) {
1004 // If we are resuming and the custom content is the current page, we call onShow().
1005 // It is also poassible that onShow will instead be called slightly after first layout
1006 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1007 if (mWorkspace.isOnOrMovingToCustomContent()) {
1008 mWorkspace.getCustomContentCallbacks().onShow();
1009 }
1010 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001011 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001012 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001017 // Ensure that items added to Launcher are queued until Launcher returns
1018 InstallShortcutReceiver.enableInstallQueue();
1019
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001021 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001022 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001023 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001024
1025 // We call onHide() aggressively. The custom content callbacks should be able to
1026 // debounce excess onHide calls.
1027 if (mWorkspace.getCustomContentCallbacks() != null) {
1028 mWorkspace.getCustomContentCallbacks().onHide();
1029 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
Adam Cohen66a01fd2013-06-11 12:48:00 -07001032 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001033 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001034 addCustomContentToLeft();
1035 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001036 }
1037
Adam Cohenbffe7452013-07-22 18:21:45 -07001038 QSBScroller mQsbScroller = new QSBScroller() {
1039 int scrollY = 0;
1040
1041 @Override
1042 public void setScrollY(int scroll) {
1043 scrollY = scroll;
1044
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
1046 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001047 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001048 }
1049 }
1050 };
1051
1052 public void resetQSBScroll() {
1053 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001054 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001055 }
1056
1057 public interface CustomContentCallbacks {
1058 // Custom content is completely shown
1059 public void onShow();
1060
1061 // Custom content is completely hidden
1062 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001063
1064 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1065 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001066 }
1067
Adam Cohen30bacb22013-08-29 14:25:25 -07001068 protected void startSettings() {
1069 }
1070
Adam Cohenbffe7452013-07-22 18:21:45 -07001071 public interface QSBScroller {
1072 public void setScrollY(int scrollY);
1073 }
1074
Winson Chung98ca0f72013-07-29 12:58:51 -07001075 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001076 CustomContentCallbacks callbacks, String description) {
1077 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001078 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001079 }
1080
Adam Cohenedb40762013-07-18 16:45:45 -07001081 // The custom content needs to offset its content to account for the QSB
1082 public int getTopOffsetForCustomContent() {
1083 return mWorkspace.getPaddingTop();
1084 }
1085
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001086 @Override
1087 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 // Flag the loader to stop early before switching
1089 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 if (mAppsCustomizeContent != null) {
1091 mAppsCustomizeContent.surrender();
1092 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001093 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001094 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001096 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001097 @Override
1098 public void onWindowFocusChanged(boolean hasFocus) {
1099 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001100 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001101 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 private boolean acceptFilter() {
1104 final InputMethodManager inputManager = (InputMethodManager)
1105 getSystemService(Context.INPUT_METHOD_SERVICE);
1106 return !inputManager.isFullscreenMode();
1107 }
1108
1109 @Override
1110 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001111 final int uniChar = event.getUnicodeChar();
1112 final boolean handled = super.onKeyDown(keyCode, event);
1113 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1114 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1116 keyCode, event);
1117 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001118 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001119 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001120 // showSearchDialog()
1121 // If there are multiple keystrokes before the search dialog takes focus,
1122 // onSearchRequested() will be called for every keystroke,
1123 // but it is idempotent, so it's fine.
1124 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
1127
Joe Onorato8a9625e2010-01-28 15:55:35 -08001128 // Eat the long press event so the keyboard doesn't come up.
1129 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1130 return true;
1131 }
1132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 return handled;
1134 }
1135
Karl Rosaen138a0412009-04-23 19:00:21 -07001136 private String getTypedText() {
1137 return mDefaultKeySsb.toString();
1138 }
1139
1140 private void clearTypedText() {
1141 mDefaultKeySsb.clear();
1142 mDefaultKeySsb.clearSpans();
1143 Selection.setSelection(mDefaultKeySsb, 0);
1144 }
1145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001147 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1148 * State
1149 */
1150 private static State intToState(int stateOrdinal) {
1151 State state = State.WORKSPACE;
1152 final State[] stateValues = State.values();
1153 for (int i = 0; i < stateValues.length; i++) {
1154 if (stateValues[i].ordinal() == stateOrdinal) {
1155 state = stateValues[i];
1156 break;
1157 }
1158 }
1159 return state;
1160 }
1161
1162 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 * Restores the previous state, if it exists.
1164 *
1165 * @param savedState The previous state.
1166 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001167 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 private void restoreState(Bundle savedState) {
1169 if (savedState == null) {
1170 return;
1171 }
1172
Michael Jurka883f55b2010-10-21 15:47:14 -07001173 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001174 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001175 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001176 }
1177
Adam Cohen21cd0022013-10-09 18:57:02 -07001178 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1179 PagedView.INVALID_RESTORE_PAGE);
1180 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001181 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183
Winson Chung3d503fb2011-07-13 17:25:49 -07001184 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186
Winson Chung3d503fb2011-07-13 17:25:49 -07001187 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1188 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001189 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1191 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001192 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1193 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1194 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001195 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001196 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 mRestoring = true;
1198 }
1199
1200 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1201 if (renameFolder) {
1202 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001203 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 mRestoring = true;
1205 }
Winson Chunga12a2502010-12-20 14:41:35 -08001206
Winson Chung785d2eb2011-04-14 16:08:02 -07001207 // Restore the AppsCustomize tab
1208 if (mAppsCustomizeTabHost != null) {
1209 String curTab = savedState.getString("apps_customize_currentTab");
1210 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001211 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001213 mAppsCustomizeContent.loadAssociatedPages(
1214 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 }
1216
Winson Chung5afbf7b2011-07-25 11:53:08 -07001217 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1218 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001220 mItemIdToViewId = (HashMap<Integer, Integer>)
1221 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 /**
1225 * Finds all the views we need and configure them properly.
1226 */
1227 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001228 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001229
Craig Mautner360310b2012-10-26 15:13:08 -07001230 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001231 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1232 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001233
John Spurlock77e1f472013-09-11 10:09:51 -04001234 mLauncherView.setSystemUiVisibility(
1235 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001236 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
Winson Chung4c98d922011-05-31 16:50:48 -07001238 // Setup the drag layer
1239 mDragLayer.setup(this, dragController);
1240
Winson Chung3d503fb2011-07-13 17:25:49 -07001241 // Setup the hotseat
1242 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1243 if (mHotseat != null) {
1244 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001245 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001246 }
1247
Adam Cohenf358a4b2013-07-23 16:47:31 -07001248 mOverviewPanel = findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001249 View widgetButton = findViewById(R.id.widget_button);
1250 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001251 @Override
1252 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001253 if (!mWorkspace.isSwitchingState()) {
1254 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1255 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001256 }
1257 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001258 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1259
1260 View wallpaperButton = findViewById(R.id.wallpaper_button);
1261 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001262 @Override
1263 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001264 if (!mWorkspace.isSwitchingState()) {
1265 startWallpaper();
1266 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001267 }
1268 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001269 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1270
1271 View settingsButton = findViewById(R.id.settings_button);
1272 settingsButton.setOnClickListener(new OnClickListener() {
Adam Cohen30bacb22013-08-29 14:25:25 -07001273 @Override
1274 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001275 if (!mWorkspace.isSwitchingState()) {
1276 startSettings();
1277 }
Adam Cohen30bacb22013-08-29 14:25:25 -07001278 }
1279 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001280 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohendbdff6b2013-09-18 19:09:15 -07001281 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001282
Winson Chung4c98d922011-05-31 16:50:48 -07001283 // Setup the workspace
1284 mWorkspace.setHapticFeedbackEnabled(false);
1285 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001286 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001287 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001288
Winson Chungf0ea4d32011-06-06 14:27:16 -07001289 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001290 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001291
Winson Chungf0ea4d32011-06-06 14:27:16 -07001292 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001293 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001294 mAppsCustomizeContent = (AppsCustomizePagedView)
1295 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1296 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001297
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001299 dragController.setDragScoller(mWorkspace);
1300 dragController.setScrollView(mDragLayer);
1301 dragController.setMoveTarget(mWorkspace);
1302 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001303 if (mSearchDropTargetBar != null) {
1304 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001305 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001306
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001307 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001308 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001309 mWeightWatcher = new WeightWatcher(this);
1310 mWeightWatcher.setAlpha(0.5f);
1311 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001312 new FrameLayout.LayoutParams(
1313 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001314 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001315 Gravity.BOTTOM)
1316 );
Adam Cohen39a06042013-07-19 14:30:12 -07001317
1318 boolean show = shouldShowWeightWatcher();
1319 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 /**
1324 * Creates a view representing a shortcut.
1325 *
1326 * @param info The data structure describing the shortcut.
1327 *
1328 * @return A View inflated from R.layout.application.
1329 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001330 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001332 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 /**
1336 * Creates a view representing a shortcut inflated from the specified resource.
1337 *
1338 * @param layoutResId The id of the XML layout used to create the shortcut.
1339 * @param parent The group the shortcut belongs to.
1340 * @param info The data structure describing the shortcut.
1341 *
1342 * @return A View inflated from layoutResId.
1343 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001344 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001345 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1346 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 return favorite;
1349 }
1350
1351 /**
1352 * Add an application shortcut to the workspace.
1353 *
1354 * @param data The intent describing the application.
1355 * @param cellInfo The position on screen where to create the shortcut.
1356 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001359 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001360
Adam Cohenfbba09b2011-07-18 21:43:05 -07001361 // First we check if we already know the exact location where we want to add this item.
1362 if (cellX >= 0 && cellY >= 0) {
1363 cellXY[0] = cellX;
1364 cellXY[1] = cellY;
1365 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001366 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 return;
1368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001370 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001371
Romain Guy73b979d2009-06-09 12:57:21 -07001372 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001373 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001375 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001376 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001377 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001378 } else {
1379 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381 }
Romain Guycbb89e42009-06-08 15:52:54 -07001382
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 /**
1384 * Add a shortcut to the workspace.
1385 *
1386 * @param data The intent describing the shortcut.
1387 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001389 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 int cellY) {
1391 int[] cellXY = mTmpAddItemCellCoordinates;
1392 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001393 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001394
Michael Jurkad3ef3062010-11-23 16:23:58 -08001395 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001396
Adam Cohen558baaf2011-08-15 15:22:57 -07001397 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001398 if (info == null) {
1399 return;
1400 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001401 final View view = createShortcut(info);
1402
Adam Cohenfbba09b2011-07-18 21:43:05 -07001403 // First we check if we already know the exact location where we want to add this item.
1404 if (cellX >= 0 && cellY >= 0) {
1405 cellXY[0] = cellX;
1406 cellXY[1] = cellY;
1407 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001408
1409 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001410 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001411 true, null,null)) {
1412 return;
1413 }
1414 DragObject dragObject = new DragObject();
1415 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001416 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1417 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001418 return;
1419 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001421 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001423 foundCellSpan = (result != null);
1424 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001425 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001426 }
1427
1428 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001429 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 return;
1431 }
1432
Adam Cohendcd297f2013-06-18 13:13:40 -07001433 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
1435 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001436 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001437 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439 }
1440
Adam Cohen2f093b62012-04-30 18:59:53 -07001441 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1442 int minHeight) {
1443 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001444 // We want to account for the extra amount of padding that we are adding to the widget
1445 // to ensure that it gets the full amount of space that it has requested
1446 int requiredWidth = minWidth + padding.left + padding.right;
1447 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001448 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001449 }
1450
Adam Cohen2f093b62012-04-30 18:59:53 -07001451 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1452 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001453 }
1454
Adam Cohen2f093b62012-04-30 18:59:53 -07001455 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1456 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001457 }
1458
Adam Cohen2f093b62012-04-30 18:59:53 -07001459 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1460 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001461 }
1462
Adam Cohen2f093b62012-04-30 18:59:53 -07001463 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1464 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001465 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001469 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001471 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001472 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001474 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001475 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1476 if (appWidgetInfo == null) {
1477 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1478 }
Romain Guycbb89e42009-06-08 15:52:54 -07001479
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001480 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001481 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001482
Adam Cohen2f093b62012-04-30 18:59:53 -07001483 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1484 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001487 // We have saved the position to which the widget was dragged-- this really only matters
1488 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 int[] cellXY = mTmpAddItemCellCoordinates;
1490 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001491 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001493 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1494 cellXY[0] = mPendingAddInfo.cellX;
1495 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001496 spanXY[0] = mPendingAddInfo.spanX;
1497 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 foundCellSpan = true;
1499 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001500 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001501 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001502 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1503 spanXY[1], cellXY, finalSpan);
1504 spanXY[0] = finalSpan[0];
1505 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001506 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001508 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001509 }
1510
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001512 if (appWidgetId != -1) {
1513 // Deleting an app widget ID is a void call but writes to disk before returning
1514 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001515 new AsyncTask<Void, Void, Void>() {
1516 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001517 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001518 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001519 }
Michael Jurka43467462013-11-14 12:03:58 +01001520 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001521 }
Winson Chung93eef082012-03-23 15:59:27 -07001522 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001523 return;
1524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001526 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001527 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1528 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 launcherInfo.spanX = spanXY[0];
1530 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001531 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1532 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001535 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536
1537 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001538 if (hostView == null) {
1539 // Perform actual inflation because we're live
1540 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1541 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1542 } else {
1543 // The AppWidgetHostView has already been inflated and instantiated
1544 launcherInfo.hostView = hostView;
1545 }
Romain Guycbb89e42009-06-08 15:52:54 -07001546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001548 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001549 launcherInfo.notifyWidgetSizeChanged(this);
1550
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001553
1554 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001556 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Adam Cohended9f8d2010-11-03 13:25:16 -07001559 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1560 @Override
1561 public void onReceive(Context context, Intent intent) {
1562 final String action = intent.getAction();
1563 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1564 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001565 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001566 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001567
Winson Chungc100e8e2011-08-09 16:02:43 -07001568 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001569 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001570 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001571 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001572 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001573 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1574 mUserPresent = true;
1575 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001576 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1577 mModel.resetLoadedState(false, true);
1578 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1579 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1580 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1581 mModel.resetLoadedState(false, true);
1582 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1583 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1584 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001585 }
1586 }
1587 };
1588
1589 @Override
1590 public void onAttachedToWindow() {
1591 super.onAttachedToWindow();
1592
1593 // Listen for broadcasts related to user-presence
1594 final IntentFilter filter = new IntentFilter();
1595 filter.addAction(Intent.ACTION_SCREEN_OFF);
1596 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001597 if (ENABLE_DEBUG_INTENTS) {
1598 filter.addAction(DebugIntents.DELETE_DATABASE);
1599 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1600 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001601 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001602 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001603 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001604 mVisible = true;
1605 }
1606
1607 @Override
1608 public void onDetachedFromWindow() {
1609 super.onDetachedFromWindow();
1610 mVisible = false;
1611
Adam Cohend113e0c2010-11-11 10:48:05 -08001612 if (mAttached) {
1613 unregisterReceiver(mReceiver);
1614 mAttached = false;
1615 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001616 updateRunning();
1617 }
1618
1619 public void onWindowVisibilityChanged(int visibility) {
1620 mVisible = visibility == View.VISIBLE;
1621 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001622 // The following code used to be in onResume, but it turns out onResume is called when
1623 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1624 // is a more appropriate event to handle
1625 if (mVisible) {
1626 mAppsCustomizeTabHost.onWindowVisible();
1627 if (!mWorkspaceLoading) {
1628 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001629 // We want to let Launcher draw itself at least once before we force it to build
1630 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001631 // apps is nice and speedy.
1632 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001633 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001634 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001635 if (mStarted) return;
1636 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001637 // We delay the layer building a bit in order to give
1638 // other message processing a time to run. In particular
1639 // this avoids a delay in hiding the IME if it was
1640 // currently shown, because doing that may involve
1641 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001642 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001643 final ViewTreeObserver.OnDrawListener listener = this;
1644 mWorkspace.post(new Runnable() {
1645 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001646 if (mWorkspace != null &&
1647 mWorkspace.getViewTreeObserver() != null) {
1648 mWorkspace.getViewTreeObserver().
1649 removeOnDrawListener(listener);
1650 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001651 }
1652 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001653 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001654 }
1655 });
1656 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001657 // When Launcher comes back to foreground, a different Activity might be responsible for
1658 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001659 if (!DISABLE_MARKET_BUTTON) {
1660 updateAppMarketIcon();
1661 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001662 clearTypedText();
1663 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 }
1665
1666 private void sendAdvanceMessage(long delay) {
1667 mHandler.removeMessages(ADVANCE_MSG);
1668 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1669 mHandler.sendMessageDelayed(msg, delay);
1670 mAutoAdvanceSentTime = System.currentTimeMillis();
1671 }
1672
1673 private void updateRunning() {
1674 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1675 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1676 mAutoAdvanceRunning = autoAdvanceRunning;
1677 if (autoAdvanceRunning) {
1678 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1679 sendAdvanceMessage(delay);
1680 } else {
1681 if (!mWidgetsToAdvance.isEmpty()) {
1682 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1683 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1684 }
1685 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001686 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 }
1688 }
1689 }
1690
1691 private final Handler mHandler = new Handler() {
1692 @Override
1693 public void handleMessage(Message msg) {
1694 if (msg.what == ADVANCE_MSG) {
1695 int i = 0;
1696 for (View key: mWidgetsToAdvance.keySet()) {
1697 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1698 final int delay = mAdvanceStagger * i;
1699 if (v instanceof Advanceable) {
1700 postDelayed(new Runnable() {
1701 public void run() {
1702 ((Advanceable) v).advance();
1703 }
1704 }, delay);
1705 }
1706 i++;
1707 }
1708 sendAdvanceMessage(mAdvanceInterval);
1709 }
1710 }
1711 };
1712
1713 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001714 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1716 if (v instanceof Advanceable) {
1717 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001718 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 updateRunning();
1720 }
1721 }
1722
1723 void removeWidgetToAutoAdvance(View hostView) {
1724 if (mWidgetsToAdvance.containsKey(hostView)) {
1725 mWidgetsToAdvance.remove(hostView);
1726 updateRunning();
1727 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 }
1729
Joe Onorato9c1289c2009-08-17 11:03:03 -04001730 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001732 launcherInfo.hostView = null;
1733 }
1734
Winson Chung93eef082012-03-23 15:59:27 -07001735 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1736 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1737 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 }
1739
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001740 public LauncherAppWidgetHost getAppWidgetHost() {
1741 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 }
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Winson Chunga9abd0e2010-10-27 17:18:37 -07001744 public LauncherModel getModel() {
1745 return mModel;
1746 }
1747
Bjorn Bringertc459e522013-06-07 19:36:01 +01001748 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001749 getWindow().closeAllPanels();
1750
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001751 // Whatever we were doing is hereby canceled.
1752 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001753 }
1754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 @Override
1756 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001757 long startTime = 0;
1758 if (DEBUG_RESUME_TIME) {
1759 startTime = System.currentTimeMillis();
1760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 super.onNewIntent(intent);
1762
1763 // Close the menu
1764 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001765 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001766 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001767
Adam Cohened307df2013-10-02 09:37:31 -07001768 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1769 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1770 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001771
Adam Cohen6fecd412013-10-02 17:41:50 -07001772 if (mWorkspace == null) {
1773 // Can be cases where mWorkspace is null, this prevents a NPE
1774 return;
1775 }
1776 Folder openFolder = mWorkspace.getOpenFolder();
1777 // In all these cases, only animate if we're already on home
1778 mWorkspace.exitWidgetResizeMode();
1779 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001780 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001781 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001782 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001783
Adam Cohen6fecd412013-10-02 17:41:50 -07001784 closeFolder();
1785 exitSpringLoadedDragMode();
1786
1787 // If we are already on home, then just animate back to the workspace,
1788 // otherwise, just wait until onResume to set the state back to Workspace
1789 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001790 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001791 } else {
1792 mOnResumeState = State.WORKSPACE;
1793 }
1794
1795 final View v = getWindow().peekDecorView();
1796 if (v != null && v.getWindowToken() != null) {
1797 InputMethodManager imm = (InputMethodManager)getSystemService(
1798 INPUT_METHOD_SERVICE);
1799 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1800 }
1801
1802 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001803 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001804 mAppsCustomizeTabHost.reset();
1805 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001806
1807 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 }
Adam Cohened307df2013-10-02 09:37:31 -07001809
Michael Jurka447bf842013-05-15 14:52:15 +02001810 if (DEBUG_RESUME_TIME) {
1811 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1812 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814
Adam Coppa120b8e2013-11-12 16:26:04 +00001815 /**
1816 * Override point for subclasses to prevent movement to the default screen when the home
1817 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1818 */
1819 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1820 return true;
1821 }
1822
1823 /**
1824 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1825 */
1826 protected void onHomeIntent() {
1827 // Do nothing
1828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001831 public void onRestoreInstanceState(Bundle state) {
1832 super.onRestoreInstanceState(state);
1833 for (int page: mSynchronouslyBoundPages) {
1834 mWorkspace.restoreInstanceStateForChild(page);
1835 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
1838 @Override
1839 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001840 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001841 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1842 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001843 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001844 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845
Michael Jurka883f55b2010-10-21 15:47:14 -07001846 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001847 // We close any open folder since it will not be re-opened, and we need to make sure
1848 // this state is reflected.
1849 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850
Adam Cohendcd297f2013-06-18 13:13:40 -07001851 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001852 mWaitingForResult) {
1853 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001854 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001855 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1856 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001857 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1858 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1859 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001860 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 }
1862
1863 if (mFolderInfo != null && mWaitingForResult) {
1864 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1865 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1866 }
Michael Jurka946ad472010-07-09 18:05:18 -07001867
Winson Chung785d2eb2011-04-14 16:08:02 -07001868 // Save the current AppsCustomize tab
1869 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001870 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1871 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001872 if (currentTabTag != null) {
1873 outState.putString("apps_customize_currentTab", currentTabTag);
1874 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001875 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1876 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001877 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001878 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
1881 @Override
1882 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001884
Winson Chunge7a03942011-08-05 15:05:12 -07001885 // Remove all pending runnables
1886 mHandler.removeMessages(ADVANCE_MSG);
1887 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001888 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001889
Winson Chungcd2b0142011-06-08 16:02:26 -07001890 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001891 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001892 mModel.stopLoader();
1893 app.setLauncher(null);
1894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001896 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001898 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001900 mAppWidgetHost = null;
1901
1902 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
1904 TextKeyListener.getInstance().release();
1905
Winson Chung81b52252012-08-27 15:34:29 -07001906 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1907 // to prevent leaking Launcher activities on orientation change.
1908 if (mModel != null) {
1909 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1910 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001911
1912 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001913 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001914
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001915 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001916 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001917 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001918 mWorkspace = null;
1919 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001920
Michael Jurka2ecf9952012-06-18 12:52:28 -07001921 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923
Adam Cohena9cf38f2011-05-02 15:36:58 -07001924 public DragController getDragController() {
1925 return mDragController;
1926 }
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 @Override
1929 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001930 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 super.startActivityForResult(intent, requestCode);
1932 }
1933
Winson Chung70d72102011-08-12 11:24:35 -07001934 /**
1935 * Indicates that we want global search for this activity by setting the globalSearch
1936 * argument for {@link #startSearch} to true.
1937 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001939 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001941
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001942 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001943
Karl Rosaen138a0412009-04-23 19:00:21 -07001944 if (initialQuery == null) {
1945 // Use any text typed in the launcher as the initial query
1946 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 if (appSearchData == null) {
1949 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001950 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
Winson Chungadf0c182012-08-23 14:59:07 -07001952 Rect sourceBounds = new Rect();
1953 if (mSearchDropTargetBar != null) {
1954 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1955 }
Romain Guycbb89e42009-06-08 15:52:54 -07001956
Bjorn Bringertc459e522013-06-07 19:36:01 +01001957 startSearch(initialQuery, selectInitialQuery,
1958 appSearchData, sourceBounds);
1959 }
1960
1961 public void startSearch(String initialQuery,
1962 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001963 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001964 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001965 }
1966
1967 /**
1968 * Starts the global search activity. This code is a copied from SearchManager
1969 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001970 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001971 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001972 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001973 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1974 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1975 if (globalSearchActivity == null) {
1976 Log.w(TAG, "No global search activity found.");
1977 return;
1978 }
1979 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1980 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1981 intent.setComponent(globalSearchActivity);
1982 // Make sure that we have a Bundle to put source in
1983 if (appSearchData == null) {
1984 appSearchData = new Bundle();
1985 } else {
1986 appSearchData = new Bundle(appSearchData);
1987 }
1988 // Set source to package name of app that starts global search, if not set already.
1989 if (!appSearchData.containsKey("source")) {
1990 appSearchData.putString("source", getPackageName());
1991 }
1992 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1993 if (!TextUtils.isEmpty(initialQuery)) {
1994 intent.putExtra(SearchManager.QUERY, initialQuery);
1995 }
1996 if (selectInitialQuery) {
1997 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1998 }
1999 intent.setSourceBounds(sourceBounds);
2000 try {
2001 startActivity(intent);
2002 } catch (ActivityNotFoundException ex) {
2003 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006
Winson Chung70d72102011-08-12 11:24:35 -07002007 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002008 public boolean onPrepareOptionsMenu(Menu menu) {
2009 super.onPrepareOptionsMenu(menu);
Dan Sandler14c6a3f2014-01-07 14:12:27 -05002010 showOverviewMode(true);
Michael Jurkab94f3f82013-09-11 18:08:54 +02002011 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002014 @Override
2015 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002016 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002017 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002018 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002019 }
2020
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 public boolean isWorkspaceLocked() {
2022 return mWorkspaceLoading || mWaitingForResult;
2023 }
2024
Michael Jurka0280c3b2010-09-17 15:00:07 -07002025 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002026 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002028 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2029 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002030 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002031 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002032 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002033
Adam Cohenad4e15c2013-10-17 16:21:35 -07002034 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2035 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2036 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2037 }
2038
2039 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2040 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2041 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002042 if (appWidgetInfo.configure != null) {
2043 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002044 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002045
Bjorn Bringert7984c942009-12-09 15:38:25 +00002046 // Launch over to configure widget, if needed
2047 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002048 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002049 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002050 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002052 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002053 Runnable onComplete = new Runnable() {
2054 @Override
2055 public void run() {
2056 // Exit spring loaded mode if necessary after adding the widget
2057 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2058 null);
2059 }
2060 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002062 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002063 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065 }
Romain Guycbb89e42009-06-08 15:52:54 -07002066
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002067 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002068 // Close any folders that may be open.
2069 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002070 mWorkspace.moveToCustomContentScreen(animate);
2071 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002072 /**
2073 * Process a shortcut drop.
2074 *
2075 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002076 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002077 * @param cell The cell it should be added to, optional
2078 * @param position The location on the screen where it was dropped, optional
2079 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002080 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002082 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002084 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002085 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002086
2087 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002088 mPendingAddInfo.cellX = cell[0];
2089 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002090 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091
2092 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2093 createShortcutIntent.setComponent(componentName);
2094 processShortcut(createShortcutIntent);
2095 }
2096
Adam Cohenfbba09b2011-07-18 21:43:05 -07002097 /**
2098 * Process a widget drop.
2099 *
2100 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002102 * @param cell The cell it should be added to, optional
2103 * @param position The location on the screen where it was dropped, optional
2104 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002105 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002106 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002107 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002108 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002109 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002110 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002111 mPendingAddInfo.minSpanX = info.minSpanX;
2112 mPendingAddInfo.minSpanY = info.minSpanY;
2113
Adam Cohenfbba09b2011-07-18 21:43:05 -07002114 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 mPendingAddInfo.cellX = cell[0];
2116 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002117 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002118 if (span != null) {
2119 mPendingAddInfo.spanX = span[0];
2120 mPendingAddInfo.spanY = span[1];
2121 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002122
Adam Cohened66b2b2012-01-23 17:28:51 -08002123 AppWidgetHostView hostView = info.boundWidget;
2124 int appWidgetId;
2125 if (hostView != null) {
2126 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002127 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002128 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002129 // In this case, we either need to start an activity to get permission to bind
2130 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002131 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002132 Bundle options = info.bindOptions;
2133
2134 boolean success = false;
2135 if (options != null) {
2136 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2137 info.componentName, options);
2138 } else {
2139 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2140 info.componentName);
2141 }
2142 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002143 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002144 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002145 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002146 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2147 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2148 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002149 // TODO: we need to make sure that this accounts for the options bundle.
2150 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002151 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2152 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002153 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002154 }
2155
Joe Onoratodeb98af2010-02-19 14:59:39 -08002156 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002157 // Handle case where user selected "Applications"
2158 String applicationName = getResources().getString(R.string.group_applications);
2159 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002160
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002161 if (applicationName != null && applicationName.equals(shortcutName)) {
2162 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2163 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002164
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002165 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2166 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002167 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002168 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002169 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002170 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
2173
Winson Chung24ab2f12010-09-16 14:10:47 -07002174 void processWallpaper(Intent intent) {
2175 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2176 }
2177
Adam Cohendcd297f2013-06-18 13:13:40 -07002178 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002179 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002180 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 folderInfo.title = getText(R.string.folder_name);
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002184 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002185 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002186 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187
2188 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002189 FolderIcon newFolder =
2190 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002191 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002192 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002193 // Force measure the new folder icon
2194 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2195 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002196 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
Romain Guycbb89e42009-06-08 15:52:54 -07002198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002200 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 }
2202
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002203 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002205 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002206 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
2208
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002209 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002210 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002211 }
2212
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002213 /**
2214 * Registers various content observers. The current implementation registers
2215 * only a favorites observer to keep track of the favorites applications.
2216 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002217 private void registerContentObservers() {
2218 ContentResolver resolver = getContentResolver();
2219 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2220 true, mWidgetObserver);
2221 }
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 @Override
2224 public boolean dispatchKeyEvent(KeyEvent event) {
2225 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2226 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002228 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002229 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002230 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002231 dumpState();
2232 return true;
2233 }
2234 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002235 }
2236 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2237 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002238 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 return true;
2240 }
2241 }
2242
2243 return super.dispatchKeyEvent(event);
2244 }
2245
Joe Onorato88ec0992009-11-19 13:16:06 -08002246 @Override
2247 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002248 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002249 if (mAppsCustomizeContent.getContentType() ==
2250 AppsCustomizePagedView.ContentType.Applications) {
2251 showWorkspace(true);
2252 } else {
2253 showOverviewMode(true);
2254 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002255 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002256 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002257 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002258 Folder openFolder = mWorkspace.getOpenFolder();
2259 if (openFolder.isEditingName()) {
2260 openFolder.dismissEditingName();
2261 } else {
2262 closeFolder();
2263 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002264 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002265 mWorkspace.exitWidgetResizeMode();
2266
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002267 // Back button is a no-op here, but give at least some feedback for the button press
2268 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002269 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002270 }
2271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002273 * Re-listen when widgets are reset.
2274 */
2275 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002276 if (mAppWidgetHost != null) {
2277 mAppWidgetHost.startListening();
2278 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002279 }
2280
2281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 * Launches the intent referred by the clicked shortcut.
2283 *
2284 * @param v The view representing the clicked shortcut.
2285 */
2286 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002287 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2288 // view has detached (it's possible for this to happen if the view is removed mid touch).
2289 if (v.getWindowToken() == null) {
2290 return;
2291 }
2292
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002293 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002294 return;
2295 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002296
Adam Cohen1697b792013-09-17 19:08:21 -07002297 if (v instanceof Workspace) {
2298 if (mWorkspace.isInOverviewMode()) {
2299 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002300 }
2301 return;
2302 }
2303
2304 if (v instanceof CellLayout) {
2305 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002306 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002307 }
2308 }
2309
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002311 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002313 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2314 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002315
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002316 // Check for special shortcuts
2317 if (intent.getComponent() != null) {
2318 final String shortcutClass = intent.getComponent().getClassName();
2319
2320 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002321 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002322 return;
2323 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2324 MemoryDumpActivity.startDump(this);
2325 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002326 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2327 toggleShowWeightWatcher();
2328 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002329 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002330 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002331
2332 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002333 int[] pos = new int[2];
2334 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002335 intent.setSourceBounds(new Rect(pos[0], pos[1],
2336 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002337
2338 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002339
Daniel Sandlerff02d492013-08-05 02:12:05 -04002340 mStats.recordLaunch(intent, shortcut);
2341
Michael Jurkaddd62e92011-02-16 17:49:14 -08002342 if (success && v instanceof BubbleTextView) {
2343 mWaitingForResume = (BubbleTextView) v;
2344 mWaitingForResume.setStayPressed(true);
2345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002347 if (v instanceof FolderIcon) {
2348 FolderIcon fi = (FolderIcon) v;
2349 handleFolderClick(fi);
2350 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002351 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002352 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002353 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002354 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002355 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002356 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 }
2358 }
2359
Michael Jurka0e260592010-06-30 17:07:39 -07002360 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002361 return false;
2362 }
2363
Michael Jurkaaf442092010-06-10 17:01:57 -07002364 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002365 * Event handler for the search button
2366 *
2367 * @param v The view that was clicked.
2368 */
2369 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002370 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2371
Amith Yamasania135ba82011-08-09 17:42:01 -07002372 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002373 }
2374
2375 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002376 * Event handler for the voice button
2377 *
2378 * @param v The view that was clicked.
2379 */
2380 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002381 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002382
Bjorn Bringertc459e522013-06-07 19:36:01 +01002383 startVoice();
2384 }
2385
2386 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002387 try {
2388 final SearchManager searchManager =
2389 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2390 ComponentName activityName = searchManager.getGlobalSearchActivity();
2391 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002392 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002393 if (activityName != null) {
2394 intent.setPackage(activityName.getPackageName());
2395 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002396 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002397 } catch (ActivityNotFoundException e) {
2398 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002399 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002400 startActivitySafely(null, intent, "onClickVoiceButton");
2401 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002402 }
2403
2404 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002405 * Event handler for the "grid" button that appears on the home screen, which
2406 * enters all apps mode.
2407 *
2408 * @param v The view that was clicked.
2409 */
2410 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002411 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002412 }
2413
2414 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002415 // Provide the same haptic feedback that the system offers for virtual keys.
2416 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002417 }
2418
Adam Cohen61f560d2013-09-30 15:58:20 -07002419 public void performHapticFeedbackOnTouchDown(View v) {
2420 // Provide the same haptic feedback that the system offers for virtual keys.
2421 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2422 }
2423
2424 public View.OnTouchListener getHapticFeedbackTouchListener() {
2425 if (mHapticFeedbackTouchListener == null) {
2426 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2427 @Override
2428 public boolean onTouch(View v, MotionEvent event) {
2429 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2430 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2431 }
2432 return false;
2433 }
2434 };
2435 }
2436 return mHapticFeedbackTouchListener;
2437 }
2438
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002439 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002440 if (!DISABLE_MARKET_BUTTON) {
2441 if (mAppMarketIntent != null) {
2442 startActivitySafely(v, mAppMarketIntent, "app market");
2443 } else {
2444 Log.e(TAG, "Invalid app market intent.");
2445 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002446 }
2447 }
2448
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002449 /**
2450 * Called when the user stops interacting with the launcher.
2451 * This implies that the user is now on the homescreen and is not doing housekeeping.
2452 */
2453 protected void onInteractionEnd() {}
2454
2455 /**
2456 * Called when the user starts interacting with the launcher.
2457 * The possible interactions are:
2458 * - open all apps
2459 * - reorder an app shortcut, or a widget
2460 * - open the overview mode.
2461 * This is a good time to stop doing things that only make sense
2462 * when the user is on the homescreen and not doing housekeeping.
2463 */
2464 protected void onInteractionBegin() {}
2465
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002466 void startApplicationDetailsActivity(ComponentName componentName) {
2467 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002468 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2469 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002470 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002471 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002472 }
2473
Michael Jurka1e2f4652013-07-08 18:03:46 -07002474 // returns true if the activity was started
2475 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002476 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002477 // System applications cannot be installed. For now, show a toast explaining that.
2478 // We may give them the option of disabling apps this way.
2479 int messageId = R.string.uninstall_system_app_text;
2480 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002481 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002482 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002483 String packageName = componentName.getPackageName();
2484 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002485 Intent intent = new Intent(
2486 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002487 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2488 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002489 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002490 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002491 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002492 }
2493
Michael Jurka86a720e2012-05-09 11:23:23 -07002494 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002498 // Only launch using the new animation if the shortcut has not opted out (this is a
2499 // private contract between launcher and may be ignored in the future).
2500 boolean useLaunchAnimation = (v != null) &&
2501 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2502 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002503 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2504 v.getMeasuredWidth(), v.getMeasuredHeight());
2505
2506 startActivity(intent, opts.toBundle());
2507 } else {
2508 startActivity(intent);
2509 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002510 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } catch (SecurityException e) {
2512 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002513 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002515 "or use the exported attribute for this activity. "
2516 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002518 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002520
Michael Jurka86a720e2012-05-09 11:23:23 -07002521 boolean startActivitySafely(View v, Intent intent, Object tag) {
2522 boolean success = false;
2523 try {
2524 success = startActivity(v, intent, tag);
2525 } catch (ActivityNotFoundException e) {
2526 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2527 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2528 }
2529 return success;
2530 }
2531
Adam Cohena9cf38f2011-05-02 15:36:58 -07002532 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002533 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002534 Folder openFolder = mWorkspace.getFolderForTag(info);
2535
2536 // If the folder info reports that the associated folder is open, then verify that
2537 // it is actually opened. There have been a few instances where this gets out of sync.
2538 if (info.opened && openFolder == null) {
2539 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002540 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002541 info.opened = false;
2542 }
2543
Adam Cohenfb91f302012-06-11 15:45:18 -07002544 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 // Close any open folder
2546 closeFolder();
2547 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002548 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 } else {
2550 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 int folderScreen;
2552 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002553 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 // .. and close it
2555 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002556 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 // Close any folder open on the current screen
2558 closeFolder();
2559 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002560 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 }
2562 }
2563 }
2564 }
2565
Adam Cohen268c4752012-06-06 17:47:33 -07002566 /**
2567 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2568 * in the DragLayer in the exact absolute location of the original FolderIcon.
2569 */
2570 private void copyFolderIconToImage(FolderIcon fi) {
2571 final int width = fi.getMeasuredWidth();
2572 final int height = fi.getMeasuredHeight();
2573
2574 // Lazy load ImageView, Bitmap and Canvas
2575 if (mFolderIconImageView == null) {
2576 mFolderIconImageView = new ImageView(this);
2577 }
2578 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2579 mFolderIconBitmap.getHeight() != height) {
2580 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2581 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2582 }
2583
2584 DragLayer.LayoutParams lp;
2585 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2586 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2587 } else {
2588 lp = new DragLayer.LayoutParams(width, height);
2589 }
2590
Adam Cohen307fe232012-08-16 17:55:58 -07002591 // The layout from which the folder is being opened may be scaled, adjust the starting
2592 // view size by this scale factor.
2593 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002594 lp.customPosition = true;
2595 lp.x = mRectForFolderAnimation.left;
2596 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002597 lp.width = (int) (scale * width);
2598 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002599
2600 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2601 fi.draw(mFolderIconCanvas);
2602 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002603 if (fi.getFolder() != null) {
2604 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2605 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002606 }
Adam Cohen268c4752012-06-06 17:47:33 -07002607 // Just in case this image view is still in the drag layer from a previous animation,
2608 // we remove it and re-add it.
2609 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2610 mDragLayer.removeView(mFolderIconImageView);
2611 }
2612 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002613 if (fi.getFolder() != null) {
2614 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002615 }
Adam Cohen268c4752012-06-06 17:47:33 -07002616 }
2617
Adam Cohen2801caf2011-05-13 20:57:39 -07002618 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002619 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002620 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2621 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2622 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2623
Adam Cohenc51934b2011-07-26 21:07:43 -07002624 FolderInfo info = (FolderInfo) fi.getTag();
2625 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2626 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002627 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2628 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002629 }
2630
Adam Cohen268c4752012-06-06 17:47:33 -07002631 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2632 copyFolderIconToImage(fi);
2633 fi.setVisibility(View.INVISIBLE);
2634
Michael Jurka2ecf9952012-06-18 12:52:28 -07002635 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002636 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002637 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2638 oa.start();
2639 }
2640
Adam Cohen268c4752012-06-06 17:47:33 -07002641 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002642 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002643 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2644 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2645 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2646
Adam Cohen268c4752012-06-06 17:47:33 -07002647 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002648
Adam Cohen268c4752012-06-06 17:47:33 -07002649 // We remove and re-draw the FolderIcon in-case it has changed
2650 mDragLayer.removeView(mFolderIconImageView);
2651 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002652 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002653 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002654 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002655 oa.addListener(new AnimatorListenerAdapter() {
2656 @Override
2657 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002658 if (cl != null) {
2659 cl.clearFolderLeaveBehind();
2660 // Remove the ImageView copy of the FolderIcon and make the original visible.
2661 mDragLayer.removeView(mFolderIconImageView);
2662 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002663 }
2664 }
2665 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002666 oa.start();
2667 }
2668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002670 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 * is animated relative to the specified View. If the View is null, no animation
2672 * is played.
2673 *
2674 * @param folderInfo The FolderInfo describing the folder to open.
2675 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002676 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002677 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002678 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679
Adam Cohena9cf38f2011-05-02 15:36:58 -07002680 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681
Adam Cohen4554ee12011-08-03 16:13:21 -07002682 // Just verify that the folder hasn't already been added to the DragLayer.
2683 // There was a one-off crash where the folder had a parent already.
2684 if (folder.getParent() == null) {
2685 mDragLayer.addView(folder);
2686 mDragController.addDropTarget((DropTarget) folder);
2687 } else {
2688 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2689 folder.getParent() + ").");
2690 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002691 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002692 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002693
2694 // Notify the accessibility manager that this folder "window" has appeared and occluded
2695 // the workspace items
2696 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2697 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002698 }
2699
2700 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002701 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002702 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002703 if (folder.isEditingName()) {
2704 folder.dismissEditingName();
2705 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002706 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002707
2708 // Dismiss the folder cling
2709 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002710 }
2711 }
2712
2713 void closeFolder(Folder folder) {
2714 folder.getInfo().opened = false;
2715
2716 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2717 if (parent != null) {
2718 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2719 shrinkAndFadeInFolderIcon(fi);
2720 }
2721 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002722
2723 // Notify the accessibility manager that this folder "window" has disappeard and no
2724 // longer occludeds the workspace items
2725 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 }
2727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002728 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002729 if (!isDraggingEnabled()) return false;
2730 if (isWorkspaceLocked()) return false;
2731 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732
Adam Cohen1697b792013-09-17 19:08:21 -07002733 if (v instanceof Workspace) {
2734 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002735 if (mWorkspace.enterOverviewMode()) {
2736 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2737 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2738 return true;
2739 } else {
2740 return false;
2741 }
Adam Cohen1697b792013-09-17 19:08:21 -07002742 }
Adam Cohen1697b792013-09-17 19:08:21 -07002743 }
2744
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002746 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 }
2748
Michael Jurka0280c3b2010-09-17 15:00:07 -07002749 resetAddInfo();
2750 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002752 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 return true;
2754 }
2755
Winson Chung3d503fb2011-07-13 17:25:49 -07002756 // The hotseat touch handling does not go through Workspace, and we always allow long press
2757 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002758 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002759 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2760 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002761 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002762 // User long pressed on empty space
2763 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2764 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2765 // Disabling reordering until we sort out some issues.
2766 if (mWorkspace.isInOverviewMode()) {
2767 mWorkspace.startReordering(v);
2768 } else {
2769 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002772 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002774 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 }
2776 }
2777 }
2778 return true;
2779 }
2780
Winson Chung3d503fb2011-07-13 17:25:49 -07002781 boolean isHotseatLayout(View layout) {
2782 return mHotseat != null && layout != null &&
2783 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2784 }
2785 Hotseat getHotseat() {
2786 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002787 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002788 View getOverviewPanel() {
2789 return mOverviewPanel;
2790 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002791 SearchDropTargetBar getSearchBar() {
2792 return mSearchDropTargetBar;
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
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002810 Workspace getWorkspace() {
2811 return mWorkspace;
2812 }
2813
Daniel Sandler843e8602010-06-07 14:59:01 -04002814 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002815 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002816 }
2817
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002818 /**
2819 * Helper method for the cameraZoomIn/cameraZoomOut animations
2820 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002821 * @param scaleFactor The scale factor used for the zoom
2822 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002823 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002825 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002826 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002827
Craig Mautner360310b2012-10-26 15:13:08 -07002828 private void setWorkspaceBackground(boolean workspace) {
2829 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002830 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002831 }
2832
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002833 void updateWallpaperVisibility(boolean visible) {
2834 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2835 int curflags = getWindow().getAttributes().flags
2836 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2837 if (wpflags != curflags) {
2838 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2839 }
Craig Mautner360310b2012-10-26 15:13:08 -07002840 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002841 }
2842
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002843 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2844 if (v instanceof LauncherTransitionable) {
2845 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2846 }
2847 }
2848
Michael Jurkabed61d22012-02-14 22:51:29 -08002849 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2850 if (v instanceof LauncherTransitionable) {
2851 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2852 }
Winson Chung70442722012-02-10 15:43:22 -08002853
2854 // Update the workspace transition step as well
2855 dispatchOnLauncherTransitionStep(v, 0f);
2856 }
2857
2858 private void dispatchOnLauncherTransitionStep(View v, float t) {
2859 if (v instanceof LauncherTransitionable) {
2860 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2861 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002862 }
2863
2864 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2865 if (v instanceof LauncherTransitionable) {
2866 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2867 }
Winson Chung70442722012-02-10 15:43:22 -08002868
2869 // Update the workspace transition step as well
2870 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002871 }
2872
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002873 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002874 * Things to test when changing the following seven functions.
2875 * - Home from workspace
2876 * - from center screen
2877 * - from other screens
2878 * - Home from all apps
2879 * - from center screen
2880 * - from other screens
2881 * - Back from all apps
2882 * - from center screen
2883 * - from other screens
2884 * - Launch app from workspace and quit
2885 * - with back
2886 * - with home
2887 * - Launch app from all apps and quit
2888 * - with back
2889 * - with home
2890 * - Go to a screen that's not the default, then all
2891 * apps, and launch and app, and go back
2892 * - with back
2893 * -with home
2894 * - On workspace, long press power and go back
2895 * - with back
2896 * - with home
2897 * - On all apps, long press power and go back
2898 * - with back
2899 * - with home
2900 * - On workspace, power off
2901 * - On all apps, power off
2902 * - Launch an app and turn off the screen while in that app
2903 * - Go back with home key
2904 * - Go back with back key TODO: make this not go to workspace
2905 * - From all apps
2906 * - From workspace
2907 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2908 * - From all apps
2909 * - From the center workspace
2910 * - From another workspace
2911 */
2912
2913 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002914 * Zoom the camera out from the workspace to reveal 'toView'.
2915 * Assumes that the view to show is anchored at either the very top or very bottom
2916 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002917 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002918 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002919 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2920 showAppsCustomizeHelper(animated, springLoaded, contentType);
2921 }
2922 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2923 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002924 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002925 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002926 mStateAnimation.cancel();
2927 mStateAnimation = null;
2928 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002929 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002930
Winson Chungf0ea4d32011-06-06 14:27:16 -07002931 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2932 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2933 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002934 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002935 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002936 final int startDelay =
2937 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002938
Michael Jurkab3e22d92011-10-31 15:58:33 -07002939 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002940
Michael Jurkad74c9842011-07-10 12:44:21 -07002941 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002942 Animator workspaceAnim =
2943 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002944 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002945 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2946 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002947 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2948 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002949
2950 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002951 toView.setScaleX(scale);
2952 toView.setScaleY(scale);
2953 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2954 scaleAnim.
2955 scaleX(1f).scaleY(1f).
2956 setDuration(duration).
2957 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002958
Winson Chungf0ea4d32011-06-06 14:27:16 -07002959 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002960 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002961 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002962 .ofFloat(toView, "alpha", 0f, 1f)
2963 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002964 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002965 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2966 @Override
2967 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002968 if (animation == null) {
2969 throw new RuntimeException("animation is null");
2970 }
Winson Chung70442722012-02-10 15:43:22 -08002971 float t = (Float) animation.getAnimatedValue();
2972 dispatchOnLauncherTransitionStep(fromView, t);
2973 dispatchOnLauncherTransitionStep(toView, t);
2974 }
2975 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002976
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002977 // toView should appear right at the end of the workspace shrink
2978 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002979 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002980 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002981 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002982
2983 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002984 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002985 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002986 // Prepare the position
2987 toView.setTranslationX(0.0f);
2988 toView.setTranslationY(0.0f);
2989 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002990 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002991 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002992 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002993 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002994 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2995 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002996
Winson Chungc7d2b602012-05-16 17:02:20 -07002997 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002998 if (mSearchDropTargetBar != null) {
2999 mSearchDropTargetBar.hideSearchBar(false);
3000 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003001 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003002 });
3003
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003004 if (workspaceAnim != null) {
3005 mStateAnimation.play(workspaceAnim);
3006 }
Michael Jurka1899a362011-11-03 13:50:45 -07003007
3008 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003009
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003010 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3011 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003012
3013 // If any of the objects being animated haven't been measured/laid out
3014 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003015 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003016 (mWorkspace.getMeasuredWidth() == 0) ||
3017 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003018 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003019 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003020
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003021 final AnimatorSet stateAnimation = mStateAnimation;
3022 final Runnable startAnimRunnable = new Runnable() {
3023 public void run() {
3024 // Check that mStateAnimation hasn't changed while
3025 // we waited for a layout/draw pass
3026 if (mStateAnimation != stateAnimation)
3027 return;
3028 setPivotsForZoom(toView, scale);
3029 dispatchOnLauncherTransitionStart(fromView, animated, false);
3030 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003031 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003032 }
3033 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003034 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003035 final ViewTreeObserver observer = toView.getViewTreeObserver();
3036 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3037 public void onGlobalLayout() {
3038 startAnimRunnable.run();
3039 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3040 }
3041 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003042 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003043 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003044 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003045 } else {
3046 toView.setTranslationX(0.0f);
3047 toView.setTranslationY(0.0f);
3048 toView.setScaleX(1.0f);
3049 toView.setScaleY(1.0f);
3050 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003051 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003052
Daniel Sandlere4f98912013-06-25 15:13:26 -04003053 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003054 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003055 if (mSearchDropTargetBar != null) {
3056 mSearchDropTargetBar.hideSearchBar(false);
3057 }
Michael Jurkaabded662011-03-04 12:06:57 -08003058 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003059 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003060 dispatchOnLauncherTransitionStart(fromView, animated, false);
3061 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003062 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003063 dispatchOnLauncherTransitionStart(toView, animated, false);
3064 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003065 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003066 }
3067
3068 /**
3069 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003070 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003071 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003072 */
Adam Cohened307df2013-10-02 09:37:31 -07003073 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003074 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003075
Michael Jurkab3e22d92011-10-31 15:58:33 -07003076 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003077 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003078 mStateAnimation.cancel();
3079 mStateAnimation = null;
3080 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003081 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003082
Winson Chungf0ea4d32011-06-06 14:27:16 -07003083 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003084 final int fadeOutDuration =
3085 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003086 final float scaleFactor = (float)
3087 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3088 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003089 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003090 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003091 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003092 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3093 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003094 toState, animated, stagger, -1);
3095 } else if (toState == Workspace.State.SPRING_LOADED ||
3096 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003097 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003098 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003099 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003100
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003102 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003103 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003104 final LauncherViewPropertyAnimator scaleAnim =
3105 new LauncherViewPropertyAnimator(fromView);
3106 scaleAnim.
3107 scaleX(scaleFactor).scaleY(scaleFactor).
3108 setDuration(duration).
3109 setInterpolator(new Workspace.ZoomInInterpolator());
3110
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003111 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003112 .ofFloat(fromView, "alpha", 1f, 0f)
3113 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003114 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003115 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3116 @Override
3117 public void onAnimationUpdate(ValueAnimator animation) {
3118 float t = 1f - (Float) animation.getAnimatedValue();
3119 dispatchOnLauncherTransitionStep(fromView, t);
3120 dispatchOnLauncherTransitionStep(toView, t);
3121 }
3122 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003123
Michael Jurka2ecf9952012-06-18 12:52:28 -07003124 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003125
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003126 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3127 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003128 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003129
3130 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003131 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003132 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003133 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003134 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3135 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003136 if (onCompleteRunnable != null) {
3137 onCompleteRunnable.run();
3138 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003139 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003140 }
3141 });
3142
Winson Chungf0ea4d32011-06-06 14:27:16 -07003143 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003144 if (workspaceAnim != null) {
3145 mStateAnimation.play(workspaceAnim);
3146 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003147 dispatchOnLauncherTransitionStart(fromView, animated, true);
3148 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003149 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003150 } else {
3151 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003152 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003153 dispatchOnLauncherTransitionStart(fromView, animated, true);
3154 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003155 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003156 dispatchOnLauncherTransitionStart(toView, animated, true);
3157 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003158 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003159 }
3160
Michael Jurkae326f182011-11-21 14:05:46 -08003161 @Override
3162 public void onTrimMemory(int level) {
3163 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003164 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003165 mAppsCustomizeTabHost.onTrimMemory();
3166 }
3167 }
3168
Adam Cohened307df2013-10-02 09:37:31 -07003169 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003170 showWorkspace(animated, null);
3171 }
3172
Adam Cohened307df2013-10-02 09:37:31 -07003173 protected void showWorkspace() {
3174 showWorkspace(true);
3175 }
3176
Adam Cohened66b2b2012-01-23 17:28:51 -08003177 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003178 if (mWorkspace.isInOverviewMode()) {
3179 mWorkspace.exitOverviewMode(animated);
3180 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003181 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003182 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003183 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003184 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003185
Winson Chungc7d2b602012-05-16 17:02:20 -07003186 // Show the search bar (only animate if we were showing the drop target bar in spring
3187 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003188 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003189 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003190 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003191
Michael Jurkab3e22d92011-10-31 15:58:33 -07003192 // Set focus to the AppsCustomize button
3193 if (mAllAppsButton != null) {
3194 mAllAppsButton.requestFocus();
3195 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003196 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003197
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003198 // Change the state *after* we've called all the transition code
3199 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003200
Winson Chung5fb63472011-02-02 17:03:37 -08003201 // Resume the auto-advance of widgets
3202 mUserPresent = true;
3203 updateRunning();
3204
alanv1d4fde62012-10-17 13:15:47 -07003205 // Send an accessibility event to announce the context change
3206 getWindow().getDecorView()
3207 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003208
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003209 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003210 }
3211
Adam Cohened307df2013-10-02 09:37:31 -07003212 void showOverviewMode(boolean animated) {
3213 mWorkspace.setVisibility(View.VISIBLE);
3214 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3215 mState = State.WORKSPACE;
3216 onWorkspaceShown(animated);
3217 }
3218
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003219 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003220 }
3221
Winson Chung82963d52013-10-09 11:20:57 -07003222 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3223 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003224 if (mState != State.WORKSPACE) return;
3225
Winson Chung82963d52013-10-09 11:20:57 -07003226 if (resetPageToZero) {
3227 mAppsCustomizeTabHost.reset();
3228 }
Winson Chungc58497e2013-09-03 17:48:37 -07003229 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003230 mAppsCustomizeTabHost.requestFocus();
3231
Michael Jurkab3e22d92011-10-31 15:58:33 -07003232 // Change the state *after* we've called all the transition code
3233 mState = State.APPS_CUSTOMIZE;
3234
3235 // Pause the auto-advance of widgets until we are out of AllApps
3236 mUserPresent = false;
3237 updateRunning();
3238 closeFolder();
3239
3240 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003241 getWindow().getDecorView()
3242 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003243 }
3244
Adam Cohen7777d962011-08-18 18:58:38 -07003245 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003246 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003247 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003248 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003249 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003250 }
Adam Cohen7777d962011-08-18 18:58:38 -07003251
Adam Cohenad4e15c2013-10-17 16:21:35 -07003252 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003253 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003254 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3255
Winson Chunge7a03942011-08-05 15:05:12 -07003256 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003257 @Override
3258 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003259 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003260 // Before we show workspace, hide all apps again because
3261 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3262 // clean up our state transition functions
3263 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003264 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003265 } else {
3266 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003267 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003268 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003269 }, delay);
3270
Winson Chung557d6ed2011-07-08 15:34:52 -07003271 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272
Michael Jurkad3ef3062010-11-23 16:23:58 -08003273 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003274 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275 final boolean animated = true;
3276 final boolean springLoaded = true;
3277 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003278 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003279 }
3280 // Otherwise, we are not in spring loaded mode, so don't do anything.
3281 }
3282
Michael Jurkab3e22d92011-10-31 15:58:33 -07003283 void lockAllApps() {
3284 // TODO
3285 }
3286
3287 void unlockAllApps() {
3288 // TODO
3289 }
3290
Winson Chungf0ea4d32011-06-06 14:27:16 -07003291 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003292 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003293 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003294 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003295 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003296 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003297 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003298 int duration = 0;
3299 if (mSearchDropTargetBar != null) {
3300 duration = mSearchDropTargetBar.getTransitionInDuration();
3301 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003302 mHotseat.animate().alpha(1f).setDuration(duration);
3303 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003304 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003305 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003306 }
3307 }
3308 }
3309
3310 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003311 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003312 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003313 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003314 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003315 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003316 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003317 int duration = 0;
3318 if (mSearchDropTargetBar != null) {
3319 duration = mSearchDropTargetBar.getTransitionOutDuration();
3320 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003321 mHotseat.animate().alpha(0f).setDuration(duration);
3322 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003323 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003324 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003325 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003326 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003327 }
3328
Patrick Dubroy5f445422011-02-18 14:35:21 -08003329 /**
3330 * Add an item from all apps or customize onto the given workspace screen.
3331 * If layout is null, add to the current screen.
3332 */
3333 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003334 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003335 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003336 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003337 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003338
Winson Chungdff8ebb2011-09-08 17:25:31 -07003339 /** Maps the current orientation to an index for referencing orientation correct global icons */
3340 private int getCurrentOrientationIndexForGlobalIcons() {
3341 // default - 0, landscape - 1
3342 switch (getResources().getConfiguration().orientation) {
3343 case Configuration.ORIENTATION_LANDSCAPE:
3344 return 1;
3345 default:
3346 return 0;
3347 }
3348 }
3349
Michael Jurkaae65ee32012-04-30 11:45:54 -07003350 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003351 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003352 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003353 // Look for the toolbar icon specified in the activity meta-data
3354 Bundle metaData = packageManager.getActivityInfo(
3355 activityName, PackageManager.GET_META_DATA).metaData;
3356 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003357 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003358 if (iconResId != 0) {
3359 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003360 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003361 }
3362 }
3363 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003364 // This can happen if the activity defines an invalid drawable
3365 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3366 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003367 } catch (Resources.NotFoundException nfe) {
3368 // This can happen if the activity defines an invalid drawable
3369 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3370 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003371 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003372 return null;
3373 }
3374
3375 // if successful in getting icon, return it; otherwise, set button to use default drawable
3376 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003377 int buttonId, ComponentName activityName, int fallbackDrawableId,
3378 String toolbarResourceName) {
3379 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003380 Resources r = getResources();
3381 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3382 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003383
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003384 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003385 // If we were unable to find the icon via the meta-data, use a generic one
3386 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003387 toolbarIcon = r.getDrawable(fallbackDrawableId);
3388 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003389 if (button != null) {
3390 button.setCompoundDrawables(toolbarIcon, null, null, null);
3391 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003392 return null;
3393 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003394 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003395 if (button != null) {
3396 button.setCompoundDrawables(toolbarIcon, null, null, null);
3397 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003398 return toolbarIcon.getConstantState();
3399 }
3400 }
3401
3402 // if successful in getting icon, return it; otherwise, set button to use default drawable
3403 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003404 int buttonId, ComponentName activityName, int fallbackDrawableId,
3405 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003406 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003407 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003408
Michael Jurka19e0fc52011-07-22 18:00:21 -07003409 if (button != null) {
3410 // If we were unable to find the icon via the meta-data, use a
3411 // generic one
3412 if (toolbarIcon == null) {
3413 button.setImageResource(fallbackDrawableId);
3414 } else {
3415 button.setImageDrawable(toolbarIcon);
3416 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003417 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003418
3419 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3420
Michael Jurka0423dcf2010-10-05 14:56:18 -07003421 }
3422
Winson Chung1b884092012-06-08 17:13:02 -07003423 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003424 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003425 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003426 }
3427
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003428 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003429 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003430 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003431 }
3432
Winson Chungbb185bd2011-11-21 12:31:42 -08003433 private void invalidatePressedFocusedStates(View container, View button) {
3434 if (container instanceof HolographicLinearLayout) {
3435 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3436 layout.invalidatePressedFocusedStates();
3437 } else if (button instanceof HolographicImageView) {
3438 HolographicImageView view = (HolographicImageView) button;
3439 view.invalidatePressedFocusedStates();
3440 }
3441 }
3442
Cristina Stancu476493b2013-08-07 17:20:14 +01003443 public View getQsbBar() {
3444 if (mQsbBar == null) {
Winson Chung54e65132013-09-27 11:44:58 -07003445 mQsbBar = mInflater.inflate(R.layout.search_bar, mSearchDropTargetBar, false);
3446 mSearchDropTargetBar.addView(mQsbBar);
Cristina Stancu476493b2013-08-07 17:20:14 +01003447 }
3448 return mQsbBar;
3449 }
3450
3451 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003452 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003453 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003454 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003455 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003456
Winson Chung1cad91e2011-05-25 17:41:01 -07003457 final SearchManager searchManager =
3458 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3459 ComponentName activityName = searchManager.getGlobalSearchActivity();
3460 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003461 int coi = getCurrentOrientationIndexForGlobalIcons();
3462 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003463 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3464 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3465 if (sGlobalSearchIcon[coi] == null) {
3466 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3467 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3468 TOOLBAR_ICON_METADATA_NAME);
3469 }
3470
Winson Chungc51db6a2011-10-05 11:44:49 -07003471 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3472 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003473 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003474 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003475 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003476 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003477 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3478 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003479 if (searchButton != null) searchButton.setVisibility(View.GONE);
3480 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003481 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003482 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003483 }
3484 }
3485
Cristina Stancu476493b2013-08-07 17:20:14 +01003486 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003487 final View searchButtonContainer = findViewById(R.id.search_button_container);
3488 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003489 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003490 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003491 }
3492
Cristina Stancu476493b2013-08-07 17:20:14 +01003493 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003494 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003495 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003496
Winson Chungc51db6a2011-10-05 11:44:49 -07003497 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003498 final SearchManager searchManager =
3499 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3500 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3501
3502 ComponentName activityName = null;
3503 if (globalSearchActivity != null) {
3504 // Check if the global search activity handles voice search
3505 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3506 intent.setPackage(globalSearchActivity.getPackageName());
3507 activityName = intent.resolveActivity(getPackageManager());
3508 }
3509
3510 if (activityName == null) {
3511 // Fallback: check if an activity other than the global search activity
3512 // resolves this
3513 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3514 activityName = intent.resolveActivity(getPackageManager());
3515 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003516 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003517 int coi = getCurrentOrientationIndexForGlobalIcons();
3518 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003519 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3520 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3521 if (sVoiceSearchIcon[coi] == null) {
3522 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3523 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3524 TOOLBAR_ICON_METADATA_NAME);
3525 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003526 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003527 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003528 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003529 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003530 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003531 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003532 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003533 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003534 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003535 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003536 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003537 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003538
Cristina Stancu476493b2013-08-07 17:20:14 +01003539 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003540 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3541 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003542 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003543 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003544 }
3545
Winson Chung5841efa2013-09-30 18:06:44 -07003546 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003547 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3548 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003549 boolean visible = !forceDisableVoiceButtonProxy &&
3550 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003551 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003552 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003553 }
3554 }
Winson Chung5841efa2013-09-30 18:06:44 -07003555
3556 /**
3557 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3558 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3559 */
3560 public void disableVoiceButtonProxy(boolean disabled) {
3561 updateVoiceButtonProxyVisible(disabled);
3562 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003563 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003564 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003565 */
3566 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003567 if (!DISABLE_MARKET_BUTTON) {
3568 final View marketButton = findViewById(R.id.market_button);
3569 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3570 // Find the app market activity by resolving an intent.
3571 // (If multiple app markets are installed, it will return the ResolverActivity.)
3572 ComponentName activityName = intent.resolveActivity(getPackageManager());
3573 if (activityName != null) {
3574 int coi = getCurrentOrientationIndexForGlobalIcons();
3575 mAppMarketIntent = intent;
3576 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3577 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3578 TOOLBAR_ICON_METADATA_NAME);
3579 marketButton.setVisibility(View.VISIBLE);
3580 } else {
3581 // We should hide and disable the view so that we don't try and restore the visibility
3582 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3583 marketButton.setVisibility(View.GONE);
3584 marketButton.setEnabled(false);
3585 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003586 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003587 }
3588
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003589 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003590 if (!DISABLE_MARKET_BUTTON) {
3591 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3592 Resources r = getResources();
3593 Drawable marketIconDrawable = d.newDrawable(r);
3594 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3595 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3596 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003597
Winson Chungd64a6662013-09-30 11:06:59 -07003598 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3599 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003600 }
3601
Michael Jurkad7c28052012-04-27 15:43:36 -07003602 @Override
3603 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003604 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003605 final List<CharSequence> text = event.getText();
3606 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003607 // Populate event with a fake title based on the current state.
3608 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003609 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003610 } else {
3611 text.add(getString(R.string.all_apps_home_button_label));
3612 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003613 return result;
3614 }
3615
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003616 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003617 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003618 */
3619 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3620 @Override
3621 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003622 closeSystemDialogs();
3623 }
3624 }
3625
3626 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003627 * Receives notifications whenever the appwidgets are reset.
3628 */
3629 private class AppWidgetResetObserver extends ContentObserver {
3630 public AppWidgetResetObserver() {
3631 super(new Handler());
3632 }
3633
3634 @Override
3635 public void onChange(boolean selfChange) {
3636 onAppWidgetReset();
3637 }
3638 }
3639
3640 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 * If the activity is currently paused, signal that we need to run the passed Runnable
3642 * in onResume.
3643 *
3644 * This needs to be called from incoming places where resources might have been loaded
3645 * while we are paused. That is becaues the Configuration might be wrong
3646 * when we're not running, and if it comes back to what it was when we
3647 * were paused, we are not restarted.
3648 *
3649 * Implementation of the method from LauncherModel.Callbacks.
3650 *
3651 * @return true if we are currently paused. The caller might be able to
3652 * skip some work in that case since we will come back again.
3653 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003654 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003655 if (mPaused) {
3656 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003657 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003658 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003659 }
3660 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003661 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003662 return true;
3663 } else {
3664 return false;
3665 }
3666 }
3667
Michael Jurkac402cd92013-05-20 15:49:32 +02003668 private boolean waitUntilResume(Runnable run) {
3669 return waitUntilResume(run, false);
3670 }
3671
Michael Jurka1e2f4652013-07-08 18:03:46 -07003672 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003673 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003674 }
3675
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003677 * If the activity is currently paused, signal that we need to re-run the loader
3678 * in onResume.
3679 *
3680 * This needs to be called from incoming places where resources might have been loaded
3681 * while we are paused. That is becaues the Configuration might be wrong
3682 * when we're not running, and if it comes back to what it was when we
3683 * were paused, we are not restarted.
3684 *
3685 * Implementation of the method from LauncherModel.Callbacks.
3686 *
3687 * @return true if we are currently paused. The caller might be able to
3688 * skip some work in that case since we will come back again.
3689 */
3690 public boolean setLoadOnResume() {
3691 if (mPaused) {
3692 Log.i(TAG, "setLoadOnResume");
3693 mOnResumeNeedsLoad = true;
3694 return true;
3695 } else {
3696 return false;
3697 }
3698 }
3699
3700 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003702 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003704 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003705 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003706 } else {
3707 return SCREEN_COUNT / 2;
3708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003710
Joe Onorato9c1289c2009-08-17 11:03:03 -04003711 /**
3712 * Refreshes the shortcuts shown on the workspace.
3713 *
3714 * Implementation of the method from LauncherModel.Callbacks.
3715 */
3716 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003717 // If we're starting binding all over again, clear any bind calls we'd postponed in
3718 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3719 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003720 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003721
Winson Chungd64d1762013-08-20 14:37:16 -07003722 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003723 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003724 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003725
Michael Jurka05bf6442011-11-30 20:28:53 -08003726 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003727 if (mHotseat != null) {
3728 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003729 }
3730 }
3731
Adam Cohendcd297f2013-06-18 13:13:40 -07003732 @Override
3733 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003734 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003735
Adam Cohen5084cba2013-09-03 12:01:16 -07003736 // If there are no screens, we need to have an empty screen
3737 if (orderedScreenIds.size() == 0) {
3738 mWorkspace.addExtraEmptyScreen();
3739 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003740
3741 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003742 // setCurrentPage() so ensure that all pages are added before calling this).
3743 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003744 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3745 mWorkspace.createCustomContentPage();
3746 }
Winson Chung64359a52013-07-08 17:17:08 -07003747 }
3748
3749 @Override
3750 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003751 // Log to disk
3752 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3753 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3754 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003755 int count = orderedScreenIds.size();
3756 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003757 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003758 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003759 }
3760
Adam Cohen39a06042013-07-19 14:30:12 -07003761 private boolean shouldShowWeightWatcher() {
3762 String spKey = LauncherAppState.getSharedPreferencesKey();
3763 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003764 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003765
3766 return show;
3767 }
3768
3769 private void toggleShowWeightWatcher() {
3770 String spKey = LauncherAppState.getSharedPreferencesKey();
3771 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3772 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3773
3774 show = !show;
3775
3776 SharedPreferences.Editor editor = sp.edit();
3777 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3778 editor.commit();
3779
3780 if (mWeightWatcher != null) {
3781 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3782 }
3783 }
3784
Winson Chungd64d1762013-08-20 14:37:16 -07003785 public void bindAppsAdded(final ArrayList<Long> newScreens,
3786 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003787 final ArrayList<ItemInfo> addAnimated,
3788 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003789 Runnable r = new Runnable() {
3790 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003791 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003792 }
3793 };
3794 if (waitUntilResume(r)) {
3795 return;
3796 }
3797
Winson Chungd64d1762013-08-20 14:37:16 -07003798 // Add the new screens
3799 bindAddScreens(newScreens);
3800
3801 // We add the items without animation on non-visible pages, and with
3802 // animations on the new page (which we will try and snap to).
3803 if (!addNotAnimated.isEmpty()) {
3804 bindItems(addNotAnimated, 0,
3805 addNotAnimated.size(), false);
3806 }
3807 if (!addAnimated.isEmpty()) {
3808 bindItems(addAnimated, 0,
3809 addAnimated.size(), true);
3810 }
Winson Chungc58497e2013-09-03 17:48:37 -07003811
Winson Chung87412982013-10-03 18:34:14 -07003812 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003813 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003814
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003815 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003816 addedApps != null && mAppsCustomizeContent != null) {
3817 mAppsCustomizeContent.addApps(addedApps);
3818 }
Winson Chungd64d1762013-08-20 14:37:16 -07003819 }
3820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003821 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 * Bind the items start-end from the list.
3823 *
3824 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003825 */
Winson Chung64359a52013-07-08 17:17:08 -07003826 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3827 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003828 Runnable r = new Runnable() {
3829 public void run() {
3830 bindItems(shortcuts, start, end, forceAnimateIcons);
3831 }
3832 };
3833 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003834 return;
3835 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003836
Winson Chungf0c6ae02012-03-21 16:10:31 -07003837 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003838 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3839 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003840 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003841 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003842 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003843 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003845
3846 // Short circuit if we are loading dock items for a configuration which has no dock
3847 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3848 mHotseat == null) {
3849 continue;
3850 }
3851
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 switch (item.itemType) {
3853 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3854 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003855 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003856 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003857
Winson Chung64359a52013-07-08 17:17:08 -07003858 /*
3859 * TODO: FIX collision case
3860 */
Winson Chungce376632013-07-11 16:12:41 -07003861 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3862 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3863 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3864 throw new RuntimeException("OCCUPIED");
3865 }
Winson Chung64359a52013-07-08 17:17:08 -07003866 }
3867
Adam Cohendcd297f2013-06-18 13:13:40 -07003868 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3869 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003870 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003871 // Animate all the applications up now
3872 shortcut.setAlpha(0f);
3873 shortcut.setScaleX(0f);
3874 shortcut.setScaleY(0f);
3875 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003876 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003877 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003879 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003880 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003881 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003882 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003883 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3884 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003885 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003886 default:
3887 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003889 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003890
Winson Chung997a9232013-07-24 15:33:46 -07003891 if (animateIcons) {
3892 // Animate to the correct page
3893 if (newShortcutsScreenId > -1) {
3894 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003895 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003896 final Runnable startBounceAnimRunnable = new Runnable() {
3897 public void run() {
3898 anim.playTogether(bounceAnims);
3899 anim.start();
3900 }
3901 };
Winson Chung997a9232013-07-24 15:33:46 -07003902 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003903 // We post the animation slightly delayed to prevent slowdowns
3904 // when we are loading right after we return to launcher.
3905 mWorkspace.postDelayed(new Runnable() {
3906 public void run() {
3907 mWorkspace.snapToPage(newScreenIndex);
Winson Chungb2323832013-10-03 15:22:09 -07003908 mWorkspace.postDelayed(startBounceAnimRunnable,
3909 NEW_APPS_ANIMATION_DELAY);
Winson Chung94d67682013-09-25 16:29:40 -07003910 }
3911 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003912 } else {
3913 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003914 }
3915 }
Winson Chung64359a52013-07-08 17:17:08 -07003916 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003918 }
3919
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 /**
3921 * Implementation of the method from LauncherModel.Callbacks.
3922 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003923 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003924 Runnable r = new Runnable() {
3925 public void run() {
3926 bindFolders(folders);
3927 }
3928 };
3929 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003930 return;
3931 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003932 sFolders.clear();
3933 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935
3936 /**
3937 * Add the views for a widget to the workspace.
3938 *
3939 * Implementation of the method from LauncherModel.Callbacks.
3940 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003941 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003942 Runnable r = new Runnable() {
3943 public void run() {
3944 bindAppWidget(item);
3945 }
3946 };
3947 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003948 return;
3949 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003950
Daniel Sandler843e8602010-06-07 14:59:01 -04003951 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3952 if (DEBUG_WIDGETS) {
3953 Log.d(TAG, "bindAppWidget: " + item);
3954 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 final Workspace workspace = mWorkspace;
3956
3957 final int appWidgetId = item.appWidgetId;
3958 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003959 if (DEBUG_WIDGETS) {
3960 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3961 }
3962
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3964
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003966 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967
Adam Cohendcd297f2013-06-18 13:13:40 -07003968 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003970 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3971
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 workspace.requestLayout();
3973
Daniel Sandler843e8602010-06-07 14:59:01 -04003974 if (DEBUG_WIDGETS) {
3975 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3976 + (SystemClock.uptimeMillis()-start) + "ms");
3977 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978 }
3979
Adam Cohen1462de32012-07-24 22:34:36 -07003980 public void onPageBoundSynchronously(int page) {
3981 mSynchronouslyBoundPages.add(page);
3982 }
3983
Joe Onorato9c1289c2009-08-17 11:03:03 -04003984 /**
3985 * Callback saying that there aren't any more items to bind.
3986 *
3987 * Implementation of the method from LauncherModel.Callbacks.
3988 */
Adam Cohene25af792013-06-06 23:08:25 -07003989 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003990 Runnable r = new Runnable() {
3991 public void run() {
3992 finishBindingItems(upgradePath);
3993 }
3994 };
3995 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003996 return;
3997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 if (mSavedState != null) {
3999 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004000 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004002 mSavedState = null;
4003 }
4004
Adam Cohen1462de32012-07-24 22:34:36 -07004005 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004007 // If we received the result of any pending adds while the loader was running (e.g. the
4008 // widget configuration forced an orientation change), process them now.
4009 for (int i = 0; i < sPendingAddList.size(); i++) {
4010 completeAdd(sPendingAddList.get(i));
4011 }
4012 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004013
Winson Chungc51db6a2011-10-05 11:44:49 -07004014 // Update the market app icon as necessary (the other icons will be managed in response to
4015 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004016 if (!DISABLE_MARKET_BUTTON) {
4017 updateAppMarketIcon();
4018 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004019
Winson Chungf0c6ae02012-03-21 16:10:31 -07004020 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004021 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004022 mWorkspace.getUniqueComponents(true, null);
4023 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004024 }
Adam Cohen99894d92013-06-14 11:22:59 -07004025
Adam Cohen66a01fd2013-06-11 12:48:00 -07004026 mWorkspace.post(new Runnable() {
4027 @Override
4028 public void run() {
4029 onFinishBindingItems();
4030 }
4031 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004032 }
4033
Winson Chunga0b7e862013-09-05 16:03:15 -07004034 public boolean isAllAppsButtonRank(int rank) {
4035 if (mHotseat != null) {
4036 return mHotseat.isAllAppsButtonRank(rank);
4037 }
4038 return false;
4039 }
4040
Winson Chunga2413752012-04-03 14:22:34 -07004041 private boolean canRunNewAppsAnimation() {
4042 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4043 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4044 }
4045
Winson Chungc9168342013-06-26 14:54:55 -07004046 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4047 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4048 PropertyValuesHolder.ofFloat("alpha", 1f),
4049 PropertyValuesHolder.ofFloat("scaleX", 1f),
4050 PropertyValuesHolder.ofFloat("scaleY", 1f));
4051 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4052 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4053 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4054 return bounceAnim;
4055 }
4056
Adam Cohen27772732013-11-11 14:00:56 +00004057 public boolean useVerticalBarLayout() {
4058 return LauncherAppState.getInstance().getDynamicGrid().
4059 getDeviceProfile().isVerticalBarLayout();
4060 }
4061
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004062 protected Rect getSearchBarBounds() {
4063 return LauncherAppState.getInstance().getDynamicGrid().
4064 getDeviceProfile().getSearchBarBounds();
4065 }
4066
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004067 @Override
4068 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004069 boolean searchVisible = updateGlobalSearchIcon();
4070 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004071 if (mSearchDropTargetBar != null) {
4072 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4073 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004074 }
4075
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004076 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004077 * Add the icons for all apps.
4078 *
4079 * Implementation of the method from LauncherModel.Callbacks.
4080 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004081 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004082 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004083 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4084 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4085 getHotseat().addAllAppsFolder(mIconCache, apps,
4086 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4087 }
4088 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004089 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004090 if (mAppsCustomizeContent != null) {
4091 mAppsCustomizeContent.onPackagesUpdated(
4092 LauncherModel.getSortedWidgetsAndShortcuts(this));
4093 }
Winson Chungc58497e2013-09-03 17:48:37 -07004094 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004095 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004096 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004097 mAppsCustomizeContent.onPackagesUpdated(
4098 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004099 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004100 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004101 }
4102
4103 /**
4104 * A package was updated.
4105 *
4106 * Implementation of the method from LauncherModel.Callbacks.
4107 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004108 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004109 Runnable r = new Runnable() {
4110 public void run() {
4111 bindAppsUpdated(apps);
4112 }
4113 };
4114 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004115 return;
4116 }
4117
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004118 if (mWorkspace != null) {
4119 mWorkspace.updateShortcuts(apps);
4120 }
Winson Chungc58497e2013-09-03 17:48:37 -07004121
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004122 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004123 mAppsCustomizeContent != null) {
4124 mAppsCustomizeContent.updateApps(apps);
4125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 }
4127
4128 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004129 * A package was uninstalled. We take both the super set of packageNames
4130 * in addition to specific applications to remove, the reason being that
4131 * this can be called when a package is updated as well. In that scenario,
4132 * we only remove specific components from the workspace, where as
4133 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004134 *
4135 * Implementation of the method from LauncherModel.Callbacks.
4136 */
Winson Chung83892cc2013-05-01 16:53:33 -07004137 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004138 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004139 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004140 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004141 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004142 }
Winson Chungd64d1762013-08-20 14:37:16 -07004143 };
4144 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004145 return;
4146 }
4147
Winson Chungdf95eb12013-10-16 14:57:07 -07004148 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004149 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004150 }
4151 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004152 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004153 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004154
Winson Chunga1820962011-10-03 16:31:06 -07004155 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004156 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004157
Winson Chungdf95eb12013-10-16 14:57:07 -07004158 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004159 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004160 mAppsCustomizeContent != null) {
4161 mAppsCustomizeContent.removeApps(appInfos);
4162 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004163 }
Joe Onoratobe386092009-11-17 17:32:16 -08004164
4165 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004166 * A number of packages were updated.
4167 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004168 private ArrayList<Object> mWidgetsAndShortcuts;
4169 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004170 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004171 bindPackagesUpdated(mWidgetsAndShortcuts);
4172 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004173 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004174 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004175 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4176 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4177 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004178 return;
4179 }
4180
Winson Chung64359a52013-07-08 17:17:08 -07004181 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004182 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004183 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004184 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004185 }
4186
Winson Chung400438b2011-01-16 17:53:48 -08004187 private int mapConfigurationOriActivityInfoOri(int configOri) {
4188 final Display d = getWindowManager().getDefaultDisplay();
4189 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4190 switch (d.getRotation()) {
4191 case Surface.ROTATION_0:
4192 case Surface.ROTATION_180:
4193 // We are currently in the same basic orientation as the natural orientation
4194 naturalOri = configOri;
4195 break;
4196 case Surface.ROTATION_90:
4197 case Surface.ROTATION_270:
4198 // We are currently in the other basic orientation to the natural orientation
4199 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4200 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4201 break;
4202 }
4203
4204 int[] oriMap = {
4205 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4206 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4207 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4208 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4209 };
4210 // Since the map starts at portrait, we need to offset if this device's natural orientation
4211 // is landscape.
4212 int indexOffset = 0;
4213 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4214 indexOffset = 1;
4215 }
4216 return oriMap[(d.getRotation() + indexOffset) % 4];
4217 }
Adam Cohen446e9402011-09-15 18:21:21 -07004218
Winson Chung4b919f82012-05-01 10:44:08 -07004219 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004220 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004221 getResources().getBoolean(R.bool.allow_rotation);
4222 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004223 }
Winson Chung4b919f82012-05-01 10:44:08 -07004224 public void lockScreenOrientation() {
4225 if (isRotationEnabled()) {
4226 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4227 .getConfiguration().orientation));
4228 }
4229 }
4230 public void unlockScreenOrientation(boolean immediate) {
4231 if (isRotationEnabled()) {
4232 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004233 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004234 } else {
4235 mHandler.postDelayed(new Runnable() {
4236 public void run() {
4237 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4238 }
4239 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004240 }
Winson Chung4b919f82012-05-01 10:44:08 -07004241 }
Winson Chung400438b2011-01-16 17:53:48 -08004242 }
4243
Adam Cohenb54a5982014-01-08 15:21:04 -08004244 private boolean shouldRunFirstRunActivity() {
4245 return !ActivityManager.isRunningInTestHarness();
4246 }
4247
Winson Chung82f55532011-08-09 14:14:23 -07004248 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004249 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004250 if (DISABLE_CLINGS) {
4251 return false;
4252 }
4253
Winson Chung3a6e7f32013-10-09 15:50:52 -07004254 // For now, limit only to phones
4255 LauncherAppState app = LauncherAppState.getInstance();
4256 DeviceProfile grid = app.getDynamicGrid().getDeviceProfile();
4257 if (grid.isTablet()) {
4258 return false;
4259 }
Winson Chung13dbfe12013-10-31 11:42:39 -07004260 if (grid.isLandscape) {
4261 return false;
4262 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004263
Brett Chabot2a9e2282011-08-23 15:03:13 -07004264 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004265 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004266
Casey Burkhardt0d9cbae2013-10-04 14:13:15 -07004267 // Disable clings for accessibility when explore by touch is enabled
4268 final AccessibilityManager a11yManager = (AccessibilityManager) getSystemService(
4269 ACCESSIBILITY_SERVICE);
4270 if (a11yManager.isTouchExplorationEnabled()) {
4271 return false;
4272 }
4273
Michael Jurkae233a8b2013-03-19 13:49:20 +01004274 // Restricted secondary users (child mode) will potentially have very few apps
4275 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004276// boolean supportsLimitedUsers =
4277// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4278// Account[] accounts = AccountManager.get(this).getAccounts();
4279// if (supportsLimitedUsers && accounts.length == 0) {
4280// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4281// Bundle restrictions = um.getUserRestrictions();
4282// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4283// return false;
4284// }
4285// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004286 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004287 }
Michael Jurka22143132012-10-04 17:42:38 +02004288
Winson Chungfa545132013-09-26 17:45:32 -07004289 private Cling initCling(int clingId, int scrimId, boolean animate,
Winson Chungaf40f202013-09-18 18:26:31 -07004290 boolean dimNavBarVisibilty) {
Winson Chungfa545132013-09-26 17:45:32 -07004291 Cling cling = (Cling) findViewById(clingId);
4292 View scrim = null;
4293 if (scrimId > 0) {
Sandeep Siddharthaa6125ef2013-09-26 20:30:40 -07004294 scrim = findViewById(R.id.cling_scrim);
Winson Chungfa545132013-09-26 17:45:32 -07004295 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004296 if (cling != null) {
Winson Chungfa545132013-09-26 17:45:32 -07004297 cling.init(this, scrim);
Winson Chungaf40f202013-09-18 18:26:31 -07004298 cling.show(animate, SHOW_CLING_DURATION);
4299
4300 if (dimNavBarVisibilty) {
4301 cling.setSystemUiVisibility(cling.getSystemUiVisibility() |
4302 View.SYSTEM_UI_FLAG_LOW_PROFILE);
Winson Chung7d7541e2011-09-16 20:14:36 -07004303 }
4304 }
4305 return cling;
4306 }
Michael Jurka22143132012-10-04 17:42:38 +02004307
Winson Chungaf40f202013-09-18 18:26:31 -07004308 private void dismissCling(final Cling cling, final Runnable postAnimationCb,
4309 final String flag, int duration, boolean restoreNavBarVisibilty) {
Winson Chung7819abd2012-11-29 14:29:38 -08004310 // To catch cases where siblings of top-level views are made invisible, just check whether
4311 // the cling is directly set to GONE before dismissing it.
4312 if (cling != null && cling.getVisibility() != View.GONE) {
Winson Chungaf40f202013-09-18 18:26:31 -07004313 final Runnable cleanUpClingCb = new Runnable() {
4314 public void run() {
Winson Chung82f55532011-08-09 14:14:23 -07004315 cling.cleanup();
Adam Cohenb54a5982014-01-08 15:21:04 -08004316 SharedPreferences.Editor editor = mSharedPrefs.edit();
4317 editor.putBoolean(flag, true);
4318 editor.apply();
Winson Chungaf40f202013-09-18 18:26:31 -07004319 if (postAnimationCb != null) {
4320 postAnimationCb.run();
4321 }
4322 }
4323 };
4324 if (duration <= 0) {
4325 cleanUpClingCb.run();
4326 } else {
4327 cling.hide(duration, cleanUpClingCb);
4328 }
Michael Jurka22143132012-10-04 17:42:38 +02004329 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chungaf40f202013-09-18 18:26:31 -07004330
4331 if (restoreNavBarVisibilty) {
4332 cling.setSystemUiVisibility(cling.getSystemUiVisibility() &
4333 ~View.SYSTEM_UI_FLAG_LOW_PROFILE);
4334 }
Winson Chung82f55532011-08-09 14:14:23 -07004335 }
4336 }
Michael Jurka22143132012-10-04 17:42:38 +02004337
Winson Chung9d9d74f2011-09-19 11:49:12 -07004338 private void removeCling(int id) {
4339 final View cling = findViewById(id);
4340 if (cling != null) {
4341 final ViewGroup parent = (ViewGroup) cling.getParent();
4342 parent.post(new Runnable() {
4343 @Override
4344 public void run() {
4345 parent.removeView(cling);
4346 }
4347 });
Michael Jurka22143132012-10-04 17:42:38 +02004348 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004349 }
4350 }
Michael Jurka974c3862012-05-22 22:00:31 -07004351
4352 private boolean skipCustomClingIfNoAccounts() {
4353 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4354 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4355 if (customCling) {
4356 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004357 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004358 Account[] accounts = am.getAccountsByType("com.google");
4359 return accounts.length == 0;
4360 }
4361 return false;
4362 }
4363
Winson Chung82e5c982013-10-09 12:04:50 -07004364 public void updateCustomContentHintVisibility() {
4365 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4366 String ccHintStr = getFirstRunCustomContentHint();
4367
4368 if (mWorkspace.hasCustomContent()) {
4369 // Show the custom content hint if ccHintStr is not empty
4370 if (cling != null) {
4371 setCustomContentHintVisibility(cling, ccHintStr, true, true);
4372 }
4373 } else {
4374 // Hide the custom content hint
4375 if (cling != null) {
4376 setCustomContentHintVisibility(cling, ccHintStr, false, true);
4377 }
4378 }
4379 }
4380
4381 private void setCustomContentHintVisibility(Cling cling, String ccHintStr, boolean visible,
4382 boolean animate) {
4383 final TextView ccHint = (TextView) cling.findViewById(R.id.custom_content_hint);
4384 if (ccHint != null) {
4385 if (visible && !ccHintStr.isEmpty()) {
4386 ccHint.setText(ccHintStr);
4387 ccHint.setVisibility(View.VISIBLE);
4388 if (animate) {
4389 ccHint.setAlpha(0f);
4390 ccHint.animate().alpha(1f)
4391 .setDuration(SHOW_CLING_DURATION)
4392 .start();
4393 } else {
4394 ccHint.setAlpha(1f);
4395 }
4396 } else {
4397 if (animate) {
4398 ccHint.animate().alpha(0f)
4399 .setDuration(SHOW_CLING_DURATION)
4400 .setListener(new AnimatorListenerAdapter() {
4401 @Override
4402 public void onAnimationEnd(Animator animation) {
4403 ccHint.setVisibility(View.GONE);
4404 }
4405 })
4406 .start();
4407 } else {
4408 ccHint.setAlpha(0f);
4409 ccHint.setVisibility(View.GONE);
4410 }
4411 }
4412 }
4413 }
4414
Adam Cohenb54a5982014-01-08 15:21:04 -08004415 public void showFirstRunActivity() {
4416 if (shouldRunFirstRunActivity() && hasFirstRunActivity()
4417 && !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false)) {
4418 Intent firstRunIntent = getFirstRunActivity();
4419 if (firstRunIntent != null) {
4420 startActivity(firstRunIntent);
4421 markFirstRunActivityShown();
4422 }
4423 }
4424 }
4425
4426 private void markFirstRunActivityShown() {
4427 SharedPreferences.Editor editor = mSharedPrefs.edit();
4428 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4429 editor.apply();
4430 }
4431
Winson Chungaf40f202013-09-18 18:26:31 -07004432 public void showFirstRunCling() {
Winson Chung46353de2012-02-16 14:05:10 -08004433 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004434 !mSharedPrefs.getBoolean(Cling.FIRST_RUN_CLING_DISMISSED_KEY, false) &&
Michael Jurka974c3862012-05-22 22:00:31 -07004435 !skipCustomClingIfNoAccounts() ) {
Adam Cohenb54a5982014-01-08 15:21:04 -08004436
4437
Michael Jurka45355c42012-10-08 13:21:35 +02004438 // If we're not using the default workspace layout, replace workspace cling
4439 // with a custom workspace cling (usually specified in an overlay)
4440 // For now, only do this on tablets
Winson Chunge6eabff2013-09-24 11:01:23 -07004441 if (!DISABLE_CUSTOM_CLINGS) {
4442 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4443 getResources().getBoolean(R.bool.config_useCustomClings)) {
4444 // Use a custom cling
4445 View cling = findViewById(R.id.workspace_cling);
4446 ViewGroup clingParent = (ViewGroup) cling.getParent();
4447 int clingIndex = clingParent.indexOfChild(cling);
4448 clingParent.removeViewAt(clingIndex);
4449 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4450 clingParent.addView(customCling, clingIndex);
4451 customCling.setId(R.id.workspace_cling);
4452 }
4453 }
4454 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4455 if (cling != null) {
4456 String sbHintStr = getFirstRunClingSearchBarHint();
4457 String ccHintStr = getFirstRunCustomContentHint();
4458 if (!sbHintStr.isEmpty()) {
4459 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4460 sbHint.setText(sbHintStr);
4461 sbHint.setVisibility(View.VISIBLE);
4462 }
Winson Chung82e5c982013-10-09 12:04:50 -07004463 setCustomContentHintVisibility(cling, ccHintStr, true, false);
Michael Jurka45355c42012-10-08 13:21:35 +02004464 }
Winson Chungfa545132013-09-26 17:45:32 -07004465 initCling(R.id.first_run_cling, 0, false, true);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004466 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004467 removeCling(R.id.first_run_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004468 }
4469 }
Winson Chungaf40f202013-09-18 18:26:31 -07004470
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004471 /**
4472 * Called when the SearchBar hint should be changed.
4473 *
4474 * @param hint the hint to be displayed in the search bar.
4475 */
4476 protected void onSearchBarHintChanged(String hint) {
4477 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4478 if (cling != null && cling.getVisibility() == View.VISIBLE && !hint.isEmpty()) {
4479 TextView sbHint = (TextView) cling.findViewById(R.id.search_bar_hint);
4480 sbHint.setText(hint);
4481 sbHint.setVisibility(View.VISIBLE);
4482 }
4483 }
4484
Winson Chunge6eabff2013-09-24 11:01:23 -07004485 protected String getFirstRunClingSearchBarHint() {
4486 return "";
4487 }
4488 protected String getFirstRunCustomContentHint() {
4489 return "";
4490 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004491 protected int getFirstRunFocusedHotseatAppDrawableId() {
4492 return -1;
4493 }
4494 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4495 return null;
4496 }
4497 protected int getFirstRunFocusedHotseatAppRank() {
4498 return -1;
4499 }
4500 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4501 return "";
4502 }
4503 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4504 return "";
4505 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004506
Winson Chungaf40f202013-09-18 18:26:31 -07004507 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004508 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004509 if (isClingsEnabled() &&
Winson Chungaf40f202013-09-18 18:26:31 -07004510 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung3a6e7f32013-10-09 15:50:52 -07004511 Cling c = initCling(R.id.workspace_cling, 0, false, true);
4512
4513 // Set the focused hotseat app if there is one
4514 c.setFocusedHotseatApp(getFirstRunFocusedHotseatAppDrawableId(),
4515 getFirstRunFocusedHotseatAppRank(),
4516 getFirstRunFocusedHotseatAppComponentName(),
4517 getFirstRunFocusedHotseatAppBubbleTitle(),
4518 getFirstRunFocusedHotseatAppBubbleDescription());
Winson Chung9d9d74f2011-09-19 11:49:12 -07004519 } else {
Winson Chungaf40f202013-09-18 18:26:31 -07004520 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004521 }
4522 }
4523 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004524 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004525 if (isClingsEnabled() &&
4526 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chungfa545132013-09-26 17:45:32 -07004527 Cling cling = initCling(R.id.folder_cling, R.id.cling_scrim,
4528 true, true);
Winson Chungaf40f202013-09-18 18:26:31 -07004529 return cling;
Winson Chung9d9d74f2011-09-19 11:49:12 -07004530 } else {
4531 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004532 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004533 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004534 }
Michael Jurka104c4562013-07-08 18:03:46 -07004535 protected SharedPreferences getSharedPrefs() {
4536 return mSharedPrefs;
4537 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004538 public boolean isFolderClingVisible() {
4539 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004540 if (cling != null) {
4541 return cling.getVisibility() == View.VISIBLE;
4542 }
4543 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004544 }
Winson Chungaf40f202013-09-18 18:26:31 -07004545 public void dismissFirstRunCling(View v) {
4546 Cling cling = (Cling) findViewById(R.id.first_run_cling);
4547 Runnable cb = new Runnable() {
4548 public void run() {
4549 // Show the workspace cling next
4550 showFirstRunWorkspaceCling();
4551 }
4552 };
4553 dismissCling(cling, cb, Cling.FIRST_RUN_CLING_DISMISSED_KEY,
4554 DISMISS_CLING_DURATION, false);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004555
4556 // Fade out the search bar for the workspace cling coming up
4557 mSearchDropTargetBar.hideSearchBar(true);
Winson Chungaf40f202013-09-18 18:26:31 -07004558 }
Winson Chung82f55532011-08-09 14:14:23 -07004559 public void dismissWorkspaceCling(View v) {
4560 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004561 Runnable cb = null;
4562 if (v == null) {
4563 cb = new Runnable() {
4564 public void run() {
4565 mWorkspace.enterOverviewMode();
4566 }
4567 };
4568 }
4569 dismissCling(cling, cb, Cling.WORKSPACE_CLING_DISMISSED_KEY,
4570 DISMISS_CLING_DURATION, true);
Winson Chung3a6e7f32013-10-09 15:50:52 -07004571
4572 // Fade in the search bar
4573 mSearchDropTargetBar.showSearchBar(true);
Winson Chung7d7541e2011-09-16 20:14:36 -07004574 }
4575 public void dismissFolderCling(View v) {
4576 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chungaf40f202013-09-18 18:26:31 -07004577 dismissCling(cling, null, Cling.FOLDER_CLING_DISMISSED_KEY,
4578 DISMISS_CLING_DURATION, true);
Winson Chung82f55532011-08-09 14:14:23 -07004579 }
4580
Mathew Inwood72fbec12013-11-19 15:45:07 +00004581 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4582 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4583 if (ri == null) {
4584 return null;
4585 }
4586 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4587 }
4588
4589 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4590 Bitmap icon) {
4591 return new ShortcutInfo(shortcutIntent, caption, icon);
4592 }
4593
4594 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4595 dragView.setTag(dragInfo);
4596 mWorkspace.onDragStartedWithItem(dragView);
4597 mWorkspace.beginDragShared(dragView, source);
4598 }
4599
Winson Chung80baf5a2010-08-09 16:03:15 -07004600 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004601 * Prints out out state for debugging.
4602 */
4603 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004604 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004605 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004606 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4607 Log.d(TAG, "mRestoring=" + mRestoring);
4608 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4609 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004610 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004611 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004612
Winson Chung785d2eb2011-04-14 16:08:02 -07004613 if (mAppsCustomizeContent != null) {
4614 mAppsCustomizeContent.dumpState();
4615 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004616 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004617 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004618
4619 @Override
4620 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4621 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004622 synchronized (sDumpLogs) {
4623 writer.println(" ");
4624 writer.println("Debug logs: ");
4625 for (int i = 0; i < sDumpLogs.size(); i++) {
4626 writer.println(" " + sDumpLogs.get(i));
4627 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004628 }
4629 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004630
4631 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004632 if (DEBUG_DUMP_LOG) {
4633 synchronized (sDumpLogs) {
4634 Log.d(TAG, "");
4635 Log.d(TAG, "*********************");
4636 Log.d(TAG, "Launcher debug logs: ");
4637 for (int i = 0; i < sDumpLogs.size(); i++) {
4638 Log.d(TAG, " " + sDumpLogs.get(i));
4639 }
4640 Log.d(TAG, "*********************");
4641 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004642 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004643 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004644 }
4645
4646 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004647 addDumpLog(tag, log, null, debugLog);
4648 }
4649
4650 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004651 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004652 if (e != null) {
4653 Log.d(tag, log, e);
4654 } else {
4655 Log.d(tag, log);
4656 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004657 }
Winson Chungede41292013-09-19 16:27:36 -07004658 if (DEBUG_DUMP_LOG) {
4659 sDateStamp.setTime(System.currentTimeMillis());
4660 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004661 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4662 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004663 }
Winson Chungede41292013-09-19 16:27:36 -07004664 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004665 }
4666
Winson Chungede41292013-09-19 16:27:36 -07004667 public void dumpLogsToLocalData() {
4668 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004669 new AsyncTask<Void, Void, Void>() {
4670 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004671 boolean success = false;
4672 sDateStamp.setTime(sRunStart);
4673 String FILENAME = sDateStamp.getMonth() + "-"
4674 + sDateStamp.getDay() + "_"
4675 + sDateStamp.getHours() + "-"
4676 + sDateStamp.getMinutes() + "_"
4677 + sDateStamp.getSeconds() + ".txt";
4678
4679 FileOutputStream fos = null;
4680 File outFile = null;
4681 try {
4682 outFile = new File(getFilesDir(), FILENAME);
4683 outFile.createNewFile();
4684 fos = new FileOutputStream(outFile);
4685 } catch (Exception e) {
4686 e.printStackTrace();
4687 }
4688 if (fos != null) {
4689 PrintWriter writer = new PrintWriter(fos);
4690
4691 writer.println(" ");
4692 writer.println("Debug logs: ");
4693 synchronized (sDumpLogs) {
4694 for (int i = 0; i < sDumpLogs.size(); i++) {
4695 writer.println(" " + sDumpLogs.get(i));
4696 }
4697 }
4698 writer.close();
4699 }
4700 try {
4701 if (fos != null) {
4702 fos.close();
4703 success = true;
4704 }
4705 } catch (IOException e) {
4706 e.printStackTrace();
4707 }
Michael Jurka43467462013-11-14 12:03:58 +01004708 return null;
Winson Chungede41292013-09-19 16:27:36 -07004709 }
Michael Jurka43467462013-11-14 12:03:58 +01004710 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004711 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004713}
Michael Jurka2763be32011-02-24 11:19:57 -08004714
Michael Jurkaabded662011-03-04 12:06:57 -08004715interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004716 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004717 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004718 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004719 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004720 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004721}
Dan Sandlerd5024042014-01-09 15:01:33 -05004722
4723interface DebugIntents {
4724 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4725 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004726}