blob: ffd63d28cc9e6d26f216fb7553e8e8ef80f43f8a [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
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Craig Mautner360310b2012-10-26 15:13:08 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Craig Mautner360310b2012-10-26 15:13:08 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080074import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070083import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080084import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070092import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Adam Cohendf2cc412011-04-27 16:56:57 -070097import com.android.common.Search;
98import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070099import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700104import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
110import java.util.Collections;
111import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
Winson Chung70d72102011-08-12 11:24:35 -0700130 private static final int MENU_GROUP_WALLPAPER = 1;
131 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
132 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700133 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
134 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 private static final int REQUEST_PICK_APPLICATION = 6;
139 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Michael Jurka8b805b12012-04-18 14:23:14 -0700143 private static final int REQUEST_BIND_APPWIDGET = 11;
144
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
146
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800147 static final int SCREEN_COUNT = 5;
148 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700151 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
152 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Winson Chung2672ff92012-05-04 16:22:30 -0700154 // The Intent extra that defines whether to ignore the launch animation
155 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
156 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: int
159 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700160 // Type: int
161 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700163 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
164 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
166 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700169 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: boolean
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
172 // Type: long
173 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700174 // Type: int
175 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
176 // Type: int
177 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
178 // Type: parcelable
179 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700181 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700182 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
183 "com.android.launcher.toolbar_search_icon";
184 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
185 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700186
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700187 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700188 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700189 private State mState = State.WORKSPACE;
190 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800191 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700192
Joe Onorato9c1289c2009-08-17 11:03:03 -0400193 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700194 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
195 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700196 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700197 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800200 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Winson Chunga2413752012-04-03 14:22:34 -0700202 // How long to wait before the new-shortcut animation automatically pans the workspace
203 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
204
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800205 private final BroadcastReceiver mCloseSystemDialogsReceiver
206 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800207 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private LayoutInflater mInflater;
210
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800212 private View mQsbDivider;
213 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700214 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800215 private DragLayer mDragLayer;
216 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700218 private AppWidgetManager mAppWidgetManager;
219 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700220
Michael Jurkac9d95c52011-08-29 14:03:34 -0700221 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700222 private AppWidgetProviderInfo mPendingAddWidgetInfo;
223
Michael Jurka0280c3b2010-09-17 15:00:07 -0700224 private int[] mTmpAddItemCellCoordinates = new int[2];
225
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226 private FolderInfo mFolderInfo;
227
Winson Chung3d503fb2011-07-13 17:25:49 -0700228 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800229 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700230
Winson Chungc51db6a2011-10-05 11:44:49 -0700231 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700232 private AppsCustomizeTabHost mAppsCustomizeTabHost;
233 private AppsCustomizePagedView mAppsCustomizeContent;
234 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700237 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
238 // scroll issues (because the workspace may not have been measured yet) and extra work.
239 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
240 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
242 private SpannableStringBuilder mDefaultKeySsb = null;
243
Joe Onorato9c1289c2009-08-17 11:03:03 -0400244 private boolean mWorkspaceLoading = true;
245
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800246 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private boolean mRestoring;
248 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700249 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250
Winson Chung4a2afa32012-07-19 14:53:05 -0700251 // Keep track of whether the user has left launcher
252 private static boolean sPausedFromUserAction = false;
253
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private Bundle mSavedInstanceState;
255
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800257 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800258 private boolean mUserPresent = true;
259 private boolean mVisible = false;
260 private boolean mAttached = false;
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;
287 private Drawable mBlackBackgroundDrawable;
288
Adam Cohen1462de32012-07-24 22:34:36 -0700289 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
290
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700291 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
292
Winson Chung46353de2012-02-16 14:05:10 -0800293 // We only want to get the SharedPreferences once since it does an FS stat each time we get
294 // it from the context.
295 private SharedPreferences mSharedPrefs;
296
Winson Chungf0c6ae02012-03-21 16:10:31 -0700297 // Holds the page that we need to animate to, and the icon views that we need to animate up
298 // when we scroll to that page on resume.
299 private int mNewShortcutAnimatePage = -1;
300 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700301 private ImageView mFolderIconImageView;
302 private Bitmap mFolderIconBitmap;
303 private Canvas mFolderIconCanvas;
304 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700305
Michael Jurkaddd62e92011-02-16 17:49:14 -0800306 private BubbleTextView mWaitingForResume;
307
Michael Jurka22143132012-10-04 17:42:38 +0200308 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
309 = new HideFromAccessibilityHelper();
310
Michael Jurkac1f5d262011-09-30 19:32:27 -0700311 private Runnable mBuildLayersRunnable = new Runnable() {
312 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700313 if (mWorkspace != null) {
314 mWorkspace.buildPageHardwareLayers();
315 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700316 }
317 };
318
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800319 private static ArrayList<PendingAddArguments> sPendingAddList
320 = new ArrayList<PendingAddArguments>();
321
322 private static class PendingAddArguments {
323 int requestCode;
324 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700325 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800326 int screen;
327 int cellX;
328 int cellY;
329 }
330
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700331
332 private boolean doesFileExist(String filename) {
333 FileInputStream fis = null;
334 try {
335 fis = openFileInput(filename);
336 fis.close();
337 return true;
338 } catch (java.io.FileNotFoundException e) {
339 return false;
340 } catch (java.io.IOException e) {
341 return true;
342 }
343 }
344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 @Override
346 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700347 if (DEBUG_STRICT_MODE) {
348 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
349 .detectDiskReads()
350 .detectDiskWrites()
351 .detectNetwork() // or .detectAll() for all detectable problems
352 .penaltyLog()
353 .build());
354 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
355 .detectLeakedSqlLiteObjects()
356 .detectLeakedClosableObjects()
357 .penaltyLog()
358 .penaltyDeath()
359 .build());
360 }
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800363 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700364 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
365 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800366 mModel = app.setLauncher(this);
367 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400368 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700370
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700371 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700372 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
373 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700374
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700375 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
376 // this also ensures that any synchronous binding below doesn't re-trigger another
377 // LauncherModel load.
378 mPaused = false;
379
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200381 android.os.Debug.startMethodTracing(
382 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 setContentView(R.layout.launcher);
387 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700388 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800390 registerContentObservers();
391
Joe Onorato7c312c12009-08-13 21:36:53 -0700392 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 mSavedState = savedInstanceState;
395 restoreState(mSavedState);
396
Winson Chunga12a2502010-12-20 14:41:35 -0800397 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700398 if (mAppsCustomizeContent != null) {
399 mAppsCustomizeContent.onPackagesUpdated();
400 }
Winson Chunga12a2502010-12-20 14:41:35 -0800401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 if (PROFILE_STARTUP) {
403 android.os.Debug.stopMethodTracing();
404 }
405
406 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700407 if (sPausedFromUserAction) {
408 // If the user leaves launcher, then we should just load items asynchronously when
409 // they return.
410 mModel.startLoader(true, -1);
411 } else {
412 // We only load the page synchronously if the user rotates (or triggers a
413 // configuration change) while launcher is in the foreground
414 mModel.startLoader(true, mWorkspace.getCurrentPage());
415 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 }
417
Michael Jurkac57b7a82011-08-09 22:02:20 -0700418 if (!mModel.isAllAppsLoaded()) {
419 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
420 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
421 }
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 // For handling default keys
424 mDefaultKeySsb = new SpannableStringBuilder();
425 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800426
427 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
428 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800429
Adam Cohenf9426d52012-06-04 17:26:21 -0700430 updateGlobalIcons();
431
432 // On large interfaces, we want the screen to auto-rotate based on the current orientation
433 unlockScreenOrientation(true);
434 }
435
Winson Chung4a2afa32012-07-19 14:53:05 -0700436 protected void onUserLeaveHint() {
437 super.onUserLeaveHint();
438 sPausedFromUserAction = true;
439 }
440
Adam Cohenf9426d52012-06-04 17:26:21 -0700441 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700442 boolean searchVisible = false;
443 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800444 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700445 int coi = getCurrentOrientationIndexForGlobalIcons();
446 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
447 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 updateAppMarketIcon();
449 searchVisible = updateGlobalSearchIcon();
450 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700452 if (sGlobalSearchIcon[coi] != null) {
453 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700454 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700455 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700456 if (sVoiceSearchIcon[coi] != null) {
457 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700458 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700459 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700460 if (sAppMarketIcon[coi] != null) {
461 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800462 }
Winson Chungadf0c182012-08-23 14:59:07 -0700463 if (mSearchDropTargetBar != null) {
464 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
Romain Guycbb89e42009-06-08 15:52:54 -0700467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 if (sLocaleConfiguration == null) {
470 new AsyncTask<Void, Void, LocaleConfiguration>() {
471 @Override
472 protected LocaleConfiguration doInBackground(Void... unused) {
473 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
474 readConfiguration(Launcher.this, localeConfiguration);
475 return localeConfiguration;
476 }
477
478 @Override
479 protected void onPostExecute(LocaleConfiguration result) {
480 sLocaleConfiguration = result;
481 checkForLocaleChange(); // recursive, but now with a locale configuration
482 }
483 }.execute();
484 return;
485 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final Configuration configuration = getResources().getConfiguration();
488
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700489 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final String locale = configuration.locale.toString();
491
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 final int mcc = configuration.mcc;
494
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 final int mnc = configuration.mnc;
497
Romain Guy84f296c2009-11-04 15:00:44 -0800498 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499
Romain Guy84f296c2009-11-04 15:00:44 -0800500 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 sLocaleConfiguration.locale = locale;
502 sLocaleConfiguration.mcc = mcc;
503 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700504
Joe Onorato0589f0f2010-02-08 13:44:00 -0800505 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700506
507 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
508 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700509 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700510 public void run() {
511 writeConfiguration(Launcher.this, localeConfiguration);
512 }
513 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700514 }
515 }
516
517 private static class LocaleConfiguration {
518 public String locale;
519 public int mcc = -1;
520 public int mnc = -1;
521 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700522
Romain Guy98d01652009-06-30 16:21:04 -0700523 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
524 DataInputStream in = null;
525 try {
526 in = new DataInputStream(context.openFileInput(PREFERENCES));
527 configuration.locale = in.readUTF();
528 configuration.mcc = in.readInt();
529 configuration.mnc = in.readInt();
530 } catch (FileNotFoundException e) {
531 // Ignore
532 } catch (IOException e) {
533 // Ignore
534 } finally {
535 if (in != null) {
536 try {
537 in.close();
538 } catch (IOException e) {
539 // Ignore
540 }
541 }
542 }
543 }
544
545 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
546 DataOutputStream out = null;
547 try {
548 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
549 out.writeUTF(configuration.locale);
550 out.writeInt(configuration.mcc);
551 out.writeInt(configuration.mnc);
552 out.flush();
553 } catch (FileNotFoundException e) {
554 // Ignore
555 } catch (IOException e) {
556 //noinspection ResultOfMethodCallIgnored
557 context.getFileStreamPath(PREFERENCES).delete();
558 } finally {
559 if (out != null) {
560 try {
561 out.close();
562 } catch (IOException e) {
563 // Ignore
564 }
565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
567 }
568
Adam Cohen716b51e2011-06-30 12:09:54 -0700569 public DragLayer getDragLayer() {
570 return mDragLayer;
571 }
572
Winson Chung36a62fe2012-05-06 18:04:42 -0700573 boolean isDraggingEnabled() {
574 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
575 // that is subsequently removed from the workspace in startBinding().
576 return !mModel.isLoadingWorkspace();
577 }
578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 static int getScreen() {
580 synchronized (sLock) {
581 return sScreen;
582 }
583 }
584
585 static void setScreen(int screen) {
586 synchronized (sLock) {
587 sScreen = screen;
588 }
589 }
590
Winson Chung557d6ed2011-07-08 15:34:52 -0700591 /**
592 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
593 * a configuration step, this allows the proper animations to run after other transitions.
594 */
595 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700596 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 switch (args.requestCode) {
598 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700599 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
600 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 break;
602 case REQUEST_PICK_SHORTCUT:
603 processShortcut(args.intent);
604 break;
605 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700606 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
607 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_CREATE_APPWIDGET:
611 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800612 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700613 result = true;
614 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800615 case REQUEST_PICK_WALLPAPER:
616 // We just wanted the activity result here so we can clear mWaitingForResult
617 break;
618 }
Michael Jurka27614d22012-04-02 06:40:22 -0700619 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
620 // if you turned the screen off and then back while in All Apps, Launcher would not
621 // return to the workspace. Clearing mAddInfo.container here fixes this issue
622 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700623 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 protected void onActivityResult(
628 final int requestCode, final int resultCode, final Intent data) {
629 if (requestCode == REQUEST_BIND_APPWIDGET) {
630 int appWidgetId = data != null ?
631 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
632 if (resultCode == RESULT_CANCELED) {
633 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
634 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700635 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 }
637 return;
638 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
641 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700642 mWaitingForResult = false;
643
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 // We have special handling for widgets
645 if (isWidgetDrop) {
646 int appWidgetId = data != null ?
647 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700648 if (appWidgetId < 0) {
649 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
650 "widget configuration activity.");
651 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
652 } else {
653 completeTwoStageWidgetDrop(resultCode, appWidgetId);
654 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800655 return;
656 }
657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 // The pattern used here is that a user PICKs a specific application,
659 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
662 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700663 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800664 final PendingAddArguments args = new PendingAddArguments();
665 args.requestCode = requestCode;
666 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700667 args.container = mPendingAddInfo.container;
668 args.screen = mPendingAddInfo.screen;
669 args.cellX = mPendingAddInfo.cellX;
670 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 if (isWorkspaceLocked()) {
672 sPendingAddList.add(args);
673 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700674 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
680 null);
681 }
682
683 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700684 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 Runnable onCompleteRunnable = null;
687 int animationType = 0;
688
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700689 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 if (resultCode == RESULT_OK) {
691 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
692 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700693 mPendingAddWidgetInfo);
694 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 onCompleteRunnable = new Runnable() {
696 @Override
697 public void run() {
698 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
699 mPendingAddInfo.screen, layout, null);
700 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
701 null);
702 }
703 };
704 } else if (resultCode == RESULT_CANCELED) {
705 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
706 onCompleteRunnable = new Runnable() {
707 @Override
708 public void run() {
709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
710 null);
711 }
712 };
713 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700714 if (mDragLayer.getAnimatedView() != null) {
715 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
716 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
717 animationType, boundWidget, true);
718 } else {
719 // The animated view may be null in the case of a rotation during widget configuration
720 onCompleteRunnable.run();
721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 }
723
724 @Override
725 protected void onResume() {
726 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700727
Winson Chung4a2afa32012-07-19 14:53:05 -0700728 // Restore the previous launcher state
729 if (mOnResumeState == State.WORKSPACE) {
730 showWorkspace(false);
731 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
732 showAllApps(false);
733 }
734 mOnResumeState = State.NONE;
735
Craig Mautner360310b2012-10-26 15:13:08 -0700736 // Background was set to gradient in onPause(), restore to black if in all apps.
737 setWorkspaceBackground(mState == State.WORKSPACE);
738
Winson Chungde0fb8f2012-05-08 14:37:08 -0700739 // Process any items that were added while Launcher was away
740 InstallShortcutReceiver.flushInstallQueue(this);
741
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800742 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700743 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700744 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700746 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400747 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700748 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
Winson Chunge4e50662012-01-23 14:45:13 -0800750
751 // Reset the pressed state of icons that were locked in the press state while activities
752 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800753 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800754 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800755 mWaitingForResume.setStayPressed(false);
756 }
Winson Chunge4e50662012-01-23 14:45:13 -0800757 if (mAppsCustomizeContent != null) {
758 // Resets the previous all apps icon press state
759 mAppsCustomizeContent.resetDrawableState();
760 }
Adam Cohen06dff352012-06-01 17:17:08 -0700761 // It is possible that widgets can receive updates while launcher is not in the foreground.
762 // Consequently, the widgets will be inflated in the orientation of the foreground activity
763 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
764 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700765 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700766
767 // Again, as with the above scenario, it's possible that one or more of the global icons
768 // were updated in the wrong orientation.
769 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700770 }
771
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700773 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700774 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
775 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
776 // when Launcher resumes and we are still in AllApps.
777 updateWallpaperVisibility(true);
778
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700779 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700780 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800781 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700782 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700783 }
Romain Guycbb89e42009-06-08 15:52:54 -0700784
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700785 @Override
786 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787 // Flag the loader to stop early before switching
788 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700789 if (mAppsCustomizeContent != null) {
790 mAppsCustomizeContent.surrender();
791 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800792 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700793 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700794
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800795 // We can't hide the IME if it was forced open. So don't bother
796 /*
797 @Override
798 public void onWindowFocusChanged(boolean hasFocus) {
799 super.onWindowFocusChanged(hasFocus);
800
801 if (hasFocus) {
802 final InputMethodManager inputManager = (InputMethodManager)
803 getSystemService(Context.INPUT_METHOD_SERVICE);
804 WindowManager.LayoutParams lp = getWindow().getAttributes();
805 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
806 android.os.Handler()) {
807 protected void onReceiveResult(int resultCode, Bundle resultData) {
808 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
809 }
810 });
811 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
812 }
813 }
814 */
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 private boolean acceptFilter() {
817 final InputMethodManager inputManager = (InputMethodManager)
818 getSystemService(Context.INPUT_METHOD_SERVICE);
819 return !inputManager.isFullscreenMode();
820 }
821
822 @Override
823 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700824 final int uniChar = event.getUnicodeChar();
825 final boolean handled = super.onKeyDown(keyCode, event);
826 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
827 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
829 keyCode, event);
830 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700831 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700832 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700833 // showSearchDialog()
834 // If there are multiple keystrokes before the search dialog takes focus,
835 // onSearchRequested() will be called for every keystroke,
836 // but it is idempotent, so it's fine.
837 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 }
839 }
840
Joe Onorato8a9625e2010-01-28 15:55:35 -0800841 // Eat the long press event so the keyboard doesn't come up.
842 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
843 return true;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 return handled;
847 }
848
Karl Rosaen138a0412009-04-23 19:00:21 -0700849 private String getTypedText() {
850 return mDefaultKeySsb.toString();
851 }
852
853 private void clearTypedText() {
854 mDefaultKeySsb.clear();
855 mDefaultKeySsb.clearSpans();
856 Selection.setSelection(mDefaultKeySsb, 0);
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700860 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
861 * State
862 */
863 private static State intToState(int stateOrdinal) {
864 State state = State.WORKSPACE;
865 final State[] stateValues = State.values();
866 for (int i = 0; i < stateValues.length; i++) {
867 if (stateValues[i].ordinal() == stateOrdinal) {
868 state = stateValues[i];
869 break;
870 }
871 }
872 return state;
873 }
874
875 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 * Restores the previous state, if it exists.
877 *
878 * @param savedState The previous state.
879 */
880 private void restoreState(Bundle savedState) {
881 if (savedState == null) {
882 return;
883 }
884
Michael Jurka883f55b2010-10-21 15:47:14 -0700885 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700886 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700887 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800888 }
889
Winson Chungf0c6ae02012-03-21 16:10:31 -0700890 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700892 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 }
894
Winson Chung3d503fb2011-07-13 17:25:49 -0700895 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
896 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700897
Winson Chung3d503fb2011-07-13 17:25:49 -0700898 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
899 mPendingAddInfo.container = pendingAddContainer;
900 mPendingAddInfo.screen = pendingAddScreen;
901 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
902 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700903 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
904 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
905 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700906 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 mRestoring = true;
908 }
909
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
912 if (renameFolder) {
913 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700914 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 mRestoring = true;
916 }
Winson Chunga12a2502010-12-20 14:41:35 -0800917
Winson Chung785d2eb2011-04-14 16:08:02 -0700918
919 // Restore the AppsCustomize tab
920 if (mAppsCustomizeTabHost != null) {
921 String curTab = savedState.getString("apps_customize_currentTab");
922 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700923 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700924 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700925 mAppsCustomizeContent.loadAssociatedPages(
926 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700927 }
928
Winson Chung5afbf7b2011-07-25 11:53:08 -0700929 int currentIndex = savedState.getInt("apps_customize_currentIndex");
930 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933
934 /**
935 * Finds all the views we need and configure them properly.
936 */
937 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700938 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400939
Craig Mautner360310b2012-10-26 15:13:08 -0700940 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700941 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
942 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700943 mQsbDivider = findViewById(R.id.qsb_divider);
944 mDockDivider = findViewById(R.id.dock_divider);
945
946 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
947 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
948 mBlackBackgroundDrawable = new ColorDrawable(Color.BLACK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Winson Chung4c98d922011-05-31 16:50:48 -0700950 // Setup the drag layer
951 mDragLayer.setup(this, dragController);
952
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 // Setup the hotseat
954 mHotseat = (Hotseat) findViewById(R.id.hotseat);
955 if (mHotseat != null) {
956 mHotseat.setup(this);
957 }
958
Winson Chung4c98d922011-05-31 16:50:48 -0700959 // Setup the workspace
960 mWorkspace.setHapticFeedbackEnabled(false);
961 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700962 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700963 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700964
Winson Chungf0ea4d32011-06-06 14:27:16 -0700965 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700966 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700967
Winson Chungf0ea4d32011-06-06 14:27:16 -0700968 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -0700969 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700970 mAppsCustomizeContent = (AppsCustomizePagedView)
971 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
972 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -0700973
Winson Chung3d503fb2011-07-13 17:25:49 -0700974 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700975 dragController.setDragScoller(mWorkspace);
976 dragController.setScrollView(mDragLayer);
977 dragController.setMoveTarget(mWorkspace);
978 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700979 if (mSearchDropTargetBar != null) {
980 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 /**
985 * Creates a view representing a shortcut.
986 *
987 * @param info The data structure describing the shortcut.
988 *
989 * @return A View inflated from R.layout.application.
990 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800991 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700993 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
995
996 /**
997 * Creates a view representing a shortcut inflated from the specified resource.
998 *
999 * @param layoutResId The id of the XML layout used to create the shortcut.
1000 * @param parent The group the shortcut belongs to.
1001 * @param info The data structure describing the shortcut.
1002 *
1003 * @return A View inflated from layoutResId.
1004 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001005 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001006 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1007 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 return favorite;
1010 }
1011
1012 /**
1013 * Add an application shortcut to the workspace.
1014 *
1015 * @param data The intent describing the application.
1016 * @param cellInfo The position on screen where to create the shortcut.
1017 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001019 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001020 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001021
Adam Cohenfbba09b2011-07-18 21:43:05 -07001022 // First we check if we already know the exact location where we want to add this item.
1023 if (cellX >= 0 && cellY >= 0) {
1024 cellXY[0] = cellX;
1025 cellXY[1] = cellY;
1026 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001027 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 return;
1029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001031 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032
Romain Guy73b979d2009-06-09 12:57:21 -07001033 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001036 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001038 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001039 } else {
1040 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 }
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 /**
1045 * Add a shortcut to the workspace.
1046 *
1047 * @param data The intent describing the shortcut.
1048 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1051 int cellY) {
1052 int[] cellXY = mTmpAddItemCellCoordinates;
1053 int[] touchXY = mPendingAddInfo.dropPos;
1054 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001055
Michael Jurkad3ef3062010-11-23 16:23:58 -08001056 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001057
Adam Cohen558baaf2011-08-15 15:22:57 -07001058 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001059 if (info == null) {
1060 return;
1061 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001062 final View view = createShortcut(info);
1063
Adam Cohenfbba09b2011-07-18 21:43:05 -07001064 // First we check if we already know the exact location where we want to add this item.
1065 if (cellX >= 0 && cellY >= 0) {
1066 cellXY[0] = cellX;
1067 cellXY[1] = cellY;
1068 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001069
1070 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001071 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001072 true, null,null)) {
1073 return;
1074 }
1075 DragObject dragObject = new DragObject();
1076 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001077 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1078 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001079 return;
1080 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001081 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001082 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001083 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001084 foundCellSpan = (result != null);
1085 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001086 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001087 }
1088
1089 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001090 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 return;
1092 }
1093
Adam Cohen558baaf2011-08-15 15:22:57 -07001094 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095
1096 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001097 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1098 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
1100 }
1101
Adam Cohen2f093b62012-04-30 18:59:53 -07001102 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1103 int minHeight) {
1104 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001105 // We want to account for the extra amount of padding that we are adding to the widget
1106 // to ensure that it gets the full amount of space that it has requested
1107 int requiredWidth = minWidth + padding.left + padding.right;
1108 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001109 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001110 }
1111
Adam Cohen2f093b62012-04-30 18:59:53 -07001112 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1113 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001114 }
1115
Adam Cohen2f093b62012-04-30 18:59:53 -07001116 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1117 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001118 }
1119
Adam Cohen2f093b62012-04-30 18:59:53 -07001120 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1121 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001122 }
1123
Adam Cohen2f093b62012-04-30 18:59:53 -07001124 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1125 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001126 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001132 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001133 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001135 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1136 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1137 if (appWidgetInfo == null) {
1138 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001141 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001142 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001143
Adam Cohen2f093b62012-04-30 18:59:53 -07001144 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1145 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001148 // We have saved the position to which the widget was dragged-- this really only matters
1149 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 int[] cellXY = mTmpAddItemCellCoordinates;
1151 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1155 cellXY[0] = mPendingAddInfo.cellX;
1156 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 spanXY[0] = mPendingAddInfo.spanX;
1158 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001159 foundCellSpan = true;
1160 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001162 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001163 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1164 spanXY[1], cellXY, finalSpan);
1165 spanXY[0] = finalSpan[0];
1166 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001167 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001170 }
1171
Michael Jurka0280c3b2010-09-17 15:00:07 -07001172 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001173 if (appWidgetId != -1) {
1174 // Deleting an app widget ID is a void call but writes to disk before returning
1175 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001176 new Thread("deleteAppWidgetId") {
1177 public void run() {
1178 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1179 }
1180 }.start();
1181 }
Winson Chung93eef082012-03-23 15:59:27 -07001182 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001183 return;
1184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001187 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1188 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 launcherInfo.spanX = spanXY[0];
1190 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001191 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1192 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196
1197 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001198 if (hostView == null) {
1199 // Perform actual inflation because we're live
1200 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1201 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1202 } else {
1203 // The AppWidgetHostView has already been inflated and instantiated
1204 launcherInfo.hostView = hostView;
1205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001208 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001209 launcherInfo.notifyWidgetSizeChanged(this);
1210
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001213
1214 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001216 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Adam Cohended9f8d2010-11-03 13:25:16 -07001219 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1220 @Override
1221 public void onReceive(Context context, Intent intent) {
1222 final String action = intent.getAction();
1223 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1224 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001225 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001226 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001227
Winson Chungc100e8e2011-08-09 16:02:43 -07001228 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001229 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001230 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1231 mAppsCustomizeTabHost.reset();
1232 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001233 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001234 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1235 mUserPresent = true;
1236 updateRunning();
1237 }
1238 }
1239 };
1240
1241 @Override
1242 public void onAttachedToWindow() {
1243 super.onAttachedToWindow();
1244
1245 // Listen for broadcasts related to user-presence
1246 final IntentFilter filter = new IntentFilter();
1247 filter.addAction(Intent.ACTION_SCREEN_OFF);
1248 filter.addAction(Intent.ACTION_USER_PRESENT);
1249 registerReceiver(mReceiver, filter);
1250
Adam Cohend113e0c2010-11-11 10:48:05 -08001251 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001252 mVisible = true;
1253 }
1254
1255 @Override
1256 public void onDetachedFromWindow() {
1257 super.onDetachedFromWindow();
1258 mVisible = false;
1259
Adam Cohend113e0c2010-11-11 10:48:05 -08001260 if (mAttached) {
1261 unregisterReceiver(mReceiver);
1262 mAttached = false;
1263 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001264 updateRunning();
1265 }
1266
1267 public void onWindowVisibilityChanged(int visibility) {
1268 mVisible = visibility == View.VISIBLE;
1269 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001270 // The following code used to be in onResume, but it turns out onResume is called when
1271 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1272 // is a more appropriate event to handle
1273 if (mVisible) {
1274 mAppsCustomizeTabHost.onWindowVisible();
1275 if (!mWorkspaceLoading) {
1276 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001277 // We want to let Launcher draw itself at least once before we force it to build
1278 // layers on all the workspace pages, so that transitioning to Launcher from other
1279 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1280 // a true draw so we wait until the second preDraw call to be safe
1281 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001282 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001283 // We delay the layer building a bit in order to give
1284 // other message processing a time to run. In particular
1285 // this avoids a delay in hiding the IME if it was
1286 // currently shown, because doing that may involve
1287 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001288 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001289
Michael Jurka6ee21d22012-02-21 18:20:27 -08001290 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001291 return true;
1292 }
1293 });
1294 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001295 // When Launcher comes back to foreground, a different Activity might be responsible for
1296 // the app market intent, so refresh the icon
1297 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001298 clearTypedText();
1299 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001300 }
1301
1302 private void sendAdvanceMessage(long delay) {
1303 mHandler.removeMessages(ADVANCE_MSG);
1304 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1305 mHandler.sendMessageDelayed(msg, delay);
1306 mAutoAdvanceSentTime = System.currentTimeMillis();
1307 }
1308
1309 private void updateRunning() {
1310 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1311 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1312 mAutoAdvanceRunning = autoAdvanceRunning;
1313 if (autoAdvanceRunning) {
1314 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1315 sendAdvanceMessage(delay);
1316 } else {
1317 if (!mWidgetsToAdvance.isEmpty()) {
1318 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1319 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1320 }
1321 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001322 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001323 }
1324 }
1325 }
1326
1327 private final Handler mHandler = new Handler() {
1328 @Override
1329 public void handleMessage(Message msg) {
1330 if (msg.what == ADVANCE_MSG) {
1331 int i = 0;
1332 for (View key: mWidgetsToAdvance.keySet()) {
1333 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1334 final int delay = mAdvanceStagger * i;
1335 if (v instanceof Advanceable) {
1336 postDelayed(new Runnable() {
1337 public void run() {
1338 ((Advanceable) v).advance();
1339 }
1340 }, delay);
1341 }
1342 i++;
1343 }
1344 sendAdvanceMessage(mAdvanceInterval);
1345 }
1346 }
1347 };
1348
1349 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001350 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001351 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1352 if (v instanceof Advanceable) {
1353 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001354 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 updateRunning();
1356 }
1357 }
1358
1359 void removeWidgetToAutoAdvance(View hostView) {
1360 if (mWidgetsToAdvance.containsKey(hostView)) {
1361 mWidgetsToAdvance.remove(hostView);
1362 updateRunning();
1363 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 }
1365
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001368 launcherInfo.hostView = null;
1369 }
1370
Winson Chung93eef082012-03-23 15:59:27 -07001371 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1372 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1373 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001374 }
1375
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001376 public LauncherAppWidgetHost getAppWidgetHost() {
1377 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Winson Chunga9abd0e2010-10-27 17:18:37 -07001380 public LauncherModel getModel() {
1381 return mModel;
1382 }
1383
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001384 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001385 getWindow().closeAllPanels();
1386
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001387 // Whatever we were doing is hereby canceled.
1388 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001389 }
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 @Override
1392 protected void onNewIntent(Intent intent) {
1393 super.onNewIntent(intent);
1394
1395 // Close the menu
1396 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001397 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001398 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001399
Jamie Genniscb222e82012-10-23 15:44:26 -07001400 final boolean alreadyOnHome =
1401 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001402 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001403
Jamie Genniscb222e82012-10-23 15:44:26 -07001404 Runnable processIntent = new Runnable() {
1405 public void run() {
1406 Folder openFolder = mWorkspace.getOpenFolder();
1407 // In all these cases, only animate if we're already on home
1408 mWorkspace.exitWidgetResizeMode();
1409 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1410 openFolder == null) {
1411 mWorkspace.moveToDefaultScreen(true);
1412 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001413
Jamie Genniscb222e82012-10-23 15:44:26 -07001414 closeFolder();
1415 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001416
Jamie Genniscb222e82012-10-23 15:44:26 -07001417 // If we are already on home, then just animate back to the workspace,
1418 // otherwise, just wait until onResume to set the state back to Workspace
1419 if (alreadyOnHome) {
1420 showWorkspace(true);
1421 } else {
1422 mOnResumeState = State.WORKSPACE;
1423 }
1424
1425 final View v = getWindow().peekDecorView();
1426 if (v != null && v.getWindowToken() != null) {
1427 InputMethodManager imm = (InputMethodManager)getSystemService(
1428 INPUT_METHOD_SERVICE);
1429 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1430 }
1431
1432 // Reset AllApps to its initial state
1433 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1434 mAppsCustomizeTabHost.reset();
1435 }
1436 }
1437 };
1438
1439 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1440 // Delay processing of the intent to allow the status bar animation to finish
1441 // first in order to avoid janky animations.
1442 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001443 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001444 // Process the intent immediately.
1445 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001446 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449 }
1450
1451 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001452 public void onRestoreInstanceState(Bundle state) {
1453 super.onRestoreInstanceState(state);
1454 for (int page: mSynchronouslyBoundPages) {
1455 mWorkspace.restoreInstanceStateForChild(page);
1456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458
1459 @Override
1460 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001461 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001462 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463
Michael Jurka883f55b2010-10-21 15:47:14 -07001464 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001465 // We close any open folder since it will not be re-opened, and we need to make sure
1466 // this state is reflected.
1467 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1470 mWaitingForResult) {
1471 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1472 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1473 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1474 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001475 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1476 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1477 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
1480 if (mFolderInfo != null && mWaitingForResult) {
1481 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1482 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1483 }
Michael Jurka946ad472010-07-09 18:05:18 -07001484
Winson Chung785d2eb2011-04-14 16:08:02 -07001485 // Save the current AppsCustomize tab
1486 if (mAppsCustomizeTabHost != null) {
1487 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1488 if (currentTabTag != null) {
1489 outState.putString("apps_customize_currentTab", currentTabTag);
1490 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001491 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1492 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001493 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 }
1495
1496 @Override
1497 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Winson Chunge7a03942011-08-05 15:05:12 -07001500 // Remove all pending runnables
1501 mHandler.removeMessages(ADVANCE_MSG);
1502 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001503 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001504
Winson Chungcd2b0142011-06-08 16:02:26 -07001505 // Stop callbacks from LauncherModel
1506 LauncherApplication app = ((LauncherApplication) getApplication());
1507 mModel.stopLoader();
1508 app.setLauncher(null);
1509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001511 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001513 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001515 mAppWidgetHost = null;
1516
1517 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518
1519 TextKeyListener.getInstance().release();
1520
Winson Chung81b52252012-08-27 15:34:29 -07001521 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1522 // to prevent leaking Launcher activities on orientation change.
1523 if (mModel != null) {
1524 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1525 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001526
1527 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001528 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001529
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001530 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001531 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1532 mWorkspace.removeAllViews();
1533 mWorkspace = null;
1534 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001535
Michael Jurka2ecf9952012-06-18 12:52:28 -07001536 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
Adam Cohena9cf38f2011-05-02 15:36:58 -07001539 public DragController getDragController() {
1540 return mDragController;
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 @Override
1544 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001545 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 super.startActivityForResult(intent, requestCode);
1547 }
1548
Winson Chung70d72102011-08-12 11:24:35 -07001549 /**
1550 * Indicates that we want global search for this activity by setting the globalSearch
1551 * argument for {@link #startSearch} to true.
1552 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001554 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001556
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001557 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Karl Rosaen138a0412009-04-23 19:00:21 -07001559 if (initialQuery == null) {
1560 // Use any text typed in the launcher as the initial query
1561 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 if (appSearchData == null) {
1564 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001565 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
Winson Chungadf0c182012-08-23 14:59:07 -07001567 Rect sourceBounds = new Rect();
1568 if (mSearchDropTargetBar != null) {
1569 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1570 }
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Michael Jurkaa33411c2012-06-14 16:18:21 -07001572 startGlobalSearch(initialQuery, selectInitialQuery,
1573 appSearchData, sourceBounds);
1574 }
1575
1576 /**
1577 * Starts the global search activity. This code is a copied from SearchManager
1578 */
1579 public void startGlobalSearch(String initialQuery,
1580 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001581 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001582 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1583 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1584 if (globalSearchActivity == null) {
1585 Log.w(TAG, "No global search activity found.");
1586 return;
1587 }
1588 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1589 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1590 intent.setComponent(globalSearchActivity);
1591 // Make sure that we have a Bundle to put source in
1592 if (appSearchData == null) {
1593 appSearchData = new Bundle();
1594 } else {
1595 appSearchData = new Bundle(appSearchData);
1596 }
1597 // Set source to package name of app that starts global search, if not set already.
1598 if (!appSearchData.containsKey("source")) {
1599 appSearchData.putString("source", getPackageName());
1600 }
1601 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1602 if (!TextUtils.isEmpty(initialQuery)) {
1603 intent.putExtra(SearchManager.QUERY, initialQuery);
1604 }
1605 if (selectInitialQuery) {
1606 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1607 }
1608 intent.setSourceBounds(sourceBounds);
1609 try {
1610 startActivity(intent);
1611 } catch (ActivityNotFoundException ex) {
1612 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
1615
Winson Chung70d72102011-08-12 11:24:35 -07001616 @Override
1617 public boolean onCreateOptionsMenu(Menu menu) {
1618 if (isWorkspaceLocked()) {
1619 return false;
1620 }
1621
1622 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001623
1624 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1625 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1626 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001627 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1628 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1629 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001630 String helpUrl = getString(R.string.help_url);
1631 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001632 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1633 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1634
Winson Chung70d72102011-08-12 11:24:35 -07001635 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1636 .setIcon(android.R.drawable.ic_menu_gallery)
1637 .setAlphabeticShortcut('W');
1638 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1639 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001640 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001641 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001642 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1643 .setIcon(android.R.drawable.ic_menu_preferences)
1644 .setIntent(settings)
1645 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001646 if (!helpUrl.isEmpty()) {
1647 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1648 .setIcon(android.R.drawable.ic_menu_help)
1649 .setIntent(help)
1650 .setAlphabeticShortcut('H');
1651 }
Winson Chung70d72102011-08-12 11:24:35 -07001652 return true;
1653 }
1654
1655 @Override
1656 public boolean onPrepareOptionsMenu(Menu menu) {
1657 super.onPrepareOptionsMenu(menu);
1658
1659 if (mAppsCustomizeTabHost.isTransitioning()) {
1660 return false;
1661 }
1662 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1663 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1664
1665 return true;
1666 }
1667
1668 @Override
1669 public boolean onOptionsItemSelected(MenuItem item) {
1670 switch (item.getItemId()) {
1671 case MENU_WALLPAPER_SETTINGS:
1672 startWallpaper();
1673 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001674 }
1675
1676 return super.onOptionsItemSelected(item);
1677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001679 @Override
1680 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001681 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001682 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001683 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001684 }
1685
Joe Onorato9c1289c2009-08-17 11:03:03 -04001686 public boolean isWorkspaceLocked() {
1687 return mWorkspaceLoading || mWaitingForResult;
1688 }
1689
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001691 mPendingAddInfo.container = ItemInfo.NO_ID;
1692 mPendingAddInfo.screen = -1;
1693 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1694 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001695 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001696 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001697 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001698
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001699 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1700 AppWidgetProviderInfo appWidgetInfo) {
1701 if (appWidgetInfo.configure != null) {
1702 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001703
Bjorn Bringert7984c942009-12-09 15:38:25 +00001704 // Launch over to configure widget, if needed
1705 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001706 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001707 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001708 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001710 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001711 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1712 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001713 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001714 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
1716 }
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Adam Cohenfbba09b2011-07-18 21:43:05 -07001718 /**
1719 * Process a shortcut drop.
1720 *
1721 * @param componentName The name of the component
1722 * @param screen The screen where it should be added
1723 * @param cell The cell it should be added to, optional
1724 * @param position The location on the screen where it was dropped, optional
1725 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001726 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1727 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001729 mPendingAddInfo.container = container;
1730 mPendingAddInfo.screen = screen;
1731 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001732
1733 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001734 mPendingAddInfo.cellX = cell[0];
1735 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001736 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737
1738 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1739 createShortcutIntent.setComponent(componentName);
1740 processShortcut(createShortcutIntent);
1741 }
1742
Adam Cohenfbba09b2011-07-18 21:43:05 -07001743 /**
1744 * Process a widget drop.
1745 *
1746 * @param info The PendingAppWidgetInfo of the widget being added.
1747 * @param screen The screen where it should be added
1748 * @param cell The cell it should be added to, optional
1749 * @param position The location on the screen where it was dropped, optional
1750 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001751 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001752 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001753 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001754 mPendingAddInfo.container = info.container = container;
1755 mPendingAddInfo.screen = info.screen = screen;
1756 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001757 mPendingAddInfo.minSpanX = info.minSpanX;
1758 mPendingAddInfo.minSpanY = info.minSpanY;
1759
Adam Cohenfbba09b2011-07-18 21:43:05 -07001760 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001761 mPendingAddInfo.cellX = cell[0];
1762 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001763 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001764 if (span != null) {
1765 mPendingAddInfo.spanX = span[0];
1766 mPendingAddInfo.spanY = span[1];
1767 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001768
Adam Cohened66b2b2012-01-23 17:28:51 -08001769 AppWidgetHostView hostView = info.boundWidget;
1770 int appWidgetId;
1771 if (hostView != null) {
1772 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001773 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001774 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001775 // In this case, we either need to start an activity to get permission to bind
1776 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001777 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001778 Bundle options = info.bindOptions;
1779
1780 boolean success = false;
1781 if (options != null) {
1782 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1783 info.componentName, options);
1784 } else {
1785 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1786 info.componentName);
1787 }
1788 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001789 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001790 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001791 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001792 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1793 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1794 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001795 // TODO: we need to make sure that this accounts for the options bundle.
1796 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001797 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1798 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001799 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001800 }
1801
Joe Onoratodeb98af2010-02-19 14:59:39 -08001802 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001803 // Handle case where user selected "Applications"
1804 String applicationName = getResources().getString(R.string.group_applications);
1805 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001806
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001807 if (applicationName != null && applicationName.equals(shortcutName)) {
1808 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1809 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001810
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001811 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1812 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001813 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001814 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001815 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001816 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
1819
Winson Chung24ab2f12010-09-16 14:10:47 -07001820 void processWallpaper(Intent intent) {
1821 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1822 }
1823
Winson Chung3d503fb2011-07-13 17:25:49 -07001824 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1825 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001826 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 folderInfo.title = getText(R.string.folder_name);
1828
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001830 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1831 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001832 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
1834 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001835 FolderIcon newFolder =
1836 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1837 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1838 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001839 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
Romain Guycbb89e42009-06-08 15:52:54 -07001841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001843 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 }
1845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001847 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001849 Intent chooser = Intent.createChooser(pickWallpaper,
1850 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001851 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1852 // Removed in Eclair MR1
1853// WallpaperManager wm = (WallpaperManager)
1854// getSystemService(Context.WALLPAPER_SERVICE);
1855// WallpaperInfo wi = wm.getWallpaperInfo();
1856// if (wi != null && wi.getSettingsActivity() != null) {
1857// LabeledIntent li = new LabeledIntent(getPackageName(),
1858// R.string.configure_wallpaper, 0);
1859// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1860// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1861// }
Mike Clerona0618e42009-10-22 13:55:21 -07001862 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001865 /**
1866 * Registers various content observers. The current implementation registers
1867 * only a favorites observer to keep track of the favorites applications.
1868 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001869 private void registerContentObservers() {
1870 ContentResolver resolver = getContentResolver();
1871 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1872 true, mWidgetObserver);
1873 }
1874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 @Override
1876 public boolean dispatchKeyEvent(KeyEvent event) {
1877 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1878 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001880 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001881 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001882 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001883 dumpState();
1884 return true;
1885 }
1886 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001887 }
1888 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1889 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001890 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return true;
1892 }
1893 }
1894
1895 return super.dispatchKeyEvent(event);
1896 }
1897
Joe Onorato88ec0992009-11-19 13:16:06 -08001898 @Override
1899 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001900 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001901 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001902 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001903 Folder openFolder = mWorkspace.getOpenFolder();
1904 if (openFolder.isEditingName()) {
1905 openFolder.dismissEditingName();
1906 } else {
1907 closeFolder();
1908 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001909 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001910 mWorkspace.exitWidgetResizeMode();
1911
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001912 // Back button is a no-op here, but give at least some feedback for the button press
1913 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001914 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001915 }
1916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001918 * Re-listen when widgets are reset.
1919 */
1920 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001921 if (mAppWidgetHost != null) {
1922 mAppWidgetHost.startListening();
1923 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001924 }
1925
1926 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 * Launches the intent referred by the clicked shortcut.
1928 *
1929 * @param v The view representing the clicked shortcut.
1930 */
1931 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001932 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1933 // view has detached (it's possible for this to happen if the view is removed mid touch).
1934 if (v.getWindowToken() == null) {
1935 return;
1936 }
1937
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001938 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001939 return;
1940 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001943 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001945 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001946 int[] pos = new int[2];
1947 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001948 intent.setSourceBounds(new Rect(pos[0], pos[1],
1949 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001950
1951 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001952
1953 if (success && v instanceof BubbleTextView) {
1954 mWaitingForResume = (BubbleTextView) v;
1955 mWaitingForResume.setStayPressed(true);
1956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001958 if (v instanceof FolderIcon) {
1959 FolderIcon fi = (FolderIcon) v;
1960 handleFolderClick(fi);
1961 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001962 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001963 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001964 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001965 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001966 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969 }
1970
Michael Jurka0e260592010-06-30 17:07:39 -07001971 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001972 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001973 // clicking anywhere on the workspace causes the customization drawer to slide down
1974 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001975 return false;
1976 }
1977
Michael Jurkaaf442092010-06-10 17:01:57 -07001978 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001979 * Event handler for the search button
1980 *
1981 * @param v The view that was clicked.
1982 */
1983 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001984 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1985
Amith Yamasania135ba82011-08-09 17:42:01 -07001986 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001987 }
1988
1989 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001990 * Event handler for the voice button
1991 *
1992 * @param v The view that was clicked.
1993 */
1994 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001995 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001996
Michael Jurkaae65ee32012-04-30 11:45:54 -07001997 try {
1998 final SearchManager searchManager =
1999 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2000 ComponentName activityName = searchManager.getGlobalSearchActivity();
2001 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002002 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002003 if (activityName != null) {
2004 intent.setPackage(activityName.getPackageName());
2005 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002006 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002007 } catch (ActivityNotFoundException e) {
2008 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002009 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002010 startActivitySafely(null, intent, "onClickVoiceButton");
2011 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002012 }
2013
2014 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002015 * Event handler for the "grid" button that appears on the home screen, which
2016 * enters all apps mode.
2017 *
2018 * @param v The view that was clicked.
2019 */
2020 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002021 showAllApps(true);
2022 }
2023
2024 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002025 // Provide the same haptic feedback that the system offers for virtual keys.
2026 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002027 }
2028
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002029 public void onClickAppMarketButton(View v) {
2030 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002031 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002032 } else {
2033 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002034 }
2035 }
2036
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002037 void startApplicationDetailsActivity(ComponentName componentName) {
2038 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002039 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2040 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002041 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002042 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002043 }
2044
Patrick Dubroy5539af72010-09-07 15:22:01 -07002045 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2046 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2047 // System applications cannot be installed. For now, show a toast explaining that.
2048 // We may give them the option of disabling apps this way.
2049 int messageId = R.string.uninstall_system_app_text;
2050 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2051 } else {
2052 String packageName = appInfo.componentName.getPackageName();
2053 String className = appInfo.componentName.getClassName();
2054 Intent intent = new Intent(
2055 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002056 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2057 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002058 startActivity(intent);
2059 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002060 }
2061
Michael Jurka86a720e2012-05-09 11:23:23 -07002062 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002066 // Only launch using the new animation if the shortcut has not opted out (this is a
2067 // private contract between launcher and may be ignored in the future).
2068 boolean useLaunchAnimation = (v != null) &&
2069 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2070 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002071 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2072 v.getMeasuredWidth(), v.getMeasuredHeight());
2073
2074 startActivity(intent, opts.toBundle());
2075 } else {
2076 startActivity(intent);
2077 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002078 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 } catch (SecurityException e) {
2080 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002081 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002083 "or use the exported attribute for this activity. "
2084 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002086 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002088
Michael Jurka86a720e2012-05-09 11:23:23 -07002089 boolean startActivitySafely(View v, Intent intent, Object tag) {
2090 boolean success = false;
2091 try {
2092 success = startActivity(v, intent, tag);
2093 } catch (ActivityNotFoundException e) {
2094 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2095 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2096 }
2097 return success;
2098 }
2099
Romain Guy8e633c52010-03-04 12:51:36 -08002100 void startActivityForResultSafely(Intent intent, int requestCode) {
2101 try {
2102 startActivityForResult(intent, requestCode);
2103 } catch (ActivityNotFoundException e) {
2104 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2105 } catch (SecurityException e) {
2106 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2107 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2108 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2109 "or use the exported attribute for this activity.", e);
2110 }
2111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112
Adam Cohena9cf38f2011-05-02 15:36:58 -07002113 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002114 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002115 Folder openFolder = mWorkspace.getFolderForTag(info);
2116
2117 // If the folder info reports that the associated folder is open, then verify that
2118 // it is actually opened. There have been a few instances where this gets out of sync.
2119 if (info.opened && openFolder == null) {
2120 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2121 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2122 info.opened = false;
2123 }
2124
Adam Cohenfb91f302012-06-11 15:45:18 -07002125 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 // Close any open folder
2127 closeFolder();
2128 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002129 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 } else {
2131 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 int folderScreen;
2133 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002134 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 // .. and close it
2136 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002137 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 // Close any folder open on the current screen
2139 closeFolder();
2140 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002141 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 }
2143 }
2144 }
2145 }
2146
Adam Cohen268c4752012-06-06 17:47:33 -07002147 /**
2148 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2149 * in the DragLayer in the exact absolute location of the original FolderIcon.
2150 */
2151 private void copyFolderIconToImage(FolderIcon fi) {
2152 final int width = fi.getMeasuredWidth();
2153 final int height = fi.getMeasuredHeight();
2154
2155 // Lazy load ImageView, Bitmap and Canvas
2156 if (mFolderIconImageView == null) {
2157 mFolderIconImageView = new ImageView(this);
2158 }
2159 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2160 mFolderIconBitmap.getHeight() != height) {
2161 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2162 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2163 }
2164
2165 DragLayer.LayoutParams lp;
2166 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2167 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2168 } else {
2169 lp = new DragLayer.LayoutParams(width, height);
2170 }
2171
Adam Cohen307fe232012-08-16 17:55:58 -07002172 // The layout from which the folder is being opened may be scaled, adjust the starting
2173 // view size by this scale factor.
2174 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002175 lp.customPosition = true;
2176 lp.x = mRectForFolderAnimation.left;
2177 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002178 lp.width = (int) (scale * width);
2179 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002180
2181 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2182 fi.draw(mFolderIconCanvas);
2183 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002184 if (fi.getFolder() != null) {
2185 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2186 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002187 }
Adam Cohen268c4752012-06-06 17:47:33 -07002188 // Just in case this image view is still in the drag layer from a previous animation,
2189 // we remove it and re-add it.
2190 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2191 mDragLayer.removeView(mFolderIconImageView);
2192 }
2193 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002194 if (fi.getFolder() != null) {
2195 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002196 }
Adam Cohen268c4752012-06-06 17:47:33 -07002197 }
2198
Adam Cohen2801caf2011-05-13 20:57:39 -07002199 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002200 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002201 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2202 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2203 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2204
Adam Cohenc51934b2011-07-26 21:07:43 -07002205 FolderInfo info = (FolderInfo) fi.getTag();
2206 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2207 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002208 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2209 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002210 }
2211
Adam Cohen268c4752012-06-06 17:47:33 -07002212 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2213 copyFolderIconToImage(fi);
2214 fi.setVisibility(View.INVISIBLE);
2215
Michael Jurka2ecf9952012-06-18 12:52:28 -07002216 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002217 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002218 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2219 oa.start();
2220 }
2221
Adam Cohen268c4752012-06-06 17:47:33 -07002222 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002223 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002224 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2225 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2226 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2227
Adam Cohen268c4752012-06-06 17:47:33 -07002228 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002229
Adam Cohen268c4752012-06-06 17:47:33 -07002230 // We remove and re-draw the FolderIcon in-case it has changed
2231 mDragLayer.removeView(mFolderIconImageView);
2232 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002233 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002234 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002235 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002236 oa.addListener(new AnimatorListenerAdapter() {
2237 @Override
2238 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002239 if (cl != null) {
2240 cl.clearFolderLeaveBehind();
2241 // Remove the ImageView copy of the FolderIcon and make the original visible.
2242 mDragLayer.removeView(mFolderIconImageView);
2243 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002244 }
2245 }
2246 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002247 oa.start();
2248 }
2249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002251 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 * is animated relative to the specified View. If the View is null, no animation
2253 * is played.
2254 *
2255 * @param folderInfo The FolderInfo describing the folder to open.
2256 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002257 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002258 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002259 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260
Adam Cohena9cf38f2011-05-02 15:36:58 -07002261 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262
Adam Cohen4554ee12011-08-03 16:13:21 -07002263 // Just verify that the folder hasn't already been added to the DragLayer.
2264 // There was a one-off crash where the folder had a parent already.
2265 if (folder.getParent() == null) {
2266 mDragLayer.addView(folder);
2267 mDragController.addDropTarget((DropTarget) folder);
2268 } else {
2269 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2270 folder.getParent() + ").");
2271 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002272 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002273 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002274 }
2275
2276 public void closeFolder() {
2277 Folder folder = mWorkspace.getOpenFolder();
2278 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002279 if (folder.isEditingName()) {
2280 folder.dismissEditingName();
2281 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002282 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002283
2284 // Dismiss the folder cling
2285 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002286 }
2287 }
2288
2289 void closeFolder(Folder folder) {
2290 folder.getInfo().opened = false;
2291
2292 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2293 if (parent != null) {
2294 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2295 shrinkAndFadeInFolderIcon(fi);
2296 }
2297 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 }
2299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002301 if (!isDraggingEnabled()) return false;
2302 if (isWorkspaceLocked()) return false;
2303 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304
2305 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002306 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 }
2308
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 resetAddInfo();
2310 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002312 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 return true;
2314 }
2315
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 // The hotseat touch handling does not go through Workspace, and we always allow long press
2317 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002318 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2320 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 if (itemUnderLongClick == null) {
2322 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002323 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2324 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002325 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002326 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002327 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002329 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002330 }
2331 }
2332 }
2333 return true;
2334 }
2335
Winson Chung3d503fb2011-07-13 17:25:49 -07002336 boolean isHotseatLayout(View layout) {
2337 return mHotseat != null && layout != null &&
2338 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2339 }
2340 Hotseat getHotseat() {
2341 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002342 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002343 SearchDropTargetBar getSearchBar() {
2344 return mSearchDropTargetBar;
2345 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002346
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 /**
2348 * Returns the CellLayout of the specified container at the specified screen.
2349 */
2350 CellLayout getCellLayout(long container, int screen) {
2351 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2352 if (mHotseat != null) {
2353 return mHotseat.getLayout();
2354 } else {
2355 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002356 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002357 } else {
2358 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002359 }
2360 }
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 Workspace getWorkspace() {
2363 return mWorkspace;
2364 }
2365
Daniel Sandler843e8602010-06-07 14:59:01 -04002366 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002367 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002368 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002369 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002370 }
2371
Craig Mautner360310b2012-10-26 15:13:08 -07002372 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002373 public boolean isAllAppsButtonRank(int rank) {
2374 return mHotseat.isAllAppsButtonRank(rank);
2375 }
2376
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002377 /**
2378 * Helper method for the cameraZoomIn/cameraZoomOut animations
2379 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002380 * @param scaleFactor The scale factor used for the zoom
2381 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002382 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002383 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002384 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002385 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002386
Winson Chung18f41f82012-05-09 13:28:10 -07002387 void disableWallpaperIfInAllApps() {
2388 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002389 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002390 if (mAppsCustomizeTabHost != null &&
2391 !mAppsCustomizeTabHost.isTransitioning()) {
2392 updateWallpaperVisibility(false);
2393 }
2394 }
2395 }
2396
Craig Mautner360310b2012-10-26 15:13:08 -07002397 private void setWorkspaceBackground(boolean workspace) {
2398 mLauncherView.setBackground(workspace ?
2399 mWorkspaceBackgroundDrawable : mBlackBackgroundDrawable);
2400 }
2401
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002402 void updateWallpaperVisibility(boolean visible) {
2403 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2404 int curflags = getWindow().getAttributes().flags
2405 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2406 if (wpflags != curflags) {
2407 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2408 }
Craig Mautner360310b2012-10-26 15:13:08 -07002409 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002410 }
2411
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002412 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2413 if (v instanceof LauncherTransitionable) {
2414 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2415 }
2416 }
2417
Michael Jurkabed61d22012-02-14 22:51:29 -08002418 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2419 if (v instanceof LauncherTransitionable) {
2420 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2421 }
Winson Chung70442722012-02-10 15:43:22 -08002422
2423 // Update the workspace transition step as well
2424 dispatchOnLauncherTransitionStep(v, 0f);
2425 }
2426
2427 private void dispatchOnLauncherTransitionStep(View v, float t) {
2428 if (v instanceof LauncherTransitionable) {
2429 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2430 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002431 }
2432
2433 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2434 if (v instanceof LauncherTransitionable) {
2435 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2436 }
Winson Chung70442722012-02-10 15:43:22 -08002437
2438 // Update the workspace transition step as well
2439 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002440 }
2441
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002443 * Things to test when changing the following seven functions.
2444 * - Home from workspace
2445 * - from center screen
2446 * - from other screens
2447 * - Home from all apps
2448 * - from center screen
2449 * - from other screens
2450 * - Back from all apps
2451 * - from center screen
2452 * - from other screens
2453 * - Launch app from workspace and quit
2454 * - with back
2455 * - with home
2456 * - Launch app from all apps and quit
2457 * - with back
2458 * - with home
2459 * - Go to a screen that's not the default, then all
2460 * apps, and launch and app, and go back
2461 * - with back
2462 * -with home
2463 * - On workspace, long press power and go back
2464 * - with back
2465 * - with home
2466 * - On all apps, long press power and go back
2467 * - with back
2468 * - with home
2469 * - On workspace, power off
2470 * - On all apps, power off
2471 * - Launch an app and turn off the screen while in that app
2472 * - Go back with home key
2473 * - Go back with back key TODO: make this not go to workspace
2474 * - From all apps
2475 * - From workspace
2476 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2477 * - From all apps
2478 * - From the center workspace
2479 * - From another workspace
2480 */
2481
2482 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002483 * Zoom the camera out from the workspace to reveal 'toView'.
2484 * Assumes that the view to show is anchored at either the very top or very bottom
2485 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002486 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002487 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002488 if (mStateAnimation != null) {
2489 mStateAnimation.cancel();
2490 mStateAnimation = null;
2491 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002492 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002493
Winson Chungf0ea4d32011-06-06 14:27:16 -07002494 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2495 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2496 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002497 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002498 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002499 final int startDelay =
2500 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002501
Michael Jurkab3e22d92011-10-31 15:58:33 -07002502 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503
Michael Jurkad74c9842011-07-10 12:44:21 -07002504 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 Animator workspaceAnim =
2506 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002507
2508 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002509 toView.setScaleX(scale);
2510 toView.setScaleY(scale);
2511 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2512 scaleAnim.
2513 scaleX(1f).scaleY(1f).
2514 setDuration(duration).
2515 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002516
Winson Chungf0ea4d32011-06-06 14:27:16 -07002517 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002518 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002519 final ObjectAnimator alphaAnim = ObjectAnimator
2520 .ofFloat(toView, "alpha", 0f, 1f)
2521 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002522 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002523 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2524 @Override
2525 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002526 if (animation == null) {
2527 throw new RuntimeException("animation is null");
2528 }
Winson Chung70442722012-02-10 15:43:22 -08002529 float t = (Float) animation.getAnimatedValue();
2530 dispatchOnLauncherTransitionStep(fromView, t);
2531 dispatchOnLauncherTransitionStep(toView, t);
2532 }
2533 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002534
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002535 // toView should appear right at the end of the workspace shrink
2536 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002537 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002538 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002539 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002540
2541 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002542 boolean animationCancelled = false;
2543
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002544 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002545 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002546 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 // Prepare the position
2548 toView.setTranslationX(0.0f);
2549 toView.setTranslationY(0.0f);
2550 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002551 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002552 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002553 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002554 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002555 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2556 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002557
Michael Jurkafa7969f2012-09-21 16:39:14 -07002558 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002559 // Hide the workspace scrollbar
2560 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002561 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002562 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002563 if (!animationCancelled) {
2564 updateWallpaperVisibility(false);
2565 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002566
2567 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002568 if (mSearchDropTargetBar != null) {
2569 mSearchDropTargetBar.hideSearchBar(false);
2570 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002571 }
2572
Adam Cohencff6af82011-09-13 14:51:53 -07002573 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002574 public void onAnimationCancel(Animator animation) {
2575 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002576 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 });
2578
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002579 if (workspaceAnim != null) {
2580 mStateAnimation.play(workspaceAnim);
2581 }
Michael Jurka1899a362011-11-03 13:50:45 -07002582
2583 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002584 final ViewTreeObserver observer;
2585
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002586 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2587 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002588
2589 // If any of the objects being animated haven't been measured/laid out
2590 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002591 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002592 (mWorkspace.getMeasuredWidth() == 0) ||
2593 (toView.getMeasuredWidth() == 0)) {
2594 observer = mWorkspace.getViewTreeObserver();
2595 delayAnim = true;
2596 } else {
2597 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002598 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002599
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002600 final AnimatorSet stateAnimation = mStateAnimation;
2601 final Runnable startAnimRunnable = new Runnable() {
2602 public void run() {
2603 // Check that mStateAnimation hasn't changed while
2604 // we waited for a layout/draw pass
2605 if (mStateAnimation != stateAnimation)
2606 return;
2607 setPivotsForZoom(toView, scale);
2608 dispatchOnLauncherTransitionStart(fromView, animated, false);
2609 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002610 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002611 public void run() {
2612 // Check that mStateAnimation hasn't changed while
2613 // we waited for a layout/draw pass
2614 if (mStateAnimation != stateAnimation)
2615 return;
2616 mStateAnimation.start();
2617 }
2618 });
2619 }
2620 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002621 if (delayAnim) {
2622 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2623 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002624 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002625 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002626 }
2627 };
2628 observer.addOnGlobalLayoutListener(delayedStart);
2629 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002630 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002631 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 } else {
2633 toView.setTranslationX(0.0f);
2634 toView.setTranslationY(0.0f);
2635 toView.setScaleX(1.0f);
2636 toView.setScaleY(1.0f);
2637 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002638 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002639
Michael Jurkabed61d22012-02-14 22:51:29 -08002640 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2641 // Hide the workspace scrollbar
2642 mWorkspace.hideScrollingIndicator(true);
2643 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002644
2645 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002646 if (mSearchDropTargetBar != null) {
2647 mSearchDropTargetBar.hideSearchBar(false);
2648 }
Michael Jurkaabded662011-03-04 12:06:57 -08002649 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002650 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002651 dispatchOnLauncherTransitionStart(fromView, animated, false);
2652 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002653 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002654 dispatchOnLauncherTransitionStart(toView, animated, false);
2655 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002656 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 }
2659
2660 /**
2661 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002662 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002663 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002664 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002665 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2666 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002667
Michael Jurkab3e22d92011-10-31 15:58:33 -07002668 if (mStateAnimation != null) {
2669 mStateAnimation.cancel();
2670 mStateAnimation = null;
2671 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002672 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002673
Winson Chungf0ea4d32011-06-06 14:27:16 -07002674 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002675 final int fadeOutDuration =
2676 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002677 final float scaleFactor = (float)
2678 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2679 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002680 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002681 Animator workspaceAnim = null;
2682
2683 if (toState == State.WORKSPACE) {
2684 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2685 workspaceAnim = mWorkspace.getChangeStateAnimation(
2686 Workspace.State.NORMAL, animated, stagger);
2687 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2688 workspaceAnim = mWorkspace.getChangeStateAnimation(
2689 Workspace.State.SPRING_LOADED, animated);
2690 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002691
Michael Jurkab3e22d92011-10-31 15:58:33 -07002692 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002693 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002694 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002695 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002696 final LauncherViewPropertyAnimator scaleAnim =
2697 new LauncherViewPropertyAnimator(fromView);
2698 scaleAnim.
2699 scaleX(scaleFactor).scaleY(scaleFactor).
2700 setDuration(duration).
2701 setInterpolator(new Workspace.ZoomInInterpolator());
2702
2703 final ObjectAnimator alphaAnim = ObjectAnimator
2704 .ofFloat(fromView, "alpha", 1f, 0f)
2705 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002706 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002707 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2708 @Override
2709 public void onAnimationUpdate(ValueAnimator animation) {
2710 float t = 1f - (Float) animation.getAnimatedValue();
2711 dispatchOnLauncherTransitionStep(fromView, t);
2712 dispatchOnLauncherTransitionStep(toView, t);
2713 }
2714 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002715
Michael Jurka2ecf9952012-06-18 12:52:28 -07002716 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002717
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002718 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2719 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002720 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002721
2722 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002723 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002724 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002725 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002727 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2728 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002729 if (mWorkspace != null) {
2730 mWorkspace.hideScrollingIndicator(false);
2731 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002732 if (onCompleteRunnable != null) {
2733 onCompleteRunnable.run();
2734 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002735 mAppsCustomizeContent.updateCurrentPageScroll();
2736 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 }
2738 });
2739
Winson Chungf0ea4d32011-06-06 14:27:16 -07002740 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002741 if (workspaceAnim != null) {
2742 mStateAnimation.play(workspaceAnim);
2743 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002744 dispatchOnLauncherTransitionStart(fromView, animated, true);
2745 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002746 final Animator stateAnimation = mStateAnimation;
2747 mWorkspace.post(new Runnable() {
2748 public void run() {
2749 if (stateAnimation != mStateAnimation)
2750 return;
2751 mStateAnimation.start();
2752 }
2753 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002754 } else {
2755 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002756 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002757 dispatchOnLauncherTransitionStart(fromView, animated, true);
2758 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002759 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002760 dispatchOnLauncherTransitionStart(toView, animated, true);
2761 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002762 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002763 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002764 }
2765
Michael Jurkae326f182011-11-21 14:05:46 -08002766 @Override
2767 public void onTrimMemory(int level) {
2768 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002769 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002770 mAppsCustomizeTabHost.onTrimMemory();
2771 }
2772 }
2773
Winson Chung18f41f82012-05-09 13:28:10 -07002774 @Override
2775 public void onWindowFocusChanged(boolean hasFocus) {
2776 if (!hasFocus) {
2777 // When another window occludes launcher (like the notification shade, or recents),
2778 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2779 updateWallpaperVisibility(true);
2780 } else {
2781 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002782 mWorkspace.postDelayed(new Runnable() {
2783 @Override
2784 public void run() {
2785 disableWallpaperIfInAllApps();
2786 }
2787 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002788 }
2789 }
2790
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002791 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002792 showWorkspace(animated, null);
2793 }
2794
2795 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002796 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002797 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002798 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002799 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002800
Winson Chungc7d2b602012-05-16 17:02:20 -07002801 // Show the search bar (only animate if we were showing the drop target bar in spring
2802 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002803 if (mSearchDropTargetBar != null) {
2804 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2805 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002806
Michael Jurkab737ee62011-11-15 15:57:22 -08002807 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002808 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002809
2810 // Set focus to the AppsCustomize button
2811 if (mAllAppsButton != null) {
2812 mAllAppsButton.requestFocus();
2813 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002814 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002815
Michael Jurkab737ee62011-11-15 15:57:22 -08002816 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002817
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002818 // Change the state *after* we've called all the transition code
2819 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002820
Winson Chung5fb63472011-02-02 17:03:37 -08002821 // Resume the auto-advance of widgets
2822 mUserPresent = true;
2823 updateRunning();
2824
alanv1d4fde62012-10-17 13:15:47 -07002825 // Send an accessibility event to announce the context change
2826 getWindow().getDecorView()
2827 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Joe Onorato00acb122009-08-04 16:04:30 -04002828 }
2829
Michael Jurkab3e22d92011-10-31 15:58:33 -07002830 void showAllApps(boolean animated) {
2831 if (mState != State.WORKSPACE) return;
2832
2833 showAppsCustomizeHelper(animated, false);
2834 mAppsCustomizeTabHost.requestFocus();
2835
Michael Jurkab3e22d92011-10-31 15:58:33 -07002836 // Change the state *after* we've called all the transition code
2837 mState = State.APPS_CUSTOMIZE;
2838
2839 // Pause the auto-advance of widgets until we are out of AllApps
2840 mUserPresent = false;
2841 updateRunning();
2842 closeFolder();
2843
2844 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002845 getWindow().getDecorView()
2846 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002847 }
2848
Adam Cohen7777d962011-08-18 18:58:38 -07002849 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002850 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002851 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002852 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002853 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002854 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002855 }
Adam Cohen7777d962011-08-18 18:58:38 -07002856
Adam Cohened66b2b2012-01-23 17:28:51 -08002857 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2858 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002859 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2860
Winson Chunge7a03942011-08-05 15:05:12 -07002861 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002862 @Override
2863 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002864 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002865 // Before we show workspace, hide all apps again because
2866 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2867 // clean up our state transition functions
2868 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002869 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002870 } else {
2871 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002872 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002873 }
2874 }, (extendedDelay ?
2875 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2876 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2877 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002878
Michael Jurkad3ef3062010-11-23 16:23:58 -08002879 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002880 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002881 final boolean animated = true;
2882 final boolean springLoaded = true;
2883 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002884 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002885 }
2886 // Otherwise, we are not in spring loaded mode, so don't do anything.
2887 }
2888
Michael Jurkab737ee62011-11-15 15:57:22 -08002889 void hideDockDivider() {
2890 if (mQsbDivider != null && mDockDivider != null) {
2891 mQsbDivider.setVisibility(View.INVISIBLE);
2892 mDockDivider.setVisibility(View.INVISIBLE);
2893 }
2894 }
2895
2896 void showDockDivider(boolean animated) {
2897 if (mQsbDivider != null && mDockDivider != null) {
2898 mQsbDivider.setVisibility(View.VISIBLE);
2899 mDockDivider.setVisibility(View.VISIBLE);
2900 if (mDividerAnimator != null) {
2901 mDividerAnimator.cancel();
2902 mQsbDivider.setAlpha(1f);
2903 mDockDivider.setAlpha(1f);
2904 mDividerAnimator = null;
2905 }
2906 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002907 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2908 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2909 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002910 int duration = 0;
2911 if (mSearchDropTargetBar != null) {
2912 duration = mSearchDropTargetBar.getTransitionInDuration();
2913 }
2914 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002915 mDividerAnimator.start();
2916 }
2917 }
2918 }
2919
Michael Jurkab3e22d92011-10-31 15:58:33 -07002920 void lockAllApps() {
2921 // TODO
2922 }
2923
2924 void unlockAllApps() {
2925 // TODO
2926 }
2927
Winson Chungf0ea4d32011-06-06 14:27:16 -07002928 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002929 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002930 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002931 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002932 if (!LauncherApplication.isScreenLarge()) {
2933 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002934 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002935 int duration = 0;
2936 if (mSearchDropTargetBar != null) {
2937 duration = mSearchDropTargetBar.getTransitionInDuration();
2938 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002939 mHotseat.animate().alpha(1f).setDuration(duration);
2940 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002941 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002942 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002943 }
2944 }
2945 }
2946
2947 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002948 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002949 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002950 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002951 if (!LauncherApplication.isScreenLarge()) {
2952 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002953 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002954 int duration = 0;
2955 if (mSearchDropTargetBar != null) {
2956 duration = mSearchDropTargetBar.getTransitionOutDuration();
2957 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002958 mHotseat.animate().alpha(0f).setDuration(duration);
2959 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002961 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002962 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002963 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002964 }
2965
Patrick Dubroy5f445422011-02-18 14:35:21 -08002966 /**
2967 * Add an item from all apps or customize onto the given workspace screen.
2968 * If layout is null, add to the current screen.
2969 */
2970 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002971 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002972 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002973 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002974 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002975
Winson Chungdff8ebb2011-09-08 17:25:31 -07002976 /** Maps the current orientation to an index for referencing orientation correct global icons */
2977 private int getCurrentOrientationIndexForGlobalIcons() {
2978 // default - 0, landscape - 1
2979 switch (getResources().getConfiguration().orientation) {
2980 case Configuration.ORIENTATION_LANDSCAPE:
2981 return 1;
2982 default:
2983 return 0;
2984 }
2985 }
2986
Michael Jurkaae65ee32012-04-30 11:45:54 -07002987 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002988 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002989 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002990 // Look for the toolbar icon specified in the activity meta-data
2991 Bundle metaData = packageManager.getActivityInfo(
2992 activityName, PackageManager.GET_META_DATA).metaData;
2993 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002994 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002995 if (iconResId != 0) {
2996 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002997 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002998 }
2999 }
3000 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003001 // This can happen if the activity defines an invalid drawable
3002 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3003 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003004 } catch (Resources.NotFoundException nfe) {
3005 // This can happen if the activity defines an invalid drawable
3006 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3007 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003008 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003009 return null;
3010 }
3011
3012 // if successful in getting icon, return it; otherwise, set button to use default drawable
3013 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003014 int buttonId, ComponentName activityName, int fallbackDrawableId,
3015 String toolbarResourceName) {
3016 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003017 Resources r = getResources();
3018 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3019 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003020
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003021 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003022 // If we were unable to find the icon via the meta-data, use a generic one
3023 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003024 toolbarIcon = r.getDrawable(fallbackDrawableId);
3025 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003026 if (button != null) {
3027 button.setCompoundDrawables(toolbarIcon, null, null, null);
3028 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003029 return null;
3030 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003031 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003032 if (button != null) {
3033 button.setCompoundDrawables(toolbarIcon, null, null, null);
3034 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003035 return toolbarIcon.getConstantState();
3036 }
3037 }
3038
3039 // if successful in getting icon, return it; otherwise, set button to use default drawable
3040 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003041 int buttonId, ComponentName activityName, int fallbackDrawableId,
3042 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003043 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003044 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003045
Michael Jurka19e0fc52011-07-22 18:00:21 -07003046 if (button != null) {
3047 // If we were unable to find the icon via the meta-data, use a
3048 // generic one
3049 if (toolbarIcon == null) {
3050 button.setImageResource(fallbackDrawableId);
3051 } else {
3052 button.setImageDrawable(toolbarIcon);
3053 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003054 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003055
3056 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3057
Michael Jurka0423dcf2010-10-05 14:56:18 -07003058 }
3059
Winson Chung1b884092012-06-08 17:13:02 -07003060 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003061 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003062 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003063 }
3064
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003065 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003066 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003067 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003068 }
3069
Winson Chungbb185bd2011-11-21 12:31:42 -08003070 private void invalidatePressedFocusedStates(View container, View button) {
3071 if (container instanceof HolographicLinearLayout) {
3072 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3073 layout.invalidatePressedFocusedStates();
3074 } else if (button instanceof HolographicImageView) {
3075 HolographicImageView view = (HolographicImageView) button;
3076 view.invalidatePressedFocusedStates();
3077 }
3078 }
3079
Winson Chungc51db6a2011-10-05 11:44:49 -07003080 private boolean updateGlobalSearchIcon() {
3081 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003082 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003083 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003084 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003085 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003086
Winson Chung1cad91e2011-05-25 17:41:01 -07003087 final SearchManager searchManager =
3088 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3089 ComponentName activityName = searchManager.getGlobalSearchActivity();
3090 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003091 int coi = getCurrentOrientationIndexForGlobalIcons();
3092 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003093 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3094 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3095 if (sGlobalSearchIcon[coi] == null) {
3096 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3097 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3098 TOOLBAR_ICON_METADATA_NAME);
3099 }
3100
Winson Chungc51db6a2011-10-05 11:44:49 -07003101 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3102 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003103 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003104 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003105 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003106 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003107 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3108 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3109 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003110 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003111 if (voiceButtonProxy != null) {
3112 voiceButtonProxy.setVisibility(View.GONE);
3113 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003114 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003115 }
3116 }
3117
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003118 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003119 final View searchButtonContainer = findViewById(R.id.search_button_container);
3120 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003121 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003122 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 }
3124
Winson Chungc51db6a2011-10-05 11:44:49 -07003125 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003126 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003127 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003128 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003129
Winson Chungc51db6a2011-10-05 11:44:49 -07003130 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003131 final SearchManager searchManager =
3132 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3133 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3134
3135 ComponentName activityName = null;
3136 if (globalSearchActivity != null) {
3137 // Check if the global search activity handles voice search
3138 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3139 intent.setPackage(globalSearchActivity.getPackageName());
3140 activityName = intent.resolveActivity(getPackageManager());
3141 }
3142
3143 if (activityName == null) {
3144 // Fallback: check if an activity other than the global search activity
3145 // resolves this
3146 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3147 activityName = intent.resolveActivity(getPackageManager());
3148 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003149 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003150 int coi = getCurrentOrientationIndexForGlobalIcons();
3151 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003152 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3153 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3154 if (sVoiceSearchIcon[coi] == null) {
3155 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3156 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3157 TOOLBAR_ICON_METADATA_NAME);
3158 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003159 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003160 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003161 if (voiceButtonProxy != null) {
3162 voiceButtonProxy.setVisibility(View.VISIBLE);
3163 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003164 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003165 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003166 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003167 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003168 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003169 if (voiceButtonProxy != null) {
3170 voiceButtonProxy.setVisibility(View.GONE);
3171 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003172 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003173 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003174 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003175
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003176 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003177 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3178 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003179 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003180 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003181 }
3182
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003183 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003184 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003185 */
3186 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003187 final View marketButton = findViewById(R.id.market_button);
3188 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3189 // Find the app market activity by resolving an intent.
3190 // (If multiple app markets are installed, it will return the ResolverActivity.)
3191 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003192 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003193 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003194 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003195 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003196 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3197 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003198 marketButton.setVisibility(View.VISIBLE);
3199 } else {
3200 // We should hide and disable the view so that we don't try and restore the visibility
3201 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3202 marketButton.setVisibility(View.GONE);
3203 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003204 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003205 }
3206
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003207 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003208 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3209 Resources r = getResources();
3210 Drawable marketIconDrawable = d.newDrawable(r);
3211 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3212 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3213 marketIconDrawable.setBounds(0, 0, w, h);
3214
3215 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003216 }
3217
Michael Jurkad7c28052012-04-27 15:43:36 -07003218 @Override
3219 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003220 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003221 final List<CharSequence> text = event.getText();
3222 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003223 // Populate event with a fake title based on the current state.
3224 if (mState == State.APPS_CUSTOMIZE) {
3225 text.add(getString(R.string.all_apps_button_label));
3226 } else {
3227 text.add(getString(R.string.all_apps_home_button_label));
3228 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003229 return result;
3230 }
3231
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003232 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003233 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003234 */
3235 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3236 @Override
3237 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003238 closeSystemDialogs();
3239 }
3240 }
3241
3242 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003243 * Receives notifications whenever the appwidgets are reset.
3244 */
3245 private class AppWidgetResetObserver extends ContentObserver {
3246 public AppWidgetResetObserver() {
3247 super(new Handler());
3248 }
3249
3250 @Override
3251 public void onChange(boolean selfChange) {
3252 onAppWidgetReset();
3253 }
3254 }
3255
3256 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003257 * If the activity is currently paused, signal that we need to re-run the loader
3258 * in onResume.
3259 *
3260 * This needs to be called from incoming places where resources might have been loaded
3261 * while we are paused. That is becaues the Configuration might be wrong
3262 * when we're not running, and if it comes back to what it was when we
3263 * were paused, we are not restarted.
3264 *
3265 * Implementation of the method from LauncherModel.Callbacks.
3266 *
3267 * @return true if we are currently paused. The caller might be able to
3268 * skip some work in that case since we will come back again.
3269 */
3270 public boolean setLoadOnResume() {
3271 if (mPaused) {
3272 Log.i(TAG, "setLoadOnResume");
3273 mOnResumeNeedsLoad = true;
3274 return true;
3275 } else {
3276 return false;
3277 }
3278 }
3279
3280 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003282 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003284 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003285 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003286 } else {
3287 return SCREEN_COUNT / 2;
3288 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003290
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 /**
3292 * Refreshes the shortcuts shown on the workspace.
3293 *
3294 * Implementation of the method from LauncherModel.Callbacks.
3295 */
3296 public void startBinding() {
3297 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003298
Winson Chungf0c6ae02012-03-21 16:10:31 -07003299 mNewShortcutAnimatePage = -1;
3300 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003301 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 int count = workspace.getChildCount();
3303 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003304 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003305 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3306 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003308 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003309 if (mHotseat != null) {
3310 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003311 }
3312 }
3313
3314 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 * Bind the items start-end from the list.
3316 *
3317 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003318 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003320 setLoadOnResume();
3321
Winson Chungf0c6ae02012-03-21 16:10:31 -07003322 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3323 Set<String> newApps = new HashSet<String>();
3324 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3325
3326 Workspace workspace = mWorkspace;
3327 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003329
3330 // Short circuit if we are loading dock items for a configuration which has no dock
3331 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3332 mHotseat == null) {
3333 continue;
3334 }
3335
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 switch (item.itemType) {
3337 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3338 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003339 ShortcutInfo info = (ShortcutInfo) item;
3340 String uri = info.intent.toUri(0).toString();
3341 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003342 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3343 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003344 boolean animateIconUp = false;
3345 synchronized (newApps) {
3346 if (newApps.contains(uri)) {
3347 animateIconUp = newApps.remove(uri);
3348 }
3349 }
3350 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003351 // Prepare the view to be animated up
3352 shortcut.setAlpha(0f);
3353 shortcut.setScaleX(0f);
3354 shortcut.setScaleY(0f);
3355 mNewShortcutAnimatePage = item.screen;
3356 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3357 mNewShortcutAnimateViews.add(shortcut);
3358 }
3359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003361 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003362 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003363 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003364 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003365 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3366 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003369 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003370
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003372 }
3373
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 /**
3375 * Implementation of the method from LauncherModel.Callbacks.
3376 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003377 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003378 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003379 sFolders.clear();
3380 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382
3383 /**
3384 * Add the views for a widget to the workspace.
3385 *
3386 * Implementation of the method from LauncherModel.Callbacks.
3387 */
3388 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003389 setLoadOnResume();
3390
Daniel Sandler843e8602010-06-07 14:59:01 -04003391 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3392 if (DEBUG_WIDGETS) {
3393 Log.d(TAG, "bindAppWidget: " + item);
3394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 final Workspace workspace = mWorkspace;
3396
3397 final int appWidgetId = item.appWidgetId;
3398 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003399 if (DEBUG_WIDGETS) {
3400 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3401 }
3402
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3404
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003406 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407
Winson Chung3d503fb2011-07-13 17:25:49 -07003408 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003410 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3411
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 workspace.requestLayout();
3413
Daniel Sandler843e8602010-06-07 14:59:01 -04003414 if (DEBUG_WIDGETS) {
3415 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3416 + (SystemClock.uptimeMillis()-start) + "ms");
3417 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003418 }
3419
Adam Cohen1462de32012-07-24 22:34:36 -07003420 public void onPageBoundSynchronously(int page) {
3421 mSynchronouslyBoundPages.add(page);
3422 }
3423
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 /**
3425 * Callback saying that there aren't any more items to bind.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
3429 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003430 setLoadOnResume();
3431
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 if (mSavedState != null) {
3433 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003434 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003436 mSavedState = null;
3437 }
3438
Adam Cohen1462de32012-07-24 22:34:36 -07003439 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003441 // If we received the result of any pending adds while the loader was running (e.g. the
3442 // widget configuration forced an orientation change), process them now.
3443 for (int i = 0; i < sPendingAddList.size(); i++) {
3444 completeAdd(sPendingAddList.get(i));
3445 }
3446 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447
Winson Chungc51db6a2011-10-05 11:44:49 -07003448 // Update the market app icon as necessary (the other icons will be managed in response to
3449 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003450 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003451
Winson Chungf0c6ae02012-03-21 16:10:31 -07003452 // Animate up any icons as necessary
3453 if (mVisible || mWorkspaceLoading) {
3454 Runnable newAppsRunnable = new Runnable() {
3455 @Override
3456 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003457 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003458 }
3459 };
Winson Chunga2413752012-04-03 14:22:34 -07003460
3461 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3462 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3463 if (canRunNewAppsAnimation()) {
3464 // If the user has not interacted recently, then either snap to the new page to show
3465 // the new-apps animation or just run them if they are to appear on the current page
3466 if (willSnapPage) {
3467 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3468 } else {
3469 runNewAppsAnimation(false);
3470 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003471 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003472 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003473 // are on another page (or just normally if they are added to the current page)
3474 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003475 }
3476 }
3477
3478 mWorkspaceLoading = false;
3479 }
3480
Winson Chunga2413752012-04-03 14:22:34 -07003481 private boolean canRunNewAppsAnimation() {
3482 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3483 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3484 }
3485
Winson Chungf0c6ae02012-03-21 16:10:31 -07003486 /**
3487 * Runs a new animation that scales up icons that were added while Launcher was in the
3488 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003489 *
3490 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491 */
Winson Chunga2413752012-04-03 14:22:34 -07003492 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003493 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003494 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003495
3496 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003497 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3498 @Override
3499 public int compare(View a, View b) {
3500 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3501 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3502 int cellCountX = LauncherModel.getCellCountX();
3503 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3504 }
3505 });
Winson Chunga2413752012-04-03 14:22:34 -07003506
3507 // Animate each of the views in place (or show them immediately if requested)
3508 if (immediate) {
3509 for (View v : mNewShortcutAnimateViews) {
3510 v.setAlpha(1f);
3511 v.setScaleX(1f);
3512 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003513 }
Winson Chunga2413752012-04-03 14:22:34 -07003514 } else {
3515 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3516 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003517 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003518 PropertyValuesHolder.ofFloat("alpha", 1f),
3519 PropertyValuesHolder.ofFloat("scaleX", 1f),
3520 PropertyValuesHolder.ofFloat("scaleY", 1f));
3521 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3522 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3523 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3524 bounceAnims.add(bounceAnim);
3525 }
3526 anim.playTogether(bounceAnims);
3527 anim.addListener(new AnimatorListenerAdapter() {
3528 @Override
3529 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003530 if (mWorkspace != null) {
3531 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3532 }
Winson Chunga2413752012-04-03 14:22:34 -07003533 }
3534 });
3535 anim.start();
3536 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003537
3538 // Clean up
3539 mNewShortcutAnimatePage = -1;
3540 mNewShortcutAnimateViews.clear();
3541 new Thread("clearNewAppsThread") {
3542 public void run() {
3543 mSharedPrefs.edit()
3544 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3545 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3546 .commit();
3547 }
3548 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003549 }
3550
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003551 @Override
3552 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003553 boolean searchVisible = updateGlobalSearchIcon();
3554 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003555 if (mSearchDropTargetBar != null) {
3556 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3557 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003558 }
3559
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003560 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 * Add the icons for all apps.
3562 *
3563 * Implementation of the method from LauncherModel.Callbacks.
3564 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003565 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003566 Runnable setAllAppsRunnable = new Runnable() {
3567 public void run() {
3568 if (mAppsCustomizeContent != null) {
3569 mAppsCustomizeContent.setApps(apps);
3570 }
3571 }
3572 };
3573
Michael Jurkac57b7a82011-08-09 22:02:20 -07003574 // Remove the progress bar entirely; we could also make it GONE
3575 // but better to remove it since we know it's not going to be used
3576 View progressBar = mAppsCustomizeTabHost.
3577 findViewById(R.id.apps_customize_progress_bar);
3578 if (progressBar != null) {
3579 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003580
3581 // We just post the call to setApps so the user sees the progress bar
3582 // disappear-- otherwise, it just looks like the progress bar froze
3583 // which doesn't look great
3584 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3585 } else {
3586 // If we did not initialize the spinner in onCreate, then we can directly set the
3587 // list of applications without waiting for any progress bars views to be hidden.
3588 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003589 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003590 }
3591
3592 /**
3593 * A package was installed.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
3596 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003597 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003598 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003599
Winson Chung785d2eb2011-04-14 16:08:02 -07003600 if (mAppsCustomizeContent != null) {
3601 mAppsCustomizeContent.addApps(apps);
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 }
3604
3605 /**
3606 * A package was updated.
3607 *
3608 * Implementation of the method from LauncherModel.Callbacks.
3609 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003610 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003611 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003612 if (mWorkspace != null) {
3613 mWorkspace.updateShortcuts(apps);
3614 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003615
Winson Chung785d2eb2011-04-14 16:08:02 -07003616 if (mAppsCustomizeContent != null) {
3617 mAppsCustomizeContent.updateApps(apps);
3618 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 }
3620
3621 /**
3622 * A package was uninstalled.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
Winson Chungcd810732012-06-18 16:45:43 -07003626 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003627 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003628 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003629 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003630
Winson Chung785d2eb2011-04-14 16:08:02 -07003631 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003632 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003633 }
Winson Chunga1820962011-10-03 16:31:06 -07003634
3635 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003636 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 }
Joe Onoratobe386092009-11-17 17:32:16 -08003638
3639 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003640 * A number of packages were updated.
3641 */
3642 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003643 if (mAppsCustomizeContent != null) {
3644 mAppsCustomizeContent.onPackagesUpdated();
3645 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003646 }
3647
Winson Chung400438b2011-01-16 17:53:48 -08003648 private int mapConfigurationOriActivityInfoOri(int configOri) {
3649 final Display d = getWindowManager().getDefaultDisplay();
3650 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3651 switch (d.getRotation()) {
3652 case Surface.ROTATION_0:
3653 case Surface.ROTATION_180:
3654 // We are currently in the same basic orientation as the natural orientation
3655 naturalOri = configOri;
3656 break;
3657 case Surface.ROTATION_90:
3658 case Surface.ROTATION_270:
3659 // We are currently in the other basic orientation to the natural orientation
3660 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3661 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3662 break;
3663 }
3664
3665 int[] oriMap = {
3666 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3667 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3668 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3669 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3670 };
3671 // Since the map starts at portrait, we need to offset if this device's natural orientation
3672 // is landscape.
3673 int indexOffset = 0;
3674 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3675 indexOffset = 1;
3676 }
3677 return oriMap[(d.getRotation() + indexOffset) % 4];
3678 }
Adam Cohen446e9402011-09-15 18:21:21 -07003679
Winson Chung4b919f82012-05-01 10:44:08 -07003680 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003681 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003682 boolean enableRotation = forceEnableRotation ||
3683 getResources().getBoolean(R.bool.allow_rotation);
3684 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003685 }
Winson Chung4b919f82012-05-01 10:44:08 -07003686 public void lockScreenOrientation() {
3687 if (isRotationEnabled()) {
3688 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3689 .getConfiguration().orientation));
3690 }
3691 }
3692 public void unlockScreenOrientation(boolean immediate) {
3693 if (isRotationEnabled()) {
3694 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003695 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003696 } else {
3697 mHandler.postDelayed(new Runnable() {
3698 public void run() {
3699 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3700 }
3701 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003702 }
Winson Chung4b919f82012-05-01 10:44:08 -07003703 }
Winson Chung400438b2011-01-16 17:53:48 -08003704 }
3705
Winson Chung82f55532011-08-09 14:14:23 -07003706 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003707 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003708 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003709 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003710
Winson Chung7d7541e2011-09-16 20:14:36 -07003711 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003712 }
Michael Jurka22143132012-10-04 17:42:38 +02003713
Winson Chung7d7541e2011-09-16 20:14:36 -07003714 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003715 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003716 if (cling != null) {
3717 cling.init(this, positionData);
3718 cling.setVisibility(View.VISIBLE);
3719 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3720 if (animate) {
3721 cling.buildLayer();
3722 cling.setAlpha(0f);
3723 cling.animate()
3724 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003725 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003726 .setDuration(SHOW_CLING_DURATION)
3727 .setStartDelay(delay)
3728 .start();
3729 } else {
3730 cling.setAlpha(1f);
3731 }
Michael Jurka22143132012-10-04 17:42:38 +02003732 cling.setFocusableInTouchMode(true);
3733 cling.post(new Runnable() {
3734 public void run() {
3735 cling.setFocusable(true);
3736 cling.requestFocus();
3737 }
3738 });
3739 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3740 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003741 }
3742 return cling;
3743 }
Michael Jurka22143132012-10-04 17:42:38 +02003744
Winson Chung7d7541e2011-09-16 20:14:36 -07003745 private void dismissCling(final Cling cling, final String flag, int duration) {
Michael Jurka22143132012-10-04 17:42:38 +02003746 if (cling != null && cling.getVisibility() == View.VISIBLE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003747 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003749 anim.addListener(new AnimatorListenerAdapter() {
3750 public void onAnimationEnd(Animator animation) {
3751 cling.setVisibility(View.GONE);
3752 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003753 // We should update the shared preferences on a background thread
3754 new Thread("dismissClingThread") {
3755 public void run() {
3756 SharedPreferences.Editor editor = mSharedPrefs.edit();
3757 editor.putBoolean(flag, true);
3758 editor.commit();
3759 }
3760 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003761 };
3762 });
3763 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003764 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003765 }
3766 }
Michael Jurka22143132012-10-04 17:42:38 +02003767
Winson Chung9d9d74f2011-09-19 11:49:12 -07003768 private void removeCling(int id) {
3769 final View cling = findViewById(id);
3770 if (cling != null) {
3771 final ViewGroup parent = (ViewGroup) cling.getParent();
3772 parent.post(new Runnable() {
3773 @Override
3774 public void run() {
3775 parent.removeView(cling);
3776 }
3777 });
Michael Jurka22143132012-10-04 17:42:38 +02003778 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003779 }
3780 }
Michael Jurka974c3862012-05-22 22:00:31 -07003781
3782 private boolean skipCustomClingIfNoAccounts() {
3783 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3784 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3785 if (customCling) {
3786 AccountManager am = AccountManager.get(this);
3787 Account[] accounts = am.getAccountsByType("com.google");
3788 return accounts.length == 0;
3789 }
3790 return false;
3791 }
3792
Winson Chung7d7541e2011-09-16 20:14:36 -07003793 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003794 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003795 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003796 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3797 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003798 // If we're not using the default workspace layout, replace workspace cling
3799 // with a custom workspace cling (usually specified in an overlay)
3800 // For now, only do this on tablets
3801 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003802 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003803 // Use a custom cling
3804 View cling = findViewById(R.id.workspace_cling);
3805 ViewGroup clingParent = (ViewGroup) cling.getParent();
3806 int clingIndex = clingParent.indexOfChild(cling);
3807 clingParent.removeViewAt(clingIndex);
3808 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3809 clingParent.addView(customCling, clingIndex);
3810 customCling.setId(R.id.workspace_cling);
3811 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003812 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003813 } else {
3814 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003815 }
3816 }
3817 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003818 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003819 if (isClingsEnabled() &&
3820 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003821 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003822 } else {
3823 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003824 }
3825 }
3826 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003827 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003828 if (isClingsEnabled() &&
3829 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3830 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003831 } else {
3832 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003833 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003834 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003835 }
3836 public boolean isFolderClingVisible() {
3837 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003838 if (cling != null) {
3839 return cling.getVisibility() == View.VISIBLE;
3840 }
3841 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003842 }
Winson Chung82f55532011-08-09 14:14:23 -07003843 public void dismissWorkspaceCling(View v) {
3844 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003845 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003846 }
3847 public void dismissAllAppsCling(View v) {
3848 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003849 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3850 }
3851 public void dismissFolderCling(View v) {
3852 Cling cling = (Cling) findViewById(R.id.folder_cling);
3853 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003854 }
3855
Winson Chung80baf5a2010-08-09 16:03:15 -07003856 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003857 * Prints out out state for debugging.
3858 */
3859 public void dumpState() {
3860 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003861 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003862 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3863 Log.d(TAG, "mRestoring=" + mRestoring);
3864 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3865 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003866 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003867 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003868
Winson Chung785d2eb2011-04-14 16:08:02 -07003869 if (mAppsCustomizeContent != null) {
3870 mAppsCustomizeContent.dumpState();
3871 }
Joe Onoratobe386092009-11-17 17:32:16 -08003872 Log.d(TAG, "END launcher2 dump state");
3873 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003874
3875 @Override
3876 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3877 super.dump(prefix, fd, writer, args);
3878 writer.println(" ");
3879 writer.println("Debug logs: ");
3880 for (int i = 0; i < sDumpLogs.size(); i++) {
3881 writer.println(" " + sDumpLogs.get(i));
3882 }
3883 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003884
3885 public static void dumpDebugLogsToConsole() {
3886 Log.d(TAG, "");
3887 Log.d(TAG, "*********************");
3888 Log.d(TAG, "Launcher debug logs: ");
3889 for (int i = 0; i < sDumpLogs.size(); i++) {
3890 Log.d(TAG, " " + sDumpLogs.get(i));
3891 }
3892 Log.d(TAG, "*********************");
3893 Log.d(TAG, "");
3894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003895}
Michael Jurka2763be32011-02-24 11:19:57 -08003896
Michael Jurkaabded662011-03-04 12:06:57 -08003897interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003898 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003899 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003900 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003901 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003902 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003903}