blob: 876b2f66da559f5c43cbf3b4164aee051336e38a [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) {
650 if (requestCode == REQUEST_BIND_APPWIDGET) {
651 int appWidgetId = data != null ?
652 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
653 if (resultCode == RESULT_CANCELED) {
654 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
655 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700656 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700657 }
658 return;
659 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700660 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
662 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700663 mWaitingForResult = false;
664
Adam Cohened66b2b2012-01-23 17:28:51 -0800665 // We have special handling for widgets
666 if (isWidgetDrop) {
667 int appWidgetId = data != null ?
668 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700669 if (appWidgetId < 0) {
670 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
671 "widget configuration activity.");
672 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
673 } else {
674 completeTwoStageWidgetDrop(resultCode, appWidgetId);
675 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800676 return;
677 }
678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 // The pattern used here is that a user PICKs a specific application,
680 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
683 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700684 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800685 final PendingAddArguments args = new PendingAddArguments();
686 args.requestCode = requestCode;
687 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700688 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700689 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700690 args.cellX = mPendingAddInfo.cellX;
691 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800692 if (isWorkspaceLocked()) {
693 sPendingAddList.add(args);
694 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700695 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700699 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800700 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
701 null);
702 }
703
704 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700705 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 Runnable onCompleteRunnable = null;
708 int animationType = 0;
709
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700710 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800711 if (resultCode == RESULT_OK) {
712 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
713 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700714 mPendingAddWidgetInfo);
715 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 onCompleteRunnable = new Runnable() {
717 @Override
718 public void run() {
719 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700720 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800721 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
722 null);
723 }
724 };
725 } else if (resultCode == RESULT_CANCELED) {
726 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
727 onCompleteRunnable = new Runnable() {
728 @Override
729 public void run() {
730 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
731 null);
732 }
733 };
734 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700735 if (mDragLayer.getAnimatedView() != null) {
736 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
737 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
738 animationType, boundWidget, true);
739 } else {
740 // The animated view may be null in the case of a rotation during widget configuration
741 onCompleteRunnable.run();
742 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744
745 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700746 protected void onStop() {
747 super.onStop();
748 FirstFrameAnimatorHelper.setIsVisible(false);
749 }
750
751 @Override
752 protected void onStart() {
753 super.onStart();
754 FirstFrameAnimatorHelper.setIsVisible(true);
755 }
756
757 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200759 long startTime = 0;
760 if (DEBUG_RESUME_TIME) {
761 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400762 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700765
Winson Chung4a2afa32012-07-19 14:53:05 -0700766 // Restore the previous launcher state
767 if (mOnResumeState == State.WORKSPACE) {
768 showWorkspace(false);
769 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
770 showAllApps(false);
771 }
772 mOnResumeState = State.NONE;
773
Craig Mautner360310b2012-10-26 15:13:08 -0700774 // Background was set to gradient in onPause(), restore to black if in all apps.
775 setWorkspaceBackground(mState == State.WORKSPACE);
776
Winson Chungde0fb8f2012-05-08 14:37:08 -0700777 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700778 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700779
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800780 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700781 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700782 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400783 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700784 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700786 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700788 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200789 // We might have postponed some bind calls until onResume (see waitUntilResume) --
790 // execute them here
791 long startTimeCallbacks = 0;
792 if (DEBUG_RESUME_TIME) {
793 startTimeCallbacks = System.currentTimeMillis();
794 }
795
796 if (mAppsCustomizeContent != null) {
797 mAppsCustomizeContent.setBulkBind(true);
798 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700799 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
800 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200801 }
802 if (mAppsCustomizeContent != null) {
803 mAppsCustomizeContent.setBulkBind(false);
804 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700805 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200806 if (DEBUG_RESUME_TIME) {
807 Log.d(TAG, "Time spent processing callbacks in onResume: " +
808 (System.currentTimeMillis() - startTimeCallbacks));
809 }
Michael Jurka447bf842013-05-15 14:52:15 +0200810 }
Michael Jurka54554252013-08-01 12:52:23 +0200811 if (mOnResumeCallbacks.size() > 0) {
812 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
813 mOnResumeCallbacks.get(i).run();
814 }
815 mOnResumeCallbacks.clear();
816 }
Winson Chunge4e50662012-01-23 14:45:13 -0800817
818 // Reset the pressed state of icons that were locked in the press state while activities
819 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800820 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800821 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800822 mWaitingForResume.setStayPressed(false);
823 }
Winson Chunge4e50662012-01-23 14:45:13 -0800824 if (mAppsCustomizeContent != null) {
825 // Resets the previous all apps icon press state
826 mAppsCustomizeContent.resetDrawableState();
827 }
Adam Cohen06dff352012-06-01 17:17:08 -0700828 // It is possible that widgets can receive updates while launcher is not in the foreground.
829 // Consequently, the widgets will be inflated in the orientation of the foreground activity
830 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
831 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700832 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700833
834 // Again, as with the above scenario, it's possible that one or more of the global icons
835 // were updated in the wrong orientation.
836 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200837 if (DEBUG_RESUME_TIME) {
838 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
839 }
Adam Cohen06dff352012-06-01 17:17:08 -0700840 }
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700844 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
845 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
846 // when Launcher resumes and we are still in AllApps.
847 updateWallpaperVisibility(true);
848
Winson Chung997a9232013-07-24 15:33:46 -0700849 // Ensure that items added to Launcher are queued until Launcher returns
850 InstallShortcutReceiver.enableInstallQueue();
851
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700853 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800854 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700855 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700856 }
Romain Guycbb89e42009-06-08 15:52:54 -0700857
Adam Cohen66a01fd2013-06-11 12:48:00 -0700858 protected void onFinishBindingItems() {
859 }
860
Adam Cohenbffe7452013-07-22 18:21:45 -0700861 QSBScroller mQsbScroller = new QSBScroller() {
862 int scrollY = 0;
863
864 @Override
865 public void setScrollY(int scroll) {
866 scrollY = scroll;
867
868 if (mWorkspace.isOnOrMovingToCustomContent()) {
869 mSearchDropTargetBar.setTranslationY(- scrollY);
870 }
871 }
872 };
873
874 public void resetQSBScroll() {
875 mSearchDropTargetBar.animate().translationY(0).start();
876 }
877
878 public interface CustomContentCallbacks {
879 // Custom content is completely shown
880 public void onShow();
881
882 // Custom content is completely hidden
883 public void onHide();
884 }
885
886 public interface QSBScroller {
887 public void setScrollY(int scrollY);
888 }
889
Adam Cohen66a01fd2013-06-11 12:48:00 -0700890 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700891 public QSBScroller addToCustomContentPage(View customContent) {
892 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700893 }
894
Winson Chung98ca0f72013-07-29 12:58:51 -0700895 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700896 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700897 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700898 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700899 }
900
Adam Cohenedb40762013-07-18 16:45:45 -0700901 // The custom content needs to offset its content to account for the QSB
902 public int getTopOffsetForCustomContent() {
903 return mWorkspace.getPaddingTop();
904 }
905
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700906 @Override
907 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 // Flag the loader to stop early before switching
909 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700910 if (mAppsCustomizeContent != null) {
911 mAppsCustomizeContent.surrender();
912 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800913 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700914 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700915
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800916 // We can't hide the IME if it was forced open. So don't bother
917 /*
918 @Override
919 public void onWindowFocusChanged(boolean hasFocus) {
920 super.onWindowFocusChanged(hasFocus);
921
922 if (hasFocus) {
923 final InputMethodManager inputManager = (InputMethodManager)
924 getSystemService(Context.INPUT_METHOD_SERVICE);
925 WindowManager.LayoutParams lp = getWindow().getAttributes();
926 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
927 android.os.Handler()) {
928 protected void onReceiveResult(int resultCode, Bundle resultData) {
929 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
930 }
931 });
932 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
933 }
934 }
935 */
936
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 private boolean acceptFilter() {
938 final InputMethodManager inputManager = (InputMethodManager)
939 getSystemService(Context.INPUT_METHOD_SERVICE);
940 return !inputManager.isFullscreenMode();
941 }
942
943 @Override
944 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700945 final int uniChar = event.getUnicodeChar();
946 final boolean handled = super.onKeyDown(keyCode, event);
947 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
948 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
950 keyCode, event);
951 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700952 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700953 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700954 // showSearchDialog()
955 // If there are multiple keystrokes before the search dialog takes focus,
956 // onSearchRequested() will be called for every keystroke,
957 // but it is idempotent, so it's fine.
958 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 }
960 }
961
Joe Onorato8a9625e2010-01-28 15:55:35 -0800962 // Eat the long press event so the keyboard doesn't come up.
963 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
964 return true;
965 }
966
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 return handled;
968 }
969
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 private String getTypedText() {
971 return mDefaultKeySsb.toString();
972 }
973
974 private void clearTypedText() {
975 mDefaultKeySsb.clear();
976 mDefaultKeySsb.clearSpans();
977 Selection.setSelection(mDefaultKeySsb, 0);
978 }
979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700981 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
982 * State
983 */
984 private static State intToState(int stateOrdinal) {
985 State state = State.WORKSPACE;
986 final State[] stateValues = State.values();
987 for (int i = 0; i < stateValues.length; i++) {
988 if (stateValues[i].ordinal() == stateOrdinal) {
989 state = stateValues[i];
990 break;
991 }
992 }
993 return state;
994 }
995
996 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 * Restores the previous state, if it exists.
998 *
999 * @param savedState The previous state.
1000 */
1001 private void restoreState(Bundle savedState) {
1002 if (savedState == null) {
1003 return;
1004 }
1005
Michael Jurka883f55b2010-10-21 15:47:14 -07001006 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001007 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001009 }
1010
Winson Chungf0c6ae02012-03-21 16:10:31 -07001011 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001013 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015
Winson Chung3d503fb2011-07-13 17:25:49 -07001016 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001017 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1020 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001021 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1023 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001024 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1025 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1026 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001027 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 mRestoring = true;
1029 }
1030
1031 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1032 if (renameFolder) {
1033 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001034 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 mRestoring = true;
1036 }
Winson Chunga12a2502010-12-20 14:41:35 -08001037
Winson Chung785d2eb2011-04-14 16:08:02 -07001038 // Restore the AppsCustomize tab
1039 if (mAppsCustomizeTabHost != null) {
1040 String curTab = savedState.getString("apps_customize_currentTab");
1041 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001042 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001043 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001044 mAppsCustomizeContent.loadAssociatedPages(
1045 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001046 }
1047
Winson Chung5afbf7b2011-07-25 11:53:08 -07001048 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1049 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
1052
1053 /**
1054 * Finds all the views we need and configure them properly.
1055 */
1056 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001057 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001058
Craig Mautner360310b2012-10-26 15:13:08 -07001059 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001060 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1061 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001062 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001063
1064 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1065 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066
Winson Chung4c98d922011-05-31 16:50:48 -07001067 // Setup the drag layer
1068 mDragLayer.setup(this, dragController);
1069
Winson Chung3d503fb2011-07-13 17:25:49 -07001070 // Setup the hotseat
1071 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1072 if (mHotseat != null) {
1073 mHotseat.setup(this);
1074 }
1075
Adam Cohenf358a4b2013-07-23 16:47:31 -07001076 mOverviewPanel = findViewById(R.id.overview_panel);
1077 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1078 @Override
1079 public void onClick(View arg0) {
1080 showAllApps(true);
1081 }
1082 });
1083 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1084 @Override
1085 public void onClick(View arg0) {
1086 startWallpaper();
1087 }
1088 });
1089
Winson Chung4c98d922011-05-31 16:50:48 -07001090 // Setup the workspace
1091 mWorkspace.setHapticFeedbackEnabled(false);
1092 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001093 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001094 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001095
Winson Chungf0ea4d32011-06-06 14:27:16 -07001096 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001097 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001098
Winson Chungf0ea4d32011-06-06 14:27:16 -07001099 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001100 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001101 mAppsCustomizeContent = (AppsCustomizePagedView)
1102 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1103 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001104
Winson Chung3d503fb2011-07-13 17:25:49 -07001105 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001106 dragController.setDragScoller(mWorkspace);
1107 dragController.setScrollView(mDragLayer);
1108 dragController.setMoveTarget(mWorkspace);
1109 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001110 if (mSearchDropTargetBar != null) {
1111 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001112 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001113
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001114 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001115 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001116 mWeightWatcher = new WeightWatcher(this);
1117 mWeightWatcher.setAlpha(0.5f);
1118 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001119 new FrameLayout.LayoutParams(
1120 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001121 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001122 Gravity.BOTTOM)
1123 );
Adam Cohen39a06042013-07-19 14:30:12 -07001124
1125 boolean show = shouldShowWeightWatcher();
1126 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 /**
1131 * Creates a view representing a shortcut.
1132 *
1133 * @param info The data structure describing the shortcut.
1134 *
1135 * @return A View inflated from R.layout.application.
1136 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001137 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001139 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141
1142 /**
1143 * Creates a view representing a shortcut inflated from the specified resource.
1144 *
1145 * @param layoutResId The id of the XML layout used to create the shortcut.
1146 * @param parent The group the shortcut belongs to.
1147 * @param info The data structure describing the shortcut.
1148 *
1149 * @return A View inflated from layoutResId.
1150 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001151 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001152 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1153 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 return favorite;
1156 }
1157
1158 /**
1159 * Add an application shortcut to the workspace.
1160 *
1161 * @param data The intent describing the application.
1162 * @param cellInfo The position on screen where to create the shortcut.
1163 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001165 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167
Adam Cohenfbba09b2011-07-18 21:43:05 -07001168 // First we check if we already know the exact location where we want to add this item.
1169 if (cellX >= 0 && cellY >= 0) {
1170 cellXY[0] = cellX;
1171 cellXY[1] = cellY;
1172 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001173 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 return;
1175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001177 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001178
Romain Guy73b979d2009-06-09 12:57:21 -07001179 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001180 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001182 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001183 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001184 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001185 } else {
1186 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188 }
Romain Guycbb89e42009-06-08 15:52:54 -07001189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 /**
1191 * Add a shortcut to the workspace.
1192 *
1193 * @param data The intent describing the shortcut.
1194 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001196 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001197 int cellY) {
1198 int[] cellXY = mTmpAddItemCellCoordinates;
1199 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001200 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001201
Michael Jurkad3ef3062010-11-23 16:23:58 -08001202 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001203
Adam Cohen558baaf2011-08-15 15:22:57 -07001204 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001205 if (info == null) {
1206 return;
1207 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001208 final View view = createShortcut(info);
1209
Adam Cohenfbba09b2011-07-18 21:43:05 -07001210 // First we check if we already know the exact location where we want to add this item.
1211 if (cellX >= 0 && cellY >= 0) {
1212 cellXY[0] = cellX;
1213 cellXY[1] = cellY;
1214 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001215
1216 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001217 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001218 true, null,null)) {
1219 return;
1220 }
1221 DragObject dragObject = new DragObject();
1222 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001223 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1224 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001225 return;
1226 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001227 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001228 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001229 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001230 foundCellSpan = (result != null);
1231 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001232 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001233 }
1234
1235 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001236 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 return;
1238 }
1239
Adam Cohendcd297f2013-06-18 13:13:40 -07001240 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
1242 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001243 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245 }
1246 }
1247
Adam Cohen2f093b62012-04-30 18:59:53 -07001248 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1249 int minHeight) {
1250 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001251 // We want to account for the extra amount of padding that we are adding to the widget
1252 // to ensure that it gets the full amount of space that it has requested
1253 int requiredWidth = minWidth + padding.left + padding.right;
1254 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001255 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001256 }
1257
Adam Cohen2f093b62012-04-30 18:59:53 -07001258 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1259 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001260 }
1261
Adam Cohen2f093b62012-04-30 18:59:53 -07001262 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1263 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001264 }
1265
Adam Cohen2f093b62012-04-30 18:59:53 -07001266 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1267 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001268 }
1269
Adam Cohen2f093b62012-04-30 18:59:53 -07001270 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1271 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001272 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001278 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001279 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001281 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001282 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1283 if (appWidgetInfo == null) {
1284 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1285 }
Romain Guycbb89e42009-06-08 15:52:54 -07001286
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001287 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001288 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001289
Adam Cohen2f093b62012-04-30 18:59:53 -07001290 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1291 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001292
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001294 // We have saved the position to which the widget was dragged-- this really only matters
1295 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001296 int[] cellXY = mTmpAddItemCellCoordinates;
1297 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001298 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001300 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1301 cellXY[0] = mPendingAddInfo.cellX;
1302 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001303 spanXY[0] = mPendingAddInfo.spanX;
1304 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001305 foundCellSpan = true;
1306 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001307 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001309 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1310 spanXY[1], cellXY, finalSpan);
1311 spanXY[0] = finalSpan[0];
1312 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001313 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001315 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001316 }
1317
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001319 if (appWidgetId != -1) {
1320 // Deleting an app widget ID is a void call but writes to disk before returning
1321 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001322 new Thread("deleteAppWidgetId") {
1323 public void run() {
1324 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1325 }
1326 }.start();
1327 }
Winson Chung93eef082012-03-23 15:59:27 -07001328 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001329 return;
1330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001332 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001333 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1334 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001335 launcherInfo.spanX = spanXY[0];
1336 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001337 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1338 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001341 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342
1343 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001344 if (hostView == null) {
1345 // Perform actual inflation because we're live
1346 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1347 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1348 } else {
1349 // The AppWidgetHostView has already been inflated and instantiated
1350 launcherInfo.hostView = hostView;
1351 }
Romain Guycbb89e42009-06-08 15:52:54 -07001352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001354 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001355 launcherInfo.notifyWidgetSizeChanged(this);
1356
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001359
1360 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001362 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
Romain Guycbb89e42009-06-08 15:52:54 -07001364
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1366 @Override
1367 public void onReceive(Context context, Intent intent) {
1368 final String action = intent.getAction();
1369 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1370 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001371 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001372 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001373
Winson Chungc100e8e2011-08-09 16:02:43 -07001374 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001375 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001376 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1377 mAppsCustomizeTabHost.reset();
1378 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001379 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1381 mUserPresent = true;
1382 updateRunning();
1383 }
1384 }
1385 };
1386
1387 @Override
1388 public void onAttachedToWindow() {
1389 super.onAttachedToWindow();
1390
1391 // Listen for broadcasts related to user-presence
1392 final IntentFilter filter = new IntentFilter();
1393 filter.addAction(Intent.ACTION_SCREEN_OFF);
1394 filter.addAction(Intent.ACTION_USER_PRESENT);
1395 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001396 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001397 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001398 mVisible = true;
1399 }
1400
1401 @Override
1402 public void onDetachedFromWindow() {
1403 super.onDetachedFromWindow();
1404 mVisible = false;
1405
Adam Cohend113e0c2010-11-11 10:48:05 -08001406 if (mAttached) {
1407 unregisterReceiver(mReceiver);
1408 mAttached = false;
1409 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001410 updateRunning();
1411 }
1412
1413 public void onWindowVisibilityChanged(int visibility) {
1414 mVisible = visibility == View.VISIBLE;
1415 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001416 // The following code used to be in onResume, but it turns out onResume is called when
1417 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1418 // is a more appropriate event to handle
1419 if (mVisible) {
1420 mAppsCustomizeTabHost.onWindowVisible();
1421 if (!mWorkspaceLoading) {
1422 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001423 // We want to let Launcher draw itself at least once before we force it to build
1424 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001425 // apps is nice and speedy.
1426 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001427 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001428 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001429 if (mStarted) return;
1430 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001431 // We delay the layer building a bit in order to give
1432 // other message processing a time to run. In particular
1433 // this avoids a delay in hiding the IME if it was
1434 // currently shown, because doing that may involve
1435 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001436 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001437 final ViewTreeObserver.OnDrawListener listener = this;
1438 mWorkspace.post(new Runnable() {
1439 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001440 if (mWorkspace != null &&
1441 mWorkspace.getViewTreeObserver() != null) {
1442 mWorkspace.getViewTreeObserver().
1443 removeOnDrawListener(listener);
1444 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001445 }
1446 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001447 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001448 }
1449 });
1450 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001451 // When Launcher comes back to foreground, a different Activity might be responsible for
1452 // the app market intent, so refresh the icon
1453 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001454 clearTypedText();
1455 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001456 }
1457
1458 private void sendAdvanceMessage(long delay) {
1459 mHandler.removeMessages(ADVANCE_MSG);
1460 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1461 mHandler.sendMessageDelayed(msg, delay);
1462 mAutoAdvanceSentTime = System.currentTimeMillis();
1463 }
1464
1465 private void updateRunning() {
1466 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1467 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1468 mAutoAdvanceRunning = autoAdvanceRunning;
1469 if (autoAdvanceRunning) {
1470 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1471 sendAdvanceMessage(delay);
1472 } else {
1473 if (!mWidgetsToAdvance.isEmpty()) {
1474 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1475 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1476 }
1477 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001478 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001479 }
1480 }
1481 }
1482
1483 private final Handler mHandler = new Handler() {
1484 @Override
1485 public void handleMessage(Message msg) {
1486 if (msg.what == ADVANCE_MSG) {
1487 int i = 0;
1488 for (View key: mWidgetsToAdvance.keySet()) {
1489 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1490 final int delay = mAdvanceStagger * i;
1491 if (v instanceof Advanceable) {
1492 postDelayed(new Runnable() {
1493 public void run() {
1494 ((Advanceable) v).advance();
1495 }
1496 }, delay);
1497 }
1498 i++;
1499 }
1500 sendAdvanceMessage(mAdvanceInterval);
1501 }
1502 }
1503 };
1504
1505 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001506 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001507 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1508 if (v instanceof Advanceable) {
1509 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001510 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001511 updateRunning();
1512 }
1513 }
1514
1515 void removeWidgetToAutoAdvance(View hostView) {
1516 if (mWidgetsToAdvance.containsKey(hostView)) {
1517 mWidgetsToAdvance.remove(hostView);
1518 updateRunning();
1519 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 }
1521
Joe Onorato9c1289c2009-08-17 11:03:03 -04001522 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001523 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001524 launcherInfo.hostView = null;
1525 }
1526
Winson Chung93eef082012-03-23 15:59:27 -07001527 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1528 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1529 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001530 }
1531
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001532 public LauncherAppWidgetHost getAppWidgetHost() {
1533 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Winson Chunga9abd0e2010-10-27 17:18:37 -07001536 public LauncherModel getModel() {
1537 return mModel;
1538 }
1539
Bjorn Bringertc459e522013-06-07 19:36:01 +01001540 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001541 getWindow().closeAllPanels();
1542
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001543 // Whatever we were doing is hereby canceled.
1544 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 @Override
1548 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001549 long startTime = 0;
1550 if (DEBUG_RESUME_TIME) {
1551 startTime = System.currentTimeMillis();
1552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 super.onNewIntent(intent);
1554
1555 // Close the menu
1556 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001557 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001558 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001559
Jamie Genniscb222e82012-10-23 15:44:26 -07001560 final boolean alreadyOnHome =
1561 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001562 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001563
Jamie Genniscb222e82012-10-23 15:44:26 -07001564 Runnable processIntent = new Runnable() {
1565 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001566 if (mWorkspace == null) {
1567 // Can be cases where mWorkspace is null, this prevents a NPE
1568 return;
1569 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001570 Folder openFolder = mWorkspace.getOpenFolder();
1571 // In all these cases, only animate if we're already on home
1572 mWorkspace.exitWidgetResizeMode();
1573 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1574 openFolder == null) {
1575 mWorkspace.moveToDefaultScreen(true);
1576 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001577
Jamie Genniscb222e82012-10-23 15:44:26 -07001578 closeFolder();
1579 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001580
Jamie Genniscb222e82012-10-23 15:44:26 -07001581 // If we are already on home, then just animate back to the workspace,
1582 // otherwise, just wait until onResume to set the state back to Workspace
1583 if (alreadyOnHome) {
1584 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001585 if (mWorkspace.isInOverviewMode()) {
1586 mWorkspace.exitOverviewMode();
1587 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001588 } else {
1589 mOnResumeState = State.WORKSPACE;
1590 }
1591
1592 final View v = getWindow().peekDecorView();
1593 if (v != null && v.getWindowToken() != null) {
1594 InputMethodManager imm = (InputMethodManager)getSystemService(
1595 INPUT_METHOD_SERVICE);
1596 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1597 }
1598
1599 // Reset AllApps to its initial state
1600 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1601 mAppsCustomizeTabHost.reset();
1602 }
1603 }
1604 };
1605
1606 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1607 // Delay processing of the intent to allow the status bar animation to finish
1608 // first in order to avoid janky animations.
1609 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001610 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001611 // Process the intent immediately.
1612 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001613 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 }
Michael Jurka447bf842013-05-15 14:52:15 +02001616 if (DEBUG_RESUME_TIME) {
1617 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
1620
1621 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001622 public void onRestoreInstanceState(Bundle state) {
1623 super.onRestoreInstanceState(state);
1624 for (int page: mSynchronouslyBoundPages) {
1625 mWorkspace.restoreInstanceStateForChild(page);
1626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
1628
1629 @Override
1630 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001631 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001632 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633
Michael Jurka883f55b2010-10-21 15:47:14 -07001634 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001635 // We close any open folder since it will not be re-opened, and we need to make sure
1636 // this state is reflected.
1637 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638
Adam Cohendcd297f2013-06-18 13:13:40 -07001639 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001640 mWaitingForResult) {
1641 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001642 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001643 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1644 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001645 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1646 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1647 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
1649
1650 if (mFolderInfo != null && mWaitingForResult) {
1651 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1652 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1653 }
Michael Jurka946ad472010-07-09 18:05:18 -07001654
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 // Save the current AppsCustomize tab
1656 if (mAppsCustomizeTabHost != null) {
1657 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1658 if (currentTabTag != null) {
1659 outState.putString("apps_customize_currentTab", currentTabTag);
1660 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001661 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1662 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
1666 @Override
1667 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001669
Winson Chunge7a03942011-08-05 15:05:12 -07001670 // Remove all pending runnables
1671 mHandler.removeMessages(ADVANCE_MSG);
1672 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001673 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001674
Winson Chungcd2b0142011-06-08 16:02:26 -07001675 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001676 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001677 mModel.stopLoader();
1678 app.setLauncher(null);
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001681 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001683 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001685 mAppWidgetHost = null;
1686
1687 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
1689 TextKeyListener.getInstance().release();
1690
Winson Chung81b52252012-08-27 15:34:29 -07001691 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1692 // to prevent leaking Launcher activities on orientation change.
1693 if (mModel != null) {
1694 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1695 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001696
1697 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001698 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001699
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001700 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001701 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1702 mWorkspace.removeAllViews();
1703 mWorkspace = null;
1704 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001705
Michael Jurka2ecf9952012-06-18 12:52:28 -07001706 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
1708
Adam Cohena9cf38f2011-05-02 15:36:58 -07001709 public DragController getDragController() {
1710 return mDragController;
1711 }
1712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 @Override
1714 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001715 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 super.startActivityForResult(intent, requestCode);
1717 }
1718
Winson Chung70d72102011-08-12 11:24:35 -07001719 /**
1720 * Indicates that we want global search for this activity by setting the globalSearch
1721 * argument for {@link #startSearch} to true.
1722 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001724 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001726
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001727 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001728
Karl Rosaen138a0412009-04-23 19:00:21 -07001729 if (initialQuery == null) {
1730 // Use any text typed in the launcher as the initial query
1731 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 if (appSearchData == null) {
1734 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001735 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 }
Winson Chungadf0c182012-08-23 14:59:07 -07001737 Rect sourceBounds = new Rect();
1738 if (mSearchDropTargetBar != null) {
1739 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1740 }
Romain Guycbb89e42009-06-08 15:52:54 -07001741
Bjorn Bringertc459e522013-06-07 19:36:01 +01001742 startSearch(initialQuery, selectInitialQuery,
1743 appSearchData, sourceBounds);
1744 }
1745
1746 public void startSearch(String initialQuery,
1747 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001748 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001749 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001750 }
1751
1752 /**
1753 * Starts the global search activity. This code is a copied from SearchManager
1754 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001755 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001756 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001757 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001758 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1759 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1760 if (globalSearchActivity == null) {
1761 Log.w(TAG, "No global search activity found.");
1762 return;
1763 }
1764 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1765 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1766 intent.setComponent(globalSearchActivity);
1767 // Make sure that we have a Bundle to put source in
1768 if (appSearchData == null) {
1769 appSearchData = new Bundle();
1770 } else {
1771 appSearchData = new Bundle(appSearchData);
1772 }
1773 // Set source to package name of app that starts global search, if not set already.
1774 if (!appSearchData.containsKey("source")) {
1775 appSearchData.putString("source", getPackageName());
1776 }
1777 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1778 if (!TextUtils.isEmpty(initialQuery)) {
1779 intent.putExtra(SearchManager.QUERY, initialQuery);
1780 }
1781 if (selectInitialQuery) {
1782 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1783 }
1784 intent.setSourceBounds(sourceBounds);
1785 try {
1786 startActivity(intent);
1787 } catch (ActivityNotFoundException ex) {
1788 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
Winson Chung70d72102011-08-12 11:24:35 -07001792 @Override
1793 public boolean onCreateOptionsMenu(Menu menu) {
1794 if (isWorkspaceLocked()) {
1795 return false;
1796 }
1797
1798 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001799
1800 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1801 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1802 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001803 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1804 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1805 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001806 String helpUrl = getString(R.string.help_url);
1807 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001808 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1809 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1810
Winson Chung70d72102011-08-12 11:24:35 -07001811 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1812 .setIcon(android.R.drawable.ic_menu_gallery)
1813 .setAlphabeticShortcut('W');
1814 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1815 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001816 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001817 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001818 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1819 .setIcon(android.R.drawable.ic_menu_preferences)
1820 .setIntent(settings)
1821 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001822 if (!helpUrl.isEmpty()) {
1823 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1824 .setIcon(android.R.drawable.ic_menu_help)
1825 .setIntent(help)
1826 .setAlphabeticShortcut('H');
1827 }
Winson Chung70d72102011-08-12 11:24:35 -07001828 return true;
1829 }
1830
1831 @Override
1832 public boolean onPrepareOptionsMenu(Menu menu) {
1833 super.onPrepareOptionsMenu(menu);
1834
1835 if (mAppsCustomizeTabHost.isTransitioning()) {
1836 return false;
1837 }
1838 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1839 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1840
1841 return true;
1842 }
1843
1844 @Override
1845 public boolean onOptionsItemSelected(MenuItem item) {
1846 switch (item.getItemId()) {
1847 case MENU_WALLPAPER_SETTINGS:
1848 startWallpaper();
1849 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001850 }
1851
1852 return super.onOptionsItemSelected(item);
1853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001855 @Override
1856 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001857 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001858 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001859 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001860 }
1861
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 public boolean isWorkspaceLocked() {
1863 return mWorkspaceLoading || mWaitingForResult;
1864 }
1865
Michael Jurka0280c3b2010-09-17 15:00:07 -07001866 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001867 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001868 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1870 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001871 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001873 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001874
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001875 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1876 AppWidgetProviderInfo appWidgetInfo) {
1877 if (appWidgetInfo.configure != null) {
1878 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001879
Bjorn Bringert7984c942009-12-09 15:38:25 +00001880 // Launch over to configure widget, if needed
1881 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001882 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001883 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001884 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001886 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001887 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001888 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001889 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001890 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892 }
Romain Guycbb89e42009-06-08 15:52:54 -07001893
Adam Cohenfbba09b2011-07-18 21:43:05 -07001894 /**
1895 * Process a shortcut drop.
1896 *
1897 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001898 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001899 * @param cell The cell it should be added to, optional
1900 * @param position The location on the screen where it was dropped, optional
1901 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001902 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001903 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001904 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001908
1909 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 mPendingAddInfo.cellX = cell[0];
1911 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001912 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001913
1914 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1915 createShortcutIntent.setComponent(componentName);
1916 processShortcut(createShortcutIntent);
1917 }
1918
Adam Cohenfbba09b2011-07-18 21:43:05 -07001919 /**
1920 * Process a widget drop.
1921 *
1922 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001923 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001924 * @param cell The cell it should be added to, optional
1925 * @param position The location on the screen where it was dropped, optional
1926 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001928 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001929 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001933 mPendingAddInfo.minSpanX = info.minSpanX;
1934 mPendingAddInfo.minSpanY = info.minSpanY;
1935
Adam Cohenfbba09b2011-07-18 21:43:05 -07001936 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001937 mPendingAddInfo.cellX = cell[0];
1938 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001939 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001940 if (span != null) {
1941 mPendingAddInfo.spanX = span[0];
1942 mPendingAddInfo.spanY = span[1];
1943 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001944
Adam Cohened66b2b2012-01-23 17:28:51 -08001945 AppWidgetHostView hostView = info.boundWidget;
1946 int appWidgetId;
1947 if (hostView != null) {
1948 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001949 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001950 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001951 // In this case, we either need to start an activity to get permission to bind
1952 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001953 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001954 Bundle options = info.bindOptions;
1955
1956 boolean success = false;
1957 if (options != null) {
1958 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1959 info.componentName, options);
1960 } else {
1961 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1962 info.componentName);
1963 }
1964 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001966 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001968 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1969 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1970 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001971 // TODO: we need to make sure that this accounts for the options bundle.
1972 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001973 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1974 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001975 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001976 }
1977
Joe Onoratodeb98af2010-02-19 14:59:39 -08001978 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001979 // Handle case where user selected "Applications"
1980 String applicationName = getResources().getString(R.string.group_applications);
1981 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001982
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001983 if (applicationName != null && applicationName.equals(shortcutName)) {
1984 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1985 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001986
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001987 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1988 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001989 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001990 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001991 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001992 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995
Winson Chung24ab2f12010-09-16 14:10:47 -07001996 void processWallpaper(Intent intent) {
1997 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1998 }
1999
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002002 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 folderInfo.title = getText(R.string.folder_name);
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002006 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002007 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002008 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009
2010 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002011 FolderIcon newFolder =
2012 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002013 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002014 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07002015 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
Romain Guycbb89e42009-06-08 15:52:54 -07002017
Joe Onorato9c1289c2009-08-17 11:03:03 -04002018 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002019 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002020 }
2021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002023 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07002025 Intent chooser = Intent.createChooser(pickWallpaper,
2026 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002027 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2028 // Removed in Eclair MR1
2029// WallpaperManager wm = (WallpaperManager)
2030// getSystemService(Context.WALLPAPER_SERVICE);
2031// WallpaperInfo wi = wm.getWallpaperInfo();
2032// if (wi != null && wi.getSettingsActivity() != null) {
2033// LabeledIntent li = new LabeledIntent(getPackageName(),
2034// R.string.configure_wallpaper, 0);
2035// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2036// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2037// }
Mike Clerona0618e42009-10-22 13:55:21 -07002038 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002041 /**
2042 * Registers various content observers. The current implementation registers
2043 * only a favorites observer to keep track of the favorites applications.
2044 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002045 private void registerContentObservers() {
2046 ContentResolver resolver = getContentResolver();
2047 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2048 true, mWidgetObserver);
2049 }
2050
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 @Override
2052 public boolean dispatchKeyEvent(KeyEvent event) {
2053 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2054 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002056 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002057 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002058 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002059 dumpState();
2060 return true;
2061 }
2062 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002063 }
2064 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2065 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002066 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 return true;
2068 }
2069 }
2070
2071 return super.dispatchKeyEvent(event);
2072 }
2073
Joe Onorato88ec0992009-11-19 13:16:06 -08002074 @Override
2075 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002076 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002077 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002078 } else if (mWorkspace.isInOverviewMode()) {
2079 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002080 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002081 Folder openFolder = mWorkspace.getOpenFolder();
2082 if (openFolder.isEditingName()) {
2083 openFolder.dismissEditingName();
2084 } else {
2085 closeFolder();
2086 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002087 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002088 mWorkspace.exitWidgetResizeMode();
2089
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002090 // Back button is a no-op here, but give at least some feedback for the button press
2091 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002092 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002093 }
2094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002096 * Re-listen when widgets are reset.
2097 */
2098 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002099 if (mAppWidgetHost != null) {
2100 mAppWidgetHost.startListening();
2101 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002102 }
2103
2104 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 * Launches the intent referred by the clicked shortcut.
2106 *
2107 * @param v The view representing the clicked shortcut.
2108 */
2109 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002110 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2111 // view has detached (it's possible for this to happen if the view is removed mid touch).
2112 if (v.getWindowToken() == null) {
2113 return;
2114 }
2115
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002116 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002117 return;
2118 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002119
Adam Cohenf358a4b2013-07-23 16:47:31 -07002120 if (v instanceof PageIndicator) {
2121 if (!mWorkspace.isInOverviewMode()) {
2122 mWorkspace.enterOverviewMode();
2123 }
2124 return;
2125 }
2126
2127 if (v instanceof CellLayout) {
2128 if (mWorkspace.isInOverviewMode()) {
2129 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2130 }
2131 }
2132
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002134 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002136 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2137 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002138
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002139 // Check for special shortcuts
2140 if (intent.getComponent() != null) {
2141 final String shortcutClass = intent.getComponent().getClassName();
2142
2143 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2144 showAllApps(true);
2145 return;
2146 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2147 MemoryDumpActivity.startDump(this);
2148 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002149 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2150 toggleShowWeightWatcher();
2151 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002152 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002153 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002154
2155 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002156 int[] pos = new int[2];
2157 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002158 intent.setSourceBounds(new Rect(pos[0], pos[1],
2159 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002160
2161 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002162
Daniel Sandlerff02d492013-08-05 02:12:05 -04002163 mStats.recordLaunch(intent, shortcut);
2164
Michael Jurkaddd62e92011-02-16 17:49:14 -08002165 if (success && v instanceof BubbleTextView) {
2166 mWaitingForResume = (BubbleTextView) v;
2167 mWaitingForResume.setStayPressed(true);
2168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002170 if (v instanceof FolderIcon) {
2171 FolderIcon fi = (FolderIcon) v;
2172 handleFolderClick(fi);
2173 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002174 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002175 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002176 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002177 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002178 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 }
2181 }
2182
Michael Jurka0e260592010-06-30 17:07:39 -07002183 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002184 return false;
2185 }
2186
Michael Jurkaaf442092010-06-10 17:01:57 -07002187 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002188 * Event handler for the search button
2189 *
2190 * @param v The view that was clicked.
2191 */
2192 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002193 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2194
Amith Yamasania135ba82011-08-09 17:42:01 -07002195 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002196 }
2197
2198 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002199 * Event handler for the voice button
2200 *
2201 * @param v The view that was clicked.
2202 */
2203 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002204 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002205
Bjorn Bringertc459e522013-06-07 19:36:01 +01002206 startVoice();
2207 }
2208
2209 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002210 try {
2211 final SearchManager searchManager =
2212 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2213 ComponentName activityName = searchManager.getGlobalSearchActivity();
2214 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002215 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002216 if (activityName != null) {
2217 intent.setPackage(activityName.getPackageName());
2218 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002219 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002220 } catch (ActivityNotFoundException e) {
2221 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002223 startActivitySafely(null, intent, "onClickVoiceButton");
2224 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002225 }
2226
2227 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002228 * Event handler for the "grid" button that appears on the home screen, which
2229 * enters all apps mode.
2230 *
2231 * @param v The view that was clicked.
2232 */
2233 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002234 showAllApps(true);
2235 }
2236
2237 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002238 // Provide the same haptic feedback that the system offers for virtual keys.
2239 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002240 }
2241
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002242 public void onClickAppMarketButton(View v) {
2243 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002244 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002245 } else {
2246 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002247 }
2248 }
2249
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002250 void startApplicationDetailsActivity(ComponentName componentName) {
2251 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002252 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2253 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002254 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002255 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002256 }
2257
Michael Jurka1e2f4652013-07-08 18:03:46 -07002258 // returns true if the activity was started
2259 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2260 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002261 // System applications cannot be installed. For now, show a toast explaining that.
2262 // We may give them the option of disabling apps this way.
2263 int messageId = R.string.uninstall_system_app_text;
2264 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002265 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002266 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002267 String packageName = componentName.getPackageName();
2268 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002269 Intent intent = new Intent(
2270 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002271 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2272 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002273 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002274 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002275 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002276 }
2277
Michael Jurka86a720e2012-05-09 11:23:23 -07002278 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002282 // Only launch using the new animation if the shortcut has not opted out (this is a
2283 // private contract between launcher and may be ignored in the future).
2284 boolean useLaunchAnimation = (v != null) &&
2285 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2286 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002287 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2288 v.getMeasuredWidth(), v.getMeasuredHeight());
2289
2290 startActivity(intent, opts.toBundle());
2291 } else {
2292 startActivity(intent);
2293 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002294 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 } catch (SecurityException e) {
2296 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002297 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002299 "or use the exported attribute for this activity. "
2300 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002302 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002304
Michael Jurka86a720e2012-05-09 11:23:23 -07002305 boolean startActivitySafely(View v, Intent intent, Object tag) {
2306 boolean success = false;
2307 try {
2308 success = startActivity(v, intent, tag);
2309 } catch (ActivityNotFoundException e) {
2310 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2311 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2312 }
2313 return success;
2314 }
2315
Romain Guy8e633c52010-03-04 12:51:36 -08002316 void startActivityForResultSafely(Intent intent, int requestCode) {
2317 try {
2318 startActivityForResult(intent, requestCode);
2319 } catch (ActivityNotFoundException e) {
2320 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2321 } catch (SecurityException e) {
2322 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2323 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2324 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2325 "or use the exported attribute for this activity.", e);
2326 }
2327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328
Adam Cohena9cf38f2011-05-02 15:36:58 -07002329 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002330 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002331 Folder openFolder = mWorkspace.getFolderForTag(info);
2332
2333 // If the folder info reports that the associated folder is open, then verify that
2334 // it is actually opened. There have been a few instances where this gets out of sync.
2335 if (info.opened && openFolder == null) {
2336 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002337 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002338 info.opened = false;
2339 }
2340
Adam Cohenfb91f302012-06-11 15:45:18 -07002341 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 // Close any open folder
2343 closeFolder();
2344 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002345 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 } else {
2347 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002348 int folderScreen;
2349 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002350 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 // .. and close it
2352 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002353 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 // Close any folder open on the current screen
2355 closeFolder();
2356 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002357 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
2359 }
2360 }
2361 }
2362
Adam Cohen268c4752012-06-06 17:47:33 -07002363 /**
2364 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2365 * in the DragLayer in the exact absolute location of the original FolderIcon.
2366 */
2367 private void copyFolderIconToImage(FolderIcon fi) {
2368 final int width = fi.getMeasuredWidth();
2369 final int height = fi.getMeasuredHeight();
2370
2371 // Lazy load ImageView, Bitmap and Canvas
2372 if (mFolderIconImageView == null) {
2373 mFolderIconImageView = new ImageView(this);
2374 }
2375 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2376 mFolderIconBitmap.getHeight() != height) {
2377 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2378 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2379 }
2380
2381 DragLayer.LayoutParams lp;
2382 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2383 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2384 } else {
2385 lp = new DragLayer.LayoutParams(width, height);
2386 }
2387
Adam Cohen307fe232012-08-16 17:55:58 -07002388 // The layout from which the folder is being opened may be scaled, adjust the starting
2389 // view size by this scale factor.
2390 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002391 lp.customPosition = true;
2392 lp.x = mRectForFolderAnimation.left;
2393 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002394 lp.width = (int) (scale * width);
2395 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002396
2397 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2398 fi.draw(mFolderIconCanvas);
2399 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002400 if (fi.getFolder() != null) {
2401 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2402 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002403 }
Adam Cohen268c4752012-06-06 17:47:33 -07002404 // Just in case this image view is still in the drag layer from a previous animation,
2405 // we remove it and re-add it.
2406 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2407 mDragLayer.removeView(mFolderIconImageView);
2408 }
2409 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002410 if (fi.getFolder() != null) {
2411 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002412 }
Adam Cohen268c4752012-06-06 17:47:33 -07002413 }
2414
Adam Cohen2801caf2011-05-13 20:57:39 -07002415 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002416 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002417 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2418 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2419 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2420
Adam Cohenc51934b2011-07-26 21:07:43 -07002421 FolderInfo info = (FolderInfo) fi.getTag();
2422 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2423 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002424 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2425 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002426 }
2427
Adam Cohen268c4752012-06-06 17:47:33 -07002428 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2429 copyFolderIconToImage(fi);
2430 fi.setVisibility(View.INVISIBLE);
2431
Michael Jurka2ecf9952012-06-18 12:52:28 -07002432 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002433 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002434 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2435 oa.start();
2436 }
2437
Adam Cohen268c4752012-06-06 17:47:33 -07002438 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002439 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002440 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2441 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2442 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2443
Adam Cohen268c4752012-06-06 17:47:33 -07002444 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002445
Adam Cohen268c4752012-06-06 17:47:33 -07002446 // We remove and re-draw the FolderIcon in-case it has changed
2447 mDragLayer.removeView(mFolderIconImageView);
2448 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002449 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002450 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002451 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002452 oa.addListener(new AnimatorListenerAdapter() {
2453 @Override
2454 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002455 if (cl != null) {
2456 cl.clearFolderLeaveBehind();
2457 // Remove the ImageView copy of the FolderIcon and make the original visible.
2458 mDragLayer.removeView(mFolderIconImageView);
2459 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002460 }
2461 }
2462 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002463 oa.start();
2464 }
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002467 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 * is animated relative to the specified View. If the View is null, no animation
2469 * is played.
2470 *
2471 * @param folderInfo The FolderInfo describing the folder to open.
2472 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002473 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002474 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002475 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476
Adam Cohena9cf38f2011-05-02 15:36:58 -07002477 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478
Adam Cohen4554ee12011-08-03 16:13:21 -07002479 // Just verify that the folder hasn't already been added to the DragLayer.
2480 // There was a one-off crash where the folder had a parent already.
2481 if (folder.getParent() == null) {
2482 mDragLayer.addView(folder);
2483 mDragController.addDropTarget((DropTarget) folder);
2484 } else {
2485 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2486 folder.getParent() + ").");
2487 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002488 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002489 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002490
2491 // Notify the accessibility manager that this folder "window" has appeared and occluded
2492 // the workspace items
2493 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2494 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002495 }
2496
2497 public void closeFolder() {
2498 Folder folder = mWorkspace.getOpenFolder();
2499 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002500 if (folder.isEditingName()) {
2501 folder.dismissEditingName();
2502 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002503 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002504
2505 // Dismiss the folder cling
2506 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002507 }
2508 }
2509
2510 void closeFolder(Folder folder) {
2511 folder.getInfo().opened = false;
2512
2513 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2514 if (parent != null) {
2515 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2516 shrinkAndFadeInFolderIcon(fi);
2517 }
2518 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002519
2520 // Notify the accessibility manager that this folder "window" has disappeard and no
2521 // longer occludeds the workspace items
2522 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002526 if (!isDraggingEnabled()) return false;
2527 if (isWorkspaceLocked()) return false;
2528 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002529
2530 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002531 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 }
2533
Michael Jurka0280c3b2010-09-17 15:00:07 -07002534 resetAddInfo();
2535 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002537 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 return true;
2539 }
2540
Winson Chung3d503fb2011-07-13 17:25:49 -07002541 // The hotseat touch handling does not go through Workspace, and we always allow long press
2542 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002543 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002544 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2545 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002546 if (itemUnderLongClick == null) {
2547 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002548 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2549 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002550 // Disabling reordering until we sort out some issues.
Adam Cohenf358a4b2013-07-23 16:47:31 -07002551 if (mWorkspace.isInOverviewMode()) {
2552 mWorkspace.startReordering(v);
Adam Cohendedbd962013-07-11 14:21:49 -07002553 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002554 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002557 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002559 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 }
2561 }
2562 }
2563 return true;
2564 }
2565
Winson Chung3d503fb2011-07-13 17:25:49 -07002566 boolean isHotseatLayout(View layout) {
2567 return mHotseat != null && layout != null &&
2568 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2569 }
2570 Hotseat getHotseat() {
2571 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002572 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002573 View getOverviewPanel() {
2574 return mOverviewPanel;
2575 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002576 SearchDropTargetBar getSearchBar() {
2577 return mSearchDropTargetBar;
2578 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002579
Winson Chung3d503fb2011-07-13 17:25:49 -07002580 /**
2581 * Returns the CellLayout of the specified container at the specified screen.
2582 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002583 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002584 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2585 if (mHotseat != null) {
2586 return mHotseat.getLayout();
2587 } else {
2588 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002589 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002590 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002591 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002592 }
2593 }
2594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 Workspace getWorkspace() {
2596 return mWorkspace;
2597 }
2598
Daniel Sandler843e8602010-06-07 14:59:01 -04002599 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002600 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002601 }
2602
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002603 /**
2604 * Helper method for the cameraZoomIn/cameraZoomOut animations
2605 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 * @param scaleFactor The scale factor used for the zoom
2607 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002610 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002612
Winson Chung18f41f82012-05-09 13:28:10 -07002613 void disableWallpaperIfInAllApps() {
2614 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002615 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002616 if (mAppsCustomizeTabHost != null &&
2617 !mAppsCustomizeTabHost.isTransitioning()) {
2618 updateWallpaperVisibility(false);
2619 }
2620 }
2621 }
2622
Craig Mautner360310b2012-10-26 15:13:08 -07002623 private void setWorkspaceBackground(boolean workspace) {
2624 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002625 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002626 }
2627
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002628 void updateWallpaperVisibility(boolean visible) {
2629 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2630 int curflags = getWindow().getAttributes().flags
2631 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2632 if (wpflags != curflags) {
2633 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2634 }
Craig Mautner360310b2012-10-26 15:13:08 -07002635 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002636 }
2637
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002638 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2639 if (v instanceof LauncherTransitionable) {
2640 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2641 }
2642 }
2643
Michael Jurkabed61d22012-02-14 22:51:29 -08002644 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2645 if (v instanceof LauncherTransitionable) {
2646 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2647 }
Winson Chung70442722012-02-10 15:43:22 -08002648
2649 // Update the workspace transition step as well
2650 dispatchOnLauncherTransitionStep(v, 0f);
2651 }
2652
2653 private void dispatchOnLauncherTransitionStep(View v, float t) {
2654 if (v instanceof LauncherTransitionable) {
2655 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2656 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002657 }
2658
2659 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2660 if (v instanceof LauncherTransitionable) {
2661 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2662 }
Winson Chung70442722012-02-10 15:43:22 -08002663
2664 // Update the workspace transition step as well
2665 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002666 }
2667
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002668 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002669 * Things to test when changing the following seven functions.
2670 * - Home from workspace
2671 * - from center screen
2672 * - from other screens
2673 * - Home from all apps
2674 * - from center screen
2675 * - from other screens
2676 * - Back from all apps
2677 * - from center screen
2678 * - from other screens
2679 * - Launch app from workspace and quit
2680 * - with back
2681 * - with home
2682 * - Launch app from all apps and quit
2683 * - with back
2684 * - with home
2685 * - Go to a screen that's not the default, then all
2686 * apps, and launch and app, and go back
2687 * - with back
2688 * -with home
2689 * - On workspace, long press power and go back
2690 * - with back
2691 * - with home
2692 * - On all apps, long press power and go back
2693 * - with back
2694 * - with home
2695 * - On workspace, power off
2696 * - On all apps, power off
2697 * - Launch an app and turn off the screen while in that app
2698 * - Go back with home key
2699 * - Go back with back key TODO: make this not go to workspace
2700 * - From all apps
2701 * - From workspace
2702 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2703 * - From all apps
2704 * - From the center workspace
2705 * - From another workspace
2706 */
2707
2708 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002709 * Zoom the camera out from the workspace to reveal 'toView'.
2710 * Assumes that the view to show is anchored at either the very top or very bottom
2711 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002712 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002713 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002714 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002715 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002716 mStateAnimation.cancel();
2717 mStateAnimation = null;
2718 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002719 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002720
Winson Chungf0ea4d32011-06-06 14:27:16 -07002721 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2722 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2723 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002724 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002725 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002726 final int startDelay =
2727 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002728
Michael Jurkab3e22d92011-10-31 15:58:33 -07002729 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002730
Michael Jurkad74c9842011-07-10 12:44:21 -07002731 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002732 Animator workspaceAnim =
2733 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734
2735 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002736 toView.setScaleX(scale);
2737 toView.setScaleY(scale);
2738 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2739 scaleAnim.
2740 scaleX(1f).scaleY(1f).
2741 setDuration(duration).
2742 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002743
Winson Chungf0ea4d32011-06-06 14:27:16 -07002744 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002745 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002746 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002747 .ofFloat(toView, "alpha", 0f, 1f)
2748 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002749 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002750 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2751 @Override
2752 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002753 if (animation == null) {
2754 throw new RuntimeException("animation is null");
2755 }
Winson Chung70442722012-02-10 15:43:22 -08002756 float t = (Float) animation.getAnimatedValue();
2757 dispatchOnLauncherTransitionStep(fromView, t);
2758 dispatchOnLauncherTransitionStep(toView, t);
2759 }
2760 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002761
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002762 // toView should appear right at the end of the workspace shrink
2763 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002764 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002765 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002766 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002767
2768 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002769 boolean animationCancelled = false;
2770
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002771 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002772 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002773 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002774 // Prepare the position
2775 toView.setTranslationX(0.0f);
2776 toView.setTranslationY(0.0f);
2777 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002778 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002779 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002780 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002781 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002782 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2783 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002784
Daniel Sandlere4f98912013-06-25 15:13:26 -04002785 if (mWorkspace != null
2786 && !springLoaded
2787 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002788 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002789 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002790 if (!animationCancelled) {
2791 updateWallpaperVisibility(false);
2792 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002793
2794 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002795 if (mSearchDropTargetBar != null) {
2796 mSearchDropTargetBar.hideSearchBar(false);
2797 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002798 }
2799
Adam Cohencff6af82011-09-13 14:51:53 -07002800 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002801 public void onAnimationCancel(Animator animation) {
2802 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002803 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804 });
2805
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002806 if (workspaceAnim != null) {
2807 mStateAnimation.play(workspaceAnim);
2808 }
Michael Jurka1899a362011-11-03 13:50:45 -07002809
2810 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002811
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002812 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2813 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002814
2815 // If any of the objects being animated haven't been measured/laid out
2816 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002817 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002818 (mWorkspace.getMeasuredWidth() == 0) ||
2819 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002820 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002821 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002822
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002823 final AnimatorSet stateAnimation = mStateAnimation;
2824 final Runnable startAnimRunnable = new Runnable() {
2825 public void run() {
2826 // Check that mStateAnimation hasn't changed while
2827 // we waited for a layout/draw pass
2828 if (mStateAnimation != stateAnimation)
2829 return;
2830 setPivotsForZoom(toView, scale);
2831 dispatchOnLauncherTransitionStart(fromView, animated, false);
2832 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002833 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002834 }
2835 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002836 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002837 final ViewTreeObserver observer = toView.getViewTreeObserver();
2838 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2839 public void onGlobalLayout() {
2840 startAnimRunnable.run();
2841 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2842 }
2843 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002844 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002845 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002846 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 } else {
2848 toView.setTranslationX(0.0f);
2849 toView.setTranslationY(0.0f);
2850 toView.setScaleX(1.0f);
2851 toView.setScaleY(1.0f);
2852 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002853 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002854
Daniel Sandlere4f98912013-06-25 15:13:26 -04002855 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002856 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002857
2858 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002859 if (mSearchDropTargetBar != null) {
2860 mSearchDropTargetBar.hideSearchBar(false);
2861 }
Michael Jurkaabded662011-03-04 12:06:57 -08002862 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002864 dispatchOnLauncherTransitionStart(fromView, animated, false);
2865 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002866 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 dispatchOnLauncherTransitionStart(toView, animated, false);
2868 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002869 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002871 }
2872
2873 /**
2874 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002875 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002876 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002877 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002878 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2879 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002880
Michael Jurkab3e22d92011-10-31 15:58:33 -07002881 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002882 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002883 mStateAnimation.cancel();
2884 mStateAnimation = null;
2885 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002886 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002887
Winson Chungf0ea4d32011-06-06 14:27:16 -07002888 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002889 final int fadeOutDuration =
2890 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 final float scaleFactor = (float)
2892 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2893 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002894 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002895 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896 if (toState == State.WORKSPACE) {
2897 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2898 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002899 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002900 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2901 workspaceAnim = mWorkspace.getChangeStateAnimation(
2902 Workspace.State.SPRING_LOADED, animated);
2903 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002904
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002906 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002907 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002908 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002909 final LauncherViewPropertyAnimator scaleAnim =
2910 new LauncherViewPropertyAnimator(fromView);
2911 scaleAnim.
2912 scaleX(scaleFactor).scaleY(scaleFactor).
2913 setDuration(duration).
2914 setInterpolator(new Workspace.ZoomInInterpolator());
2915
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002916 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002917 .ofFloat(fromView, "alpha", 1f, 0f)
2918 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002919 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002920 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2921 @Override
2922 public void onAnimationUpdate(ValueAnimator animation) {
2923 float t = 1f - (Float) animation.getAnimatedValue();
2924 dispatchOnLauncherTransitionStep(fromView, t);
2925 dispatchOnLauncherTransitionStep(toView, t);
2926 }
2927 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002928
Michael Jurka2ecf9952012-06-18 12:52:28 -07002929 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002930
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002931 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2932 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002933 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002934
2935 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002936 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002937 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002938 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002939 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002940 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2941 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002942 if (onCompleteRunnable != null) {
2943 onCompleteRunnable.run();
2944 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002945 mAppsCustomizeContent.updateCurrentPageScroll();
2946 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002947 }
2948 });
2949
Winson Chungf0ea4d32011-06-06 14:27:16 -07002950 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002951 if (workspaceAnim != null) {
2952 mStateAnimation.play(workspaceAnim);
2953 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002954 dispatchOnLauncherTransitionStart(fromView, animated, true);
2955 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002956 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 } else {
2958 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002959 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002960 dispatchOnLauncherTransitionStart(fromView, animated, true);
2961 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002962 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002963 dispatchOnLauncherTransitionStart(toView, animated, true);
2964 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002965 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002966 }
2967
Michael Jurkae326f182011-11-21 14:05:46 -08002968 @Override
2969 public void onTrimMemory(int level) {
2970 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002971 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002972 mAppsCustomizeTabHost.onTrimMemory();
2973 }
2974 }
2975
Winson Chung18f41f82012-05-09 13:28:10 -07002976 @Override
2977 public void onWindowFocusChanged(boolean hasFocus) {
2978 if (!hasFocus) {
2979 // When another window occludes launcher (like the notification shade, or recents),
2980 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2981 updateWallpaperVisibility(true);
2982 } else {
2983 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002984 mWorkspace.postDelayed(new Runnable() {
2985 @Override
2986 public void run() {
2987 disableWallpaperIfInAllApps();
2988 }
2989 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002990 }
2991 }
2992
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002993 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002994 showWorkspace(animated, null);
2995 }
2996
2997 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002999 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003001 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003002
Winson Chungc7d2b602012-05-16 17:02:20 -07003003 // Show the search bar (only animate if we were showing the drop target bar in spring
3004 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003005 if (mSearchDropTargetBar != null) {
3006 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3007 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003008
Michael Jurkab737ee62011-11-15 15:57:22 -08003009 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07003010 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011
3012 // Set focus to the AppsCustomize button
3013 if (mAllAppsButton != null) {
3014 mAllAppsButton.requestFocus();
3015 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003016 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003017
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003018 // Change the state *after* we've called all the transition code
3019 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003020
Winson Chung5fb63472011-02-02 17:03:37 -08003021 // Resume the auto-advance of widgets
3022 mUserPresent = true;
3023 updateRunning();
3024
alanv1d4fde62012-10-17 13:15:47 -07003025 // Send an accessibility event to announce the context change
3026 getWindow().getDecorView()
3027 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003028
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003029 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003030 }
3031
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003032 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003033 }
3034
Michael Jurkab3e22d92011-10-31 15:58:33 -07003035 void showAllApps(boolean animated) {
3036 if (mState != State.WORKSPACE) return;
3037
3038 showAppsCustomizeHelper(animated, false);
3039 mAppsCustomizeTabHost.requestFocus();
3040
Michael Jurkab3e22d92011-10-31 15:58:33 -07003041 // Change the state *after* we've called all the transition code
3042 mState = State.APPS_CUSTOMIZE;
3043
3044 // Pause the auto-advance of widgets until we are out of AllApps
3045 mUserPresent = false;
3046 updateRunning();
3047 closeFolder();
3048
3049 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003050 getWindow().getDecorView()
3051 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003052 }
3053
Adam Cohen7777d962011-08-18 18:58:38 -07003054 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003055 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003056 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003057 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003058 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003059 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003060 }
Adam Cohen7777d962011-08-18 18:58:38 -07003061
Adam Cohened66b2b2012-01-23 17:28:51 -08003062 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3063 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003064 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3065
Winson Chunge7a03942011-08-05 15:05:12 -07003066 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003067 @Override
3068 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003069 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003070 // Before we show workspace, hide all apps again because
3071 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3072 // clean up our state transition functions
3073 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003074 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003075 } else {
3076 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003077 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003078 }
3079 }, (extendedDelay ?
3080 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3081 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3082 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083
Michael Jurkad3ef3062010-11-23 16:23:58 -08003084 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003085 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003086 final boolean animated = true;
3087 final boolean springLoaded = true;
3088 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003089 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003090 }
3091 // Otherwise, we are not in spring loaded mode, so don't do anything.
3092 }
3093
Michael Jurkab737ee62011-11-15 15:57:22 -08003094 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003095 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003096 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003097 }
3098 }
3099
3100 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003101 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003102 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003103 if (mDividerAnimator != null) {
3104 mDividerAnimator.cancel();
3105 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003106 mDividerAnimator = null;
3107 }
3108 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003109 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003110 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003111 int duration = 0;
3112 if (mSearchDropTargetBar != null) {
3113 duration = mSearchDropTargetBar.getTransitionInDuration();
3114 }
3115 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003116 mDividerAnimator.start();
3117 }
3118 }
3119 }
3120
Michael Jurkab3e22d92011-10-31 15:58:33 -07003121 void lockAllApps() {
3122 // TODO
3123 }
3124
3125 void unlockAllApps() {
3126 // TODO
3127 }
3128
Winson Chungf0ea4d32011-06-06 14:27:16 -07003129 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003130 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003133 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003134 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003135 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003136 int duration = 0;
3137 if (mSearchDropTargetBar != null) {
3138 duration = mSearchDropTargetBar.getTransitionInDuration();
3139 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003140 mHotseat.animate().alpha(1f).setDuration(duration);
3141 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003142 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003143 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003144 }
3145 }
3146 }
3147
3148 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003149 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003150 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003152 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003153 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003154 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003155 int duration = 0;
3156 if (mSearchDropTargetBar != null) {
3157 duration = mSearchDropTargetBar.getTransitionOutDuration();
3158 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003159 mHotseat.animate().alpha(0f).setDuration(duration);
3160 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003161 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003163 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003164 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003165 }
3166
Patrick Dubroy5f445422011-02-18 14:35:21 -08003167 /**
3168 * Add an item from all apps or customize onto the given workspace screen.
3169 * If layout is null, add to the current screen.
3170 */
3171 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003172 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003173 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003174 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003175 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003176
Winson Chungdff8ebb2011-09-08 17:25:31 -07003177 /** Maps the current orientation to an index for referencing orientation correct global icons */
3178 private int getCurrentOrientationIndexForGlobalIcons() {
3179 // default - 0, landscape - 1
3180 switch (getResources().getConfiguration().orientation) {
3181 case Configuration.ORIENTATION_LANDSCAPE:
3182 return 1;
3183 default:
3184 return 0;
3185 }
3186 }
3187
Michael Jurkaae65ee32012-04-30 11:45:54 -07003188 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003189 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003190 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003191 // Look for the toolbar icon specified in the activity meta-data
3192 Bundle metaData = packageManager.getActivityInfo(
3193 activityName, PackageManager.GET_META_DATA).metaData;
3194 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003196 if (iconResId != 0) {
3197 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003198 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003199 }
3200 }
3201 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003202 // This can happen if the activity defines an invalid drawable
3203 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3204 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003205 } catch (Resources.NotFoundException nfe) {
3206 // This can happen if the activity defines an invalid drawable
3207 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3208 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003209 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210 return null;
3211 }
3212
3213 // if successful in getting icon, return it; otherwise, set button to use default drawable
3214 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003215 int buttonId, ComponentName activityName, int fallbackDrawableId,
3216 String toolbarResourceName) {
3217 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003218 Resources r = getResources();
3219 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3220 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003221
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003222 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003223 // If we were unable to find the icon via the meta-data, use a generic one
3224 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003225 toolbarIcon = r.getDrawable(fallbackDrawableId);
3226 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003227 if (button != null) {
3228 button.setCompoundDrawables(toolbarIcon, null, null, null);
3229 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003230 return null;
3231 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003232 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003233 if (button != null) {
3234 button.setCompoundDrawables(toolbarIcon, null, null, null);
3235 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003236 return toolbarIcon.getConstantState();
3237 }
3238 }
3239
3240 // if successful in getting icon, return it; otherwise, set button to use default drawable
3241 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003242 int buttonId, ComponentName activityName, int fallbackDrawableId,
3243 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003244 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003245 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003246
Michael Jurka19e0fc52011-07-22 18:00:21 -07003247 if (button != null) {
3248 // If we were unable to find the icon via the meta-data, use a
3249 // generic one
3250 if (toolbarIcon == null) {
3251 button.setImageResource(fallbackDrawableId);
3252 } else {
3253 button.setImageDrawable(toolbarIcon);
3254 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003255 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003256
3257 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3258
Michael Jurka0423dcf2010-10-05 14:56:18 -07003259 }
3260
Winson Chung1b884092012-06-08 17:13:02 -07003261 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003262 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003263 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003264 }
3265
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003266 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003267 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003268 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003269 }
3270
Winson Chungbb185bd2011-11-21 12:31:42 -08003271 private void invalidatePressedFocusedStates(View container, View button) {
3272 if (container instanceof HolographicLinearLayout) {
3273 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3274 layout.invalidatePressedFocusedStates();
3275 } else if (button instanceof HolographicImageView) {
3276 HolographicImageView view = (HolographicImageView) button;
3277 view.invalidatePressedFocusedStates();
3278 }
3279 }
3280
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 private boolean updateGlobalSearchIcon() {
3282 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003283 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003285 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003286
Winson Chung1cad91e2011-05-25 17:41:01 -07003287 final SearchManager searchManager =
3288 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3289 ComponentName activityName = searchManager.getGlobalSearchActivity();
3290 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003291 int coi = getCurrentOrientationIndexForGlobalIcons();
3292 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003293 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3294 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3295 if (sGlobalSearchIcon[coi] == null) {
3296 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3297 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3298 TOOLBAR_ICON_METADATA_NAME);
3299 }
3300
Winson Chungc51db6a2011-10-05 11:44:49 -07003301 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3302 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003303 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003305 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003306 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003307 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3308 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3309 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003310 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003311 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003312 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003313 }
3314 }
3315
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003316 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003317 final View searchButtonContainer = findViewById(R.id.search_button_container);
3318 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003319 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003320 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003321 }
3322
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003324 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003325 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003326
Winson Chungc51db6a2011-10-05 11:44:49 -07003327 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003328 final SearchManager searchManager =
3329 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3330 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3331
3332 ComponentName activityName = null;
3333 if (globalSearchActivity != null) {
3334 // Check if the global search activity handles voice search
3335 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3336 intent.setPackage(globalSearchActivity.getPackageName());
3337 activityName = intent.resolveActivity(getPackageManager());
3338 }
3339
3340 if (activityName == null) {
3341 // Fallback: check if an activity other than the global search activity
3342 // resolves this
3343 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3344 activityName = intent.resolveActivity(getPackageManager());
3345 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003346 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003347 int coi = getCurrentOrientationIndexForGlobalIcons();
3348 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003349 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3350 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3351 if (sVoiceSearchIcon[coi] == null) {
3352 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3353 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3354 TOOLBAR_ICON_METADATA_NAME);
3355 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003356 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003357 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003358 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003359 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003360 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003361 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003362 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003363 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003364 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003365 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003366 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003367 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003368
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003369 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003370 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3371 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003372 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003373 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003374 }
3375
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003376 public void setVoiceButtonProxyVisible(boolean visible) {
3377 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3378 if (voiceButtonProxy != null) {
3379 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3380 }
3381 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003382 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003383 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003384 */
3385 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003386 final View marketButton = findViewById(R.id.market_button);
3387 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3388 // Find the app market activity by resolving an intent.
3389 // (If multiple app markets are installed, it will return the ResolverActivity.)
3390 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003391 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003392 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003393 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003394 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003395 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3396 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003397 marketButton.setVisibility(View.VISIBLE);
3398 } else {
3399 // We should hide and disable the view so that we don't try and restore the visibility
3400 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3401 marketButton.setVisibility(View.GONE);
3402 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003403 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003404 }
3405
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003406 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003407 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3408 Resources r = getResources();
3409 Drawable marketIconDrawable = d.newDrawable(r);
3410 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3411 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3412 marketIconDrawable.setBounds(0, 0, w, h);
3413
3414 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003415 }
3416
Michael Jurkad7c28052012-04-27 15:43:36 -07003417 @Override
3418 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003419 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003420 final List<CharSequence> text = event.getText();
3421 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003422 // Populate event with a fake title based on the current state.
3423 if (mState == State.APPS_CUSTOMIZE) {
3424 text.add(getString(R.string.all_apps_button_label));
3425 } else {
3426 text.add(getString(R.string.all_apps_home_button_label));
3427 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003428 return result;
3429 }
3430
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003431 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003432 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003433 */
3434 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3435 @Override
3436 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003437 closeSystemDialogs();
3438 }
3439 }
3440
3441 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003442 * Receives notifications whenever the appwidgets are reset.
3443 */
3444 private class AppWidgetResetObserver extends ContentObserver {
3445 public AppWidgetResetObserver() {
3446 super(new Handler());
3447 }
3448
3449 @Override
3450 public void onChange(boolean selfChange) {
3451 onAppWidgetReset();
3452 }
3453 }
3454
3455 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003456 * If the activity is currently paused, signal that we need to run the passed Runnable
3457 * in onResume.
3458 *
3459 * This needs to be called from incoming places where resources might have been loaded
3460 * while we are paused. That is becaues the Configuration might be wrong
3461 * when we're not running, and if it comes back to what it was when we
3462 * were paused, we are not restarted.
3463 *
3464 * Implementation of the method from LauncherModel.Callbacks.
3465 *
3466 * @return true if we are currently paused. The caller might be able to
3467 * skip some work in that case since we will come back again.
3468 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003469 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003470 if (mPaused) {
3471 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003472 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003473 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003474 }
3475 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003476 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003477 return true;
3478 } else {
3479 return false;
3480 }
3481 }
3482
Michael Jurkac402cd92013-05-20 15:49:32 +02003483 private boolean waitUntilResume(Runnable run) {
3484 return waitUntilResume(run, false);
3485 }
3486
Michael Jurka1e2f4652013-07-08 18:03:46 -07003487 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003488 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003489 }
3490
3491 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003492 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003493 }
3494
Michael Jurka7607c2f2013-04-03 14:33:19 -07003495 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003496 * If the activity is currently paused, signal that we need to re-run the loader
3497 * in onResume.
3498 *
3499 * This needs to be called from incoming places where resources might have been loaded
3500 * while we are paused. That is becaues the Configuration might be wrong
3501 * when we're not running, and if it comes back to what it was when we
3502 * were paused, we are not restarted.
3503 *
3504 * Implementation of the method from LauncherModel.Callbacks.
3505 *
3506 * @return true if we are currently paused. The caller might be able to
3507 * skip some work in that case since we will come back again.
3508 */
3509 public boolean setLoadOnResume() {
3510 if (mPaused) {
3511 Log.i(TAG, "setLoadOnResume");
3512 mOnResumeNeedsLoad = true;
3513 return true;
3514 } else {
3515 return false;
3516 }
3517 }
3518
3519 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003521 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003523 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003524 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003525 } else {
3526 return SCREEN_COUNT / 2;
3527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 /**
3531 * Refreshes the shortcuts shown on the workspace.
3532 *
3533 * Implementation of the method from LauncherModel.Callbacks.
3534 */
3535 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003536 // If we're starting binding all over again, clear any bind calls we'd postponed in
3537 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3538 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003539 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003540
Michael Jurka7607c2f2013-04-03 14:33:19 -07003541 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003542 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 int count = workspace.getChildCount();
3544 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003545 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003546 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3547 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003549 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003550 if (mHotseat != null) {
3551 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003552 }
3553 }
3554
Adam Cohendcd297f2013-06-18 13:13:40 -07003555 @Override
3556 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003557 bindAddScreens(orderedScreenIds);
3558 mWorkspace.addExtraEmptyScreen();
3559 }
3560
3561 @Override
3562 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003563 int count = orderedScreenIds.size();
3564 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003565 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Winson Chung64359a52013-07-08 17:17:08 -07003566 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003567 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003568 }
3569
Adam Cohen39a06042013-07-19 14:30:12 -07003570 private boolean shouldShowWeightWatcher() {
3571 String spKey = LauncherAppState.getSharedPreferencesKey();
3572 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003573 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003574
3575 return show;
3576 }
3577
3578 private void toggleShowWeightWatcher() {
3579 String spKey = LauncherAppState.getSharedPreferencesKey();
3580 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3581 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3582
3583 show = !show;
3584
3585 SharedPreferences.Editor editor = sp.edit();
3586 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3587 editor.commit();
3588
3589 if (mWeightWatcher != null) {
3590 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3591 }
3592 }
3593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 * Bind the items start-end from the list.
3596 *
3597 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003598 */
Winson Chung64359a52013-07-08 17:17:08 -07003599 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3600 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 if (waitUntilResume(new Runnable() {
3602 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003603 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003604 }
3605 })) {
3606 return;
3607 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003608
Winson Chungf0c6ae02012-03-21 16:10:31 -07003609 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003610 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3611 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003612 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003613 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003614 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003615 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003617
3618 // Short circuit if we are loading dock items for a configuration which has no dock
3619 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3620 mHotseat == null) {
3621 continue;
3622 }
3623
Joe Onorato9c1289c2009-08-17 11:03:03 -04003624 switch (item.itemType) {
3625 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3626 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003627 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003628 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003629
Winson Chung64359a52013-07-08 17:17:08 -07003630 /*
3631 * TODO: FIX collision case
3632 */
Winson Chungce376632013-07-11 16:12:41 -07003633 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3634 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3635 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3636 throw new RuntimeException("OCCUPIED");
3637 }
Winson Chung64359a52013-07-08 17:17:08 -07003638 }
3639
Adam Cohendcd297f2013-06-18 13:13:40 -07003640 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3641 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003642 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003643 // Animate all the applications up now
3644 shortcut.setAlpha(0f);
3645 shortcut.setScaleX(0f);
3646 shortcut.setScaleY(0f);
3647 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003648 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003651 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003652 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003653 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003654 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003655 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3656 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003657 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003658 default:
3659 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003661 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662
Winson Chung997a9232013-07-24 15:33:46 -07003663 if (animateIcons) {
3664 // Animate to the correct page
3665 if (newShortcutsScreenId > -1) {
3666 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3667 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3668 if (newShortcutsScreenId != currentScreenId) {
3669 mWorkspace.snapToPage(newScreenIndex);
3670 }
3671 }
3672
Winson Chung64359a52013-07-08 17:17:08 -07003673 // We post the animation slightly delayed to prevent slowdowns when we are loading
3674 // right after we return to launcher.
3675 mWorkspace.postDelayed(new Runnable() {
3676 public void run() {
3677 anim.playTogether(bounceAnims);
3678 anim.start();
3679 }
3680 }, NEW_APPS_ANIMATION_DELAY);
3681 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003683 }
3684
Joe Onorato9c1289c2009-08-17 11:03:03 -04003685 /**
3686 * Implementation of the method from LauncherModel.Callbacks.
3687 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3689 if (waitUntilResume(new Runnable() {
3690 public void run() {
3691 bindFolders(folders);
3692 }
3693 })) {
3694 return;
3695 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003696 sFolders.clear();
3697 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699
3700 /**
3701 * Add the views for a widget to the workspace.
3702 *
3703 * Implementation of the method from LauncherModel.Callbacks.
3704 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003705 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3706 if (waitUntilResume(new Runnable() {
3707 public void run() {
3708 bindAppWidget(item);
3709 }
3710 })) {
3711 return;
3712 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003713
Daniel Sandler843e8602010-06-07 14:59:01 -04003714 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3715 if (DEBUG_WIDGETS) {
3716 Log.d(TAG, "bindAppWidget: " + item);
3717 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003718 final Workspace workspace = mWorkspace;
3719
3720 final int appWidgetId = item.appWidgetId;
3721 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003722 if (DEBUG_WIDGETS) {
3723 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3724 }
3725
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3727
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003729 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730
Adam Cohendcd297f2013-06-18 13:13:40 -07003731 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003733 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3734
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 workspace.requestLayout();
3736
Daniel Sandler843e8602010-06-07 14:59:01 -04003737 if (DEBUG_WIDGETS) {
3738 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3739 + (SystemClock.uptimeMillis()-start) + "ms");
3740 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 }
3742
Adam Cohen1462de32012-07-24 22:34:36 -07003743 public void onPageBoundSynchronously(int page) {
3744 mSynchronouslyBoundPages.add(page);
3745 }
3746
Joe Onorato9c1289c2009-08-17 11:03:03 -04003747 /**
3748 * Callback saying that there aren't any more items to bind.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
3751 */
Adam Cohene25af792013-06-06 23:08:25 -07003752 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003753 if (waitUntilResume(new Runnable() {
3754 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003755 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003756 }
3757 })) {
3758 return;
3759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 if (mSavedState != null) {
3761 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003762 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003763 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003764 mSavedState = null;
3765 }
3766
Winson Chung98ca0f72013-07-29 12:58:51 -07003767 // Create the custom content page here before onLayout to prevent flashing
3768 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3769 mWorkspace.createCustomContentPage();
3770 }
3771
Adam Cohen1462de32012-07-24 22:34:36 -07003772 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003774 // If we received the result of any pending adds while the loader was running (e.g. the
3775 // widget configuration forced an orientation change), process them now.
3776 for (int i = 0; i < sPendingAddList.size(); i++) {
3777 completeAdd(sPendingAddList.get(i));
3778 }
3779 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780
Winson Chungc51db6a2011-10-05 11:44:49 -07003781 // Update the market app icon as necessary (the other icons will be managed in response to
3782 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003783 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003784
Winson Chungf0c6ae02012-03-21 16:10:31 -07003785 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003786 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003787 mWorkspace.stripDuplicateApps();
3788 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003789 }
Adam Cohen99894d92013-06-14 11:22:59 -07003790
Adam Cohen66a01fd2013-06-11 12:48:00 -07003791 mWorkspace.post(new Runnable() {
3792 @Override
3793 public void run() {
3794 onFinishBindingItems();
3795 }
3796 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003797 }
3798
Winson Chunga2413752012-04-03 14:22:34 -07003799 private boolean canRunNewAppsAnimation() {
3800 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3801 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3802 }
3803
Winson Chungc9168342013-06-26 14:54:55 -07003804 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3805 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3806 PropertyValuesHolder.ofFloat("alpha", 1f),
3807 PropertyValuesHolder.ofFloat("scaleX", 1f),
3808 PropertyValuesHolder.ofFloat("scaleY", 1f));
3809 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3810 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3811 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3812 return bounceAnim;
3813 }
3814
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003815 @Override
3816 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003817 boolean searchVisible = updateGlobalSearchIcon();
3818 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003819 if (mSearchDropTargetBar != null) {
3820 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3821 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003822 }
3823
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003824 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003825 * Add the icons for all apps.
3826 *
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003829 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003830 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3831 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003832 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003833 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 }
3836
3837 /**
3838 * A package was updated.
3839 *
3840 * Implementation of the method from LauncherModel.Callbacks.
3841 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003842 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3843 if (waitUntilResume(new Runnable() {
3844 public void run() {
3845 bindAppsUpdated(apps);
3846 }
3847 })) {
3848 return;
3849 }
3850
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003851 if (mWorkspace != null) {
3852 mWorkspace.updateShortcuts(apps);
3853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003854 }
3855
3856 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003857 * A package was uninstalled. We take both the super set of packageNames
3858 * in addition to specific applications to remove, the reason being that
3859 * this can be called when a package is updated as well. In that scenario,
3860 * we only remove specific components from the workspace, where as
3861 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 *
3863 * Implementation of the method from LauncherModel.Callbacks.
3864 */
Winson Chung83892cc2013-05-01 16:53:33 -07003865 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3866 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003867 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003868 if (waitUntilResume(new Runnable() {
3869 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003870 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003871 }
3872 })) {
3873 return;
3874 }
3875
Winson Chung64359a52013-07-08 17:17:08 -07003876 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003877 mWorkspace.removeItemsByPackageName(packageNames);
3878 } else {
3879 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003880 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003881
Winson Chunga1820962011-10-03 16:31:06 -07003882 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003883 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
Joe Onoratobe386092009-11-17 17:32:16 -08003885
3886 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003887 * A number of packages were updated.
3888 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003889 private ArrayList<Object> mWidgetsAndShortcuts;
3890 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003891 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 bindPackagesUpdated(mWidgetsAndShortcuts);
3893 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003894 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003895 };
3896
3897 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3898 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3899 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003900 return;
3901 }
3902
Winson Chung64359a52013-07-08 17:17:08 -07003903 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003904 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003905 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003906 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003907 }
3908
Winson Chung400438b2011-01-16 17:53:48 -08003909 private int mapConfigurationOriActivityInfoOri(int configOri) {
3910 final Display d = getWindowManager().getDefaultDisplay();
3911 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3912 switch (d.getRotation()) {
3913 case Surface.ROTATION_0:
3914 case Surface.ROTATION_180:
3915 // We are currently in the same basic orientation as the natural orientation
3916 naturalOri = configOri;
3917 break;
3918 case Surface.ROTATION_90:
3919 case Surface.ROTATION_270:
3920 // We are currently in the other basic orientation to the natural orientation
3921 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3922 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3923 break;
3924 }
3925
3926 int[] oriMap = {
3927 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3928 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3929 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3930 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3931 };
3932 // Since the map starts at portrait, we need to offset if this device's natural orientation
3933 // is landscape.
3934 int indexOffset = 0;
3935 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3936 indexOffset = 1;
3937 }
3938 return oriMap[(d.getRotation() + indexOffset) % 4];
3939 }
Adam Cohen446e9402011-09-15 18:21:21 -07003940
Winson Chung4b919f82012-05-01 10:44:08 -07003941 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003942 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003943 getResources().getBoolean(R.bool.allow_rotation);
3944 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003945 }
Winson Chung4b919f82012-05-01 10:44:08 -07003946 public void lockScreenOrientation() {
3947 if (isRotationEnabled()) {
3948 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3949 .getConfiguration().orientation));
3950 }
3951 }
3952 public void unlockScreenOrientation(boolean immediate) {
3953 if (isRotationEnabled()) {
3954 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003955 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003956 } else {
3957 mHandler.postDelayed(new Runnable() {
3958 public void run() {
3959 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3960 }
3961 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003962 }
Winson Chung4b919f82012-05-01 10:44:08 -07003963 }
Winson Chung400438b2011-01-16 17:53:48 -08003964 }
3965
Winson Chung82f55532011-08-09 14:14:23 -07003966 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003968 if (DISABLE_CLINGS) {
3969 return false;
3970 }
3971
Brett Chabot2a9e2282011-08-23 15:03:13 -07003972 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003973 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003974
Michael Jurkae233a8b2013-03-19 13:49:20 +01003975 // Restricted secondary users (child mode) will potentially have very few apps
3976 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003977// boolean supportsLimitedUsers =
3978// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3979// Account[] accounts = AccountManager.get(this).getAccounts();
3980// if (supportsLimitedUsers && accounts.length == 0) {
3981// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3982// Bundle restrictions = um.getUserRestrictions();
3983// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3984// return false;
3985// }
3986// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003987 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003988 }
Michael Jurka22143132012-10-04 17:42:38 +02003989
Winson Chung7d7541e2011-09-16 20:14:36 -07003990 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003991 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003992 if (cling != null) {
3993 cling.init(this, positionData);
3994 cling.setVisibility(View.VISIBLE);
3995 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3996 if (animate) {
3997 cling.buildLayer();
3998 cling.setAlpha(0f);
3999 cling.animate()
4000 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004001 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004002 .setDuration(SHOW_CLING_DURATION)
4003 .setStartDelay(delay)
4004 .start();
4005 } else {
4006 cling.setAlpha(1f);
4007 }
Michael Jurka22143132012-10-04 17:42:38 +02004008 cling.setFocusableInTouchMode(true);
4009 cling.post(new Runnable() {
4010 public void run() {
4011 cling.setFocusable(true);
4012 cling.requestFocus();
4013 }
4014 });
4015 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4016 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004017 }
4018 return cling;
4019 }
Michael Jurka22143132012-10-04 17:42:38 +02004020
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004022 // To catch cases where siblings of top-level views are made invisible, just check whether
4023 // the cling is directly set to GONE before dismissing it.
4024 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004025 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004027 anim.addListener(new AnimatorListenerAdapter() {
4028 public void onAnimationEnd(Animator animation) {
4029 cling.setVisibility(View.GONE);
4030 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004031 // We should update the shared preferences on a background thread
4032 new Thread("dismissClingThread") {
4033 public void run() {
4034 SharedPreferences.Editor editor = mSharedPrefs.edit();
4035 editor.putBoolean(flag, true);
4036 editor.commit();
4037 }
4038 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004039 };
4040 });
4041 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004042 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004043 }
4044 }
Michael Jurka22143132012-10-04 17:42:38 +02004045
Winson Chung9d9d74f2011-09-19 11:49:12 -07004046 private void removeCling(int id) {
4047 final View cling = findViewById(id);
4048 if (cling != null) {
4049 final ViewGroup parent = (ViewGroup) cling.getParent();
4050 parent.post(new Runnable() {
4051 @Override
4052 public void run() {
4053 parent.removeView(cling);
4054 }
4055 });
Michael Jurka22143132012-10-04 17:42:38 +02004056 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004057 }
4058 }
Michael Jurka974c3862012-05-22 22:00:31 -07004059
4060 private boolean skipCustomClingIfNoAccounts() {
4061 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4062 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4063 if (customCling) {
4064 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004065 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004066 Account[] accounts = am.getAccountsByType("com.google");
4067 return accounts.length == 0;
4068 }
4069 return false;
4070 }
4071
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004074 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004075 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4076 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004077 // If we're not using the default workspace layout, replace workspace cling
4078 // with a custom workspace cling (usually specified in an overlay)
4079 // For now, only do this on tablets
4080 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004081 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004082 // Use a custom cling
4083 View cling = findViewById(R.id.workspace_cling);
4084 ViewGroup clingParent = (ViewGroup) cling.getParent();
4085 int clingIndex = clingParent.indexOfChild(cling);
4086 clingParent.removeViewAt(clingIndex);
4087 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4088 clingParent.addView(customCling, clingIndex);
4089 customCling.setId(R.id.workspace_cling);
4090 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004091 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004092 } else {
4093 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 }
4095 }
4096 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004097 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004098 if (isClingsEnabled() &&
4099 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004101 } else {
4102 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 }
4104 }
4105 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004106 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004107 if (isClingsEnabled() &&
4108 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4109 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004110 } else {
4111 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004112 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 }
4115 public boolean isFolderClingVisible() {
4116 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004117 if (cling != null) {
4118 return cling.getVisibility() == View.VISIBLE;
4119 }
4120 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004121 }
Winson Chung82f55532011-08-09 14:14:23 -07004122 public void dismissWorkspaceCling(View v) {
4123 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004125 }
4126 public void dismissAllAppsCling(View v) {
4127 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004128 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4129 }
4130 public void dismissFolderCling(View v) {
4131 Cling cling = (Cling) findViewById(R.id.folder_cling);
4132 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004133 }
4134
Winson Chung80baf5a2010-08-09 16:03:15 -07004135 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004136 * Prints out out state for debugging.
4137 */
4138 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004139 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004140 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004141 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4142 Log.d(TAG, "mRestoring=" + mRestoring);
4143 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4144 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004145 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004146 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004147
Winson Chung785d2eb2011-04-14 16:08:02 -07004148 if (mAppsCustomizeContent != null) {
4149 mAppsCustomizeContent.dumpState();
4150 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004151 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004152 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004153
4154 @Override
4155 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4156 super.dump(prefix, fd, writer, args);
4157 writer.println(" ");
4158 writer.println("Debug logs: ");
4159 for (int i = 0; i < sDumpLogs.size(); i++) {
4160 writer.println(" " + sDumpLogs.get(i));
4161 }
4162 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004163
4164 public static void dumpDebugLogsToConsole() {
4165 Log.d(TAG, "");
4166 Log.d(TAG, "*********************");
4167 Log.d(TAG, "Launcher debug logs: ");
4168 for (int i = 0; i < sDumpLogs.size(); i++) {
4169 Log.d(TAG, " " + sDumpLogs.get(i));
4170 }
4171 Log.d(TAG, "*********************");
4172 Log.d(TAG, "");
4173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004174}
Michael Jurka2763be32011-02-24 11:19:57 -08004175
Michael Jurkaabded662011-03-04 12:06:57 -08004176interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004177 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004178 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004179 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004180 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004181 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004182}