blob: b2fbb3e608e54d200c1e1bea26d8fefd0482d4b3 [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;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080088import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070097import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700102import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
Winson Chung70d72102011-08-12 11:24:35 -0700128 private static final int MENU_GROUP_WALLPAPER = 1;
129 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
130 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700131 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
132 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 private static final int REQUEST_BIND_APPWIDGET = 11;
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700149 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
150 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Winson Chung2672ff92012-05-04 16:22:30 -0700152 // The Intent extra that defines whether to ignore the launch animation
153 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
154 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700158 // Type: int
159 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
162 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
164 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: boolean
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
170 // Type: long
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700172 // Type: int
173 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
174 // Type: int
175 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
176 // Type: parcelable
177 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700180 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
181 "com.android.launcher.toolbar_search_icon";
182 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
183 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700186 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800189 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700192 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
193 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700194 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800198 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Winson Chunga2413752012-04-03 14:22:34 -0700200 // How long to wait before the new-shortcut animation automatically pans the workspace
201 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
202
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800203 private final BroadcastReceiver mCloseSystemDialogsReceiver
204 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800205 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private LayoutInflater mInflater;
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private View mQsbDivider;
211 private View mDockDivider;
212 private DragLayer mDragLayer;
213 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private AppWidgetManager mAppWidgetManager;
216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurkac9d95c52011-08-29 14:03:34 -0700218 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700219 private AppWidgetProviderInfo mPendingAddWidgetInfo;
220
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221 private int[] mTmpAddItemCellCoordinates = new int[2];
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private FolderInfo mFolderInfo;
224
Winson Chung3d503fb2011-07-13 17:25:49 -0700225 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800226 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700227
Winson Chungc51db6a2011-10-05 11:44:49 -0700228 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700229 private AppsCustomizeTabHost mAppsCustomizeTabHost;
230 private AppsCustomizePagedView mAppsCustomizeContent;
231 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700234 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
235 // scroll issues (because the workspace may not have been measured yet) and extra work.
236 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
237 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private SpannableStringBuilder mDefaultKeySsb = null;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private boolean mWorkspaceLoading = true;
242
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800243 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private boolean mRestoring;
245 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700246 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Winson Chung4a2afa32012-07-19 14:53:05 -0700248 // Keep track of whether the user has left launcher
249 private static boolean sPausedFromUserAction = false;
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private Bundle mSavedInstanceState;
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800255 private boolean mUserPresent = true;
256 private boolean mVisible = false;
257 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static LocaleConfiguration sLocaleConfiguration = null;
260
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700261 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700279 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Adam Cohen1462de32012-07-24 22:34:36 -0700283 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
284
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700285 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
286
Winson Chung46353de2012-02-16 14:05:10 -0800287 // We only want to get the SharedPreferences once since it does an FS stat each time we get
288 // it from the context.
289 private SharedPreferences mSharedPrefs;
290
Winson Chungf0c6ae02012-03-21 16:10:31 -0700291 // Holds the page that we need to animate to, and the icon views that we need to animate up
292 // when we scroll to that page on resume.
293 private int mNewShortcutAnimatePage = -1;
294 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700295 private ImageView mFolderIconImageView;
296 private Bitmap mFolderIconBitmap;
297 private Canvas mFolderIconCanvas;
298 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700299
Michael Jurkaddd62e92011-02-16 17:49:14 -0800300 private BubbleTextView mWaitingForResume;
301
Michael Jurka22143132012-10-04 17:42:38 +0200302 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
303 = new HideFromAccessibilityHelper();
304
Michael Jurkac1f5d262011-09-30 19:32:27 -0700305 private Runnable mBuildLayersRunnable = new Runnable() {
306 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700307 if (mWorkspace != null) {
308 mWorkspace.buildPageHardwareLayers();
309 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700310 }
311 };
312
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800313 private static ArrayList<PendingAddArguments> sPendingAddList
314 = new ArrayList<PendingAddArguments>();
315
316 private static class PendingAddArguments {
317 int requestCode;
318 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700319 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800320 int screen;
321 int cellX;
322 int cellY;
323 }
324
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700325
326 private boolean doesFileExist(String filename) {
327 FileInputStream fis = null;
328 try {
329 fis = openFileInput(filename);
330 fis.close();
331 return true;
332 } catch (java.io.FileNotFoundException e) {
333 return false;
334 } catch (java.io.IOException e) {
335 return true;
336 }
337 }
338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 @Override
340 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700341 if (DEBUG_STRICT_MODE) {
342 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
343 .detectDiskReads()
344 .detectDiskWrites()
345 .detectNetwork() // or .detectAll() for all detectable problems
346 .penaltyLog()
347 .build());
348 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
349 .detectLeakedSqlLiteObjects()
350 .detectLeakedClosableObjects()
351 .penaltyLog()
352 .penaltyDeath()
353 .build());
354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700358 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
359 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800360 mModel = app.setLauncher(this);
361 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400362 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700364
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700365 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
367 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700368
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700369 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
370 // this also ensures that any synchronous binding below doesn't re-trigger another
371 // LauncherModel load.
372 mPaused = false;
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200375 android.os.Debug.startMethodTracing(
376 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
378
379 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 setContentView(R.layout.launcher);
381 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700382 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800384 registerContentObservers();
385
Joe Onorato7c312c12009-08-13 21:36:53 -0700386 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 mSavedState = savedInstanceState;
389 restoreState(mSavedState);
390
Winson Chunga12a2502010-12-20 14:41:35 -0800391 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700392 if (mAppsCustomizeContent != null) {
393 mAppsCustomizeContent.onPackagesUpdated();
394 }
Winson Chunga12a2502010-12-20 14:41:35 -0800395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 if (PROFILE_STARTUP) {
397 android.os.Debug.stopMethodTracing();
398 }
399
400 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700401 if (sPausedFromUserAction) {
402 // If the user leaves launcher, then we should just load items asynchronously when
403 // they return.
404 mModel.startLoader(true, -1);
405 } else {
406 // We only load the page synchronously if the user rotates (or triggers a
407 // configuration change) while launcher is in the foreground
408 mModel.startLoader(true, mWorkspace.getCurrentPage());
409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 }
411
Michael Jurkac57b7a82011-08-09 22:02:20 -0700412 if (!mModel.isAllAppsLoaded()) {
413 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
414 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 // For handling default keys
418 mDefaultKeySsb = new SpannableStringBuilder();
419 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800420
421 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
422 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800423
Adam Cohenf9426d52012-06-04 17:26:21 -0700424 updateGlobalIcons();
425
426 // On large interfaces, we want the screen to auto-rotate based on the current orientation
427 unlockScreenOrientation(true);
428 }
429
Winson Chung4a2afa32012-07-19 14:53:05 -0700430 protected void onUserLeaveHint() {
431 super.onUserLeaveHint();
432 sPausedFromUserAction = true;
433 }
434
Adam Cohenf9426d52012-06-04 17:26:21 -0700435 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700436 boolean searchVisible = false;
437 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800438 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700439 int coi = getCurrentOrientationIndexForGlobalIcons();
440 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
441 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700442 updateAppMarketIcon();
443 searchVisible = updateGlobalSearchIcon();
444 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700445 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700446 if (sGlobalSearchIcon[coi] != null) {
447 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700448 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700449 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 if (sVoiceSearchIcon[coi] != null) {
451 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sAppMarketIcon[coi] != null) {
455 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800456 }
Winson Chungadf0c182012-08-23 14:59:07 -0700457 if (mSearchDropTargetBar != null) {
458 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
Romain Guycbb89e42009-06-08 15:52:54 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 if (sLocaleConfiguration == null) {
464 new AsyncTask<Void, Void, LocaleConfiguration>() {
465 @Override
466 protected LocaleConfiguration doInBackground(Void... unused) {
467 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
468 readConfiguration(Launcher.this, localeConfiguration);
469 return localeConfiguration;
470 }
471
472 @Override
473 protected void onPostExecute(LocaleConfiguration result) {
474 sLocaleConfiguration = result;
475 checkForLocaleChange(); // recursive, but now with a locale configuration
476 }
477 }.execute();
478 return;
479 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final Configuration configuration = getResources().getConfiguration();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final String locale = configuration.locale.toString();
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mcc = configuration.mcc;
488
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700489 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final int mnc = configuration.mnc;
491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493
Romain Guy84f296c2009-11-04 15:00:44 -0800494 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 sLocaleConfiguration.locale = locale;
496 sLocaleConfiguration.mcc = mcc;
497 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700498
Joe Onorato0589f0f2010-02-08 13:44:00 -0800499 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500
501 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
502 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700503 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 public void run() {
505 writeConfiguration(Launcher.this, localeConfiguration);
506 }
507 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700508 }
509 }
510
511 private static class LocaleConfiguration {
512 public String locale;
513 public int mcc = -1;
514 public int mnc = -1;
515 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700516
Romain Guy98d01652009-06-30 16:21:04 -0700517 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
518 DataInputStream in = null;
519 try {
520 in = new DataInputStream(context.openFileInput(PREFERENCES));
521 configuration.locale = in.readUTF();
522 configuration.mcc = in.readInt();
523 configuration.mnc = in.readInt();
524 } catch (FileNotFoundException e) {
525 // Ignore
526 } catch (IOException e) {
527 // Ignore
528 } finally {
529 if (in != null) {
530 try {
531 in.close();
532 } catch (IOException e) {
533 // Ignore
534 }
535 }
536 }
537 }
538
539 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
540 DataOutputStream out = null;
541 try {
542 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
543 out.writeUTF(configuration.locale);
544 out.writeInt(configuration.mcc);
545 out.writeInt(configuration.mnc);
546 out.flush();
547 } catch (FileNotFoundException e) {
548 // Ignore
549 } catch (IOException e) {
550 //noinspection ResultOfMethodCallIgnored
551 context.getFileStreamPath(PREFERENCES).delete();
552 } finally {
553 if (out != null) {
554 try {
555 out.close();
556 } catch (IOException e) {
557 // Ignore
558 }
559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 }
561 }
562
Adam Cohen716b51e2011-06-30 12:09:54 -0700563 public DragLayer getDragLayer() {
564 return mDragLayer;
565 }
566
Winson Chung36a62fe2012-05-06 18:04:42 -0700567 boolean isDraggingEnabled() {
568 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
569 // that is subsequently removed from the workspace in startBinding().
570 return !mModel.isLoadingWorkspace();
571 }
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 static int getScreen() {
574 synchronized (sLock) {
575 return sScreen;
576 }
577 }
578
579 static void setScreen(int screen) {
580 synchronized (sLock) {
581 sScreen = screen;
582 }
583 }
584
Winson Chung557d6ed2011-07-08 15:34:52 -0700585 /**
586 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
587 * a configuration step, this allows the proper animations to run after other transitions.
588 */
589 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700590 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800591 switch (args.requestCode) {
592 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700593 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
594 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 break;
596 case REQUEST_PICK_SHORTCUT:
597 processShortcut(args.intent);
598 break;
599 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700600 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
601 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 result = true;
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_CREATE_APPWIDGET:
605 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700607 result = true;
608 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800609 case REQUEST_PICK_WALLPAPER:
610 // We just wanted the activity result here so we can clear mWaitingForResult
611 break;
612 }
Michael Jurka27614d22012-04-02 06:40:22 -0700613 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
614 // if you turned the screen off and then back while in All Apps, Launcher would not
615 // return to the workspace. Clearing mAddInfo.container here fixes this issue
616 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700617 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700621 protected void onActivityResult(
622 final int requestCode, final int resultCode, final Intent data) {
623 if (requestCode == REQUEST_BIND_APPWIDGET) {
624 int appWidgetId = data != null ?
625 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
626 if (resultCode == RESULT_CANCELED) {
627 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
628 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700629 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700630 }
631 return;
632 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700633 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800634 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
635 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700636 mWaitingForResult = false;
637
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 // We have special handling for widgets
639 if (isWidgetDrop) {
640 int appWidgetId = data != null ?
641 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700642 if (appWidgetId < 0) {
643 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
644 "widget configuration activity.");
645 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
646 } else {
647 completeTwoStageWidgetDrop(resultCode, appWidgetId);
648 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 return;
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // The pattern used here is that a user PICKs a specific application,
653 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
656 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700657 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 final PendingAddArguments args = new PendingAddArguments();
659 args.requestCode = requestCode;
660 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700661 args.container = mPendingAddInfo.container;
662 args.screen = mPendingAddInfo.screen;
663 args.cellX = mPendingAddInfo.cellX;
664 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800665 if (isWorkspaceLocked()) {
666 sPendingAddList.add(args);
667 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700668 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700672 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
674 null);
675 }
676
677 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700678 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700679 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800680 Runnable onCompleteRunnable = null;
681 int animationType = 0;
682
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700683 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 if (resultCode == RESULT_OK) {
685 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
686 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 mPendingAddWidgetInfo);
688 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800689 onCompleteRunnable = new Runnable() {
690 @Override
691 public void run() {
692 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
693 mPendingAddInfo.screen, layout, null);
694 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
695 null);
696 }
697 };
698 } else if (resultCode == RESULT_CANCELED) {
699 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
700 onCompleteRunnable = new Runnable() {
701 @Override
702 public void run() {
703 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
704 null);
705 }
706 };
707 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700708 if (mDragLayer.getAnimatedView() != null) {
709 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
710 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
711 animationType, boundWidget, true);
712 } else {
713 // The animated view may be null in the case of a rotation during widget configuration
714 onCompleteRunnable.run();
715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 @Override
719 protected void onResume() {
720 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700721
Winson Chung4a2afa32012-07-19 14:53:05 -0700722 // Restore the previous launcher state
723 if (mOnResumeState == State.WORKSPACE) {
724 showWorkspace(false);
725 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
726 showAllApps(false);
727 }
728 mOnResumeState = State.NONE;
729
Winson Chungde0fb8f2012-05-08 14:37:08 -0700730 // Process any items that were added while Launcher was away
731 InstallShortcutReceiver.flushInstallQueue(this);
732
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800733 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700734 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700735 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700737 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700739 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
Winson Chunge4e50662012-01-23 14:45:13 -0800741
742 // Reset the pressed state of icons that were locked in the press state while activities
743 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800744 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800745 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800746 mWaitingForResume.setStayPressed(false);
747 }
Winson Chunge4e50662012-01-23 14:45:13 -0800748 if (mAppsCustomizeContent != null) {
749 // Resets the previous all apps icon press state
750 mAppsCustomizeContent.resetDrawableState();
751 }
Adam Cohen06dff352012-06-01 17:17:08 -0700752 // It is possible that widgets can receive updates while launcher is not in the foreground.
753 // Consequently, the widgets will be inflated in the orientation of the foreground activity
754 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
755 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700756 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700757
758 // Again, as with the above scenario, it's possible that one or more of the global icons
759 // were updated in the wrong orientation.
760 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700765 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
766 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
767 // when Launcher resumes and we are still in AllApps.
768 updateWallpaperVisibility(true);
769
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700771 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800772 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700773 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700774 }
Romain Guycbb89e42009-06-08 15:52:54 -0700775
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700776 @Override
777 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400778 // Flag the loader to stop early before switching
779 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700780 if (mAppsCustomizeContent != null) {
781 mAppsCustomizeContent.surrender();
782 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800783 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700784 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700785
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800786 // We can't hide the IME if it was forced open. So don't bother
787 /*
788 @Override
789 public void onWindowFocusChanged(boolean hasFocus) {
790 super.onWindowFocusChanged(hasFocus);
791
792 if (hasFocus) {
793 final InputMethodManager inputManager = (InputMethodManager)
794 getSystemService(Context.INPUT_METHOD_SERVICE);
795 WindowManager.LayoutParams lp = getWindow().getAttributes();
796 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
797 android.os.Handler()) {
798 protected void onReceiveResult(int resultCode, Bundle resultData) {
799 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
800 }
801 });
802 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
803 }
804 }
805 */
806
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 private boolean acceptFilter() {
808 final InputMethodManager inputManager = (InputMethodManager)
809 getSystemService(Context.INPUT_METHOD_SERVICE);
810 return !inputManager.isFullscreenMode();
811 }
812
813 @Override
814 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700815 final int uniChar = event.getUnicodeChar();
816 final boolean handled = super.onKeyDown(keyCode, event);
817 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
818 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
820 keyCode, event);
821 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700822 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700823 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700824 // showSearchDialog()
825 // If there are multiple keystrokes before the search dialog takes focus,
826 // onSearchRequested() will be called for every keystroke,
827 // but it is idempotent, so it's fine.
828 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
830 }
831
Joe Onorato8a9625e2010-01-28 15:55:35 -0800832 // Eat the long press event so the keyboard doesn't come up.
833 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
834 return true;
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 return handled;
838 }
839
Karl Rosaen138a0412009-04-23 19:00:21 -0700840 private String getTypedText() {
841 return mDefaultKeySsb.toString();
842 }
843
844 private void clearTypedText() {
845 mDefaultKeySsb.clear();
846 mDefaultKeySsb.clearSpans();
847 Selection.setSelection(mDefaultKeySsb, 0);
848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700851 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
852 * State
853 */
854 private static State intToState(int stateOrdinal) {
855 State state = State.WORKSPACE;
856 final State[] stateValues = State.values();
857 for (int i = 0; i < stateValues.length; i++) {
858 if (stateValues[i].ordinal() == stateOrdinal) {
859 state = stateValues[i];
860 break;
861 }
862 }
863 return state;
864 }
865
866 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 * Restores the previous state, if it exists.
868 *
869 * @param savedState The previous state.
870 */
871 private void restoreState(Bundle savedState) {
872 if (savedState == null) {
873 return;
874 }
875
Michael Jurka883f55b2010-10-21 15:47:14 -0700876 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700877 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700878 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800879 }
880
Winson Chungf0c6ae02012-03-21 16:10:31 -0700881 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700883 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
887 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700888
Winson Chung3d503fb2011-07-13 17:25:49 -0700889 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
890 mPendingAddInfo.container = pendingAddContainer;
891 mPendingAddInfo.screen = pendingAddScreen;
892 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
893 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
895 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
896 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700897 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 mRestoring = true;
899 }
900
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700901
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
903 if (renameFolder) {
904 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700905 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 mRestoring = true;
907 }
Winson Chunga12a2502010-12-20 14:41:35 -0800908
Winson Chung785d2eb2011-04-14 16:08:02 -0700909
910 // Restore the AppsCustomize tab
911 if (mAppsCustomizeTabHost != null) {
912 String curTab = savedState.getString("apps_customize_currentTab");
913 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700914 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700915 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700916 mAppsCustomizeContent.loadAssociatedPages(
917 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700918 }
919
Winson Chung5afbf7b2011-07-25 11:53:08 -0700920 int currentIndex = savedState.getInt("apps_customize_currentIndex");
921 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
925 /**
926 * Finds all the views we need and configure them properly.
927 */
928 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700929 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400930
Winson Chung4c98d922011-05-31 16:50:48 -0700931 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
932 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800933 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
934 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935
Winson Chung4c98d922011-05-31 16:50:48 -0700936 // Setup the drag layer
937 mDragLayer.setup(this, dragController);
938
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 // Setup the hotseat
940 mHotseat = (Hotseat) findViewById(R.id.hotseat);
941 if (mHotseat != null) {
942 mHotseat.setup(this);
943 }
944
Winson Chung4c98d922011-05-31 16:50:48 -0700945 // Setup the workspace
946 mWorkspace.setHapticFeedbackEnabled(false);
947 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700948 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700949 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700950
Winson Chungf0ea4d32011-06-06 14:27:16 -0700951 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700952 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700953
Winson Chungf0ea4d32011-06-06 14:27:16 -0700954 // Setup AppsCustomize
955 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
956 findViewById(R.id.apps_customize_pane);
957 mAppsCustomizeContent = (AppsCustomizePagedView)
958 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
959 mAppsCustomizeContent.setup(this, dragController);
Winson Chung0e721a42012-08-02 17:40:30 -0700960
Winson Chung3d503fb2011-07-13 17:25:49 -0700961 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700962 dragController.setDragScoller(mWorkspace);
963 dragController.setScrollView(mDragLayer);
964 dragController.setMoveTarget(mWorkspace);
965 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700966 if (mSearchDropTargetBar != null) {
967 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970
971 /**
972 * Creates a view representing a shortcut.
973 *
974 * @param info The data structure describing the shortcut.
975 *
976 * @return A View inflated from R.layout.application.
977 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700980 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 /**
984 * Creates a view representing a shortcut inflated from the specified resource.
985 *
986 * @param layoutResId The id of the XML layout used to create the shortcut.
987 * @param parent The group the shortcut belongs to.
988 * @param info The data structure describing the shortcut.
989 *
990 * @return A View inflated from layoutResId.
991 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800993 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
994 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 return favorite;
997 }
998
999 /**
1000 * Add an application shortcut to the workspace.
1001 *
1002 * @param data The intent describing the application.
1003 * @param cellInfo The position on screen where to create the shortcut.
1004 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001006 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001007 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008
Adam Cohenfbba09b2011-07-18 21:43:05 -07001009 // First we check if we already know the exact location where we want to add this item.
1010 if (cellX >= 0 && cellY >= 0) {
1011 cellXY[0] = cellX;
1012 cellXY[1] = cellY;
1013 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001014 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015 return;
1016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001018 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019
Romain Guy73b979d2009-06-09 12:57:21 -07001020 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001023 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001024 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001025 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 } else {
1027 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 }
1029 }
Romain Guycbb89e42009-06-08 15:52:54 -07001030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 /**
1032 * Add a shortcut to the workspace.
1033 *
1034 * @param data The intent describing the shortcut.
1035 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1038 int cellY) {
1039 int[] cellXY = mTmpAddItemCellCoordinates;
1040 int[] touchXY = mPendingAddInfo.dropPos;
1041 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
Michael Jurkad3ef3062010-11-23 16:23:58 -08001043 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001044
Adam Cohen558baaf2011-08-15 15:22:57 -07001045 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001046 if (info == null) {
1047 return;
1048 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001049 final View view = createShortcut(info);
1050
Adam Cohenfbba09b2011-07-18 21:43:05 -07001051 // First we check if we already know the exact location where we want to add this item.
1052 if (cellX >= 0 && cellY >= 0) {
1053 cellXY[0] = cellX;
1054 cellXY[1] = cellY;
1055 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001056
1057 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001058 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001059 true, null,null)) {
1060 return;
1061 }
1062 DragObject dragObject = new DragObject();
1063 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001064 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1065 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001066 return;
1067 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001068 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001069 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001070 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001071 foundCellSpan = (result != null);
1072 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001073 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001074 }
1075
1076 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001077 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 return;
1079 }
1080
Adam Cohen558baaf2011-08-15 15:22:57 -07001081 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
1083 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001084 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1085 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087 }
1088
Adam Cohen2f093b62012-04-30 18:59:53 -07001089 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1090 int minHeight) {
1091 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001092 // We want to account for the extra amount of padding that we are adding to the widget
1093 // to ensure that it gets the full amount of space that it has requested
1094 int requiredWidth = minWidth + padding.left + padding.right;
1095 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001096 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001097 }
1098
Adam Cohen2f093b62012-04-30 18:59:53 -07001099 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1100 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001101 }
1102
Adam Cohen2f093b62012-04-30 18:59:53 -07001103 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1104 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001105 }
1106
Adam Cohen2f093b62012-04-30 18:59:53 -07001107 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1108 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001109 }
1110
Adam Cohen2f093b62012-04-30 18:59:53 -07001111 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1112 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001113 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001114 }
1115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001119 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001122 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1123 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1124 if (appWidgetInfo == null) {
1125 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1126 }
Romain Guycbb89e42009-06-08 15:52:54 -07001127
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001128 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001129 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001130
Adam Cohen2f093b62012-04-30 18:59:53 -07001131 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1132 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001135 // We have saved the position to which the widget was dragged-- this really only matters
1136 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 int[] cellXY = mTmpAddItemCellCoordinates;
1138 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001141 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1142 cellXY[0] = mPendingAddInfo.cellX;
1143 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001144 spanXY[0] = mPendingAddInfo.spanX;
1145 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001146 foundCellSpan = true;
1147 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001149 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001150 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1151 spanXY[1], cellXY, finalSpan);
1152 spanXY[0] = finalSpan[0];
1153 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001155 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001156 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001157 }
1158
Michael Jurka0280c3b2010-09-17 15:00:07 -07001159 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001160 if (appWidgetId != -1) {
1161 // Deleting an app widget ID is a void call but writes to disk before returning
1162 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001163 new Thread("deleteAppWidgetId") {
1164 public void run() {
1165 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1166 }
1167 }.start();
1168 }
Winson Chung93eef082012-03-23 15:59:27 -07001169 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001170 return;
1171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001174 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1175 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001176 launcherInfo.spanX = spanXY[0];
1177 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001178 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1179 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001182 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
1184 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001185 if (hostView == null) {
1186 // Perform actual inflation because we're live
1187 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1188 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1189 } else {
1190 // The AppWidgetHostView has already been inflated and instantiated
1191 launcherInfo.hostView = hostView;
1192 }
Romain Guycbb89e42009-06-08 15:52:54 -07001193
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001195 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001196 launcherInfo.notifyWidgetSizeChanged(this);
1197
Winson Chung3d503fb2011-07-13 17:25:49 -07001198 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001200
1201 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001203 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
Adam Cohended9f8d2010-11-03 13:25:16 -07001206 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1207 @Override
1208 public void onReceive(Context context, Intent intent) {
1209 final String action = intent.getAction();
1210 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1211 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001212 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001213 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001214
Winson Chungc100e8e2011-08-09 16:02:43 -07001215 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001216 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001217 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1218 mAppsCustomizeTabHost.reset();
1219 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001221 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1222 mUserPresent = true;
1223 updateRunning();
1224 }
1225 }
1226 };
1227
1228 @Override
1229 public void onAttachedToWindow() {
1230 super.onAttachedToWindow();
1231
1232 // Listen for broadcasts related to user-presence
1233 final IntentFilter filter = new IntentFilter();
1234 filter.addAction(Intent.ACTION_SCREEN_OFF);
1235 filter.addAction(Intent.ACTION_USER_PRESENT);
1236 registerReceiver(mReceiver, filter);
1237
Adam Cohend113e0c2010-11-11 10:48:05 -08001238 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001239 mVisible = true;
1240 }
1241
1242 @Override
1243 public void onDetachedFromWindow() {
1244 super.onDetachedFromWindow();
1245 mVisible = false;
1246
Adam Cohend113e0c2010-11-11 10:48:05 -08001247 if (mAttached) {
1248 unregisterReceiver(mReceiver);
1249 mAttached = false;
1250 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 updateRunning();
1252 }
1253
1254 public void onWindowVisibilityChanged(int visibility) {
1255 mVisible = visibility == View.VISIBLE;
1256 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001257 // The following code used to be in onResume, but it turns out onResume is called when
1258 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1259 // is a more appropriate event to handle
1260 if (mVisible) {
1261 mAppsCustomizeTabHost.onWindowVisible();
1262 if (!mWorkspaceLoading) {
1263 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001264 // We want to let Launcher draw itself at least once before we force it to build
1265 // layers on all the workspace pages, so that transitioning to Launcher from other
1266 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1267 // a true draw so we wait until the second preDraw call to be safe
1268 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001269 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001270 // We delay the layer building a bit in order to give
1271 // other message processing a time to run. In particular
1272 // this avoids a delay in hiding the IME if it was
1273 // currently shown, because doing that may involve
1274 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001275 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001276
Michael Jurka6ee21d22012-02-21 18:20:27 -08001277 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001278 return true;
1279 }
1280 });
1281 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001282 // When Launcher comes back to foreground, a different Activity might be responsible for
1283 // the app market intent, so refresh the icon
1284 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001285 clearTypedText();
1286 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001287 }
1288
1289 private void sendAdvanceMessage(long delay) {
1290 mHandler.removeMessages(ADVANCE_MSG);
1291 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1292 mHandler.sendMessageDelayed(msg, delay);
1293 mAutoAdvanceSentTime = System.currentTimeMillis();
1294 }
1295
1296 private void updateRunning() {
1297 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1298 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1299 mAutoAdvanceRunning = autoAdvanceRunning;
1300 if (autoAdvanceRunning) {
1301 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1302 sendAdvanceMessage(delay);
1303 } else {
1304 if (!mWidgetsToAdvance.isEmpty()) {
1305 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1306 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1307 }
1308 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001309 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 }
1311 }
1312 }
1313
1314 private final Handler mHandler = new Handler() {
1315 @Override
1316 public void handleMessage(Message msg) {
1317 if (msg.what == ADVANCE_MSG) {
1318 int i = 0;
1319 for (View key: mWidgetsToAdvance.keySet()) {
1320 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1321 final int delay = mAdvanceStagger * i;
1322 if (v instanceof Advanceable) {
1323 postDelayed(new Runnable() {
1324 public void run() {
1325 ((Advanceable) v).advance();
1326 }
1327 }, delay);
1328 }
1329 i++;
1330 }
1331 sendAdvanceMessage(mAdvanceInterval);
1332 }
1333 }
1334 };
1335
1336 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001337 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001338 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1339 if (v instanceof Advanceable) {
1340 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001341 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001342 updateRunning();
1343 }
1344 }
1345
1346 void removeWidgetToAutoAdvance(View hostView) {
1347 if (mWidgetsToAdvance.containsKey(hostView)) {
1348 mWidgetsToAdvance.remove(hostView);
1349 updateRunning();
1350 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 }
1352
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001354 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 launcherInfo.hostView = null;
1356 }
1357
Winson Chung93eef082012-03-23 15:59:27 -07001358 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1359 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1360 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 }
1362
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001363 public LauncherAppWidgetHost getAppWidgetHost() {
1364 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Romain Guycbb89e42009-06-08 15:52:54 -07001366
Winson Chunga9abd0e2010-10-27 17:18:37 -07001367 public LauncherModel getModel() {
1368 return mModel;
1369 }
1370
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001371 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 getWindow().closeAllPanels();
1373
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001374 // Whatever we were doing is hereby canceled.
1375 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001376 }
1377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 @Override
1379 protected void onNewIntent(Intent intent) {
1380 super.onNewIntent(intent);
1381
1382 // Close the menu
1383 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001384 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001385 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001386
Joe Onorato14f122b2009-11-19 14:06:36 -08001387 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1388 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001389
Adam Cohenac56cff2011-09-28 20:45:37 -07001390 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001391 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001392 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001393 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1394 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001395 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001396 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001397
1398 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001399 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001400
1401 // If we are already on home, then just animate back to the workspace, otherwise, just
1402 // wait until onResume to set the state back to Workspace
1403 if (alreadyOnHome) {
1404 showWorkspace(true);
1405 } else {
1406 mOnResumeState = State.WORKSPACE;
1407 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001408
Joe Onorato3a8820b2009-11-10 15:06:42 -08001409 final View v = getWindow().peekDecorView();
1410 if (v != null && v.getWindowToken() != null) {
1411 InputMethodManager imm = (InputMethodManager)getSystemService(
1412 INPUT_METHOD_SERVICE);
1413 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001415
Michael Jurka35aa14d2011-07-07 17:01:08 -07001416 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001417 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001418 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421 }
1422
1423 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001424 public void onRestoreInstanceState(Bundle state) {
1425 super.onRestoreInstanceState(state);
1426 for (int page: mSynchronouslyBoundPages) {
1427 mWorkspace.restoreInstanceStateForChild(page);
1428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430
1431 @Override
1432 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001433 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001434 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
Michael Jurka883f55b2010-10-21 15:47:14 -07001436 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001437 // We close any open folder since it will not be re-opened, and we need to make sure
1438 // this state is reflected.
1439 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
Winson Chung3d503fb2011-07-13 17:25:49 -07001441 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1442 mWaitingForResult) {
1443 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1444 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1445 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1446 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001447 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1448 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1449 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
1452 if (mFolderInfo != null && mWaitingForResult) {
1453 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1454 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1455 }
Michael Jurka946ad472010-07-09 18:05:18 -07001456
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 // Save the current AppsCustomize tab
1458 if (mAppsCustomizeTabHost != null) {
1459 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1460 if (currentTabTag != null) {
1461 outState.putString("apps_customize_currentTab", currentTabTag);
1462 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001463 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1464 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 @Override
1469 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Winson Chunge7a03942011-08-05 15:05:12 -07001472 // Remove all pending runnables
1473 mHandler.removeMessages(ADVANCE_MSG);
1474 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001475 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001476
Winson Chungcd2b0142011-06-08 16:02:26 -07001477 // Stop callbacks from LauncherModel
1478 LauncherApplication app = ((LauncherApplication) getApplication());
1479 mModel.stopLoader();
1480 app.setLauncher(null);
1481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001483 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001485 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001487 mAppWidgetHost = null;
1488
1489 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490
1491 TextKeyListener.getInstance().release();
1492
Winson Chung81b52252012-08-27 15:34:29 -07001493 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1494 // to prevent leaking Launcher activities on orientation change.
1495 if (mModel != null) {
1496 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1497 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001498
1499 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001500 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001501
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001502 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001503 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1504 mWorkspace.removeAllViews();
1505 mWorkspace = null;
1506 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001507
Michael Jurka2ecf9952012-06-18 12:52:28 -07001508 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510
Adam Cohena9cf38f2011-05-02 15:36:58 -07001511 public DragController getDragController() {
1512 return mDragController;
1513 }
1514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 @Override
1516 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001517 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 super.startActivityForResult(intent, requestCode);
1519 }
1520
Winson Chung70d72102011-08-12 11:24:35 -07001521 /**
1522 * Indicates that we want global search for this activity by setting the globalSearch
1523 * argument for {@link #startSearch} to true.
1524 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001526 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001528
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001529 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001530
Karl Rosaen138a0412009-04-23 19:00:21 -07001531 if (initialQuery == null) {
1532 // Use any text typed in the launcher as the initial query
1533 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 if (appSearchData == null) {
1536 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001537 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
Winson Chungadf0c182012-08-23 14:59:07 -07001539 Rect sourceBounds = new Rect();
1540 if (mSearchDropTargetBar != null) {
1541 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1542 }
Romain Guycbb89e42009-06-08 15:52:54 -07001543
Michael Jurkaa33411c2012-06-14 16:18:21 -07001544 startGlobalSearch(initialQuery, selectInitialQuery,
1545 appSearchData, sourceBounds);
1546 }
1547
1548 /**
1549 * Starts the global search activity. This code is a copied from SearchManager
1550 */
1551 public void startGlobalSearch(String initialQuery,
1552 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001553 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001554 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1555 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1556 if (globalSearchActivity == null) {
1557 Log.w(TAG, "No global search activity found.");
1558 return;
1559 }
1560 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1561 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1562 intent.setComponent(globalSearchActivity);
1563 // Make sure that we have a Bundle to put source in
1564 if (appSearchData == null) {
1565 appSearchData = new Bundle();
1566 } else {
1567 appSearchData = new Bundle(appSearchData);
1568 }
1569 // Set source to package name of app that starts global search, if not set already.
1570 if (!appSearchData.containsKey("source")) {
1571 appSearchData.putString("source", getPackageName());
1572 }
1573 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1574 if (!TextUtils.isEmpty(initialQuery)) {
1575 intent.putExtra(SearchManager.QUERY, initialQuery);
1576 }
1577 if (selectInitialQuery) {
1578 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1579 }
1580 intent.setSourceBounds(sourceBounds);
1581 try {
1582 startActivity(intent);
1583 } catch (ActivityNotFoundException ex) {
1584 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
Winson Chung70d72102011-08-12 11:24:35 -07001588 @Override
1589 public boolean onCreateOptionsMenu(Menu menu) {
1590 if (isWorkspaceLocked()) {
1591 return false;
1592 }
1593
1594 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001595
1596 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1597 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1598 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001599 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1600 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1601 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001602 String helpUrl = getString(R.string.help_url);
1603 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001604 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1605 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1606
Winson Chung70d72102011-08-12 11:24:35 -07001607 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1608 .setIcon(android.R.drawable.ic_menu_gallery)
1609 .setAlphabeticShortcut('W');
1610 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1611 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001612 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001613 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001614 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1615 .setIcon(android.R.drawable.ic_menu_preferences)
1616 .setIntent(settings)
1617 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001618 if (!helpUrl.isEmpty()) {
1619 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1620 .setIcon(android.R.drawable.ic_menu_help)
1621 .setIntent(help)
1622 .setAlphabeticShortcut('H');
1623 }
Winson Chung70d72102011-08-12 11:24:35 -07001624 return true;
1625 }
1626
1627 @Override
1628 public boolean onPrepareOptionsMenu(Menu menu) {
1629 super.onPrepareOptionsMenu(menu);
1630
1631 if (mAppsCustomizeTabHost.isTransitioning()) {
1632 return false;
1633 }
1634 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1635 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1636
1637 return true;
1638 }
1639
1640 @Override
1641 public boolean onOptionsItemSelected(MenuItem item) {
1642 switch (item.getItemId()) {
1643 case MENU_WALLPAPER_SETTINGS:
1644 startWallpaper();
1645 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001646 }
1647
1648 return super.onOptionsItemSelected(item);
1649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001651 @Override
1652 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001653 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001654 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001655 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001656 }
1657
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 public boolean isWorkspaceLocked() {
1659 return mWorkspaceLoading || mWaitingForResult;
1660 }
1661
Michael Jurka0280c3b2010-09-17 15:00:07 -07001662 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001663 mPendingAddInfo.container = ItemInfo.NO_ID;
1664 mPendingAddInfo.screen = -1;
1665 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1666 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001667 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001668 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001669 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001670
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001671 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1672 AppWidgetProviderInfo appWidgetInfo) {
1673 if (appWidgetInfo.configure != null) {
1674 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001675
Bjorn Bringert7984c942009-12-09 15:38:25 +00001676 // Launch over to configure widget, if needed
1677 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001678 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001679 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001680 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001682 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001683 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1684 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001685 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001686 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688 }
Romain Guycbb89e42009-06-08 15:52:54 -07001689
Adam Cohenfbba09b2011-07-18 21:43:05 -07001690 /**
1691 * Process a shortcut drop.
1692 *
1693 * @param componentName The name of the component
1694 * @param screen The screen where it should be added
1695 * @param cell The cell it should be added to, optional
1696 * @param position The location on the screen where it was dropped, optional
1697 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001698 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1699 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001700 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001701 mPendingAddInfo.container = container;
1702 mPendingAddInfo.screen = screen;
1703 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001704
1705 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001706 mPendingAddInfo.cellX = cell[0];
1707 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001708 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001709
1710 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1711 createShortcutIntent.setComponent(componentName);
1712 processShortcut(createShortcutIntent);
1713 }
1714
Adam Cohenfbba09b2011-07-18 21:43:05 -07001715 /**
1716 * Process a widget drop.
1717 *
1718 * @param info The PendingAppWidgetInfo of the widget being added.
1719 * @param screen The screen where it should be added
1720 * @param cell The cell it should be added to, optional
1721 * @param position The location on the screen where it was dropped, optional
1722 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001723 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001724 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001725 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001726 mPendingAddInfo.container = info.container = container;
1727 mPendingAddInfo.screen = info.screen = screen;
1728 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001729 mPendingAddInfo.minSpanX = info.minSpanX;
1730 mPendingAddInfo.minSpanY = info.minSpanY;
1731
Adam Cohenfbba09b2011-07-18 21:43:05 -07001732 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001733 mPendingAddInfo.cellX = cell[0];
1734 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001735 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001736 if (span != null) {
1737 mPendingAddInfo.spanX = span[0];
1738 mPendingAddInfo.spanY = span[1];
1739 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001740
Adam Cohened66b2b2012-01-23 17:28:51 -08001741 AppWidgetHostView hostView = info.boundWidget;
1742 int appWidgetId;
1743 if (hostView != null) {
1744 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001745 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001746 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001747 // In this case, we either need to start an activity to get permission to bind
1748 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001749 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001750 Bundle options = info.bindOptions;
1751
1752 boolean success = false;
1753 if (options != null) {
1754 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1755 info.componentName, options);
1756 } else {
1757 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1758 info.componentName);
1759 }
1760 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001761 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001762 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001763 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001764 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1765 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1766 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001767 // TODO: we need to make sure that this accounts for the options bundle.
1768 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001769 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1770 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001771 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001772 }
1773
Joe Onoratodeb98af2010-02-19 14:59:39 -08001774 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 // Handle case where user selected "Applications"
1776 String applicationName = getResources().getString(R.string.group_applications);
1777 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001778
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 if (applicationName != null && applicationName.equals(shortcutName)) {
1780 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1781 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1784 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001785 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001786 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001788 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
Winson Chung24ab2f12010-09-16 14:10:47 -07001792 void processWallpaper(Intent intent) {
1793 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1794 }
1795
Winson Chung3d503fb2011-07-13 17:25:49 -07001796 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1797 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001798 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 folderInfo.title = getText(R.string.folder_name);
1800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001802 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1803 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001804 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805
1806 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001807 FolderIcon newFolder =
1808 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1809 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1810 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001811 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
Romain Guycbb89e42009-06-08 15:52:54 -07001813
Joe Onorato9c1289c2009-08-17 11:03:03 -04001814 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001815 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 }
1817
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001819 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001821 Intent chooser = Intent.createChooser(pickWallpaper,
1822 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001823 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1824 // Removed in Eclair MR1
1825// WallpaperManager wm = (WallpaperManager)
1826// getSystemService(Context.WALLPAPER_SERVICE);
1827// WallpaperInfo wi = wm.getWallpaperInfo();
1828// if (wi != null && wi.getSettingsActivity() != null) {
1829// LabeledIntent li = new LabeledIntent(getPackageName(),
1830// R.string.configure_wallpaper, 0);
1831// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1832// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1833// }
Mike Clerona0618e42009-10-22 13:55:21 -07001834 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001837 /**
1838 * Registers various content observers. The current implementation registers
1839 * only a favorites observer to keep track of the favorites applications.
1840 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001841 private void registerContentObservers() {
1842 ContentResolver resolver = getContentResolver();
1843 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1844 true, mWidgetObserver);
1845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 @Override
1848 public boolean dispatchKeyEvent(KeyEvent event) {
1849 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1850 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001852 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001853 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001854 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001855 dumpState();
1856 return true;
1857 }
1858 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001859 }
1860 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1861 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001862 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 return true;
1864 }
1865 }
1866
1867 return super.dispatchKeyEvent(event);
1868 }
1869
Joe Onorato88ec0992009-11-19 13:16:06 -08001870 @Override
1871 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001872 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001873 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001874 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001875 Folder openFolder = mWorkspace.getOpenFolder();
1876 if (openFolder.isEditingName()) {
1877 openFolder.dismissEditingName();
1878 } else {
1879 closeFolder();
1880 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001881 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001882 mWorkspace.exitWidgetResizeMode();
1883
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001884 // Back button is a no-op here, but give at least some feedback for the button press
1885 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001886 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001887 }
1888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001890 * Re-listen when widgets are reset.
1891 */
1892 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001893 if (mAppWidgetHost != null) {
1894 mAppWidgetHost.startListening();
1895 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001896 }
1897
1898 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 * Launches the intent referred by the clicked shortcut.
1900 *
1901 * @param v The view representing the clicked shortcut.
1902 */
1903 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001904 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1905 // view has detached (it's possible for this to happen if the view is removed mid touch).
1906 if (v.getWindowToken() == null) {
1907 return;
1908 }
1909
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001910 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001911 return;
1912 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001915 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001917 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001918 int[] pos = new int[2];
1919 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001920 intent.setSourceBounds(new Rect(pos[0], pos[1],
1921 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001922
1923 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001924
1925 if (success && v instanceof BubbleTextView) {
1926 mWaitingForResume = (BubbleTextView) v;
1927 mWaitingForResume.setStayPressed(true);
1928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001930 if (v instanceof FolderIcon) {
1931 FolderIcon fi = (FolderIcon) v;
1932 handleFolderClick(fi);
1933 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001934 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001935 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001936 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001937 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001938 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941 }
1942
Michael Jurka0e260592010-06-30 17:07:39 -07001943 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001944 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001945 // clicking anywhere on the workspace causes the customization drawer to slide down
1946 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001947 return false;
1948 }
1949
Michael Jurkaaf442092010-06-10 17:01:57 -07001950 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001951 * Event handler for the search button
1952 *
1953 * @param v The view that was clicked.
1954 */
1955 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001956 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1957
Amith Yamasania135ba82011-08-09 17:42:01 -07001958 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001959 }
1960
1961 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001962 * Event handler for the voice button
1963 *
1964 * @param v The view that was clicked.
1965 */
1966 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001967 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001968
Michael Jurkaae65ee32012-04-30 11:45:54 -07001969 try {
1970 final SearchManager searchManager =
1971 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1972 ComponentName activityName = searchManager.getGlobalSearchActivity();
1973 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001974 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001975 if (activityName != null) {
1976 intent.setPackage(activityName.getPackageName());
1977 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001978 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001979 } catch (ActivityNotFoundException e) {
1980 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001981 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001982 startActivitySafely(null, intent, "onClickVoiceButton");
1983 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001984 }
1985
1986 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001987 * Event handler for the "grid" button that appears on the home screen, which
1988 * enters all apps mode.
1989 *
1990 * @param v The view that was clicked.
1991 */
1992 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001993 showAllApps(true);
1994 }
1995
1996 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001997 // Provide the same haptic feedback that the system offers for virtual keys.
1998 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001999 }
2000
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002001 public void onClickAppMarketButton(View v) {
2002 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002003 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002004 } else {
2005 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002006 }
2007 }
2008
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002009 void startApplicationDetailsActivity(ComponentName componentName) {
2010 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002011 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2012 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002013 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002014 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002015 }
2016
Patrick Dubroy5539af72010-09-07 15:22:01 -07002017 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2018 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2019 // System applications cannot be installed. For now, show a toast explaining that.
2020 // We may give them the option of disabling apps this way.
2021 int messageId = R.string.uninstall_system_app_text;
2022 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2023 } else {
2024 String packageName = appInfo.componentName.getPackageName();
2025 String className = appInfo.componentName.getClassName();
2026 Intent intent = new Intent(
2027 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002028 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2029 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002030 startActivity(intent);
2031 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002032 }
2033
Michael Jurka86a720e2012-05-09 11:23:23 -07002034 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002038 // Only launch using the new animation if the shortcut has not opted out (this is a
2039 // private contract between launcher and may be ignored in the future).
2040 boolean useLaunchAnimation = (v != null) &&
2041 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2042 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002043 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2044 v.getMeasuredWidth(), v.getMeasuredHeight());
2045
2046 startActivity(intent, opts.toBundle());
2047 } else {
2048 startActivity(intent);
2049 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002050 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } catch (SecurityException e) {
2052 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002053 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002055 "or use the exported attribute for this activity. "
2056 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002058 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002060
Michael Jurka86a720e2012-05-09 11:23:23 -07002061 boolean startActivitySafely(View v, Intent intent, Object tag) {
2062 boolean success = false;
2063 try {
2064 success = startActivity(v, intent, tag);
2065 } catch (ActivityNotFoundException e) {
2066 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2067 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2068 }
2069 return success;
2070 }
2071
Romain Guy8e633c52010-03-04 12:51:36 -08002072 void startActivityForResultSafely(Intent intent, int requestCode) {
2073 try {
2074 startActivityForResult(intent, requestCode);
2075 } catch (ActivityNotFoundException e) {
2076 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2077 } catch (SecurityException e) {
2078 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2079 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2080 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2081 "or use the exported attribute for this activity.", e);
2082 }
2083 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084
Adam Cohena9cf38f2011-05-02 15:36:58 -07002085 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002086 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002087 Folder openFolder = mWorkspace.getFolderForTag(info);
2088
2089 // If the folder info reports that the associated folder is open, then verify that
2090 // it is actually opened. There have been a few instances where this gets out of sync.
2091 if (info.opened && openFolder == null) {
2092 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2093 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2094 info.opened = false;
2095 }
2096
Adam Cohenfb91f302012-06-11 15:45:18 -07002097 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 // Close any open folder
2099 closeFolder();
2100 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002101 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 } else {
2103 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 int folderScreen;
2105 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002106 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 // .. and close it
2108 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002109 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 // Close any folder open on the current screen
2111 closeFolder();
2112 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002113 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115 }
2116 }
2117 }
2118
Adam Cohen268c4752012-06-06 17:47:33 -07002119 /**
2120 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2121 * in the DragLayer in the exact absolute location of the original FolderIcon.
2122 */
2123 private void copyFolderIconToImage(FolderIcon fi) {
2124 final int width = fi.getMeasuredWidth();
2125 final int height = fi.getMeasuredHeight();
2126
2127 // Lazy load ImageView, Bitmap and Canvas
2128 if (mFolderIconImageView == null) {
2129 mFolderIconImageView = new ImageView(this);
2130 }
2131 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2132 mFolderIconBitmap.getHeight() != height) {
2133 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2134 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2135 }
2136
2137 DragLayer.LayoutParams lp;
2138 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2139 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2140 } else {
2141 lp = new DragLayer.LayoutParams(width, height);
2142 }
2143
Adam Cohen307fe232012-08-16 17:55:58 -07002144 // The layout from which the folder is being opened may be scaled, adjust the starting
2145 // view size by this scale factor.
2146 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002147 lp.customPosition = true;
2148 lp.x = mRectForFolderAnimation.left;
2149 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002150 lp.width = (int) (scale * width);
2151 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002152
2153 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2154 fi.draw(mFolderIconCanvas);
2155 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002156 if (fi.getFolder() != null) {
2157 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2158 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002159 }
Adam Cohen268c4752012-06-06 17:47:33 -07002160 // Just in case this image view is still in the drag layer from a previous animation,
2161 // we remove it and re-add it.
2162 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2163 mDragLayer.removeView(mFolderIconImageView);
2164 }
2165 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002166 if (fi.getFolder() != null) {
2167 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002168 }
Adam Cohen268c4752012-06-06 17:47:33 -07002169 }
2170
Adam Cohen2801caf2011-05-13 20:57:39 -07002171 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002172 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002173 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2174 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2175 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2176
Adam Cohenc51934b2011-07-26 21:07:43 -07002177 FolderInfo info = (FolderInfo) fi.getTag();
2178 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2179 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002180 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2181 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002182 }
2183
Adam Cohen268c4752012-06-06 17:47:33 -07002184 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2185 copyFolderIconToImage(fi);
2186 fi.setVisibility(View.INVISIBLE);
2187
Michael Jurka2ecf9952012-06-18 12:52:28 -07002188 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002189 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002190 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2191 oa.start();
2192 }
2193
Adam Cohen268c4752012-06-06 17:47:33 -07002194 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002195 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002196 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2197 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2198 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2199
Adam Cohen268c4752012-06-06 17:47:33 -07002200 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002201
Adam Cohen268c4752012-06-06 17:47:33 -07002202 // We remove and re-draw the FolderIcon in-case it has changed
2203 mDragLayer.removeView(mFolderIconImageView);
2204 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002205 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002206 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002207 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002208 oa.addListener(new AnimatorListenerAdapter() {
2209 @Override
2210 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002211 if (cl != null) {
2212 cl.clearFolderLeaveBehind();
2213 // Remove the ImageView copy of the FolderIcon and make the original visible.
2214 mDragLayer.removeView(mFolderIconImageView);
2215 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002216 }
2217 }
2218 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002219 oa.start();
2220 }
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002223 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 * is animated relative to the specified View. If the View is null, no animation
2225 * is played.
2226 *
2227 * @param folderInfo The FolderInfo describing the folder to open.
2228 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002229 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002230 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002231 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232
Adam Cohena9cf38f2011-05-02 15:36:58 -07002233 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234
Adam Cohen4554ee12011-08-03 16:13:21 -07002235 // Just verify that the folder hasn't already been added to the DragLayer.
2236 // There was a one-off crash where the folder had a parent already.
2237 if (folder.getParent() == null) {
2238 mDragLayer.addView(folder);
2239 mDragController.addDropTarget((DropTarget) folder);
2240 } else {
2241 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2242 folder.getParent() + ").");
2243 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002244 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002245 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002246 }
2247
2248 public void closeFolder() {
2249 Folder folder = mWorkspace.getOpenFolder();
2250 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002251 if (folder.isEditingName()) {
2252 folder.dismissEditingName();
2253 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002254 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002255
2256 // Dismiss the folder cling
2257 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002258 }
2259 }
2260
2261 void closeFolder(Folder folder) {
2262 folder.getInfo().opened = false;
2263
2264 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2265 if (parent != null) {
2266 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2267 shrinkAndFadeInFolderIcon(fi);
2268 }
2269 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 }
2271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002273 if (!isDraggingEnabled()) return false;
2274 if (isWorkspaceLocked()) return false;
2275 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276
2277 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002278 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 resetAddInfo();
2282 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002284 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 return true;
2286 }
2287
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 // The hotseat touch handling does not go through Workspace, and we always allow long press
2289 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002290 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002291 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2292 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002293 if (itemUnderLongClick == null) {
2294 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002295 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2296 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002297 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002299 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002301 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 }
2303 }
2304 }
2305 return true;
2306 }
2307
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 boolean isHotseatLayout(View layout) {
2309 return mHotseat != null && layout != null &&
2310 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2311 }
2312 Hotseat getHotseat() {
2313 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002314 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002315 SearchDropTargetBar getSearchBar() {
2316 return mSearchDropTargetBar;
2317 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002318
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 /**
2320 * Returns the CellLayout of the specified container at the specified screen.
2321 */
2322 CellLayout getCellLayout(long container, int screen) {
2323 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2324 if (mHotseat != null) {
2325 return mHotseat.getLayout();
2326 } else {
2327 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002328 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 } else {
2330 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002331 }
2332 }
2333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 Workspace getWorkspace() {
2335 return mWorkspace;
2336 }
2337
Daniel Sandler843e8602010-06-07 14:59:01 -04002338 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2339 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002340 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002341 }
2342
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002343 public boolean isAllAppsButtonRank(int rank) {
2344 return mHotseat.isAllAppsButtonRank(rank);
2345 }
2346
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 /**
2348 * Helper method for the cameraZoomIn/cameraZoomOut animations
2349 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 * @param scaleFactor The scale factor used for the zoom
2352 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002353 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002354 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002355 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002356 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002357
Winson Chung18f41f82012-05-09 13:28:10 -07002358 void disableWallpaperIfInAllApps() {
2359 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002360 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002361 if (mAppsCustomizeTabHost != null &&
2362 !mAppsCustomizeTabHost.isTransitioning()) {
2363 updateWallpaperVisibility(false);
2364 }
2365 }
2366 }
2367
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002368 void updateWallpaperVisibility(boolean visible) {
2369 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2370 int curflags = getWindow().getAttributes().flags
2371 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2372 if (wpflags != curflags) {
2373 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2374 }
2375 }
2376
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002377 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2378 if (v instanceof LauncherTransitionable) {
2379 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2380 }
2381 }
2382
Michael Jurkabed61d22012-02-14 22:51:29 -08002383 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2384 if (v instanceof LauncherTransitionable) {
2385 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2386 }
Winson Chung70442722012-02-10 15:43:22 -08002387
2388 // Update the workspace transition step as well
2389 dispatchOnLauncherTransitionStep(v, 0f);
2390 }
2391
2392 private void dispatchOnLauncherTransitionStep(View v, float t) {
2393 if (v instanceof LauncherTransitionable) {
2394 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2395 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002396 }
2397
2398 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2399 if (v instanceof LauncherTransitionable) {
2400 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2401 }
Winson Chung70442722012-02-10 15:43:22 -08002402
2403 // Update the workspace transition step as well
2404 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002405 }
2406
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002407 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002408 * Things to test when changing the following seven functions.
2409 * - Home from workspace
2410 * - from center screen
2411 * - from other screens
2412 * - Home from all apps
2413 * - from center screen
2414 * - from other screens
2415 * - Back from all apps
2416 * - from center screen
2417 * - from other screens
2418 * - Launch app from workspace and quit
2419 * - with back
2420 * - with home
2421 * - Launch app from all apps and quit
2422 * - with back
2423 * - with home
2424 * - Go to a screen that's not the default, then all
2425 * apps, and launch and app, and go back
2426 * - with back
2427 * -with home
2428 * - On workspace, long press power and go back
2429 * - with back
2430 * - with home
2431 * - On all apps, long press power and go back
2432 * - with back
2433 * - with home
2434 * - On workspace, power off
2435 * - On all apps, power off
2436 * - Launch an app and turn off the screen while in that app
2437 * - Go back with home key
2438 * - Go back with back key TODO: make this not go to workspace
2439 * - From all apps
2440 * - From workspace
2441 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2442 * - From all apps
2443 * - From the center workspace
2444 * - From another workspace
2445 */
2446
2447 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448 * Zoom the camera out from the workspace to reveal 'toView'.
2449 * Assumes that the view to show is anchored at either the very top or very bottom
2450 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002452 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002453 if (mStateAnimation != null) {
2454 mStateAnimation.cancel();
2455 mStateAnimation = null;
2456 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002457 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002458
Winson Chungf0ea4d32011-06-06 14:27:16 -07002459 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2460 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2461 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002462 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002463 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002464 final int startDelay =
2465 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002466
Michael Jurkab3e22d92011-10-31 15:58:33 -07002467 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468
Michael Jurkad74c9842011-07-10 12:44:21 -07002469 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002470 Animator workspaceAnim =
2471 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472
2473 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002474 toView.setScaleX(scale);
2475 toView.setScaleY(scale);
2476 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2477 scaleAnim.
2478 scaleX(1f).scaleY(1f).
2479 setDuration(duration).
2480 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002481
Winson Chungf0ea4d32011-06-06 14:27:16 -07002482 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002483 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002484 final ObjectAnimator alphaAnim = ObjectAnimator
2485 .ofFloat(toView, "alpha", 0f, 1f)
2486 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002487 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002488 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2489 @Override
2490 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002491 if (animation == null) {
2492 throw new RuntimeException("animation is null");
2493 }
Winson Chung70442722012-02-10 15:43:22 -08002494 float t = (Float) animation.getAnimatedValue();
2495 dispatchOnLauncherTransitionStep(fromView, t);
2496 dispatchOnLauncherTransitionStep(toView, t);
2497 }
2498 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002499
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002500 // toView should appear right at the end of the workspace shrink
2501 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002502 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002503 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002504 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505
2506 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002507 boolean animationCancelled = false;
2508
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002509 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002510 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002511 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512 // Prepare the position
2513 toView.setTranslationX(0.0f);
2514 toView.setTranslationY(0.0f);
2515 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002516 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002518 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002519 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002520 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2521 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002522
Michael Jurkafa7969f2012-09-21 16:39:14 -07002523 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002524 // Hide the workspace scrollbar
2525 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002526 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002527 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002528 if (!animationCancelled) {
2529 updateWallpaperVisibility(false);
2530 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002531
2532 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002533 if (mSearchDropTargetBar != null) {
2534 mSearchDropTargetBar.hideSearchBar(false);
2535 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002536 }
2537
Adam Cohencff6af82011-09-13 14:51:53 -07002538 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002539 public void onAnimationCancel(Animator animation) {
2540 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002541 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 });
2543
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002544 if (workspaceAnim != null) {
2545 mStateAnimation.play(workspaceAnim);
2546 }
Michael Jurka1899a362011-11-03 13:50:45 -07002547
2548 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002549 final ViewTreeObserver observer;
2550
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002551 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2552 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002553
2554 // If any of the objects being animated haven't been measured/laid out
2555 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002556 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002557 (mWorkspace.getMeasuredWidth() == 0) ||
2558 (toView.getMeasuredWidth() == 0)) {
2559 observer = mWorkspace.getViewTreeObserver();
2560 delayAnim = true;
2561 } else {
2562 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002563 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002564
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002565 final AnimatorSet stateAnimation = mStateAnimation;
2566 final Runnable startAnimRunnable = new Runnable() {
2567 public void run() {
2568 // Check that mStateAnimation hasn't changed while
2569 // we waited for a layout/draw pass
2570 if (mStateAnimation != stateAnimation)
2571 return;
2572 setPivotsForZoom(toView, scale);
2573 dispatchOnLauncherTransitionStart(fromView, animated, false);
2574 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002575 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002576 public void run() {
2577 // Check that mStateAnimation hasn't changed while
2578 // we waited for a layout/draw pass
2579 if (mStateAnimation != stateAnimation)
2580 return;
2581 mStateAnimation.start();
2582 }
2583 });
2584 }
2585 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002586 if (delayAnim) {
2587 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2588 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002589 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002590 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002591 }
2592 };
2593 observer.addOnGlobalLayoutListener(delayedStart);
2594 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002595 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002596 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 } else {
2598 toView.setTranslationX(0.0f);
2599 toView.setTranslationY(0.0f);
2600 toView.setScaleX(1.0f);
2601 toView.setScaleY(1.0f);
2602 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002603 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002604
Michael Jurkabed61d22012-02-14 22:51:29 -08002605 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2606 // Hide the workspace scrollbar
2607 mWorkspace.hideScrollingIndicator(true);
2608 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002609
2610 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002611 if (mSearchDropTargetBar != null) {
2612 mSearchDropTargetBar.hideSearchBar(false);
2613 }
Michael Jurkaabded662011-03-04 12:06:57 -08002614 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002615 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002616 dispatchOnLauncherTransitionStart(fromView, animated, false);
2617 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002618 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002619 dispatchOnLauncherTransitionStart(toView, animated, false);
2620 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002621 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002622 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002623 }
2624
2625 /**
2626 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002627 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002628 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002629 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002630 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2631 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002632
Michael Jurkab3e22d92011-10-31 15:58:33 -07002633 if (mStateAnimation != null) {
2634 mStateAnimation.cancel();
2635 mStateAnimation = null;
2636 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002637 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002638
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002640 final int fadeOutDuration =
2641 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002642 final float scaleFactor = (float)
2643 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2644 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002645 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002646 Animator workspaceAnim = null;
2647
2648 if (toState == State.WORKSPACE) {
2649 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2650 workspaceAnim = mWorkspace.getChangeStateAnimation(
2651 Workspace.State.NORMAL, animated, stagger);
2652 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2653 workspaceAnim = mWorkspace.getChangeStateAnimation(
2654 Workspace.State.SPRING_LOADED, animated);
2655 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002656
Michael Jurkab3e22d92011-10-31 15:58:33 -07002657 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002658 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002659 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002660 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002661 final LauncherViewPropertyAnimator scaleAnim =
2662 new LauncherViewPropertyAnimator(fromView);
2663 scaleAnim.
2664 scaleX(scaleFactor).scaleY(scaleFactor).
2665 setDuration(duration).
2666 setInterpolator(new Workspace.ZoomInInterpolator());
2667
2668 final ObjectAnimator alphaAnim = ObjectAnimator
2669 .ofFloat(fromView, "alpha", 1f, 0f)
2670 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002671 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002672 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2673 @Override
2674 public void onAnimationUpdate(ValueAnimator animation) {
2675 float t = 1f - (Float) animation.getAnimatedValue();
2676 dispatchOnLauncherTransitionStep(fromView, t);
2677 dispatchOnLauncherTransitionStep(toView, t);
2678 }
2679 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002680
Michael Jurka2ecf9952012-06-18 12:52:28 -07002681 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002682
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002683 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2684 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002685 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002686
2687 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002688 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002689 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002690 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002692 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2693 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002694 if (mWorkspace != null) {
2695 mWorkspace.hideScrollingIndicator(false);
2696 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002697 if (onCompleteRunnable != null) {
2698 onCompleteRunnable.run();
2699 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002700 mAppsCustomizeContent.updateCurrentPageScroll();
2701 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002702 }
2703 });
2704
Winson Chungf0ea4d32011-06-06 14:27:16 -07002705 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706 if (workspaceAnim != null) {
2707 mStateAnimation.play(workspaceAnim);
2708 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002709 dispatchOnLauncherTransitionStart(fromView, animated, true);
2710 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002711 final Animator stateAnimation = mStateAnimation;
2712 mWorkspace.post(new Runnable() {
2713 public void run() {
2714 if (stateAnimation != mStateAnimation)
2715 return;
2716 mStateAnimation.start();
2717 }
2718 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002719 } else {
2720 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002721 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002722 dispatchOnLauncherTransitionStart(fromView, animated, true);
2723 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002724 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002725 dispatchOnLauncherTransitionStart(toView, animated, true);
2726 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002727 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002728 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002729 }
2730
Michael Jurkae326f182011-11-21 14:05:46 -08002731 @Override
2732 public void onTrimMemory(int level) {
2733 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002734 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002735 mAppsCustomizeTabHost.onTrimMemory();
2736 }
2737 }
2738
Winson Chung18f41f82012-05-09 13:28:10 -07002739 @Override
2740 public void onWindowFocusChanged(boolean hasFocus) {
2741 if (!hasFocus) {
2742 // When another window occludes launcher (like the notification shade, or recents),
2743 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2744 updateWallpaperVisibility(true);
2745 } else {
2746 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002747 mWorkspace.postDelayed(new Runnable() {
2748 @Override
2749 public void run() {
2750 disableWallpaperIfInAllApps();
2751 }
2752 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002753 }
2754 }
2755
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002756 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002757 showWorkspace(animated, null);
2758 }
2759
2760 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002761 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002762 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002763 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002764 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002765
Winson Chungc7d2b602012-05-16 17:02:20 -07002766 // Show the search bar (only animate if we were showing the drop target bar in spring
2767 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002768 if (mSearchDropTargetBar != null) {
2769 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2770 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002771
Michael Jurkab737ee62011-11-15 15:57:22 -08002772 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002773 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002774
2775 // Set focus to the AppsCustomize button
2776 if (mAllAppsButton != null) {
2777 mAllAppsButton.requestFocus();
2778 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002779 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002780
Michael Jurkab737ee62011-11-15 15:57:22 -08002781 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002782
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002783 // Change the state *after* we've called all the transition code
2784 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002785
Winson Chung5fb63472011-02-02 17:03:37 -08002786 // Resume the auto-advance of widgets
2787 mUserPresent = true;
2788 updateRunning();
2789
alanv1d4fde62012-10-17 13:15:47 -07002790 // Send an accessibility event to announce the context change
2791 getWindow().getDecorView()
2792 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Joe Onorato00acb122009-08-04 16:04:30 -04002793 }
2794
Michael Jurkab3e22d92011-10-31 15:58:33 -07002795 void showAllApps(boolean animated) {
2796 if (mState != State.WORKSPACE) return;
2797
2798 showAppsCustomizeHelper(animated, false);
2799 mAppsCustomizeTabHost.requestFocus();
2800
Michael Jurkab3e22d92011-10-31 15:58:33 -07002801 // Change the state *after* we've called all the transition code
2802 mState = State.APPS_CUSTOMIZE;
2803
2804 // Pause the auto-advance of widgets until we are out of AllApps
2805 mUserPresent = false;
2806 updateRunning();
2807 closeFolder();
2808
2809 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002810 getWindow().getDecorView()
2811 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002812 }
2813
Adam Cohen7777d962011-08-18 18:58:38 -07002814 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002815 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002816 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002817 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002818 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002819 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002820 }
Adam Cohen7777d962011-08-18 18:58:38 -07002821
Adam Cohened66b2b2012-01-23 17:28:51 -08002822 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2823 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002824 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2825
Winson Chunge7a03942011-08-05 15:05:12 -07002826 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002827 @Override
2828 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002829 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002830 // Before we show workspace, hide all apps again because
2831 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2832 // clean up our state transition functions
2833 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002834 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002835 } else {
2836 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002837 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002838 }
2839 }, (extendedDelay ?
2840 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2841 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2842 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002843
Michael Jurkad3ef3062010-11-23 16:23:58 -08002844 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002845 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002846 final boolean animated = true;
2847 final boolean springLoaded = true;
2848 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002849 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002850 }
2851 // Otherwise, we are not in spring loaded mode, so don't do anything.
2852 }
2853
Michael Jurkab737ee62011-11-15 15:57:22 -08002854 void hideDockDivider() {
2855 if (mQsbDivider != null && mDockDivider != null) {
2856 mQsbDivider.setVisibility(View.INVISIBLE);
2857 mDockDivider.setVisibility(View.INVISIBLE);
2858 }
2859 }
2860
2861 void showDockDivider(boolean animated) {
2862 if (mQsbDivider != null && mDockDivider != null) {
2863 mQsbDivider.setVisibility(View.VISIBLE);
2864 mDockDivider.setVisibility(View.VISIBLE);
2865 if (mDividerAnimator != null) {
2866 mDividerAnimator.cancel();
2867 mQsbDivider.setAlpha(1f);
2868 mDockDivider.setAlpha(1f);
2869 mDividerAnimator = null;
2870 }
2871 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002872 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2873 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2874 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002875 int duration = 0;
2876 if (mSearchDropTargetBar != null) {
2877 duration = mSearchDropTargetBar.getTransitionInDuration();
2878 }
2879 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002880 mDividerAnimator.start();
2881 }
2882 }
2883 }
2884
Michael Jurkab3e22d92011-10-31 15:58:33 -07002885 void lockAllApps() {
2886 // TODO
2887 }
2888
2889 void unlockAllApps() {
2890 // TODO
2891 }
2892
Winson Chungf0ea4d32011-06-06 14:27:16 -07002893 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002894 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002895 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002896 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002897 if (!LauncherApplication.isScreenLarge()) {
2898 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002899 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002900 int duration = 0;
2901 if (mSearchDropTargetBar != null) {
2902 duration = mSearchDropTargetBar.getTransitionInDuration();
2903 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002904 mHotseat.animate().alpha(1f).setDuration(duration);
2905 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002906 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002907 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002908 }
2909 }
2910 }
2911
2912 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002913 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002914 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002915 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002916 if (!LauncherApplication.isScreenLarge()) {
2917 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002918 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002919 int duration = 0;
2920 if (mSearchDropTargetBar != null) {
2921 duration = mSearchDropTargetBar.getTransitionOutDuration();
2922 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002923 mHotseat.animate().alpha(0f).setDuration(duration);
2924 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002925 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002926 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002927 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002928 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002929 }
2930
Patrick Dubroy5f445422011-02-18 14:35:21 -08002931 /**
2932 * Add an item from all apps or customize onto the given workspace screen.
2933 * If layout is null, add to the current screen.
2934 */
2935 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002936 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002937 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002938 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002939 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002940
Winson Chungdff8ebb2011-09-08 17:25:31 -07002941 /** Maps the current orientation to an index for referencing orientation correct global icons */
2942 private int getCurrentOrientationIndexForGlobalIcons() {
2943 // default - 0, landscape - 1
2944 switch (getResources().getConfiguration().orientation) {
2945 case Configuration.ORIENTATION_LANDSCAPE:
2946 return 1;
2947 default:
2948 return 0;
2949 }
2950 }
2951
Michael Jurkaae65ee32012-04-30 11:45:54 -07002952 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002953 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002954 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002955 // Look for the toolbar icon specified in the activity meta-data
2956 Bundle metaData = packageManager.getActivityInfo(
2957 activityName, PackageManager.GET_META_DATA).metaData;
2958 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002959 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002960 if (iconResId != 0) {
2961 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002962 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002963 }
2964 }
2965 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002966 // This can happen if the activity defines an invalid drawable
2967 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2968 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002969 } catch (Resources.NotFoundException nfe) {
2970 // This can happen if the activity defines an invalid drawable
2971 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2972 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002973 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002974 return null;
2975 }
2976
2977 // if successful in getting icon, return it; otherwise, set button to use default drawable
2978 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002979 int buttonId, ComponentName activityName, int fallbackDrawableId,
2980 String toolbarResourceName) {
2981 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002982 Resources r = getResources();
2983 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2984 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002985
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002986 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002987 // If we were unable to find the icon via the meta-data, use a generic one
2988 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002989 toolbarIcon = r.getDrawable(fallbackDrawableId);
2990 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002991 if (button != null) {
2992 button.setCompoundDrawables(toolbarIcon, null, null, null);
2993 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002994 return null;
2995 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002996 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002997 if (button != null) {
2998 button.setCompoundDrawables(toolbarIcon, null, null, null);
2999 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003000 return toolbarIcon.getConstantState();
3001 }
3002 }
3003
3004 // if successful in getting icon, return it; otherwise, set button to use default drawable
3005 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003006 int buttonId, ComponentName activityName, int fallbackDrawableId,
3007 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003008 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003009 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003010
Michael Jurka19e0fc52011-07-22 18:00:21 -07003011 if (button != null) {
3012 // If we were unable to find the icon via the meta-data, use a
3013 // generic one
3014 if (toolbarIcon == null) {
3015 button.setImageResource(fallbackDrawableId);
3016 } else {
3017 button.setImageDrawable(toolbarIcon);
3018 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003019 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003020
3021 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3022
Michael Jurka0423dcf2010-10-05 14:56:18 -07003023 }
3024
Winson Chung1b884092012-06-08 17:13:02 -07003025 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003026 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003027 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003028 }
3029
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003030 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003031 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003032 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003033 }
3034
Winson Chungbb185bd2011-11-21 12:31:42 -08003035 private void invalidatePressedFocusedStates(View container, View button) {
3036 if (container instanceof HolographicLinearLayout) {
3037 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3038 layout.invalidatePressedFocusedStates();
3039 } else if (button instanceof HolographicImageView) {
3040 HolographicImageView view = (HolographicImageView) button;
3041 view.invalidatePressedFocusedStates();
3042 }
3043 }
3044
Winson Chungc51db6a2011-10-05 11:44:49 -07003045 private boolean updateGlobalSearchIcon() {
3046 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003047 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003048 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003049 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003050 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003051
Winson Chung1cad91e2011-05-25 17:41:01 -07003052 final SearchManager searchManager =
3053 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3054 ComponentName activityName = searchManager.getGlobalSearchActivity();
3055 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003056 int coi = getCurrentOrientationIndexForGlobalIcons();
3057 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003058 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3059 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3060 if (sGlobalSearchIcon[coi] == null) {
3061 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3062 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3063 TOOLBAR_ICON_METADATA_NAME);
3064 }
3065
Winson Chungc51db6a2011-10-05 11:44:49 -07003066 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3067 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003068 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003069 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003070 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003071 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003072 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3073 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3074 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003075 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003076 if (voiceButtonProxy != null) {
3077 voiceButtonProxy.setVisibility(View.GONE);
3078 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003079 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003080 }
3081 }
3082
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003083 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003084 final View searchButtonContainer = findViewById(R.id.search_button_container);
3085 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003086 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003087 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003088 }
3089
Winson Chungc51db6a2011-10-05 11:44:49 -07003090 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003091 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003092 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003093 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003094
Winson Chungc51db6a2011-10-05 11:44:49 -07003095 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003096 final SearchManager searchManager =
3097 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3098 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3099
3100 ComponentName activityName = null;
3101 if (globalSearchActivity != null) {
3102 // Check if the global search activity handles voice search
3103 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3104 intent.setPackage(globalSearchActivity.getPackageName());
3105 activityName = intent.resolveActivity(getPackageManager());
3106 }
3107
3108 if (activityName == null) {
3109 // Fallback: check if an activity other than the global search activity
3110 // resolves this
3111 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3112 activityName = intent.resolveActivity(getPackageManager());
3113 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003114 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003115 int coi = getCurrentOrientationIndexForGlobalIcons();
3116 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003117 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3118 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3119 if (sVoiceSearchIcon[coi] == null) {
3120 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3121 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3122 TOOLBAR_ICON_METADATA_NAME);
3123 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003124 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003125 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003126 if (voiceButtonProxy != null) {
3127 voiceButtonProxy.setVisibility(View.VISIBLE);
3128 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003129 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003130 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003131 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003132 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003133 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003134 if (voiceButtonProxy != null) {
3135 voiceButtonProxy.setVisibility(View.GONE);
3136 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003137 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003138 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003139 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003140
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003141 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003142 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3143 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003144 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003145 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003146 }
3147
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003148 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003149 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003150 */
3151 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003152 final View marketButton = findViewById(R.id.market_button);
3153 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3154 // Find the app market activity by resolving an intent.
3155 // (If multiple app markets are installed, it will return the ResolverActivity.)
3156 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003157 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003158 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003159 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003160 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003161 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3162 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003163 marketButton.setVisibility(View.VISIBLE);
3164 } else {
3165 // We should hide and disable the view so that we don't try and restore the visibility
3166 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3167 marketButton.setVisibility(View.GONE);
3168 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003170 }
3171
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003172 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003173 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3174 Resources r = getResources();
3175 Drawable marketIconDrawable = d.newDrawable(r);
3176 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3177 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3178 marketIconDrawable.setBounds(0, 0, w, h);
3179
3180 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003181 }
3182
Michael Jurkad7c28052012-04-27 15:43:36 -07003183 @Override
3184 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003185 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003186 final List<CharSequence> text = event.getText();
3187 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003188 // Populate event with a fake title based on the current state.
3189 if (mState == State.APPS_CUSTOMIZE) {
3190 text.add(getString(R.string.all_apps_button_label));
3191 } else {
3192 text.add(getString(R.string.all_apps_home_button_label));
3193 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003194 return result;
3195 }
3196
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003197 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003198 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003199 */
3200 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3201 @Override
3202 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003203 closeSystemDialogs();
3204 }
3205 }
3206
3207 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003208 * Receives notifications whenever the appwidgets are reset.
3209 */
3210 private class AppWidgetResetObserver extends ContentObserver {
3211 public AppWidgetResetObserver() {
3212 super(new Handler());
3213 }
3214
3215 @Override
3216 public void onChange(boolean selfChange) {
3217 onAppWidgetReset();
3218 }
3219 }
3220
3221 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003222 * If the activity is currently paused, signal that we need to re-run the loader
3223 * in onResume.
3224 *
3225 * This needs to be called from incoming places where resources might have been loaded
3226 * while we are paused. That is becaues the Configuration might be wrong
3227 * when we're not running, and if it comes back to what it was when we
3228 * were paused, we are not restarted.
3229 *
3230 * Implementation of the method from LauncherModel.Callbacks.
3231 *
3232 * @return true if we are currently paused. The caller might be able to
3233 * skip some work in that case since we will come back again.
3234 */
3235 public boolean setLoadOnResume() {
3236 if (mPaused) {
3237 Log.i(TAG, "setLoadOnResume");
3238 mOnResumeNeedsLoad = true;
3239 return true;
3240 } else {
3241 return false;
3242 }
3243 }
3244
3245 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003246 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003249 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003250 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003251 } else {
3252 return SCREEN_COUNT / 2;
3253 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003254 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003255
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 /**
3257 * Refreshes the shortcuts shown on the workspace.
3258 *
3259 * Implementation of the method from LauncherModel.Callbacks.
3260 */
3261 public void startBinding() {
3262 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003263
Winson Chungf0c6ae02012-03-21 16:10:31 -07003264 mNewShortcutAnimatePage = -1;
3265 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003266 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003267 int count = workspace.getChildCount();
3268 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003269 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003270 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3271 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003273 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003274 if (mHotseat != null) {
3275 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 }
3277 }
3278
3279 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 * Bind the items start-end from the list.
3281 *
3282 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003283 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003285 setLoadOnResume();
3286
Winson Chungf0c6ae02012-03-21 16:10:31 -07003287 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3288 Set<String> newApps = new HashSet<String>();
3289 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3290
3291 Workspace workspace = mWorkspace;
3292 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003294
3295 // Short circuit if we are loading dock items for a configuration which has no dock
3296 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3297 mHotseat == null) {
3298 continue;
3299 }
3300
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 switch (item.itemType) {
3302 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3303 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003304 ShortcutInfo info = (ShortcutInfo) item;
3305 String uri = info.intent.toUri(0).toString();
3306 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003307 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3308 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003309 boolean animateIconUp = false;
3310 synchronized (newApps) {
3311 if (newApps.contains(uri)) {
3312 animateIconUp = newApps.remove(uri);
3313 }
3314 }
3315 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003316 // Prepare the view to be animated up
3317 shortcut.setAlpha(0f);
3318 shortcut.setScaleX(0f);
3319 shortcut.setScaleY(0f);
3320 mNewShortcutAnimatePage = item.screen;
3321 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3322 mNewShortcutAnimateViews.add(shortcut);
3323 }
3324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003326 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003327 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003328 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003329 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003330 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3331 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003332 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003334 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003335
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003337 }
3338
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 /**
3340 * Implementation of the method from LauncherModel.Callbacks.
3341 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003342 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003343 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003344 sFolders.clear();
3345 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003346 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003347
3348 /**
3349 * Add the views for a widget to the workspace.
3350 *
3351 * Implementation of the method from LauncherModel.Callbacks.
3352 */
3353 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003354 setLoadOnResume();
3355
Daniel Sandler843e8602010-06-07 14:59:01 -04003356 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3357 if (DEBUG_WIDGETS) {
3358 Log.d(TAG, "bindAppWidget: " + item);
3359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 final Workspace workspace = mWorkspace;
3361
3362 final int appWidgetId = item.appWidgetId;
3363 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003364 if (DEBUG_WIDGETS) {
3365 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3366 }
3367
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3369
Joe Onorato9c1289c2009-08-17 11:03:03 -04003370 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003371 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372
Winson Chung3d503fb2011-07-13 17:25:49 -07003373 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003375 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 workspace.requestLayout();
3378
Daniel Sandler843e8602010-06-07 14:59:01 -04003379 if (DEBUG_WIDGETS) {
3380 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3381 + (SystemClock.uptimeMillis()-start) + "ms");
3382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 }
3384
Adam Cohen1462de32012-07-24 22:34:36 -07003385 public void onPageBoundSynchronously(int page) {
3386 mSynchronouslyBoundPages.add(page);
3387 }
3388
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 /**
3390 * Callback saying that there aren't any more items to bind.
3391 *
3392 * Implementation of the method from LauncherModel.Callbacks.
3393 */
3394 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003395 setLoadOnResume();
3396
Joe Onorato9c1289c2009-08-17 11:03:03 -04003397 if (mSavedState != null) {
3398 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003399 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 mSavedState = null;
3402 }
3403
Adam Cohen1462de32012-07-24 22:34:36 -07003404 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003406 // If we received the result of any pending adds while the loader was running (e.g. the
3407 // widget configuration forced an orientation change), process them now.
3408 for (int i = 0; i < sPendingAddList.size(); i++) {
3409 completeAdd(sPendingAddList.get(i));
3410 }
3411 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412
Winson Chungc51db6a2011-10-05 11:44:49 -07003413 // Update the market app icon as necessary (the other icons will be managed in response to
3414 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003415 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003416
Winson Chungf0c6ae02012-03-21 16:10:31 -07003417 // Animate up any icons as necessary
3418 if (mVisible || mWorkspaceLoading) {
3419 Runnable newAppsRunnable = new Runnable() {
3420 @Override
3421 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003422 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003423 }
3424 };
Winson Chunga2413752012-04-03 14:22:34 -07003425
3426 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3427 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3428 if (canRunNewAppsAnimation()) {
3429 // If the user has not interacted recently, then either snap to the new page to show
3430 // the new-apps animation or just run them if they are to appear on the current page
3431 if (willSnapPage) {
3432 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3433 } else {
3434 runNewAppsAnimation(false);
3435 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003436 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003437 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003438 // are on another page (or just normally if they are added to the current page)
3439 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003440 }
3441 }
3442
3443 mWorkspaceLoading = false;
3444 }
3445
Winson Chunga2413752012-04-03 14:22:34 -07003446 private boolean canRunNewAppsAnimation() {
3447 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3448 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3449 }
3450
Winson Chungf0c6ae02012-03-21 16:10:31 -07003451 /**
3452 * Runs a new animation that scales up icons that were added while Launcher was in the
3453 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003454 *
3455 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003456 */
Winson Chunga2413752012-04-03 14:22:34 -07003457 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003458 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003459 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003460
3461 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003462 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3463 @Override
3464 public int compare(View a, View b) {
3465 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3466 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3467 int cellCountX = LauncherModel.getCellCountX();
3468 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3469 }
3470 });
Winson Chunga2413752012-04-03 14:22:34 -07003471
3472 // Animate each of the views in place (or show them immediately if requested)
3473 if (immediate) {
3474 for (View v : mNewShortcutAnimateViews) {
3475 v.setAlpha(1f);
3476 v.setScaleX(1f);
3477 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003478 }
Winson Chunga2413752012-04-03 14:22:34 -07003479 } else {
3480 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3481 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003482 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003483 PropertyValuesHolder.ofFloat("alpha", 1f),
3484 PropertyValuesHolder.ofFloat("scaleX", 1f),
3485 PropertyValuesHolder.ofFloat("scaleY", 1f));
3486 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3487 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3488 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3489 bounceAnims.add(bounceAnim);
3490 }
3491 anim.playTogether(bounceAnims);
3492 anim.addListener(new AnimatorListenerAdapter() {
3493 @Override
3494 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003495 if (mWorkspace != null) {
3496 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3497 }
Winson Chunga2413752012-04-03 14:22:34 -07003498 }
3499 });
3500 anim.start();
3501 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003502
3503 // Clean up
3504 mNewShortcutAnimatePage = -1;
3505 mNewShortcutAnimateViews.clear();
3506 new Thread("clearNewAppsThread") {
3507 public void run() {
3508 mSharedPrefs.edit()
3509 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3510 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3511 .commit();
3512 }
3513 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003514 }
3515
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003516 @Override
3517 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003518 boolean searchVisible = updateGlobalSearchIcon();
3519 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003520 if (mSearchDropTargetBar != null) {
3521 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3522 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003523 }
3524
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003525 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003526 * Add the icons for all apps.
3527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003530 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003531 Runnable setAllAppsRunnable = new Runnable() {
3532 public void run() {
3533 if (mAppsCustomizeContent != null) {
3534 mAppsCustomizeContent.setApps(apps);
3535 }
3536 }
3537 };
3538
Michael Jurkac57b7a82011-08-09 22:02:20 -07003539 // Remove the progress bar entirely; we could also make it GONE
3540 // but better to remove it since we know it's not going to be used
3541 View progressBar = mAppsCustomizeTabHost.
3542 findViewById(R.id.apps_customize_progress_bar);
3543 if (progressBar != null) {
3544 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003545
3546 // We just post the call to setApps so the user sees the progress bar
3547 // disappear-- otherwise, it just looks like the progress bar froze
3548 // which doesn't look great
3549 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3550 } else {
3551 // If we did not initialize the spinner in onCreate, then we can directly set the
3552 // list of applications without waiting for any progress bars views to be hidden.
3553 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 }
3556
3557 /**
3558 * A package was installed.
3559 *
3560 * Implementation of the method from LauncherModel.Callbacks.
3561 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003562 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003563 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003564
Winson Chung785d2eb2011-04-14 16:08:02 -07003565 if (mAppsCustomizeContent != null) {
3566 mAppsCustomizeContent.addApps(apps);
3567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 }
3569
3570 /**
3571 * A package was updated.
3572 *
3573 * Implementation of the method from LauncherModel.Callbacks.
3574 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003575 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003576 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003577 if (mWorkspace != null) {
3578 mWorkspace.updateShortcuts(apps);
3579 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003580
Winson Chung785d2eb2011-04-14 16:08:02 -07003581 if (mAppsCustomizeContent != null) {
3582 mAppsCustomizeContent.updateApps(apps);
3583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 }
3585
3586 /**
3587 * A package was uninstalled.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
Winson Chungcd810732012-06-18 16:45:43 -07003591 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003592 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003593 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003594 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003595
Winson Chung785d2eb2011-04-14 16:08:02 -07003596 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003597 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003598 }
Winson Chunga1820962011-10-03 16:31:06 -07003599
3600 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003601 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 }
Joe Onoratobe386092009-11-17 17:32:16 -08003603
3604 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003605 * A number of packages were updated.
3606 */
3607 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003608 if (mAppsCustomizeContent != null) {
3609 mAppsCustomizeContent.onPackagesUpdated();
3610 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003611 }
3612
Winson Chung400438b2011-01-16 17:53:48 -08003613 private int mapConfigurationOriActivityInfoOri(int configOri) {
3614 final Display d = getWindowManager().getDefaultDisplay();
3615 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3616 switch (d.getRotation()) {
3617 case Surface.ROTATION_0:
3618 case Surface.ROTATION_180:
3619 // We are currently in the same basic orientation as the natural orientation
3620 naturalOri = configOri;
3621 break;
3622 case Surface.ROTATION_90:
3623 case Surface.ROTATION_270:
3624 // We are currently in the other basic orientation to the natural orientation
3625 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3626 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3627 break;
3628 }
3629
3630 int[] oriMap = {
3631 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3632 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3633 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3634 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3635 };
3636 // Since the map starts at portrait, we need to offset if this device's natural orientation
3637 // is landscape.
3638 int indexOffset = 0;
3639 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3640 indexOffset = 1;
3641 }
3642 return oriMap[(d.getRotation() + indexOffset) % 4];
3643 }
Adam Cohen446e9402011-09-15 18:21:21 -07003644
Winson Chung4b919f82012-05-01 10:44:08 -07003645 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003646 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003647 boolean enableRotation = forceEnableRotation ||
3648 getResources().getBoolean(R.bool.allow_rotation);
3649 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003650 }
Winson Chung4b919f82012-05-01 10:44:08 -07003651 public void lockScreenOrientation() {
3652 if (isRotationEnabled()) {
3653 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3654 .getConfiguration().orientation));
3655 }
3656 }
3657 public void unlockScreenOrientation(boolean immediate) {
3658 if (isRotationEnabled()) {
3659 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003660 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003661 } else {
3662 mHandler.postDelayed(new Runnable() {
3663 public void run() {
3664 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3665 }
3666 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003667 }
Winson Chung4b919f82012-05-01 10:44:08 -07003668 }
Winson Chung400438b2011-01-16 17:53:48 -08003669 }
3670
Winson Chung82f55532011-08-09 14:14:23 -07003671 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003672 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003673 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003674 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003675
Winson Chung7d7541e2011-09-16 20:14:36 -07003676 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003677 }
Michael Jurka22143132012-10-04 17:42:38 +02003678
Winson Chung7d7541e2011-09-16 20:14:36 -07003679 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003680 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003681 if (cling != null) {
3682 cling.init(this, positionData);
3683 cling.setVisibility(View.VISIBLE);
3684 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3685 if (animate) {
3686 cling.buildLayer();
3687 cling.setAlpha(0f);
3688 cling.animate()
3689 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003690 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003691 .setDuration(SHOW_CLING_DURATION)
3692 .setStartDelay(delay)
3693 .start();
3694 } else {
3695 cling.setAlpha(1f);
3696 }
Michael Jurka22143132012-10-04 17:42:38 +02003697 cling.setFocusableInTouchMode(true);
3698 cling.post(new Runnable() {
3699 public void run() {
3700 cling.setFocusable(true);
3701 cling.requestFocus();
3702 }
3703 });
3704 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3705 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003706 }
3707 return cling;
3708 }
Michael Jurka22143132012-10-04 17:42:38 +02003709
Winson Chung7d7541e2011-09-16 20:14:36 -07003710 private void dismissCling(final Cling cling, final String flag, int duration) {
Michael Jurka22143132012-10-04 17:42:38 +02003711 if (cling != null && cling.getVisibility() == View.VISIBLE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003712 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003713 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003714 anim.addListener(new AnimatorListenerAdapter() {
3715 public void onAnimationEnd(Animator animation) {
3716 cling.setVisibility(View.GONE);
3717 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003718 // We should update the shared preferences on a background thread
3719 new Thread("dismissClingThread") {
3720 public void run() {
3721 SharedPreferences.Editor editor = mSharedPrefs.edit();
3722 editor.putBoolean(flag, true);
3723 editor.commit();
3724 }
3725 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003726 };
3727 });
3728 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003729 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003730 }
3731 }
Michael Jurka22143132012-10-04 17:42:38 +02003732
Winson Chung9d9d74f2011-09-19 11:49:12 -07003733 private void removeCling(int id) {
3734 final View cling = findViewById(id);
3735 if (cling != null) {
3736 final ViewGroup parent = (ViewGroup) cling.getParent();
3737 parent.post(new Runnable() {
3738 @Override
3739 public void run() {
3740 parent.removeView(cling);
3741 }
3742 });
Michael Jurka22143132012-10-04 17:42:38 +02003743 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003744 }
3745 }
Michael Jurka974c3862012-05-22 22:00:31 -07003746
3747 private boolean skipCustomClingIfNoAccounts() {
3748 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3749 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3750 if (customCling) {
3751 AccountManager am = AccountManager.get(this);
3752 Account[] accounts = am.getAccountsByType("com.google");
3753 return accounts.length == 0;
3754 }
3755 return false;
3756 }
3757
Winson Chung7d7541e2011-09-16 20:14:36 -07003758 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003759 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003760 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003761 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3762 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003763 // If we're not using the default workspace layout, replace workspace cling
3764 // with a custom workspace cling (usually specified in an overlay)
3765 // For now, only do this on tablets
3766 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003767 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003768 // Use a custom cling
3769 View cling = findViewById(R.id.workspace_cling);
3770 ViewGroup clingParent = (ViewGroup) cling.getParent();
3771 int clingIndex = clingParent.indexOfChild(cling);
3772 clingParent.removeViewAt(clingIndex);
3773 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3774 clingParent.addView(customCling, clingIndex);
3775 customCling.setId(R.id.workspace_cling);
3776 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003777 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003778 } else {
3779 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003780 }
3781 }
3782 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003783 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003784 if (isClingsEnabled() &&
3785 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003786 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003787 } else {
3788 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003789 }
3790 }
3791 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003792 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003793 if (isClingsEnabled() &&
3794 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3795 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003796 } else {
3797 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003798 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003799 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003800 }
3801 public boolean isFolderClingVisible() {
3802 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003803 if (cling != null) {
3804 return cling.getVisibility() == View.VISIBLE;
3805 }
3806 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003807 }
Winson Chung82f55532011-08-09 14:14:23 -07003808 public void dismissWorkspaceCling(View v) {
3809 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003810 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003811 }
3812 public void dismissAllAppsCling(View v) {
3813 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003814 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3815 }
3816 public void dismissFolderCling(View v) {
3817 Cling cling = (Cling) findViewById(R.id.folder_cling);
3818 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003819 }
3820
Winson Chung80baf5a2010-08-09 16:03:15 -07003821 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003822 * Prints out out state for debugging.
3823 */
3824 public void dumpState() {
3825 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003826 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003827 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3828 Log.d(TAG, "mRestoring=" + mRestoring);
3829 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3830 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003831 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003832 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003833
Winson Chung785d2eb2011-04-14 16:08:02 -07003834 if (mAppsCustomizeContent != null) {
3835 mAppsCustomizeContent.dumpState();
3836 }
Joe Onoratobe386092009-11-17 17:32:16 -08003837 Log.d(TAG, "END launcher2 dump state");
3838 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003839
3840 @Override
3841 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3842 super.dump(prefix, fd, writer, args);
3843 writer.println(" ");
3844 writer.println("Debug logs: ");
3845 for (int i = 0; i < sDumpLogs.size(); i++) {
3846 writer.println(" " + sDumpLogs.get(i));
3847 }
3848 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003849
3850 public static void dumpDebugLogsToConsole() {
3851 Log.d(TAG, "");
3852 Log.d(TAG, "*********************");
3853 Log.d(TAG, "Launcher debug logs: ");
3854 for (int i = 0; i < sDumpLogs.size(); i++) {
3855 Log.d(TAG, " " + sDumpLogs.get(i));
3856 }
3857 Log.d(TAG, "*********************");
3858 Log.d(TAG, "");
3859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003860}
Michael Jurka2763be32011-02-24 11:19:57 -08003861
Michael Jurkaabded662011-03-04 12:06:57 -08003862interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003863 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003864 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003865 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003866 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003867 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003868}