blob: 2f1c8f0fc31cb5b90812476beca0c7fa8f83931c [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;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070083import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080084import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070092import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Daniel Sandler325dc232013-06-05 22:57:57 -040097import com.android.launcher3.R;
98import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Michael Jurkac402cd92013-05-20 15:49:32 +0200127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
145
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800146 static final int SCREEN_COUNT = 5;
147 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800150 // To turn on these properties, type
151 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
152 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
153 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Winson Chung2672ff92012-05-04 16:22:30 -0700155 // The Intent extra that defines whether to ignore the launch animation
156 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400157 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700164 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
165 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700175 // Type: int
176 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
177 // Type: int
178 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
179 // Type: parcelable
180 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700183 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100184 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700185 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100186 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700187
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700189 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700190 private State mState = State.WORKSPACE;
191 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700193
Joe Onorato9c1289c2009-08-17 11:03:03 -0400194 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700195 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
196 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700197 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700198 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800201 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Winson Chunga2413752012-04-03 14:22:34 -0700203 // How long to wait before the new-shortcut animation automatically pans the workspace
204 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
205
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800206 private final BroadcastReceiver mCloseSystemDialogsReceiver
207 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800208 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private LayoutInflater mInflater;
211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800213 private View mQsbDivider;
214 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700215 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800216 private DragLayer mDragLayer;
217 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700219 private AppWidgetManager mAppWidgetManager;
220 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700221
Michael Jurkac9d95c52011-08-29 14:03:34 -0700222 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700223 private AppWidgetProviderInfo mPendingAddWidgetInfo;
224
Michael Jurka0280c3b2010-09-17 15:00:07 -0700225 private int[] mTmpAddItemCellCoordinates = new int[2];
226
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private FolderInfo mFolderInfo;
228
Winson Chung3d503fb2011-07-13 17:25:49 -0700229 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800230 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700231
Winson Chungc51db6a2011-10-05 11:44:49 -0700232 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700233 private AppsCustomizeTabHost mAppsCustomizeTabHost;
234 private AppsCustomizePagedView mAppsCustomizeContent;
235 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700238 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
239 // scroll issues (because the workspace may not have been measured yet) and extra work.
240 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
241 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
243 private SpannableStringBuilder mDefaultKeySsb = null;
244
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 private boolean mWorkspaceLoading = true;
246
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800247 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private boolean mRestoring;
249 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700250 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
Michael Jurka7607c2f2013-04-03 14:33:19 -0700252 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
253
Winson Chung4a2afa32012-07-19 14:53:05 -0700254 // Keep track of whether the user has left launcher
255 private static boolean sPausedFromUserAction = false;
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Bundle mSavedInstanceState;
258
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800260 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800261 private boolean mUserPresent = true;
262 private boolean mVisible = false;
263 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700264 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700266 private static LocaleConfiguration sLocaleConfiguration = null;
267
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700268 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700269
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700270 private Intent mAppMarketIntent = null;
271
Adam Cohended9f8d2010-11-03 13:25:16 -0700272 // Related to the auto-advancing of widgets
273 private final int ADVANCE_MSG = 1;
274 private final int mAdvanceInterval = 20000;
275 private final int mAdvanceStagger = 250;
276 private long mAutoAdvanceSentTime;
277 private long mAutoAdvanceTimeLeft = -1;
278 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
279 new HashMap<View, AppWidgetProviderInfo>();
280
Winson Chung400438b2011-01-16 17:53:48 -0800281 // Determines how long to wait after a rotation before restoring the screen orientation to
282 // match the sensor state.
283 private final int mRestoreScreenOrientationDelay = 500;
284
Michael Jurka4ef207b2010-11-29 17:05:45 -0800285 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700286 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
287 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
288 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800289
Craig Mautner360310b2012-10-26 15:13:08 -0700290 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700291
Adam Cohen1462de32012-07-24 22:34:36 -0700292 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
293
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700294 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
295
Winson Chung46353de2012-02-16 14:05:10 -0800296 // We only want to get the SharedPreferences once since it does an FS stat each time we get
297 // it from the context.
298 private SharedPreferences mSharedPrefs;
299
Adam Cohene25af792013-06-06 23:08:25 -0700300 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
301
Winson Chungf0c6ae02012-03-21 16:10:31 -0700302 // Holds the page that we need to animate to, and the icon views that we need to animate up
303 // when we scroll to that page on resume.
304 private int mNewShortcutAnimatePage = -1;
305 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700306 private ImageView mFolderIconImageView;
307 private Bitmap mFolderIconBitmap;
308 private Canvas mFolderIconCanvas;
309 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700310
Michael Jurkaddd62e92011-02-16 17:49:14 -0800311 private BubbleTextView mWaitingForResume;
312
Michael Jurka22143132012-10-04 17:42:38 +0200313 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
314 = new HideFromAccessibilityHelper();
315
Michael Jurkac1f5d262011-09-30 19:32:27 -0700316 private Runnable mBuildLayersRunnable = new Runnable() {
317 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700318 if (mWorkspace != null) {
319 mWorkspace.buildPageHardwareLayers();
320 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700321 }
322 };
323
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800324 private static ArrayList<PendingAddArguments> sPendingAddList
325 = new ArrayList<PendingAddArguments>();
326
Michael Jurka0a457bf2012-11-19 14:05:05 -0800327 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
328
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800329 private static class PendingAddArguments {
330 int requestCode;
331 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700332 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800333 int screen;
334 int cellX;
335 int cellY;
336 }
337
Michael Jurka0a457bf2012-11-19 14:05:05 -0800338 private static boolean isPropertyEnabled(String propertyName) {
339 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700340 }
341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 @Override
343 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700344 if (DEBUG_STRICT_MODE) {
345 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
346 .detectDiskReads()
347 .detectDiskWrites()
348 .detectNetwork() // or .detectAll() for all detectable problems
349 .penaltyLog()
350 .build());
351 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
352 .detectLeakedSqlLiteObjects()
353 .detectLeakedClosableObjects()
354 .penaltyLog()
355 .penaltyDeath()
356 .build());
357 }
358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800360 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700361 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
362 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800363 mModel = app.setLauncher(this);
364 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400365 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700367
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700368 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700369 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
370 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700371
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700372 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
373 // this also ensures that any synchronous binding below doesn't re-trigger another
374 // LauncherModel load.
375 mPaused = false;
376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200378 android.os.Debug.startMethodTracing(
379 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 }
381
382 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 setContentView(R.layout.launcher);
384 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700385 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800387 registerContentObservers();
388
Joe Onorato7c312c12009-08-13 21:36:53 -0700389 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 mSavedState = savedInstanceState;
392 restoreState(mSavedState);
393
Winson Chunga12a2502010-12-20 14:41:35 -0800394 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700395 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200396 mAppsCustomizeContent.onPackagesUpdated(
397 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700398 }
Winson Chunga12a2502010-12-20 14:41:35 -0800399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 if (PROFILE_STARTUP) {
401 android.os.Debug.stopMethodTracing();
402 }
403
404 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700405 if (sPausedFromUserAction) {
406 // If the user leaves launcher, then we should just load items asynchronously when
407 // they return.
408 mModel.startLoader(true, -1);
409 } else {
410 // We only load the page synchronously if the user rotates (or triggers a
411 // configuration change) while launcher is in the foreground
412 mModel.startLoader(true, mWorkspace.getCurrentPage());
413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415
Michael Jurkac57b7a82011-08-09 22:02:20 -0700416 if (!mModel.isAllAppsLoaded()) {
417 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
418 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 // For handling default keys
422 mDefaultKeySsb = new SpannableStringBuilder();
423 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800424
425 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
426 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800427
Adam Cohenf9426d52012-06-04 17:26:21 -0700428 updateGlobalIcons();
429
430 // On large interfaces, we want the screen to auto-rotate based on the current orientation
431 unlockScreenOrientation(true);
432 }
433
Winson Chung4a2afa32012-07-19 14:53:05 -0700434 protected void onUserLeaveHint() {
435 super.onUserLeaveHint();
436 sPausedFromUserAction = true;
437 }
438
Adam Cohenf9426d52012-06-04 17:26:21 -0700439 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700440 boolean searchVisible = false;
441 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800442 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700443 int coi = getCurrentOrientationIndexForGlobalIcons();
444 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
445 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 updateAppMarketIcon();
447 searchVisible = updateGlobalSearchIcon();
448 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 if (sGlobalSearchIcon[coi] != null) {
451 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sVoiceSearchIcon[coi] != null) {
455 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700456 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 if (sAppMarketIcon[coi] != null) {
459 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800460 }
Winson Chungadf0c182012-08-23 14:59:07 -0700461 if (mSearchDropTargetBar != null) {
462 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
Romain Guycbb89e42009-06-08 15:52:54 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700467 if (sLocaleConfiguration == null) {
468 new AsyncTask<Void, Void, LocaleConfiguration>() {
469 @Override
470 protected LocaleConfiguration doInBackground(Void... unused) {
471 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
472 readConfiguration(Launcher.this, localeConfiguration);
473 return localeConfiguration;
474 }
475
476 @Override
477 protected void onPostExecute(LocaleConfiguration result) {
478 sLocaleConfiguration = result;
479 checkForLocaleChange(); // recursive, but now with a locale configuration
480 }
481 }.execute();
482 return;
483 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final Configuration configuration = getResources().getConfiguration();
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final String locale = configuration.locale.toString();
489
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final int mcc = configuration.mcc;
492
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 final int mnc = configuration.mnc;
495
Romain Guy84f296c2009-11-04 15:00:44 -0800496 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497
Romain Guy84f296c2009-11-04 15:00:44 -0800498 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499 sLocaleConfiguration.locale = locale;
500 sLocaleConfiguration.mcc = mcc;
501 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700502
Joe Onorato0589f0f2010-02-08 13:44:00 -0800503 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504
505 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
506 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700507 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700508 public void run() {
509 writeConfiguration(Launcher.this, localeConfiguration);
510 }
511 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700512 }
513 }
514
515 private static class LocaleConfiguration {
516 public String locale;
517 public int mcc = -1;
518 public int mnc = -1;
519 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700520
Romain Guy98d01652009-06-30 16:21:04 -0700521 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
522 DataInputStream in = null;
523 try {
524 in = new DataInputStream(context.openFileInput(PREFERENCES));
525 configuration.locale = in.readUTF();
526 configuration.mcc = in.readInt();
527 configuration.mnc = in.readInt();
528 } catch (FileNotFoundException e) {
529 // Ignore
530 } catch (IOException e) {
531 // Ignore
532 } finally {
533 if (in != null) {
534 try {
535 in.close();
536 } catch (IOException e) {
537 // Ignore
538 }
539 }
540 }
541 }
542
543 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
544 DataOutputStream out = null;
545 try {
546 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
547 out.writeUTF(configuration.locale);
548 out.writeInt(configuration.mcc);
549 out.writeInt(configuration.mnc);
550 out.flush();
551 } catch (FileNotFoundException e) {
552 // Ignore
553 } catch (IOException e) {
554 //noinspection ResultOfMethodCallIgnored
555 context.getFileStreamPath(PREFERENCES).delete();
556 } finally {
557 if (out != null) {
558 try {
559 out.close();
560 } catch (IOException e) {
561 // Ignore
562 }
563 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 }
565 }
566
Bjorn Bringertc459e522013-06-07 19:36:01 +0100567 public LayoutInflater getInflater() {
568 return mInflater;
569 }
570
Adam Cohen716b51e2011-06-30 12:09:54 -0700571 public DragLayer getDragLayer() {
572 return mDragLayer;
573 }
574
Winson Chung36a62fe2012-05-06 18:04:42 -0700575 boolean isDraggingEnabled() {
576 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
577 // that is subsequently removed from the workspace in startBinding().
578 return !mModel.isLoadingWorkspace();
579 }
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 static int getScreen() {
582 synchronized (sLock) {
583 return sScreen;
584 }
585 }
586
587 static void setScreen(int screen) {
588 synchronized (sLock) {
589 sScreen = screen;
590 }
591 }
592
Winson Chung557d6ed2011-07-08 15:34:52 -0700593 /**
594 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
595 * a configuration step, this allows the proper animations to run after other transitions.
596 */
597 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700598 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 switch (args.requestCode) {
600 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
602 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800603 break;
604 case REQUEST_PICK_SHORTCUT:
605 processShortcut(args.intent);
606 break;
607 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700608 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
609 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700610 result = true;
611 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800612 case REQUEST_CREATE_APPWIDGET:
613 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800614 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 result = true;
616 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_PICK_WALLPAPER:
618 // We just wanted the activity result here so we can clear mWaitingForResult
619 break;
620 }
Michael Jurka27614d22012-04-02 06:40:22 -0700621 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
622 // if you turned the screen off and then back while in All Apps, Launcher would not
623 // return to the workspace. Clearing mAddInfo.container here fixes this issue
624 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700625 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800626 }
627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700629 protected void onActivityResult(
630 final int requestCode, final int resultCode, final Intent data) {
631 if (requestCode == REQUEST_BIND_APPWIDGET) {
632 int appWidgetId = data != null ?
633 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
634 if (resultCode == RESULT_CANCELED) {
635 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
636 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700637 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700638 }
639 return;
640 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700641 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
643 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700644 mWaitingForResult = false;
645
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 // We have special handling for widgets
647 if (isWidgetDrop) {
648 int appWidgetId = data != null ?
649 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700650 if (appWidgetId < 0) {
651 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
652 "widget configuration activity.");
653 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
654 } else {
655 completeTwoStageWidgetDrop(resultCode, appWidgetId);
656 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 return;
658 }
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // The pattern used here is that a user PICKs a specific application,
661 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
664 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700665 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 final PendingAddArguments args = new PendingAddArguments();
667 args.requestCode = requestCode;
668 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700669 args.container = mPendingAddInfo.container;
670 args.screen = mPendingAddInfo.screen;
671 args.cellX = mPendingAddInfo.cellX;
672 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 if (isWorkspaceLocked()) {
674 sPendingAddList.add(args);
675 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700676 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
682 null);
683 }
684
685 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700686 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 Runnable onCompleteRunnable = null;
689 int animationType = 0;
690
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700691 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 if (resultCode == RESULT_OK) {
693 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
694 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700695 mPendingAddWidgetInfo);
696 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 onCompleteRunnable = new Runnable() {
698 @Override
699 public void run() {
700 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
701 mPendingAddInfo.screen, layout, null);
702 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
703 null);
704 }
705 };
706 } else if (resultCode == RESULT_CANCELED) {
707 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
708 onCompleteRunnable = new Runnable() {
709 @Override
710 public void run() {
711 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
712 null);
713 }
714 };
715 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700716 if (mDragLayer.getAnimatedView() != null) {
717 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
718 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
719 animationType, boundWidget, true);
720 } else {
721 // The animated view may be null in the case of a rotation during widget configuration
722 onCompleteRunnable.run();
723 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
725
726 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700727 protected void onStop() {
728 super.onStop();
729 FirstFrameAnimatorHelper.setIsVisible(false);
730 }
731
732 @Override
733 protected void onStart() {
734 super.onStart();
735 FirstFrameAnimatorHelper.setIsVisible(true);
736 }
737
738 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200740 long startTime = 0;
741 if (DEBUG_RESUME_TIME) {
742 startTime = System.currentTimeMillis();
743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700745
Winson Chung4a2afa32012-07-19 14:53:05 -0700746 // Restore the previous launcher state
747 if (mOnResumeState == State.WORKSPACE) {
748 showWorkspace(false);
749 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
750 showAllApps(false);
751 }
752 mOnResumeState = State.NONE;
753
Craig Mautner360310b2012-10-26 15:13:08 -0700754 // Background was set to gradient in onPause(), restore to black if in all apps.
755 setWorkspaceBackground(mState == State.WORKSPACE);
756
Winson Chungde0fb8f2012-05-08 14:37:08 -0700757 // Process any items that were added while Launcher was away
758 InstallShortcutReceiver.flushInstallQueue(this);
759
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800760 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700761 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700762 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400763 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700764 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400765 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700766 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200768 if (mOnResumeCallbacks.size() > 0) {
769 // We might have postponed some bind calls until onResume (see waitUntilResume) --
770 // execute them here
771 long startTimeCallbacks = 0;
772 if (DEBUG_RESUME_TIME) {
773 startTimeCallbacks = System.currentTimeMillis();
774 }
775
776 if (mAppsCustomizeContent != null) {
777 mAppsCustomizeContent.setBulkBind(true);
778 }
779 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
780 mOnResumeCallbacks.get(i).run();
781 }
782 if (mAppsCustomizeContent != null) {
783 mAppsCustomizeContent.setBulkBind(false);
784 }
785 mOnResumeCallbacks.clear();
786 if (DEBUG_RESUME_TIME) {
787 Log.d(TAG, "Time spent processing callbacks in onResume: " +
788 (System.currentTimeMillis() - startTimeCallbacks));
789 }
Michael Jurka447bf842013-05-15 14:52:15 +0200790 }
Winson Chunge4e50662012-01-23 14:45:13 -0800791
792 // Reset the pressed state of icons that were locked in the press state while activities
793 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800794 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800795 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800796 mWaitingForResume.setStayPressed(false);
797 }
Winson Chunge4e50662012-01-23 14:45:13 -0800798 if (mAppsCustomizeContent != null) {
799 // Resets the previous all apps icon press state
800 mAppsCustomizeContent.resetDrawableState();
801 }
Adam Cohen06dff352012-06-01 17:17:08 -0700802 // It is possible that widgets can receive updates while launcher is not in the foreground.
803 // Consequently, the widgets will be inflated in the orientation of the foreground activity
804 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
805 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700806 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700807
808 // Again, as with the above scenario, it's possible that one or more of the global icons
809 // were updated in the wrong orientation.
810 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200811 if (DEBUG_RESUME_TIME) {
812 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
813 }
Adam Cohen06dff352012-06-01 17:17:08 -0700814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700818 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
819 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
820 // when Launcher resumes and we are still in AllApps.
821 updateWallpaperVisibility(true);
822
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700823 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700824 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800825 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700826 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700827 }
Romain Guycbb89e42009-06-08 15:52:54 -0700828
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700829 @Override
830 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400831 // Flag the loader to stop early before switching
832 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700833 if (mAppsCustomizeContent != null) {
834 mAppsCustomizeContent.surrender();
835 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800836 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700837 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700838
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800839 // We can't hide the IME if it was forced open. So don't bother
840 /*
841 @Override
842 public void onWindowFocusChanged(boolean hasFocus) {
843 super.onWindowFocusChanged(hasFocus);
844
845 if (hasFocus) {
846 final InputMethodManager inputManager = (InputMethodManager)
847 getSystemService(Context.INPUT_METHOD_SERVICE);
848 WindowManager.LayoutParams lp = getWindow().getAttributes();
849 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
850 android.os.Handler()) {
851 protected void onReceiveResult(int resultCode, Bundle resultData) {
852 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
853 }
854 });
855 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
856 }
857 }
858 */
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 private boolean acceptFilter() {
861 final InputMethodManager inputManager = (InputMethodManager)
862 getSystemService(Context.INPUT_METHOD_SERVICE);
863 return !inputManager.isFullscreenMode();
864 }
865
866 @Override
867 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700868 final int uniChar = event.getUnicodeChar();
869 final boolean handled = super.onKeyDown(keyCode, event);
870 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
871 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
873 keyCode, event);
874 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700875 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700876 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700877 // showSearchDialog()
878 // If there are multiple keystrokes before the search dialog takes focus,
879 // onSearchRequested() will be called for every keystroke,
880 // but it is idempotent, so it's fine.
881 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883 }
884
Joe Onorato8a9625e2010-01-28 15:55:35 -0800885 // Eat the long press event so the keyboard doesn't come up.
886 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
887 return true;
888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 return handled;
891 }
892
Karl Rosaen138a0412009-04-23 19:00:21 -0700893 private String getTypedText() {
894 return mDefaultKeySsb.toString();
895 }
896
897 private void clearTypedText() {
898 mDefaultKeySsb.clear();
899 mDefaultKeySsb.clearSpans();
900 Selection.setSelection(mDefaultKeySsb, 0);
901 }
902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700904 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
905 * State
906 */
907 private static State intToState(int stateOrdinal) {
908 State state = State.WORKSPACE;
909 final State[] stateValues = State.values();
910 for (int i = 0; i < stateValues.length; i++) {
911 if (stateValues[i].ordinal() == stateOrdinal) {
912 state = stateValues[i];
913 break;
914 }
915 }
916 return state;
917 }
918
919 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 * Restores the previous state, if it exists.
921 *
922 * @param savedState The previous state.
923 */
924 private void restoreState(Bundle savedState) {
925 if (savedState == null) {
926 return;
927 }
928
Michael Jurka883f55b2010-10-21 15:47:14 -0700929 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700930 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700931 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800932 }
933
Winson Chungf0c6ae02012-03-21 16:10:31 -0700934 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700936 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
940 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700941
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
943 mPendingAddInfo.container = pendingAddContainer;
944 mPendingAddInfo.screen = pendingAddScreen;
945 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
946 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
948 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
949 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700950 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 mRestoring = true;
952 }
953
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
956 if (renameFolder) {
957 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700958 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 mRestoring = true;
960 }
Winson Chunga12a2502010-12-20 14:41:35 -0800961
Winson Chung785d2eb2011-04-14 16:08:02 -0700962
963 // Restore the AppsCustomize tab
964 if (mAppsCustomizeTabHost != null) {
965 String curTab = savedState.getString("apps_customize_currentTab");
966 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700967 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700969 mAppsCustomizeContent.loadAssociatedPages(
970 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 }
972
Winson Chung5afbf7b2011-07-25 11:53:08 -0700973 int currentIndex = savedState.getInt("apps_customize_currentIndex");
974 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Finds all the views we need and configure them properly.
980 */
981 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700982 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400983
Craig Mautner360310b2012-10-26 15:13:08 -0700984 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700985 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
986 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700987 mQsbDivider = findViewById(R.id.qsb_divider);
988 mDockDivider = findViewById(R.id.dock_divider);
989
990 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
991 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Winson Chung4c98d922011-05-31 16:50:48 -0700993 // Setup the drag layer
994 mDragLayer.setup(this, dragController);
995
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 // Setup the hotseat
997 mHotseat = (Hotseat) findViewById(R.id.hotseat);
998 if (mHotseat != null) {
999 mHotseat.setup(this);
1000 }
1001
Winson Chung4c98d922011-05-31 16:50:48 -07001002 // Setup the workspace
1003 mWorkspace.setHapticFeedbackEnabled(false);
1004 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001005 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001006 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001007
Winson Chungf0ea4d32011-06-06 14:27:16 -07001008 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001009 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001010
Winson Chungf0ea4d32011-06-06 14:27:16 -07001011 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001012 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001013 mAppsCustomizeContent = (AppsCustomizePagedView)
1014 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1015 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Winson Chung3d503fb2011-07-13 17:25:49 -07001017 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001018 dragController.setDragScoller(mWorkspace);
1019 dragController.setScrollView(mDragLayer);
1020 dragController.setMoveTarget(mWorkspace);
1021 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001022 if (mSearchDropTargetBar != null) {
1023 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026
1027 /**
1028 * Creates a view representing a shortcut.
1029 *
1030 * @param info The data structure describing the shortcut.
1031 *
1032 * @return A View inflated from R.layout.application.
1033 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001036 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038
1039 /**
1040 * Creates a view representing a shortcut inflated from the specified resource.
1041 *
1042 * @param layoutResId The id of the XML layout used to create the shortcut.
1043 * @param parent The group the shortcut belongs to.
1044 * @param info The data structure describing the shortcut.
1045 *
1046 * @return A View inflated from layoutResId.
1047 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001048 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001049 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1050 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 return favorite;
1053 }
1054
1055 /**
1056 * Add an application shortcut to the workspace.
1057 *
1058 * @param data The intent describing the application.
1059 * @param cellInfo The position on screen where to create the shortcut.
1060 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001061 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064
Adam Cohenfbba09b2011-07-18 21:43:05 -07001065 // First we check if we already know the exact location where we want to add this item.
1066 if (cellX >= 0 && cellY >= 0) {
1067 cellXY[0] = cellX;
1068 cellXY[1] = cellY;
1069 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001070 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 return;
1072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001074 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001075
Romain Guy73b979d2009-06-09 12:57:21 -07001076 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001077 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001079 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001080 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001081 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001082 } else {
1083 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085 }
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 /**
1088 * Add a shortcut to the workspace.
1089 *
1090 * @param data The intent describing the shortcut.
1091 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001093 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1094 int cellY) {
1095 int[] cellXY = mTmpAddItemCellCoordinates;
1096 int[] touchXY = mPendingAddInfo.dropPos;
1097 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Michael Jurkad3ef3062010-11-23 16:23:58 -08001099 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001100
Adam Cohen558baaf2011-08-15 15:22:57 -07001101 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001102 if (info == null) {
1103 return;
1104 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001105 final View view = createShortcut(info);
1106
Adam Cohenfbba09b2011-07-18 21:43:05 -07001107 // First we check if we already know the exact location where we want to add this item.
1108 if (cellX >= 0 && cellY >= 0) {
1109 cellXY[0] = cellX;
1110 cellXY[1] = cellY;
1111 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001112
1113 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001114 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001115 true, null,null)) {
1116 return;
1117 }
1118 DragObject dragObject = new DragObject();
1119 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001120 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1121 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001122 return;
1123 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001124 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001125 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001126 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001127 foundCellSpan = (result != null);
1128 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001129 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001130 }
1131
1132 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001133 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 return;
1135 }
1136
Adam Cohen558baaf2011-08-15 15:22:57 -07001137 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138
1139 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001140 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1141 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143 }
1144
Adam Cohen2f093b62012-04-30 18:59:53 -07001145 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1146 int minHeight) {
1147 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001148 // We want to account for the extra amount of padding that we are adding to the widget
1149 // to ensure that it gets the full amount of space that it has requested
1150 int requiredWidth = minWidth + padding.left + padding.right;
1151 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001152 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001153 }
1154
Adam Cohen2f093b62012-04-30 18:59:53 -07001155 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1156 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001157 }
1158
Adam Cohen2f093b62012-04-30 18:59:53 -07001159 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1160 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001161 }
1162
Adam Cohen2f093b62012-04-30 18:59:53 -07001163 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1164 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001165 }
1166
Adam Cohen2f093b62012-04-30 18:59:53 -07001167 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1168 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001169 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001170 }
1171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001175 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001178 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1179 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1180 if (appWidgetInfo == null) {
1181 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1182 }
Romain Guycbb89e42009-06-08 15:52:54 -07001183
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001185 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001186
Adam Cohen2f093b62012-04-30 18:59:53 -07001187 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1188 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001189
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001191 // We have saved the position to which the widget was dragged-- this really only matters
1192 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001193 int[] cellXY = mTmpAddItemCellCoordinates;
1194 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001195 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001196 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001197 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1198 cellXY[0] = mPendingAddInfo.cellX;
1199 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001200 spanXY[0] = mPendingAddInfo.spanX;
1201 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001202 foundCellSpan = true;
1203 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001205 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001206 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1207 spanXY[1], cellXY, finalSpan);
1208 spanXY[0] = finalSpan[0];
1209 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001210 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001212 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001213 }
1214
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001216 if (appWidgetId != -1) {
1217 // Deleting an app widget ID is a void call but writes to disk before returning
1218 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001219 new Thread("deleteAppWidgetId") {
1220 public void run() {
1221 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1222 }
1223 }.start();
1224 }
Winson Chung93eef082012-03-23 15:59:27 -07001225 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001226 return;
1227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001229 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001230 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1231 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001232 launcherInfo.spanX = spanXY[0];
1233 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1235 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
1240 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001241 if (hostView == null) {
1242 // Perform actual inflation because we're live
1243 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1244 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1245 } else {
1246 // The AppWidgetHostView has already been inflated and instantiated
1247 launcherInfo.hostView = hostView;
1248 }
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001251 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001252 launcherInfo.notifyWidgetSizeChanged(this);
1253
Winson Chung3d503fb2011-07-13 17:25:49 -07001254 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001256
1257 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001259 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
Romain Guycbb89e42009-06-08 15:52:54 -07001261
Adam Cohended9f8d2010-11-03 13:25:16 -07001262 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1263 @Override
1264 public void onReceive(Context context, Intent intent) {
1265 final String action = intent.getAction();
1266 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1267 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001268 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001270
Winson Chungc100e8e2011-08-09 16:02:43 -07001271 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001272 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001273 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1274 mAppsCustomizeTabHost.reset();
1275 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001276 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001277 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1278 mUserPresent = true;
1279 updateRunning();
1280 }
1281 }
1282 };
1283
1284 @Override
1285 public void onAttachedToWindow() {
1286 super.onAttachedToWindow();
1287
1288 // Listen for broadcasts related to user-presence
1289 final IntentFilter filter = new IntentFilter();
1290 filter.addAction(Intent.ACTION_SCREEN_OFF);
1291 filter.addAction(Intent.ACTION_USER_PRESENT);
1292 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001293 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001294 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001295 mVisible = true;
1296 }
1297
1298 @Override
1299 public void onDetachedFromWindow() {
1300 super.onDetachedFromWindow();
1301 mVisible = false;
1302
Adam Cohend113e0c2010-11-11 10:48:05 -08001303 if (mAttached) {
1304 unregisterReceiver(mReceiver);
1305 mAttached = false;
1306 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001307 updateRunning();
1308 }
1309
1310 public void onWindowVisibilityChanged(int visibility) {
1311 mVisible = visibility == View.VISIBLE;
1312 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001313 // The following code used to be in onResume, but it turns out onResume is called when
1314 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1315 // is a more appropriate event to handle
1316 if (mVisible) {
1317 mAppsCustomizeTabHost.onWindowVisible();
1318 if (!mWorkspaceLoading) {
1319 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001320 // We want to let Launcher draw itself at least once before we force it to build
1321 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001322 // apps is nice and speedy.
1323 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001324 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001325 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001326 if (mStarted) return;
1327 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001328 // We delay the layer building a bit in order to give
1329 // other message processing a time to run. In particular
1330 // this avoids a delay in hiding the IME if it was
1331 // currently shown, because doing that may involve
1332 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001333 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001334 final ViewTreeObserver.OnDrawListener listener = this;
1335 mWorkspace.post(new Runnable() {
1336 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001337 if (mWorkspace != null &&
1338 mWorkspace.getViewTreeObserver() != null) {
1339 mWorkspace.getViewTreeObserver().
1340 removeOnDrawListener(listener);
1341 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001342 }
1343 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001344 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001345 }
1346 });
1347 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001348 // When Launcher comes back to foreground, a different Activity might be responsible for
1349 // the app market intent, so refresh the icon
1350 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001351 clearTypedText();
1352 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001353 }
1354
1355 private void sendAdvanceMessage(long delay) {
1356 mHandler.removeMessages(ADVANCE_MSG);
1357 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1358 mHandler.sendMessageDelayed(msg, delay);
1359 mAutoAdvanceSentTime = System.currentTimeMillis();
1360 }
1361
1362 private void updateRunning() {
1363 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1364 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1365 mAutoAdvanceRunning = autoAdvanceRunning;
1366 if (autoAdvanceRunning) {
1367 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1368 sendAdvanceMessage(delay);
1369 } else {
1370 if (!mWidgetsToAdvance.isEmpty()) {
1371 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1372 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1373 }
1374 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001375 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001376 }
1377 }
1378 }
1379
1380 private final Handler mHandler = new Handler() {
1381 @Override
1382 public void handleMessage(Message msg) {
1383 if (msg.what == ADVANCE_MSG) {
1384 int i = 0;
1385 for (View key: mWidgetsToAdvance.keySet()) {
1386 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1387 final int delay = mAdvanceStagger * i;
1388 if (v instanceof Advanceable) {
1389 postDelayed(new Runnable() {
1390 public void run() {
1391 ((Advanceable) v).advance();
1392 }
1393 }, delay);
1394 }
1395 i++;
1396 }
1397 sendAdvanceMessage(mAdvanceInterval);
1398 }
1399 }
1400 };
1401
1402 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001403 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001404 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1405 if (v instanceof Advanceable) {
1406 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001407 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001408 updateRunning();
1409 }
1410 }
1411
1412 void removeWidgetToAutoAdvance(View hostView) {
1413 if (mWidgetsToAdvance.containsKey(hostView)) {
1414 mWidgetsToAdvance.remove(hostView);
1415 updateRunning();
1416 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001417 }
1418
Joe Onorato9c1289c2009-08-17 11:03:03 -04001419 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001420 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 launcherInfo.hostView = null;
1422 }
1423
Winson Chung93eef082012-03-23 15:59:27 -07001424 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1425 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1426 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001427 }
1428
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001429 public LauncherAppWidgetHost getAppWidgetHost() {
1430 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Romain Guycbb89e42009-06-08 15:52:54 -07001432
Winson Chunga9abd0e2010-10-27 17:18:37 -07001433 public LauncherModel getModel() {
1434 return mModel;
1435 }
1436
Bjorn Bringertc459e522013-06-07 19:36:01 +01001437 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001438 getWindow().closeAllPanels();
1439
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001440 // Whatever we were doing is hereby canceled.
1441 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001442 }
1443
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 @Override
1445 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001446 long startTime = 0;
1447 if (DEBUG_RESUME_TIME) {
1448 startTime = System.currentTimeMillis();
1449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 super.onNewIntent(intent);
1451
1452 // Close the menu
1453 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001454 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001455 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001456
Jamie Genniscb222e82012-10-23 15:44:26 -07001457 final boolean alreadyOnHome =
1458 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001459 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001460
Jamie Genniscb222e82012-10-23 15:44:26 -07001461 Runnable processIntent = new Runnable() {
1462 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001463 if (mWorkspace == null) {
1464 // Can be cases where mWorkspace is null, this prevents a NPE
1465 return;
1466 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001467 Folder openFolder = mWorkspace.getOpenFolder();
1468 // In all these cases, only animate if we're already on home
1469 mWorkspace.exitWidgetResizeMode();
1470 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1471 openFolder == null) {
1472 mWorkspace.moveToDefaultScreen(true);
1473 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001474
Jamie Genniscb222e82012-10-23 15:44:26 -07001475 closeFolder();
1476 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001477
Jamie Genniscb222e82012-10-23 15:44:26 -07001478 // If we are already on home, then just animate back to the workspace,
1479 // otherwise, just wait until onResume to set the state back to Workspace
1480 if (alreadyOnHome) {
1481 showWorkspace(true);
1482 } else {
1483 mOnResumeState = State.WORKSPACE;
1484 }
1485
1486 final View v = getWindow().peekDecorView();
1487 if (v != null && v.getWindowToken() != null) {
1488 InputMethodManager imm = (InputMethodManager)getSystemService(
1489 INPUT_METHOD_SERVICE);
1490 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1491 }
1492
1493 // Reset AllApps to its initial state
1494 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1495 mAppsCustomizeTabHost.reset();
1496 }
1497 }
1498 };
1499
1500 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1501 // Delay processing of the intent to allow the status bar animation to finish
1502 // first in order to avoid janky animations.
1503 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001504 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001505 // Process the intent immediately.
1506 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001507 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
Michael Jurka447bf842013-05-15 14:52:15 +02001510 if (DEBUG_RESUME_TIME) {
1511 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
1514
1515 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001516 public void onRestoreInstanceState(Bundle state) {
1517 super.onRestoreInstanceState(state);
1518 for (int page: mSynchronouslyBoundPages) {
1519 mWorkspace.restoreInstanceStateForChild(page);
1520 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 @Override
1524 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001525 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001526 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
Michael Jurka883f55b2010-10-21 15:47:14 -07001528 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001529 // We close any open folder since it will not be re-opened, and we need to make sure
1530 // this state is reflected.
1531 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532
Winson Chung3d503fb2011-07-13 17:25:49 -07001533 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1534 mWaitingForResult) {
1535 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1536 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1537 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1538 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001539 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1540 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1541 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
1543
1544 if (mFolderInfo != null && mWaitingForResult) {
1545 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1546 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1547 }
Michael Jurka946ad472010-07-09 18:05:18 -07001548
Winson Chung785d2eb2011-04-14 16:08:02 -07001549 // Save the current AppsCustomize tab
1550 if (mAppsCustomizeTabHost != null) {
1551 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1552 if (currentTabTag != null) {
1553 outState.putString("apps_customize_currentTab", currentTabTag);
1554 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001555 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1556 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
1560 @Override
1561 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Winson Chunge7a03942011-08-05 15:05:12 -07001564 // Remove all pending runnables
1565 mHandler.removeMessages(ADVANCE_MSG);
1566 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001567 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001568
Winson Chungcd2b0142011-06-08 16:02:26 -07001569 // Stop callbacks from LauncherModel
1570 LauncherApplication app = ((LauncherApplication) getApplication());
1571 mModel.stopLoader();
1572 app.setLauncher(null);
1573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001575 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001577 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001579 mAppWidgetHost = null;
1580
1581 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
1583 TextKeyListener.getInstance().release();
1584
Winson Chung81b52252012-08-27 15:34:29 -07001585 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1586 // to prevent leaking Launcher activities on orientation change.
1587 if (mModel != null) {
1588 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1589 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001590
1591 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001592 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001593
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001594 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001595 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1596 mWorkspace.removeAllViews();
1597 mWorkspace = null;
1598 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001599
Michael Jurka2ecf9952012-06-18 12:52:28 -07001600 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602
Adam Cohena9cf38f2011-05-02 15:36:58 -07001603 public DragController getDragController() {
1604 return mDragController;
1605 }
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 @Override
1608 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001609 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 super.startActivityForResult(intent, requestCode);
1611 }
1612
Winson Chung70d72102011-08-12 11:24:35 -07001613 /**
1614 * Indicates that we want global search for this activity by setting the globalSearch
1615 * argument for {@link #startSearch} to true.
1616 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001618 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001620
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001621 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001622
Karl Rosaen138a0412009-04-23 19:00:21 -07001623 if (initialQuery == null) {
1624 // Use any text typed in the launcher as the initial query
1625 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 if (appSearchData == null) {
1628 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001629 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Winson Chungadf0c182012-08-23 14:59:07 -07001631 Rect sourceBounds = new Rect();
1632 if (mSearchDropTargetBar != null) {
1633 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1634 }
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Bjorn Bringertc459e522013-06-07 19:36:01 +01001636 startSearch(initialQuery, selectInitialQuery,
1637 appSearchData, sourceBounds);
1638 }
1639
1640 public void startSearch(String initialQuery,
1641 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001642 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001643 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001644 }
1645
1646 /**
1647 * Starts the global search activity. This code is a copied from SearchManager
1648 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001649 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001650 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001651 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001652 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1653 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1654 if (globalSearchActivity == null) {
1655 Log.w(TAG, "No global search activity found.");
1656 return;
1657 }
1658 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1659 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1660 intent.setComponent(globalSearchActivity);
1661 // Make sure that we have a Bundle to put source in
1662 if (appSearchData == null) {
1663 appSearchData = new Bundle();
1664 } else {
1665 appSearchData = new Bundle(appSearchData);
1666 }
1667 // Set source to package name of app that starts global search, if not set already.
1668 if (!appSearchData.containsKey("source")) {
1669 appSearchData.putString("source", getPackageName());
1670 }
1671 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1672 if (!TextUtils.isEmpty(initialQuery)) {
1673 intent.putExtra(SearchManager.QUERY, initialQuery);
1674 }
1675 if (selectInitialQuery) {
1676 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1677 }
1678 intent.setSourceBounds(sourceBounds);
1679 try {
1680 startActivity(intent);
1681 } catch (ActivityNotFoundException ex) {
1682 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685
Winson Chung70d72102011-08-12 11:24:35 -07001686 @Override
1687 public boolean onCreateOptionsMenu(Menu menu) {
1688 if (isWorkspaceLocked()) {
1689 return false;
1690 }
1691
1692 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001693
1694 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1695 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1696 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001697 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1698 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1699 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001700 String helpUrl = getString(R.string.help_url);
1701 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001702 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1703 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1704
Winson Chung70d72102011-08-12 11:24:35 -07001705 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1706 .setIcon(android.R.drawable.ic_menu_gallery)
1707 .setAlphabeticShortcut('W');
1708 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1709 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001710 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001711 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001712 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1713 .setIcon(android.R.drawable.ic_menu_preferences)
1714 .setIntent(settings)
1715 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001716 if (!helpUrl.isEmpty()) {
1717 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1718 .setIcon(android.R.drawable.ic_menu_help)
1719 .setIntent(help)
1720 .setAlphabeticShortcut('H');
1721 }
Winson Chung70d72102011-08-12 11:24:35 -07001722 return true;
1723 }
1724
1725 @Override
1726 public boolean onPrepareOptionsMenu(Menu menu) {
1727 super.onPrepareOptionsMenu(menu);
1728
1729 if (mAppsCustomizeTabHost.isTransitioning()) {
1730 return false;
1731 }
1732 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1733 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1734
1735 return true;
1736 }
1737
1738 @Override
1739 public boolean onOptionsItemSelected(MenuItem item) {
1740 switch (item.getItemId()) {
1741 case MENU_WALLPAPER_SETTINGS:
1742 startWallpaper();
1743 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001744 }
1745
1746 return super.onOptionsItemSelected(item);
1747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001749 @Override
1750 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001751 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001752 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001753 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001754 }
1755
Joe Onorato9c1289c2009-08-17 11:03:03 -04001756 public boolean isWorkspaceLocked() {
1757 return mWorkspaceLoading || mWaitingForResult;
1758 }
1759
Michael Jurka0280c3b2010-09-17 15:00:07 -07001760 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001761 mPendingAddInfo.container = ItemInfo.NO_ID;
1762 mPendingAddInfo.screen = -1;
1763 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1764 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001765 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001766 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001767 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001768
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001769 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1770 AppWidgetProviderInfo appWidgetInfo) {
1771 if (appWidgetInfo.configure != null) {
1772 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001773
Bjorn Bringert7984c942009-12-09 15:38:25 +00001774 // Launch over to configure widget, if needed
1775 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001776 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001777 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001778 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001780 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001781 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1782 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001783 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001784 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
1786 }
Romain Guycbb89e42009-06-08 15:52:54 -07001787
Adam Cohenfbba09b2011-07-18 21:43:05 -07001788 /**
1789 * Process a shortcut drop.
1790 *
1791 * @param componentName The name of the component
1792 * @param screen The screen where it should be added
1793 * @param cell The cell it should be added to, optional
1794 * @param position The location on the screen where it was dropped, optional
1795 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001796 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1797 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001798 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001799 mPendingAddInfo.container = container;
1800 mPendingAddInfo.screen = screen;
1801 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001802
1803 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001804 mPendingAddInfo.cellX = cell[0];
1805 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001806 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001807
1808 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1809 createShortcutIntent.setComponent(componentName);
1810 processShortcut(createShortcutIntent);
1811 }
1812
Adam Cohenfbba09b2011-07-18 21:43:05 -07001813 /**
1814 * Process a widget drop.
1815 *
1816 * @param info The PendingAppWidgetInfo of the widget being added.
1817 * @param screen The screen where it should be added
1818 * @param cell The cell it should be added to, optional
1819 * @param position The location on the screen where it was dropped, optional
1820 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001821 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001822 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001823 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001824 mPendingAddInfo.container = info.container = container;
1825 mPendingAddInfo.screen = info.screen = screen;
1826 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001827 mPendingAddInfo.minSpanX = info.minSpanX;
1828 mPendingAddInfo.minSpanY = info.minSpanY;
1829
Adam Cohenfbba09b2011-07-18 21:43:05 -07001830 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 mPendingAddInfo.cellX = cell[0];
1832 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001834 if (span != null) {
1835 mPendingAddInfo.spanX = span[0];
1836 mPendingAddInfo.spanY = span[1];
1837 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001838
Adam Cohened66b2b2012-01-23 17:28:51 -08001839 AppWidgetHostView hostView = info.boundWidget;
1840 int appWidgetId;
1841 if (hostView != null) {
1842 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001843 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001844 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001845 // In this case, we either need to start an activity to get permission to bind
1846 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001847 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001848 Bundle options = info.bindOptions;
1849
1850 boolean success = false;
1851 if (options != null) {
1852 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1853 info.componentName, options);
1854 } else {
1855 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1856 info.componentName);
1857 }
1858 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001859 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001860 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001861 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001862 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1863 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1864 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001865 // TODO: we need to make sure that this accounts for the options bundle.
1866 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001867 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1868 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001869 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001870 }
1871
Joe Onoratodeb98af2010-02-19 14:59:39 -08001872 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001873 // Handle case where user selected "Applications"
1874 String applicationName = getResources().getString(R.string.group_applications);
1875 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001876
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001877 if (applicationName != null && applicationName.equals(shortcutName)) {
1878 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1879 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001880
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001881 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1882 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001883 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001884 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001885 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001886 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
1889
Winson Chung24ab2f12010-09-16 14:10:47 -07001890 void processWallpaper(Intent intent) {
1891 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1892 }
1893
Winson Chung3d503fb2011-07-13 17:25:49 -07001894 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1895 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001896 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 folderInfo.title = getText(R.string.folder_name);
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001900 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1901 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001902 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
1904 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 FolderIcon newFolder =
1906 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1907 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1908 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001909 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
Romain Guycbb89e42009-06-08 15:52:54 -07001911
Joe Onorato9c1289c2009-08-17 11:03:03 -04001912 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001913 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001914 }
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001917 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001919 Intent chooser = Intent.createChooser(pickWallpaper,
1920 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001921 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1922 // Removed in Eclair MR1
1923// WallpaperManager wm = (WallpaperManager)
1924// getSystemService(Context.WALLPAPER_SERVICE);
1925// WallpaperInfo wi = wm.getWallpaperInfo();
1926// if (wi != null && wi.getSettingsActivity() != null) {
1927// LabeledIntent li = new LabeledIntent(getPackageName(),
1928// R.string.configure_wallpaper, 0);
1929// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1930// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1931// }
Mike Clerona0618e42009-10-22 13:55:21 -07001932 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001935 /**
1936 * Registers various content observers. The current implementation registers
1937 * only a favorites observer to keep track of the favorites applications.
1938 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001939 private void registerContentObservers() {
1940 ContentResolver resolver = getContentResolver();
1941 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1942 true, mWidgetObserver);
1943 }
1944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 @Override
1946 public boolean dispatchKeyEvent(KeyEvent event) {
1947 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1948 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001950 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001951 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001952 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001953 dumpState();
1954 return true;
1955 }
1956 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001957 }
1958 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1959 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001960 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 return true;
1962 }
1963 }
1964
1965 return super.dispatchKeyEvent(event);
1966 }
1967
Joe Onorato88ec0992009-11-19 13:16:06 -08001968 @Override
1969 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001970 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001971 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001972 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001973 Folder openFolder = mWorkspace.getOpenFolder();
1974 if (openFolder.isEditingName()) {
1975 openFolder.dismissEditingName();
1976 } else {
1977 closeFolder();
1978 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001979 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001980 mWorkspace.exitWidgetResizeMode();
1981
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001982 // Back button is a no-op here, but give at least some feedback for the button press
1983 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001984 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001988 * Re-listen when widgets are reset.
1989 */
1990 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001991 if (mAppWidgetHost != null) {
1992 mAppWidgetHost.startListening();
1993 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001994 }
1995
1996 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 * Launches the intent referred by the clicked shortcut.
1998 *
1999 * @param v The view representing the clicked shortcut.
2000 */
2001 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002002 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2003 // view has detached (it's possible for this to happen if the view is removed mid touch).
2004 if (v.getWindowToken() == null) {
2005 return;
2006 }
2007
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002008 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002009 return;
2010 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002013 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002015 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002016
2017 ComponentName widgetComp = new ComponentName(this, WidgetAdder.class);
2018 if (intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
2019 showAllApps(true);
2020 return;
2021 }
Joe Onorato13724ea2009-12-02 21:16:35 -08002022 int[] pos = new int[2];
2023 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002024 intent.setSourceBounds(new Rect(pos[0], pos[1],
2025 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002026
2027 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002028
2029 if (success && v instanceof BubbleTextView) {
2030 mWaitingForResume = (BubbleTextView) v;
2031 mWaitingForResume.setStayPressed(true);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002034 if (v instanceof FolderIcon) {
2035 FolderIcon fi = (FolderIcon) v;
2036 handleFolderClick(fi);
2037 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002038 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002039 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002040 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002041 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002042 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
2045 }
2046
Michael Jurka0e260592010-06-30 17:07:39 -07002047 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002048 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002049 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002050 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2051 showWorkspace(true);
2052 }
Michael Jurka0e260592010-06-30 17:07:39 -07002053 return false;
2054 }
2055
Michael Jurkaaf442092010-06-10 17:01:57 -07002056 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002057 * Event handler for the search button
2058 *
2059 * @param v The view that was clicked.
2060 */
2061 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002062 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2063
Amith Yamasania135ba82011-08-09 17:42:01 -07002064 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002065 }
2066
2067 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002068 * Event handler for the voice button
2069 *
2070 * @param v The view that was clicked.
2071 */
2072 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002073 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002074
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 startVoice();
2076 }
2077
2078 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002079 try {
2080 final SearchManager searchManager =
2081 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2082 ComponentName activityName = searchManager.getGlobalSearchActivity();
2083 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002084 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002085 if (activityName != null) {
2086 intent.setPackage(activityName.getPackageName());
2087 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002088 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002089 } catch (ActivityNotFoundException e) {
2090 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002091 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002092 startActivitySafely(null, intent, "onClickVoiceButton");
2093 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002094 }
2095
2096 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002097 * Event handler for the "grid" button that appears on the home screen, which
2098 * enters all apps mode.
2099 *
2100 * @param v The view that was clicked.
2101 */
2102 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002103 showAllApps(true);
2104 }
2105
2106 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002107 // Provide the same haptic feedback that the system offers for virtual keys.
2108 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002109 }
2110
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002111 public void onClickAppMarketButton(View v) {
2112 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002113 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002114 } else {
2115 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002116 }
2117 }
2118
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002119 void startApplicationDetailsActivity(ComponentName componentName) {
2120 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002121 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2122 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002123 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002124 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002125 }
2126
Patrick Dubroy5539af72010-09-07 15:22:01 -07002127 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2128 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2129 // System applications cannot be installed. For now, show a toast explaining that.
2130 // We may give them the option of disabling apps this way.
2131 int messageId = R.string.uninstall_system_app_text;
2132 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2133 } else {
2134 String packageName = appInfo.componentName.getPackageName();
2135 String className = appInfo.componentName.getClassName();
2136 Intent intent = new Intent(
2137 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002138 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2139 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002140 startActivity(intent);
2141 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002142 }
2143
Michael Jurka86a720e2012-05-09 11:23:23 -07002144 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002148 // Only launch using the new animation if the shortcut has not opted out (this is a
2149 // private contract between launcher and may be ignored in the future).
2150 boolean useLaunchAnimation = (v != null) &&
2151 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2152 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002153 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2154 v.getMeasuredWidth(), v.getMeasuredHeight());
2155
2156 startActivity(intent, opts.toBundle());
2157 } else {
2158 startActivity(intent);
2159 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002160 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 } catch (SecurityException e) {
2162 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002163 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002164 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002165 "or use the exported attribute for this activity. "
2166 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002168 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002170
Michael Jurka86a720e2012-05-09 11:23:23 -07002171 boolean startActivitySafely(View v, Intent intent, Object tag) {
2172 boolean success = false;
2173 try {
2174 success = startActivity(v, intent, tag);
2175 } catch (ActivityNotFoundException e) {
2176 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2177 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2178 }
2179 return success;
2180 }
2181
Romain Guy8e633c52010-03-04 12:51:36 -08002182 void startActivityForResultSafely(Intent intent, int requestCode) {
2183 try {
2184 startActivityForResult(intent, requestCode);
2185 } catch (ActivityNotFoundException e) {
2186 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2187 } catch (SecurityException e) {
2188 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2189 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2190 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2191 "or use the exported attribute for this activity.", e);
2192 }
2193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194
Adam Cohena9cf38f2011-05-02 15:36:58 -07002195 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002196 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002197 Folder openFolder = mWorkspace.getFolderForTag(info);
2198
2199 // If the folder info reports that the associated folder is open, then verify that
2200 // it is actually opened. There have been a few instances where this gets out of sync.
2201 if (info.opened && openFolder == null) {
2202 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2203 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2204 info.opened = false;
2205 }
2206
Adam Cohenfb91f302012-06-11 15:45:18 -07002207 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 // Close any open folder
2209 closeFolder();
2210 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002211 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 } else {
2213 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 int folderScreen;
2215 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002216 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 // .. and close it
2218 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002219 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 // Close any folder open on the current screen
2221 closeFolder();
2222 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002223 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 }
2225 }
2226 }
2227 }
2228
Adam Cohen268c4752012-06-06 17:47:33 -07002229 /**
2230 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2231 * in the DragLayer in the exact absolute location of the original FolderIcon.
2232 */
2233 private void copyFolderIconToImage(FolderIcon fi) {
2234 final int width = fi.getMeasuredWidth();
2235 final int height = fi.getMeasuredHeight();
2236
2237 // Lazy load ImageView, Bitmap and Canvas
2238 if (mFolderIconImageView == null) {
2239 mFolderIconImageView = new ImageView(this);
2240 }
2241 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2242 mFolderIconBitmap.getHeight() != height) {
2243 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2244 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2245 }
2246
2247 DragLayer.LayoutParams lp;
2248 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2249 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2250 } else {
2251 lp = new DragLayer.LayoutParams(width, height);
2252 }
2253
Adam Cohen307fe232012-08-16 17:55:58 -07002254 // The layout from which the folder is being opened may be scaled, adjust the starting
2255 // view size by this scale factor.
2256 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002257 lp.customPosition = true;
2258 lp.x = mRectForFolderAnimation.left;
2259 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002260 lp.width = (int) (scale * width);
2261 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002262
2263 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2264 fi.draw(mFolderIconCanvas);
2265 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002266 if (fi.getFolder() != null) {
2267 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2268 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002269 }
Adam Cohen268c4752012-06-06 17:47:33 -07002270 // Just in case this image view is still in the drag layer from a previous animation,
2271 // we remove it and re-add it.
2272 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2273 mDragLayer.removeView(mFolderIconImageView);
2274 }
2275 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002276 if (fi.getFolder() != null) {
2277 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002278 }
Adam Cohen268c4752012-06-06 17:47:33 -07002279 }
2280
Adam Cohen2801caf2011-05-13 20:57:39 -07002281 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002282 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002283 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2284 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2285 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2286
Adam Cohenc51934b2011-07-26 21:07:43 -07002287 FolderInfo info = (FolderInfo) fi.getTag();
2288 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2289 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002290 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2291 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002292 }
2293
Adam Cohen268c4752012-06-06 17:47:33 -07002294 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2295 copyFolderIconToImage(fi);
2296 fi.setVisibility(View.INVISIBLE);
2297
Michael Jurka2ecf9952012-06-18 12:52:28 -07002298 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002299 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002300 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2301 oa.start();
2302 }
2303
Adam Cohen268c4752012-06-06 17:47:33 -07002304 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002305 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002306 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2307 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2308 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2309
Adam Cohen268c4752012-06-06 17:47:33 -07002310 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002311
Adam Cohen268c4752012-06-06 17:47:33 -07002312 // We remove and re-draw the FolderIcon in-case it has changed
2313 mDragLayer.removeView(mFolderIconImageView);
2314 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002315 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002316 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002317 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002318 oa.addListener(new AnimatorListenerAdapter() {
2319 @Override
2320 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002321 if (cl != null) {
2322 cl.clearFolderLeaveBehind();
2323 // Remove the ImageView copy of the FolderIcon and make the original visible.
2324 mDragLayer.removeView(mFolderIconImageView);
2325 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002326 }
2327 }
2328 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002329 oa.start();
2330 }
2331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002333 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 * is animated relative to the specified View. If the View is null, no animation
2335 * is played.
2336 *
2337 * @param folderInfo The FolderInfo describing the folder to open.
2338 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002339 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002340 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002341 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342
Adam Cohena9cf38f2011-05-02 15:36:58 -07002343 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002344
Adam Cohen4554ee12011-08-03 16:13:21 -07002345 // Just verify that the folder hasn't already been added to the DragLayer.
2346 // There was a one-off crash where the folder had a parent already.
2347 if (folder.getParent() == null) {
2348 mDragLayer.addView(folder);
2349 mDragController.addDropTarget((DropTarget) folder);
2350 } else {
2351 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2352 folder.getParent() + ").");
2353 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002354 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002355 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002356
2357 // Notify the accessibility manager that this folder "window" has appeared and occluded
2358 // the workspace items
2359 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2360 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002361 }
2362
2363 public void closeFolder() {
2364 Folder folder = mWorkspace.getOpenFolder();
2365 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002366 if (folder.isEditingName()) {
2367 folder.dismissEditingName();
2368 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002369 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002370
2371 // Dismiss the folder cling
2372 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002373 }
2374 }
2375
2376 void closeFolder(Folder folder) {
2377 folder.getInfo().opened = false;
2378
2379 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2380 if (parent != null) {
2381 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2382 shrinkAndFadeInFolderIcon(fi);
2383 }
2384 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002385
2386 // Notify the accessibility manager that this folder "window" has disappeard and no
2387 // longer occludeds the workspace items
2388 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 }
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002392 if (!isDraggingEnabled()) return false;
2393 if (isWorkspaceLocked()) return false;
2394 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395
2396 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002397 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 }
2399
Michael Jurka0280c3b2010-09-17 15:00:07 -07002400 resetAddInfo();
2401 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002403 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 return true;
2405 }
2406
Winson Chung3d503fb2011-07-13 17:25:49 -07002407 // The hotseat touch handling does not go through Workspace, and we always allow long press
2408 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002409 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2411 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002412 if (itemUnderLongClick == null) {
2413 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002414 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2415 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002416 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002420 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 }
2422 }
2423 }
2424 return true;
2425 }
2426
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 boolean isHotseatLayout(View layout) {
2428 return mHotseat != null && layout != null &&
2429 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2430 }
2431 Hotseat getHotseat() {
2432 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002433 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002434 SearchDropTargetBar getSearchBar() {
2435 return mSearchDropTargetBar;
2436 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002437
Winson Chung3d503fb2011-07-13 17:25:49 -07002438 /**
2439 * Returns the CellLayout of the specified container at the specified screen.
2440 */
2441 CellLayout getCellLayout(long container, int screen) {
2442 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2443 if (mHotseat != null) {
2444 return mHotseat.getLayout();
2445 } else {
2446 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002447 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 } else {
2449 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002450 }
2451 }
2452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 Workspace getWorkspace() {
2454 return mWorkspace;
2455 }
2456
Daniel Sandler843e8602010-06-07 14:59:01 -04002457 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002458 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002459 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002460 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002461 }
2462
Craig Mautner360310b2012-10-26 15:13:08 -07002463 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002464 public boolean isAllAppsButtonRank(int rank) {
2465 return mHotseat.isAllAppsButtonRank(rank);
2466 }
2467
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468 /**
2469 * Helper method for the cameraZoomIn/cameraZoomOut animations
2470 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002471 * @param scaleFactor The scale factor used for the zoom
2472 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002473 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002474 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002475 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002476 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002477
Winson Chung18f41f82012-05-09 13:28:10 -07002478 void disableWallpaperIfInAllApps() {
2479 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002480 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002481 if (mAppsCustomizeTabHost != null &&
2482 !mAppsCustomizeTabHost.isTransitioning()) {
2483 updateWallpaperVisibility(false);
2484 }
2485 }
2486 }
2487
Craig Mautner360310b2012-10-26 15:13:08 -07002488 private void setWorkspaceBackground(boolean workspace) {
2489 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002490 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002491 }
2492
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002493 void updateWallpaperVisibility(boolean visible) {
2494 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2495 int curflags = getWindow().getAttributes().flags
2496 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2497 if (wpflags != curflags) {
2498 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2499 }
Craig Mautner360310b2012-10-26 15:13:08 -07002500 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002501 }
2502
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002503 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2504 if (v instanceof LauncherTransitionable) {
2505 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2506 }
2507 }
2508
Michael Jurkabed61d22012-02-14 22:51:29 -08002509 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2510 if (v instanceof LauncherTransitionable) {
2511 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2512 }
Winson Chung70442722012-02-10 15:43:22 -08002513
2514 // Update the workspace transition step as well
2515 dispatchOnLauncherTransitionStep(v, 0f);
2516 }
2517
2518 private void dispatchOnLauncherTransitionStep(View v, float t) {
2519 if (v instanceof LauncherTransitionable) {
2520 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2521 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002522 }
2523
2524 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2525 if (v instanceof LauncherTransitionable) {
2526 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2527 }
Winson Chung70442722012-02-10 15:43:22 -08002528
2529 // Update the workspace transition step as well
2530 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002531 }
2532
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002533 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002534 * Things to test when changing the following seven functions.
2535 * - Home from workspace
2536 * - from center screen
2537 * - from other screens
2538 * - Home from all apps
2539 * - from center screen
2540 * - from other screens
2541 * - Back from all apps
2542 * - from center screen
2543 * - from other screens
2544 * - Launch app from workspace and quit
2545 * - with back
2546 * - with home
2547 * - Launch app from all apps and quit
2548 * - with back
2549 * - with home
2550 * - Go to a screen that's not the default, then all
2551 * apps, and launch and app, and go back
2552 * - with back
2553 * -with home
2554 * - On workspace, long press power and go back
2555 * - with back
2556 * - with home
2557 * - On all apps, long press power and go back
2558 * - with back
2559 * - with home
2560 * - On workspace, power off
2561 * - On all apps, power off
2562 * - Launch an app and turn off the screen while in that app
2563 * - Go back with home key
2564 * - Go back with back key TODO: make this not go to workspace
2565 * - From all apps
2566 * - From workspace
2567 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2568 * - From all apps
2569 * - From the center workspace
2570 * - From another workspace
2571 */
2572
2573 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002574 * Zoom the camera out from the workspace to reveal 'toView'.
2575 * Assumes that the view to show is anchored at either the very top or very bottom
2576 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002577 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002578 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002579 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002580 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 mStateAnimation.cancel();
2582 mStateAnimation = null;
2583 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002584 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002585
Winson Chungf0ea4d32011-06-06 14:27:16 -07002586 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2587 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2588 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002589 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002590 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002591 final int startDelay =
2592 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002593
Michael Jurkab3e22d92011-10-31 15:58:33 -07002594 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595
Michael Jurkad74c9842011-07-10 12:44:21 -07002596 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002597 Animator workspaceAnim =
2598 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599
2600 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002601 toView.setScaleX(scale);
2602 toView.setScaleY(scale);
2603 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2604 scaleAnim.
2605 scaleX(1f).scaleY(1f).
2606 setDuration(duration).
2607 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002608
Winson Chungf0ea4d32011-06-06 14:27:16 -07002609 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002610 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002611 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002612 .ofFloat(toView, "alpha", 0f, 1f)
2613 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002614 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002615 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2616 @Override
2617 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002618 if (animation == null) {
2619 throw new RuntimeException("animation is null");
2620 }
Winson Chung70442722012-02-10 15:43:22 -08002621 float t = (Float) animation.getAnimatedValue();
2622 dispatchOnLauncherTransitionStep(fromView, t);
2623 dispatchOnLauncherTransitionStep(toView, t);
2624 }
2625 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002626
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002627 // toView should appear right at the end of the workspace shrink
2628 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002629 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002630 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002631 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002632
2633 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002634 boolean animationCancelled = false;
2635
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002636 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002637 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002638 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639 // Prepare the position
2640 toView.setTranslationX(0.0f);
2641 toView.setTranslationY(0.0f);
2642 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002643 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002645 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002646 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002647 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2648 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002649
Michael Jurkafa7969f2012-09-21 16:39:14 -07002650 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002651 // Hide the workspace scrollbar
2652 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002653 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002654 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002655 if (!animationCancelled) {
2656 updateWallpaperVisibility(false);
2657 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002658
2659 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002660 if (mSearchDropTargetBar != null) {
2661 mSearchDropTargetBar.hideSearchBar(false);
2662 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002663 }
2664
Adam Cohencff6af82011-09-13 14:51:53 -07002665 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002666 public void onAnimationCancel(Animator animation) {
2667 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002668 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002669 });
2670
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002671 if (workspaceAnim != null) {
2672 mStateAnimation.play(workspaceAnim);
2673 }
Michael Jurka1899a362011-11-03 13:50:45 -07002674
2675 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002676
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002677 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2678 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002679
2680 // If any of the objects being animated haven't been measured/laid out
2681 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002682 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002683 (mWorkspace.getMeasuredWidth() == 0) ||
2684 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002685 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002686 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002687
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002688 final AnimatorSet stateAnimation = mStateAnimation;
2689 final Runnable startAnimRunnable = new Runnable() {
2690 public void run() {
2691 // Check that mStateAnimation hasn't changed while
2692 // we waited for a layout/draw pass
2693 if (mStateAnimation != stateAnimation)
2694 return;
2695 setPivotsForZoom(toView, scale);
2696 dispatchOnLauncherTransitionStart(fromView, animated, false);
2697 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002698 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002699 }
2700 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002701 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002702 final ViewTreeObserver observer = toView.getViewTreeObserver();
2703 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2704 public void onGlobalLayout() {
2705 startAnimRunnable.run();
2706 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2707 }
2708 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002710 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 } else {
2713 toView.setTranslationX(0.0f);
2714 toView.setTranslationY(0.0f);
2715 toView.setScaleX(1.0f);
2716 toView.setScaleY(1.0f);
2717 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002718 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002719
Michael Jurkabed61d22012-02-14 22:51:29 -08002720 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2721 // Hide the workspace scrollbar
2722 mWorkspace.hideScrollingIndicator(true);
2723 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002724
2725 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002726 if (mSearchDropTargetBar != null) {
2727 mSearchDropTargetBar.hideSearchBar(false);
2728 }
Michael Jurkaabded662011-03-04 12:06:57 -08002729 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002730 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002731 dispatchOnLauncherTransitionStart(fromView, animated, false);
2732 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002733 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002734 dispatchOnLauncherTransitionStart(toView, animated, false);
2735 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002736 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002738 }
2739
2740 /**
2741 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002742 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002743 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002744 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002745 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2746 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002747
Michael Jurkab3e22d92011-10-31 15:58:33 -07002748 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002749 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750 mStateAnimation.cancel();
2751 mStateAnimation = null;
2752 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002753 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002754
Winson Chungf0ea4d32011-06-06 14:27:16 -07002755 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002756 final int fadeOutDuration =
2757 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002758 final float scaleFactor = (float)
2759 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2760 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002761 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002762 Animator workspaceAnim = null;
2763
2764 if (toState == State.WORKSPACE) {
2765 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2766 workspaceAnim = mWorkspace.getChangeStateAnimation(
2767 Workspace.State.NORMAL, animated, stagger);
2768 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2769 workspaceAnim = mWorkspace.getChangeStateAnimation(
2770 Workspace.State.SPRING_LOADED, animated);
2771 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002772
Michael Jurkab3e22d92011-10-31 15:58:33 -07002773 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002774 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002775 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002776 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002777 final LauncherViewPropertyAnimator scaleAnim =
2778 new LauncherViewPropertyAnimator(fromView);
2779 scaleAnim.
2780 scaleX(scaleFactor).scaleY(scaleFactor).
2781 setDuration(duration).
2782 setInterpolator(new Workspace.ZoomInInterpolator());
2783
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002784 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002785 .ofFloat(fromView, "alpha", 1f, 0f)
2786 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002787 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002788 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2789 @Override
2790 public void onAnimationUpdate(ValueAnimator animation) {
2791 float t = 1f - (Float) animation.getAnimatedValue();
2792 dispatchOnLauncherTransitionStep(fromView, t);
2793 dispatchOnLauncherTransitionStep(toView, t);
2794 }
2795 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002796
Michael Jurka2ecf9952012-06-18 12:52:28 -07002797 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002798
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002799 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2800 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002801 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002802
2803 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002804 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002805 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002806 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002807 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002808 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2809 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002810 if (mWorkspace != null) {
2811 mWorkspace.hideScrollingIndicator(false);
2812 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002813 if (onCompleteRunnable != null) {
2814 onCompleteRunnable.run();
2815 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002816 mAppsCustomizeContent.updateCurrentPageScroll();
2817 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 }
2819 });
2820
Winson Chungf0ea4d32011-06-06 14:27:16 -07002821 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002822 if (workspaceAnim != null) {
2823 mStateAnimation.play(workspaceAnim);
2824 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002825 dispatchOnLauncherTransitionStart(fromView, animated, true);
2826 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002827 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828 } else {
2829 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002830 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002831 dispatchOnLauncherTransitionStart(fromView, animated, true);
2832 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002833 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002834 dispatchOnLauncherTransitionStart(toView, animated, true);
2835 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002836 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 }
2839
Michael Jurkae326f182011-11-21 14:05:46 -08002840 @Override
2841 public void onTrimMemory(int level) {
2842 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002843 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002844 mAppsCustomizeTabHost.onTrimMemory();
2845 }
2846 }
2847
Winson Chung18f41f82012-05-09 13:28:10 -07002848 @Override
2849 public void onWindowFocusChanged(boolean hasFocus) {
2850 if (!hasFocus) {
2851 // When another window occludes launcher (like the notification shade, or recents),
2852 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2853 updateWallpaperVisibility(true);
2854 } else {
2855 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002856 mWorkspace.postDelayed(new Runnable() {
2857 @Override
2858 public void run() {
2859 disableWallpaperIfInAllApps();
2860 }
2861 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002862 }
2863 }
2864
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002865 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002866 showWorkspace(animated, null);
2867 }
2868
2869 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002870 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002871 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002872 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002873 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002874
Winson Chungc7d2b602012-05-16 17:02:20 -07002875 // Show the search bar (only animate if we were showing the drop target bar in spring
2876 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002877 if (mSearchDropTargetBar != null) {
2878 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2879 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002880
Michael Jurkab737ee62011-11-15 15:57:22 -08002881 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002882 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002883
2884 // Set focus to the AppsCustomize button
2885 if (mAllAppsButton != null) {
2886 mAllAppsButton.requestFocus();
2887 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002888 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002889
Michael Jurkab737ee62011-11-15 15:57:22 -08002890 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002891
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 // Change the state *after* we've called all the transition code
2893 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002894
Winson Chung5fb63472011-02-02 17:03:37 -08002895 // Resume the auto-advance of widgets
2896 mUserPresent = true;
2897 updateRunning();
2898
alanv1d4fde62012-10-17 13:15:47 -07002899 // Send an accessibility event to announce the context change
2900 getWindow().getDecorView()
2901 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002902
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002903 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002904 }
2905
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002906 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002907 }
2908
Michael Jurkab3e22d92011-10-31 15:58:33 -07002909 void showAllApps(boolean animated) {
2910 if (mState != State.WORKSPACE) return;
2911
2912 showAppsCustomizeHelper(animated, false);
2913 mAppsCustomizeTabHost.requestFocus();
2914
Michael Jurkab3e22d92011-10-31 15:58:33 -07002915 // Change the state *after* we've called all the transition code
2916 mState = State.APPS_CUSTOMIZE;
2917
2918 // Pause the auto-advance of widgets until we are out of AllApps
2919 mUserPresent = false;
2920 updateRunning();
2921 closeFolder();
2922
2923 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002924 getWindow().getDecorView()
2925 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002926 }
2927
Adam Cohen7777d962011-08-18 18:58:38 -07002928 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002929 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002930 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002931 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002932 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002933 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002934 }
Adam Cohen7777d962011-08-18 18:58:38 -07002935
Adam Cohened66b2b2012-01-23 17:28:51 -08002936 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2937 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002938 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2939
Winson Chunge7a03942011-08-05 15:05:12 -07002940 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002941 @Override
2942 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002943 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002944 // Before we show workspace, hide all apps again because
2945 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2946 // clean up our state transition functions
2947 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002948 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002949 } else {
2950 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002951 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002952 }
2953 }, (extendedDelay ?
2954 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2955 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2956 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002957
Michael Jurkad3ef3062010-11-23 16:23:58 -08002958 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002959 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002960 final boolean animated = true;
2961 final boolean springLoaded = true;
2962 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002963 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002964 }
2965 // Otherwise, we are not in spring loaded mode, so don't do anything.
2966 }
2967
Michael Jurkab737ee62011-11-15 15:57:22 -08002968 void hideDockDivider() {
2969 if (mQsbDivider != null && mDockDivider != null) {
2970 mQsbDivider.setVisibility(View.INVISIBLE);
2971 mDockDivider.setVisibility(View.INVISIBLE);
2972 }
2973 }
2974
2975 void showDockDivider(boolean animated) {
2976 if (mQsbDivider != null && mDockDivider != null) {
2977 mQsbDivider.setVisibility(View.VISIBLE);
2978 mDockDivider.setVisibility(View.VISIBLE);
2979 if (mDividerAnimator != null) {
2980 mDividerAnimator.cancel();
2981 mQsbDivider.setAlpha(1f);
2982 mDockDivider.setAlpha(1f);
2983 mDividerAnimator = null;
2984 }
2985 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002986 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2987 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2988 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002989 int duration = 0;
2990 if (mSearchDropTargetBar != null) {
2991 duration = mSearchDropTargetBar.getTransitionInDuration();
2992 }
2993 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002994 mDividerAnimator.start();
2995 }
2996 }
2997 }
2998
Michael Jurkab3e22d92011-10-31 15:58:33 -07002999 void lockAllApps() {
3000 // TODO
3001 }
3002
3003 void unlockAllApps() {
3004 // TODO
3005 }
3006
Winson Chungf0ea4d32011-06-06 14:27:16 -07003007 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003008 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003009 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003010 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003011 if (!LauncherApplication.isScreenLarge()) {
3012 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003013 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003014 int duration = 0;
3015 if (mSearchDropTargetBar != null) {
3016 duration = mSearchDropTargetBar.getTransitionInDuration();
3017 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003018 mHotseat.animate().alpha(1f).setDuration(duration);
3019 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003020 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003021 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003022 }
3023 }
3024 }
3025
3026 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003027 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003030 if (!LauncherApplication.isScreenLarge()) {
3031 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003032 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003033 int duration = 0;
3034 if (mSearchDropTargetBar != null) {
3035 duration = mSearchDropTargetBar.getTransitionOutDuration();
3036 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003037 mHotseat.animate().alpha(0f).setDuration(duration);
3038 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003040 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003042 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003043 }
3044
Patrick Dubroy5f445422011-02-18 14:35:21 -08003045 /**
3046 * Add an item from all apps or customize onto the given workspace screen.
3047 * If layout is null, add to the current screen.
3048 */
3049 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003050 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003051 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003052 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003053 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003054
Winson Chungdff8ebb2011-09-08 17:25:31 -07003055 /** Maps the current orientation to an index for referencing orientation correct global icons */
3056 private int getCurrentOrientationIndexForGlobalIcons() {
3057 // default - 0, landscape - 1
3058 switch (getResources().getConfiguration().orientation) {
3059 case Configuration.ORIENTATION_LANDSCAPE:
3060 return 1;
3061 default:
3062 return 0;
3063 }
3064 }
3065
Michael Jurkaae65ee32012-04-30 11:45:54 -07003066 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003067 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003068 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003069 // Look for the toolbar icon specified in the activity meta-data
3070 Bundle metaData = packageManager.getActivityInfo(
3071 activityName, PackageManager.GET_META_DATA).metaData;
3072 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003073 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003074 if (iconResId != 0) {
3075 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003076 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 }
3078 }
3079 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003080 // This can happen if the activity defines an invalid drawable
3081 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3082 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003083 } catch (Resources.NotFoundException nfe) {
3084 // This can happen if the activity defines an invalid drawable
3085 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3086 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003087 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003088 return null;
3089 }
3090
3091 // if successful in getting icon, return it; otherwise, set button to use default drawable
3092 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003093 int buttonId, ComponentName activityName, int fallbackDrawableId,
3094 String toolbarResourceName) {
3095 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003096 Resources r = getResources();
3097 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3098 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003099
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003100 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003101 // If we were unable to find the icon via the meta-data, use a generic one
3102 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003103 toolbarIcon = r.getDrawable(fallbackDrawableId);
3104 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003105 if (button != null) {
3106 button.setCompoundDrawables(toolbarIcon, null, null, null);
3107 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003108 return null;
3109 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003110 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003111 if (button != null) {
3112 button.setCompoundDrawables(toolbarIcon, null, null, null);
3113 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003114 return toolbarIcon.getConstantState();
3115 }
3116 }
3117
3118 // if successful in getting icon, return it; otherwise, set button to use default drawable
3119 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003120 int buttonId, ComponentName activityName, int fallbackDrawableId,
3121 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003122 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003123 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003124
Michael Jurka19e0fc52011-07-22 18:00:21 -07003125 if (button != null) {
3126 // If we were unable to find the icon via the meta-data, use a
3127 // generic one
3128 if (toolbarIcon == null) {
3129 button.setImageResource(fallbackDrawableId);
3130 } else {
3131 button.setImageDrawable(toolbarIcon);
3132 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003133 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003134
3135 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3136
Michael Jurka0423dcf2010-10-05 14:56:18 -07003137 }
3138
Winson Chung1b884092012-06-08 17:13:02 -07003139 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003140 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003141 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003142 }
3143
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003144 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003145 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003146 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003147 }
3148
Winson Chungbb185bd2011-11-21 12:31:42 -08003149 private void invalidatePressedFocusedStates(View container, View button) {
3150 if (container instanceof HolographicLinearLayout) {
3151 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3152 layout.invalidatePressedFocusedStates();
3153 } else if (button instanceof HolographicImageView) {
3154 HolographicImageView view = (HolographicImageView) button;
3155 view.invalidatePressedFocusedStates();
3156 }
3157 }
3158
Winson Chungc51db6a2011-10-05 11:44:49 -07003159 private boolean updateGlobalSearchIcon() {
3160 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003161 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003162 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003163 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003164
Winson Chung1cad91e2011-05-25 17:41:01 -07003165 final SearchManager searchManager =
3166 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3167 ComponentName activityName = searchManager.getGlobalSearchActivity();
3168 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003169 int coi = getCurrentOrientationIndexForGlobalIcons();
3170 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003171 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3172 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3173 if (sGlobalSearchIcon[coi] == null) {
3174 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3175 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3176 TOOLBAR_ICON_METADATA_NAME);
3177 }
3178
Winson Chungc51db6a2011-10-05 11:44:49 -07003179 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3180 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003181 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003182 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003183 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003184 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003185 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3186 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3187 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003188 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003189 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003190 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003191 }
3192 }
3193
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003194 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003195 final View searchButtonContainer = findViewById(R.id.search_button_container);
3196 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003197 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003198 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003199 }
3200
Winson Chungc51db6a2011-10-05 11:44:49 -07003201 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003202 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003203 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003204
Winson Chungc51db6a2011-10-05 11:44:49 -07003205 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003206 final SearchManager searchManager =
3207 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3208 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3209
3210 ComponentName activityName = null;
3211 if (globalSearchActivity != null) {
3212 // Check if the global search activity handles voice search
3213 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3214 intent.setPackage(globalSearchActivity.getPackageName());
3215 activityName = intent.resolveActivity(getPackageManager());
3216 }
3217
3218 if (activityName == null) {
3219 // Fallback: check if an activity other than the global search activity
3220 // resolves this
3221 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3222 activityName = intent.resolveActivity(getPackageManager());
3223 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003224 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003225 int coi = getCurrentOrientationIndexForGlobalIcons();
3226 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003227 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3228 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3229 if (sVoiceSearchIcon[coi] == null) {
3230 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3231 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3232 TOOLBAR_ICON_METADATA_NAME);
3233 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003234 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003235 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003236 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003237 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003238 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003239 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003240 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003241 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003242 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003243 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003244 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003245 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003246
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003247 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003248 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3249 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003250 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003251 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003252 }
3253
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003254 public void setVoiceButtonProxyVisible(boolean visible) {
3255 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3256 if (voiceButtonProxy != null) {
3257 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3258 }
3259 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003260 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003261 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003262 */
3263 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003264 final View marketButton = findViewById(R.id.market_button);
3265 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3266 // Find the app market activity by resolving an intent.
3267 // (If multiple app markets are installed, it will return the ResolverActivity.)
3268 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003269 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003270 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003271 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003272 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003273 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3274 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003275 marketButton.setVisibility(View.VISIBLE);
3276 } else {
3277 // We should hide and disable the view so that we don't try and restore the visibility
3278 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3279 marketButton.setVisibility(View.GONE);
3280 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003281 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003282 }
3283
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003284 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003285 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3286 Resources r = getResources();
3287 Drawable marketIconDrawable = d.newDrawable(r);
3288 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3289 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3290 marketIconDrawable.setBounds(0, 0, w, h);
3291
3292 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003293 }
3294
Michael Jurkad7c28052012-04-27 15:43:36 -07003295 @Override
3296 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003297 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003298 final List<CharSequence> text = event.getText();
3299 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003300 // Populate event with a fake title based on the current state.
3301 if (mState == State.APPS_CUSTOMIZE) {
3302 text.add(getString(R.string.all_apps_button_label));
3303 } else {
3304 text.add(getString(R.string.all_apps_home_button_label));
3305 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003306 return result;
3307 }
3308
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003309 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003310 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003311 */
3312 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3313 @Override
3314 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003315 closeSystemDialogs();
3316 }
3317 }
3318
3319 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003320 * Receives notifications whenever the appwidgets are reset.
3321 */
3322 private class AppWidgetResetObserver extends ContentObserver {
3323 public AppWidgetResetObserver() {
3324 super(new Handler());
3325 }
3326
3327 @Override
3328 public void onChange(boolean selfChange) {
3329 onAppWidgetReset();
3330 }
3331 }
3332
3333 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003334 * If the activity is currently paused, signal that we need to run the passed Runnable
3335 * in onResume.
3336 *
3337 * This needs to be called from incoming places where resources might have been loaded
3338 * while we are paused. That is becaues the Configuration might be wrong
3339 * when we're not running, and if it comes back to what it was when we
3340 * were paused, we are not restarted.
3341 *
3342 * Implementation of the method from LauncherModel.Callbacks.
3343 *
3344 * @return true if we are currently paused. The caller might be able to
3345 * skip some work in that case since we will come back again.
3346 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003347 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003348 if (mPaused) {
3349 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003350 if (deletePreviousRunnables) {
3351 while (mOnResumeCallbacks.remove(run)) {
3352 }
3353 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003354 mOnResumeCallbacks.add(run);
3355 return true;
3356 } else {
3357 return false;
3358 }
3359 }
3360
Michael Jurkac402cd92013-05-20 15:49:32 +02003361 private boolean waitUntilResume(Runnable run) {
3362 return waitUntilResume(run, false);
3363 }
3364
Michael Jurka7607c2f2013-04-03 14:33:19 -07003365 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003366 * If the activity is currently paused, signal that we need to re-run the loader
3367 * in onResume.
3368 *
3369 * This needs to be called from incoming places where resources might have been loaded
3370 * while we are paused. That is becaues the Configuration might be wrong
3371 * when we're not running, and if it comes back to what it was when we
3372 * were paused, we are not restarted.
3373 *
3374 * Implementation of the method from LauncherModel.Callbacks.
3375 *
3376 * @return true if we are currently paused. The caller might be able to
3377 * skip some work in that case since we will come back again.
3378 */
3379 public boolean setLoadOnResume() {
3380 if (mPaused) {
3381 Log.i(TAG, "setLoadOnResume");
3382 mOnResumeNeedsLoad = true;
3383 return true;
3384 } else {
3385 return false;
3386 }
3387 }
3388
3389 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003391 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003393 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003394 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003395 } else {
3396 return SCREEN_COUNT / 2;
3397 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003399
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 /**
3401 * Refreshes the shortcuts shown on the workspace.
3402 *
3403 * Implementation of the method from LauncherModel.Callbacks.
3404 */
3405 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003406 // If we're starting binding all over again, clear any bind calls we'd postponed in
3407 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3408 // from scratch again
3409 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003410
Michael Jurka7607c2f2013-04-03 14:33:19 -07003411 final Workspace workspace = mWorkspace;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003412 mNewShortcutAnimatePage = -1;
3413 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003414 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003415 int count = workspace.getChildCount();
3416 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003417 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003418 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3419 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003421 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003422 if (mHotseat != null) {
3423 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003424 }
3425 }
3426
3427 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003428 * Bind the items start-end from the list.
3429 *
3430 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003431 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003432 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3433 if (waitUntilResume(new Runnable() {
3434 public void run() {
3435 bindItems(shortcuts, start, end);
3436 }
3437 })) {
3438 return;
3439 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003440
Winson Chungf0c6ae02012-03-21 16:10:31 -07003441 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3442 Set<String> newApps = new HashSet<String>();
3443 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3444
3445 Workspace workspace = mWorkspace;
3446 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003448
3449 // Short circuit if we are loading dock items for a configuration which has no dock
3450 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3451 mHotseat == null) {
3452 continue;
3453 }
3454
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 switch (item.itemType) {
3456 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3457 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003458 ShortcutInfo info = (ShortcutInfo) item;
3459 String uri = info.intent.toUri(0).toString();
3460 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003461 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3462 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003463 boolean animateIconUp = false;
3464 synchronized (newApps) {
3465 if (newApps.contains(uri)) {
3466 animateIconUp = newApps.remove(uri);
3467 }
3468 }
3469 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003470 // Prepare the view to be animated up
3471 shortcut.setAlpha(0f);
3472 shortcut.setScaleX(0f);
3473 shortcut.setScaleY(0f);
3474 mNewShortcutAnimatePage = item.screen;
3475 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3476 mNewShortcutAnimateViews.add(shortcut);
3477 }
3478 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003479 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003480 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003481 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003482 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003483 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003484 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3485 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003488 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003489
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003491 }
3492
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 /**
3494 * Implementation of the method from LauncherModel.Callbacks.
3495 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003496 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3497 if (waitUntilResume(new Runnable() {
3498 public void run() {
3499 bindFolders(folders);
3500 }
3501 })) {
3502 return;
3503 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003504 sFolders.clear();
3505 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003506 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507
3508 /**
3509 * Add the views for a widget to the workspace.
3510 *
3511 * Implementation of the method from LauncherModel.Callbacks.
3512 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003513 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3514 if (waitUntilResume(new Runnable() {
3515 public void run() {
3516 bindAppWidget(item);
3517 }
3518 })) {
3519 return;
3520 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003521
Daniel Sandler843e8602010-06-07 14:59:01 -04003522 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3523 if (DEBUG_WIDGETS) {
3524 Log.d(TAG, "bindAppWidget: " + item);
3525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 final Workspace workspace = mWorkspace;
3527
3528 final int appWidgetId = item.appWidgetId;
3529 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003530 if (DEBUG_WIDGETS) {
3531 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3532 }
3533
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3535
Joe Onorato9c1289c2009-08-17 11:03:03 -04003536 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003537 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538
Winson Chung3d503fb2011-07-13 17:25:49 -07003539 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003541 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3542
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 workspace.requestLayout();
3544
Daniel Sandler843e8602010-06-07 14:59:01 -04003545 if (DEBUG_WIDGETS) {
3546 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3547 + (SystemClock.uptimeMillis()-start) + "ms");
3548 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003549 }
3550
Adam Cohen1462de32012-07-24 22:34:36 -07003551 public void onPageBoundSynchronously(int page) {
3552 mSynchronouslyBoundPages.add(page);
3553 }
3554
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 /**
3556 * Callback saying that there aren't any more items to bind.
3557 *
3558 * Implementation of the method from LauncherModel.Callbacks.
3559 */
Adam Cohene25af792013-06-06 23:08:25 -07003560 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003561 if (waitUntilResume(new Runnable() {
3562 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003563 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003564 }
3565 })) {
3566 return;
3567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 if (mSavedState != null) {
3569 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003570 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572 mSavedState = null;
3573 }
3574
Adam Cohen1462de32012-07-24 22:34:36 -07003575 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003577 // If we received the result of any pending adds while the loader was running (e.g. the
3578 // widget configuration forced an orientation change), process them now.
3579 for (int i = 0; i < sPendingAddList.size(); i++) {
3580 completeAdd(sPendingAddList.get(i));
3581 }
3582 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583
Winson Chungc51db6a2011-10-05 11:44:49 -07003584 // Update the market app icon as necessary (the other icons will be managed in response to
3585 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003586 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003587
Winson Chungf0c6ae02012-03-21 16:10:31 -07003588 // Animate up any icons as necessary
3589 if (mVisible || mWorkspaceLoading) {
3590 Runnable newAppsRunnable = new Runnable() {
3591 @Override
3592 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003593 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003594 }
3595 };
Winson Chunga2413752012-04-03 14:22:34 -07003596
3597 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3598 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3599 if (canRunNewAppsAnimation()) {
3600 // If the user has not interacted recently, then either snap to the new page to show
3601 // the new-apps animation or just run them if they are to appear on the current page
3602 if (willSnapPage) {
3603 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3604 } else {
3605 runNewAppsAnimation(false);
3606 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003607 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003608 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003609 // are on another page (or just normally if they are added to the current page)
3610 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003611 }
3612 }
3613
3614 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003615 if (upgradePath) {
3616 mWorkspace.saveWorkspaceToDb();
3617
3618 // Run through this twice... a little hackleberry, but the right solution is complex.
3619 mWorkspace.stripDuplicateApps();
3620 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
3621 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003622 }
3623
Winson Chunga2413752012-04-03 14:22:34 -07003624 private boolean canRunNewAppsAnimation() {
3625 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3626 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3627 }
3628
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629 /**
3630 * Runs a new animation that scales up icons that were added while Launcher was in the
3631 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003632 *
3633 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003634 */
Winson Chunga2413752012-04-03 14:22:34 -07003635 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003636 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003637 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003638
3639 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003640 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3641 @Override
3642 public int compare(View a, View b) {
3643 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3644 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3645 int cellCountX = LauncherModel.getCellCountX();
3646 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3647 }
3648 });
Winson Chunga2413752012-04-03 14:22:34 -07003649
3650 // Animate each of the views in place (or show them immediately if requested)
3651 if (immediate) {
3652 for (View v : mNewShortcutAnimateViews) {
3653 v.setAlpha(1f);
3654 v.setScaleX(1f);
3655 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003656 }
Winson Chunga2413752012-04-03 14:22:34 -07003657 } else {
3658 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3659 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003660 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003661 PropertyValuesHolder.ofFloat("alpha", 1f),
3662 PropertyValuesHolder.ofFloat("scaleX", 1f),
3663 PropertyValuesHolder.ofFloat("scaleY", 1f));
3664 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3665 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3666 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3667 bounceAnims.add(bounceAnim);
3668 }
3669 anim.playTogether(bounceAnims);
3670 anim.addListener(new AnimatorListenerAdapter() {
3671 @Override
3672 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003673 if (mWorkspace != null) {
3674 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3675 }
Winson Chunga2413752012-04-03 14:22:34 -07003676 }
3677 });
3678 anim.start();
3679 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003680
3681 // Clean up
3682 mNewShortcutAnimatePage = -1;
3683 mNewShortcutAnimateViews.clear();
3684 new Thread("clearNewAppsThread") {
3685 public void run() {
3686 mSharedPrefs.edit()
3687 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3688 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3689 .commit();
3690 }
3691 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003692 }
3693
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003694 @Override
3695 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003696 boolean searchVisible = updateGlobalSearchIcon();
3697 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003698 if (mSearchDropTargetBar != null) {
3699 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3700 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003701 }
3702
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003703 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003704 * Add the icons for all apps.
3705 *
3706 * Implementation of the method from LauncherModel.Callbacks.
3707 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003708 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003709 Runnable setAllAppsRunnable = new Runnable() {
3710 public void run() {
3711 if (mAppsCustomizeContent != null) {
3712 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003713
3714 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3715 getHotseat().addAllAppsFolder(mIconCache, apps,
3716 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3717 mIntentsOnWorkspaceFromUpgradePath = null;
3718 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003719 }
3720 }
3721 };
3722
Michael Jurkac57b7a82011-08-09 22:02:20 -07003723 // Remove the progress bar entirely; we could also make it GONE
3724 // but better to remove it since we know it's not going to be used
3725 View progressBar = mAppsCustomizeTabHost.
3726 findViewById(R.id.apps_customize_progress_bar);
3727 if (progressBar != null) {
3728 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003729
3730 // We just post the call to setApps so the user sees the progress bar
3731 // disappear-- otherwise, it just looks like the progress bar froze
3732 // which doesn't look great
3733 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3734 } else {
3735 // If we did not initialize the spinner in onCreate, then we can directly set the
3736 // list of applications without waiting for any progress bars views to be hidden.
3737 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003738 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003739 }
3740
3741 /**
3742 * A package was installed.
3743 *
3744 * Implementation of the method from LauncherModel.Callbacks.
3745 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003746 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3747 if (waitUntilResume(new Runnable() {
3748 public void run() {
3749 bindAppsAdded(apps);
3750 }
3751 })) {
3752 return;
3753 }
3754
Winson Chungf0ea4d32011-06-06 14:27:16 -07003755
Winson Chung785d2eb2011-04-14 16:08:02 -07003756 if (mAppsCustomizeContent != null) {
3757 mAppsCustomizeContent.addApps(apps);
3758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 }
3760
3761 /**
3762 * A package was updated.
3763 *
3764 * Implementation of the method from LauncherModel.Callbacks.
3765 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003766 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3767 if (waitUntilResume(new Runnable() {
3768 public void run() {
3769 bindAppsUpdated(apps);
3770 }
3771 })) {
3772 return;
3773 }
3774
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003775 if (mWorkspace != null) {
3776 mWorkspace.updateShortcuts(apps);
3777 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003778
Winson Chung785d2eb2011-04-14 16:08:02 -07003779 if (mAppsCustomizeContent != null) {
3780 mAppsCustomizeContent.updateApps(apps);
3781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782 }
3783
3784 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003785 * A package was uninstalled. We take both the super set of packageNames
3786 * in addition to specific applications to remove, the reason being that
3787 * this can be called when a package is updated as well. In that scenario,
3788 * we only remove specific components from the workspace, where as
3789 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
Winson Chung83892cc2013-05-01 16:53:33 -07003793 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3794 final ArrayList<ApplicationInfo> appInfos,
3795 final boolean matchPackageNamesOnly) {
3796 if (waitUntilResume(new Runnable() {
3797 public void run() {
3798 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3799 }
3800 })) {
3801 return;
3802 }
3803
3804 if (matchPackageNamesOnly) {
3805 mWorkspace.removeItemsByPackageName(packageNames);
3806 } else {
3807 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003808 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003809
Winson Chung785d2eb2011-04-14 16:08:02 -07003810 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003811 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003812 }
Winson Chunga1820962011-10-03 16:31:06 -07003813
3814 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003815 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 }
Joe Onoratobe386092009-11-17 17:32:16 -08003817
3818 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003819 * A number of packages were updated.
3820 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003821
3822 private ArrayList<Object> mWidgetsAndShortcuts;
3823 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003824 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003825 bindPackagesUpdated(mWidgetsAndShortcuts);
3826 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003827 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003828 };
3829
3830 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3831 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3832 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003833 return;
3834 }
3835
Winson Chung785d2eb2011-04-14 16:08:02 -07003836 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003837 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003838 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003839 }
3840
Winson Chung400438b2011-01-16 17:53:48 -08003841 private int mapConfigurationOriActivityInfoOri(int configOri) {
3842 final Display d = getWindowManager().getDefaultDisplay();
3843 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3844 switch (d.getRotation()) {
3845 case Surface.ROTATION_0:
3846 case Surface.ROTATION_180:
3847 // We are currently in the same basic orientation as the natural orientation
3848 naturalOri = configOri;
3849 break;
3850 case Surface.ROTATION_90:
3851 case Surface.ROTATION_270:
3852 // We are currently in the other basic orientation to the natural orientation
3853 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3854 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3855 break;
3856 }
3857
3858 int[] oriMap = {
3859 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3860 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3861 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3862 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3863 };
3864 // Since the map starts at portrait, we need to offset if this device's natural orientation
3865 // is landscape.
3866 int indexOffset = 0;
3867 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3868 indexOffset = 1;
3869 }
3870 return oriMap[(d.getRotation() + indexOffset) % 4];
3871 }
Adam Cohen446e9402011-09-15 18:21:21 -07003872
Winson Chung4b919f82012-05-01 10:44:08 -07003873 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003874 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003875 getResources().getBoolean(R.bool.allow_rotation);
3876 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003877 }
Winson Chung4b919f82012-05-01 10:44:08 -07003878 public void lockScreenOrientation() {
3879 if (isRotationEnabled()) {
3880 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3881 .getConfiguration().orientation));
3882 }
3883 }
3884 public void unlockScreenOrientation(boolean immediate) {
3885 if (isRotationEnabled()) {
3886 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003887 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003888 } else {
3889 mHandler.postDelayed(new Runnable() {
3890 public void run() {
3891 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3892 }
3893 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003894 }
Winson Chung4b919f82012-05-01 10:44:08 -07003895 }
Winson Chung400438b2011-01-16 17:53:48 -08003896 }
3897
Winson Chung82f55532011-08-09 14:14:23 -07003898 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003899 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003900 if (DISABLE_CLINGS) {
3901 return false;
3902 }
3903
Brett Chabot2a9e2282011-08-23 15:03:13 -07003904 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003905 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003906
Michael Jurkae233a8b2013-03-19 13:49:20 +01003907 // Restricted secondary users (child mode) will potentially have very few apps
3908 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003909// boolean supportsLimitedUsers =
3910// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3911// Account[] accounts = AccountManager.get(this).getAccounts();
3912// if (supportsLimitedUsers && accounts.length == 0) {
3913// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3914// Bundle restrictions = um.getUserRestrictions();
3915// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3916// return false;
3917// }
3918// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003919 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003920 }
Michael Jurka22143132012-10-04 17:42:38 +02003921
Winson Chung7d7541e2011-09-16 20:14:36 -07003922 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003923 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003924 if (cling != null) {
3925 cling.init(this, positionData);
3926 cling.setVisibility(View.VISIBLE);
3927 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3928 if (animate) {
3929 cling.buildLayer();
3930 cling.setAlpha(0f);
3931 cling.animate()
3932 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003933 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003934 .setDuration(SHOW_CLING_DURATION)
3935 .setStartDelay(delay)
3936 .start();
3937 } else {
3938 cling.setAlpha(1f);
3939 }
Michael Jurka22143132012-10-04 17:42:38 +02003940 cling.setFocusableInTouchMode(true);
3941 cling.post(new Runnable() {
3942 public void run() {
3943 cling.setFocusable(true);
3944 cling.requestFocus();
3945 }
3946 });
3947 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3948 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003949 }
3950 return cling;
3951 }
Michael Jurka22143132012-10-04 17:42:38 +02003952
Winson Chung7d7541e2011-09-16 20:14:36 -07003953 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003954 // To catch cases where siblings of top-level views are made invisible, just check whether
3955 // the cling is directly set to GONE before dismissing it.
3956 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003957 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003958 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003959 anim.addListener(new AnimatorListenerAdapter() {
3960 public void onAnimationEnd(Animator animation) {
3961 cling.setVisibility(View.GONE);
3962 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003963 // We should update the shared preferences on a background thread
3964 new Thread("dismissClingThread") {
3965 public void run() {
3966 SharedPreferences.Editor editor = mSharedPrefs.edit();
3967 editor.putBoolean(flag, true);
3968 editor.commit();
3969 }
3970 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003971 };
3972 });
3973 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003974 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003975 }
3976 }
Michael Jurka22143132012-10-04 17:42:38 +02003977
Winson Chung9d9d74f2011-09-19 11:49:12 -07003978 private void removeCling(int id) {
3979 final View cling = findViewById(id);
3980 if (cling != null) {
3981 final ViewGroup parent = (ViewGroup) cling.getParent();
3982 parent.post(new Runnable() {
3983 @Override
3984 public void run() {
3985 parent.removeView(cling);
3986 }
3987 });
Michael Jurka22143132012-10-04 17:42:38 +02003988 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003989 }
3990 }
Michael Jurka974c3862012-05-22 22:00:31 -07003991
3992 private boolean skipCustomClingIfNoAccounts() {
3993 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3994 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3995 if (customCling) {
3996 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04003997 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07003998 Account[] accounts = am.getAccountsByType("com.google");
3999 return accounts.length == 0;
4000 }
4001 return false;
4002 }
4003
Winson Chung7d7541e2011-09-16 20:14:36 -07004004 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004005 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004006 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004007 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4008 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004009 // If we're not using the default workspace layout, replace workspace cling
4010 // with a custom workspace cling (usually specified in an overlay)
4011 // For now, only do this on tablets
4012 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004013 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004014 // Use a custom cling
4015 View cling = findViewById(R.id.workspace_cling);
4016 ViewGroup clingParent = (ViewGroup) cling.getParent();
4017 int clingIndex = clingParent.indexOfChild(cling);
4018 clingParent.removeViewAt(clingIndex);
4019 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4020 clingParent.addView(customCling, clingIndex);
4021 customCling.setId(R.id.workspace_cling);
4022 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004023 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004024 } else {
4025 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 }
4027 }
4028 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004029 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004030 if (isClingsEnabled() &&
4031 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004033 } else {
4034 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004035 }
4036 }
4037 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004039 if (isClingsEnabled() &&
4040 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4041 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004042 } else {
4043 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004044 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004045 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004046 }
4047 public boolean isFolderClingVisible() {
4048 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004049 if (cling != null) {
4050 return cling.getVisibility() == View.VISIBLE;
4051 }
4052 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004053 }
Winson Chung82f55532011-08-09 14:14:23 -07004054 public void dismissWorkspaceCling(View v) {
4055 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004057 }
4058 public void dismissAllAppsCling(View v) {
4059 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4061 }
4062 public void dismissFolderCling(View v) {
4063 Cling cling = (Cling) findViewById(R.id.folder_cling);
4064 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004065 }
4066
Winson Chung80baf5a2010-08-09 16:03:15 -07004067 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004068 * Prints out out state for debugging.
4069 */
4070 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004071 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004072 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004073 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4074 Log.d(TAG, "mRestoring=" + mRestoring);
4075 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4076 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004077 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004078 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004079
Winson Chung785d2eb2011-04-14 16:08:02 -07004080 if (mAppsCustomizeContent != null) {
4081 mAppsCustomizeContent.dumpState();
4082 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004083 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004084 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004085
4086 @Override
4087 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4088 super.dump(prefix, fd, writer, args);
4089 writer.println(" ");
4090 writer.println("Debug logs: ");
4091 for (int i = 0; i < sDumpLogs.size(); i++) {
4092 writer.println(" " + sDumpLogs.get(i));
4093 }
4094 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004095
4096 public static void dumpDebugLogsToConsole() {
4097 Log.d(TAG, "");
4098 Log.d(TAG, "*********************");
4099 Log.d(TAG, "Launcher debug logs: ");
4100 for (int i = 0; i < sDumpLogs.size(); i++) {
4101 Log.d(TAG, " " + sDumpLogs.get(i));
4102 }
4103 Log.d(TAG, "*********************");
4104 Log.d(TAG, "");
4105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004106}
Michael Jurka2763be32011-02-24 11:19:57 -08004107
Michael Jurkaabded662011-03-04 12:06:57 -08004108interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004109 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004110 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004111 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004112 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004113 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004114}