blob: 7f130c0c0750327f00c36db181688bc58d3aef8e [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070090import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
96import android.widget.TextView;
97import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070098
Daniel Sandler325dc232013-06-05 22:57:57 -040099import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700114import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100119public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Daniel Sandlerf061f822013-06-27 13:59:36 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400128 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
Winson Chung70d72102011-08-12 11:24:35 -0700130 private static final int MENU_GROUP_WALLPAPER = 1;
131 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
132 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700133 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
134 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 private static final int REQUEST_PICK_APPLICATION = 6;
139 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Michael Jurka8b805b12012-04-18 14:23:14 -0700143 private static final int REQUEST_BIND_APPWIDGET = 11;
144
Mathew Inwood876a8462013-06-14 14:12:41 +0100145 /**
146 * IntentStarter uses request codes starting with this. This must be greater than all activity
147 * request codes used internally.
148 */
149 protected static final int REQUEST_LAST = 100;
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
152
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800153 static final int SCREEN_COUNT = 5;
154 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Romain Guy98d01652009-06-30 16:21:04 -0700156 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800157 // To turn on these properties, type
158 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
159 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
160 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
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";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100189 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700190 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100191 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700192 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100193 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700194
Adam Cohen39a06042013-07-19 14:30:12 -0700195 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700196 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700197
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700198 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700199 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700200 private State mState = State.WORKSPACE;
201 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800202 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700205 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
206 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700207 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700208 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800211 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
214 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700215 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800217 private final BroadcastReceiver mCloseSystemDialogsReceiver
218 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800219 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private LayoutInflater mInflater;
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700225 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800226 private DragLayer mDragLayer;
227 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700228 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700229
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700230 private AppWidgetManager mAppWidgetManager;
231 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700232
Michael Jurkac9d95c52011-08-29 14:03:34 -0700233 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700234 private AppWidgetProviderInfo mPendingAddWidgetInfo;
235
Michael Jurka0280c3b2010-09-17 15:00:07 -0700236 private int[] mTmpAddItemCellCoordinates = new int[2];
237
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238 private FolderInfo mFolderInfo;
239
Winson Chung3d503fb2011-07-13 17:25:49 -0700240 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700241 private View mOverviewPanel;
242
Michael Jurka838a4ca2011-02-07 13:33:06 -0800243 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700244
Winson Chungc51db6a2011-10-05 11:44:49 -0700245 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700246 private AppsCustomizeTabHost mAppsCustomizeTabHost;
247 private AppsCustomizePagedView mAppsCustomizeContent;
248 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700251 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
252 // scroll issues (because the workspace may not have been measured yet) and extra work.
253 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
254 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
256 private SpannableStringBuilder mDefaultKeySsb = null;
257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 private boolean mWorkspaceLoading = true;
259
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800260 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private boolean mRestoring;
262 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700263 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
Michael Jurka1e2f4652013-07-08 18:03:46 -0700265 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700266 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
267
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // Keep track of whether the user has left launcher
269 private static boolean sPausedFromUserAction = false;
270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedInstanceState;
272
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800275 private boolean mUserPresent = true;
276 private boolean mVisible = false;
277 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700278 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static LocaleConfiguration sLocaleConfiguration = null;
281
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700282 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700283
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700284 private Intent mAppMarketIntent = null;
285
Adam Cohended9f8d2010-11-03 13:25:16 -0700286 // Related to the auto-advancing of widgets
287 private final int ADVANCE_MSG = 1;
288 private final int mAdvanceInterval = 20000;
289 private final int mAdvanceStagger = 250;
290 private long mAutoAdvanceSentTime;
291 private long mAutoAdvanceTimeLeft = -1;
292 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
293 new HashMap<View, AppWidgetProviderInfo>();
294
Winson Chung400438b2011-01-16 17:53:48 -0800295 // Determines how long to wait after a rotation before restoring the screen orientation to
296 // match the sensor state.
297 private final int mRestoreScreenOrientationDelay = 500;
298
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700300 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
301 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
302 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800303
Craig Mautner360310b2012-10-26 15:13:08 -0700304 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
307
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700308 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
309
Winson Chung46353de2012-02-16 14:05:10 -0800310 // We only want to get the SharedPreferences once since it does an FS stat each time we get
311 // it from the context.
312 private SharedPreferences mSharedPrefs;
313
Adam Cohene25af792013-06-06 23:08:25 -0700314 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
315
Winson Chungf0c6ae02012-03-21 16:10:31 -0700316 // Holds the page that we need to animate to, and the icon views that we need to animate up
317 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700318 private ImageView mFolderIconImageView;
319 private Bitmap mFolderIconBitmap;
320 private Canvas mFolderIconCanvas;
321 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 private BubbleTextView mWaitingForResume;
324
Michael Jurka22143132012-10-04 17:42:38 +0200325 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
326 = new HideFromAccessibilityHelper();
327
Michael Jurkac1f5d262011-09-30 19:32:27 -0700328 private Runnable mBuildLayersRunnable = new Runnable() {
329 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700330 if (mWorkspace != null) {
331 mWorkspace.buildPageHardwareLayers();
332 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700333 }
334 };
335
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800336 private static ArrayList<PendingAddArguments> sPendingAddList
337 = new ArrayList<PendingAddArguments>();
338
Michael Jurka0a457bf2012-11-19 14:05:05 -0800339 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
340
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800341 private static class PendingAddArguments {
342 int requestCode;
343 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700344 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800346 int cellX;
347 int cellY;
348 }
349
Daniel Sandlerff02d492013-08-05 02:12:05 -0400350 private Stats mStats;
351
Michael Jurka0a457bf2012-11-19 14:05:05 -0800352 private static boolean isPropertyEnabled(String propertyName) {
353 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 @Override
357 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700358 if (DEBUG_STRICT_MODE) {
359 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
360 .detectDiskReads()
361 .detectDiskWrites()
362 .detectNetwork() // or .detectAll() for all detectable problems
363 .penaltyLog()
364 .build());
365 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
366 .detectLeakedSqlLiteObjects()
367 .detectLeakedClosableObjects()
368 .penaltyLog()
369 .penaltyDeath()
370 .build());
371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400374
375 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
376 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400377 LauncherAppState app = LauncherAppState.getInstance();
378 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700379 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800380 mModel = app.setLauncher(this);
381 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400382 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 mStats = new Stats(this);
386
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700387 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700388 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
389 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700390
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700391 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
392 // this also ensures that any synchronous binding below doesn't re-trigger another
393 // LauncherModel load.
394 mPaused = false;
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200397 android.os.Debug.startMethodTracing(
398 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 }
400
401 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 setContentView(R.layout.launcher);
403 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700404 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800406 registerContentObservers();
407
Joe Onorato7c312c12009-08-13 21:36:53 -0700408 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 mSavedState = savedInstanceState;
411 restoreState(mSavedState);
412
Winson Chunga12a2502010-12-20 14:41:35 -0800413 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700414 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200415 mAppsCustomizeContent.onPackagesUpdated(
416 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700417 }
Winson Chunga12a2502010-12-20 14:41:35 -0800418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 if (PROFILE_STARTUP) {
420 android.os.Debug.stopMethodTracing();
421 }
422
423 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700424 if (sPausedFromUserAction) {
425 // If the user leaves launcher, then we should just load items asynchronously when
426 // they return.
427 mModel.startLoader(true, -1);
428 } else {
429 // We only load the page synchronously if the user rotates (or triggers a
430 // configuration change) while launcher is in the foreground
431 mModel.startLoader(true, mWorkspace.getCurrentPage());
432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
435 // For handling default keys
436 mDefaultKeySsb = new SpannableStringBuilder();
437 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800438
439 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
440 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800441
Adam Cohenf9426d52012-06-04 17:26:21 -0700442 updateGlobalIcons();
443
444 // On large interfaces, we want the screen to auto-rotate based on the current orientation
445 unlockScreenOrientation(true);
446 }
447
Winson Chung4a2afa32012-07-19 14:53:05 -0700448 protected void onUserLeaveHint() {
449 super.onUserLeaveHint();
450 sPausedFromUserAction = true;
451 }
452
Winson Chung98ca0f72013-07-29 12:58:51 -0700453 /** To be overriden by subclasses to hint to Launcher that we have custom content */
454 protected boolean hasCustomContentToLeft() {
455 return false;
456 }
457
Adam Cohenf9426d52012-06-04 17:26:21 -0700458 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700459 boolean searchVisible = false;
460 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800461 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700462 int coi = getCurrentOrientationIndexForGlobalIcons();
463 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
464 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700465 updateAppMarketIcon();
466 searchVisible = updateGlobalSearchIcon();
467 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700468 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700469 if (sGlobalSearchIcon[coi] != null) {
470 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700471 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700472 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700473 if (sVoiceSearchIcon[coi] != null) {
474 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700475 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700476 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700477 if (sAppMarketIcon[coi] != null) {
478 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479 }
Winson Chungadf0c182012-08-23 14:59:07 -0700480 if (mSearchDropTargetBar != null) {
481 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
Romain Guycbb89e42009-06-08 15:52:54 -0700484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 if (sLocaleConfiguration == null) {
487 new AsyncTask<Void, Void, LocaleConfiguration>() {
488 @Override
489 protected LocaleConfiguration doInBackground(Void... unused) {
490 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
491 readConfiguration(Launcher.this, localeConfiguration);
492 return localeConfiguration;
493 }
494
495 @Override
496 protected void onPostExecute(LocaleConfiguration result) {
497 sLocaleConfiguration = result;
498 checkForLocaleChange(); // recursive, but now with a locale configuration
499 }
500 }.execute();
501 return;
502 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700503
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final Configuration configuration = getResources().getConfiguration();
505
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700506 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 final String locale = configuration.locale.toString();
508
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700509 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 final int mcc = configuration.mcc;
511
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 final int mnc = configuration.mnc;
514
Romain Guy84f296c2009-11-04 15:00:44 -0800515 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516
Romain Guy84f296c2009-11-04 15:00:44 -0800517 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700518 sLocaleConfiguration.locale = locale;
519 sLocaleConfiguration.mcc = mcc;
520 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700521
Joe Onorato0589f0f2010-02-08 13:44:00 -0800522 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700523
524 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
525 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700526 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700527 public void run() {
528 writeConfiguration(Launcher.this, localeConfiguration);
529 }
530 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700531 }
532 }
533
534 private static class LocaleConfiguration {
535 public String locale;
536 public int mcc = -1;
537 public int mnc = -1;
538 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700539
Romain Guy98d01652009-06-30 16:21:04 -0700540 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
541 DataInputStream in = null;
542 try {
543 in = new DataInputStream(context.openFileInput(PREFERENCES));
544 configuration.locale = in.readUTF();
545 configuration.mcc = in.readInt();
546 configuration.mnc = in.readInt();
547 } catch (FileNotFoundException e) {
548 // Ignore
549 } catch (IOException e) {
550 // Ignore
551 } finally {
552 if (in != null) {
553 try {
554 in.close();
555 } catch (IOException e) {
556 // Ignore
557 }
558 }
559 }
560 }
561
562 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
563 DataOutputStream out = null;
564 try {
565 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
566 out.writeUTF(configuration.locale);
567 out.writeInt(configuration.mcc);
568 out.writeInt(configuration.mnc);
569 out.flush();
570 } catch (FileNotFoundException e) {
571 // Ignore
572 } catch (IOException e) {
573 //noinspection ResultOfMethodCallIgnored
574 context.getFileStreamPath(PREFERENCES).delete();
575 } finally {
576 if (out != null) {
577 try {
578 out.close();
579 } catch (IOException e) {
580 // Ignore
581 }
582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
584 }
585
Bjorn Bringertc459e522013-06-07 19:36:01 +0100586 public LayoutInflater getInflater() {
587 return mInflater;
588 }
589
Adam Cohen716b51e2011-06-30 12:09:54 -0700590 public DragLayer getDragLayer() {
591 return mDragLayer;
592 }
593
Winson Chung36a62fe2012-05-06 18:04:42 -0700594 boolean isDraggingEnabled() {
595 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
596 // that is subsequently removed from the workspace in startBinding().
597 return !mModel.isLoadingWorkspace();
598 }
599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 static int getScreen() {
601 synchronized (sLock) {
602 return sScreen;
603 }
604 }
605
606 static void setScreen(int screen) {
607 synchronized (sLock) {
608 sScreen = screen;
609 }
610 }
611
Winson Chung557d6ed2011-07-08 15:34:52 -0700612 /**
613 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
614 * a configuration step, this allows the proper animations to run after other transitions.
615 */
616 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700617 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 switch (args.requestCode) {
619 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700620 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700621 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 break;
623 case REQUEST_PICK_SHORTCUT:
624 processShortcut(args.intent);
625 break;
626 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700627 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700628 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700629 result = true;
630 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800631 case REQUEST_CREATE_APPWIDGET:
632 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700633 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700634 result = true;
635 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800636 case REQUEST_PICK_WALLPAPER:
637 // We just wanted the activity result here so we can clear mWaitingForResult
638 break;
639 }
Michael Jurka27614d22012-04-02 06:40:22 -0700640 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
641 // if you turned the screen off and then back while in All Apps, Launcher would not
642 // return to the workspace. Clearing mAddInfo.container here fixes this issue
643 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700644 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700648 protected void onActivityResult(
649 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700650 // Reset the startActivity waiting flag
651 mWaitingForResult = false;
652
Michael Jurka8b805b12012-04-18 14:23:14 -0700653 if (requestCode == REQUEST_BIND_APPWIDGET) {
654 int appWidgetId = data != null ?
655 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
656 if (resultCode == RESULT_CANCELED) {
657 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
658 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700659 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700660 }
661 return;
662 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700663 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800664 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
665 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700666
Adam Cohened66b2b2012-01-23 17:28:51 -0800667 // We have special handling for widgets
668 if (isWidgetDrop) {
669 int appWidgetId = data != null ?
670 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700671 if (appWidgetId < 0) {
672 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
673 "widget configuration activity.");
674 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
675 } else {
676 completeTwoStageWidgetDrop(resultCode, appWidgetId);
677 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 return;
679 }
680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 // The pattern used here is that a user PICKs a specific application,
682 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
685 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700686 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800687 final PendingAddArguments args = new PendingAddArguments();
688 args.requestCode = requestCode;
689 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700692 args.cellX = mPendingAddInfo.cellX;
693 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800694 if (isWorkspaceLocked()) {
695 sPendingAddList.add(args);
696 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700697 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700701 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
703 null);
704 }
705
706 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700707 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800709 Runnable onCompleteRunnable = null;
710 int animationType = 0;
711
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700712 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 if (resultCode == RESULT_OK) {
714 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
715 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700716 mPendingAddWidgetInfo);
717 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 onCompleteRunnable = new Runnable() {
719 @Override
720 public void run() {
721 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
724 null);
725 }
726 };
727 } else if (resultCode == RESULT_CANCELED) {
728 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
729 onCompleteRunnable = new Runnable() {
730 @Override
731 public void run() {
732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
733 null);
734 }
735 };
736 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700737 if (mDragLayer.getAnimatedView() != null) {
738 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
739 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
740 animationType, boundWidget, true);
741 } else {
742 // The animated view may be null in the case of a rotation during widget configuration
743 onCompleteRunnable.run();
744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 }
746
747 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700748 protected void onStop() {
749 super.onStop();
750 FirstFrameAnimatorHelper.setIsVisible(false);
751 }
752
753 @Override
754 protected void onStart() {
755 super.onStart();
756 FirstFrameAnimatorHelper.setIsVisible(true);
757 }
758
759 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200761 long startTime = 0;
762 if (DEBUG_RESUME_TIME) {
763 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400764 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700767
Winson Chung4a2afa32012-07-19 14:53:05 -0700768 // Restore the previous launcher state
769 if (mOnResumeState == State.WORKSPACE) {
770 showWorkspace(false);
771 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
772 showAllApps(false);
773 }
774 mOnResumeState = State.NONE;
775
Craig Mautner360310b2012-10-26 15:13:08 -0700776 // Background was set to gradient in onPause(), restore to black if in all apps.
777 setWorkspaceBackground(mState == State.WORKSPACE);
778
Winson Chungde0fb8f2012-05-08 14:37:08 -0700779 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700780 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700781
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800782 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700783 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700784 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700786 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700788 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700790 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200791 // We might have postponed some bind calls until onResume (see waitUntilResume) --
792 // execute them here
793 long startTimeCallbacks = 0;
794 if (DEBUG_RESUME_TIME) {
795 startTimeCallbacks = System.currentTimeMillis();
796 }
797
798 if (mAppsCustomizeContent != null) {
799 mAppsCustomizeContent.setBulkBind(true);
800 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700801 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
802 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200803 }
804 if (mAppsCustomizeContent != null) {
805 mAppsCustomizeContent.setBulkBind(false);
806 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700807 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200808 if (DEBUG_RESUME_TIME) {
809 Log.d(TAG, "Time spent processing callbacks in onResume: " +
810 (System.currentTimeMillis() - startTimeCallbacks));
811 }
Michael Jurka447bf842013-05-15 14:52:15 +0200812 }
Michael Jurka54554252013-08-01 12:52:23 +0200813 if (mOnResumeCallbacks.size() > 0) {
814 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
815 mOnResumeCallbacks.get(i).run();
816 }
817 mOnResumeCallbacks.clear();
818 }
Winson Chunge4e50662012-01-23 14:45:13 -0800819
820 // Reset the pressed state of icons that were locked in the press state while activities
821 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800822 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800823 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800824 mWaitingForResume.setStayPressed(false);
825 }
Winson Chunge4e50662012-01-23 14:45:13 -0800826 if (mAppsCustomizeContent != null) {
827 // Resets the previous all apps icon press state
828 mAppsCustomizeContent.resetDrawableState();
829 }
Adam Cohen06dff352012-06-01 17:17:08 -0700830 // It is possible that widgets can receive updates while launcher is not in the foreground.
831 // Consequently, the widgets will be inflated in the orientation of the foreground activity
832 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
833 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700834 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700835
836 // Again, as with the above scenario, it's possible that one or more of the global icons
837 // were updated in the wrong orientation.
838 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200839 if (DEBUG_RESUME_TIME) {
840 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
841 }
Adam Cohen06dff352012-06-01 17:17:08 -0700842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700846 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
847 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
848 // when Launcher resumes and we are still in AllApps.
849 updateWallpaperVisibility(true);
850
Winson Chung997a9232013-07-24 15:33:46 -0700851 // Ensure that items added to Launcher are queued until Launcher returns
852 InstallShortcutReceiver.enableInstallQueue();
853
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700854 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700855 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800856 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700857 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700858 }
Romain Guycbb89e42009-06-08 15:52:54 -0700859
Adam Cohen66a01fd2013-06-11 12:48:00 -0700860 protected void onFinishBindingItems() {
861 }
862
Adam Cohenbffe7452013-07-22 18:21:45 -0700863 QSBScroller mQsbScroller = new QSBScroller() {
864 int scrollY = 0;
865
866 @Override
867 public void setScrollY(int scroll) {
868 scrollY = scroll;
869
870 if (mWorkspace.isOnOrMovingToCustomContent()) {
871 mSearchDropTargetBar.setTranslationY(- scrollY);
872 }
873 }
874 };
875
876 public void resetQSBScroll() {
877 mSearchDropTargetBar.animate().translationY(0).start();
878 }
879
880 public interface CustomContentCallbacks {
881 // Custom content is completely shown
882 public void onShow();
883
884 // Custom content is completely hidden
885 public void onHide();
886 }
887
888 public interface QSBScroller {
889 public void setScrollY(int scrollY);
890 }
891
Adam Cohen66a01fd2013-06-11 12:48:00 -0700892 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700893 public QSBScroller addToCustomContentPage(View customContent) {
894 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700895 }
896
Winson Chung98ca0f72013-07-29 12:58:51 -0700897 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700898 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700899 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700900 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700901 }
902
Adam Cohenedb40762013-07-18 16:45:45 -0700903 // The custom content needs to offset its content to account for the QSB
904 public int getTopOffsetForCustomContent() {
905 return mWorkspace.getPaddingTop();
906 }
907
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700908 @Override
909 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400910 // Flag the loader to stop early before switching
911 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700912 if (mAppsCustomizeContent != null) {
913 mAppsCustomizeContent.surrender();
914 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800915 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700916 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700917
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800918 // We can't hide the IME if it was forced open. So don't bother
919 /*
920 @Override
921 public void onWindowFocusChanged(boolean hasFocus) {
922 super.onWindowFocusChanged(hasFocus);
923
924 if (hasFocus) {
925 final InputMethodManager inputManager = (InputMethodManager)
926 getSystemService(Context.INPUT_METHOD_SERVICE);
927 WindowManager.LayoutParams lp = getWindow().getAttributes();
928 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
929 android.os.Handler()) {
930 protected void onReceiveResult(int resultCode, Bundle resultData) {
931 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
932 }
933 });
934 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
935 }
936 }
937 */
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 private boolean acceptFilter() {
940 final InputMethodManager inputManager = (InputMethodManager)
941 getSystemService(Context.INPUT_METHOD_SERVICE);
942 return !inputManager.isFullscreenMode();
943 }
944
945 @Override
946 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700947 final int uniChar = event.getUnicodeChar();
948 final boolean handled = super.onKeyDown(keyCode, event);
949 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
950 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
952 keyCode, event);
953 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700955 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700956 // showSearchDialog()
957 // If there are multiple keystrokes before the search dialog takes focus,
958 // onSearchRequested() will be called for every keystroke,
959 // but it is idempotent, so it's fine.
960 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 }
962 }
963
Joe Onorato8a9625e2010-01-28 15:55:35 -0800964 // Eat the long press event so the keyboard doesn't come up.
965 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
966 return true;
967 }
968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 return handled;
970 }
971
Karl Rosaen138a0412009-04-23 19:00:21 -0700972 private String getTypedText() {
973 return mDefaultKeySsb.toString();
974 }
975
976 private void clearTypedText() {
977 mDefaultKeySsb.clear();
978 mDefaultKeySsb.clearSpans();
979 Selection.setSelection(mDefaultKeySsb, 0);
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700983 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
984 * State
985 */
986 private static State intToState(int stateOrdinal) {
987 State state = State.WORKSPACE;
988 final State[] stateValues = State.values();
989 for (int i = 0; i < stateValues.length; i++) {
990 if (stateValues[i].ordinal() == stateOrdinal) {
991 state = stateValues[i];
992 break;
993 }
994 }
995 return state;
996 }
997
998 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 * Restores the previous state, if it exists.
1000 *
1001 * @param savedState The previous state.
1002 */
1003 private void restoreState(Bundle savedState) {
1004 if (savedState == null) {
1005 return;
1006 }
1007
Michael Jurka883f55b2010-10-21 15:47:14 -07001008 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001009 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001011 }
1012
Winson Chungf0c6ae02012-03-21 16:10:31 -07001013 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001015 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 }
1017
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001019 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001020
Winson Chung3d503fb2011-07-13 17:25:49 -07001021 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1022 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001023 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001024 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1025 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001026 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1027 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1028 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001029 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 mRestoring = true;
1031 }
1032
1033 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1034 if (renameFolder) {
1035 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001036 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 mRestoring = true;
1038 }
Winson Chunga12a2502010-12-20 14:41:35 -08001039
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 // Restore the AppsCustomize tab
1041 if (mAppsCustomizeTabHost != null) {
1042 String curTab = savedState.getString("apps_customize_currentTab");
1043 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001044 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001045 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001046 mAppsCustomizeContent.loadAssociatedPages(
1047 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001048 }
1049
Winson Chung5afbf7b2011-07-25 11:53:08 -07001050 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1051 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054
1055 /**
1056 * Finds all the views we need and configure them properly.
1057 */
1058 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001059 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001060
Craig Mautner360310b2012-10-26 15:13:08 -07001061 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001062 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1063 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001064 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001065
1066 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1067 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
Winson Chung4c98d922011-05-31 16:50:48 -07001069 // Setup the drag layer
1070 mDragLayer.setup(this, dragController);
1071
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 // Setup the hotseat
1073 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1074 if (mHotseat != null) {
1075 mHotseat.setup(this);
1076 }
1077
Adam Cohenf358a4b2013-07-23 16:47:31 -07001078 mOverviewPanel = findViewById(R.id.overview_panel);
1079 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1080 @Override
1081 public void onClick(View arg0) {
1082 showAllApps(true);
1083 }
1084 });
1085 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1086 @Override
1087 public void onClick(View arg0) {
1088 startWallpaper();
1089 }
1090 });
1091
Winson Chung4c98d922011-05-31 16:50:48 -07001092 // Setup the workspace
1093 mWorkspace.setHapticFeedbackEnabled(false);
1094 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001095 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001096 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001097
Winson Chungf0ea4d32011-06-06 14:27:16 -07001098 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001099 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001100
Winson Chungf0ea4d32011-06-06 14:27:16 -07001101 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001102 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001103 mAppsCustomizeContent = (AppsCustomizePagedView)
1104 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1105 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001106
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001108 dragController.setDragScoller(mWorkspace);
1109 dragController.setScrollView(mDragLayer);
1110 dragController.setMoveTarget(mWorkspace);
1111 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001112 if (mSearchDropTargetBar != null) {
1113 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001114 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001115
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001116 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001117 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001118 mWeightWatcher = new WeightWatcher(this);
1119 mWeightWatcher.setAlpha(0.5f);
1120 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001121 new FrameLayout.LayoutParams(
1122 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001123 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001124 Gravity.BOTTOM)
1125 );
Adam Cohen39a06042013-07-19 14:30:12 -07001126
1127 boolean show = shouldShowWeightWatcher();
1128 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131
1132 /**
1133 * Creates a view representing a shortcut.
1134 *
1135 * @param info The data structure describing the shortcut.
1136 *
1137 * @return A View inflated from R.layout.application.
1138 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001139 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001141 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143
1144 /**
1145 * Creates a view representing a shortcut inflated from the specified resource.
1146 *
1147 * @param layoutResId The id of the XML layout used to create the shortcut.
1148 * @param parent The group the shortcut belongs to.
1149 * @param info The data structure describing the shortcut.
1150 *
1151 * @return A View inflated from layoutResId.
1152 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001153 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001154 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1155 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 return favorite;
1158 }
1159
1160 /**
1161 * Add an application shortcut to the workspace.
1162 *
1163 * @param data The intent describing the application.
1164 * @param cellInfo The position on screen where to create the shortcut.
1165 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001169
Adam Cohenfbba09b2011-07-18 21:43:05 -07001170 // First we check if we already know the exact location where we want to add this item.
1171 if (cellX >= 0 && cellY >= 0) {
1172 cellXY[0] = cellX;
1173 cellXY[1] = cellY;
1174 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001175 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001176 return;
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001179 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001180
Romain Guy73b979d2009-06-09 12:57:21 -07001181 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001182 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001184 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001185 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001186 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001187 } else {
1188 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 /**
1193 * Add a shortcut to the workspace.
1194 *
1195 * @param data The intent describing the shortcut.
1196 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001198 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001199 int cellY) {
1200 int[] cellXY = mTmpAddItemCellCoordinates;
1201 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001202 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Michael Jurkad3ef3062010-11-23 16:23:58 -08001204 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001205
Adam Cohen558baaf2011-08-15 15:22:57 -07001206 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001207 if (info == null) {
1208 return;
1209 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001210 final View view = createShortcut(info);
1211
Adam Cohenfbba09b2011-07-18 21:43:05 -07001212 // First we check if we already know the exact location where we want to add this item.
1213 if (cellX >= 0 && cellY >= 0) {
1214 cellXY[0] = cellX;
1215 cellXY[1] = cellY;
1216 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001217
1218 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001219 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001220 true, null,null)) {
1221 return;
1222 }
1223 DragObject dragObject = new DragObject();
1224 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001225 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1226 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001227 return;
1228 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001229 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001230 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001232 foundCellSpan = (result != null);
1233 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001234 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001235 }
1236
1237 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001238 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 return;
1240 }
1241
Adam Cohendcd297f2013-06-18 13:13:40 -07001242 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243
1244 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001245 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001246 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
1249
Adam Cohen2f093b62012-04-30 18:59:53 -07001250 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1251 int minHeight) {
1252 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001253 // We want to account for the extra amount of padding that we are adding to the widget
1254 // to ensure that it gets the full amount of space that it has requested
1255 int requiredWidth = minWidth + padding.left + padding.right;
1256 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001257 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001258 }
1259
Adam Cohen2f093b62012-04-30 18:59:53 -07001260 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1261 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001262 }
1263
Adam Cohen2f093b62012-04-30 18:59:53 -07001264 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1265 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001266 }
1267
Adam Cohen2f093b62012-04-30 18:59:53 -07001268 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1269 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001270 }
1271
Adam Cohen2f093b62012-04-30 18:59:53 -07001272 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1273 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001274 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001278 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001280 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001281 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001283 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001284 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1285 if (appWidgetInfo == null) {
1286 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1287 }
Romain Guycbb89e42009-06-08 15:52:54 -07001288
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001289 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001290 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001291
Adam Cohen2f093b62012-04-30 18:59:53 -07001292 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1293 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001296 // We have saved the position to which the widget was dragged-- this really only matters
1297 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001298 int[] cellXY = mTmpAddItemCellCoordinates;
1299 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001300 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1303 cellXY[0] = mPendingAddInfo.cellX;
1304 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001305 spanXY[0] = mPendingAddInfo.spanX;
1306 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001307 foundCellSpan = true;
1308 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001311 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1312 spanXY[1], cellXY, finalSpan);
1313 spanXY[0] = finalSpan[0];
1314 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001315 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001316 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001317 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001318 }
1319
Michael Jurka0280c3b2010-09-17 15:00:07 -07001320 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001321 if (appWidgetId != -1) {
1322 // Deleting an app widget ID is a void call but writes to disk before returning
1323 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001324 new Thread("deleteAppWidgetId") {
1325 public void run() {
1326 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1327 }
1328 }.start();
1329 }
Winson Chung93eef082012-03-23 15:59:27 -07001330 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001331 return;
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001334 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001335 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1336 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001337 launcherInfo.spanX = spanXY[0];
1338 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001339 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1340 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001343 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344
1345 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001346 if (hostView == null) {
1347 // Perform actual inflation because we're live
1348 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1349 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1350 } else {
1351 // The AppWidgetHostView has already been inflated and instantiated
1352 launcherInfo.hostView = hostView;
1353 }
Romain Guycbb89e42009-06-08 15:52:54 -07001354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001356 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001357 launcherInfo.notifyWidgetSizeChanged(this);
1358
Adam Cohendcd297f2013-06-18 13:13:40 -07001359 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001361
1362 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001364 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Romain Guycbb89e42009-06-08 15:52:54 -07001366
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1368 @Override
1369 public void onReceive(Context context, Intent intent) {
1370 final String action = intent.getAction();
1371 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1372 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001373 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001375
Winson Chungc100e8e2011-08-09 16:02:43 -07001376 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001377 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001378 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1379 mAppsCustomizeTabHost.reset();
1380 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001381 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001382 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1383 mUserPresent = true;
1384 updateRunning();
1385 }
1386 }
1387 };
1388
1389 @Override
1390 public void onAttachedToWindow() {
1391 super.onAttachedToWindow();
1392
1393 // Listen for broadcasts related to user-presence
1394 final IntentFilter filter = new IntentFilter();
1395 filter.addAction(Intent.ACTION_SCREEN_OFF);
1396 filter.addAction(Intent.ACTION_USER_PRESENT);
1397 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001398 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001399 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001400 mVisible = true;
1401 }
1402
1403 @Override
1404 public void onDetachedFromWindow() {
1405 super.onDetachedFromWindow();
1406 mVisible = false;
1407
Adam Cohend113e0c2010-11-11 10:48:05 -08001408 if (mAttached) {
1409 unregisterReceiver(mReceiver);
1410 mAttached = false;
1411 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001412 updateRunning();
1413 }
1414
1415 public void onWindowVisibilityChanged(int visibility) {
1416 mVisible = visibility == View.VISIBLE;
1417 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001418 // The following code used to be in onResume, but it turns out onResume is called when
1419 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1420 // is a more appropriate event to handle
1421 if (mVisible) {
1422 mAppsCustomizeTabHost.onWindowVisible();
1423 if (!mWorkspaceLoading) {
1424 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001425 // We want to let Launcher draw itself at least once before we force it to build
1426 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001427 // apps is nice and speedy.
1428 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001429 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001430 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001431 if (mStarted) return;
1432 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001433 // We delay the layer building a bit in order to give
1434 // other message processing a time to run. In particular
1435 // this avoids a delay in hiding the IME if it was
1436 // currently shown, because doing that may involve
1437 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001438 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001439 final ViewTreeObserver.OnDrawListener listener = this;
1440 mWorkspace.post(new Runnable() {
1441 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001442 if (mWorkspace != null &&
1443 mWorkspace.getViewTreeObserver() != null) {
1444 mWorkspace.getViewTreeObserver().
1445 removeOnDrawListener(listener);
1446 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001447 }
1448 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001449 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001450 }
1451 });
1452 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001453 // When Launcher comes back to foreground, a different Activity might be responsible for
1454 // the app market intent, so refresh the icon
1455 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001456 clearTypedText();
1457 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001458 }
1459
1460 private void sendAdvanceMessage(long delay) {
1461 mHandler.removeMessages(ADVANCE_MSG);
1462 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1463 mHandler.sendMessageDelayed(msg, delay);
1464 mAutoAdvanceSentTime = System.currentTimeMillis();
1465 }
1466
1467 private void updateRunning() {
1468 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1469 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1470 mAutoAdvanceRunning = autoAdvanceRunning;
1471 if (autoAdvanceRunning) {
1472 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1473 sendAdvanceMessage(delay);
1474 } else {
1475 if (!mWidgetsToAdvance.isEmpty()) {
1476 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1477 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1478 }
1479 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001480 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001481 }
1482 }
1483 }
1484
1485 private final Handler mHandler = new Handler() {
1486 @Override
1487 public void handleMessage(Message msg) {
1488 if (msg.what == ADVANCE_MSG) {
1489 int i = 0;
1490 for (View key: mWidgetsToAdvance.keySet()) {
1491 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1492 final int delay = mAdvanceStagger * i;
1493 if (v instanceof Advanceable) {
1494 postDelayed(new Runnable() {
1495 public void run() {
1496 ((Advanceable) v).advance();
1497 }
1498 }, delay);
1499 }
1500 i++;
1501 }
1502 sendAdvanceMessage(mAdvanceInterval);
1503 }
1504 }
1505 };
1506
1507 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001508 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001509 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1510 if (v instanceof Advanceable) {
1511 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001512 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001513 updateRunning();
1514 }
1515 }
1516
1517 void removeWidgetToAutoAdvance(View hostView) {
1518 if (mWidgetsToAdvance.containsKey(hostView)) {
1519 mWidgetsToAdvance.remove(hostView);
1520 updateRunning();
1521 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001522 }
1523
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001525 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001526 launcherInfo.hostView = null;
1527 }
1528
Winson Chung93eef082012-03-23 15:59:27 -07001529 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1530 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1531 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001532 }
1533
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001534 public LauncherAppWidgetHost getAppWidgetHost() {
1535 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Winson Chunga9abd0e2010-10-27 17:18:37 -07001538 public LauncherModel getModel() {
1539 return mModel;
1540 }
1541
Bjorn Bringertc459e522013-06-07 19:36:01 +01001542 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001543 getWindow().closeAllPanels();
1544
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001545 // Whatever we were doing is hereby canceled.
1546 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 @Override
1550 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001551 long startTime = 0;
1552 if (DEBUG_RESUME_TIME) {
1553 startTime = System.currentTimeMillis();
1554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 super.onNewIntent(intent);
1556
1557 // Close the menu
1558 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001559 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001560 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001561
Jamie Genniscb222e82012-10-23 15:44:26 -07001562 final boolean alreadyOnHome =
1563 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001564 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001565
Jamie Genniscb222e82012-10-23 15:44:26 -07001566 Runnable processIntent = new Runnable() {
1567 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001568 if (mWorkspace == null) {
1569 // Can be cases where mWorkspace is null, this prevents a NPE
1570 return;
1571 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001572 Folder openFolder = mWorkspace.getOpenFolder();
1573 // In all these cases, only animate if we're already on home
1574 mWorkspace.exitWidgetResizeMode();
1575 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1576 openFolder == null) {
1577 mWorkspace.moveToDefaultScreen(true);
1578 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001579
Jamie Genniscb222e82012-10-23 15:44:26 -07001580 closeFolder();
1581 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001582
Jamie Genniscb222e82012-10-23 15:44:26 -07001583 // If we are already on home, then just animate back to the workspace,
1584 // otherwise, just wait until onResume to set the state back to Workspace
1585 if (alreadyOnHome) {
1586 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001587 if (mWorkspace.isInOverviewMode()) {
1588 mWorkspace.exitOverviewMode();
1589 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001590 } else {
1591 mOnResumeState = State.WORKSPACE;
1592 }
1593
1594 final View v = getWindow().peekDecorView();
1595 if (v != null && v.getWindowToken() != null) {
1596 InputMethodManager imm = (InputMethodManager)getSystemService(
1597 INPUT_METHOD_SERVICE);
1598 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1599 }
1600
1601 // Reset AllApps to its initial state
1602 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1603 mAppsCustomizeTabHost.reset();
1604 }
1605 }
1606 };
1607
1608 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1609 // Delay processing of the intent to allow the status bar animation to finish
1610 // first in order to avoid janky animations.
1611 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001612 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001613 // Process the intent immediately.
1614 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001615 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 }
Michael Jurka447bf842013-05-15 14:52:15 +02001618 if (DEBUG_RESUME_TIME) {
1619 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
1622
1623 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001624 public void onRestoreInstanceState(Bundle state) {
1625 super.onRestoreInstanceState(state);
1626 for (int page: mSynchronouslyBoundPages) {
1627 mWorkspace.restoreInstanceStateForChild(page);
1628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630
1631 @Override
1632 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001633 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001634 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635
Michael Jurka883f55b2010-10-21 15:47:14 -07001636 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001637 // We close any open folder since it will not be re-opened, and we need to make sure
1638 // this state is reflected.
1639 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
Adam Cohendcd297f2013-06-18 13:13:40 -07001641 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001642 mWaitingForResult) {
1643 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001644 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001645 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1646 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001647 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1648 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1649 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
1651
1652 if (mFolderInfo != null && mWaitingForResult) {
1653 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1654 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1655 }
Michael Jurka946ad472010-07-09 18:05:18 -07001656
Winson Chung785d2eb2011-04-14 16:08:02 -07001657 // Save the current AppsCustomize tab
1658 if (mAppsCustomizeTabHost != null) {
1659 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1660 if (currentTabTag != null) {
1661 outState.putString("apps_customize_currentTab", currentTabTag);
1662 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001663 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1664 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 }
1667
1668 @Override
1669 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001671
Winson Chunge7a03942011-08-05 15:05:12 -07001672 // Remove all pending runnables
1673 mHandler.removeMessages(ADVANCE_MSG);
1674 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001675 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001676
Winson Chungcd2b0142011-06-08 16:02:26 -07001677 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001678 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001679 mModel.stopLoader();
1680 app.setLauncher(null);
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001683 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001685 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001687 mAppWidgetHost = null;
1688
1689 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690
1691 TextKeyListener.getInstance().release();
1692
Winson Chung81b52252012-08-27 15:34:29 -07001693 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1694 // to prevent leaking Launcher activities on orientation change.
1695 if (mModel != null) {
1696 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1697 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001698
1699 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001700 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001701
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001702 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001703 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1704 mWorkspace.removeAllViews();
1705 mWorkspace = null;
1706 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001707
Michael Jurka2ecf9952012-06-18 12:52:28 -07001708 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
1710
Adam Cohena9cf38f2011-05-02 15:36:58 -07001711 public DragController getDragController() {
1712 return mDragController;
1713 }
1714
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 @Override
1716 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001717 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 super.startActivityForResult(intent, requestCode);
1719 }
1720
Winson Chung70d72102011-08-12 11:24:35 -07001721 /**
1722 * Indicates that we want global search for this activity by setting the globalSearch
1723 * argument for {@link #startSearch} to true.
1724 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001726 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001728
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001729 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Karl Rosaen138a0412009-04-23 19:00:21 -07001731 if (initialQuery == null) {
1732 // Use any text typed in the launcher as the initial query
1733 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 if (appSearchData == null) {
1736 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001737 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 }
Winson Chungadf0c182012-08-23 14:59:07 -07001739 Rect sourceBounds = new Rect();
1740 if (mSearchDropTargetBar != null) {
1741 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1742 }
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Bjorn Bringertc459e522013-06-07 19:36:01 +01001744 startSearch(initialQuery, selectInitialQuery,
1745 appSearchData, sourceBounds);
1746 }
1747
1748 public void startSearch(String initialQuery,
1749 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001750 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001751 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001752 }
1753
1754 /**
1755 * Starts the global search activity. This code is a copied from SearchManager
1756 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001757 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001758 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001759 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001760 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1761 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1762 if (globalSearchActivity == null) {
1763 Log.w(TAG, "No global search activity found.");
1764 return;
1765 }
1766 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1767 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1768 intent.setComponent(globalSearchActivity);
1769 // Make sure that we have a Bundle to put source in
1770 if (appSearchData == null) {
1771 appSearchData = new Bundle();
1772 } else {
1773 appSearchData = new Bundle(appSearchData);
1774 }
1775 // Set source to package name of app that starts global search, if not set already.
1776 if (!appSearchData.containsKey("source")) {
1777 appSearchData.putString("source", getPackageName());
1778 }
1779 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1780 if (!TextUtils.isEmpty(initialQuery)) {
1781 intent.putExtra(SearchManager.QUERY, initialQuery);
1782 }
1783 if (selectInitialQuery) {
1784 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1785 }
1786 intent.setSourceBounds(sourceBounds);
1787 try {
1788 startActivity(intent);
1789 } catch (ActivityNotFoundException ex) {
1790 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
1793
Winson Chung70d72102011-08-12 11:24:35 -07001794 @Override
1795 public boolean onCreateOptionsMenu(Menu menu) {
1796 if (isWorkspaceLocked()) {
1797 return false;
1798 }
1799
1800 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001801
1802 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1803 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1804 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001805 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1806 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1807 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001808 String helpUrl = getString(R.string.help_url);
1809 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001810 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1811 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1812
Winson Chung70d72102011-08-12 11:24:35 -07001813 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1814 .setIcon(android.R.drawable.ic_menu_gallery)
1815 .setAlphabeticShortcut('W');
1816 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1817 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001818 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001819 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001820 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1821 .setIcon(android.R.drawable.ic_menu_preferences)
1822 .setIntent(settings)
1823 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001824 if (!helpUrl.isEmpty()) {
1825 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1826 .setIcon(android.R.drawable.ic_menu_help)
1827 .setIntent(help)
1828 .setAlphabeticShortcut('H');
1829 }
Winson Chung70d72102011-08-12 11:24:35 -07001830 return true;
1831 }
1832
1833 @Override
1834 public boolean onPrepareOptionsMenu(Menu menu) {
1835 super.onPrepareOptionsMenu(menu);
1836
1837 if (mAppsCustomizeTabHost.isTransitioning()) {
1838 return false;
1839 }
1840 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1841 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1842
1843 return true;
1844 }
1845
1846 @Override
1847 public boolean onOptionsItemSelected(MenuItem item) {
1848 switch (item.getItemId()) {
1849 case MENU_WALLPAPER_SETTINGS:
1850 startWallpaper();
1851 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001852 }
1853
1854 return super.onOptionsItemSelected(item);
1855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001857 @Override
1858 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001859 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001860 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001861 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001862 }
1863
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 public boolean isWorkspaceLocked() {
1865 return mWorkspaceLoading || mWaitingForResult;
1866 }
1867
Michael Jurka0280c3b2010-09-17 15:00:07 -07001868 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001870 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001871 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1872 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001873 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001874 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001875 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001876
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001877 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1878 AppWidgetProviderInfo appWidgetInfo) {
1879 if (appWidgetInfo.configure != null) {
1880 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001881
Bjorn Bringert7984c942009-12-09 15:38:25 +00001882 // Launch over to configure widget, if needed
1883 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001884 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001885 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001886 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001888 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001889 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001890 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001891 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001892 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
1894 }
Romain Guycbb89e42009-06-08 15:52:54 -07001895
Adam Cohenfbba09b2011-07-18 21:43:05 -07001896 /**
1897 * Process a shortcut drop.
1898 *
1899 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001901 * @param cell The cell it should be added to, optional
1902 * @param position The location on the screen where it was dropped, optional
1903 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001904 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001908 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001910
1911 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 mPendingAddInfo.cellX = cell[0];
1913 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001914 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001915
1916 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1917 createShortcutIntent.setComponent(componentName);
1918 processShortcut(createShortcutIntent);
1919 }
1920
Adam Cohenfbba09b2011-07-18 21:43:05 -07001921 /**
1922 * Process a widget drop.
1923 *
1924 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001925 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001926 * @param cell The cell it should be added to, optional
1927 * @param position The location on the screen where it was dropped, optional
1928 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001929 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001930 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001931 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001935 mPendingAddInfo.minSpanX = info.minSpanX;
1936 mPendingAddInfo.minSpanY = info.minSpanY;
1937
Adam Cohenfbba09b2011-07-18 21:43:05 -07001938 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 mPendingAddInfo.cellX = cell[0];
1940 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001941 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001942 if (span != null) {
1943 mPendingAddInfo.spanX = span[0];
1944 mPendingAddInfo.spanY = span[1];
1945 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001946
Adam Cohened66b2b2012-01-23 17:28:51 -08001947 AppWidgetHostView hostView = info.boundWidget;
1948 int appWidgetId;
1949 if (hostView != null) {
1950 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001951 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001952 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001953 // In this case, we either need to start an activity to get permission to bind
1954 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001955 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001956 Bundle options = info.bindOptions;
1957
1958 boolean success = false;
1959 if (options != null) {
1960 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1961 info.componentName, options);
1962 } else {
1963 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1964 info.componentName);
1965 }
1966 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001968 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001969 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001970 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1971 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1972 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001973 // TODO: we need to make sure that this accounts for the options bundle.
1974 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001975 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1976 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001977 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001978 }
1979
Joe Onoratodeb98af2010-02-19 14:59:39 -08001980 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001981 // Handle case where user selected "Applications"
1982 String applicationName = getResources().getString(R.string.group_applications);
1983 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001985 if (applicationName != null && applicationName.equals(shortcutName)) {
1986 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1987 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001988
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001989 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1990 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001991 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001992 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001994 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
1997
Winson Chung24ab2f12010-09-16 14:10:47 -07001998 void processWallpaper(Intent intent) {
1999 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2000 }
2001
Adam Cohendcd297f2013-06-18 13:13:40 -07002002 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002003 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002004 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 folderInfo.title = getText(R.string.folder_name);
2006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002008 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002009 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002010 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011
2012 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002013 FolderIcon newFolder =
2014 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002016 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07002017 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 }
Romain Guycbb89e42009-06-08 15:52:54 -07002019
Joe Onorato9c1289c2009-08-17 11:03:03 -04002020 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002021 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002022 }
2023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002025 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07002027 Intent chooser = Intent.createChooser(pickWallpaper,
2028 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002029 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2030 // Removed in Eclair MR1
2031// WallpaperManager wm = (WallpaperManager)
2032// getSystemService(Context.WALLPAPER_SERVICE);
2033// WallpaperInfo wi = wm.getWallpaperInfo();
2034// if (wi != null && wi.getSettingsActivity() != null) {
2035// LabeledIntent li = new LabeledIntent(getPackageName(),
2036// R.string.configure_wallpaper, 0);
2037// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2038// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2039// }
Mike Clerona0618e42009-10-22 13:55:21 -07002040 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002043 /**
2044 * Registers various content observers. The current implementation registers
2045 * only a favorites observer to keep track of the favorites applications.
2046 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002047 private void registerContentObservers() {
2048 ContentResolver resolver = getContentResolver();
2049 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2050 true, mWidgetObserver);
2051 }
2052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 @Override
2054 public boolean dispatchKeyEvent(KeyEvent event) {
2055 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2056 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002058 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002059 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002060 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002061 dumpState();
2062 return true;
2063 }
2064 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002065 }
2066 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2067 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002068 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 return true;
2070 }
2071 }
2072
2073 return super.dispatchKeyEvent(event);
2074 }
2075
Joe Onorato88ec0992009-11-19 13:16:06 -08002076 @Override
2077 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002078 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002079 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002080 } else if (mWorkspace.isInOverviewMode()) {
2081 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002082 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002083 Folder openFolder = mWorkspace.getOpenFolder();
2084 if (openFolder.isEditingName()) {
2085 openFolder.dismissEditingName();
2086 } else {
2087 closeFolder();
2088 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002089 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002090 mWorkspace.exitWidgetResizeMode();
2091
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002092 // Back button is a no-op here, but give at least some feedback for the button press
2093 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002094 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002095 }
2096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002098 * Re-listen when widgets are reset.
2099 */
2100 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002101 if (mAppWidgetHost != null) {
2102 mAppWidgetHost.startListening();
2103 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002104 }
2105
2106 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 * Launches the intent referred by the clicked shortcut.
2108 *
2109 * @param v The view representing the clicked shortcut.
2110 */
2111 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002112 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2113 // view has detached (it's possible for this to happen if the view is removed mid touch).
2114 if (v.getWindowToken() == null) {
2115 return;
2116 }
2117
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002118 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002119 return;
2120 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002121
Adam Cohenf358a4b2013-07-23 16:47:31 -07002122 if (v instanceof PageIndicator) {
2123 if (!mWorkspace.isInOverviewMode()) {
2124 mWorkspace.enterOverviewMode();
2125 }
2126 return;
2127 }
2128
2129 if (v instanceof CellLayout) {
2130 if (mWorkspace.isInOverviewMode()) {
2131 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2132 }
2133 }
2134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002136 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002138 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2139 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002140
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002141 // Check for special shortcuts
2142 if (intent.getComponent() != null) {
2143 final String shortcutClass = intent.getComponent().getClassName();
2144
2145 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2146 showAllApps(true);
2147 return;
2148 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2149 MemoryDumpActivity.startDump(this);
2150 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002151 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2152 toggleShowWeightWatcher();
2153 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002154 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002155 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002156
2157 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002158 int[] pos = new int[2];
2159 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002160 intent.setSourceBounds(new Rect(pos[0], pos[1],
2161 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002162
2163 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002164
Daniel Sandlerff02d492013-08-05 02:12:05 -04002165 mStats.recordLaunch(intent, shortcut);
2166
Michael Jurkaddd62e92011-02-16 17:49:14 -08002167 if (success && v instanceof BubbleTextView) {
2168 mWaitingForResume = (BubbleTextView) v;
2169 mWaitingForResume.setStayPressed(true);
2170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002172 if (v instanceof FolderIcon) {
2173 FolderIcon fi = (FolderIcon) v;
2174 handleFolderClick(fi);
2175 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002176 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002177 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002178 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002179 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002180 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 }
2183 }
2184
Michael Jurka0e260592010-06-30 17:07:39 -07002185 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002186 return false;
2187 }
2188
Michael Jurkaaf442092010-06-10 17:01:57 -07002189 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002190 * Event handler for the search button
2191 *
2192 * @param v The view that was clicked.
2193 */
2194 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002195 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2196
Amith Yamasania135ba82011-08-09 17:42:01 -07002197 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002198 }
2199
2200 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002201 * Event handler for the voice button
2202 *
2203 * @param v The view that was clicked.
2204 */
2205 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002206 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002207
Bjorn Bringertc459e522013-06-07 19:36:01 +01002208 startVoice();
2209 }
2210
2211 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002212 try {
2213 final SearchManager searchManager =
2214 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2215 ComponentName activityName = searchManager.getGlobalSearchActivity();
2216 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002217 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002218 if (activityName != null) {
2219 intent.setPackage(activityName.getPackageName());
2220 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002221 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002222 } catch (ActivityNotFoundException e) {
2223 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002224 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002225 startActivitySafely(null, intent, "onClickVoiceButton");
2226 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002227 }
2228
2229 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002230 * Event handler for the "grid" button that appears on the home screen, which
2231 * enters all apps mode.
2232 *
2233 * @param v The view that was clicked.
2234 */
2235 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002236 showAllApps(true);
2237 }
2238
2239 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002240 // Provide the same haptic feedback that the system offers for virtual keys.
2241 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002242 }
2243
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002244 public void onClickAppMarketButton(View v) {
2245 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002246 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002247 } else {
2248 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002249 }
2250 }
2251
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002252 void startApplicationDetailsActivity(ComponentName componentName) {
2253 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002254 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2255 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002256 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002257 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002258 }
2259
Michael Jurka1e2f4652013-07-08 18:03:46 -07002260 // returns true if the activity was started
2261 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2262 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002263 // System applications cannot be installed. For now, show a toast explaining that.
2264 // We may give them the option of disabling apps this way.
2265 int messageId = R.string.uninstall_system_app_text;
2266 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002267 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002268 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002269 String packageName = componentName.getPackageName();
2270 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002271 Intent intent = new Intent(
2272 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002273 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2274 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002275 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002276 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002277 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002278 }
2279
Michael Jurka86a720e2012-05-09 11:23:23 -07002280 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002284 // Only launch using the new animation if the shortcut has not opted out (this is a
2285 // private contract between launcher and may be ignored in the future).
2286 boolean useLaunchAnimation = (v != null) &&
2287 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2288 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002289 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2290 v.getMeasuredWidth(), v.getMeasuredHeight());
2291
2292 startActivity(intent, opts.toBundle());
2293 } else {
2294 startActivity(intent);
2295 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002296 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 } catch (SecurityException e) {
2298 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002299 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002301 "or use the exported attribute for this activity. "
2302 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002304 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002306
Michael Jurka86a720e2012-05-09 11:23:23 -07002307 boolean startActivitySafely(View v, Intent intent, Object tag) {
2308 boolean success = false;
2309 try {
2310 success = startActivity(v, intent, tag);
2311 } catch (ActivityNotFoundException e) {
2312 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2313 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2314 }
2315 return success;
2316 }
2317
Romain Guy8e633c52010-03-04 12:51:36 -08002318 void startActivityForResultSafely(Intent intent, int requestCode) {
2319 try {
2320 startActivityForResult(intent, requestCode);
2321 } catch (ActivityNotFoundException e) {
2322 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2323 } catch (SecurityException e) {
2324 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2325 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2326 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2327 "or use the exported attribute for this activity.", e);
2328 }
2329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330
Adam Cohena9cf38f2011-05-02 15:36:58 -07002331 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002332 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002333 Folder openFolder = mWorkspace.getFolderForTag(info);
2334
2335 // If the folder info reports that the associated folder is open, then verify that
2336 // it is actually opened. There have been a few instances where this gets out of sync.
2337 if (info.opened && openFolder == null) {
2338 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002340 info.opened = false;
2341 }
2342
Adam Cohenfb91f302012-06-11 15:45:18 -07002343 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344 // Close any open folder
2345 closeFolder();
2346 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002347 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 } else {
2349 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 int folderScreen;
2351 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002352 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 // .. and close it
2354 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002355 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 // Close any folder open on the current screen
2357 closeFolder();
2358 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002359 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 }
2361 }
2362 }
2363 }
2364
Adam Cohen268c4752012-06-06 17:47:33 -07002365 /**
2366 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2367 * in the DragLayer in the exact absolute location of the original FolderIcon.
2368 */
2369 private void copyFolderIconToImage(FolderIcon fi) {
2370 final int width = fi.getMeasuredWidth();
2371 final int height = fi.getMeasuredHeight();
2372
2373 // Lazy load ImageView, Bitmap and Canvas
2374 if (mFolderIconImageView == null) {
2375 mFolderIconImageView = new ImageView(this);
2376 }
2377 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2378 mFolderIconBitmap.getHeight() != height) {
2379 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2380 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2381 }
2382
2383 DragLayer.LayoutParams lp;
2384 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2385 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2386 } else {
2387 lp = new DragLayer.LayoutParams(width, height);
2388 }
2389
Adam Cohen307fe232012-08-16 17:55:58 -07002390 // The layout from which the folder is being opened may be scaled, adjust the starting
2391 // view size by this scale factor.
2392 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002393 lp.customPosition = true;
2394 lp.x = mRectForFolderAnimation.left;
2395 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002396 lp.width = (int) (scale * width);
2397 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002398
2399 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2400 fi.draw(mFolderIconCanvas);
2401 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002402 if (fi.getFolder() != null) {
2403 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2404 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002405 }
Adam Cohen268c4752012-06-06 17:47:33 -07002406 // Just in case this image view is still in the drag layer from a previous animation,
2407 // we remove it and re-add it.
2408 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2409 mDragLayer.removeView(mFolderIconImageView);
2410 }
2411 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002412 if (fi.getFolder() != null) {
2413 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002414 }
Adam Cohen268c4752012-06-06 17:47:33 -07002415 }
2416
Adam Cohen2801caf2011-05-13 20:57:39 -07002417 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002418 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002419 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2420 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2421 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2422
Adam Cohenc51934b2011-07-26 21:07:43 -07002423 FolderInfo info = (FolderInfo) fi.getTag();
2424 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2425 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002426 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2427 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002428 }
2429
Adam Cohen268c4752012-06-06 17:47:33 -07002430 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2431 copyFolderIconToImage(fi);
2432 fi.setVisibility(View.INVISIBLE);
2433
Michael Jurka2ecf9952012-06-18 12:52:28 -07002434 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002435 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002436 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2437 oa.start();
2438 }
2439
Adam Cohen268c4752012-06-06 17:47:33 -07002440 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002441 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002442 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2443 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2444 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2445
Adam Cohen268c4752012-06-06 17:47:33 -07002446 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002447
Adam Cohen268c4752012-06-06 17:47:33 -07002448 // We remove and re-draw the FolderIcon in-case it has changed
2449 mDragLayer.removeView(mFolderIconImageView);
2450 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002451 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002452 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002453 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002454 oa.addListener(new AnimatorListenerAdapter() {
2455 @Override
2456 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002457 if (cl != null) {
2458 cl.clearFolderLeaveBehind();
2459 // Remove the ImageView copy of the FolderIcon and make the original visible.
2460 mDragLayer.removeView(mFolderIconImageView);
2461 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002462 }
2463 }
2464 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002465 oa.start();
2466 }
2467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002469 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 * is animated relative to the specified View. If the View is null, no animation
2471 * is played.
2472 *
2473 * @param folderInfo The FolderInfo describing the folder to open.
2474 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002475 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002476 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002477 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478
Adam Cohena9cf38f2011-05-02 15:36:58 -07002479 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480
Adam Cohen4554ee12011-08-03 16:13:21 -07002481 // Just verify that the folder hasn't already been added to the DragLayer.
2482 // There was a one-off crash where the folder had a parent already.
2483 if (folder.getParent() == null) {
2484 mDragLayer.addView(folder);
2485 mDragController.addDropTarget((DropTarget) folder);
2486 } else {
2487 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2488 folder.getParent() + ").");
2489 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002490 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002491 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002492
2493 // Notify the accessibility manager that this folder "window" has appeared and occluded
2494 // the workspace items
2495 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2496 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002497 }
2498
2499 public void closeFolder() {
2500 Folder folder = mWorkspace.getOpenFolder();
2501 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002502 if (folder.isEditingName()) {
2503 folder.dismissEditingName();
2504 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002505 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002506
2507 // Dismiss the folder cling
2508 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002509 }
2510 }
2511
2512 void closeFolder(Folder folder) {
2513 folder.getInfo().opened = false;
2514
2515 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2516 if (parent != null) {
2517 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2518 shrinkAndFadeInFolderIcon(fi);
2519 }
2520 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002521
2522 // Notify the accessibility manager that this folder "window" has disappeard and no
2523 // longer occludeds the workspace items
2524 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 }
2526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002528 if (!isDraggingEnabled()) return false;
2529 if (isWorkspaceLocked()) return false;
2530 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531
2532 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002533 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 }
2535
Michael Jurka0280c3b2010-09-17 15:00:07 -07002536 resetAddInfo();
2537 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002539 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 return true;
2541 }
2542
Winson Chung3d503fb2011-07-13 17:25:49 -07002543 // The hotseat touch handling does not go through Workspace, and we always allow long press
2544 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002545 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002546 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2547 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002548 if (itemUnderLongClick == null) {
2549 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002550 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2551 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002552 // Disabling reordering until we sort out some issues.
Adam Cohenf358a4b2013-07-23 16:47:31 -07002553 if (mWorkspace.isInOverviewMode()) {
2554 mWorkspace.startReordering(v);
Adam Cohendedbd962013-07-11 14:21:49 -07002555 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002556 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002559 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002561 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 }
2563 }
2564 }
2565 return true;
2566 }
2567
Winson Chung3d503fb2011-07-13 17:25:49 -07002568 boolean isHotseatLayout(View layout) {
2569 return mHotseat != null && layout != null &&
2570 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2571 }
2572 Hotseat getHotseat() {
2573 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002574 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002575 View getOverviewPanel() {
2576 return mOverviewPanel;
2577 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002578 SearchDropTargetBar getSearchBar() {
2579 return mSearchDropTargetBar;
2580 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002581
Winson Chung3d503fb2011-07-13 17:25:49 -07002582 /**
2583 * Returns the CellLayout of the specified container at the specified screen.
2584 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002585 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002586 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2587 if (mHotseat != null) {
2588 return mHotseat.getLayout();
2589 } else {
2590 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002591 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002592 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002593 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002594 }
2595 }
2596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 Workspace getWorkspace() {
2598 return mWorkspace;
2599 }
2600
Daniel Sandler843e8602010-06-07 14:59:01 -04002601 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002602 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002603 }
2604
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 /**
2606 * Helper method for the cameraZoomIn/cameraZoomOut animations
2607 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 * @param scaleFactor The scale factor used for the zoom
2609 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002612 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002614
Winson Chung18f41f82012-05-09 13:28:10 -07002615 void disableWallpaperIfInAllApps() {
2616 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002617 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002618 if (mAppsCustomizeTabHost != null &&
2619 !mAppsCustomizeTabHost.isTransitioning()) {
2620 updateWallpaperVisibility(false);
2621 }
2622 }
2623 }
2624
Craig Mautner360310b2012-10-26 15:13:08 -07002625 private void setWorkspaceBackground(boolean workspace) {
2626 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002627 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002628 }
2629
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002630 void updateWallpaperVisibility(boolean visible) {
2631 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2632 int curflags = getWindow().getAttributes().flags
2633 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2634 if (wpflags != curflags) {
2635 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2636 }
Craig Mautner360310b2012-10-26 15:13:08 -07002637 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002638 }
2639
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002640 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2641 if (v instanceof LauncherTransitionable) {
2642 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2643 }
2644 }
2645
Michael Jurkabed61d22012-02-14 22:51:29 -08002646 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2647 if (v instanceof LauncherTransitionable) {
2648 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2649 }
Winson Chung70442722012-02-10 15:43:22 -08002650
2651 // Update the workspace transition step as well
2652 dispatchOnLauncherTransitionStep(v, 0f);
2653 }
2654
2655 private void dispatchOnLauncherTransitionStep(View v, float t) {
2656 if (v instanceof LauncherTransitionable) {
2657 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2658 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002659 }
2660
2661 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2662 if (v instanceof LauncherTransitionable) {
2663 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2664 }
Winson Chung70442722012-02-10 15:43:22 -08002665
2666 // Update the workspace transition step as well
2667 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002668 }
2669
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002670 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002671 * Things to test when changing the following seven functions.
2672 * - Home from workspace
2673 * - from center screen
2674 * - from other screens
2675 * - Home from all apps
2676 * - from center screen
2677 * - from other screens
2678 * - Back from all apps
2679 * - from center screen
2680 * - from other screens
2681 * - Launch app from workspace and quit
2682 * - with back
2683 * - with home
2684 * - Launch app from all apps and quit
2685 * - with back
2686 * - with home
2687 * - Go to a screen that's not the default, then all
2688 * apps, and launch and app, and go back
2689 * - with back
2690 * -with home
2691 * - On workspace, long press power and go back
2692 * - with back
2693 * - with home
2694 * - On all apps, long press power and go back
2695 * - with back
2696 * - with home
2697 * - On workspace, power off
2698 * - On all apps, power off
2699 * - Launch an app and turn off the screen while in that app
2700 * - Go back with home key
2701 * - Go back with back key TODO: make this not go to workspace
2702 * - From all apps
2703 * - From workspace
2704 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2705 * - From all apps
2706 * - From the center workspace
2707 * - From another workspace
2708 */
2709
2710 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002711 * Zoom the camera out from the workspace to reveal 'toView'.
2712 * Assumes that the view to show is anchored at either the very top or very bottom
2713 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002714 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002715 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002716 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002717 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002718 mStateAnimation.cancel();
2719 mStateAnimation = null;
2720 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002721 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002722
Winson Chungf0ea4d32011-06-06 14:27:16 -07002723 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2724 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2725 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002726 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002727 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002728 final int startDelay =
2729 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002730
Michael Jurkab3e22d92011-10-31 15:58:33 -07002731 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732
Michael Jurkad74c9842011-07-10 12:44:21 -07002733 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002734 Animator workspaceAnim =
2735 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736
2737 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002738 toView.setScaleX(scale);
2739 toView.setScaleY(scale);
2740 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2741 scaleAnim.
2742 scaleX(1f).scaleY(1f).
2743 setDuration(duration).
2744 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002745
Winson Chungf0ea4d32011-06-06 14:27:16 -07002746 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002747 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002748 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002749 .ofFloat(toView, "alpha", 0f, 1f)
2750 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002752 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2753 @Override
2754 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002755 if (animation == null) {
2756 throw new RuntimeException("animation is null");
2757 }
Winson Chung70442722012-02-10 15:43:22 -08002758 float t = (Float) animation.getAnimatedValue();
2759 dispatchOnLauncherTransitionStep(fromView, t);
2760 dispatchOnLauncherTransitionStep(toView, t);
2761 }
2762 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002763
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002764 // toView should appear right at the end of the workspace shrink
2765 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002766 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002767 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002768 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002769
2770 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002771 boolean animationCancelled = false;
2772
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002773 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002774 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002775 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 // Prepare the position
2777 toView.setTranslationX(0.0f);
2778 toView.setTranslationY(0.0f);
2779 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002780 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002782 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002783 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002784 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2785 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002786
Daniel Sandlere4f98912013-06-25 15:13:26 -04002787 if (mWorkspace != null
2788 && !springLoaded
2789 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002790 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002791 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002792 if (!animationCancelled) {
2793 updateWallpaperVisibility(false);
2794 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002795
2796 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002797 if (mSearchDropTargetBar != null) {
2798 mSearchDropTargetBar.hideSearchBar(false);
2799 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002800 }
2801
Adam Cohencff6af82011-09-13 14:51:53 -07002802 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002803 public void onAnimationCancel(Animator animation) {
2804 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002805 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 });
2807
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002808 if (workspaceAnim != null) {
2809 mStateAnimation.play(workspaceAnim);
2810 }
Michael Jurka1899a362011-11-03 13:50:45 -07002811
2812 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002813
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002814 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2815 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002816
2817 // If any of the objects being animated haven't been measured/laid out
2818 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002819 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002820 (mWorkspace.getMeasuredWidth() == 0) ||
2821 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002822 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002823 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002824
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002825 final AnimatorSet stateAnimation = mStateAnimation;
2826 final Runnable startAnimRunnable = new Runnable() {
2827 public void run() {
2828 // Check that mStateAnimation hasn't changed while
2829 // we waited for a layout/draw pass
2830 if (mStateAnimation != stateAnimation)
2831 return;
2832 setPivotsForZoom(toView, scale);
2833 dispatchOnLauncherTransitionStart(fromView, animated, false);
2834 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002835 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002836 }
2837 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002839 final ViewTreeObserver observer = toView.getViewTreeObserver();
2840 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2841 public void onGlobalLayout() {
2842 startAnimRunnable.run();
2843 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2844 }
2845 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002847 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002848 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 } else {
2850 toView.setTranslationX(0.0f);
2851 toView.setTranslationY(0.0f);
2852 toView.setScaleX(1.0f);
2853 toView.setScaleY(1.0f);
2854 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002855 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002856
Daniel Sandlere4f98912013-06-25 15:13:26 -04002857 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002859
2860 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002861 if (mSearchDropTargetBar != null) {
2862 mSearchDropTargetBar.hideSearchBar(false);
2863 }
Michael Jurkaabded662011-03-04 12:06:57 -08002864 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002865 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002866 dispatchOnLauncherTransitionStart(fromView, animated, false);
2867 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002868 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002869 dispatchOnLauncherTransitionStart(toView, animated, false);
2870 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002871 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002873 }
2874
2875 /**
2876 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002877 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002878 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002879 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002880 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2881 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002882
Michael Jurkab3e22d92011-10-31 15:58:33 -07002883 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002884 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002885 mStateAnimation.cancel();
2886 mStateAnimation = null;
2887 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002888 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002889
Winson Chungf0ea4d32011-06-06 14:27:16 -07002890 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002891 final int fadeOutDuration =
2892 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002893 final float scaleFactor = (float)
2894 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2895 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002896 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002897 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002898 if (toState == State.WORKSPACE) {
2899 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2900 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002901 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002902 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2903 workspaceAnim = mWorkspace.getChangeStateAnimation(
2904 Workspace.State.SPRING_LOADED, animated);
2905 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002906
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002908 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002909 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002910 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002911 final LauncherViewPropertyAnimator scaleAnim =
2912 new LauncherViewPropertyAnimator(fromView);
2913 scaleAnim.
2914 scaleX(scaleFactor).scaleY(scaleFactor).
2915 setDuration(duration).
2916 setInterpolator(new Workspace.ZoomInInterpolator());
2917
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002918 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002919 .ofFloat(fromView, "alpha", 1f, 0f)
2920 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002921 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002922 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2923 @Override
2924 public void onAnimationUpdate(ValueAnimator animation) {
2925 float t = 1f - (Float) animation.getAnimatedValue();
2926 dispatchOnLauncherTransitionStep(fromView, t);
2927 dispatchOnLauncherTransitionStep(toView, t);
2928 }
2929 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002930
Michael Jurka2ecf9952012-06-18 12:52:28 -07002931 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002932
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002933 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2934 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002935 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002936
2937 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002938 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002939 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002940 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002941 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002942 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2943 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002944 if (onCompleteRunnable != null) {
2945 onCompleteRunnable.run();
2946 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002947 mAppsCustomizeContent.updateCurrentPageScroll();
2948 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002949 }
2950 });
2951
Winson Chungf0ea4d32011-06-06 14:27:16 -07002952 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002953 if (workspaceAnim != null) {
2954 mStateAnimation.play(workspaceAnim);
2955 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002956 dispatchOnLauncherTransitionStart(fromView, animated, true);
2957 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002958 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002959 } else {
2960 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002961 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002962 dispatchOnLauncherTransitionStart(fromView, animated, true);
2963 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002964 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002965 dispatchOnLauncherTransitionStart(toView, animated, true);
2966 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002967 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002968 }
2969
Michael Jurkae326f182011-11-21 14:05:46 -08002970 @Override
2971 public void onTrimMemory(int level) {
2972 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002973 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002974 mAppsCustomizeTabHost.onTrimMemory();
2975 }
2976 }
2977
Winson Chung18f41f82012-05-09 13:28:10 -07002978 @Override
2979 public void onWindowFocusChanged(boolean hasFocus) {
2980 if (!hasFocus) {
2981 // When another window occludes launcher (like the notification shade, or recents),
2982 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2983 updateWallpaperVisibility(true);
2984 } else {
2985 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002986 mWorkspace.postDelayed(new Runnable() {
2987 @Override
2988 public void run() {
2989 disableWallpaperIfInAllApps();
2990 }
2991 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002992 }
2993 }
2994
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002995 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002996 showWorkspace(animated, null);
2997 }
2998
2999 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003001 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003002 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003003 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003004
Winson Chungc7d2b602012-05-16 17:02:20 -07003005 // Show the search bar (only animate if we were showing the drop target bar in spring
3006 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003007 if (mSearchDropTargetBar != null) {
3008 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3009 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003010
Michael Jurkab737ee62011-11-15 15:57:22 -08003011 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07003012 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003013
3014 // Set focus to the AppsCustomize button
3015 if (mAllAppsButton != null) {
3016 mAllAppsButton.requestFocus();
3017 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003018 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003019
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003020 // Change the state *after* we've called all the transition code
3021 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003022
Winson Chung5fb63472011-02-02 17:03:37 -08003023 // Resume the auto-advance of widgets
3024 mUserPresent = true;
3025 updateRunning();
3026
alanv1d4fde62012-10-17 13:15:47 -07003027 // Send an accessibility event to announce the context change
3028 getWindow().getDecorView()
3029 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003030
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003031 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003032 }
3033
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003034 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003035 }
3036
Michael Jurkab3e22d92011-10-31 15:58:33 -07003037 void showAllApps(boolean animated) {
3038 if (mState != State.WORKSPACE) return;
3039
3040 showAppsCustomizeHelper(animated, false);
3041 mAppsCustomizeTabHost.requestFocus();
3042
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 // Change the state *after* we've called all the transition code
3044 mState = State.APPS_CUSTOMIZE;
3045
3046 // Pause the auto-advance of widgets until we are out of AllApps
3047 mUserPresent = false;
3048 updateRunning();
3049 closeFolder();
3050
3051 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003052 getWindow().getDecorView()
3053 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003054 }
3055
Adam Cohen7777d962011-08-18 18:58:38 -07003056 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003057 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003058 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003059 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003060 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003061 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003062 }
Adam Cohen7777d962011-08-18 18:58:38 -07003063
Adam Cohened66b2b2012-01-23 17:28:51 -08003064 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3065 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003066 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3067
Winson Chunge7a03942011-08-05 15:05:12 -07003068 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003069 @Override
3070 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003071 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003072 // Before we show workspace, hide all apps again because
3073 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3074 // clean up our state transition functions
3075 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003076 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003077 } else {
3078 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003079 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003080 }
3081 }, (extendedDelay ?
3082 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3083 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3084 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003085
Michael Jurkad3ef3062010-11-23 16:23:58 -08003086 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003087 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003088 final boolean animated = true;
3089 final boolean springLoaded = true;
3090 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003091 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003092 }
3093 // Otherwise, we are not in spring loaded mode, so don't do anything.
3094 }
3095
Michael Jurkab737ee62011-11-15 15:57:22 -08003096 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003097 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003098 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003099 }
3100 }
3101
3102 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003103 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003104 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003105 if (mDividerAnimator != null) {
3106 mDividerAnimator.cancel();
3107 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003108 mDividerAnimator = null;
3109 }
3110 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003111 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003112 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003113 int duration = 0;
3114 if (mSearchDropTargetBar != null) {
3115 duration = mSearchDropTargetBar.getTransitionInDuration();
3116 }
3117 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003118 mDividerAnimator.start();
3119 }
3120 }
3121 }
3122
Michael Jurkab3e22d92011-10-31 15:58:33 -07003123 void lockAllApps() {
3124 // TODO
3125 }
3126
3127 void unlockAllApps() {
3128 // TODO
3129 }
3130
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003134 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003135 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003136 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003137 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003138 int duration = 0;
3139 if (mSearchDropTargetBar != null) {
3140 duration = mSearchDropTargetBar.getTransitionInDuration();
3141 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003142 mHotseat.animate().alpha(1f).setDuration(duration);
3143 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003144 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003145 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003146 }
3147 }
3148 }
3149
3150 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003152 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003153 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003154 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003155 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003156 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003157 int duration = 0;
3158 if (mSearchDropTargetBar != null) {
3159 duration = mSearchDropTargetBar.getTransitionOutDuration();
3160 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003161 mHotseat.animate().alpha(0f).setDuration(duration);
3162 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003163 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003165 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003166 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003167 }
3168
Patrick Dubroy5f445422011-02-18 14:35:21 -08003169 /**
3170 * Add an item from all apps or customize onto the given workspace screen.
3171 * If layout is null, add to the current screen.
3172 */
3173 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003174 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003175 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003176 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003177 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003178
Winson Chungdff8ebb2011-09-08 17:25:31 -07003179 /** Maps the current orientation to an index for referencing orientation correct global icons */
3180 private int getCurrentOrientationIndexForGlobalIcons() {
3181 // default - 0, landscape - 1
3182 switch (getResources().getConfiguration().orientation) {
3183 case Configuration.ORIENTATION_LANDSCAPE:
3184 return 1;
3185 default:
3186 return 0;
3187 }
3188 }
3189
Michael Jurkaae65ee32012-04-30 11:45:54 -07003190 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003192 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003193 // Look for the toolbar icon specified in the activity meta-data
3194 Bundle metaData = packageManager.getActivityInfo(
3195 activityName, PackageManager.GET_META_DATA).metaData;
3196 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003197 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003198 if (iconResId != 0) {
3199 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003200 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003201 }
3202 }
3203 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003204 // This can happen if the activity defines an invalid drawable
3205 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3206 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003207 } catch (Resources.NotFoundException nfe) {
3208 // This can happen if the activity defines an invalid drawable
3209 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3210 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003211 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003212 return null;
3213 }
3214
3215 // if successful in getting icon, return it; otherwise, set button to use default drawable
3216 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003217 int buttonId, ComponentName activityName, int fallbackDrawableId,
3218 String toolbarResourceName) {
3219 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003220 Resources r = getResources();
3221 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3222 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003223
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003224 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003225 // If we were unable to find the icon via the meta-data, use a generic one
3226 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003227 toolbarIcon = r.getDrawable(fallbackDrawableId);
3228 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003229 if (button != null) {
3230 button.setCompoundDrawables(toolbarIcon, null, null, null);
3231 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232 return null;
3233 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003234 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003235 if (button != null) {
3236 button.setCompoundDrawables(toolbarIcon, null, null, null);
3237 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003238 return toolbarIcon.getConstantState();
3239 }
3240 }
3241
3242 // if successful in getting icon, return it; otherwise, set button to use default drawable
3243 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003244 int buttonId, ComponentName activityName, int fallbackDrawableId,
3245 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003246 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003247 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003248
Michael Jurka19e0fc52011-07-22 18:00:21 -07003249 if (button != null) {
3250 // If we were unable to find the icon via the meta-data, use a
3251 // generic one
3252 if (toolbarIcon == null) {
3253 button.setImageResource(fallbackDrawableId);
3254 } else {
3255 button.setImageDrawable(toolbarIcon);
3256 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003257 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003258
3259 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3260
Michael Jurka0423dcf2010-10-05 14:56:18 -07003261 }
3262
Winson Chung1b884092012-06-08 17:13:02 -07003263 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003264 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003265 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003266 }
3267
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003268 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003269 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003270 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003271 }
3272
Winson Chungbb185bd2011-11-21 12:31:42 -08003273 private void invalidatePressedFocusedStates(View container, View button) {
3274 if (container instanceof HolographicLinearLayout) {
3275 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3276 layout.invalidatePressedFocusedStates();
3277 } else if (button instanceof HolographicImageView) {
3278 HolographicImageView view = (HolographicImageView) button;
3279 view.invalidatePressedFocusedStates();
3280 }
3281 }
3282
Winson Chungc51db6a2011-10-05 11:44:49 -07003283 private boolean updateGlobalSearchIcon() {
3284 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003285 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003286 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003287 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003288
Winson Chung1cad91e2011-05-25 17:41:01 -07003289 final SearchManager searchManager =
3290 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3291 ComponentName activityName = searchManager.getGlobalSearchActivity();
3292 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003293 int coi = getCurrentOrientationIndexForGlobalIcons();
3294 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003295 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3296 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3297 if (sGlobalSearchIcon[coi] == null) {
3298 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3299 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3300 TOOLBAR_ICON_METADATA_NAME);
3301 }
3302
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3304 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003305 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003306 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003307 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003308 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003309 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3310 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3311 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003312 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003313 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003315 }
3316 }
3317
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003318 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003319 final View searchButtonContainer = findViewById(R.id.search_button_container);
3320 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003321 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003322 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003323 }
3324
Winson Chungc51db6a2011-10-05 11:44:49 -07003325 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003327 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003328
Winson Chungc51db6a2011-10-05 11:44:49 -07003329 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003330 final SearchManager searchManager =
3331 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3332 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3333
3334 ComponentName activityName = null;
3335 if (globalSearchActivity != null) {
3336 // Check if the global search activity handles voice search
3337 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3338 intent.setPackage(globalSearchActivity.getPackageName());
3339 activityName = intent.resolveActivity(getPackageManager());
3340 }
3341
3342 if (activityName == null) {
3343 // Fallback: check if an activity other than the global search activity
3344 // resolves this
3345 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3346 activityName = intent.resolveActivity(getPackageManager());
3347 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003348 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003349 int coi = getCurrentOrientationIndexForGlobalIcons();
3350 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003351 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3352 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3353 if (sVoiceSearchIcon[coi] == null) {
3354 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3355 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3356 TOOLBAR_ICON_METADATA_NAME);
3357 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003358 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003359 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003360 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003361 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003362 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003363 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003364 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003365 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003366 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003367 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003368 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003369 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003370
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003371 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003372 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3373 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003374 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003375 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003376 }
3377
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003378 public void setVoiceButtonProxyVisible(boolean visible) {
3379 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3380 if (voiceButtonProxy != null) {
3381 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3382 }
3383 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003384 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003385 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003386 */
3387 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003388 final View marketButton = findViewById(R.id.market_button);
3389 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3390 // Find the app market activity by resolving an intent.
3391 // (If multiple app markets are installed, it will return the ResolverActivity.)
3392 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003393 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003394 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003395 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003396 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003397 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3398 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003399 marketButton.setVisibility(View.VISIBLE);
3400 } else {
3401 // We should hide and disable the view so that we don't try and restore the visibility
3402 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3403 marketButton.setVisibility(View.GONE);
3404 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003405 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003406 }
3407
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003408 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003409 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3410 Resources r = getResources();
3411 Drawable marketIconDrawable = d.newDrawable(r);
3412 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3413 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3414 marketIconDrawable.setBounds(0, 0, w, h);
3415
3416 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003417 }
3418
Michael Jurkad7c28052012-04-27 15:43:36 -07003419 @Override
3420 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003421 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003422 final List<CharSequence> text = event.getText();
3423 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003424 // Populate event with a fake title based on the current state.
3425 if (mState == State.APPS_CUSTOMIZE) {
3426 text.add(getString(R.string.all_apps_button_label));
3427 } else {
3428 text.add(getString(R.string.all_apps_home_button_label));
3429 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003430 return result;
3431 }
3432
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003433 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003434 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003435 */
3436 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3437 @Override
3438 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003439 closeSystemDialogs();
3440 }
3441 }
3442
3443 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003444 * Receives notifications whenever the appwidgets are reset.
3445 */
3446 private class AppWidgetResetObserver extends ContentObserver {
3447 public AppWidgetResetObserver() {
3448 super(new Handler());
3449 }
3450
3451 @Override
3452 public void onChange(boolean selfChange) {
3453 onAppWidgetReset();
3454 }
3455 }
3456
3457 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003458 * If the activity is currently paused, signal that we need to run the passed Runnable
3459 * in onResume.
3460 *
3461 * This needs to be called from incoming places where resources might have been loaded
3462 * while we are paused. That is becaues the Configuration might be wrong
3463 * when we're not running, and if it comes back to what it was when we
3464 * were paused, we are not restarted.
3465 *
3466 * Implementation of the method from LauncherModel.Callbacks.
3467 *
3468 * @return true if we are currently paused. The caller might be able to
3469 * skip some work in that case since we will come back again.
3470 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003471 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003472 if (mPaused) {
3473 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003474 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003475 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003476 }
3477 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003478 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003479 return true;
3480 } else {
3481 return false;
3482 }
3483 }
3484
Michael Jurkac402cd92013-05-20 15:49:32 +02003485 private boolean waitUntilResume(Runnable run) {
3486 return waitUntilResume(run, false);
3487 }
3488
Michael Jurka1e2f4652013-07-08 18:03:46 -07003489 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003490 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003491 }
3492
3493 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003494 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003495 }
3496
Michael Jurka7607c2f2013-04-03 14:33:19 -07003497 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003498 * If the activity is currently paused, signal that we need to re-run the loader
3499 * in onResume.
3500 *
3501 * This needs to be called from incoming places where resources might have been loaded
3502 * while we are paused. That is becaues the Configuration might be wrong
3503 * when we're not running, and if it comes back to what it was when we
3504 * were paused, we are not restarted.
3505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 *
3508 * @return true if we are currently paused. The caller might be able to
3509 * skip some work in that case since we will come back again.
3510 */
3511 public boolean setLoadOnResume() {
3512 if (mPaused) {
3513 Log.i(TAG, "setLoadOnResume");
3514 mOnResumeNeedsLoad = true;
3515 return true;
3516 } else {
3517 return false;
3518 }
3519 }
3520
3521 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003523 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003525 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003526 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003527 } else {
3528 return SCREEN_COUNT / 2;
3529 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003531
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 /**
3533 * Refreshes the shortcuts shown on the workspace.
3534 *
3535 * Implementation of the method from LauncherModel.Callbacks.
3536 */
3537 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003538 // If we're starting binding all over again, clear any bind calls we'd postponed in
3539 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3540 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003541 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003542
Michael Jurka7607c2f2013-04-03 14:33:19 -07003543 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003544 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 int count = workspace.getChildCount();
3546 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003547 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003548 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3549 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003551 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003552 if (mHotseat != null) {
3553 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003554 }
3555 }
3556
Adam Cohendcd297f2013-06-18 13:13:40 -07003557 @Override
3558 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003559 bindAddScreens(orderedScreenIds);
3560 mWorkspace.addExtraEmptyScreen();
3561 }
3562
3563 @Override
3564 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003565 int count = orderedScreenIds.size();
3566 for (int i = 0; i < count; i++) {
Winson Chung8481e322013-08-09 16:06:38 -07003567 Log.d(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Winson Chung64359a52013-07-08 17:17:08 -07003568 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003569 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003570 }
3571
Adam Cohen39a06042013-07-19 14:30:12 -07003572 private boolean shouldShowWeightWatcher() {
3573 String spKey = LauncherAppState.getSharedPreferencesKey();
3574 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003575 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003576
3577 return show;
3578 }
3579
3580 private void toggleShowWeightWatcher() {
3581 String spKey = LauncherAppState.getSharedPreferencesKey();
3582 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3583 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3584
3585 show = !show;
3586
3587 SharedPreferences.Editor editor = sp.edit();
3588 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3589 editor.commit();
3590
3591 if (mWeightWatcher != null) {
3592 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3593 }
3594 }
3595
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 * Bind the items start-end from the list.
3598 *
3599 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003600 */
Winson Chung64359a52013-07-08 17:17:08 -07003601 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3602 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003603 if (waitUntilResume(new Runnable() {
3604 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003605 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003606 }
3607 })) {
3608 return;
3609 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003610
Winson Chungf0c6ae02012-03-21 16:10:31 -07003611 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003612 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3613 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003614 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003615 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003616 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003617 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003619
3620 // Short circuit if we are loading dock items for a configuration which has no dock
3621 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3622 mHotseat == null) {
3623 continue;
3624 }
3625
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 switch (item.itemType) {
3627 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3628 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003630 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003631
Winson Chung64359a52013-07-08 17:17:08 -07003632 /*
3633 * TODO: FIX collision case
3634 */
Winson Chungce376632013-07-11 16:12:41 -07003635 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3636 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3637 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3638 throw new RuntimeException("OCCUPIED");
3639 }
Winson Chung64359a52013-07-08 17:17:08 -07003640 }
3641
Adam Cohendcd297f2013-06-18 13:13:40 -07003642 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3643 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003644 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003645 // Animate all the applications up now
3646 shortcut.setAlpha(0f);
3647 shortcut.setScaleX(0f);
3648 shortcut.setScaleY(0f);
3649 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003650 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003651 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003653 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003654 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003655 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003656 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003657 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3658 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003660 default:
3661 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003663 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003664
Winson Chung997a9232013-07-24 15:33:46 -07003665 if (animateIcons) {
3666 // Animate to the correct page
3667 if (newShortcutsScreenId > -1) {
3668 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3669 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3670 if (newShortcutsScreenId != currentScreenId) {
3671 mWorkspace.snapToPage(newScreenIndex);
3672 }
3673 }
3674
Winson Chung64359a52013-07-08 17:17:08 -07003675 // We post the animation slightly delayed to prevent slowdowns when we are loading
3676 // right after we return to launcher.
3677 mWorkspace.postDelayed(new Runnable() {
3678 public void run() {
3679 anim.playTogether(bounceAnims);
3680 anim.start();
3681 }
3682 }, NEW_APPS_ANIMATION_DELAY);
3683 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003685 }
3686
Joe Onorato9c1289c2009-08-17 11:03:03 -04003687 /**
3688 * Implementation of the method from LauncherModel.Callbacks.
3689 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3691 if (waitUntilResume(new Runnable() {
3692 public void run() {
3693 bindFolders(folders);
3694 }
3695 })) {
3696 return;
3697 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003698 sFolders.clear();
3699 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003700 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003701
3702 /**
3703 * Add the views for a widget to the workspace.
3704 *
3705 * Implementation of the method from LauncherModel.Callbacks.
3706 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003707 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3708 if (waitUntilResume(new Runnable() {
3709 public void run() {
3710 bindAppWidget(item);
3711 }
3712 })) {
3713 return;
3714 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003715
Daniel Sandler843e8602010-06-07 14:59:01 -04003716 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3717 if (DEBUG_WIDGETS) {
3718 Log.d(TAG, "bindAppWidget: " + item);
3719 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003720 final Workspace workspace = mWorkspace;
3721
3722 final int appWidgetId = item.appWidgetId;
3723 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003724 if (DEBUG_WIDGETS) {
3725 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3726 }
3727
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3729
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003731 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732
Adam Cohendcd297f2013-06-18 13:13:40 -07003733 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003735 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3736
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 workspace.requestLayout();
3738
Daniel Sandler843e8602010-06-07 14:59:01 -04003739 if (DEBUG_WIDGETS) {
3740 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3741 + (SystemClock.uptimeMillis()-start) + "ms");
3742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 }
3744
Adam Cohen1462de32012-07-24 22:34:36 -07003745 public void onPageBoundSynchronously(int page) {
3746 mSynchronouslyBoundPages.add(page);
3747 }
3748
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 /**
3750 * Callback saying that there aren't any more items to bind.
3751 *
3752 * Implementation of the method from LauncherModel.Callbacks.
3753 */
Adam Cohene25af792013-06-06 23:08:25 -07003754 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003755 if (waitUntilResume(new Runnable() {
3756 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003757 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003758 }
3759 })) {
3760 return;
3761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 if (mSavedState != null) {
3763 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003764 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 mSavedState = null;
3767 }
3768
Winson Chung98ca0f72013-07-29 12:58:51 -07003769 // Create the custom content page here before onLayout to prevent flashing
3770 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3771 mWorkspace.createCustomContentPage();
3772 }
3773
Adam Cohen1462de32012-07-24 22:34:36 -07003774 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003776 // If we received the result of any pending adds while the loader was running (e.g. the
3777 // widget configuration forced an orientation change), process them now.
3778 for (int i = 0; i < sPendingAddList.size(); i++) {
3779 completeAdd(sPendingAddList.get(i));
3780 }
3781 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782
Winson Chungc51db6a2011-10-05 11:44:49 -07003783 // Update the market app icon as necessary (the other icons will be managed in response to
3784 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003785 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003786
Winson Chungf0c6ae02012-03-21 16:10:31 -07003787 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003788 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003789 mWorkspace.stripDuplicateApps();
3790 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003791 }
Adam Cohen99894d92013-06-14 11:22:59 -07003792
Adam Cohen66a01fd2013-06-11 12:48:00 -07003793 mWorkspace.post(new Runnable() {
3794 @Override
3795 public void run() {
3796 onFinishBindingItems();
3797 }
3798 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 }
3800
Winson Chunga2413752012-04-03 14:22:34 -07003801 private boolean canRunNewAppsAnimation() {
3802 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3803 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3804 }
3805
Winson Chungc9168342013-06-26 14:54:55 -07003806 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3807 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3808 PropertyValuesHolder.ofFloat("alpha", 1f),
3809 PropertyValuesHolder.ofFloat("scaleX", 1f),
3810 PropertyValuesHolder.ofFloat("scaleY", 1f));
3811 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3812 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3813 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3814 return bounceAnim;
3815 }
3816
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003817 @Override
3818 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003819 boolean searchVisible = updateGlobalSearchIcon();
3820 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003821 if (mSearchDropTargetBar != null) {
3822 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3823 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003824 }
3825
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003826 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 * Add the icons for all apps.
3828 *
3829 * Implementation of the method from LauncherModel.Callbacks.
3830 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003831 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003832 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3833 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003834 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003835 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 }
3838
3839 /**
3840 * A package was updated.
3841 *
3842 * Implementation of the method from LauncherModel.Callbacks.
3843 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003844 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3845 if (waitUntilResume(new Runnable() {
3846 public void run() {
3847 bindAppsUpdated(apps);
3848 }
3849 })) {
3850 return;
3851 }
3852
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003853 if (mWorkspace != null) {
3854 mWorkspace.updateShortcuts(apps);
3855 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 }
3857
3858 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003859 * A package was uninstalled. We take both the super set of packageNames
3860 * in addition to specific applications to remove, the reason being that
3861 * this can be called when a package is updated as well. In that scenario,
3862 * we only remove specific components from the workspace, where as
3863 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 *
3865 * Implementation of the method from LauncherModel.Callbacks.
3866 */
Winson Chung83892cc2013-05-01 16:53:33 -07003867 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3868 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003869 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003870 if (waitUntilResume(new Runnable() {
3871 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003872 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003873 }
3874 })) {
3875 return;
3876 }
3877
Winson Chung64359a52013-07-08 17:17:08 -07003878 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003879 mWorkspace.removeItemsByPackageName(packageNames);
3880 } else {
3881 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003882 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003883
Winson Chunga1820962011-10-03 16:31:06 -07003884 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003885 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 }
Joe Onoratobe386092009-11-17 17:32:16 -08003887
3888 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003889 * A number of packages were updated.
3890 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003891 private ArrayList<Object> mWidgetsAndShortcuts;
3892 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003893 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003894 bindPackagesUpdated(mWidgetsAndShortcuts);
3895 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003896 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003897 };
3898
3899 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3900 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3901 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003902 return;
3903 }
3904
Winson Chung64359a52013-07-08 17:17:08 -07003905 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003906 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003907 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003908 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003909 }
3910
Winson Chung400438b2011-01-16 17:53:48 -08003911 private int mapConfigurationOriActivityInfoOri(int configOri) {
3912 final Display d = getWindowManager().getDefaultDisplay();
3913 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3914 switch (d.getRotation()) {
3915 case Surface.ROTATION_0:
3916 case Surface.ROTATION_180:
3917 // We are currently in the same basic orientation as the natural orientation
3918 naturalOri = configOri;
3919 break;
3920 case Surface.ROTATION_90:
3921 case Surface.ROTATION_270:
3922 // We are currently in the other basic orientation to the natural orientation
3923 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3924 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3925 break;
3926 }
3927
3928 int[] oriMap = {
3929 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3930 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3931 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3932 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3933 };
3934 // Since the map starts at portrait, we need to offset if this device's natural orientation
3935 // is landscape.
3936 int indexOffset = 0;
3937 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3938 indexOffset = 1;
3939 }
3940 return oriMap[(d.getRotation() + indexOffset) % 4];
3941 }
Adam Cohen446e9402011-09-15 18:21:21 -07003942
Winson Chung4b919f82012-05-01 10:44:08 -07003943 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003944 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003945 getResources().getBoolean(R.bool.allow_rotation);
3946 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003947 }
Winson Chung4b919f82012-05-01 10:44:08 -07003948 public void lockScreenOrientation() {
3949 if (isRotationEnabled()) {
3950 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3951 .getConfiguration().orientation));
3952 }
3953 }
3954 public void unlockScreenOrientation(boolean immediate) {
3955 if (isRotationEnabled()) {
3956 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003957 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003958 } else {
3959 mHandler.postDelayed(new Runnable() {
3960 public void run() {
3961 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3962 }
3963 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003964 }
Winson Chung4b919f82012-05-01 10:44:08 -07003965 }
Winson Chung400438b2011-01-16 17:53:48 -08003966 }
3967
Winson Chung82f55532011-08-09 14:14:23 -07003968 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003969 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003970 if (DISABLE_CLINGS) {
3971 return false;
3972 }
3973
Brett Chabot2a9e2282011-08-23 15:03:13 -07003974 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003975 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003976
Michael Jurkae233a8b2013-03-19 13:49:20 +01003977 // Restricted secondary users (child mode) will potentially have very few apps
3978 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003979// boolean supportsLimitedUsers =
3980// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3981// Account[] accounts = AccountManager.get(this).getAccounts();
3982// if (supportsLimitedUsers && accounts.length == 0) {
3983// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3984// Bundle restrictions = um.getUserRestrictions();
3985// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3986// return false;
3987// }
3988// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003989 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003990 }
Michael Jurka22143132012-10-04 17:42:38 +02003991
Winson Chung7d7541e2011-09-16 20:14:36 -07003992 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003993 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003994 if (cling != null) {
3995 cling.init(this, positionData);
3996 cling.setVisibility(View.VISIBLE);
3997 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3998 if (animate) {
3999 cling.buildLayer();
4000 cling.setAlpha(0f);
4001 cling.animate()
4002 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004003 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004004 .setDuration(SHOW_CLING_DURATION)
4005 .setStartDelay(delay)
4006 .start();
4007 } else {
4008 cling.setAlpha(1f);
4009 }
Michael Jurka22143132012-10-04 17:42:38 +02004010 cling.setFocusableInTouchMode(true);
4011 cling.post(new Runnable() {
4012 public void run() {
4013 cling.setFocusable(true);
4014 cling.requestFocus();
4015 }
4016 });
4017 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4018 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004019 }
4020 return cling;
4021 }
Michael Jurka22143132012-10-04 17:42:38 +02004022
Winson Chung7d7541e2011-09-16 20:14:36 -07004023 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004024 // To catch cases where siblings of top-level views are made invisible, just check whether
4025 // the cling is directly set to GONE before dismissing it.
4026 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004027 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004028 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004029 anim.addListener(new AnimatorListenerAdapter() {
4030 public void onAnimationEnd(Animator animation) {
4031 cling.setVisibility(View.GONE);
4032 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004033 // We should update the shared preferences on a background thread
4034 new Thread("dismissClingThread") {
4035 public void run() {
4036 SharedPreferences.Editor editor = mSharedPrefs.edit();
4037 editor.putBoolean(flag, true);
4038 editor.commit();
4039 }
4040 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004041 };
4042 });
4043 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004044 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004045 }
4046 }
Michael Jurka22143132012-10-04 17:42:38 +02004047
Winson Chung9d9d74f2011-09-19 11:49:12 -07004048 private void removeCling(int id) {
4049 final View cling = findViewById(id);
4050 if (cling != null) {
4051 final ViewGroup parent = (ViewGroup) cling.getParent();
4052 parent.post(new Runnable() {
4053 @Override
4054 public void run() {
4055 parent.removeView(cling);
4056 }
4057 });
Michael Jurka22143132012-10-04 17:42:38 +02004058 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004059 }
4060 }
Michael Jurka974c3862012-05-22 22:00:31 -07004061
4062 private boolean skipCustomClingIfNoAccounts() {
4063 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4064 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4065 if (customCling) {
4066 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004067 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004068 Account[] accounts = am.getAccountsByType("com.google");
4069 return accounts.length == 0;
4070 }
4071 return false;
4072 }
4073
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004075 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004076 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004077 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4078 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004079 // If we're not using the default workspace layout, replace workspace cling
4080 // with a custom workspace cling (usually specified in an overlay)
4081 // For now, only do this on tablets
4082 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004083 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004084 // Use a custom cling
4085 View cling = findViewById(R.id.workspace_cling);
4086 ViewGroup clingParent = (ViewGroup) cling.getParent();
4087 int clingIndex = clingParent.indexOfChild(cling);
4088 clingParent.removeViewAt(clingIndex);
4089 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4090 clingParent.addView(customCling, clingIndex);
4091 customCling.setId(R.id.workspace_cling);
4092 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004094 } else {
4095 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004096 }
4097 }
4098 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004099 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004100 if (isClingsEnabled() &&
4101 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004102 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004103 } else {
4104 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004105 }
4106 }
4107 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004108 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004109 if (isClingsEnabled() &&
4110 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4111 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004112 } else {
4113 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004114 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004116 }
4117 public boolean isFolderClingVisible() {
4118 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004119 if (cling != null) {
4120 return cling.getVisibility() == View.VISIBLE;
4121 }
4122 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004123 }
Winson Chung82f55532011-08-09 14:14:23 -07004124 public void dismissWorkspaceCling(View v) {
4125 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004126 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004127 }
4128 public void dismissAllAppsCling(View v) {
4129 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004130 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4131 }
4132 public void dismissFolderCling(View v) {
4133 Cling cling = (Cling) findViewById(R.id.folder_cling);
4134 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004135 }
4136
Winson Chung80baf5a2010-08-09 16:03:15 -07004137 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004138 * Prints out out state for debugging.
4139 */
4140 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004141 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004142 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004143 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4144 Log.d(TAG, "mRestoring=" + mRestoring);
4145 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4146 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004147 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004148 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004149
Winson Chung785d2eb2011-04-14 16:08:02 -07004150 if (mAppsCustomizeContent != null) {
4151 mAppsCustomizeContent.dumpState();
4152 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004153 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004154 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004155
4156 @Override
4157 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4158 super.dump(prefix, fd, writer, args);
4159 writer.println(" ");
4160 writer.println("Debug logs: ");
4161 for (int i = 0; i < sDumpLogs.size(); i++) {
4162 writer.println(" " + sDumpLogs.get(i));
4163 }
4164 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004165
4166 public static void dumpDebugLogsToConsole() {
4167 Log.d(TAG, "");
4168 Log.d(TAG, "*********************");
4169 Log.d(TAG, "Launcher debug logs: ");
4170 for (int i = 0; i < sDumpLogs.size(); i++) {
4171 Log.d(TAG, " " + sDumpLogs.get(i));
4172 }
4173 Log.d(TAG, "*********************");
4174 Log.d(TAG, "");
4175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004176}
Michael Jurka2763be32011-02-24 11:19:57 -08004177
Michael Jurkaabded662011-03-04 12:06:57 -08004178interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004179 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004180 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004181 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004182 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004183 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004184}