blob: 70fa7eb9e3c3f944815069b9741b3ef40c5694af [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;
Craig Mautner360310b2012-10-26 15:13:08 -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;
Craig Mautner360310b2012-10-26 15:13:08 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Daniel Sandler924b9932013-06-12 00:38:25 -040073import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080074import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080075import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070077import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070078import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080079import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040081import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070082
Daniel Sandler325dc232013-06-05 22:57:57 -040083import com.android.launcher3.R;
84import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080085
Adam Cohenc0dcf592011-06-01 15:30:43 -070086import java.io.DataInputStream;
87import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070088import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070089import java.io.FileNotFoundException;
90import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070091import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070092import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070093import java.util.Collection;
94import java.util.Collections;
95import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070097import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -070098import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -070099import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Default launcher application.
103 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100104public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700105 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700106 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700108 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Daniel Sandler924b9932013-06-12 00:38:25 -0400110 static final boolean PROFILE_STARTUP = true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400111 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700112 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandler924b9932013-06-12 00:38:25 -0400113 static final boolean DEBUG_RESUME_TIME = true;
114 static final boolean DEBUG_MEMORY = true;
Romain Guy6fefcf12009-06-11 13:07:43 -0700115
Winson Chung70d72102011-08-12 11:24:35 -0700116 private static final int MENU_GROUP_WALLPAPER = 1;
117 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
118 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700119 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
120 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int REQUEST_PICK_APPLICATION = 6;
125 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700127 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Michael Jurka8b805b12012-04-18 14:23:14 -0700129 private static final int REQUEST_BIND_APPWIDGET = 11;
130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
132
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800133 static final int SCREEN_COUNT = 5;
134 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Romain Guy98d01652009-06-30 16:21:04 -0700136 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800137 // To turn on these properties, type
138 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
139 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
140 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chung2672ff92012-05-04 16:22:30 -0700142 // The Intent extra that defines whether to ignore the launch animation
143 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400144 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: int
147 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700148 // Type: int
149 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
152 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: boolean
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
160 // Type: long
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
166 // Type: parcelable
167 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100169 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700170 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100171 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700172 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100173 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700174
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700176 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700177 private State mState = State.WORKSPACE;
178 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800179 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700180
Joe Onorato9c1289c2009-08-17 11:03:03 -0400181 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700182 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
183 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700184 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700185 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800188 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Winson Chunga2413752012-04-03 14:22:34 -0700190 // How long to wait before the new-shortcut animation automatically pans the workspace
191 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
192
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800193 private final BroadcastReceiver mCloseSystemDialogsReceiver
194 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800195 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private LayoutInflater mInflater;
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private View mQsbDivider;
201 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700202 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800203 private DragLayer mDragLayer;
204 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700206 private AppWidgetManager mAppWidgetManager;
207 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700208
Michael Jurkac9d95c52011-08-29 14:03:34 -0700209 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700210 private AppWidgetProviderInfo mPendingAddWidgetInfo;
211
Michael Jurka0280c3b2010-09-17 15:00:07 -0700212 private int[] mTmpAddItemCellCoordinates = new int[2];
213
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private FolderInfo mFolderInfo;
215
Winson Chung3d503fb2011-07-13 17:25:49 -0700216 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800217 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700218
Winson Chungc51db6a2011-10-05 11:44:49 -0700219 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700220 private AppsCustomizeTabHost mAppsCustomizeTabHost;
221 private AppsCustomizePagedView mAppsCustomizeContent;
222 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700225 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
226 // scroll issues (because the workspace may not have been measured yet) and extra work.
227 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
228 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
230 private SpannableStringBuilder mDefaultKeySsb = null;
231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 private boolean mWorkspaceLoading = true;
233
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800234 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private boolean mRestoring;
236 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700237 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
Michael Jurka7607c2f2013-04-03 14:33:19 -0700239 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
240
Winson Chung4a2afa32012-07-19 14:53:05 -0700241 // Keep track of whether the user has left launcher
242 private static boolean sPausedFromUserAction = false;
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private Bundle mSavedInstanceState;
245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800248 private boolean mUserPresent = true;
249 private boolean mVisible = false;
250 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700251 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700253 private static LocaleConfiguration sLocaleConfiguration = null;
254
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700255 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700257 private Intent mAppMarketIntent = null;
258
Adam Cohended9f8d2010-11-03 13:25:16 -0700259 // Related to the auto-advancing of widgets
260 private final int ADVANCE_MSG = 1;
261 private final int mAdvanceInterval = 20000;
262 private final int mAdvanceStagger = 250;
263 private long mAutoAdvanceSentTime;
264 private long mAutoAdvanceTimeLeft = -1;
265 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
266 new HashMap<View, AppWidgetProviderInfo>();
267
Winson Chung400438b2011-01-16 17:53:48 -0800268 // Determines how long to wait after a rotation before restoring the screen orientation to
269 // match the sensor state.
270 private final int mRestoreScreenOrientationDelay = 500;
271
Michael Jurka4ef207b2010-11-29 17:05:45 -0800272 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700273 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
274 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
275 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800276
Craig Mautner360310b2012-10-26 15:13:08 -0700277 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700278
Adam Cohen1462de32012-07-24 22:34:36 -0700279 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
280
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700281 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
282
Winson Chung46353de2012-02-16 14:05:10 -0800283 // We only want to get the SharedPreferences once since it does an FS stat each time we get
284 // it from the context.
285 private SharedPreferences mSharedPrefs;
286
Adam Cohene25af792013-06-06 23:08:25 -0700287 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
288
Winson Chungf0c6ae02012-03-21 16:10:31 -0700289 // Holds the page that we need to animate to, and the icon views that we need to animate up
290 // when we scroll to that page on resume.
291 private int mNewShortcutAnimatePage = -1;
292 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700293 private ImageView mFolderIconImageView;
294 private Bitmap mFolderIconBitmap;
295 private Canvas mFolderIconCanvas;
296 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700297
Michael Jurkaddd62e92011-02-16 17:49:14 -0800298 private BubbleTextView mWaitingForResume;
299
Michael Jurka22143132012-10-04 17:42:38 +0200300 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
301 = new HideFromAccessibilityHelper();
302
Michael Jurkac1f5d262011-09-30 19:32:27 -0700303 private Runnable mBuildLayersRunnable = new Runnable() {
304 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700305 if (mWorkspace != null) {
306 mWorkspace.buildPageHardwareLayers();
307 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 }
309 };
310
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800311 private static ArrayList<PendingAddArguments> sPendingAddList
312 = new ArrayList<PendingAddArguments>();
313
Michael Jurka0a457bf2012-11-19 14:05:05 -0800314 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
315
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800316 private static class PendingAddArguments {
317 int requestCode;
318 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700319 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 int screen;
321 int cellX;
322 int cellY;
323 }
324
Michael Jurka0a457bf2012-11-19 14:05:05 -0800325 private static boolean isPropertyEnabled(String propertyName) {
326 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700327 }
328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 @Override
330 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700331 if (DEBUG_STRICT_MODE) {
332 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
333 .detectDiskReads()
334 .detectDiskWrites()
335 .detectNetwork() // or .detectAll() for all detectable problems
336 .penaltyLog()
337 .build());
338 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
339 .detectLeakedSqlLiteObjects()
340 .detectLeakedClosableObjects()
341 .penaltyLog()
342 .penaltyDeath()
343 .build());
344 }
345
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400347 LauncherAppState app = LauncherAppState.getInstance();
348 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700349 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800350 mModel = app.setLauncher(this);
351 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400352 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700354
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700355 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700356 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
357 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700358
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700359 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
360 // this also ensures that any synchronous binding below doesn't re-trigger another
361 // LauncherModel load.
362 mPaused = false;
363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200365 android.os.Debug.startMethodTracing(
366 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 }
368
369 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 setContentView(R.layout.launcher);
371 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700372 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800374 registerContentObservers();
375
Joe Onorato7c312c12009-08-13 21:36:53 -0700376 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 mSavedState = savedInstanceState;
379 restoreState(mSavedState);
380
Winson Chunga12a2502010-12-20 14:41:35 -0800381 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700382 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200383 mAppsCustomizeContent.onPackagesUpdated(
384 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700385 }
Winson Chunga12a2502010-12-20 14:41:35 -0800386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 if (PROFILE_STARTUP) {
388 android.os.Debug.stopMethodTracing();
389 }
390
391 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700392 if (sPausedFromUserAction) {
393 // If the user leaves launcher, then we should just load items asynchronously when
394 // they return.
395 mModel.startLoader(true, -1);
396 } else {
397 // We only load the page synchronously if the user rotates (or triggers a
398 // configuration change) while launcher is in the foreground
399 mModel.startLoader(true, mWorkspace.getCurrentPage());
400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 }
402
Michael Jurkac57b7a82011-08-09 22:02:20 -0700403 if (!mModel.isAllAppsLoaded()) {
404 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
405 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 // For handling default keys
409 mDefaultKeySsb = new SpannableStringBuilder();
410 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800411
412 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
413 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800414
Adam Cohenf9426d52012-06-04 17:26:21 -0700415 updateGlobalIcons();
416
417 // On large interfaces, we want the screen to auto-rotate based on the current orientation
418 unlockScreenOrientation(true);
419 }
420
Winson Chung4a2afa32012-07-19 14:53:05 -0700421 protected void onUserLeaveHint() {
422 super.onUserLeaveHint();
423 sPausedFromUserAction = true;
424 }
425
Adam Cohenf9426d52012-06-04 17:26:21 -0700426 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700427 boolean searchVisible = false;
428 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800429 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700430 int coi = getCurrentOrientationIndexForGlobalIcons();
431 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
432 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700433 updateAppMarketIcon();
434 searchVisible = updateGlobalSearchIcon();
435 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700436 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700437 if (sGlobalSearchIcon[coi] != null) {
438 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700439 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700441 if (sVoiceSearchIcon[coi] != null) {
442 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700445 if (sAppMarketIcon[coi] != null) {
446 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800447 }
Winson Chungadf0c182012-08-23 14:59:07 -0700448 if (mSearchDropTargetBar != null) {
449 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 }
Romain Guycbb89e42009-06-08 15:52:54 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700454 if (sLocaleConfiguration == null) {
455 new AsyncTask<Void, Void, LocaleConfiguration>() {
456 @Override
457 protected LocaleConfiguration doInBackground(Void... unused) {
458 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
459 readConfiguration(Launcher.this, localeConfiguration);
460 return localeConfiguration;
461 }
462
463 @Override
464 protected void onPostExecute(LocaleConfiguration result) {
465 sLocaleConfiguration = result;
466 checkForLocaleChange(); // recursive, but now with a locale configuration
467 }
468 }.execute();
469 return;
470 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 final Configuration configuration = getResources().getConfiguration();
473
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 final String locale = configuration.locale.toString();
476
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final int mcc = configuration.mcc;
479
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final int mnc = configuration.mnc;
482
Romain Guy84f296c2009-11-04 15:00:44 -0800483 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484
Romain Guy84f296c2009-11-04 15:00:44 -0800485 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 sLocaleConfiguration.locale = locale;
487 sLocaleConfiguration.mcc = mcc;
488 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700489
Joe Onorato0589f0f2010-02-08 13:44:00 -0800490 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491
492 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
493 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700494 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 public void run() {
496 writeConfiguration(Launcher.this, localeConfiguration);
497 }
498 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700499 }
500 }
501
502 private static class LocaleConfiguration {
503 public String locale;
504 public int mcc = -1;
505 public int mnc = -1;
506 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700507
Romain Guy98d01652009-06-30 16:21:04 -0700508 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
509 DataInputStream in = null;
510 try {
511 in = new DataInputStream(context.openFileInput(PREFERENCES));
512 configuration.locale = in.readUTF();
513 configuration.mcc = in.readInt();
514 configuration.mnc = in.readInt();
515 } catch (FileNotFoundException e) {
516 // Ignore
517 } catch (IOException e) {
518 // Ignore
519 } finally {
520 if (in != null) {
521 try {
522 in.close();
523 } catch (IOException e) {
524 // Ignore
525 }
526 }
527 }
528 }
529
530 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
531 DataOutputStream out = null;
532 try {
533 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
534 out.writeUTF(configuration.locale);
535 out.writeInt(configuration.mcc);
536 out.writeInt(configuration.mnc);
537 out.flush();
538 } catch (FileNotFoundException e) {
539 // Ignore
540 } catch (IOException e) {
541 //noinspection ResultOfMethodCallIgnored
542 context.getFileStreamPath(PREFERENCES).delete();
543 } finally {
544 if (out != null) {
545 try {
546 out.close();
547 } catch (IOException e) {
548 // Ignore
549 }
550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551 }
552 }
553
Bjorn Bringertc459e522013-06-07 19:36:01 +0100554 public LayoutInflater getInflater() {
555 return mInflater;
556 }
557
Adam Cohen716b51e2011-06-30 12:09:54 -0700558 public DragLayer getDragLayer() {
559 return mDragLayer;
560 }
561
Winson Chung36a62fe2012-05-06 18:04:42 -0700562 boolean isDraggingEnabled() {
563 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
564 // that is subsequently removed from the workspace in startBinding().
565 return !mModel.isLoadingWorkspace();
566 }
567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 static int getScreen() {
569 synchronized (sLock) {
570 return sScreen;
571 }
572 }
573
574 static void setScreen(int screen) {
575 synchronized (sLock) {
576 sScreen = screen;
577 }
578 }
579
Winson Chung557d6ed2011-07-08 15:34:52 -0700580 /**
581 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
582 * a configuration step, this allows the proper animations to run after other transitions.
583 */
584 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700585 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800586 switch (args.requestCode) {
587 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700588 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
589 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800590 break;
591 case REQUEST_PICK_SHORTCUT:
592 processShortcut(args.intent);
593 break;
594 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700595 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
596 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700597 result = true;
598 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 case REQUEST_CREATE_APPWIDGET:
600 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 result = true;
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_PICK_WALLPAPER:
605 // We just wanted the activity result here so we can clear mWaitingForResult
606 break;
607 }
Michael Jurka27614d22012-04-02 06:40:22 -0700608 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
609 // if you turned the screen off and then back while in All Apps, Launcher would not
610 // return to the workspace. Clearing mAddInfo.container here fixes this issue
611 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700612 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800613 }
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 protected void onActivityResult(
617 final int requestCode, final int resultCode, final Intent data) {
618 if (requestCode == REQUEST_BIND_APPWIDGET) {
619 int appWidgetId = data != null ?
620 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
621 if (resultCode == RESULT_CANCELED) {
622 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
623 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700624 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700625 }
626 return;
627 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700628 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800629 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
630 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700631 mWaitingForResult = false;
632
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 // We have special handling for widgets
634 if (isWidgetDrop) {
635 int appWidgetId = data != null ?
636 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700637 if (appWidgetId < 0) {
638 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
639 "widget configuration activity.");
640 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
641 } else {
642 completeTwoStageWidgetDrop(resultCode, appWidgetId);
643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 return;
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 // The pattern used here is that a user PICKs a specific application,
648 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
651 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700652 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 final PendingAddArguments args = new PendingAddArguments();
654 args.requestCode = requestCode;
655 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 args.container = mPendingAddInfo.container;
657 args.screen = mPendingAddInfo.screen;
658 args.cellX = mPendingAddInfo.cellX;
659 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 if (isWorkspaceLocked()) {
661 sPendingAddList.add(args);
662 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700663 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
669 null);
670 }
671
672 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700674 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 Runnable onCompleteRunnable = null;
676 int animationType = 0;
677
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700678 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 if (resultCode == RESULT_OK) {
680 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
681 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700682 mPendingAddWidgetInfo);
683 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 onCompleteRunnable = new Runnable() {
685 @Override
686 public void run() {
687 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
688 mPendingAddInfo.screen, layout, null);
689 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
690 null);
691 }
692 };
693 } else if (resultCode == RESULT_CANCELED) {
694 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
695 onCompleteRunnable = new Runnable() {
696 @Override
697 public void run() {
698 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
699 null);
700 }
701 };
702 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700703 if (mDragLayer.getAnimatedView() != null) {
704 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
705 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
706 animationType, boundWidget, true);
707 } else {
708 // The animated view may be null in the case of a rotation during widget configuration
709 onCompleteRunnable.run();
710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712
713 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700714 protected void onStop() {
715 super.onStop();
716 FirstFrameAnimatorHelper.setIsVisible(false);
717 }
718
719 @Override
720 protected void onStart() {
721 super.onStart();
722 FirstFrameAnimatorHelper.setIsVisible(true);
723 }
724
725 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200727 long startTime = 0;
728 if (DEBUG_RESUME_TIME) {
729 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400730 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700733
Winson Chung4a2afa32012-07-19 14:53:05 -0700734 // Restore the previous launcher state
735 if (mOnResumeState == State.WORKSPACE) {
736 showWorkspace(false);
737 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
738 showAllApps(false);
739 }
740 mOnResumeState = State.NONE;
741
Craig Mautner360310b2012-10-26 15:13:08 -0700742 // Background was set to gradient in onPause(), restore to black if in all apps.
743 setWorkspaceBackground(mState == State.WORKSPACE);
744
Winson Chungde0fb8f2012-05-08 14:37:08 -0700745 // Process any items that were added while Launcher was away
746 InstallShortcutReceiver.flushInstallQueue(this);
747
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800748 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700749 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700750 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400751 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700752 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400753 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700754 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200756 if (mOnResumeCallbacks.size() > 0) {
757 // We might have postponed some bind calls until onResume (see waitUntilResume) --
758 // execute them here
759 long startTimeCallbacks = 0;
760 if (DEBUG_RESUME_TIME) {
761 startTimeCallbacks = System.currentTimeMillis();
762 }
763
764 if (mAppsCustomizeContent != null) {
765 mAppsCustomizeContent.setBulkBind(true);
766 }
767 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
768 mOnResumeCallbacks.get(i).run();
769 }
770 if (mAppsCustomizeContent != null) {
771 mAppsCustomizeContent.setBulkBind(false);
772 }
773 mOnResumeCallbacks.clear();
774 if (DEBUG_RESUME_TIME) {
775 Log.d(TAG, "Time spent processing callbacks in onResume: " +
776 (System.currentTimeMillis() - startTimeCallbacks));
777 }
Michael Jurka447bf842013-05-15 14:52:15 +0200778 }
Winson Chunge4e50662012-01-23 14:45:13 -0800779
780 // Reset the pressed state of icons that were locked in the press state while activities
781 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800782 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800783 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800784 mWaitingForResume.setStayPressed(false);
785 }
Winson Chunge4e50662012-01-23 14:45:13 -0800786 if (mAppsCustomizeContent != null) {
787 // Resets the previous all apps icon press state
788 mAppsCustomizeContent.resetDrawableState();
789 }
Adam Cohen06dff352012-06-01 17:17:08 -0700790 // It is possible that widgets can receive updates while launcher is not in the foreground.
791 // Consequently, the widgets will be inflated in the orientation of the foreground activity
792 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
793 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700794 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700795
796 // Again, as with the above scenario, it's possible that one or more of the global icons
797 // were updated in the wrong orientation.
798 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200799 if (DEBUG_RESUME_TIME) {
800 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
801 }
Adam Cohen06dff352012-06-01 17:17:08 -0700802 }
803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700805 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700806 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
807 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
808 // when Launcher resumes and we are still in AllApps.
809 updateWallpaperVisibility(true);
810
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700811 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700812 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800813 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700814 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700815 }
Romain Guycbb89e42009-06-08 15:52:54 -0700816
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700817 @Override
818 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400819 // Flag the loader to stop early before switching
820 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 if (mAppsCustomizeContent != null) {
822 mAppsCustomizeContent.surrender();
823 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800824 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700825 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700826
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800827 // We can't hide the IME if it was forced open. So don't bother
828 /*
829 @Override
830 public void onWindowFocusChanged(boolean hasFocus) {
831 super.onWindowFocusChanged(hasFocus);
832
833 if (hasFocus) {
834 final InputMethodManager inputManager = (InputMethodManager)
835 getSystemService(Context.INPUT_METHOD_SERVICE);
836 WindowManager.LayoutParams lp = getWindow().getAttributes();
837 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
838 android.os.Handler()) {
839 protected void onReceiveResult(int resultCode, Bundle resultData) {
840 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
841 }
842 });
843 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
844 }
845 }
846 */
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 private boolean acceptFilter() {
849 final InputMethodManager inputManager = (InputMethodManager)
850 getSystemService(Context.INPUT_METHOD_SERVICE);
851 return !inputManager.isFullscreenMode();
852 }
853
854 @Override
855 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700856 final int uniChar = event.getUnicodeChar();
857 final boolean handled = super.onKeyDown(keyCode, event);
858 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
859 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
861 keyCode, event);
862 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700863 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700864 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700865 // showSearchDialog()
866 // If there are multiple keystrokes before the search dialog takes focus,
867 // onSearchRequested() will be called for every keystroke,
868 // but it is idempotent, so it's fine.
869 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 }
871 }
872
Joe Onorato8a9625e2010-01-28 15:55:35 -0800873 // Eat the long press event so the keyboard doesn't come up.
874 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
875 return true;
876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 return handled;
879 }
880
Karl Rosaen138a0412009-04-23 19:00:21 -0700881 private String getTypedText() {
882 return mDefaultKeySsb.toString();
883 }
884
885 private void clearTypedText() {
886 mDefaultKeySsb.clear();
887 mDefaultKeySsb.clearSpans();
888 Selection.setSelection(mDefaultKeySsb, 0);
889 }
890
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700892 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
893 * State
894 */
895 private static State intToState(int stateOrdinal) {
896 State state = State.WORKSPACE;
897 final State[] stateValues = State.values();
898 for (int i = 0; i < stateValues.length; i++) {
899 if (stateValues[i].ordinal() == stateOrdinal) {
900 state = stateValues[i];
901 break;
902 }
903 }
904 return state;
905 }
906
907 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 * Restores the previous state, if it exists.
909 *
910 * @param savedState The previous state.
911 */
912 private void restoreState(Bundle savedState) {
913 if (savedState == null) {
914 return;
915 }
916
Michael Jurka883f55b2010-10-21 15:47:14 -0700917 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700918 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700919 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800920 }
921
Winson Chungf0c6ae02012-03-21 16:10:31 -0700922 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700924 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 }
926
Winson Chung3d503fb2011-07-13 17:25:49 -0700927 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
928 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929
Winson Chung3d503fb2011-07-13 17:25:49 -0700930 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
931 mPendingAddInfo.container = pendingAddContainer;
932 mPendingAddInfo.screen = pendingAddScreen;
933 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
934 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
936 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
937 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700938 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 mRestoring = true;
940 }
941
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
944 if (renameFolder) {
945 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700946 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 mRestoring = true;
948 }
Winson Chunga12a2502010-12-20 14:41:35 -0800949
Winson Chung785d2eb2011-04-14 16:08:02 -0700950
951 // Restore the AppsCustomize tab
952 if (mAppsCustomizeTabHost != null) {
953 String curTab = savedState.getString("apps_customize_currentTab");
954 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700955 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700956 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700957 mAppsCustomizeContent.loadAssociatedPages(
958 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700959 }
960
Winson Chung5afbf7b2011-07-25 11:53:08 -0700961 int currentIndex = savedState.getInt("apps_customize_currentIndex");
962 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 /**
967 * Finds all the views we need and configure them properly.
968 */
969 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700970 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400971
Craig Mautner360310b2012-10-26 15:13:08 -0700972 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700973 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
974 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700975 mQsbDivider = findViewById(R.id.qsb_divider);
976 mDockDivider = findViewById(R.id.dock_divider);
977
978 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
979 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
Winson Chung4c98d922011-05-31 16:50:48 -0700981 // Setup the drag layer
982 mDragLayer.setup(this, dragController);
983
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 // Setup the hotseat
985 mHotseat = (Hotseat) findViewById(R.id.hotseat);
986 if (mHotseat != null) {
987 mHotseat.setup(this);
988 }
989
Winson Chung4c98d922011-05-31 16:50:48 -0700990 // Setup the workspace
991 mWorkspace.setHapticFeedbackEnabled(false);
992 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700993 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700994 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700995
Winson Chungf0ea4d32011-06-06 14:27:16 -0700996 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700997 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700998
Winson Chungf0ea4d32011-06-06 14:27:16 -0700999 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001000 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001001 mAppsCustomizeContent = (AppsCustomizePagedView)
1002 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1003 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001004
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001006 dragController.setDragScoller(mWorkspace);
1007 dragController.setScrollView(mDragLayer);
1008 dragController.setMoveTarget(mWorkspace);
1009 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001010 if (mSearchDropTargetBar != null) {
1011 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001012 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001013
1014 if (DEBUG_MEMORY) {
1015 Log.v(TAG, "adding WeightWatcher");
1016 ((FrameLayout) mLauncherView).addView(new WeightWatcher(this),
1017 new FrameLayout.LayoutParams(
1018 FrameLayout.LayoutParams.MATCH_PARENT,
1019 44,
1020 Gravity.BOTTOM)
1021 );
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024
1025 /**
1026 * Creates a view representing a shortcut.
1027 *
1028 * @param info The data structure describing the shortcut.
1029 *
1030 * @return A View inflated from R.layout.application.
1031 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001034 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036
1037 /**
1038 * Creates a view representing a shortcut inflated from the specified resource.
1039 *
1040 * @param layoutResId The id of the XML layout used to create the shortcut.
1041 * @param parent The group the shortcut belongs to.
1042 * @param info The data structure describing the shortcut.
1043 *
1044 * @return A View inflated from layoutResId.
1045 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001046 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001047 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1048 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 return favorite;
1051 }
1052
1053 /**
1054 * Add an application shortcut to the workspace.
1055 *
1056 * @param data The intent describing the application.
1057 * @param cellInfo The position on screen where to create the shortcut.
1058 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001059 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001060 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001061 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062
Adam Cohenfbba09b2011-07-18 21:43:05 -07001063 // First we check if we already know the exact location where we want to add this item.
1064 if (cellX >= 0 && cellY >= 0) {
1065 cellXY[0] = cellX;
1066 cellXY[1] = cellY;
1067 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001068 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001069 return;
1070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001072 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001073
Romain Guy73b979d2009-06-09 12:57:21 -07001074 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001075 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001077 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001078 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001079 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001080 } else {
1081 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 /**
1086 * Add a shortcut to the workspace.
1087 *
1088 * @param data The intent describing the shortcut.
1089 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1092 int cellY) {
1093 int[] cellXY = mTmpAddItemCellCoordinates;
1094 int[] touchXY = mPendingAddInfo.dropPos;
1095 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Michael Jurkad3ef3062010-11-23 16:23:58 -08001097 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001098
Adam Cohen558baaf2011-08-15 15:22:57 -07001099 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001100 if (info == null) {
1101 return;
1102 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001103 final View view = createShortcut(info);
1104
Adam Cohenfbba09b2011-07-18 21:43:05 -07001105 // First we check if we already know the exact location where we want to add this item.
1106 if (cellX >= 0 && cellY >= 0) {
1107 cellXY[0] = cellX;
1108 cellXY[1] = cellY;
1109 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001110
1111 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001112 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001113 true, null,null)) {
1114 return;
1115 }
1116 DragObject dragObject = new DragObject();
1117 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001118 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1119 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001120 return;
1121 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001122 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001123 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001124 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001125 foundCellSpan = (result != null);
1126 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001127 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001128 }
1129
1130 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001131 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001132 return;
1133 }
1134
Adam Cohen558baaf2011-08-15 15:22:57 -07001135 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
1137 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001138 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1139 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
1141 }
1142
Adam Cohen2f093b62012-04-30 18:59:53 -07001143 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1144 int minHeight) {
1145 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001146 // We want to account for the extra amount of padding that we are adding to the widget
1147 // to ensure that it gets the full amount of space that it has requested
1148 int requiredWidth = minWidth + padding.left + padding.right;
1149 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001150 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001151 }
1152
Adam Cohen2f093b62012-04-30 18:59:53 -07001153 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1154 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001155 }
1156
Adam Cohen2f093b62012-04-30 18:59:53 -07001157 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1158 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001159 }
1160
Adam Cohen2f093b62012-04-30 18:59:53 -07001161 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1162 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001163 }
1164
Adam Cohen2f093b62012-04-30 18:59:53 -07001165 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1166 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001167 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001168 }
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001173 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001176 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1177 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1178 if (appWidgetInfo == null) {
1179 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1180 }
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001184
Adam Cohen2f093b62012-04-30 18:59:53 -07001185 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1186 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001189 // We have saved the position to which the widget was dragged-- this really only matters
1190 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001191 int[] cellXY = mTmpAddItemCellCoordinates;
1192 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001193 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1196 cellXY[0] = mPendingAddInfo.cellX;
1197 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 spanXY[0] = mPendingAddInfo.spanX;
1199 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001200 foundCellSpan = true;
1201 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001203 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001204 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1205 spanXY[1], cellXY, finalSpan);
1206 spanXY[0] = finalSpan[0];
1207 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001208 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001211 }
1212
Michael Jurka0280c3b2010-09-17 15:00:07 -07001213 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001214 if (appWidgetId != -1) {
1215 // Deleting an app widget ID is a void call but writes to disk before returning
1216 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001217 new Thread("deleteAppWidgetId") {
1218 public void run() {
1219 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1220 }
1221 }.start();
1222 }
Winson Chung93eef082012-03-23 15:59:27 -07001223 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001224 return;
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001228 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1229 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 launcherInfo.spanX = spanXY[0];
1231 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001232 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1233 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001236 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
1238 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001239 if (hostView == null) {
1240 // Perform actual inflation because we're live
1241 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1242 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1243 } else {
1244 // The AppWidgetHostView has already been inflated and instantiated
1245 launcherInfo.hostView = hostView;
1246 }
Romain Guycbb89e42009-06-08 15:52:54 -07001247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001249 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001250 launcherInfo.notifyWidgetSizeChanged(this);
1251
Winson Chung3d503fb2011-07-13 17:25:49 -07001252 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001254
1255 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001257 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
Romain Guycbb89e42009-06-08 15:52:54 -07001259
Adam Cohended9f8d2010-11-03 13:25:16 -07001260 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1261 @Override
1262 public void onReceive(Context context, Intent intent) {
1263 final String action = intent.getAction();
1264 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1265 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001266 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001267 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001268
Winson Chungc100e8e2011-08-09 16:02:43 -07001269 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001270 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001271 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1272 mAppsCustomizeTabHost.reset();
1273 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001274 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001275 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1276 mUserPresent = true;
1277 updateRunning();
1278 }
1279 }
1280 };
1281
1282 @Override
1283 public void onAttachedToWindow() {
1284 super.onAttachedToWindow();
1285
1286 // Listen for broadcasts related to user-presence
1287 final IntentFilter filter = new IntentFilter();
1288 filter.addAction(Intent.ACTION_SCREEN_OFF);
1289 filter.addAction(Intent.ACTION_USER_PRESENT);
1290 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001291 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001292 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001293 mVisible = true;
1294 }
1295
1296 @Override
1297 public void onDetachedFromWindow() {
1298 super.onDetachedFromWindow();
1299 mVisible = false;
1300
Adam Cohend113e0c2010-11-11 10:48:05 -08001301 if (mAttached) {
1302 unregisterReceiver(mReceiver);
1303 mAttached = false;
1304 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001305 updateRunning();
1306 }
1307
1308 public void onWindowVisibilityChanged(int visibility) {
1309 mVisible = visibility == View.VISIBLE;
1310 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001311 // The following code used to be in onResume, but it turns out onResume is called when
1312 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1313 // is a more appropriate event to handle
1314 if (mVisible) {
1315 mAppsCustomizeTabHost.onWindowVisible();
1316 if (!mWorkspaceLoading) {
1317 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001318 // We want to let Launcher draw itself at least once before we force it to build
1319 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001320 // apps is nice and speedy.
1321 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001322 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001323 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001324 if (mStarted) return;
1325 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001326 // We delay the layer building a bit in order to give
1327 // other message processing a time to run. In particular
1328 // this avoids a delay in hiding the IME if it was
1329 // currently shown, because doing that may involve
1330 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001331 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001332 final ViewTreeObserver.OnDrawListener listener = this;
1333 mWorkspace.post(new Runnable() {
1334 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001335 if (mWorkspace != null &&
1336 mWorkspace.getViewTreeObserver() != null) {
1337 mWorkspace.getViewTreeObserver().
1338 removeOnDrawListener(listener);
1339 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001340 }
1341 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001342 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001343 }
1344 });
1345 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001346 // When Launcher comes back to foreground, a different Activity might be responsible for
1347 // the app market intent, so refresh the icon
1348 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001349 clearTypedText();
1350 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 }
1352
1353 private void sendAdvanceMessage(long delay) {
1354 mHandler.removeMessages(ADVANCE_MSG);
1355 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1356 mHandler.sendMessageDelayed(msg, delay);
1357 mAutoAdvanceSentTime = System.currentTimeMillis();
1358 }
1359
1360 private void updateRunning() {
1361 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1362 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1363 mAutoAdvanceRunning = autoAdvanceRunning;
1364 if (autoAdvanceRunning) {
1365 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1366 sendAdvanceMessage(delay);
1367 } else {
1368 if (!mWidgetsToAdvance.isEmpty()) {
1369 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1370 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1371 }
1372 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001373 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 }
1375 }
1376 }
1377
1378 private final Handler mHandler = new Handler() {
1379 @Override
1380 public void handleMessage(Message msg) {
1381 if (msg.what == ADVANCE_MSG) {
1382 int i = 0;
1383 for (View key: mWidgetsToAdvance.keySet()) {
1384 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1385 final int delay = mAdvanceStagger * i;
1386 if (v instanceof Advanceable) {
1387 postDelayed(new Runnable() {
1388 public void run() {
1389 ((Advanceable) v).advance();
1390 }
1391 }, delay);
1392 }
1393 i++;
1394 }
1395 sendAdvanceMessage(mAdvanceInterval);
1396 }
1397 }
1398 };
1399
1400 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001401 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001402 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1403 if (v instanceof Advanceable) {
1404 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001405 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001406 updateRunning();
1407 }
1408 }
1409
1410 void removeWidgetToAutoAdvance(View hostView) {
1411 if (mWidgetsToAdvance.containsKey(hostView)) {
1412 mWidgetsToAdvance.remove(hostView);
1413 updateRunning();
1414 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001415 }
1416
Joe Onorato9c1289c2009-08-17 11:03:03 -04001417 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001418 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 launcherInfo.hostView = null;
1420 }
1421
Winson Chung93eef082012-03-23 15:59:27 -07001422 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1423 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1424 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001425 }
1426
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001427 public LauncherAppWidgetHost getAppWidgetHost() {
1428 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Winson Chunga9abd0e2010-10-27 17:18:37 -07001431 public LauncherModel getModel() {
1432 return mModel;
1433 }
1434
Bjorn Bringertc459e522013-06-07 19:36:01 +01001435 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001436 getWindow().closeAllPanels();
1437
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001438 // Whatever we were doing is hereby canceled.
1439 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001440 }
1441
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 @Override
1443 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001444 long startTime = 0;
1445 if (DEBUG_RESUME_TIME) {
1446 startTime = System.currentTimeMillis();
1447 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 super.onNewIntent(intent);
1449
1450 // Close the menu
1451 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001452 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001454
Jamie Genniscb222e82012-10-23 15:44:26 -07001455 final boolean alreadyOnHome =
1456 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001457 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001458
Jamie Genniscb222e82012-10-23 15:44:26 -07001459 Runnable processIntent = new Runnable() {
1460 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001461 if (mWorkspace == null) {
1462 // Can be cases where mWorkspace is null, this prevents a NPE
1463 return;
1464 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001465 Folder openFolder = mWorkspace.getOpenFolder();
1466 // In all these cases, only animate if we're already on home
1467 mWorkspace.exitWidgetResizeMode();
1468 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1469 openFolder == null) {
1470 mWorkspace.moveToDefaultScreen(true);
1471 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001472
Jamie Genniscb222e82012-10-23 15:44:26 -07001473 closeFolder();
1474 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001475
Jamie Genniscb222e82012-10-23 15:44:26 -07001476 // If we are already on home, then just animate back to the workspace,
1477 // otherwise, just wait until onResume to set the state back to Workspace
1478 if (alreadyOnHome) {
1479 showWorkspace(true);
1480 } else {
1481 mOnResumeState = State.WORKSPACE;
1482 }
1483
1484 final View v = getWindow().peekDecorView();
1485 if (v != null && v.getWindowToken() != null) {
1486 InputMethodManager imm = (InputMethodManager)getSystemService(
1487 INPUT_METHOD_SERVICE);
1488 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1489 }
1490
1491 // Reset AllApps to its initial state
1492 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1493 mAppsCustomizeTabHost.reset();
1494 }
1495 }
1496 };
1497
1498 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1499 // Delay processing of the intent to allow the status bar animation to finish
1500 // first in order to avoid janky animations.
1501 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001502 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001503 // Process the intent immediately.
1504 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001505 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Michael Jurka447bf842013-05-15 14:52:15 +02001508 if (DEBUG_RESUME_TIME) {
1509 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1510 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512
1513 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001514 public void onRestoreInstanceState(Bundle state) {
1515 super.onRestoreInstanceState(state);
1516 for (int page: mSynchronouslyBoundPages) {
1517 mWorkspace.restoreInstanceStateForChild(page);
1518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
1521 @Override
1522 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001523 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001524 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525
Michael Jurka883f55b2010-10-21 15:47:14 -07001526 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001527 // We close any open folder since it will not be re-opened, and we need to make sure
1528 // this state is reflected.
1529 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1532 mWaitingForResult) {
1533 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1534 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1535 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1536 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001537 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1538 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1539 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
1542 if (mFolderInfo != null && mWaitingForResult) {
1543 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1544 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1545 }
Michael Jurka946ad472010-07-09 18:05:18 -07001546
Winson Chung785d2eb2011-04-14 16:08:02 -07001547 // Save the current AppsCustomize tab
1548 if (mAppsCustomizeTabHost != null) {
1549 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1550 if (currentTabTag != null) {
1551 outState.putString("apps_customize_currentTab", currentTabTag);
1552 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001553 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1554 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 }
1557
1558 @Override
1559 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001561
Winson Chunge7a03942011-08-05 15:05:12 -07001562 // Remove all pending runnables
1563 mHandler.removeMessages(ADVANCE_MSG);
1564 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001565 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001566
Winson Chungcd2b0142011-06-08 16:02:26 -07001567 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001568 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001569 mModel.stopLoader();
1570 app.setLauncher(null);
1571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001573 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001575 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001577 mAppWidgetHost = null;
1578
1579 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 TextKeyListener.getInstance().release();
1582
Winson Chung81b52252012-08-27 15:34:29 -07001583 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1584 // to prevent leaking Launcher activities on orientation change.
1585 if (mModel != null) {
1586 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1587 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001588
1589 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001590 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001591
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001592 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001593 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1594 mWorkspace.removeAllViews();
1595 mWorkspace = null;
1596 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001597
Michael Jurka2ecf9952012-06-18 12:52:28 -07001598 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600
Adam Cohena9cf38f2011-05-02 15:36:58 -07001601 public DragController getDragController() {
1602 return mDragController;
1603 }
1604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 @Override
1606 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001607 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 super.startActivityForResult(intent, requestCode);
1609 }
1610
Winson Chung70d72102011-08-12 11:24:35 -07001611 /**
1612 * Indicates that we want global search for this activity by setting the globalSearch
1613 * argument for {@link #startSearch} to true.
1614 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001616 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001618
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001619 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001620
Karl Rosaen138a0412009-04-23 19:00:21 -07001621 if (initialQuery == null) {
1622 // Use any text typed in the launcher as the initial query
1623 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 if (appSearchData == null) {
1626 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001627 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
Winson Chungadf0c182012-08-23 14:59:07 -07001629 Rect sourceBounds = new Rect();
1630 if (mSearchDropTargetBar != null) {
1631 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Bjorn Bringertc459e522013-06-07 19:36:01 +01001634 startSearch(initialQuery, selectInitialQuery,
1635 appSearchData, sourceBounds);
1636 }
1637
1638 public void startSearch(String initialQuery,
1639 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001640 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001641 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001642 }
1643
1644 /**
1645 * Starts the global search activity. This code is a copied from SearchManager
1646 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001647 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001648 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001649 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001650 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1651 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1652 if (globalSearchActivity == null) {
1653 Log.w(TAG, "No global search activity found.");
1654 return;
1655 }
1656 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1657 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1658 intent.setComponent(globalSearchActivity);
1659 // Make sure that we have a Bundle to put source in
1660 if (appSearchData == null) {
1661 appSearchData = new Bundle();
1662 } else {
1663 appSearchData = new Bundle(appSearchData);
1664 }
1665 // Set source to package name of app that starts global search, if not set already.
1666 if (!appSearchData.containsKey("source")) {
1667 appSearchData.putString("source", getPackageName());
1668 }
1669 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1670 if (!TextUtils.isEmpty(initialQuery)) {
1671 intent.putExtra(SearchManager.QUERY, initialQuery);
1672 }
1673 if (selectInitialQuery) {
1674 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1675 }
1676 intent.setSourceBounds(sourceBounds);
1677 try {
1678 startActivity(intent);
1679 } catch (ActivityNotFoundException ex) {
1680 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683
Winson Chung70d72102011-08-12 11:24:35 -07001684 @Override
1685 public boolean onCreateOptionsMenu(Menu menu) {
1686 if (isWorkspaceLocked()) {
1687 return false;
1688 }
1689
1690 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001691
1692 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1693 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1694 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001695 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1696 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1697 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001698 String helpUrl = getString(R.string.help_url);
1699 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001700 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1701 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1702
Winson Chung70d72102011-08-12 11:24:35 -07001703 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1704 .setIcon(android.R.drawable.ic_menu_gallery)
1705 .setAlphabeticShortcut('W');
1706 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1707 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001708 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001709 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001710 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1711 .setIcon(android.R.drawable.ic_menu_preferences)
1712 .setIntent(settings)
1713 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001714 if (!helpUrl.isEmpty()) {
1715 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1716 .setIcon(android.R.drawable.ic_menu_help)
1717 .setIntent(help)
1718 .setAlphabeticShortcut('H');
1719 }
Winson Chung70d72102011-08-12 11:24:35 -07001720 return true;
1721 }
1722
1723 @Override
1724 public boolean onPrepareOptionsMenu(Menu menu) {
1725 super.onPrepareOptionsMenu(menu);
1726
1727 if (mAppsCustomizeTabHost.isTransitioning()) {
1728 return false;
1729 }
1730 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1731 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1732
1733 return true;
1734 }
1735
1736 @Override
1737 public boolean onOptionsItemSelected(MenuItem item) {
1738 switch (item.getItemId()) {
1739 case MENU_WALLPAPER_SETTINGS:
1740 startWallpaper();
1741 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001742 }
1743
1744 return super.onOptionsItemSelected(item);
1745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001747 @Override
1748 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001749 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001750 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001751 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001752 }
1753
Joe Onorato9c1289c2009-08-17 11:03:03 -04001754 public boolean isWorkspaceLocked() {
1755 return mWorkspaceLoading || mWaitingForResult;
1756 }
1757
Michael Jurka0280c3b2010-09-17 15:00:07 -07001758 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001759 mPendingAddInfo.container = ItemInfo.NO_ID;
1760 mPendingAddInfo.screen = -1;
1761 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1762 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001763 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001764 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001766
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001767 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1768 AppWidgetProviderInfo appWidgetInfo) {
1769 if (appWidgetInfo.configure != null) {
1770 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001771
Bjorn Bringert7984c942009-12-09 15:38:25 +00001772 // Launch over to configure widget, if needed
1773 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001774 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001775 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001776 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001778 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001779 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1780 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001781 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001782 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784 }
Romain Guycbb89e42009-06-08 15:52:54 -07001785
Adam Cohenfbba09b2011-07-18 21:43:05 -07001786 /**
1787 * Process a shortcut drop.
1788 *
1789 * @param componentName The name of the component
1790 * @param screen The screen where it should be added
1791 * @param cell The cell it should be added to, optional
1792 * @param position The location on the screen where it was dropped, optional
1793 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001794 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1795 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001797 mPendingAddInfo.container = container;
1798 mPendingAddInfo.screen = screen;
1799 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800
1801 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001802 mPendingAddInfo.cellX = cell[0];
1803 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001804 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805
1806 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1807 createShortcutIntent.setComponent(componentName);
1808 processShortcut(createShortcutIntent);
1809 }
1810
Adam Cohenfbba09b2011-07-18 21:43:05 -07001811 /**
1812 * Process a widget drop.
1813 *
1814 * @param info The PendingAppWidgetInfo of the widget being added.
1815 * @param screen The screen where it should be added
1816 * @param cell The cell it should be added to, optional
1817 * @param position The location on the screen where it was dropped, optional
1818 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001819 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001820 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001821 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001822 mPendingAddInfo.container = info.container = container;
1823 mPendingAddInfo.screen = info.screen = screen;
1824 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001825 mPendingAddInfo.minSpanX = info.minSpanX;
1826 mPendingAddInfo.minSpanY = info.minSpanY;
1827
Adam Cohenfbba09b2011-07-18 21:43:05 -07001828 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001829 mPendingAddInfo.cellX = cell[0];
1830 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001831 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001832 if (span != null) {
1833 mPendingAddInfo.spanX = span[0];
1834 mPendingAddInfo.spanY = span[1];
1835 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001836
Adam Cohened66b2b2012-01-23 17:28:51 -08001837 AppWidgetHostView hostView = info.boundWidget;
1838 int appWidgetId;
1839 if (hostView != null) {
1840 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001841 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001842 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001843 // In this case, we either need to start an activity to get permission to bind
1844 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001845 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001846 Bundle options = info.bindOptions;
1847
1848 boolean success = false;
1849 if (options != null) {
1850 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1851 info.componentName, options);
1852 } else {
1853 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1854 info.componentName);
1855 }
1856 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001857 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001858 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001859 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001860 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1861 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1862 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001863 // TODO: we need to make sure that this accounts for the options bundle.
1864 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001865 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1866 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001867 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001868 }
1869
Joe Onoratodeb98af2010-02-19 14:59:39 -08001870 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001871 // Handle case where user selected "Applications"
1872 String applicationName = getResources().getString(R.string.group_applications);
1873 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001874
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001875 if (applicationName != null && applicationName.equals(shortcutName)) {
1876 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1877 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001878
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001879 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1880 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001881 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001882 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001883 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001884 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
1887
Winson Chung24ab2f12010-09-16 14:10:47 -07001888 void processWallpaper(Intent intent) {
1889 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1890 }
1891
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1893 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001894 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 folderInfo.title = getText(R.string.folder_name);
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001898 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1899 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001900 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
1902 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001903 FolderIcon newFolder =
1904 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1905 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1906 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001907 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Joe Onorato9c1289c2009-08-17 11:03:03 -04001910 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001911 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001915 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001917 Intent chooser = Intent.createChooser(pickWallpaper,
1918 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001919 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1920 // Removed in Eclair MR1
1921// WallpaperManager wm = (WallpaperManager)
1922// getSystemService(Context.WALLPAPER_SERVICE);
1923// WallpaperInfo wi = wm.getWallpaperInfo();
1924// if (wi != null && wi.getSettingsActivity() != null) {
1925// LabeledIntent li = new LabeledIntent(getPackageName(),
1926// R.string.configure_wallpaper, 0);
1927// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1928// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1929// }
Mike Clerona0618e42009-10-22 13:55:21 -07001930 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001933 /**
1934 * Registers various content observers. The current implementation registers
1935 * only a favorites observer to keep track of the favorites applications.
1936 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001937 private void registerContentObservers() {
1938 ContentResolver resolver = getContentResolver();
1939 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1940 true, mWidgetObserver);
1941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 @Override
1944 public boolean dispatchKeyEvent(KeyEvent event) {
1945 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1946 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001948 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001949 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001950 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001951 dumpState();
1952 return true;
1953 }
1954 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001955 }
1956 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1957 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001958 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 return true;
1960 }
1961 }
1962
1963 return super.dispatchKeyEvent(event);
1964 }
1965
Joe Onorato88ec0992009-11-19 13:16:06 -08001966 @Override
1967 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001968 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001969 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001970 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001971 Folder openFolder = mWorkspace.getOpenFolder();
1972 if (openFolder.isEditingName()) {
1973 openFolder.dismissEditingName();
1974 } else {
1975 closeFolder();
1976 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001977 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001978 mWorkspace.exitWidgetResizeMode();
1979
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001980 // Back button is a no-op here, but give at least some feedback for the button press
1981 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001982 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001986 * Re-listen when widgets are reset.
1987 */
1988 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001989 if (mAppWidgetHost != null) {
1990 mAppWidgetHost.startListening();
1991 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001992 }
1993
1994 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 * Launches the intent referred by the clicked shortcut.
1996 *
1997 * @param v The view representing the clicked shortcut.
1998 */
1999 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002000 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2001 // view has detached (it's possible for this to happen if the view is removed mid touch).
2002 if (v.getWindowToken() == null) {
2003 return;
2004 }
2005
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002006 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002007 return;
2008 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002011 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002013 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002014
2015 ComponentName widgetComp = new ComponentName(this, WidgetAdder.class);
2016 if (intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
2017 showAllApps(true);
2018 return;
2019 }
Joe Onorato13724ea2009-12-02 21:16:35 -08002020 int[] pos = new int[2];
2021 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002022 intent.setSourceBounds(new Rect(pos[0], pos[1],
2023 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002024
2025 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002026
2027 if (success && v instanceof BubbleTextView) {
2028 mWaitingForResume = (BubbleTextView) v;
2029 mWaitingForResume.setStayPressed(true);
2030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002032 if (v instanceof FolderIcon) {
2033 FolderIcon fi = (FolderIcon) v;
2034 handleFolderClick(fi);
2035 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002036 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002037 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002038 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002039 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002040 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043 }
2044
Michael Jurka0e260592010-06-30 17:07:39 -07002045 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002046 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002047 // clicking anywhere on the workspace causes the customization drawer to slide down
2048 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002049 return false;
2050 }
2051
Michael Jurkaaf442092010-06-10 17:01:57 -07002052 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002053 * Event handler for the search button
2054 *
2055 * @param v The view that was clicked.
2056 */
2057 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002058 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2059
Amith Yamasania135ba82011-08-09 17:42:01 -07002060 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002061 }
2062
2063 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002064 * Event handler for the voice button
2065 *
2066 * @param v The view that was clicked.
2067 */
2068 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002069 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002070
Bjorn Bringertc459e522013-06-07 19:36:01 +01002071 startVoice();
2072 }
2073
2074 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002075 try {
2076 final SearchManager searchManager =
2077 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2078 ComponentName activityName = searchManager.getGlobalSearchActivity();
2079 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002080 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002081 if (activityName != null) {
2082 intent.setPackage(activityName.getPackageName());
2083 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002084 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002085 } catch (ActivityNotFoundException e) {
2086 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002087 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002088 startActivitySafely(null, intent, "onClickVoiceButton");
2089 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002090 }
2091
2092 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002093 * Event handler for the "grid" button that appears on the home screen, which
2094 * enters all apps mode.
2095 *
2096 * @param v The view that was clicked.
2097 */
2098 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002099 showAllApps(true);
2100 }
2101
2102 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002103 // Provide the same haptic feedback that the system offers for virtual keys.
2104 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002105 }
2106
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002107 public void onClickAppMarketButton(View v) {
2108 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002109 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002110 } else {
2111 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002112 }
2113 }
2114
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002115 void startApplicationDetailsActivity(ComponentName componentName) {
2116 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002117 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2118 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002119 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002120 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002121 }
2122
Patrick Dubroy5539af72010-09-07 15:22:01 -07002123 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2124 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2125 // System applications cannot be installed. For now, show a toast explaining that.
2126 // We may give them the option of disabling apps this way.
2127 int messageId = R.string.uninstall_system_app_text;
2128 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2129 } else {
2130 String packageName = appInfo.componentName.getPackageName();
2131 String className = appInfo.componentName.getClassName();
2132 Intent intent = new Intent(
2133 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002134 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2135 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002136 startActivity(intent);
2137 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002138 }
2139
Michael Jurka86a720e2012-05-09 11:23:23 -07002140 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002144 // Only launch using the new animation if the shortcut has not opted out (this is a
2145 // private contract between launcher and may be ignored in the future).
2146 boolean useLaunchAnimation = (v != null) &&
2147 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2148 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002149 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2150 v.getMeasuredWidth(), v.getMeasuredHeight());
2151
2152 startActivity(intent, opts.toBundle());
2153 } else {
2154 startActivity(intent);
2155 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002156 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 } catch (SecurityException e) {
2158 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002159 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002161 "or use the exported attribute for this activity. "
2162 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002164 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002166
Michael Jurka86a720e2012-05-09 11:23:23 -07002167 boolean startActivitySafely(View v, Intent intent, Object tag) {
2168 boolean success = false;
2169 try {
2170 success = startActivity(v, intent, tag);
2171 } catch (ActivityNotFoundException e) {
2172 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2173 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2174 }
2175 return success;
2176 }
2177
Romain Guy8e633c52010-03-04 12:51:36 -08002178 void startActivityForResultSafely(Intent intent, int requestCode) {
2179 try {
2180 startActivityForResult(intent, requestCode);
2181 } catch (ActivityNotFoundException e) {
2182 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2183 } catch (SecurityException e) {
2184 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2185 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2186 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2187 "or use the exported attribute for this activity.", e);
2188 }
2189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
Adam Cohena9cf38f2011-05-02 15:36:58 -07002191 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002192 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002193 Folder openFolder = mWorkspace.getFolderForTag(info);
2194
2195 // If the folder info reports that the associated folder is open, then verify that
2196 // it is actually opened. There have been a few instances where this gets out of sync.
2197 if (info.opened && openFolder == null) {
2198 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2199 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2200 info.opened = false;
2201 }
2202
Adam Cohenfb91f302012-06-11 15:45:18 -07002203 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 // Close any open folder
2205 closeFolder();
2206 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002207 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 } else {
2209 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 int folderScreen;
2211 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002212 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 // .. and close it
2214 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002215 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 // Close any folder open on the current screen
2217 closeFolder();
2218 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002219 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 }
2221 }
2222 }
2223 }
2224
Adam Cohen268c4752012-06-06 17:47:33 -07002225 /**
2226 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2227 * in the DragLayer in the exact absolute location of the original FolderIcon.
2228 */
2229 private void copyFolderIconToImage(FolderIcon fi) {
2230 final int width = fi.getMeasuredWidth();
2231 final int height = fi.getMeasuredHeight();
2232
2233 // Lazy load ImageView, Bitmap and Canvas
2234 if (mFolderIconImageView == null) {
2235 mFolderIconImageView = new ImageView(this);
2236 }
2237 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2238 mFolderIconBitmap.getHeight() != height) {
2239 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2240 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2241 }
2242
2243 DragLayer.LayoutParams lp;
2244 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2245 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2246 } else {
2247 lp = new DragLayer.LayoutParams(width, height);
2248 }
2249
Adam Cohen307fe232012-08-16 17:55:58 -07002250 // The layout from which the folder is being opened may be scaled, adjust the starting
2251 // view size by this scale factor.
2252 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002253 lp.customPosition = true;
2254 lp.x = mRectForFolderAnimation.left;
2255 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002256 lp.width = (int) (scale * width);
2257 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002258
2259 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2260 fi.draw(mFolderIconCanvas);
2261 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002262 if (fi.getFolder() != null) {
2263 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2264 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002265 }
Adam Cohen268c4752012-06-06 17:47:33 -07002266 // Just in case this image view is still in the drag layer from a previous animation,
2267 // we remove it and re-add it.
2268 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2269 mDragLayer.removeView(mFolderIconImageView);
2270 }
2271 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002272 if (fi.getFolder() != null) {
2273 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002274 }
Adam Cohen268c4752012-06-06 17:47:33 -07002275 }
2276
Adam Cohen2801caf2011-05-13 20:57:39 -07002277 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002278 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002279 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2280 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2281 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2282
Adam Cohenc51934b2011-07-26 21:07:43 -07002283 FolderInfo info = (FolderInfo) fi.getTag();
2284 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2285 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002286 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2287 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002288 }
2289
Adam Cohen268c4752012-06-06 17:47:33 -07002290 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2291 copyFolderIconToImage(fi);
2292 fi.setVisibility(View.INVISIBLE);
2293
Michael Jurka2ecf9952012-06-18 12:52:28 -07002294 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002295 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002296 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2297 oa.start();
2298 }
2299
Adam Cohen268c4752012-06-06 17:47:33 -07002300 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002301 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002302 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2303 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2304 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2305
Adam Cohen268c4752012-06-06 17:47:33 -07002306 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002307
Adam Cohen268c4752012-06-06 17:47:33 -07002308 // We remove and re-draw the FolderIcon in-case it has changed
2309 mDragLayer.removeView(mFolderIconImageView);
2310 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002311 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002312 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002313 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002314 oa.addListener(new AnimatorListenerAdapter() {
2315 @Override
2316 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002317 if (cl != null) {
2318 cl.clearFolderLeaveBehind();
2319 // Remove the ImageView copy of the FolderIcon and make the original visible.
2320 mDragLayer.removeView(mFolderIconImageView);
2321 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002322 }
2323 }
2324 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002325 oa.start();
2326 }
2327
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002329 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 * is animated relative to the specified View. If the View is null, no animation
2331 * is played.
2332 *
2333 * @param folderInfo The FolderInfo describing the folder to open.
2334 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002335 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002336 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002337 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338
Adam Cohena9cf38f2011-05-02 15:36:58 -07002339 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340
Adam Cohen4554ee12011-08-03 16:13:21 -07002341 // Just verify that the folder hasn't already been added to the DragLayer.
2342 // There was a one-off crash where the folder had a parent already.
2343 if (folder.getParent() == null) {
2344 mDragLayer.addView(folder);
2345 mDragController.addDropTarget((DropTarget) folder);
2346 } else {
2347 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2348 folder.getParent() + ").");
2349 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002350 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002351 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002352
2353 // Notify the accessibility manager that this folder "window" has appeared and occluded
2354 // the workspace items
2355 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2356 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002357 }
2358
2359 public void closeFolder() {
2360 Folder folder = mWorkspace.getOpenFolder();
2361 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002362 if (folder.isEditingName()) {
2363 folder.dismissEditingName();
2364 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002365 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002366
2367 // Dismiss the folder cling
2368 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002369 }
2370 }
2371
2372 void closeFolder(Folder folder) {
2373 folder.getInfo().opened = false;
2374
2375 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2376 if (parent != null) {
2377 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2378 shrinkAndFadeInFolderIcon(fi);
2379 }
2380 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002381
2382 // Notify the accessibility manager that this folder "window" has disappeard and no
2383 // longer occludeds the workspace items
2384 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 }
2386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002388 if (!isDraggingEnabled()) return false;
2389 if (isWorkspaceLocked()) return false;
2390 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391
2392 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002393 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
2395
Michael Jurka0280c3b2010-09-17 15:00:07 -07002396 resetAddInfo();
2397 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002399 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 return true;
2401 }
2402
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 // The hotseat touch handling does not go through Workspace, and we always allow long press
2404 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002405 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2407 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002408 if (itemUnderLongClick == null) {
2409 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002410 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2411 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002412 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002414 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002416 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 }
2418 }
2419 }
2420 return true;
2421 }
2422
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 boolean isHotseatLayout(View layout) {
2424 return mHotseat != null && layout != null &&
2425 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2426 }
2427 Hotseat getHotseat() {
2428 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002429 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002430 SearchDropTargetBar getSearchBar() {
2431 return mSearchDropTargetBar;
2432 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002433
Winson Chung3d503fb2011-07-13 17:25:49 -07002434 /**
2435 * Returns the CellLayout of the specified container at the specified screen.
2436 */
2437 CellLayout getCellLayout(long container, int screen) {
2438 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2439 if (mHotseat != null) {
2440 return mHotseat.getLayout();
2441 } else {
2442 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002443 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002444 } else {
2445 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002446 }
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 Workspace getWorkspace() {
2450 return mWorkspace;
2451 }
2452
Daniel Sandler843e8602010-06-07 14:59:01 -04002453 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002454 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002455 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002456 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002457 }
2458
Craig Mautner360310b2012-10-26 15:13:08 -07002459 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002460 public boolean isAllAppsButtonRank(int rank) {
2461 return mHotseat.isAllAppsButtonRank(rank);
2462 }
2463
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 /**
2465 * Helper method for the cameraZoomIn/cameraZoomOut animations
2466 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002467 * @param scaleFactor The scale factor used for the zoom
2468 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002469 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002470 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002471 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002472 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002473
Winson Chung18f41f82012-05-09 13:28:10 -07002474 void disableWallpaperIfInAllApps() {
2475 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002476 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002477 if (mAppsCustomizeTabHost != null &&
2478 !mAppsCustomizeTabHost.isTransitioning()) {
2479 updateWallpaperVisibility(false);
2480 }
2481 }
2482 }
2483
Craig Mautner360310b2012-10-26 15:13:08 -07002484 private void setWorkspaceBackground(boolean workspace) {
2485 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002486 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002487 }
2488
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002489 void updateWallpaperVisibility(boolean visible) {
2490 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2491 int curflags = getWindow().getAttributes().flags
2492 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2493 if (wpflags != curflags) {
2494 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2495 }
Craig Mautner360310b2012-10-26 15:13:08 -07002496 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002497 }
2498
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002499 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2500 if (v instanceof LauncherTransitionable) {
2501 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2502 }
2503 }
2504
Michael Jurkabed61d22012-02-14 22:51:29 -08002505 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2506 if (v instanceof LauncherTransitionable) {
2507 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2508 }
Winson Chung70442722012-02-10 15:43:22 -08002509
2510 // Update the workspace transition step as well
2511 dispatchOnLauncherTransitionStep(v, 0f);
2512 }
2513
2514 private void dispatchOnLauncherTransitionStep(View v, float t) {
2515 if (v instanceof LauncherTransitionable) {
2516 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2517 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002518 }
2519
2520 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2521 if (v instanceof LauncherTransitionable) {
2522 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2523 }
Winson Chung70442722012-02-10 15:43:22 -08002524
2525 // Update the workspace transition step as well
2526 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002527 }
2528
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002529 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002530 * Things to test when changing the following seven functions.
2531 * - Home from workspace
2532 * - from center screen
2533 * - from other screens
2534 * - Home from all apps
2535 * - from center screen
2536 * - from other screens
2537 * - Back from all apps
2538 * - from center screen
2539 * - from other screens
2540 * - Launch app from workspace and quit
2541 * - with back
2542 * - with home
2543 * - Launch app from all apps and quit
2544 * - with back
2545 * - with home
2546 * - Go to a screen that's not the default, then all
2547 * apps, and launch and app, and go back
2548 * - with back
2549 * -with home
2550 * - On workspace, long press power and go back
2551 * - with back
2552 * - with home
2553 * - On all apps, long press power and go back
2554 * - with back
2555 * - with home
2556 * - On workspace, power off
2557 * - On all apps, power off
2558 * - Launch an app and turn off the screen while in that app
2559 * - Go back with home key
2560 * - Go back with back key TODO: make this not go to workspace
2561 * - From all apps
2562 * - From workspace
2563 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2564 * - From all apps
2565 * - From the center workspace
2566 * - From another workspace
2567 */
2568
2569 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002570 * Zoom the camera out from the workspace to reveal 'toView'.
2571 * Assumes that the view to show is anchored at either the very top or very bottom
2572 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002573 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002574 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002575 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002576 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002577 mStateAnimation.cancel();
2578 mStateAnimation = null;
2579 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002580 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002581
Winson Chungf0ea4d32011-06-06 14:27:16 -07002582 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2583 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2584 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002585 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002586 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002587 final int startDelay =
2588 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002589
Michael Jurkab3e22d92011-10-31 15:58:33 -07002590 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591
Michael Jurkad74c9842011-07-10 12:44:21 -07002592 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002593 Animator workspaceAnim =
2594 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595
2596 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002597 toView.setScaleX(scale);
2598 toView.setScaleY(scale);
2599 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2600 scaleAnim.
2601 scaleX(1f).scaleY(1f).
2602 setDuration(duration).
2603 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002604
Winson Chungf0ea4d32011-06-06 14:27:16 -07002605 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002606 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002607 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002608 .ofFloat(toView, "alpha", 0f, 1f)
2609 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002610 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002611 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2612 @Override
2613 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002614 if (animation == null) {
2615 throw new RuntimeException("animation is null");
2616 }
Winson Chung70442722012-02-10 15:43:22 -08002617 float t = (Float) animation.getAnimatedValue();
2618 dispatchOnLauncherTransitionStep(fromView, t);
2619 dispatchOnLauncherTransitionStep(toView, t);
2620 }
2621 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002622
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002623 // toView should appear right at the end of the workspace shrink
2624 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002625 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002626 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002627 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002628
2629 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002630 boolean animationCancelled = false;
2631
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002632 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002633 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002634 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 // Prepare the position
2636 toView.setTranslationX(0.0f);
2637 toView.setTranslationY(0.0f);
2638 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002639 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002641 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002642 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002643 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2644 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002645
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002646 if (mWorkspace != null && !springLoaded && !LauncherAppState.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002647 // Hide the workspace scrollbar
2648 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002649 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002650 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002651 if (!animationCancelled) {
2652 updateWallpaperVisibility(false);
2653 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002654
2655 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002656 if (mSearchDropTargetBar != null) {
2657 mSearchDropTargetBar.hideSearchBar(false);
2658 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002659 }
2660
Adam Cohencff6af82011-09-13 14:51:53 -07002661 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002662 public void onAnimationCancel(Animator animation) {
2663 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002664 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002665 });
2666
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002667 if (workspaceAnim != null) {
2668 mStateAnimation.play(workspaceAnim);
2669 }
Michael Jurka1899a362011-11-03 13:50:45 -07002670
2671 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002672
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002673 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2674 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002675
2676 // If any of the objects being animated haven't been measured/laid out
2677 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002679 (mWorkspace.getMeasuredWidth() == 0) ||
2680 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002681 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002682 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002683
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002684 final AnimatorSet stateAnimation = mStateAnimation;
2685 final Runnable startAnimRunnable = new Runnable() {
2686 public void run() {
2687 // Check that mStateAnimation hasn't changed while
2688 // we waited for a layout/draw pass
2689 if (mStateAnimation != stateAnimation)
2690 return;
2691 setPivotsForZoom(toView, scale);
2692 dispatchOnLauncherTransitionStart(fromView, animated, false);
2693 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002694 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002695 }
2696 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002697 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002698 final ViewTreeObserver observer = toView.getViewTreeObserver();
2699 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2700 public void onGlobalLayout() {
2701 startAnimRunnable.run();
2702 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2703 }
2704 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002706 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002707 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002708 } else {
2709 toView.setTranslationX(0.0f);
2710 toView.setTranslationY(0.0f);
2711 toView.setScaleX(1.0f);
2712 toView.setScaleY(1.0f);
2713 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002714 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002715
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002716 if (!springLoaded && !LauncherAppState.isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002717 // Hide the workspace scrollbar
2718 mWorkspace.hideScrollingIndicator(true);
2719 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002720
2721 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002722 if (mSearchDropTargetBar != null) {
2723 mSearchDropTargetBar.hideSearchBar(false);
2724 }
Michael Jurkaabded662011-03-04 12:06:57 -08002725 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002726 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002727 dispatchOnLauncherTransitionStart(fromView, animated, false);
2728 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002729 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002730 dispatchOnLauncherTransitionStart(toView, animated, false);
2731 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002732 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002733 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002734 }
2735
2736 /**
2737 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002738 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002740 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002741 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2742 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002743
Michael Jurkab3e22d92011-10-31 15:58:33 -07002744 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002745 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002746 mStateAnimation.cancel();
2747 mStateAnimation = null;
2748 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002749 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002750
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002752 final int fadeOutDuration =
2753 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002754 final float scaleFactor = (float)
2755 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2756 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002757 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002758 Animator workspaceAnim = null;
2759
2760 if (toState == State.WORKSPACE) {
2761 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2762 workspaceAnim = mWorkspace.getChangeStateAnimation(
2763 Workspace.State.NORMAL, animated, stagger);
2764 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2765 workspaceAnim = mWorkspace.getChangeStateAnimation(
2766 Workspace.State.SPRING_LOADED, animated);
2767 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002768
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002770 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002771 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002773 final LauncherViewPropertyAnimator scaleAnim =
2774 new LauncherViewPropertyAnimator(fromView);
2775 scaleAnim.
2776 scaleX(scaleFactor).scaleY(scaleFactor).
2777 setDuration(duration).
2778 setInterpolator(new Workspace.ZoomInInterpolator());
2779
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002780 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002781 .ofFloat(fromView, "alpha", 1f, 0f)
2782 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002783 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002784 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2785 @Override
2786 public void onAnimationUpdate(ValueAnimator animation) {
2787 float t = 1f - (Float) animation.getAnimatedValue();
2788 dispatchOnLauncherTransitionStep(fromView, t);
2789 dispatchOnLauncherTransitionStep(toView, t);
2790 }
2791 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002792
Michael Jurka2ecf9952012-06-18 12:52:28 -07002793 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002794
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002795 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2796 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002797 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002798
2799 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002800 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002801 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002802 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002804 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2805 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002806 if (mWorkspace != null) {
2807 mWorkspace.hideScrollingIndicator(false);
2808 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002809 if (onCompleteRunnable != null) {
2810 onCompleteRunnable.run();
2811 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002812 mAppsCustomizeContent.updateCurrentPageScroll();
2813 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 }
2815 });
2816
Winson Chungf0ea4d32011-06-06 14:27:16 -07002817 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002818 if (workspaceAnim != null) {
2819 mStateAnimation.play(workspaceAnim);
2820 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002821 dispatchOnLauncherTransitionStart(fromView, animated, true);
2822 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002823 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 } else {
2825 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002826 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002827 dispatchOnLauncherTransitionStart(fromView, animated, true);
2828 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002829 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 dispatchOnLauncherTransitionStart(toView, animated, true);
2831 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002832 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002833 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002834 }
2835
Michael Jurkae326f182011-11-21 14:05:46 -08002836 @Override
2837 public void onTrimMemory(int level) {
2838 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002839 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002840 mAppsCustomizeTabHost.onTrimMemory();
2841 }
2842 }
2843
Winson Chung18f41f82012-05-09 13:28:10 -07002844 @Override
2845 public void onWindowFocusChanged(boolean hasFocus) {
2846 if (!hasFocus) {
2847 // When another window occludes launcher (like the notification shade, or recents),
2848 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2849 updateWallpaperVisibility(true);
2850 } else {
2851 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002852 mWorkspace.postDelayed(new Runnable() {
2853 @Override
2854 public void run() {
2855 disableWallpaperIfInAllApps();
2856 }
2857 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002858 }
2859 }
2860
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002861 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002862 showWorkspace(animated, null);
2863 }
2864
2865 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002866 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002867 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002868 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002869 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002870
Winson Chungc7d2b602012-05-16 17:02:20 -07002871 // Show the search bar (only animate if we were showing the drop target bar in spring
2872 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002873 if (mSearchDropTargetBar != null) {
2874 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2875 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002876
Michael Jurkab737ee62011-11-15 15:57:22 -08002877 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002878 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002879
2880 // Set focus to the AppsCustomize button
2881 if (mAllAppsButton != null) {
2882 mAllAppsButton.requestFocus();
2883 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002884 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002885
Michael Jurkab737ee62011-11-15 15:57:22 -08002886 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002887
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002888 // Change the state *after* we've called all the transition code
2889 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002890
Winson Chung5fb63472011-02-02 17:03:37 -08002891 // Resume the auto-advance of widgets
2892 mUserPresent = true;
2893 updateRunning();
2894
alanv1d4fde62012-10-17 13:15:47 -07002895 // Send an accessibility event to announce the context change
2896 getWindow().getDecorView()
2897 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002898
2899 onWorkspaceShown();
2900 }
2901
2902 public void onWorkspaceShown() {
Joe Onorato00acb122009-08-04 16:04:30 -04002903 }
2904
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905 void showAllApps(boolean animated) {
2906 if (mState != State.WORKSPACE) return;
2907
2908 showAppsCustomizeHelper(animated, false);
2909 mAppsCustomizeTabHost.requestFocus();
2910
Michael Jurkab3e22d92011-10-31 15:58:33 -07002911 // Change the state *after* we've called all the transition code
2912 mState = State.APPS_CUSTOMIZE;
2913
2914 // Pause the auto-advance of widgets until we are out of AllApps
2915 mUserPresent = false;
2916 updateRunning();
2917 closeFolder();
2918
2919 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002920 getWindow().getDecorView()
2921 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002922 }
2923
Adam Cohen7777d962011-08-18 18:58:38 -07002924 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002925 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002926 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002927 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002928 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002929 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002930 }
Adam Cohen7777d962011-08-18 18:58:38 -07002931
Adam Cohened66b2b2012-01-23 17:28:51 -08002932 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2933 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002934 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2935
Winson Chunge7a03942011-08-05 15:05:12 -07002936 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002937 @Override
2938 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002939 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002940 // Before we show workspace, hide all apps again because
2941 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2942 // clean up our state transition functions
2943 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002944 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002945 } else {
2946 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002947 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002948 }
2949 }, (extendedDelay ?
2950 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2951 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2952 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002955 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002956 final boolean animated = true;
2957 final boolean springLoaded = true;
2958 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002959 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960 }
2961 // Otherwise, we are not in spring loaded mode, so don't do anything.
2962 }
2963
Michael Jurkab737ee62011-11-15 15:57:22 -08002964 void hideDockDivider() {
2965 if (mQsbDivider != null && mDockDivider != null) {
2966 mQsbDivider.setVisibility(View.INVISIBLE);
2967 mDockDivider.setVisibility(View.INVISIBLE);
2968 }
2969 }
2970
2971 void showDockDivider(boolean animated) {
2972 if (mQsbDivider != null && mDockDivider != null) {
2973 mQsbDivider.setVisibility(View.VISIBLE);
2974 mDockDivider.setVisibility(View.VISIBLE);
2975 if (mDividerAnimator != null) {
2976 mDividerAnimator.cancel();
2977 mQsbDivider.setAlpha(1f);
2978 mDockDivider.setAlpha(1f);
2979 mDividerAnimator = null;
2980 }
2981 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002982 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2983 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2984 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002985 int duration = 0;
2986 if (mSearchDropTargetBar != null) {
2987 duration = mSearchDropTargetBar.getTransitionInDuration();
2988 }
2989 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002990 mDividerAnimator.start();
2991 }
2992 }
2993 }
2994
Michael Jurkab3e22d92011-10-31 15:58:33 -07002995 void lockAllApps() {
2996 // TODO
2997 }
2998
2999 void unlockAllApps() {
3000 // TODO
3001 }
3002
Winson Chungf0ea4d32011-06-06 14:27:16 -07003003 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003004 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003005 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003006 void showHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003007 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003008 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003009 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003010 int duration = 0;
3011 if (mSearchDropTargetBar != null) {
3012 duration = mSearchDropTargetBar.getTransitionInDuration();
3013 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003014 mHotseat.animate().alpha(1f).setDuration(duration);
3015 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003016 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003017 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003018 }
3019 }
3020 }
3021
3022 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003023 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003024 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003025 void hideHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003026 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003027 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003028 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003029 int duration = 0;
3030 if (mSearchDropTargetBar != null) {
3031 duration = mSearchDropTargetBar.getTransitionOutDuration();
3032 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003033 mHotseat.animate().alpha(0f).setDuration(duration);
3034 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003035 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003036 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003037 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003038 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003039 }
3040
Patrick Dubroy5f445422011-02-18 14:35:21 -08003041 /**
3042 * Add an item from all apps or customize onto the given workspace screen.
3043 * If layout is null, add to the current screen.
3044 */
3045 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003046 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003047 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003048 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003049 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003050
Winson Chungdff8ebb2011-09-08 17:25:31 -07003051 /** Maps the current orientation to an index for referencing orientation correct global icons */
3052 private int getCurrentOrientationIndexForGlobalIcons() {
3053 // default - 0, landscape - 1
3054 switch (getResources().getConfiguration().orientation) {
3055 case Configuration.ORIENTATION_LANDSCAPE:
3056 return 1;
3057 default:
3058 return 0;
3059 }
3060 }
3061
Michael Jurkaae65ee32012-04-30 11:45:54 -07003062 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003063 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003064 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003065 // Look for the toolbar icon specified in the activity meta-data
3066 Bundle metaData = packageManager.getActivityInfo(
3067 activityName, PackageManager.GET_META_DATA).metaData;
3068 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003069 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003070 if (iconResId != 0) {
3071 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003072 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003073 }
3074 }
3075 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003076 // This can happen if the activity defines an invalid drawable
3077 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3078 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003079 } catch (Resources.NotFoundException nfe) {
3080 // This can happen if the activity defines an invalid drawable
3081 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3082 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003083 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003084 return null;
3085 }
3086
3087 // if successful in getting icon, return it; otherwise, set button to use default drawable
3088 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003089 int buttonId, ComponentName activityName, int fallbackDrawableId,
3090 String toolbarResourceName) {
3091 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003092 Resources r = getResources();
3093 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3094 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003095
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003096 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003097 // If we were unable to find the icon via the meta-data, use a generic one
3098 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003099 toolbarIcon = r.getDrawable(fallbackDrawableId);
3100 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003101 if (button != null) {
3102 button.setCompoundDrawables(toolbarIcon, null, null, null);
3103 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003104 return null;
3105 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003106 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003107 if (button != null) {
3108 button.setCompoundDrawables(toolbarIcon, null, null, null);
3109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003110 return toolbarIcon.getConstantState();
3111 }
3112 }
3113
3114 // if successful in getting icon, return it; otherwise, set button to use default drawable
3115 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003116 int buttonId, ComponentName activityName, int fallbackDrawableId,
3117 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003118 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003119 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003120
Michael Jurka19e0fc52011-07-22 18:00:21 -07003121 if (button != null) {
3122 // If we were unable to find the icon via the meta-data, use a
3123 // generic one
3124 if (toolbarIcon == null) {
3125 button.setImageResource(fallbackDrawableId);
3126 } else {
3127 button.setImageDrawable(toolbarIcon);
3128 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003129 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003130
3131 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3132
Michael Jurka0423dcf2010-10-05 14:56:18 -07003133 }
3134
Winson Chung1b884092012-06-08 17:13:02 -07003135 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003136 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003137 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003138 }
3139
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003140 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003142 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 }
3144
Winson Chungbb185bd2011-11-21 12:31:42 -08003145 private void invalidatePressedFocusedStates(View container, View button) {
3146 if (container instanceof HolographicLinearLayout) {
3147 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3148 layout.invalidatePressedFocusedStates();
3149 } else if (button instanceof HolographicImageView) {
3150 HolographicImageView view = (HolographicImageView) button;
3151 view.invalidatePressedFocusedStates();
3152 }
3153 }
3154
Winson Chungc51db6a2011-10-05 11:44:49 -07003155 private boolean updateGlobalSearchIcon() {
3156 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003157 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003158 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003159 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003160
Winson Chung1cad91e2011-05-25 17:41:01 -07003161 final SearchManager searchManager =
3162 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3163 ComponentName activityName = searchManager.getGlobalSearchActivity();
3164 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003165 int coi = getCurrentOrientationIndexForGlobalIcons();
3166 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003167 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3168 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3169 if (sGlobalSearchIcon[coi] == null) {
3170 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3171 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3172 TOOLBAR_ICON_METADATA_NAME);
3173 }
3174
Winson Chungc51db6a2011-10-05 11:44:49 -07003175 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3176 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003177 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003178 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003179 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003180 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003181 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3182 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3183 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003184 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003185 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003187 }
3188 }
3189
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003190 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003191 final View searchButtonContainer = findViewById(R.id.search_button_container);
3192 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003193 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003194 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003195 }
3196
Winson Chungc51db6a2011-10-05 11:44:49 -07003197 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003198 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003199 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003200
Winson Chungc51db6a2011-10-05 11:44:49 -07003201 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003202 final SearchManager searchManager =
3203 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3204 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3205
3206 ComponentName activityName = null;
3207 if (globalSearchActivity != null) {
3208 // Check if the global search activity handles voice search
3209 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3210 intent.setPackage(globalSearchActivity.getPackageName());
3211 activityName = intent.resolveActivity(getPackageManager());
3212 }
3213
3214 if (activityName == null) {
3215 // Fallback: check if an activity other than the global search activity
3216 // resolves this
3217 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3218 activityName = intent.resolveActivity(getPackageManager());
3219 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003220 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003221 int coi = getCurrentOrientationIndexForGlobalIcons();
3222 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003223 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3224 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3225 if (sVoiceSearchIcon[coi] == null) {
3226 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3227 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3228 TOOLBAR_ICON_METADATA_NAME);
3229 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003230 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003231 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003232 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003233 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003234 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003235 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003236 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003237 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003238 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003239 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003240 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003241 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003242
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003243 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003244 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3245 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003246 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003247 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003248 }
3249
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003250 public void setVoiceButtonProxyVisible(boolean visible) {
3251 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3252 if (voiceButtonProxy != null) {
3253 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3254 }
3255 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003256 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003257 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003258 */
3259 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003260 final View marketButton = findViewById(R.id.market_button);
3261 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3262 // Find the app market activity by resolving an intent.
3263 // (If multiple app markets are installed, it will return the ResolverActivity.)
3264 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003265 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003266 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003267 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003268 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003269 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3270 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003271 marketButton.setVisibility(View.VISIBLE);
3272 } else {
3273 // We should hide and disable the view so that we don't try and restore the visibility
3274 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3275 marketButton.setVisibility(View.GONE);
3276 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003277 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003278 }
3279
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003280 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003281 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3282 Resources r = getResources();
3283 Drawable marketIconDrawable = d.newDrawable(r);
3284 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3285 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3286 marketIconDrawable.setBounds(0, 0, w, h);
3287
3288 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003289 }
3290
Michael Jurkad7c28052012-04-27 15:43:36 -07003291 @Override
3292 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003293 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003294 final List<CharSequence> text = event.getText();
3295 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003296 // Populate event with a fake title based on the current state.
3297 if (mState == State.APPS_CUSTOMIZE) {
3298 text.add(getString(R.string.all_apps_button_label));
3299 } else {
3300 text.add(getString(R.string.all_apps_home_button_label));
3301 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003302 return result;
3303 }
3304
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003305 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003306 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003307 */
3308 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3309 @Override
3310 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003311 closeSystemDialogs();
3312 }
3313 }
3314
3315 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003316 * Receives notifications whenever the appwidgets are reset.
3317 */
3318 private class AppWidgetResetObserver extends ContentObserver {
3319 public AppWidgetResetObserver() {
3320 super(new Handler());
3321 }
3322
3323 @Override
3324 public void onChange(boolean selfChange) {
3325 onAppWidgetReset();
3326 }
3327 }
3328
3329 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003330 * If the activity is currently paused, signal that we need to run the passed Runnable
3331 * in onResume.
3332 *
3333 * This needs to be called from incoming places where resources might have been loaded
3334 * while we are paused. That is becaues the Configuration might be wrong
3335 * when we're not running, and if it comes back to what it was when we
3336 * were paused, we are not restarted.
3337 *
3338 * Implementation of the method from LauncherModel.Callbacks.
3339 *
3340 * @return true if we are currently paused. The caller might be able to
3341 * skip some work in that case since we will come back again.
3342 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003343 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003344 if (mPaused) {
3345 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003346 if (deletePreviousRunnables) {
3347 while (mOnResumeCallbacks.remove(run)) {
3348 }
3349 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003350 mOnResumeCallbacks.add(run);
3351 return true;
3352 } else {
3353 return false;
3354 }
3355 }
3356
Michael Jurkac402cd92013-05-20 15:49:32 +02003357 private boolean waitUntilResume(Runnable run) {
3358 return waitUntilResume(run, false);
3359 }
3360
Michael Jurka7607c2f2013-04-03 14:33:19 -07003361 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003362 * If the activity is currently paused, signal that we need to re-run the loader
3363 * in onResume.
3364 *
3365 * This needs to be called from incoming places where resources might have been loaded
3366 * while we are paused. That is becaues the Configuration might be wrong
3367 * when we're not running, and if it comes back to what it was when we
3368 * were paused, we are not restarted.
3369 *
3370 * Implementation of the method from LauncherModel.Callbacks.
3371 *
3372 * @return true if we are currently paused. The caller might be able to
3373 * skip some work in that case since we will come back again.
3374 */
3375 public boolean setLoadOnResume() {
3376 if (mPaused) {
3377 Log.i(TAG, "setLoadOnResume");
3378 mOnResumeNeedsLoad = true;
3379 return true;
3380 } else {
3381 return false;
3382 }
3383 }
3384
3385 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003387 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003389 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003390 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003391 } else {
3392 return SCREEN_COUNT / 2;
3393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003395
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 /**
3397 * Refreshes the shortcuts shown on the workspace.
3398 *
3399 * Implementation of the method from LauncherModel.Callbacks.
3400 */
3401 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003402 // If we're starting binding all over again, clear any bind calls we'd postponed in
3403 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3404 // from scratch again
3405 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003406
Michael Jurka7607c2f2013-04-03 14:33:19 -07003407 final Workspace workspace = mWorkspace;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003408 mNewShortcutAnimatePage = -1;
3409 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003410 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 int count = workspace.getChildCount();
3412 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003413 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003414 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3415 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003417 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003418 if (mHotseat != null) {
3419 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003420 }
3421 }
3422
3423 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 * Bind the items start-end from the list.
3425 *
3426 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003427 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003428 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3429 if (waitUntilResume(new Runnable() {
3430 public void run() {
3431 bindItems(shortcuts, start, end);
3432 }
3433 })) {
3434 return;
3435 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003436
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3438 Set<String> newApps = new HashSet<String>();
3439 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3440
3441 Workspace workspace = mWorkspace;
3442 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003444
3445 // Short circuit if we are loading dock items for a configuration which has no dock
3446 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3447 mHotseat == null) {
3448 continue;
3449 }
3450
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 switch (item.itemType) {
3452 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3453 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003454 ShortcutInfo info = (ShortcutInfo) item;
3455 String uri = info.intent.toUri(0).toString();
3456 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003457 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3458 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003459 boolean animateIconUp = false;
3460 synchronized (newApps) {
3461 if (newApps.contains(uri)) {
3462 animateIconUp = newApps.remove(uri);
3463 }
3464 }
3465 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003466 // Prepare the view to be animated up
3467 shortcut.setAlpha(0f);
3468 shortcut.setScaleX(0f);
3469 shortcut.setScaleY(0f);
3470 mNewShortcutAnimatePage = item.screen;
3471 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3472 mNewShortcutAnimateViews.add(shortcut);
3473 }
3474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003476 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003477 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003478 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003479 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003480 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3481 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003484 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003485
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003487 }
3488
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 /**
3490 * Implementation of the method from LauncherModel.Callbacks.
3491 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003492 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3493 if (waitUntilResume(new Runnable() {
3494 public void run() {
3495 bindFolders(folders);
3496 }
3497 })) {
3498 return;
3499 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003500 sFolders.clear();
3501 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503
3504 /**
3505 * Add the views for a widget to the workspace.
3506 *
3507 * Implementation of the method from LauncherModel.Callbacks.
3508 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3510 if (waitUntilResume(new Runnable() {
3511 public void run() {
3512 bindAppWidget(item);
3513 }
3514 })) {
3515 return;
3516 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003517
Daniel Sandler843e8602010-06-07 14:59:01 -04003518 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3519 if (DEBUG_WIDGETS) {
3520 Log.d(TAG, "bindAppWidget: " + item);
3521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 final Workspace workspace = mWorkspace;
3523
3524 final int appWidgetId = item.appWidgetId;
3525 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003526 if (DEBUG_WIDGETS) {
3527 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3528 }
3529
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3531
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003533 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534
Winson Chung3d503fb2011-07-13 17:25:49 -07003535 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003537 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3538
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 workspace.requestLayout();
3540
Daniel Sandler843e8602010-06-07 14:59:01 -04003541 if (DEBUG_WIDGETS) {
3542 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3543 + (SystemClock.uptimeMillis()-start) + "ms");
3544 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 }
3546
Adam Cohen1462de32012-07-24 22:34:36 -07003547 public void onPageBoundSynchronously(int page) {
3548 mSynchronouslyBoundPages.add(page);
3549 }
3550
Joe Onorato9c1289c2009-08-17 11:03:03 -04003551 /**
3552 * Callback saying that there aren't any more items to bind.
3553 *
3554 * Implementation of the method from LauncherModel.Callbacks.
3555 */
Adam Cohene25af792013-06-06 23:08:25 -07003556 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003557 if (waitUntilResume(new Runnable() {
3558 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003559 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003560 }
3561 })) {
3562 return;
3563 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003564 if (mSavedState != null) {
3565 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003566 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 mSavedState = null;
3569 }
3570
Adam Cohen1462de32012-07-24 22:34:36 -07003571 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003573 // If we received the result of any pending adds while the loader was running (e.g. the
3574 // widget configuration forced an orientation change), process them now.
3575 for (int i = 0; i < sPendingAddList.size(); i++) {
3576 completeAdd(sPendingAddList.get(i));
3577 }
3578 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003579
Winson Chungc51db6a2011-10-05 11:44:49 -07003580 // Update the market app icon as necessary (the other icons will be managed in response to
3581 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003582 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003583
Winson Chungf0c6ae02012-03-21 16:10:31 -07003584 // Animate up any icons as necessary
3585 if (mVisible || mWorkspaceLoading) {
3586 Runnable newAppsRunnable = new Runnable() {
3587 @Override
3588 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003589 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003590 }
3591 };
Winson Chunga2413752012-04-03 14:22:34 -07003592
3593 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3594 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3595 if (canRunNewAppsAnimation()) {
3596 // If the user has not interacted recently, then either snap to the new page to show
3597 // the new-apps animation or just run them if they are to appear on the current page
3598 if (willSnapPage) {
3599 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3600 } else {
3601 runNewAppsAnimation(false);
3602 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003603 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003604 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003605 // are on another page (or just normally if they are added to the current page)
3606 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003607 }
3608 }
3609
3610 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003611 if (upgradePath) {
3612 mWorkspace.saveWorkspaceToDb();
3613
3614 // Run through this twice... a little hackleberry, but the right solution is complex.
3615 mWorkspace.stripDuplicateApps();
3616 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
3617 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003618 }
3619
Winson Chunga2413752012-04-03 14:22:34 -07003620 private boolean canRunNewAppsAnimation() {
3621 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3622 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3623 }
3624
Winson Chungf0c6ae02012-03-21 16:10:31 -07003625 /**
3626 * Runs a new animation that scales up icons that were added while Launcher was in the
3627 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003628 *
3629 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003630 */
Winson Chunga2413752012-04-03 14:22:34 -07003631 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003632 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003633 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003634
3635 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003636 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3637 @Override
3638 public int compare(View a, View b) {
3639 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3640 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3641 int cellCountX = LauncherModel.getCellCountX();
3642 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3643 }
3644 });
Winson Chunga2413752012-04-03 14:22:34 -07003645
3646 // Animate each of the views in place (or show them immediately if requested)
3647 if (immediate) {
3648 for (View v : mNewShortcutAnimateViews) {
3649 v.setAlpha(1f);
3650 v.setScaleX(1f);
3651 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003652 }
Winson Chunga2413752012-04-03 14:22:34 -07003653 } else {
3654 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3655 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003656 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003657 PropertyValuesHolder.ofFloat("alpha", 1f),
3658 PropertyValuesHolder.ofFloat("scaleX", 1f),
3659 PropertyValuesHolder.ofFloat("scaleY", 1f));
3660 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3661 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3662 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3663 bounceAnims.add(bounceAnim);
3664 }
3665 anim.playTogether(bounceAnims);
3666 anim.addListener(new AnimatorListenerAdapter() {
3667 @Override
3668 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003669 if (mWorkspace != null) {
3670 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3671 }
Winson Chunga2413752012-04-03 14:22:34 -07003672 }
3673 });
3674 anim.start();
3675 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003676
3677 // Clean up
3678 mNewShortcutAnimatePage = -1;
3679 mNewShortcutAnimateViews.clear();
3680 new Thread("clearNewAppsThread") {
3681 public void run() {
3682 mSharedPrefs.edit()
3683 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3684 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3685 .commit();
3686 }
3687 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003688 }
3689
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003690 @Override
3691 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003692 boolean searchVisible = updateGlobalSearchIcon();
3693 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003694 if (mSearchDropTargetBar != null) {
3695 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3696 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003697 }
3698
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003699 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 * Add the icons for all apps.
3701 *
3702 * Implementation of the method from LauncherModel.Callbacks.
3703 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003704 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003705 Runnable setAllAppsRunnable = new Runnable() {
3706 public void run() {
3707 if (mAppsCustomizeContent != null) {
3708 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003709
3710 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3711 getHotseat().addAllAppsFolder(mIconCache, apps,
3712 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3713 mIntentsOnWorkspaceFromUpgradePath = null;
3714 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003715 }
3716 }
3717 };
3718
Michael Jurkac57b7a82011-08-09 22:02:20 -07003719 // Remove the progress bar entirely; we could also make it GONE
3720 // but better to remove it since we know it's not going to be used
3721 View progressBar = mAppsCustomizeTabHost.
3722 findViewById(R.id.apps_customize_progress_bar);
3723 if (progressBar != null) {
3724 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003725
3726 // We just post the call to setApps so the user sees the progress bar
3727 // disappear-- otherwise, it just looks like the progress bar froze
3728 // which doesn't look great
3729 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3730 } else {
3731 // If we did not initialize the spinner in onCreate, then we can directly set the
3732 // list of applications without waiting for any progress bars views to be hidden.
3733 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 }
3736
3737 /**
3738 * A package was installed.
3739 *
3740 * Implementation of the method from LauncherModel.Callbacks.
3741 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3743 if (waitUntilResume(new Runnable() {
3744 public void run() {
3745 bindAppsAdded(apps);
3746 }
3747 })) {
3748 return;
3749 }
3750
Winson Chungf0ea4d32011-06-06 14:27:16 -07003751
Winson Chung785d2eb2011-04-14 16:08:02 -07003752 if (mAppsCustomizeContent != null) {
3753 mAppsCustomizeContent.addApps(apps);
3754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003755 }
3756
3757 /**
3758 * A package was updated.
3759 *
3760 * Implementation of the method from LauncherModel.Callbacks.
3761 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003762 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3763 if (waitUntilResume(new Runnable() {
3764 public void run() {
3765 bindAppsUpdated(apps);
3766 }
3767 })) {
3768 return;
3769 }
3770
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003771 if (mWorkspace != null) {
3772 mWorkspace.updateShortcuts(apps);
3773 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003774
Winson Chung785d2eb2011-04-14 16:08:02 -07003775 if (mAppsCustomizeContent != null) {
3776 mAppsCustomizeContent.updateApps(apps);
3777 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 }
3779
3780 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003781 * A package was uninstalled. We take both the super set of packageNames
3782 * in addition to specific applications to remove, the reason being that
3783 * this can be called when a package is updated as well. In that scenario,
3784 * we only remove specific components from the workspace, where as
3785 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 *
3787 * Implementation of the method from LauncherModel.Callbacks.
3788 */
Winson Chung83892cc2013-05-01 16:53:33 -07003789 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3790 final ArrayList<ApplicationInfo> appInfos,
3791 final boolean matchPackageNamesOnly) {
3792 if (waitUntilResume(new Runnable() {
3793 public void run() {
3794 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3795 }
3796 })) {
3797 return;
3798 }
3799
3800 if (matchPackageNamesOnly) {
3801 mWorkspace.removeItemsByPackageName(packageNames);
3802 } else {
3803 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003804 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003805
Winson Chung785d2eb2011-04-14 16:08:02 -07003806 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003807 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003808 }
Winson Chunga1820962011-10-03 16:31:06 -07003809
3810 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003811 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812 }
Joe Onoratobe386092009-11-17 17:32:16 -08003813
3814 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003815 * A number of packages were updated.
3816 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003817
3818 private ArrayList<Object> mWidgetsAndShortcuts;
3819 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003820 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003821 bindPackagesUpdated(mWidgetsAndShortcuts);
3822 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003823 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003824 };
3825
3826 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3827 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3828 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003829 return;
3830 }
3831
Winson Chung785d2eb2011-04-14 16:08:02 -07003832 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003833 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003834 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003835 }
3836
Winson Chung400438b2011-01-16 17:53:48 -08003837 private int mapConfigurationOriActivityInfoOri(int configOri) {
3838 final Display d = getWindowManager().getDefaultDisplay();
3839 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3840 switch (d.getRotation()) {
3841 case Surface.ROTATION_0:
3842 case Surface.ROTATION_180:
3843 // We are currently in the same basic orientation as the natural orientation
3844 naturalOri = configOri;
3845 break;
3846 case Surface.ROTATION_90:
3847 case Surface.ROTATION_270:
3848 // We are currently in the other basic orientation to the natural orientation
3849 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3850 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3851 break;
3852 }
3853
3854 int[] oriMap = {
3855 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3856 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3857 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3858 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3859 };
3860 // Since the map starts at portrait, we need to offset if this device's natural orientation
3861 // is landscape.
3862 int indexOffset = 0;
3863 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3864 indexOffset = 1;
3865 }
3866 return oriMap[(d.getRotation() + indexOffset) % 4];
3867 }
Adam Cohen446e9402011-09-15 18:21:21 -07003868
Winson Chung4b919f82012-05-01 10:44:08 -07003869 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003870 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003871 getResources().getBoolean(R.bool.allow_rotation);
3872 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003873 }
Winson Chung4b919f82012-05-01 10:44:08 -07003874 public void lockScreenOrientation() {
3875 if (isRotationEnabled()) {
3876 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3877 .getConfiguration().orientation));
3878 }
3879 }
3880 public void unlockScreenOrientation(boolean immediate) {
3881 if (isRotationEnabled()) {
3882 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003883 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003884 } else {
3885 mHandler.postDelayed(new Runnable() {
3886 public void run() {
3887 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3888 }
3889 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003890 }
Winson Chung4b919f82012-05-01 10:44:08 -07003891 }
Winson Chung400438b2011-01-16 17:53:48 -08003892 }
3893
Winson Chung82f55532011-08-09 14:14:23 -07003894 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003895 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003896 if (DISABLE_CLINGS) {
3897 return false;
3898 }
3899
Brett Chabot2a9e2282011-08-23 15:03:13 -07003900 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003901 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003902
Michael Jurkae233a8b2013-03-19 13:49:20 +01003903 // Restricted secondary users (child mode) will potentially have very few apps
3904 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003905// boolean supportsLimitedUsers =
3906// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3907// Account[] accounts = AccountManager.get(this).getAccounts();
3908// if (supportsLimitedUsers && accounts.length == 0) {
3909// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3910// Bundle restrictions = um.getUserRestrictions();
3911// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3912// return false;
3913// }
3914// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003915 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003916 }
Michael Jurka22143132012-10-04 17:42:38 +02003917
Winson Chung7d7541e2011-09-16 20:14:36 -07003918 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003919 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003920 if (cling != null) {
3921 cling.init(this, positionData);
3922 cling.setVisibility(View.VISIBLE);
3923 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3924 if (animate) {
3925 cling.buildLayer();
3926 cling.setAlpha(0f);
3927 cling.animate()
3928 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003929 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003930 .setDuration(SHOW_CLING_DURATION)
3931 .setStartDelay(delay)
3932 .start();
3933 } else {
3934 cling.setAlpha(1f);
3935 }
Michael Jurka22143132012-10-04 17:42:38 +02003936 cling.setFocusableInTouchMode(true);
3937 cling.post(new Runnable() {
3938 public void run() {
3939 cling.setFocusable(true);
3940 cling.requestFocus();
3941 }
3942 });
3943 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3944 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003945 }
3946 return cling;
3947 }
Michael Jurka22143132012-10-04 17:42:38 +02003948
Winson Chung7d7541e2011-09-16 20:14:36 -07003949 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003950 // To catch cases where siblings of top-level views are made invisible, just check whether
3951 // the cling is directly set to GONE before dismissing it.
3952 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003953 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003954 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003955 anim.addListener(new AnimatorListenerAdapter() {
3956 public void onAnimationEnd(Animator animation) {
3957 cling.setVisibility(View.GONE);
3958 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003959 // We should update the shared preferences on a background thread
3960 new Thread("dismissClingThread") {
3961 public void run() {
3962 SharedPreferences.Editor editor = mSharedPrefs.edit();
3963 editor.putBoolean(flag, true);
3964 editor.commit();
3965 }
3966 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003967 };
3968 });
3969 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003970 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003971 }
3972 }
Michael Jurka22143132012-10-04 17:42:38 +02003973
Winson Chung9d9d74f2011-09-19 11:49:12 -07003974 private void removeCling(int id) {
3975 final View cling = findViewById(id);
3976 if (cling != null) {
3977 final ViewGroup parent = (ViewGroup) cling.getParent();
3978 parent.post(new Runnable() {
3979 @Override
3980 public void run() {
3981 parent.removeView(cling);
3982 }
3983 });
Michael Jurka22143132012-10-04 17:42:38 +02003984 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003985 }
3986 }
Michael Jurka974c3862012-05-22 22:00:31 -07003987
3988 private boolean skipCustomClingIfNoAccounts() {
3989 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3990 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3991 if (customCling) {
3992 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04003993 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07003994 Account[] accounts = am.getAccountsByType("com.google");
3995 return accounts.length == 0;
3996 }
3997 return false;
3998 }
3999
Winson Chung7d7541e2011-09-16 20:14:36 -07004000 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004001 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004002 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004003 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4004 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004005 // If we're not using the default workspace layout, replace workspace cling
4006 // with a custom workspace cling (usually specified in an overlay)
4007 // For now, only do this on tablets
4008 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004009 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004010 // Use a custom cling
4011 View cling = findViewById(R.id.workspace_cling);
4012 ViewGroup clingParent = (ViewGroup) cling.getParent();
4013 int clingIndex = clingParent.indexOfChild(cling);
4014 clingParent.removeViewAt(clingIndex);
4015 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4016 clingParent.addView(customCling, clingIndex);
4017 customCling.setId(R.id.workspace_cling);
4018 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004019 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004020 } else {
4021 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004022 }
4023 }
4024 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004025 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004026 if (isClingsEnabled() &&
4027 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004028 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004029 } else {
4030 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 }
4032 }
4033 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004034 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004035 if (isClingsEnabled() &&
4036 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4037 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004038 } else {
4039 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004040 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004042 }
4043 public boolean isFolderClingVisible() {
4044 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004045 if (cling != null) {
4046 return cling.getVisibility() == View.VISIBLE;
4047 }
4048 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004049 }
Winson Chung82f55532011-08-09 14:14:23 -07004050 public void dismissWorkspaceCling(View v) {
4051 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004052 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004053 }
4054 public void dismissAllAppsCling(View v) {
4055 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4057 }
4058 public void dismissFolderCling(View v) {
4059 Cling cling = (Cling) findViewById(R.id.folder_cling);
4060 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004061 }
4062
Winson Chung80baf5a2010-08-09 16:03:15 -07004063 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004064 * Prints out out state for debugging.
4065 */
4066 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004067 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004068 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004069 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4070 Log.d(TAG, "mRestoring=" + mRestoring);
4071 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4072 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004073 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004074 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004075
Winson Chung785d2eb2011-04-14 16:08:02 -07004076 if (mAppsCustomizeContent != null) {
4077 mAppsCustomizeContent.dumpState();
4078 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004079 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004080 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004081
4082 @Override
4083 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4084 super.dump(prefix, fd, writer, args);
4085 writer.println(" ");
4086 writer.println("Debug logs: ");
4087 for (int i = 0; i < sDumpLogs.size(); i++) {
4088 writer.println(" " + sDumpLogs.get(i));
4089 }
4090 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004091
4092 public static void dumpDebugLogsToConsole() {
4093 Log.d(TAG, "");
4094 Log.d(TAG, "*********************");
4095 Log.d(TAG, "Launcher debug logs: ");
4096 for (int i = 0; i < sDumpLogs.size(); i++) {
4097 Log.d(TAG, " " + sDumpLogs.get(i));
4098 }
4099 Log.d(TAG, "*********************");
4100 Log.d(TAG, "");
4101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004102}
Michael Jurka2763be32011-02-24 11:19:57 -08004103
Michael Jurkaabded662011-03-04 12:06:57 -08004104interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004105 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004106 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004107 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004108 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004109 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004110}