blob: c216572a6d163664cc9a8bfbee0b7312ae2ce330 [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 Cohen99894d92013-06-14 11:22:59 -070047import android.content.pm.ResolveInfo;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Craig Mautner360310b2012-10-26 15:13:08 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Craig Mautner360310b2012-10-26 15:13:08 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070067import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080068import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Winson Chungc9168342013-06-26 14:54:55 -070074import android.util.Pair;
Daniel Sandler924b9932013-06-12 00:38:25 -040075import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080076import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080077import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080078import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070079import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070080import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080081import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040083import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070084
Daniel Sandler325dc232013-06-05 22:57:57 -040085import com.android.launcher3.R;
86import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080087
Adam Cohenc0dcf592011-06-01 15:30:43 -070088import java.io.DataInputStream;
89import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070090import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070091import java.io.FileNotFoundException;
92import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070093import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070095import java.util.Collection;
96import java.util.Collections;
97import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070098import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070099import java.util.HashSet;
Winson Chungc9168342013-06-26 14:54:55 -0700100import java.util.Iterator;
Michael Jurkad7c28052012-04-27 15:43:36 -0700101import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700102import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800104/**
105 * Default launcher application.
106 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100107public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700108 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700109 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800110 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700111 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112
Daniel Sandler924b9932013-06-12 00:38:25 -0400113 static final boolean PROFILE_STARTUP = true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400114 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700115 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandler924b9932013-06-12 00:38:25 -0400116 static final boolean DEBUG_RESUME_TIME = true;
117 static final boolean DEBUG_MEMORY = true;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
Winson Chung70d72102011-08-12 11:24:35 -0700119 private static final int MENU_GROUP_WALLPAPER = 1;
120 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
121 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700122 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
123 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int REQUEST_PICK_APPLICATION = 6;
128 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700130 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Michael Jurka8b805b12012-04-18 14:23:14 -0700132 private static final int REQUEST_BIND_APPWIDGET = 11;
133
Mathew Inwood876a8462013-06-14 14:12:41 +0100134 /**
135 * IntentStarter uses request codes starting with this. This must be greater than all activity
136 * request codes used internally.
137 */
138 protected static final int REQUEST_LAST = 100;
139
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800146 // To turn on these properties, type
147 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
148 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
149 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Winson Chung2672ff92012-05-04 16:22:30 -0700151 // The Intent extra that defines whether to ignore the launch animation
152 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400153 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
156 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700157 // Type: int
158 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
161 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
163 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: boolean
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
169 // Type: long
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700171 // Type: int
172 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
173 // Type: int
174 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
175 // Type: parcelable
176 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100178 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700179 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100180 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700181 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100182 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700185 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800188 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700191 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
192 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700193 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700194 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800197 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Winson Chunga2413752012-04-03 14:22:34 -0700199 // How long to wait before the new-shortcut animation automatically pans the workspace
200 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
201
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800202 private final BroadcastReceiver mCloseSystemDialogsReceiver
203 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800204 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private LayoutInflater mInflater;
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private View mQsbDivider;
210 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700211 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800212 private DragLayer mDragLayer;
213 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private AppWidgetManager mAppWidgetManager;
216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurkac9d95c52011-08-29 14:03:34 -0700218 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700219 private AppWidgetProviderInfo mPendingAddWidgetInfo;
220
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221 private int[] mTmpAddItemCellCoordinates = new int[2];
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private FolderInfo mFolderInfo;
224
Winson Chung3d503fb2011-07-13 17:25:49 -0700225 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800226 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700227
Winson Chungc51db6a2011-10-05 11:44:49 -0700228 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700229 private AppsCustomizeTabHost mAppsCustomizeTabHost;
230 private AppsCustomizePagedView mAppsCustomizeContent;
231 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700234 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
235 // scroll issues (because the workspace may not have been measured yet) and extra work.
236 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
237 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private SpannableStringBuilder mDefaultKeySsb = null;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private boolean mWorkspaceLoading = true;
242
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800243 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private boolean mRestoring;
245 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700246 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Michael Jurka7607c2f2013-04-03 14:33:19 -0700248 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
249
Winson Chung4a2afa32012-07-19 14:53:05 -0700250 // Keep track of whether the user has left launcher
251 private static boolean sPausedFromUserAction = false;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Bundle mSavedInstanceState;
254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800257 private boolean mUserPresent = true;
258 private boolean mVisible = false;
259 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700260 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400261
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700262 private static LocaleConfiguration sLocaleConfiguration = null;
263
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700264 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700266 private Intent mAppMarketIntent = null;
267
Adam Cohended9f8d2010-11-03 13:25:16 -0700268 // Related to the auto-advancing of widgets
269 private final int ADVANCE_MSG = 1;
270 private final int mAdvanceInterval = 20000;
271 private final int mAdvanceStagger = 250;
272 private long mAutoAdvanceSentTime;
273 private long mAutoAdvanceTimeLeft = -1;
274 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
275 new HashMap<View, AppWidgetProviderInfo>();
276
Winson Chung400438b2011-01-16 17:53:48 -0800277 // Determines how long to wait after a rotation before restoring the screen orientation to
278 // match the sensor state.
279 private final int mRestoreScreenOrientationDelay = 500;
280
Michael Jurka4ef207b2010-11-29 17:05:45 -0800281 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700282 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
283 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
284 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800285
Craig Mautner360310b2012-10-26 15:13:08 -0700286 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700287
Adam Cohen1462de32012-07-24 22:34:36 -0700288 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
289
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700290 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
291
Winson Chung46353de2012-02-16 14:05:10 -0800292 // We only want to get the SharedPreferences once since it does an FS stat each time we get
293 // it from the context.
294 private SharedPreferences mSharedPrefs;
295
Adam Cohene25af792013-06-06 23:08:25 -0700296 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
297
Winson Chungf0c6ae02012-03-21 16:10:31 -0700298 // Holds the page that we need to animate to, and the icon views that we need to animate up
299 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700300 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700301 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700302 private ImageView mFolderIconImageView;
303 private Bitmap mFolderIconBitmap;
304 private Canvas mFolderIconCanvas;
305 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700306
Michael Jurkaddd62e92011-02-16 17:49:14 -0800307 private BubbleTextView mWaitingForResume;
308
Michael Jurka22143132012-10-04 17:42:38 +0200309 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
310 = new HideFromAccessibilityHelper();
311
Michael Jurkac1f5d262011-09-30 19:32:27 -0700312 private Runnable mBuildLayersRunnable = new Runnable() {
313 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700314 if (mWorkspace != null) {
315 mWorkspace.buildPageHardwareLayers();
316 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700317 }
318 };
319
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 private static ArrayList<PendingAddArguments> sPendingAddList
321 = new ArrayList<PendingAddArguments>();
322
Michael Jurka0a457bf2012-11-19 14:05:05 -0800323 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
324
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800325 private static class PendingAddArguments {
326 int requestCode;
327 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700328 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700329 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800330 int cellX;
331 int cellY;
332 }
333
Michael Jurka0a457bf2012-11-19 14:05:05 -0800334 private static boolean isPropertyEnabled(String propertyName) {
335 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700336 }
337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 @Override
339 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700340 if (DEBUG_STRICT_MODE) {
341 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
342 .detectDiskReads()
343 .detectDiskWrites()
344 .detectNetwork() // or .detectAll() for all detectable problems
345 .penaltyLog()
346 .build());
347 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
348 .detectLeakedSqlLiteObjects()
349 .detectLeakedClosableObjects()
350 .penaltyLog()
351 .penaltyDeath()
352 .build());
353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400356 LauncherAppState app = LauncherAppState.getInstance();
357 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700358 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 mModel = app.setLauncher(this);
360 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400361 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700363
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700364 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
366 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700367
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700368 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
369 // this also ensures that any synchronous binding below doesn't re-trigger another
370 // LauncherModel load.
371 mPaused = false;
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200374 android.os.Debug.startMethodTracing(
375 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
377
378 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 setContentView(R.layout.launcher);
380 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700381 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800383 registerContentObservers();
384
Joe Onorato7c312c12009-08-13 21:36:53 -0700385 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 mSavedState = savedInstanceState;
388 restoreState(mSavedState);
389
Winson Chunga12a2502010-12-20 14:41:35 -0800390 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700391 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200392 mAppsCustomizeContent.onPackagesUpdated(
393 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700394 }
Winson Chunga12a2502010-12-20 14:41:35 -0800395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 if (PROFILE_STARTUP) {
397 android.os.Debug.stopMethodTracing();
398 }
399
400 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700401 if (sPausedFromUserAction) {
402 // If the user leaves launcher, then we should just load items asynchronously when
403 // they return.
404 mModel.startLoader(true, -1);
405 } else {
406 // We only load the page synchronously if the user rotates (or triggers a
407 // configuration change) while launcher is in the foreground
408 mModel.startLoader(true, mWorkspace.getCurrentPage());
409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411
Michael Jurkac57b7a82011-08-09 22:02:20 -0700412 if (!mModel.isAllAppsLoaded()) {
413 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
414 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 // For handling default keys
418 mDefaultKeySsb = new SpannableStringBuilder();
419 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800420
421 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
422 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800423
Adam Cohenf9426d52012-06-04 17:26:21 -0700424 updateGlobalIcons();
425
426 // On large interfaces, we want the screen to auto-rotate based on the current orientation
427 unlockScreenOrientation(true);
428 }
429
Winson Chung4a2afa32012-07-19 14:53:05 -0700430 protected void onUserLeaveHint() {
431 super.onUserLeaveHint();
432 sPausedFromUserAction = true;
433 }
434
Adam Cohenf9426d52012-06-04 17:26:21 -0700435 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700436 boolean searchVisible = false;
437 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800438 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700439 int coi = getCurrentOrientationIndexForGlobalIcons();
440 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
441 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700442 updateAppMarketIcon();
443 searchVisible = updateGlobalSearchIcon();
444 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700445 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700446 if (sGlobalSearchIcon[coi] != null) {
447 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 if (sVoiceSearchIcon[coi] != null) {
451 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sAppMarketIcon[coi] != null) {
455 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800456 }
Winson Chungadf0c182012-08-23 14:59:07 -0700457 if (mSearchDropTargetBar != null) {
458 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
Romain Guycbb89e42009-06-08 15:52:54 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 if (sLocaleConfiguration == null) {
464 new AsyncTask<Void, Void, LocaleConfiguration>() {
465 @Override
466 protected LocaleConfiguration doInBackground(Void... unused) {
467 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
468 readConfiguration(Launcher.this, localeConfiguration);
469 return localeConfiguration;
470 }
471
472 @Override
473 protected void onPostExecute(LocaleConfiguration result) {
474 sLocaleConfiguration = result;
475 checkForLocaleChange(); // recursive, but now with a locale configuration
476 }
477 }.execute();
478 return;
479 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final Configuration configuration = getResources().getConfiguration();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final String locale = configuration.locale.toString();
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mcc = configuration.mcc;
488
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700489 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final int mnc = configuration.mnc;
491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Romain Guy84f296c2009-11-04 15:00:44 -0800494 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 sLocaleConfiguration.locale = locale;
496 sLocaleConfiguration.mcc = mcc;
497 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700498
Joe Onorato0589f0f2010-02-08 13:44:00 -0800499 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500
501 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
502 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700503 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 public void run() {
505 writeConfiguration(Launcher.this, localeConfiguration);
506 }
507 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700508 }
509 }
510
511 private static class LocaleConfiguration {
512 public String locale;
513 public int mcc = -1;
514 public int mnc = -1;
515 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700516
Romain Guy98d01652009-06-30 16:21:04 -0700517 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
518 DataInputStream in = null;
519 try {
520 in = new DataInputStream(context.openFileInput(PREFERENCES));
521 configuration.locale = in.readUTF();
522 configuration.mcc = in.readInt();
523 configuration.mnc = in.readInt();
524 } catch (FileNotFoundException e) {
525 // Ignore
526 } catch (IOException e) {
527 // Ignore
528 } finally {
529 if (in != null) {
530 try {
531 in.close();
532 } catch (IOException e) {
533 // Ignore
534 }
535 }
536 }
537 }
538
539 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
540 DataOutputStream out = null;
541 try {
542 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
543 out.writeUTF(configuration.locale);
544 out.writeInt(configuration.mcc);
545 out.writeInt(configuration.mnc);
546 out.flush();
547 } catch (FileNotFoundException e) {
548 // Ignore
549 } catch (IOException e) {
550 //noinspection ResultOfMethodCallIgnored
551 context.getFileStreamPath(PREFERENCES).delete();
552 } finally {
553 if (out != null) {
554 try {
555 out.close();
556 } catch (IOException e) {
557 // Ignore
558 }
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
562
Bjorn Bringertc459e522013-06-07 19:36:01 +0100563 public LayoutInflater getInflater() {
564 return mInflater;
565 }
566
Adam Cohen716b51e2011-06-30 12:09:54 -0700567 public DragLayer getDragLayer() {
568 return mDragLayer;
569 }
570
Winson Chung36a62fe2012-05-06 18:04:42 -0700571 boolean isDraggingEnabled() {
572 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
573 // that is subsequently removed from the workspace in startBinding().
574 return !mModel.isLoadingWorkspace();
575 }
576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 static int getScreen() {
578 synchronized (sLock) {
579 return sScreen;
580 }
581 }
582
583 static void setScreen(int screen) {
584 synchronized (sLock) {
585 sScreen = screen;
586 }
587 }
588
Winson Chung557d6ed2011-07-08 15:34:52 -0700589 /**
590 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
591 * a configuration step, this allows the proper animations to run after other transitions.
592 */
593 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700594 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 switch (args.requestCode) {
596 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700597 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700598 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 break;
600 case REQUEST_PICK_SHORTCUT:
601 processShortcut(args.intent);
602 break;
603 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700604 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700605 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700606 result = true;
607 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 case REQUEST_CREATE_APPWIDGET:
609 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700610 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700611 result = true;
612 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800613 case REQUEST_PICK_WALLPAPER:
614 // We just wanted the activity result here so we can clear mWaitingForResult
615 break;
616 }
Michael Jurka27614d22012-04-02 06:40:22 -0700617 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
618 // if you turned the screen off and then back while in All Apps, Launcher would not
619 // return to the workspace. Clearing mAddInfo.container here fixes this issue
620 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700621 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700625 protected void onActivityResult(
626 final int requestCode, final int resultCode, final Intent data) {
627 if (requestCode == REQUEST_BIND_APPWIDGET) {
628 int appWidgetId = data != null ?
629 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
630 if (resultCode == RESULT_CANCELED) {
631 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
632 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700633 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 }
635 return;
636 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700637 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
639 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700640 mWaitingForResult = false;
641
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 // We have special handling for widgets
643 if (isWidgetDrop) {
644 int appWidgetId = data != null ?
645 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700646 if (appWidgetId < 0) {
647 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
648 "widget configuration activity.");
649 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
650 } else {
651 completeTwoStageWidgetDrop(resultCode, appWidgetId);
652 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 return;
654 }
655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // The pattern used here is that a user PICKs a specific application,
657 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
660 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700661 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 final PendingAddArguments args = new PendingAddArguments();
663 args.requestCode = requestCode;
664 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700666 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700667 args.cellX = mPendingAddInfo.cellX;
668 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 if (isWorkspaceLocked()) {
670 sPendingAddList.add(args);
671 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700676 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
678 null);
679 }
680
681 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700682 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700683 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 Runnable onCompleteRunnable = null;
685 int animationType = 0;
686
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 if (resultCode == RESULT_OK) {
689 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
690 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700691 mPendingAddWidgetInfo);
692 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 onCompleteRunnable = new Runnable() {
694 @Override
695 public void run() {
696 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700697 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800698 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
699 null);
700 }
701 };
702 } else if (resultCode == RESULT_CANCELED) {
703 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
704 onCompleteRunnable = new Runnable() {
705 @Override
706 public void run() {
707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
708 null);
709 }
710 };
711 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700712 if (mDragLayer.getAnimatedView() != null) {
713 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
714 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
715 animationType, boundWidget, true);
716 } else {
717 // The animated view may be null in the case of a rotation during widget configuration
718 onCompleteRunnable.run();
719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 }
721
722 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700723 protected void onStop() {
724 super.onStop();
725 FirstFrameAnimatorHelper.setIsVisible(false);
726 }
727
728 @Override
729 protected void onStart() {
730 super.onStart();
731 FirstFrameAnimatorHelper.setIsVisible(true);
732 }
733
734 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200736 long startTime = 0;
737 if (DEBUG_RESUME_TIME) {
738 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400739 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700742
Winson Chung4a2afa32012-07-19 14:53:05 -0700743 // Restore the previous launcher state
744 if (mOnResumeState == State.WORKSPACE) {
745 showWorkspace(false);
746 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
747 showAllApps(false);
748 }
749 mOnResumeState = State.NONE;
750
Craig Mautner360310b2012-10-26 15:13:08 -0700751 // Background was set to gradient in onPause(), restore to black if in all apps.
752 setWorkspaceBackground(mState == State.WORKSPACE);
753
Winson Chungde0fb8f2012-05-08 14:37:08 -0700754 // Process any items that were added while Launcher was away
755 InstallShortcutReceiver.flushInstallQueue(this);
756
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800757 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700758 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700759 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400760 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700761 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400762 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700763 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200765 if (mOnResumeCallbacks.size() > 0) {
766 // We might have postponed some bind calls until onResume (see waitUntilResume) --
767 // execute them here
768 long startTimeCallbacks = 0;
769 if (DEBUG_RESUME_TIME) {
770 startTimeCallbacks = System.currentTimeMillis();
771 }
772
773 if (mAppsCustomizeContent != null) {
774 mAppsCustomizeContent.setBulkBind(true);
775 }
776 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
777 mOnResumeCallbacks.get(i).run();
778 }
779 if (mAppsCustomizeContent != null) {
780 mAppsCustomizeContent.setBulkBind(false);
781 }
782 mOnResumeCallbacks.clear();
783 if (DEBUG_RESUME_TIME) {
784 Log.d(TAG, "Time spent processing callbacks in onResume: " +
785 (System.currentTimeMillis() - startTimeCallbacks));
786 }
Michael Jurka447bf842013-05-15 14:52:15 +0200787 }
Winson Chunge4e50662012-01-23 14:45:13 -0800788
789 // Reset the pressed state of icons that were locked in the press state while activities
790 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800791 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800792 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800793 mWaitingForResume.setStayPressed(false);
794 }
Winson Chunge4e50662012-01-23 14:45:13 -0800795 if (mAppsCustomizeContent != null) {
796 // Resets the previous all apps icon press state
797 mAppsCustomizeContent.resetDrawableState();
798 }
Adam Cohen06dff352012-06-01 17:17:08 -0700799 // It is possible that widgets can receive updates while launcher is not in the foreground.
800 // Consequently, the widgets will be inflated in the orientation of the foreground activity
801 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
802 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700803 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700804
805 // Again, as with the above scenario, it's possible that one or more of the global icons
806 // were updated in the wrong orientation.
807 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200808 if (DEBUG_RESUME_TIME) {
809 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
810 }
Adam Cohen06dff352012-06-01 17:17:08 -0700811 }
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700814 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700815 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
816 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
817 // when Launcher resumes and we are still in AllApps.
818 updateWallpaperVisibility(true);
819
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700820 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700821 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800822 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700823 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700824 }
Romain Guycbb89e42009-06-08 15:52:54 -0700825
Adam Cohen66a01fd2013-06-11 12:48:00 -0700826 protected void onFinishBindingItems() {
827 }
828
829 // Add a fullscreen unpadded view to the workspace to the left all other screens.
830 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700831 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700832 }
833
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700834 @Override
835 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400836 // Flag the loader to stop early before switching
837 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700838 if (mAppsCustomizeContent != null) {
839 mAppsCustomizeContent.surrender();
840 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800841 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700842 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800844 // We can't hide the IME if it was forced open. So don't bother
845 /*
846 @Override
847 public void onWindowFocusChanged(boolean hasFocus) {
848 super.onWindowFocusChanged(hasFocus);
849
850 if (hasFocus) {
851 final InputMethodManager inputManager = (InputMethodManager)
852 getSystemService(Context.INPUT_METHOD_SERVICE);
853 WindowManager.LayoutParams lp = getWindow().getAttributes();
854 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
855 android.os.Handler()) {
856 protected void onReceiveResult(int resultCode, Bundle resultData) {
857 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
858 }
859 });
860 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
861 }
862 }
863 */
864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 private boolean acceptFilter() {
866 final InputMethodManager inputManager = (InputMethodManager)
867 getSystemService(Context.INPUT_METHOD_SERVICE);
868 return !inputManager.isFullscreenMode();
869 }
870
871 @Override
872 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700873 final int uniChar = event.getUnicodeChar();
874 final boolean handled = super.onKeyDown(keyCode, event);
875 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
876 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
878 keyCode, event);
879 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700880 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700881 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700882 // showSearchDialog()
883 // If there are multiple keystrokes before the search dialog takes focus,
884 // onSearchRequested() will be called for every keystroke,
885 // but it is idempotent, so it's fine.
886 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
888 }
889
Joe Onorato8a9625e2010-01-28 15:55:35 -0800890 // Eat the long press event so the keyboard doesn't come up.
891 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
892 return true;
893 }
894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 return handled;
896 }
897
Karl Rosaen138a0412009-04-23 19:00:21 -0700898 private String getTypedText() {
899 return mDefaultKeySsb.toString();
900 }
901
902 private void clearTypedText() {
903 mDefaultKeySsb.clear();
904 mDefaultKeySsb.clearSpans();
905 Selection.setSelection(mDefaultKeySsb, 0);
906 }
907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700909 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
910 * State
911 */
912 private static State intToState(int stateOrdinal) {
913 State state = State.WORKSPACE;
914 final State[] stateValues = State.values();
915 for (int i = 0; i < stateValues.length; i++) {
916 if (stateValues[i].ordinal() == stateOrdinal) {
917 state = stateValues[i];
918 break;
919 }
920 }
921 return state;
922 }
923
924 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 * Restores the previous state, if it exists.
926 *
927 * @param savedState The previous state.
928 */
929 private void restoreState(Bundle savedState) {
930 if (savedState == null) {
931 return;
932 }
933
Michael Jurka883f55b2010-10-21 15:47:14 -0700934 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700935 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700936 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800937 }
938
Winson Chungf0c6ae02012-03-21 16:10:31 -0700939 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700941 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700945 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
948 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700950 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
951 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
953 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
954 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700955 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 mRestoring = true;
957 }
958
959 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
960 if (renameFolder) {
961 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700962 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 mRestoring = true;
964 }
Winson Chunga12a2502010-12-20 14:41:35 -0800965
Winson Chung785d2eb2011-04-14 16:08:02 -0700966 // Restore the AppsCustomize tab
967 if (mAppsCustomizeTabHost != null) {
968 String curTab = savedState.getString("apps_customize_currentTab");
969 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700970 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700971 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700972 mAppsCustomizeContent.loadAssociatedPages(
973 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700974 }
975
Winson Chung5afbf7b2011-07-25 11:53:08 -0700976 int currentIndex = savedState.getInt("apps_customize_currentIndex");
977 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980
981 /**
982 * Finds all the views we need and configure them properly.
983 */
984 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700985 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400986
Craig Mautner360310b2012-10-26 15:13:08 -0700987 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700988 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
989 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700990 mQsbDivider = findViewById(R.id.qsb_divider);
991 mDockDivider = findViewById(R.id.dock_divider);
992
993 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
994 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
Winson Chung4c98d922011-05-31 16:50:48 -0700996 // Setup the drag layer
997 mDragLayer.setup(this, dragController);
998
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 // Setup the hotseat
1000 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1001 if (mHotseat != null) {
1002 mHotseat.setup(this);
1003 }
1004
Winson Chung4c98d922011-05-31 16:50:48 -07001005 // Setup the workspace
1006 mWorkspace.setHapticFeedbackEnabled(false);
1007 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001008 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001009 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001010
Winson Chungf0ea4d32011-06-06 14:27:16 -07001011 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001012 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001013
Winson Chungf0ea4d32011-06-06 14:27:16 -07001014 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001015 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001016 mAppsCustomizeContent = (AppsCustomizePagedView)
1017 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1018 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001019
Winson Chung3d503fb2011-07-13 17:25:49 -07001020 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001021 dragController.setDragScoller(mWorkspace);
1022 dragController.setScrollView(mDragLayer);
1023 dragController.setMoveTarget(mWorkspace);
1024 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001025 if (mSearchDropTargetBar != null) {
1026 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001027 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001028
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001029 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001030 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001031 final View ww = new WeightWatcher(this);
1032 ww.setAlpha(0.5f);
1033 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001034 new FrameLayout.LayoutParams(
1035 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001036 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001037 Gravity.BOTTOM)
1038 );
1039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041
1042 /**
1043 * Creates a view representing a shortcut.
1044 *
1045 * @param info The data structure describing the shortcut.
1046 *
1047 * @return A View inflated from R.layout.application.
1048 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001049 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001051 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
1054 /**
1055 * Creates a view representing a shortcut inflated from the specified resource.
1056 *
1057 * @param layoutResId The id of the XML layout used to create the shortcut.
1058 * @param parent The group the shortcut belongs to.
1059 * @param info The data structure describing the shortcut.
1060 *
1061 * @return A View inflated from layoutResId.
1062 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001063 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001064 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1065 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 return favorite;
1068 }
1069
1070 /**
1071 * Add an application shortcut to the workspace.
1072 *
1073 * @param data The intent describing the application.
1074 * @param cellInfo The position on screen where to create the shortcut.
1075 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001076 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001078 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001079
Adam Cohenfbba09b2011-07-18 21:43:05 -07001080 // First we check if we already know the exact location where we want to add this item.
1081 if (cellX >= 0 && cellY >= 0) {
1082 cellXY[0] = cellX;
1083 cellXY[1] = cellY;
1084 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001085 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 return;
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001089 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001090
Romain Guy73b979d2009-06-09 12:57:21 -07001091 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001092 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001094 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001095 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001096 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001097 } else {
1098 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 /**
1103 * Add a shortcut to the workspace.
1104 *
1105 * @param data The intent describing the shortcut.
1106 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001108 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001109 int cellY) {
1110 int[] cellXY = mTmpAddItemCellCoordinates;
1111 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001112 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Michael Jurkad3ef3062010-11-23 16:23:58 -08001114 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001115
Adam Cohen558baaf2011-08-15 15:22:57 -07001116 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001117 if (info == null) {
1118 return;
1119 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001120 final View view = createShortcut(info);
1121
Adam Cohenfbba09b2011-07-18 21:43:05 -07001122 // First we check if we already know the exact location where we want to add this item.
1123 if (cellX >= 0 && cellY >= 0) {
1124 cellXY[0] = cellX;
1125 cellXY[1] = cellY;
1126 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001127
1128 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001129 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001130 true, null,null)) {
1131 return;
1132 }
1133 DragObject dragObject = new DragObject();
1134 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001135 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1136 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001137 return;
1138 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001139 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001140 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001141 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001142 foundCellSpan = (result != null);
1143 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001144 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001145 }
1146
1147 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001148 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 return;
1150 }
1151
Adam Cohendcd297f2013-06-18 13:13:40 -07001152 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153
1154 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001155 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001156 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 }
1158 }
1159
Adam Cohen2f093b62012-04-30 18:59:53 -07001160 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1161 int minHeight) {
1162 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001163 // We want to account for the extra amount of padding that we are adding to the widget
1164 // to ensure that it gets the full amount of space that it has requested
1165 int requiredWidth = minWidth + padding.left + padding.right;
1166 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001167 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001168 }
1169
Adam Cohen2f093b62012-04-30 18:59:53 -07001170 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1171 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001172 }
1173
Adam Cohen2f093b62012-04-30 18:59:53 -07001174 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1175 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001176 }
1177
Adam Cohen2f093b62012-04-30 18:59:53 -07001178 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1179 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001180 }
1181
Adam Cohen2f093b62012-04-30 18:59:53 -07001182 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1183 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001184 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001185 }
1186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001190 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001193 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001194 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1195 if (appWidgetInfo == null) {
1196 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1197 }
Romain Guycbb89e42009-06-08 15:52:54 -07001198
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001199 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001200 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001201
Adam Cohen2f093b62012-04-30 18:59:53 -07001202 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1203 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001206 // We have saved the position to which the widget was dragged-- this really only matters
1207 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001208 int[] cellXY = mTmpAddItemCellCoordinates;
1209 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001210 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1213 cellXY[0] = mPendingAddInfo.cellX;
1214 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001215 spanXY[0] = mPendingAddInfo.spanX;
1216 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001217 foundCellSpan = true;
1218 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001220 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001221 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1222 spanXY[1], cellXY, finalSpan);
1223 spanXY[0] = finalSpan[0];
1224 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001225 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001226 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001227 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001228 }
1229
Michael Jurka0280c3b2010-09-17 15:00:07 -07001230 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001231 if (appWidgetId != -1) {
1232 // Deleting an app widget ID is a void call but writes to disk before returning
1233 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001234 new Thread("deleteAppWidgetId") {
1235 public void run() {
1236 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1237 }
1238 }.start();
1239 }
Winson Chung93eef082012-03-23 15:59:27 -07001240 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001241 return;
1242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001245 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1246 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001247 launcherInfo.spanX = spanXY[0];
1248 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001249 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1250 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001253 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254
1255 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001256 if (hostView == null) {
1257 // Perform actual inflation because we're live
1258 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1259 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1260 } else {
1261 // The AppWidgetHostView has already been inflated and instantiated
1262 launcherInfo.hostView = hostView;
1263 }
Romain Guycbb89e42009-06-08 15:52:54 -07001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001266 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001267 launcherInfo.notifyWidgetSizeChanged(this);
1268
Adam Cohendcd297f2013-06-18 13:13:40 -07001269 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001271
1272 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001274 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
Adam Cohended9f8d2010-11-03 13:25:16 -07001277 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1278 @Override
1279 public void onReceive(Context context, Intent intent) {
1280 final String action = intent.getAction();
1281 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1282 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001283 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001285
Winson Chungc100e8e2011-08-09 16:02:43 -07001286 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001287 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001288 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1289 mAppsCustomizeTabHost.reset();
1290 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001291 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001292 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1293 mUserPresent = true;
1294 updateRunning();
1295 }
1296 }
1297 };
1298
1299 @Override
1300 public void onAttachedToWindow() {
1301 super.onAttachedToWindow();
1302
1303 // Listen for broadcasts related to user-presence
1304 final IntentFilter filter = new IntentFilter();
1305 filter.addAction(Intent.ACTION_SCREEN_OFF);
1306 filter.addAction(Intent.ACTION_USER_PRESENT);
1307 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001308 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001309 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 mVisible = true;
1311 }
1312
1313 @Override
1314 public void onDetachedFromWindow() {
1315 super.onDetachedFromWindow();
1316 mVisible = false;
1317
Adam Cohend113e0c2010-11-11 10:48:05 -08001318 if (mAttached) {
1319 unregisterReceiver(mReceiver);
1320 mAttached = false;
1321 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 updateRunning();
1323 }
1324
1325 public void onWindowVisibilityChanged(int visibility) {
1326 mVisible = visibility == View.VISIBLE;
1327 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001328 // The following code used to be in onResume, but it turns out onResume is called when
1329 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1330 // is a more appropriate event to handle
1331 if (mVisible) {
1332 mAppsCustomizeTabHost.onWindowVisible();
1333 if (!mWorkspaceLoading) {
1334 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001335 // We want to let Launcher draw itself at least once before we force it to build
1336 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001337 // apps is nice and speedy.
1338 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001339 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001340 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001341 if (mStarted) return;
1342 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001343 // We delay the layer building a bit in order to give
1344 // other message processing a time to run. In particular
1345 // this avoids a delay in hiding the IME if it was
1346 // currently shown, because doing that may involve
1347 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001348 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001349 final ViewTreeObserver.OnDrawListener listener = this;
1350 mWorkspace.post(new Runnable() {
1351 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001352 if (mWorkspace != null &&
1353 mWorkspace.getViewTreeObserver() != null) {
1354 mWorkspace.getViewTreeObserver().
1355 removeOnDrawListener(listener);
1356 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001357 }
1358 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001359 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001360 }
1361 });
1362 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001363 // When Launcher comes back to foreground, a different Activity might be responsible for
1364 // the app market intent, so refresh the icon
1365 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001366 clearTypedText();
1367 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001368 }
1369
1370 private void sendAdvanceMessage(long delay) {
1371 mHandler.removeMessages(ADVANCE_MSG);
1372 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1373 mHandler.sendMessageDelayed(msg, delay);
1374 mAutoAdvanceSentTime = System.currentTimeMillis();
1375 }
1376
1377 private void updateRunning() {
1378 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1379 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1380 mAutoAdvanceRunning = autoAdvanceRunning;
1381 if (autoAdvanceRunning) {
1382 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1383 sendAdvanceMessage(delay);
1384 } else {
1385 if (!mWidgetsToAdvance.isEmpty()) {
1386 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1387 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1388 }
1389 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001390 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001391 }
1392 }
1393 }
1394
1395 private final Handler mHandler = new Handler() {
1396 @Override
1397 public void handleMessage(Message msg) {
1398 if (msg.what == ADVANCE_MSG) {
1399 int i = 0;
1400 for (View key: mWidgetsToAdvance.keySet()) {
1401 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1402 final int delay = mAdvanceStagger * i;
1403 if (v instanceof Advanceable) {
1404 postDelayed(new Runnable() {
1405 public void run() {
1406 ((Advanceable) v).advance();
1407 }
1408 }, delay);
1409 }
1410 i++;
1411 }
1412 sendAdvanceMessage(mAdvanceInterval);
1413 }
1414 }
1415 };
1416
1417 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001418 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001419 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1420 if (v instanceof Advanceable) {
1421 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001422 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001423 updateRunning();
1424 }
1425 }
1426
1427 void removeWidgetToAutoAdvance(View hostView) {
1428 if (mWidgetsToAdvance.containsKey(hostView)) {
1429 mWidgetsToAdvance.remove(hostView);
1430 updateRunning();
1431 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 }
1433
Joe Onorato9c1289c2009-08-17 11:03:03 -04001434 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001435 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001436 launcherInfo.hostView = null;
1437 }
1438
Winson Chung93eef082012-03-23 15:59:27 -07001439 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1440 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1441 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001442 }
1443
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001444 public LauncherAppWidgetHost getAppWidgetHost() {
1445 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
Romain Guycbb89e42009-06-08 15:52:54 -07001447
Winson Chunga9abd0e2010-10-27 17:18:37 -07001448 public LauncherModel getModel() {
1449 return mModel;
1450 }
1451
Bjorn Bringertc459e522013-06-07 19:36:01 +01001452 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001453 getWindow().closeAllPanels();
1454
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001455 // Whatever we were doing is hereby canceled.
1456 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001457 }
1458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 @Override
1460 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001461 long startTime = 0;
1462 if (DEBUG_RESUME_TIME) {
1463 startTime = System.currentTimeMillis();
1464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 super.onNewIntent(intent);
1466
1467 // Close the menu
1468 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001469 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001470 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001471
Jamie Genniscb222e82012-10-23 15:44:26 -07001472 final boolean alreadyOnHome =
1473 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001474 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001475
Jamie Genniscb222e82012-10-23 15:44:26 -07001476 Runnable processIntent = new Runnable() {
1477 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001478 if (mWorkspace == null) {
1479 // Can be cases where mWorkspace is null, this prevents a NPE
1480 return;
1481 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001482 Folder openFolder = mWorkspace.getOpenFolder();
1483 // In all these cases, only animate if we're already on home
1484 mWorkspace.exitWidgetResizeMode();
1485 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1486 openFolder == null) {
1487 mWorkspace.moveToDefaultScreen(true);
1488 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001489
Jamie Genniscb222e82012-10-23 15:44:26 -07001490 closeFolder();
1491 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001492
Jamie Genniscb222e82012-10-23 15:44:26 -07001493 // If we are already on home, then just animate back to the workspace,
1494 // otherwise, just wait until onResume to set the state back to Workspace
1495 if (alreadyOnHome) {
1496 showWorkspace(true);
1497 } else {
1498 mOnResumeState = State.WORKSPACE;
1499 }
1500
1501 final View v = getWindow().peekDecorView();
1502 if (v != null && v.getWindowToken() != null) {
1503 InputMethodManager imm = (InputMethodManager)getSystemService(
1504 INPUT_METHOD_SERVICE);
1505 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1506 }
1507
1508 // Reset AllApps to its initial state
1509 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1510 mAppsCustomizeTabHost.reset();
1511 }
1512 }
1513 };
1514
1515 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1516 // Delay processing of the intent to allow the status bar animation to finish
1517 // first in order to avoid janky animations.
1518 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001519 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001520 // Process the intent immediately.
1521 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001522 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
Michael Jurka447bf842013-05-15 14:52:15 +02001525 if (DEBUG_RESUME_TIME) {
1526 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
1530 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001531 public void onRestoreInstanceState(Bundle state) {
1532 super.onRestoreInstanceState(state);
1533 for (int page: mSynchronouslyBoundPages) {
1534 mWorkspace.restoreInstanceStateForChild(page);
1535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537
1538 @Override
1539 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001540 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001541 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
Michael Jurka883f55b2010-10-21 15:47:14 -07001543 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001544 // We close any open folder since it will not be re-opened, and we need to make sure
1545 // this state is reflected.
1546 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 mWaitingForResult) {
1550 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1553 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001554 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1555 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1556 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
1558
1559 if (mFolderInfo != null && mWaitingForResult) {
1560 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1561 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1562 }
Michael Jurka946ad472010-07-09 18:05:18 -07001563
Winson Chung785d2eb2011-04-14 16:08:02 -07001564 // Save the current AppsCustomize tab
1565 if (mAppsCustomizeTabHost != null) {
1566 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1567 if (currentTabTag != null) {
1568 outState.putString("apps_customize_currentTab", currentTabTag);
1569 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001570 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1571 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
1575 @Override
1576 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Winson Chunge7a03942011-08-05 15:05:12 -07001579 // Remove all pending runnables
1580 mHandler.removeMessages(ADVANCE_MSG);
1581 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001582 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001583
Winson Chungcd2b0142011-06-08 16:02:26 -07001584 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001585 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001586 mModel.stopLoader();
1587 app.setLauncher(null);
1588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001590 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001592 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001594 mAppWidgetHost = null;
1595
1596 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
1598 TextKeyListener.getInstance().release();
1599
Winson Chung81b52252012-08-27 15:34:29 -07001600 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1601 // to prevent leaking Launcher activities on orientation change.
1602 if (mModel != null) {
1603 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1604 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001605
1606 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001607 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001608
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001609 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001610 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1611 mWorkspace.removeAllViews();
1612 mWorkspace = null;
1613 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001614
Michael Jurka2ecf9952012-06-18 12:52:28 -07001615 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
1617
Adam Cohena9cf38f2011-05-02 15:36:58 -07001618 public DragController getDragController() {
1619 return mDragController;
1620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 @Override
1623 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001624 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 super.startActivityForResult(intent, requestCode);
1626 }
1627
Winson Chung70d72102011-08-12 11:24:35 -07001628 /**
1629 * Indicates that we want global search for this activity by setting the globalSearch
1630 * argument for {@link #startSearch} to true.
1631 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001633 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001635
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001636 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001637
Karl Rosaen138a0412009-04-23 19:00:21 -07001638 if (initialQuery == null) {
1639 // Use any text typed in the launcher as the initial query
1640 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 if (appSearchData == null) {
1643 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001644 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
Winson Chungadf0c182012-08-23 14:59:07 -07001646 Rect sourceBounds = new Rect();
1647 if (mSearchDropTargetBar != null) {
1648 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1649 }
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Bjorn Bringertc459e522013-06-07 19:36:01 +01001651 startSearch(initialQuery, selectInitialQuery,
1652 appSearchData, sourceBounds);
1653 }
1654
1655 public void startSearch(String initialQuery,
1656 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001657 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001658 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001659 }
1660
1661 /**
1662 * Starts the global search activity. This code is a copied from SearchManager
1663 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001664 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001665 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001666 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001667 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1668 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1669 if (globalSearchActivity == null) {
1670 Log.w(TAG, "No global search activity found.");
1671 return;
1672 }
1673 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1674 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1675 intent.setComponent(globalSearchActivity);
1676 // Make sure that we have a Bundle to put source in
1677 if (appSearchData == null) {
1678 appSearchData = new Bundle();
1679 } else {
1680 appSearchData = new Bundle(appSearchData);
1681 }
1682 // Set source to package name of app that starts global search, if not set already.
1683 if (!appSearchData.containsKey("source")) {
1684 appSearchData.putString("source", getPackageName());
1685 }
1686 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1687 if (!TextUtils.isEmpty(initialQuery)) {
1688 intent.putExtra(SearchManager.QUERY, initialQuery);
1689 }
1690 if (selectInitialQuery) {
1691 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1692 }
1693 intent.setSourceBounds(sourceBounds);
1694 try {
1695 startActivity(intent);
1696 } catch (ActivityNotFoundException ex) {
1697 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
1700
Winson Chung70d72102011-08-12 11:24:35 -07001701 @Override
1702 public boolean onCreateOptionsMenu(Menu menu) {
1703 if (isWorkspaceLocked()) {
1704 return false;
1705 }
1706
1707 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001708
1709 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1710 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1711 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001712 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1713 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1714 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001715 String helpUrl = getString(R.string.help_url);
1716 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001717 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1718 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1719
Winson Chung70d72102011-08-12 11:24:35 -07001720 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1721 .setIcon(android.R.drawable.ic_menu_gallery)
1722 .setAlphabeticShortcut('W');
1723 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1724 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001725 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001726 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001727 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1728 .setIcon(android.R.drawable.ic_menu_preferences)
1729 .setIntent(settings)
1730 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001731 if (!helpUrl.isEmpty()) {
1732 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1733 .setIcon(android.R.drawable.ic_menu_help)
1734 .setIntent(help)
1735 .setAlphabeticShortcut('H');
1736 }
Winson Chung70d72102011-08-12 11:24:35 -07001737 return true;
1738 }
1739
1740 @Override
1741 public boolean onPrepareOptionsMenu(Menu menu) {
1742 super.onPrepareOptionsMenu(menu);
1743
1744 if (mAppsCustomizeTabHost.isTransitioning()) {
1745 return false;
1746 }
1747 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1748 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1749
1750 return true;
1751 }
1752
1753 @Override
1754 public boolean onOptionsItemSelected(MenuItem item) {
1755 switch (item.getItemId()) {
1756 case MENU_WALLPAPER_SETTINGS:
1757 startWallpaper();
1758 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001759 }
1760
1761 return super.onOptionsItemSelected(item);
1762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001764 @Override
1765 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001766 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001767 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001768 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001769 }
1770
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 public boolean isWorkspaceLocked() {
1772 return mWorkspaceLoading || mWaitingForResult;
1773 }
1774
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001776 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001777 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001778 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1779 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001780 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001781 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001782 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001783
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001784 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1785 AppWidgetProviderInfo appWidgetInfo) {
1786 if (appWidgetInfo.configure != null) {
1787 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001788
Bjorn Bringert7984c942009-12-09 15:38:25 +00001789 // Launch over to configure widget, if needed
1790 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001791 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001792 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001793 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001795 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001796 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001797 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001798 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001799 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
1801 }
Romain Guycbb89e42009-06-08 15:52:54 -07001802
Adam Cohenfbba09b2011-07-18 21:43:05 -07001803 /**
1804 * Process a shortcut drop.
1805 *
1806 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001807 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001808 * @param cell The cell it should be added to, optional
1809 * @param position The location on the screen where it was dropped, optional
1810 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001811 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001812 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001814 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001815 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001816 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001817
1818 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001819 mPendingAddInfo.cellX = cell[0];
1820 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001821 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822
1823 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1824 createShortcutIntent.setComponent(componentName);
1825 processShortcut(createShortcutIntent);
1826 }
1827
Adam Cohenfbba09b2011-07-18 21:43:05 -07001828 /**
1829 * Process a widget drop.
1830 *
1831 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001832 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 * @param cell The cell it should be added to, optional
1834 * @param position The location on the screen where it was dropped, optional
1835 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001836 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001837 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001838 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001840 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001842 mPendingAddInfo.minSpanX = info.minSpanX;
1843 mPendingAddInfo.minSpanY = info.minSpanY;
1844
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 mPendingAddInfo.cellX = cell[0];
1847 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001848 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001849 if (span != null) {
1850 mPendingAddInfo.spanX = span[0];
1851 mPendingAddInfo.spanY = span[1];
1852 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001853
Adam Cohened66b2b2012-01-23 17:28:51 -08001854 AppWidgetHostView hostView = info.boundWidget;
1855 int appWidgetId;
1856 if (hostView != null) {
1857 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001858 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001859 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001860 // In this case, we either need to start an activity to get permission to bind
1861 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001862 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001863 Bundle options = info.bindOptions;
1864
1865 boolean success = false;
1866 if (options != null) {
1867 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1868 info.componentName, options);
1869 } else {
1870 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1871 info.componentName);
1872 }
1873 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001874 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001875 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001876 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001877 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1878 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1879 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001880 // TODO: we need to make sure that this accounts for the options bundle.
1881 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001882 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1883 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001884 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001885 }
1886
Joe Onoratodeb98af2010-02-19 14:59:39 -08001887 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001888 // Handle case where user selected "Applications"
1889 String applicationName = getResources().getString(R.string.group_applications);
1890 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001891
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001892 if (applicationName != null && applicationName.equals(shortcutName)) {
1893 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1894 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001895
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001896 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1897 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001898 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001899 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001901 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904
Winson Chung24ab2f12010-09-16 14:10:47 -07001905 void processWallpaper(Intent intent) {
1906 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1907 }
1908
Adam Cohendcd297f2013-06-18 13:13:40 -07001909 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001911 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 folderInfo.title = getText(R.string.folder_name);
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001915 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001917 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918
1919 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001920 FolderIcon newFolder =
1921 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001924 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
Romain Guycbb89e42009-06-08 15:52:54 -07001926
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001928 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001934 Intent chooser = Intent.createChooser(pickWallpaper,
1935 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001936 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1937 // Removed in Eclair MR1
1938// WallpaperManager wm = (WallpaperManager)
1939// getSystemService(Context.WALLPAPER_SERVICE);
1940// WallpaperInfo wi = wm.getWallpaperInfo();
1941// if (wi != null && wi.getSettingsActivity() != null) {
1942// LabeledIntent li = new LabeledIntent(getPackageName(),
1943// R.string.configure_wallpaper, 0);
1944// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1945// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1946// }
Mike Clerona0618e42009-10-22 13:55:21 -07001947 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001950 /**
1951 * Registers various content observers. The current implementation registers
1952 * only a favorites observer to keep track of the favorites applications.
1953 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001954 private void registerContentObservers() {
1955 ContentResolver resolver = getContentResolver();
1956 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1957 true, mWidgetObserver);
1958 }
1959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 @Override
1961 public boolean dispatchKeyEvent(KeyEvent event) {
1962 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1963 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001965 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001966 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001967 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001968 dumpState();
1969 return true;
1970 }
1971 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001972 }
1973 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1974 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001975 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 return true;
1977 }
1978 }
1979
1980 return super.dispatchKeyEvent(event);
1981 }
1982
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 @Override
1984 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001985 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001986 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001987 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001988 Folder openFolder = mWorkspace.getOpenFolder();
1989 if (openFolder.isEditingName()) {
1990 openFolder.dismissEditingName();
1991 } else {
1992 closeFolder();
1993 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001994 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001995 mWorkspace.exitWidgetResizeMode();
1996
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001997 // Back button is a no-op here, but give at least some feedback for the button press
1998 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001999 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002003 * Re-listen when widgets are reset.
2004 */
2005 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002006 if (mAppWidgetHost != null) {
2007 mAppWidgetHost.startListening();
2008 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002009 }
2010
2011 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 * Launches the intent referred by the clicked shortcut.
2013 *
2014 * @param v The view representing the clicked shortcut.
2015 */
2016 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002017 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2018 // view has detached (it's possible for this to happen if the view is removed mid touch).
2019 if (v.getWindowToken() == null) {
2020 return;
2021 }
2022
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002023 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002024 return;
2025 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002028 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002030 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002031
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002032 // Check for special shortcuts
2033 if (intent.getComponent() != null) {
2034 final String shortcutClass = intent.getComponent().getClassName();
2035
2036 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2037 showAllApps(true);
2038 return;
2039 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2040 MemoryDumpActivity.startDump(this);
2041 return;
2042 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002043 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002044
2045 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002046 int[] pos = new int[2];
2047 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002048 intent.setSourceBounds(new Rect(pos[0], pos[1],
2049 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002050
2051 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002052
2053 if (success && v instanceof BubbleTextView) {
2054 mWaitingForResume = (BubbleTextView) v;
2055 mWaitingForResume.setStayPressed(true);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002058 if (v instanceof FolderIcon) {
2059 FolderIcon fi = (FolderIcon) v;
2060 handleFolderClick(fi);
2061 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002062 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002063 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002064 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002065 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002066 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069 }
2070
Michael Jurka0e260592010-06-30 17:07:39 -07002071 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002072 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002073 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002074 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2075 showWorkspace(true);
2076 }
Michael Jurka0e260592010-06-30 17:07:39 -07002077 return false;
2078 }
2079
Michael Jurkaaf442092010-06-10 17:01:57 -07002080 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002081 * Event handler for the search button
2082 *
2083 * @param v The view that was clicked.
2084 */
2085 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002086 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2087
Amith Yamasania135ba82011-08-09 17:42:01 -07002088 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002089 }
2090
2091 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002092 * Event handler for the voice button
2093 *
2094 * @param v The view that was clicked.
2095 */
2096 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002097 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002098
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 startVoice();
2100 }
2101
2102 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002103 try {
2104 final SearchManager searchManager =
2105 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2106 ComponentName activityName = searchManager.getGlobalSearchActivity();
2107 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002108 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002109 if (activityName != null) {
2110 intent.setPackage(activityName.getPackageName());
2111 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002112 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002113 } catch (ActivityNotFoundException e) {
2114 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002115 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002116 startActivitySafely(null, intent, "onClickVoiceButton");
2117 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002118 }
2119
2120 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002121 * Event handler for the "grid" button that appears on the home screen, which
2122 * enters all apps mode.
2123 *
2124 * @param v The view that was clicked.
2125 */
2126 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002127 showAllApps(true);
2128 }
2129
2130 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002131 // Provide the same haptic feedback that the system offers for virtual keys.
2132 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002133 }
2134
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002135 public void onClickAppMarketButton(View v) {
2136 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002137 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002138 } else {
2139 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002140 }
2141 }
2142
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002143 void startApplicationDetailsActivity(ComponentName componentName) {
2144 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002145 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2146 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002147 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002148 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002149 }
2150
Patrick Dubroy5539af72010-09-07 15:22:01 -07002151 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2152 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2153 // System applications cannot be installed. For now, show a toast explaining that.
2154 // We may give them the option of disabling apps this way.
2155 int messageId = R.string.uninstall_system_app_text;
2156 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2157 } else {
2158 String packageName = appInfo.componentName.getPackageName();
2159 String className = appInfo.componentName.getClassName();
2160 Intent intent = new Intent(
2161 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002162 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2163 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002164 startActivity(intent);
2165 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002166 }
2167
Michael Jurka86a720e2012-05-09 11:23:23 -07002168 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002172 // Only launch using the new animation if the shortcut has not opted out (this is a
2173 // private contract between launcher and may be ignored in the future).
2174 boolean useLaunchAnimation = (v != null) &&
2175 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2176 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002177 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2178 v.getMeasuredWidth(), v.getMeasuredHeight());
2179
2180 startActivity(intent, opts.toBundle());
2181 } else {
2182 startActivity(intent);
2183 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002184 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 } catch (SecurityException e) {
2186 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002187 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002189 "or use the exported attribute for this activity. "
2190 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002192 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002194
Michael Jurka86a720e2012-05-09 11:23:23 -07002195 boolean startActivitySafely(View v, Intent intent, Object tag) {
2196 boolean success = false;
2197 try {
2198 success = startActivity(v, intent, tag);
2199 } catch (ActivityNotFoundException e) {
2200 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2201 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2202 }
2203 return success;
2204 }
2205
Romain Guy8e633c52010-03-04 12:51:36 -08002206 void startActivityForResultSafely(Intent intent, int requestCode) {
2207 try {
2208 startActivityForResult(intent, requestCode);
2209 } catch (ActivityNotFoundException e) {
2210 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2211 } catch (SecurityException e) {
2212 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2213 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2214 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2215 "or use the exported attribute for this activity.", e);
2216 }
2217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218
Adam Cohena9cf38f2011-05-02 15:36:58 -07002219 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002220 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002221 Folder openFolder = mWorkspace.getFolderForTag(info);
2222
2223 // If the folder info reports that the associated folder is open, then verify that
2224 // it is actually opened. There have been a few instances where this gets out of sync.
2225 if (info.opened && openFolder == null) {
2226 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002227 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002228 info.opened = false;
2229 }
2230
Adam Cohenfb91f302012-06-11 15:45:18 -07002231 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 // Close any open folder
2233 closeFolder();
2234 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002235 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 } else {
2237 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 int folderScreen;
2239 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002240 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 // .. and close it
2242 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002243 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 // Close any folder open on the current screen
2245 closeFolder();
2246 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002247 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
2250 }
2251 }
2252
Adam Cohen268c4752012-06-06 17:47:33 -07002253 /**
2254 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2255 * in the DragLayer in the exact absolute location of the original FolderIcon.
2256 */
2257 private void copyFolderIconToImage(FolderIcon fi) {
2258 final int width = fi.getMeasuredWidth();
2259 final int height = fi.getMeasuredHeight();
2260
2261 // Lazy load ImageView, Bitmap and Canvas
2262 if (mFolderIconImageView == null) {
2263 mFolderIconImageView = new ImageView(this);
2264 }
2265 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2266 mFolderIconBitmap.getHeight() != height) {
2267 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2268 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2269 }
2270
2271 DragLayer.LayoutParams lp;
2272 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2273 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2274 } else {
2275 lp = new DragLayer.LayoutParams(width, height);
2276 }
2277
Adam Cohen307fe232012-08-16 17:55:58 -07002278 // The layout from which the folder is being opened may be scaled, adjust the starting
2279 // view size by this scale factor.
2280 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002281 lp.customPosition = true;
2282 lp.x = mRectForFolderAnimation.left;
2283 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002284 lp.width = (int) (scale * width);
2285 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002286
2287 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2288 fi.draw(mFolderIconCanvas);
2289 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002290 if (fi.getFolder() != null) {
2291 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2292 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002293 }
Adam Cohen268c4752012-06-06 17:47:33 -07002294 // Just in case this image view is still in the drag layer from a previous animation,
2295 // we remove it and re-add it.
2296 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2297 mDragLayer.removeView(mFolderIconImageView);
2298 }
2299 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002300 if (fi.getFolder() != null) {
2301 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002302 }
Adam Cohen268c4752012-06-06 17:47:33 -07002303 }
2304
Adam Cohen2801caf2011-05-13 20:57:39 -07002305 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002306 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002307 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2308 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2309 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2310
Adam Cohenc51934b2011-07-26 21:07:43 -07002311 FolderInfo info = (FolderInfo) fi.getTag();
2312 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2313 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002314 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2315 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002316 }
2317
Adam Cohen268c4752012-06-06 17:47:33 -07002318 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2319 copyFolderIconToImage(fi);
2320 fi.setVisibility(View.INVISIBLE);
2321
Michael Jurka2ecf9952012-06-18 12:52:28 -07002322 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002323 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002324 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2325 oa.start();
2326 }
2327
Adam Cohen268c4752012-06-06 17:47:33 -07002328 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002329 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002330 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2331 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2332 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2333
Adam Cohen268c4752012-06-06 17:47:33 -07002334 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002335
Adam Cohen268c4752012-06-06 17:47:33 -07002336 // We remove and re-draw the FolderIcon in-case it has changed
2337 mDragLayer.removeView(mFolderIconImageView);
2338 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002339 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002340 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002341 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002342 oa.addListener(new AnimatorListenerAdapter() {
2343 @Override
2344 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002345 if (cl != null) {
2346 cl.clearFolderLeaveBehind();
2347 // Remove the ImageView copy of the FolderIcon and make the original visible.
2348 mDragLayer.removeView(mFolderIconImageView);
2349 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002350 }
2351 }
2352 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002353 oa.start();
2354 }
2355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002357 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 * is animated relative to the specified View. If the View is null, no animation
2359 * is played.
2360 *
2361 * @param folderInfo The FolderInfo describing the folder to open.
2362 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002363 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002364 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002365 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366
Adam Cohena9cf38f2011-05-02 15:36:58 -07002367 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368
Adam Cohen4554ee12011-08-03 16:13:21 -07002369 // Just verify that the folder hasn't already been added to the DragLayer.
2370 // There was a one-off crash where the folder had a parent already.
2371 if (folder.getParent() == null) {
2372 mDragLayer.addView(folder);
2373 mDragController.addDropTarget((DropTarget) folder);
2374 } else {
2375 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2376 folder.getParent() + ").");
2377 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002378 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002379 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002380
2381 // Notify the accessibility manager that this folder "window" has appeared and occluded
2382 // the workspace items
2383 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2384 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002385 }
2386
2387 public void closeFolder() {
2388 Folder folder = mWorkspace.getOpenFolder();
2389 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002390 if (folder.isEditingName()) {
2391 folder.dismissEditingName();
2392 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002393 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002394
2395 // Dismiss the folder cling
2396 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002397 }
2398 }
2399
2400 void closeFolder(Folder folder) {
2401 folder.getInfo().opened = false;
2402
2403 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2404 if (parent != null) {
2405 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2406 shrinkAndFadeInFolderIcon(fi);
2407 }
2408 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002409
2410 // Notify the accessibility manager that this folder "window" has disappeard and no
2411 // longer occludeds the workspace items
2412 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 }
2414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002416 if (!isDraggingEnabled()) return false;
2417 if (isWorkspaceLocked()) return false;
2418 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419
2420 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002421 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 }
2423
Michael Jurka0280c3b2010-09-17 15:00:07 -07002424 resetAddInfo();
2425 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002427 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 return true;
2429 }
2430
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 // The hotseat touch handling does not go through Workspace, and we always allow long press
2432 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002434 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2435 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002436 if (itemUnderLongClick == null) {
2437 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002438 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2439 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002440 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002442 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002444 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 }
2446 }
2447 }
2448 return true;
2449 }
2450
Winson Chung3d503fb2011-07-13 17:25:49 -07002451 boolean isHotseatLayout(View layout) {
2452 return mHotseat != null && layout != null &&
2453 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2454 }
2455 Hotseat getHotseat() {
2456 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002457 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002458 SearchDropTargetBar getSearchBar() {
2459 return mSearchDropTargetBar;
2460 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002461
Winson Chung3d503fb2011-07-13 17:25:49 -07002462 /**
2463 * Returns the CellLayout of the specified container at the specified screen.
2464 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002465 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002466 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2467 if (mHotseat != null) {
2468 return mHotseat.getLayout();
2469 } else {
2470 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002471 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002472 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002473 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002474 }
2475 }
2476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 Workspace getWorkspace() {
2478 return mWorkspace;
2479 }
2480
Daniel Sandler843e8602010-06-07 14:59:01 -04002481 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002482 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002483 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002484 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002485 }
2486
Craig Mautner360310b2012-10-26 15:13:08 -07002487 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002488 public boolean isAllAppsButtonRank(int rank) {
2489 return mHotseat.isAllAppsButtonRank(rank);
2490 }
2491
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 /**
2493 * Helper method for the cameraZoomIn/cameraZoomOut animations
2494 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 * @param scaleFactor The scale factor used for the zoom
2496 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002497 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002499 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002500 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002501
Winson Chung18f41f82012-05-09 13:28:10 -07002502 void disableWallpaperIfInAllApps() {
2503 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002504 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002505 if (mAppsCustomizeTabHost != null &&
2506 !mAppsCustomizeTabHost.isTransitioning()) {
2507 updateWallpaperVisibility(false);
2508 }
2509 }
2510 }
2511
Craig Mautner360310b2012-10-26 15:13:08 -07002512 private void setWorkspaceBackground(boolean workspace) {
2513 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002514 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002515 }
2516
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002517 void updateWallpaperVisibility(boolean visible) {
2518 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2519 int curflags = getWindow().getAttributes().flags
2520 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2521 if (wpflags != curflags) {
2522 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2523 }
Craig Mautner360310b2012-10-26 15:13:08 -07002524 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002525 }
2526
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002527 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2528 if (v instanceof LauncherTransitionable) {
2529 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2530 }
2531 }
2532
Michael Jurkabed61d22012-02-14 22:51:29 -08002533 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2534 if (v instanceof LauncherTransitionable) {
2535 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2536 }
Winson Chung70442722012-02-10 15:43:22 -08002537
2538 // Update the workspace transition step as well
2539 dispatchOnLauncherTransitionStep(v, 0f);
2540 }
2541
2542 private void dispatchOnLauncherTransitionStep(View v, float t) {
2543 if (v instanceof LauncherTransitionable) {
2544 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2545 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002546 }
2547
2548 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2549 if (v instanceof LauncherTransitionable) {
2550 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2551 }
Winson Chung70442722012-02-10 15:43:22 -08002552
2553 // Update the workspace transition step as well
2554 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002555 }
2556
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002557 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002558 * Things to test when changing the following seven functions.
2559 * - Home from workspace
2560 * - from center screen
2561 * - from other screens
2562 * - Home from all apps
2563 * - from center screen
2564 * - from other screens
2565 * - Back from all apps
2566 * - from center screen
2567 * - from other screens
2568 * - Launch app from workspace and quit
2569 * - with back
2570 * - with home
2571 * - Launch app from all apps and quit
2572 * - with back
2573 * - with home
2574 * - Go to a screen that's not the default, then all
2575 * apps, and launch and app, and go back
2576 * - with back
2577 * -with home
2578 * - On workspace, long press power and go back
2579 * - with back
2580 * - with home
2581 * - On all apps, long press power and go back
2582 * - with back
2583 * - with home
2584 * - On workspace, power off
2585 * - On all apps, power off
2586 * - Launch an app and turn off the screen while in that app
2587 * - Go back with home key
2588 * - Go back with back key TODO: make this not go to workspace
2589 * - From all apps
2590 * - From workspace
2591 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2592 * - From all apps
2593 * - From the center workspace
2594 * - From another workspace
2595 */
2596
2597 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 * Zoom the camera out from the workspace to reveal 'toView'.
2599 * Assumes that the view to show is anchored at either the very top or very bottom
2600 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002601 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002602 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002604 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002605 mStateAnimation.cancel();
2606 mStateAnimation = null;
2607 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002608 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002609
Winson Chungf0ea4d32011-06-06 14:27:16 -07002610 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2611 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2612 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002613 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002614 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002615 final int startDelay =
2616 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002617
Michael Jurkab3e22d92011-10-31 15:58:33 -07002618 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619
Michael Jurkad74c9842011-07-10 12:44:21 -07002620 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002621 Animator workspaceAnim =
2622 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623
2624 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002625 toView.setScaleX(scale);
2626 toView.setScaleY(scale);
2627 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2628 scaleAnim.
2629 scaleX(1f).scaleY(1f).
2630 setDuration(duration).
2631 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002632
Winson Chungf0ea4d32011-06-06 14:27:16 -07002633 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002634 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002635 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002636 .ofFloat(toView, "alpha", 0f, 1f)
2637 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002638 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002639 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2640 @Override
2641 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002642 if (animation == null) {
2643 throw new RuntimeException("animation is null");
2644 }
Winson Chung70442722012-02-10 15:43:22 -08002645 float t = (Float) animation.getAnimatedValue();
2646 dispatchOnLauncherTransitionStep(fromView, t);
2647 dispatchOnLauncherTransitionStep(toView, t);
2648 }
2649 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002650
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002651 // toView should appear right at the end of the workspace shrink
2652 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002653 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002654 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002655 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002656
2657 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002658 boolean animationCancelled = false;
2659
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002660 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002661 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002662 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002663 // Prepare the position
2664 toView.setTranslationX(0.0f);
2665 toView.setTranslationY(0.0f);
2666 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002667 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002668 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002669 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002670 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002671 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2672 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002673
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002674 if (mWorkspace != null && !springLoaded && !LauncherAppState.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002675 // Hide the workspace scrollbar
2676 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002677 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002678 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002679 if (!animationCancelled) {
2680 updateWallpaperVisibility(false);
2681 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002682
2683 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002684 if (mSearchDropTargetBar != null) {
2685 mSearchDropTargetBar.hideSearchBar(false);
2686 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002687 }
2688
Adam Cohencff6af82011-09-13 14:51:53 -07002689 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002690 public void onAnimationCancel(Animator animation) {
2691 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002692 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693 });
2694
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002695 if (workspaceAnim != null) {
2696 mStateAnimation.play(workspaceAnim);
2697 }
Michael Jurka1899a362011-11-03 13:50:45 -07002698
2699 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002700
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002701 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2702 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703
2704 // If any of the objects being animated haven't been measured/laid out
2705 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002706 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002707 (mWorkspace.getMeasuredWidth() == 0) ||
2708 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002710 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002711
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002712 final AnimatorSet stateAnimation = mStateAnimation;
2713 final Runnable startAnimRunnable = new Runnable() {
2714 public void run() {
2715 // Check that mStateAnimation hasn't changed while
2716 // we waited for a layout/draw pass
2717 if (mStateAnimation != stateAnimation)
2718 return;
2719 setPivotsForZoom(toView, scale);
2720 dispatchOnLauncherTransitionStart(fromView, animated, false);
2721 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002722 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002723 }
2724 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002725 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002726 final ViewTreeObserver observer = toView.getViewTreeObserver();
2727 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2728 public void onGlobalLayout() {
2729 startAnimRunnable.run();
2730 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2731 }
2732 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002733 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002734 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002735 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736 } else {
2737 toView.setTranslationX(0.0f);
2738 toView.setTranslationY(0.0f);
2739 toView.setScaleX(1.0f);
2740 toView.setScaleY(1.0f);
2741 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002742 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002743
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002744 if (!springLoaded && !LauncherAppState.isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002745 // Hide the workspace scrollbar
2746 mWorkspace.hideScrollingIndicator(true);
2747 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002748
2749 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002750 if (mSearchDropTargetBar != null) {
2751 mSearchDropTargetBar.hideSearchBar(false);
2752 }
Michael Jurkaabded662011-03-04 12:06:57 -08002753 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002754 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002755 dispatchOnLauncherTransitionStart(fromView, animated, false);
2756 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002757 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 dispatchOnLauncherTransitionStart(toView, animated, false);
2759 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002760 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002761 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002762 }
2763
2764 /**
2765 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002766 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002767 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002768 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002769 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2770 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002771
Michael Jurkab3e22d92011-10-31 15:58:33 -07002772 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002773 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002774 mStateAnimation.cancel();
2775 mStateAnimation = null;
2776 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002777 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002778
Winson Chungf0ea4d32011-06-06 14:27:16 -07002779 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002780 final int fadeOutDuration =
2781 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final float scaleFactor = (float)
2783 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2784 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002785 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002786 Animator workspaceAnim = null;
2787
2788 if (toState == State.WORKSPACE) {
2789 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2790 workspaceAnim = mWorkspace.getChangeStateAnimation(
2791 Workspace.State.NORMAL, animated, stagger);
2792 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2793 workspaceAnim = mWorkspace.getChangeStateAnimation(
2794 Workspace.State.SPRING_LOADED, animated);
2795 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002796
Michael Jurkab3e22d92011-10-31 15:58:33 -07002797 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002798 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002799 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002801 final LauncherViewPropertyAnimator scaleAnim =
2802 new LauncherViewPropertyAnimator(fromView);
2803 scaleAnim.
2804 scaleX(scaleFactor).scaleY(scaleFactor).
2805 setDuration(duration).
2806 setInterpolator(new Workspace.ZoomInInterpolator());
2807
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002808 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002809 .ofFloat(fromView, "alpha", 1f, 0f)
2810 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002811 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002812 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2813 @Override
2814 public void onAnimationUpdate(ValueAnimator animation) {
2815 float t = 1f - (Float) animation.getAnimatedValue();
2816 dispatchOnLauncherTransitionStep(fromView, t);
2817 dispatchOnLauncherTransitionStep(toView, t);
2818 }
2819 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002820
Michael Jurka2ecf9952012-06-18 12:52:28 -07002821 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002822
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002823 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2824 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002825 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002826
2827 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002828 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002829 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002830 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002831 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002832 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2833 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002834 if (mWorkspace != null) {
2835 mWorkspace.hideScrollingIndicator(false);
2836 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002837 if (onCompleteRunnable != null) {
2838 onCompleteRunnable.run();
2839 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002840 mAppsCustomizeContent.updateCurrentPageScroll();
2841 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002842 }
2843 });
2844
Winson Chungf0ea4d32011-06-06 14:27:16 -07002845 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 if (workspaceAnim != null) {
2847 mStateAnimation.play(workspaceAnim);
2848 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002849 dispatchOnLauncherTransitionStart(fromView, animated, true);
2850 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002851 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002852 } else {
2853 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002855 dispatchOnLauncherTransitionStart(fromView, animated, true);
2856 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 dispatchOnLauncherTransitionStart(toView, animated, true);
2859 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002860 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 }
2863
Michael Jurkae326f182011-11-21 14:05:46 -08002864 @Override
2865 public void onTrimMemory(int level) {
2866 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002867 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002868 mAppsCustomizeTabHost.onTrimMemory();
2869 }
2870 }
2871
Winson Chung18f41f82012-05-09 13:28:10 -07002872 @Override
2873 public void onWindowFocusChanged(boolean hasFocus) {
2874 if (!hasFocus) {
2875 // When another window occludes launcher (like the notification shade, or recents),
2876 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2877 updateWallpaperVisibility(true);
2878 } else {
2879 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002880 mWorkspace.postDelayed(new Runnable() {
2881 @Override
2882 public void run() {
2883 disableWallpaperIfInAllApps();
2884 }
2885 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002886 }
2887 }
2888
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002889 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002890 showWorkspace(animated, null);
2891 }
2892
2893 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002895 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002896 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002897 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002898
Winson Chungc7d2b602012-05-16 17:02:20 -07002899 // Show the search bar (only animate if we were showing the drop target bar in spring
2900 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002901 if (mSearchDropTargetBar != null) {
2902 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2903 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002904
Michael Jurkab737ee62011-11-15 15:57:22 -08002905 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002906 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907
2908 // Set focus to the AppsCustomize button
2909 if (mAllAppsButton != null) {
2910 mAllAppsButton.requestFocus();
2911 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002912 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002913
Michael Jurkab737ee62011-11-15 15:57:22 -08002914 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002915
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002916 // Change the state *after* we've called all the transition code
2917 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002918
Winson Chung5fb63472011-02-02 17:03:37 -08002919 // Resume the auto-advance of widgets
2920 mUserPresent = true;
2921 updateRunning();
2922
alanv1d4fde62012-10-17 13:15:47 -07002923 // Send an accessibility event to announce the context change
2924 getWindow().getDecorView()
2925 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002926
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002927 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002928 }
2929
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002930 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002931 }
2932
Michael Jurkab3e22d92011-10-31 15:58:33 -07002933 void showAllApps(boolean animated) {
2934 if (mState != State.WORKSPACE) return;
2935
2936 showAppsCustomizeHelper(animated, false);
2937 mAppsCustomizeTabHost.requestFocus();
2938
Michael Jurkab3e22d92011-10-31 15:58:33 -07002939 // Change the state *after* we've called all the transition code
2940 mState = State.APPS_CUSTOMIZE;
2941
2942 // Pause the auto-advance of widgets until we are out of AllApps
2943 mUserPresent = false;
2944 updateRunning();
2945 closeFolder();
2946
2947 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002948 getWindow().getDecorView()
2949 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002950 }
2951
Adam Cohen7777d962011-08-18 18:58:38 -07002952 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002953 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002954 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002955 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002956 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002957 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002958 }
Adam Cohen7777d962011-08-18 18:58:38 -07002959
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2961 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002962 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2963
Winson Chunge7a03942011-08-05 15:05:12 -07002964 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002965 @Override
2966 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002967 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002968 // Before we show workspace, hide all apps again because
2969 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2970 // clean up our state transition functions
2971 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002972 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002973 } else {
2974 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002975 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002976 }
2977 }, (extendedDelay ?
2978 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2979 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2980 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002981
Michael Jurkad3ef3062010-11-23 16:23:58 -08002982 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002983 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984 final boolean animated = true;
2985 final boolean springLoaded = true;
2986 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002987 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002988 }
2989 // Otherwise, we are not in spring loaded mode, so don't do anything.
2990 }
2991
Michael Jurkab737ee62011-11-15 15:57:22 -08002992 void hideDockDivider() {
2993 if (mQsbDivider != null && mDockDivider != null) {
2994 mQsbDivider.setVisibility(View.INVISIBLE);
2995 mDockDivider.setVisibility(View.INVISIBLE);
2996 }
2997 }
2998
2999 void showDockDivider(boolean animated) {
3000 if (mQsbDivider != null && mDockDivider != null) {
3001 mQsbDivider.setVisibility(View.VISIBLE);
3002 mDockDivider.setVisibility(View.VISIBLE);
3003 if (mDividerAnimator != null) {
3004 mDividerAnimator.cancel();
3005 mQsbDivider.setAlpha(1f);
3006 mDockDivider.setAlpha(1f);
3007 mDividerAnimator = null;
3008 }
3009 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003010 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3011 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3012 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003013 int duration = 0;
3014 if (mSearchDropTargetBar != null) {
3015 duration = mSearchDropTargetBar.getTransitionInDuration();
3016 }
3017 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003018 mDividerAnimator.start();
3019 }
3020 }
3021 }
3022
Michael Jurkab3e22d92011-10-31 15:58:33 -07003023 void lockAllApps() {
3024 // TODO
3025 }
3026
3027 void unlockAllApps() {
3028 // TODO
3029 }
3030
Winson Chungf0ea4d32011-06-06 14:27:16 -07003031 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003032 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 void showHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003035 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003037 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003038 int duration = 0;
3039 if (mSearchDropTargetBar != null) {
3040 duration = mSearchDropTargetBar.getTransitionInDuration();
3041 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003042 mHotseat.animate().alpha(1f).setDuration(duration);
3043 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003045 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046 }
3047 }
3048 }
3049
3050 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003051 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003052 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003053 void hideHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003054 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003056 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003057 int duration = 0;
3058 if (mSearchDropTargetBar != null) {
3059 duration = mSearchDropTargetBar.getTransitionOutDuration();
3060 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003061 mHotseat.animate().alpha(0f).setDuration(duration);
3062 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003064 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003065 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003066 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003067 }
3068
Patrick Dubroy5f445422011-02-18 14:35:21 -08003069 /**
3070 * Add an item from all apps or customize onto the given workspace screen.
3071 * If layout is null, add to the current screen.
3072 */
3073 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003074 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003075 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003076 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003077 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003078
Winson Chungdff8ebb2011-09-08 17:25:31 -07003079 /** Maps the current orientation to an index for referencing orientation correct global icons */
3080 private int getCurrentOrientationIndexForGlobalIcons() {
3081 // default - 0, landscape - 1
3082 switch (getResources().getConfiguration().orientation) {
3083 case Configuration.ORIENTATION_LANDSCAPE:
3084 return 1;
3085 default:
3086 return 0;
3087 }
3088 }
3089
Michael Jurkaae65ee32012-04-30 11:45:54 -07003090 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003092 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 // Look for the toolbar icon specified in the activity meta-data
3094 Bundle metaData = packageManager.getActivityInfo(
3095 activityName, PackageManager.GET_META_DATA).metaData;
3096 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003097 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 if (iconResId != 0) {
3099 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003100 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 }
3102 }
3103 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003104 // This can happen if the activity defines an invalid drawable
3105 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3106 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003107 } catch (Resources.NotFoundException nfe) {
3108 // This can happen if the activity defines an invalid drawable
3109 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3110 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003111 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003112 return null;
3113 }
3114
3115 // if successful in getting icon, return it; otherwise, set button to use default drawable
3116 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003117 int buttonId, ComponentName activityName, int fallbackDrawableId,
3118 String toolbarResourceName) {
3119 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003120 Resources r = getResources();
3121 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3122 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003124 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003125 // If we were unable to find the icon via the meta-data, use a generic one
3126 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003127 toolbarIcon = r.getDrawable(fallbackDrawableId);
3128 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003129 if (button != null) {
3130 button.setCompoundDrawables(toolbarIcon, null, null, null);
3131 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 return null;
3133 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003134 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003135 if (button != null) {
3136 button.setCompoundDrawables(toolbarIcon, null, null, null);
3137 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003138 return toolbarIcon.getConstantState();
3139 }
3140 }
3141
3142 // if successful in getting icon, return it; otherwise, set button to use default drawable
3143 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 int buttonId, ComponentName activityName, int fallbackDrawableId,
3145 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003147 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003148
Michael Jurka19e0fc52011-07-22 18:00:21 -07003149 if (button != null) {
3150 // If we were unable to find the icon via the meta-data, use a
3151 // generic one
3152 if (toolbarIcon == null) {
3153 button.setImageResource(fallbackDrawableId);
3154 } else {
3155 button.setImageDrawable(toolbarIcon);
3156 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003158
3159 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3160
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 }
3162
Winson Chung1b884092012-06-08 17:13:02 -07003163 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003164 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003165 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 }
3167
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003168 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003169 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 }
3172
Winson Chungbb185bd2011-11-21 12:31:42 -08003173 private void invalidatePressedFocusedStates(View container, View button) {
3174 if (container instanceof HolographicLinearLayout) {
3175 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3176 layout.invalidatePressedFocusedStates();
3177 } else if (button instanceof HolographicImageView) {
3178 HolographicImageView view = (HolographicImageView) button;
3179 view.invalidatePressedFocusedStates();
3180 }
3181 }
3182
Winson Chungc51db6a2011-10-05 11:44:49 -07003183 private boolean updateGlobalSearchIcon() {
3184 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003185 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003187 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003188
Winson Chung1cad91e2011-05-25 17:41:01 -07003189 final SearchManager searchManager =
3190 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3191 ComponentName activityName = searchManager.getGlobalSearchActivity();
3192 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003193 int coi = getCurrentOrientationIndexForGlobalIcons();
3194 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3196 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3197 if (sGlobalSearchIcon[coi] == null) {
3198 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3199 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3200 TOOLBAR_ICON_METADATA_NAME);
3201 }
3202
Winson Chungc51db6a2011-10-05 11:44:49 -07003203 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3204 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003205 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003207 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003208 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003209 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3210 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3211 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003212 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003213 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003214 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003215 }
3216 }
3217
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003218 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003219 final View searchButtonContainer = findViewById(R.id.search_button_container);
3220 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003221 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003222 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003223 }
3224
Winson Chungc51db6a2011-10-05 11:44:49 -07003225 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003226 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003227 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003228
Winson Chungc51db6a2011-10-05 11:44:49 -07003229 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003230 final SearchManager searchManager =
3231 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3232 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3233
3234 ComponentName activityName = null;
3235 if (globalSearchActivity != null) {
3236 // Check if the global search activity handles voice search
3237 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3238 intent.setPackage(globalSearchActivity.getPackageName());
3239 activityName = intent.resolveActivity(getPackageManager());
3240 }
3241
3242 if (activityName == null) {
3243 // Fallback: check if an activity other than the global search activity
3244 // resolves this
3245 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3246 activityName = intent.resolveActivity(getPackageManager());
3247 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003248 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003249 int coi = getCurrentOrientationIndexForGlobalIcons();
3250 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003251 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3252 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3253 if (sVoiceSearchIcon[coi] == null) {
3254 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3255 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3256 TOOLBAR_ICON_METADATA_NAME);
3257 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003258 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003259 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003260 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003261 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003262 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003263 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003265 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003266 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003268 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003269 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003270
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003271 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3273 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003274 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003275 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003276 }
3277
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003278 public void setVoiceButtonProxyVisible(boolean visible) {
3279 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3280 if (voiceButtonProxy != null) {
3281 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3282 }
3283 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003284 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003285 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003286 */
3287 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003288 final View marketButton = findViewById(R.id.market_button);
3289 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3290 // Find the app market activity by resolving an intent.
3291 // (If multiple app markets are installed, it will return the ResolverActivity.)
3292 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003293 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003294 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003295 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003296 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003297 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3298 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003299 marketButton.setVisibility(View.VISIBLE);
3300 } else {
3301 // We should hide and disable the view so that we don't try and restore the visibility
3302 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3303 marketButton.setVisibility(View.GONE);
3304 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003305 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003306 }
3307
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003308 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003309 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3310 Resources r = getResources();
3311 Drawable marketIconDrawable = d.newDrawable(r);
3312 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3313 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3314 marketIconDrawable.setBounds(0, 0, w, h);
3315
3316 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003317 }
3318
Michael Jurkad7c28052012-04-27 15:43:36 -07003319 @Override
3320 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003321 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003322 final List<CharSequence> text = event.getText();
3323 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003324 // Populate event with a fake title based on the current state.
3325 if (mState == State.APPS_CUSTOMIZE) {
3326 text.add(getString(R.string.all_apps_button_label));
3327 } else {
3328 text.add(getString(R.string.all_apps_home_button_label));
3329 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003330 return result;
3331 }
3332
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003333 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003334 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003335 */
3336 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3337 @Override
3338 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003339 closeSystemDialogs();
3340 }
3341 }
3342
3343 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003344 * Receives notifications whenever the appwidgets are reset.
3345 */
3346 private class AppWidgetResetObserver extends ContentObserver {
3347 public AppWidgetResetObserver() {
3348 super(new Handler());
3349 }
3350
3351 @Override
3352 public void onChange(boolean selfChange) {
3353 onAppWidgetReset();
3354 }
3355 }
3356
3357 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003358 * If the activity is currently paused, signal that we need to run the passed Runnable
3359 * in onResume.
3360 *
3361 * This needs to be called from incoming places where resources might have been loaded
3362 * while we are paused. That is becaues the Configuration might be wrong
3363 * when we're not running, and if it comes back to what it was when we
3364 * were paused, we are not restarted.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 *
3368 * @return true if we are currently paused. The caller might be able to
3369 * skip some work in that case since we will come back again.
3370 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003371 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003372 if (mPaused) {
3373 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 if (deletePreviousRunnables) {
3375 while (mOnResumeCallbacks.remove(run)) {
3376 }
3377 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003378 mOnResumeCallbacks.add(run);
3379 return true;
3380 } else {
3381 return false;
3382 }
3383 }
3384
Michael Jurkac402cd92013-05-20 15:49:32 +02003385 private boolean waitUntilResume(Runnable run) {
3386 return waitUntilResume(run, false);
3387 }
3388
Michael Jurka7607c2f2013-04-03 14:33:19 -07003389 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003390 * If the activity is currently paused, signal that we need to re-run the loader
3391 * in onResume.
3392 *
3393 * This needs to be called from incoming places where resources might have been loaded
3394 * while we are paused. That is becaues the Configuration might be wrong
3395 * when we're not running, and if it comes back to what it was when we
3396 * were paused, we are not restarted.
3397 *
3398 * Implementation of the method from LauncherModel.Callbacks.
3399 *
3400 * @return true if we are currently paused. The caller might be able to
3401 * skip some work in that case since we will come back again.
3402 */
3403 public boolean setLoadOnResume() {
3404 if (mPaused) {
3405 Log.i(TAG, "setLoadOnResume");
3406 mOnResumeNeedsLoad = true;
3407 return true;
3408 } else {
3409 return false;
3410 }
3411 }
3412
3413 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003415 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003417 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003418 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003419 } else {
3420 return SCREEN_COUNT / 2;
3421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003423
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 /**
3425 * Refreshes the shortcuts shown on the workspace.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
3429 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003430 // If we're starting binding all over again, clear any bind calls we'd postponed in
3431 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3432 // from scratch again
3433 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003434
Michael Jurka7607c2f2013-04-03 14:33:19 -07003435 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003436 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003438 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003439 int count = workspace.getChildCount();
3440 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003441 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003442 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3443 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003445 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003446 if (mHotseat != null) {
3447 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003448 }
3449 }
3450
Adam Cohendcd297f2013-06-18 13:13:40 -07003451 @Override
3452 public void bindScreens(ArrayList<Long> orderedScreenIds) {
3453 int count = orderedScreenIds.size();
3454 for (int i = 0; i < count; i++) {
3455 mWorkspace.insertNewWorkspaceScreenOnBind(orderedScreenIds.get(i));
3456 }
3457 mWorkspace.addExtraEmptyScreen();
3458 }
3459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003460 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 * Bind the items start-end from the list.
3462 *
3463 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003464 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003465 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3466 if (waitUntilResume(new Runnable() {
3467 public void run() {
3468 bindItems(shortcuts, start, end);
3469 }
3470 })) {
3471 return;
3472 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003473
Winson Chungf0c6ae02012-03-21 16:10:31 -07003474 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3475 Set<String> newApps = new HashSet<String>();
3476 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3477
3478 Workspace workspace = mWorkspace;
3479 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003481
3482 // Short circuit if we are loading dock items for a configuration which has no dock
3483 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3484 mHotseat == null) {
3485 continue;
3486 }
3487
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 switch (item.itemType) {
3489 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3490 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491 ShortcutInfo info = (ShortcutInfo) item;
3492 String uri = info.intent.toUri(0).toString();
3493 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003494
3495 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3496 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003497 boolean animateIconUp = false;
3498 synchronized (newApps) {
3499 if (newApps.contains(uri)) {
3500 animateIconUp = newApps.remove(uri);
3501 }
3502 }
3503 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003504 // Prepare the view to be animated up
3505 shortcut.setAlpha(0f);
3506 shortcut.setScaleX(0f);
3507 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003508 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003509 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3510 mNewShortcutAnimateViews.add(shortcut);
3511 }
3512 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003514 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003515 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003516 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003517 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003518 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3519 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003522 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003523
Joe Onorato9c1289c2009-08-17 11:03:03 -04003524 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003525 }
3526
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 /**
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003530 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3531 if (waitUntilResume(new Runnable() {
3532 public void run() {
3533 bindFolders(folders);
3534 }
3535 })) {
3536 return;
3537 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003538 sFolders.clear();
3539 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003540 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541
3542 /**
3543 * Add the views for a widget to the workspace.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003547 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3548 if (waitUntilResume(new Runnable() {
3549 public void run() {
3550 bindAppWidget(item);
3551 }
3552 })) {
3553 return;
3554 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003555
Daniel Sandler843e8602010-06-07 14:59:01 -04003556 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3557 if (DEBUG_WIDGETS) {
3558 Log.d(TAG, "bindAppWidget: " + item);
3559 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 final Workspace workspace = mWorkspace;
3561
3562 final int appWidgetId = item.appWidgetId;
3563 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003564 if (DEBUG_WIDGETS) {
3565 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3566 }
3567
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3569
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003571 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003572
Adam Cohendcd297f2013-06-18 13:13:40 -07003573 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003575 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3576
Joe Onorato9c1289c2009-08-17 11:03:03 -04003577 workspace.requestLayout();
3578
Daniel Sandler843e8602010-06-07 14:59:01 -04003579 if (DEBUG_WIDGETS) {
3580 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3581 + (SystemClock.uptimeMillis()-start) + "ms");
3582 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003583 }
3584
Adam Cohen1462de32012-07-24 22:34:36 -07003585 public void onPageBoundSynchronously(int page) {
3586 mSynchronouslyBoundPages.add(page);
3587 }
3588
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 /**
3590 * Callback saying that there aren't any more items to bind.
3591 *
3592 * Implementation of the method from LauncherModel.Callbacks.
3593 */
Adam Cohene25af792013-06-06 23:08:25 -07003594 public void finishBindingItems(final boolean upgradePath) {
Winson Chungc9168342013-06-26 14:54:55 -07003595
Michael Jurka7607c2f2013-04-03 14:33:19 -07003596 if (waitUntilResume(new Runnable() {
3597 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003598 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003599 }
3600 })) {
3601 return;
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 if (mSavedState != null) {
3604 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003605 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 mSavedState = null;
3608 }
3609
Adam Cohen1462de32012-07-24 22:34:36 -07003610 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003612 // If we received the result of any pending adds while the loader was running (e.g. the
3613 // widget configuration forced an orientation change), process them now.
3614 for (int i = 0; i < sPendingAddList.size(); i++) {
3615 completeAdd(sPendingAddList.get(i));
3616 }
3617 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618
Winson Chungc51db6a2011-10-05 11:44:49 -07003619 // Update the market app icon as necessary (the other icons will be managed in response to
3620 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003621 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003622
Winson Chungf0c6ae02012-03-21 16:10:31 -07003623 // Animate up any icons as necessary
3624 if (mVisible || mWorkspaceLoading) {
3625 Runnable newAppsRunnable = new Runnable() {
3626 @Override
3627 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003628 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003629 }
3630 };
Winson Chunga2413752012-04-03 14:22:34 -07003631
Adam Cohendcd297f2013-06-18 13:13:40 -07003632 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3633 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003634 if (canRunNewAppsAnimation()) {
3635 // If the user has not interacted recently, then either snap to the new page to show
3636 // the new-apps animation or just run them if they are to appear on the current page
3637 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003638 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003639 } else {
3640 runNewAppsAnimation(false);
3641 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003642 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003643 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003644 // are on another page (or just normally if they are added to the current page)
3645 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003646 }
3647 }
3648
3649 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003650 if (upgradePath) {
3651 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003652 mWorkspace.stripDuplicateApps();
3653 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003654 }
Adam Cohen99894d92013-06-14 11:22:59 -07003655
Adam Cohen66a01fd2013-06-11 12:48:00 -07003656 mWorkspace.post(new Runnable() {
3657 @Override
3658 public void run() {
3659 onFinishBindingItems();
3660 }
3661 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662 }
3663
Winson Chunga2413752012-04-03 14:22:34 -07003664 private boolean canRunNewAppsAnimation() {
3665 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3666 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3667 }
3668
Winson Chungc9168342013-06-26 14:54:55 -07003669 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3670 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3671 PropertyValuesHolder.ofFloat("alpha", 1f),
3672 PropertyValuesHolder.ofFloat("scaleX", 1f),
3673 PropertyValuesHolder.ofFloat("scaleY", 1f));
3674 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3675 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3676 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3677 return bounceAnim;
3678 }
3679
Winson Chungf0c6ae02012-03-21 16:10:31 -07003680 /**
3681 * Runs a new animation that scales up icons that were added while Launcher was in the
3682 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003683 *
3684 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 */
Winson Chunga2413752012-04-03 14:22:34 -07003686 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003687 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003689
3690 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003691 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3692 @Override
3693 public int compare(View a, View b) {
3694 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3695 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3696 int cellCountX = LauncherModel.getCellCountX();
3697 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3698 }
3699 });
Winson Chunga2413752012-04-03 14:22:34 -07003700
3701 // Animate each of the views in place (or show them immediately if requested)
3702 if (immediate) {
3703 for (View v : mNewShortcutAnimateViews) {
3704 v.setAlpha(1f);
3705 v.setScaleX(1f);
3706 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003707 }
Winson Chunga2413752012-04-03 14:22:34 -07003708 } else {
3709 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3710 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003711 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003712 }
3713 anim.playTogether(bounceAnims);
3714 anim.addListener(new AnimatorListenerAdapter() {
3715 @Override
3716 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003717 if (mWorkspace != null) {
3718 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3719 }
Winson Chunga2413752012-04-03 14:22:34 -07003720 }
3721 });
3722 anim.start();
3723 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003724
3725 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003726 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003727 mNewShortcutAnimateViews.clear();
3728 new Thread("clearNewAppsThread") {
3729 public void run() {
3730 mSharedPrefs.edit()
3731 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3732 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3733 .commit();
3734 }
3735 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003736 }
3737
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003738 @Override
3739 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003740 boolean searchVisible = updateGlobalSearchIcon();
3741 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003742 if (mSearchDropTargetBar != null) {
3743 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3744 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003745 }
3746
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 * Add the icons for all apps.
3749 *
3750 * Implementation of the method from LauncherModel.Callbacks.
3751 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003752 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003753 Runnable setAllAppsRunnable = new Runnable() {
3754 public void run() {
3755 if (mAppsCustomizeContent != null) {
3756 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003757
3758 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3759 getHotseat().addAllAppsFolder(mIconCache, apps,
3760 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3761 mIntentsOnWorkspaceFromUpgradePath = null;
3762 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003763 }
3764 }
3765 };
3766
Michael Jurkac57b7a82011-08-09 22:02:20 -07003767 // Remove the progress bar entirely; we could also make it GONE
3768 // but better to remove it since we know it's not going to be used
3769 View progressBar = mAppsCustomizeTabHost.
3770 findViewById(R.id.apps_customize_progress_bar);
3771 if (progressBar != null) {
3772 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003773
3774 // We just post the call to setApps so the user sees the progress bar
3775 // disappear-- otherwise, it just looks like the progress bar froze
3776 // which doesn't look great
3777 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3778 } else {
3779 // If we did not initialize the spinner in onCreate, then we can directly set the
3780 // list of applications without waiting for any progress bars views to be hidden.
3781 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003782 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003783 }
3784
3785 /**
3786 * A package was installed.
3787 *
3788 * Implementation of the method from LauncherModel.Callbacks.
3789 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003790 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3791 if (waitUntilResume(new Runnable() {
3792 public void run() {
3793 bindAppsAdded(apps);
3794 }
3795 })) {
3796 return;
3797 }
3798
Winson Chungc9168342013-06-26 14:54:55 -07003799 final Launcher launcher = this;
3800 final Context context = this;
3801 final ArrayList<ApplicationInfo> appsCopy = new ArrayList<ApplicationInfo>();
3802 appsCopy.addAll(apps);
3803
3804 // Process newly added apps
3805 mWorkspace.post(new Runnable() {
3806 @Override
3807 public void run() {
3808 // Process newly added apps
3809 LauncherModel model = getModel();
3810 Iterator<ApplicationInfo> iter = appsCopy.iterator();
3811 ArrayList<View> animatedShortcuts = new ArrayList<View>();
3812
3813 while (iter.hasNext()) {
3814 ApplicationInfo a = iter.next();
3815 Pair<Long, int[]> coords = LauncherModel.findNextAvailableIconSpace(context,
3816 a.title.toString(), a.intent);
3817 if (coords == null) {
3818 // If we can't find a valid position, then just add a new screen.
3819 // This takes time so we need to re-queue the add until the new
3820 // page is added.
3821 long screenId = LauncherAppState.getInstance().getLauncherProvider().generateNewScreenId();
3822 mWorkspace.insertNewWorkspaceScreen(screenId, false);
3823 model.updateWorkspaceScreenOrder(launcher, mWorkspace.getScreenOrder(),
3824 new Runnable() {
3825 @Override
3826 public void run() {
3827 bindAppsAdded(appsCopy);
3828 }
3829 });
3830 return;
3831 } else {
3832 final ShortcutInfo shortcutInfo = a.makeShortcut();
3833 final View shortcut = createShortcut(shortcutInfo);
3834 // Add the view to the screen
3835 mWorkspace.addInScreenFromBind(shortcut,
3836 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3837 coords.first, coords.second[0], coords.second[1], 1, 1);
3838 // Add the shortcut to the db
3839 model.addItemToDatabase(context, shortcutInfo,
3840 LauncherSettings.Favorites.CONTAINER_DESKTOP,
3841 coords.first, coords.second[0], coords.second[1], false);
3842 // Animate the shortcut
3843 animatedShortcuts.add(shortcut);
3844 }
3845 iter.remove();
3846 }
3847
3848 // Animate all the applications up
3849 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3850 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3851 for (int i = 0; i < animatedShortcuts.size(); ++i) {
3852 View shortcut = animatedShortcuts.get(i);
3853 shortcut.setAlpha(0f);
3854 shortcut.setScaleX(0f);
3855 shortcut.setScaleY(0f);
3856 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3857 }
3858 anim.playTogether(bounceAnims);
3859 anim.start();
3860 }
3861 });
Winson Chungf0ea4d32011-06-06 14:27:16 -07003862
Winson Chung785d2eb2011-04-14 16:08:02 -07003863 if (mAppsCustomizeContent != null) {
3864 mAppsCustomizeContent.addApps(apps);
3865 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003866 }
3867
3868 /**
3869 * A package was updated.
3870 *
3871 * Implementation of the method from LauncherModel.Callbacks.
3872 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003873 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3874 if (waitUntilResume(new Runnable() {
3875 public void run() {
3876 bindAppsUpdated(apps);
3877 }
3878 })) {
3879 return;
3880 }
3881
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003882 if (mWorkspace != null) {
3883 mWorkspace.updateShortcuts(apps);
3884 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003885
Winson Chung785d2eb2011-04-14 16:08:02 -07003886 if (mAppsCustomizeContent != null) {
3887 mAppsCustomizeContent.updateApps(apps);
3888 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 }
3890
3891 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003892 * A package was uninstalled. We take both the super set of packageNames
3893 * in addition to specific applications to remove, the reason being that
3894 * this can be called when a package is updated as well. In that scenario,
3895 * we only remove specific components from the workspace, where as
3896 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003897 *
3898 * Implementation of the method from LauncherModel.Callbacks.
3899 */
Winson Chung83892cc2013-05-01 16:53:33 -07003900 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3901 final ArrayList<ApplicationInfo> appInfos,
3902 final boolean matchPackageNamesOnly) {
3903 if (waitUntilResume(new Runnable() {
3904 public void run() {
3905 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3906 }
3907 })) {
3908 return;
3909 }
3910
3911 if (matchPackageNamesOnly) {
3912 mWorkspace.removeItemsByPackageName(packageNames);
3913 } else {
3914 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003915 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003916
Winson Chung785d2eb2011-04-14 16:08:02 -07003917 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003918 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003919 }
Winson Chunga1820962011-10-03 16:31:06 -07003920
3921 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003922 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 }
Joe Onoratobe386092009-11-17 17:32:16 -08003924
3925 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003926 * A number of packages were updated.
3927 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003928
3929 private ArrayList<Object> mWidgetsAndShortcuts;
3930 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003931 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003932 bindPackagesUpdated(mWidgetsAndShortcuts);
3933 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003934 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003935 };
3936
3937 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3938 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3939 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003940 return;
3941 }
3942
Winson Chung785d2eb2011-04-14 16:08:02 -07003943 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003944 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003945 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003946 }
3947
Winson Chung400438b2011-01-16 17:53:48 -08003948 private int mapConfigurationOriActivityInfoOri(int configOri) {
3949 final Display d = getWindowManager().getDefaultDisplay();
3950 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3951 switch (d.getRotation()) {
3952 case Surface.ROTATION_0:
3953 case Surface.ROTATION_180:
3954 // We are currently in the same basic orientation as the natural orientation
3955 naturalOri = configOri;
3956 break;
3957 case Surface.ROTATION_90:
3958 case Surface.ROTATION_270:
3959 // We are currently in the other basic orientation to the natural orientation
3960 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3961 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3962 break;
3963 }
3964
3965 int[] oriMap = {
3966 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3967 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3968 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3969 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3970 };
3971 // Since the map starts at portrait, we need to offset if this device's natural orientation
3972 // is landscape.
3973 int indexOffset = 0;
3974 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3975 indexOffset = 1;
3976 }
3977 return oriMap[(d.getRotation() + indexOffset) % 4];
3978 }
Adam Cohen446e9402011-09-15 18:21:21 -07003979
Winson Chung4b919f82012-05-01 10:44:08 -07003980 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003981 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003982 getResources().getBoolean(R.bool.allow_rotation);
3983 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003984 }
Winson Chung4b919f82012-05-01 10:44:08 -07003985 public void lockScreenOrientation() {
3986 if (isRotationEnabled()) {
3987 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3988 .getConfiguration().orientation));
3989 }
3990 }
3991 public void unlockScreenOrientation(boolean immediate) {
3992 if (isRotationEnabled()) {
3993 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003994 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003995 } else {
3996 mHandler.postDelayed(new Runnable() {
3997 public void run() {
3998 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3999 }
4000 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004001 }
Winson Chung4b919f82012-05-01 10:44:08 -07004002 }
Winson Chung400438b2011-01-16 17:53:48 -08004003 }
4004
Winson Chung82f55532011-08-09 14:14:23 -07004005 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07004006 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07004007 if (DISABLE_CLINGS) {
4008 return false;
4009 }
4010
Brett Chabot2a9e2282011-08-23 15:03:13 -07004011 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07004012 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07004013
Michael Jurkae233a8b2013-03-19 13:49:20 +01004014 // Restricted secondary users (child mode) will potentially have very few apps
4015 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04004016// boolean supportsLimitedUsers =
4017// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
4018// Account[] accounts = AccountManager.get(this).getAccounts();
4019// if (supportsLimitedUsers && accounts.length == 0) {
4020// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
4021// Bundle restrictions = um.getUserRestrictions();
4022// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
4023// return false;
4024// }
4025// }
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 return true;
Winson Chung82f55532011-08-09 14:14:23 -07004027 }
Michael Jurka22143132012-10-04 17:42:38 +02004028
Winson Chung7d7541e2011-09-16 20:14:36 -07004029 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004030 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004031 if (cling != null) {
4032 cling.init(this, positionData);
4033 cling.setVisibility(View.VISIBLE);
4034 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4035 if (animate) {
4036 cling.buildLayer();
4037 cling.setAlpha(0f);
4038 cling.animate()
4039 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004040 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 .setDuration(SHOW_CLING_DURATION)
4042 .setStartDelay(delay)
4043 .start();
4044 } else {
4045 cling.setAlpha(1f);
4046 }
Michael Jurka22143132012-10-04 17:42:38 +02004047 cling.setFocusableInTouchMode(true);
4048 cling.post(new Runnable() {
4049 public void run() {
4050 cling.setFocusable(true);
4051 cling.requestFocus();
4052 }
4053 });
4054 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4055 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 }
4057 return cling;
4058 }
Michael Jurka22143132012-10-04 17:42:38 +02004059
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004061 // To catch cases where siblings of top-level views are made invisible, just check whether
4062 // the cling is directly set to GONE before dismissing it.
4063 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004064 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004066 anim.addListener(new AnimatorListenerAdapter() {
4067 public void onAnimationEnd(Animator animation) {
4068 cling.setVisibility(View.GONE);
4069 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004070 // We should update the shared preferences on a background thread
4071 new Thread("dismissClingThread") {
4072 public void run() {
4073 SharedPreferences.Editor editor = mSharedPrefs.edit();
4074 editor.putBoolean(flag, true);
4075 editor.commit();
4076 }
4077 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004078 };
4079 });
4080 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004081 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004082 }
4083 }
Michael Jurka22143132012-10-04 17:42:38 +02004084
Winson Chung9d9d74f2011-09-19 11:49:12 -07004085 private void removeCling(int id) {
4086 final View cling = findViewById(id);
4087 if (cling != null) {
4088 final ViewGroup parent = (ViewGroup) cling.getParent();
4089 parent.post(new Runnable() {
4090 @Override
4091 public void run() {
4092 parent.removeView(cling);
4093 }
4094 });
Michael Jurka22143132012-10-04 17:42:38 +02004095 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004096 }
4097 }
Michael Jurka974c3862012-05-22 22:00:31 -07004098
4099 private boolean skipCustomClingIfNoAccounts() {
4100 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4101 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4102 if (customCling) {
4103 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004104 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004105 Account[] accounts = am.getAccountsByType("com.google");
4106 return accounts.length == 0;
4107 }
4108 return false;
4109 }
4110
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004112 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004113 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004114 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4115 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004116 // If we're not using the default workspace layout, replace workspace cling
4117 // with a custom workspace cling (usually specified in an overlay)
4118 // For now, only do this on tablets
4119 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004120 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004121 // Use a custom cling
4122 View cling = findViewById(R.id.workspace_cling);
4123 ViewGroup clingParent = (ViewGroup) cling.getParent();
4124 int clingIndex = clingParent.indexOfChild(cling);
4125 clingParent.removeViewAt(clingIndex);
4126 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4127 clingParent.addView(customCling, clingIndex);
4128 customCling.setId(R.id.workspace_cling);
4129 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004130 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004131 } else {
4132 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 }
4134 }
4135 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004136 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004137 if (isClingsEnabled() &&
4138 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004140 } else {
4141 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004142 }
4143 }
4144 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004145 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004146 if (isClingsEnabled() &&
4147 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4148 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004149 } else {
4150 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004151 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004152 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004153 }
4154 public boolean isFolderClingVisible() {
4155 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004156 if (cling != null) {
4157 return cling.getVisibility() == View.VISIBLE;
4158 }
4159 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004160 }
Winson Chung82f55532011-08-09 14:14:23 -07004161 public void dismissWorkspaceCling(View v) {
4162 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004163 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004164 }
4165 public void dismissAllAppsCling(View v) {
4166 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004167 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4168 }
4169 public void dismissFolderCling(View v) {
4170 Cling cling = (Cling) findViewById(R.id.folder_cling);
4171 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004172 }
4173
Winson Chung80baf5a2010-08-09 16:03:15 -07004174 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004175 * Prints out out state for debugging.
4176 */
4177 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004178 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004179 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004180 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4181 Log.d(TAG, "mRestoring=" + mRestoring);
4182 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4183 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004184 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004185 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004186
Winson Chung785d2eb2011-04-14 16:08:02 -07004187 if (mAppsCustomizeContent != null) {
4188 mAppsCustomizeContent.dumpState();
4189 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004190 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004191 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004192
4193 @Override
4194 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4195 super.dump(prefix, fd, writer, args);
4196 writer.println(" ");
4197 writer.println("Debug logs: ");
4198 for (int i = 0; i < sDumpLogs.size(); i++) {
4199 writer.println(" " + sDumpLogs.get(i));
4200 }
4201 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004202
4203 public static void dumpDebugLogsToConsole() {
4204 Log.d(TAG, "");
4205 Log.d(TAG, "*********************");
4206 Log.d(TAG, "Launcher debug logs: ");
4207 for (int i = 0; i < sDumpLogs.size(); i++) {
4208 Log.d(TAG, " " + sDumpLogs.get(i));
4209 }
4210 Log.d(TAG, "*********************");
4211 Log.d(TAG, "");
4212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004213}
Michael Jurka2763be32011-02-24 11:19:57 -08004214
Michael Jurkaabded662011-03-04 12:06:57 -08004215interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004216 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004217 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004218 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004219 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004220 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004221}