blob: f31f82e2b0488f9a4f58b2cc9e4c59f65a50c33f [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 Cohen16d7ffc2011-10-05 17:49:14 -0700283 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
284
Winson Chung46353de2012-02-16 14:05:10 -0800285 // We only want to get the SharedPreferences once since it does an FS stat each time we get
286 // it from the context.
287 private SharedPreferences mSharedPrefs;
288
Winson Chungf0c6ae02012-03-21 16:10:31 -0700289 // Holds the page that we need to animate to, and the icon views that we need to animate up
290 // when we scroll to that page on resume.
291 private int mNewShortcutAnimatePage = -1;
292 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700293 private ImageView mFolderIconImageView;
294 private Bitmap mFolderIconBitmap;
295 private Canvas mFolderIconCanvas;
296 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700297
Michael Jurkaddd62e92011-02-16 17:49:14 -0800298 private BubbleTextView mWaitingForResume;
299
Michael Jurkac1f5d262011-09-30 19:32:27 -0700300 private Runnable mBuildLayersRunnable = new Runnable() {
301 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700302 if (mWorkspace != null) {
303 mWorkspace.buildPageHardwareLayers();
304 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700305 }
306 };
307
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800308 private static ArrayList<PendingAddArguments> sPendingAddList
309 = new ArrayList<PendingAddArguments>();
310
311 private static class PendingAddArguments {
312 int requestCode;
313 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700314 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315 int screen;
316 int cellX;
317 int cellY;
318 }
319
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700320
321 private boolean doesFileExist(String filename) {
322 FileInputStream fis = null;
323 try {
324 fis = openFileInput(filename);
325 fis.close();
326 return true;
327 } catch (java.io.FileNotFoundException e) {
328 return false;
329 } catch (java.io.IOException e) {
330 return true;
331 }
332 }
333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 @Override
335 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700336 if (DEBUG_STRICT_MODE) {
337 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
338 .detectDiskReads()
339 .detectDiskWrites()
340 .detectNetwork() // or .detectAll() for all detectable problems
341 .penaltyLog()
342 .build());
343 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
344 .detectLeakedSqlLiteObjects()
345 .detectLeakedClosableObjects()
346 .penaltyLog()
347 .penaltyDeath()
348 .build());
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800352 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700353 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
354 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800355 mModel = app.setLauncher(this);
356 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400357 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700359
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700360 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700361 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
362 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700364 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
365 // this also ensures that any synchronous binding below doesn't re-trigger another
366 // LauncherModel load.
367 mPaused = false;
368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200370 android.os.Debug.startMethodTracing(
371 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 }
373
374 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 setContentView(R.layout.launcher);
376 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700377 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800379 registerContentObservers();
380
Joe Onorato7c312c12009-08-13 21:36:53 -0700381 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mSavedState = savedInstanceState;
384 restoreState(mSavedState);
385
Winson Chunga12a2502010-12-20 14:41:35 -0800386 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700387 if (mAppsCustomizeContent != null) {
388 mAppsCustomizeContent.onPackagesUpdated();
389 }
Winson Chunga12a2502010-12-20 14:41:35 -0800390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 if (PROFILE_STARTUP) {
392 android.os.Debug.stopMethodTracing();
393 }
394
395 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700396 if (sPausedFromUserAction) {
397 // If the user leaves launcher, then we should just load items asynchronously when
398 // they return.
399 mModel.startLoader(true, -1);
400 } else {
401 // We only load the page synchronously if the user rotates (or triggers a
402 // configuration change) while launcher is in the foreground
403 mModel.startLoader(true, mWorkspace.getCurrentPage());
404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
406
Michael Jurkac57b7a82011-08-09 22:02:20 -0700407 if (!mModel.isAllAppsLoaded()) {
408 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
409 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
410 }
411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 // For handling default keys
413 mDefaultKeySsb = new SpannableStringBuilder();
414 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800415
416 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
417 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800418
Adam Cohenf9426d52012-06-04 17:26:21 -0700419 updateGlobalIcons();
420
421 // On large interfaces, we want the screen to auto-rotate based on the current orientation
422 unlockScreenOrientation(true);
423 }
424
Winson Chung4a2afa32012-07-19 14:53:05 -0700425 protected void onUserLeaveHint() {
426 super.onUserLeaveHint();
427 sPausedFromUserAction = true;
428 }
429
Adam Cohenf9426d52012-06-04 17:26:21 -0700430 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700431 boolean searchVisible = false;
432 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800433 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700434 int coi = getCurrentOrientationIndexForGlobalIcons();
435 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
436 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700437 updateAppMarketIcon();
438 searchVisible = updateGlobalSearchIcon();
439 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700440 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700441 if (sGlobalSearchIcon[coi] != null) {
442 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700444 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700445 if (sVoiceSearchIcon[coi] != null) {
446 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700447 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700448 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700449 if (sAppMarketIcon[coi] != null) {
450 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800451 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700452 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
Romain Guycbb89e42009-06-08 15:52:54 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 if (sLocaleConfiguration == null) {
457 new AsyncTask<Void, Void, LocaleConfiguration>() {
458 @Override
459 protected LocaleConfiguration doInBackground(Void... unused) {
460 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
461 readConfiguration(Launcher.this, localeConfiguration);
462 return localeConfiguration;
463 }
464
465 @Override
466 protected void onPostExecute(LocaleConfiguration result) {
467 sLocaleConfiguration = result;
468 checkForLocaleChange(); // recursive, but now with a locale configuration
469 }
470 }.execute();
471 return;
472 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 final Configuration configuration = getResources().getConfiguration();
475
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700476 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 final String locale = configuration.locale.toString();
478
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 final int mcc = configuration.mcc;
481
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700482 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 final int mnc = configuration.mnc;
484
Romain Guy84f296c2009-11-04 15:00:44 -0800485 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486
Romain Guy84f296c2009-11-04 15:00:44 -0800487 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 sLocaleConfiguration.locale = locale;
489 sLocaleConfiguration.mcc = mcc;
490 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700491
Joe Onorato0589f0f2010-02-08 13:44:00 -0800492 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493
494 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
495 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700496 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497 public void run() {
498 writeConfiguration(Launcher.this, localeConfiguration);
499 }
500 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700501 }
502 }
503
504 private static class LocaleConfiguration {
505 public String locale;
506 public int mcc = -1;
507 public int mnc = -1;
508 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700509
Romain Guy98d01652009-06-30 16:21:04 -0700510 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
511 DataInputStream in = null;
512 try {
513 in = new DataInputStream(context.openFileInput(PREFERENCES));
514 configuration.locale = in.readUTF();
515 configuration.mcc = in.readInt();
516 configuration.mnc = in.readInt();
517 } catch (FileNotFoundException e) {
518 // Ignore
519 } catch (IOException e) {
520 // Ignore
521 } finally {
522 if (in != null) {
523 try {
524 in.close();
525 } catch (IOException e) {
526 // Ignore
527 }
528 }
529 }
530 }
531
532 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
533 DataOutputStream out = null;
534 try {
535 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
536 out.writeUTF(configuration.locale);
537 out.writeInt(configuration.mcc);
538 out.writeInt(configuration.mnc);
539 out.flush();
540 } catch (FileNotFoundException e) {
541 // Ignore
542 } catch (IOException e) {
543 //noinspection ResultOfMethodCallIgnored
544 context.getFileStreamPath(PREFERENCES).delete();
545 } finally {
546 if (out != null) {
547 try {
548 out.close();
549 } catch (IOException e) {
550 // Ignore
551 }
552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554 }
555
Adam Cohen716b51e2011-06-30 12:09:54 -0700556 public DragLayer getDragLayer() {
557 return mDragLayer;
558 }
559
Winson Chung36a62fe2012-05-06 18:04:42 -0700560 boolean isDraggingEnabled() {
561 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
562 // that is subsequently removed from the workspace in startBinding().
563 return !mModel.isLoadingWorkspace();
564 }
565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 static int getScreen() {
567 synchronized (sLock) {
568 return sScreen;
569 }
570 }
571
572 static void setScreen(int screen) {
573 synchronized (sLock) {
574 sScreen = screen;
575 }
576 }
577
Winson Chung557d6ed2011-07-08 15:34:52 -0700578 /**
579 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
580 * a configuration step, this allows the proper animations to run after other transitions.
581 */
582 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700583 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800584 switch (args.requestCode) {
585 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700586 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
587 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 break;
589 case REQUEST_PICK_SHORTCUT:
590 processShortcut(args.intent);
591 break;
592 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700593 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
594 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700595 result = true;
596 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 case REQUEST_CREATE_APPWIDGET:
598 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800599 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700600 result = true;
601 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800602 case REQUEST_PICK_WALLPAPER:
603 // We just wanted the activity result here so we can clear mWaitingForResult
604 break;
605 }
Michael Jurka27614d22012-04-02 06:40:22 -0700606 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
607 // if you turned the screen off and then back while in All Apps, Launcher would not
608 // return to the workspace. Clearing mAddInfo.container here fixes this issue
609 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700610 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700614 protected void onActivityResult(
615 final int requestCode, final int resultCode, final Intent data) {
616 if (requestCode == REQUEST_BIND_APPWIDGET) {
617 int appWidgetId = data != null ?
618 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
619 if (resultCode == RESULT_CANCELED) {
620 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
621 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700622 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700623 }
624 return;
625 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700626 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800627 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
628 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700629 mWaitingForResult = false;
630
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 // We have special handling for widgets
632 if (isWidgetDrop) {
633 int appWidgetId = data != null ?
634 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700635 if (appWidgetId < 0) {
636 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
637 "widget configuration activity.");
638 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
639 } else {
640 completeTwoStageWidgetDrop(resultCode, appWidgetId);
641 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800642 return;
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 // The pattern used here is that a user PICKs a specific application,
646 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
649 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700650 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800651 final PendingAddArguments args = new PendingAddArguments();
652 args.requestCode = requestCode;
653 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 args.container = mPendingAddInfo.container;
655 args.screen = mPendingAddInfo.screen;
656 args.cellX = mPendingAddInfo.cellX;
657 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800658 if (isWorkspaceLocked()) {
659 sPendingAddList.add(args);
660 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700661 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800664 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700665 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
667 null);
668 }
669
670 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700671 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700672 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 Runnable onCompleteRunnable = null;
674 int animationType = 0;
675
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700676 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 if (resultCode == RESULT_OK) {
678 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
679 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 mPendingAddWidgetInfo);
681 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 onCompleteRunnable = new Runnable() {
683 @Override
684 public void run() {
685 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
686 mPendingAddInfo.screen, layout, null);
687 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
688 null);
689 }
690 };
691 } else if (resultCode == RESULT_CANCELED) {
692 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
693 onCompleteRunnable = new Runnable() {
694 @Override
695 public void run() {
696 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
697 null);
698 }
699 };
700 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700701 if (mDragLayer.getAnimatedView() != null) {
702 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
703 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
704 animationType, boundWidget, true);
705 } else {
706 // The animated view may be null in the case of a rotation during widget configuration
707 onCompleteRunnable.run();
708 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
711 @Override
712 protected void onResume() {
713 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700714
Winson Chung4a2afa32012-07-19 14:53:05 -0700715 // Restore the previous launcher state
716 if (mOnResumeState == State.WORKSPACE) {
717 showWorkspace(false);
718 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
719 showAllApps(false);
720 }
721 mOnResumeState = State.NONE;
722
Winson Chungde0fb8f2012-05-08 14:37:08 -0700723 // Process any items that were added while Launcher was away
724 InstallShortcutReceiver.flushInstallQueue(this);
725
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800726 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700727 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700728 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400729 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700730 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700732 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 }
Winson Chunge4e50662012-01-23 14:45:13 -0800734
735 // Reset the pressed state of icons that were locked in the press state while activities
736 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800737 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800738 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800739 mWaitingForResume.setStayPressed(false);
740 }
Winson Chunge4e50662012-01-23 14:45:13 -0800741 if (mAppsCustomizeContent != null) {
742 // Resets the previous all apps icon press state
743 mAppsCustomizeContent.resetDrawableState();
744 }
Adam Cohen06dff352012-06-01 17:17:08 -0700745 // It is possible that widgets can receive updates while launcher is not in the foreground.
746 // Consequently, the widgets will be inflated in the orientation of the foreground activity
747 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
748 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700749 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700750
751 // Again, as with the above scenario, it's possible that one or more of the global icons
752 // were updated in the wrong orientation.
753 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700757 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700758 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
759 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
760 // when Launcher resumes and we are still in AllApps.
761 updateWallpaperVisibility(true);
762
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700763 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700764 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800765 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700766 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 }
Romain Guycbb89e42009-06-08 15:52:54 -0700768
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700769 @Override
770 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 // Flag the loader to stop early before switching
772 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700773 if (mAppsCustomizeContent != null) {
774 mAppsCustomizeContent.surrender();
775 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800776 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700777 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700778
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800779 // We can't hide the IME if it was forced open. So don't bother
780 /*
781 @Override
782 public void onWindowFocusChanged(boolean hasFocus) {
783 super.onWindowFocusChanged(hasFocus);
784
785 if (hasFocus) {
786 final InputMethodManager inputManager = (InputMethodManager)
787 getSystemService(Context.INPUT_METHOD_SERVICE);
788 WindowManager.LayoutParams lp = getWindow().getAttributes();
789 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
790 android.os.Handler()) {
791 protected void onReceiveResult(int resultCode, Bundle resultData) {
792 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
793 }
794 });
795 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
796 }
797 }
798 */
799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 private boolean acceptFilter() {
801 final InputMethodManager inputManager = (InputMethodManager)
802 getSystemService(Context.INPUT_METHOD_SERVICE);
803 return !inputManager.isFullscreenMode();
804 }
805
806 @Override
807 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700808 final int uniChar = event.getUnicodeChar();
809 final boolean handled = super.onKeyDown(keyCode, event);
810 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
811 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
813 keyCode, event);
814 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700815 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700816 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700817 // showSearchDialog()
818 // If there are multiple keystrokes before the search dialog takes focus,
819 // onSearchRequested() will be called for every keystroke,
820 // but it is idempotent, so it's fine.
821 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
823 }
824
Joe Onorato8a9625e2010-01-28 15:55:35 -0800825 // Eat the long press event so the keyboard doesn't come up.
826 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
827 return true;
828 }
829
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 return handled;
831 }
832
Karl Rosaen138a0412009-04-23 19:00:21 -0700833 private String getTypedText() {
834 return mDefaultKeySsb.toString();
835 }
836
837 private void clearTypedText() {
838 mDefaultKeySsb.clear();
839 mDefaultKeySsb.clearSpans();
840 Selection.setSelection(mDefaultKeySsb, 0);
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700844 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
845 * State
846 */
847 private static State intToState(int stateOrdinal) {
848 State state = State.WORKSPACE;
849 final State[] stateValues = State.values();
850 for (int i = 0; i < stateValues.length; i++) {
851 if (stateValues[i].ordinal() == stateOrdinal) {
852 state = stateValues[i];
853 break;
854 }
855 }
856 return state;
857 }
858
859 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 * Restores the previous state, if it exists.
861 *
862 * @param savedState The previous state.
863 */
864 private void restoreState(Bundle savedState) {
865 if (savedState == null) {
866 return;
867 }
868
Michael Jurka883f55b2010-10-21 15:47:14 -0700869 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700870 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700871 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800872 }
873
Winson Chungf0c6ae02012-03-21 16:10:31 -0700874 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700876 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
878
Winson Chung3d503fb2011-07-13 17:25:49 -0700879 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
880 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700881
Winson Chung3d503fb2011-07-13 17:25:49 -0700882 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
883 mPendingAddInfo.container = pendingAddContainer;
884 mPendingAddInfo.screen = pendingAddScreen;
885 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
886 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700887 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
888 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
889 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700890 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 mRestoring = true;
892 }
893
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
896 if (renameFolder) {
897 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700898 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 mRestoring = true;
900 }
Winson Chunga12a2502010-12-20 14:41:35 -0800901
Winson Chung785d2eb2011-04-14 16:08:02 -0700902
903 // Restore the AppsCustomize tab
904 if (mAppsCustomizeTabHost != null) {
905 String curTab = savedState.getString("apps_customize_currentTab");
906 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700907 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700908 mAppsCustomizeContent.setContentType(
909 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
910 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700911 mAppsCustomizeContent.loadAssociatedPages(
912 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700913 }
914
Winson Chung5afbf7b2011-07-25 11:53:08 -0700915 int currentIndex = savedState.getInt("apps_customize_currentIndex");
916 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919
920 /**
921 * Finds all the views we need and configure them properly.
922 */
923 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700924 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400925
Winson Chung4c98d922011-05-31 16:50:48 -0700926 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
927 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800928 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
929 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930
Winson Chung4c98d922011-05-31 16:50:48 -0700931 // Setup the drag layer
932 mDragLayer.setup(this, dragController);
933
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 // Setup the hotseat
935 mHotseat = (Hotseat) findViewById(R.id.hotseat);
936 if (mHotseat != null) {
937 mHotseat.setup(this);
938 }
939
Winson Chung4c98d922011-05-31 16:50:48 -0700940 // Setup the workspace
941 mWorkspace.setHapticFeedbackEnabled(false);
942 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700943 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700944 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700945
Winson Chungf0ea4d32011-06-06 14:27:16 -0700946 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700947 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700948
Winson Chungf0ea4d32011-06-06 14:27:16 -0700949 // Setup AppsCustomize
950 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
951 findViewById(R.id.apps_customize_pane);
952 mAppsCustomizeContent = (AppsCustomizePagedView)
953 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
954 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400955
Michael Jurka5130e402011-10-13 04:55:35 -0700956 // Get the all apps button
957 mAllAppsButton = findViewById(R.id.all_apps_button);
958 if (mAllAppsButton != null) {
959 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
960 @Override
961 public boolean onTouch(View v, MotionEvent event) {
962 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
963 onTouchDownAllAppsButton(v);
964 }
965 return false;
966 }
967 });
968 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700970 dragController.setDragScoller(mWorkspace);
971 dragController.setScrollView(mDragLayer);
972 dragController.setMoveTarget(mWorkspace);
973 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700974 if (mSearchDropTargetBar != null) {
975 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Creates a view representing a shortcut.
981 *
982 * @param info The data structure describing the shortcut.
983 *
984 * @return A View inflated from R.layout.application.
985 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800986 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700988 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990
991 /**
992 * Creates a view representing a shortcut inflated from the specified resource.
993 *
994 * @param layoutResId The id of the XML layout used to create the shortcut.
995 * @param parent The group the shortcut belongs to.
996 * @param info The data structure describing the shortcut.
997 *
998 * @return A View inflated from layoutResId.
999 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001001 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1002 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 return favorite;
1005 }
1006
1007 /**
1008 * Add an application shortcut to the workspace.
1009 *
1010 * @param data The intent describing the application.
1011 * @param cellInfo The position on screen where to create the shortcut.
1012 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016
Adam Cohenfbba09b2011-07-18 21:43:05 -07001017 // First we check if we already know the exact location where we want to add this item.
1018 if (cellX >= 0 && cellY >= 0) {
1019 cellXY[0] = cellX;
1020 cellXY[1] = cellY;
1021 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001022 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001023 return;
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001026 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027
Romain Guy73b979d2009-06-09 12:57:21 -07001028 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001029 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001031 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001033 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 } else {
1035 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037 }
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
1040 * Add a shortcut to the workspace.
1041 *
1042 * @param data The intent describing the shortcut.
1043 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1046 int cellY) {
1047 int[] cellXY = mTmpAddItemCellCoordinates;
1048 int[] touchXY = mPendingAddInfo.dropPos;
1049 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Michael Jurkad3ef3062010-11-23 16:23:58 -08001051 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001052
Adam Cohen558baaf2011-08-15 15:22:57 -07001053 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001054 if (info == null) {
1055 return;
1056 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001057 final View view = createShortcut(info);
1058
Adam Cohenfbba09b2011-07-18 21:43:05 -07001059 // First we check if we already know the exact location where we want to add this item.
1060 if (cellX >= 0 && cellY >= 0) {
1061 cellXY[0] = cellX;
1062 cellXY[1] = cellY;
1063 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001064
1065 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001066 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001067 true, null,null)) {
1068 return;
1069 }
1070 DragObject dragObject = new DragObject();
1071 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001072 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1073 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001074 return;
1075 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001077 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001078 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001079 foundCellSpan = (result != null);
1080 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001081 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001082 }
1083
1084 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001085 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 return;
1087 }
1088
Adam Cohen558baaf2011-08-15 15:22:57 -07001089 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001092 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1093 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095 }
1096
Adam Cohen2f093b62012-04-30 18:59:53 -07001097 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1098 int minHeight) {
1099 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001100 // We want to account for the extra amount of padding that we are adding to the widget
1101 // to ensure that it gets the full amount of space that it has requested
1102 int requiredWidth = minWidth + padding.left + padding.right;
1103 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001105 }
1106
Adam Cohen2f093b62012-04-30 18:59:53 -07001107 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1108 return getSpanForWidget(context, info.provider, 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, AppWidgetProviderInfo info) {
1112 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001113 }
1114
Adam Cohen2f093b62012-04-30 18:59:53 -07001115 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1116 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001117 }
1118
Adam Cohen2f093b62012-04-30 18:59:53 -07001119 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1120 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001121 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001122 }
1123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001127 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001128 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001130 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1131 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1132 if (appWidgetInfo == null) {
1133 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1134 }
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001136 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001138
Adam Cohen2f093b62012-04-30 18:59:53 -07001139 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1140 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001143 // We have saved the position to which the widget was dragged-- this really only matters
1144 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001145 int[] cellXY = mTmpAddItemCellCoordinates;
1146 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001149 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1150 cellXY[0] = mPendingAddInfo.cellX;
1151 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 spanXY[0] = mPendingAddInfo.spanX;
1153 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001154 foundCellSpan = true;
1155 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001157 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001158 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1159 spanXY[1], cellXY, finalSpan);
1160 spanXY[0] = finalSpan[0];
1161 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001165 }
1166
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001168 if (appWidgetId != -1) {
1169 // Deleting an app widget ID is a void call but writes to disk before returning
1170 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001171 new Thread("deleteAppWidgetId") {
1172 public void run() {
1173 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1174 }
1175 }.start();
1176 }
Winson Chung93eef082012-03-23 15:59:27 -07001177 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001178 return;
1179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001182 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1183 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 launcherInfo.spanX = spanXY[0];
1185 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001186 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1187 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
1192 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001193 if (hostView == null) {
1194 // Perform actual inflation because we're live
1195 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1196 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1197 } else {
1198 // The AppWidgetHostView has already been inflated and instantiated
1199 launcherInfo.hostView = hostView;
1200 }
Romain Guycbb89e42009-06-08 15:52:54 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001204 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001205 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001207
1208 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001210 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
Romain Guycbb89e42009-06-08 15:52:54 -07001212
Adam Cohended9f8d2010-11-03 13:25:16 -07001213 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1214 @Override
1215 public void onReceive(Context context, Intent intent) {
1216 final String action = intent.getAction();
1217 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1218 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001219 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001220 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001221
Winson Chungc100e8e2011-08-09 16:02:43 -07001222 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001223 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001224 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1225 mAppsCustomizeTabHost.reset();
1226 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001227 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001228 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1229 mUserPresent = true;
1230 updateRunning();
1231 }
1232 }
1233 };
1234
1235 @Override
1236 public void onAttachedToWindow() {
1237 super.onAttachedToWindow();
1238
1239 // Listen for broadcasts related to user-presence
1240 final IntentFilter filter = new IntentFilter();
1241 filter.addAction(Intent.ACTION_SCREEN_OFF);
1242 filter.addAction(Intent.ACTION_USER_PRESENT);
1243 registerReceiver(mReceiver, filter);
1244
Adam Cohend113e0c2010-11-11 10:48:05 -08001245 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 mVisible = true;
1247 }
1248
1249 @Override
1250 public void onDetachedFromWindow() {
1251 super.onDetachedFromWindow();
1252 mVisible = false;
1253
Adam Cohend113e0c2010-11-11 10:48:05 -08001254 if (mAttached) {
1255 unregisterReceiver(mReceiver);
1256 mAttached = false;
1257 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001258 updateRunning();
1259 }
1260
1261 public void onWindowVisibilityChanged(int visibility) {
1262 mVisible = visibility == View.VISIBLE;
1263 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001264 // The following code used to be in onResume, but it turns out onResume is called when
1265 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1266 // is a more appropriate event to handle
1267 if (mVisible) {
1268 mAppsCustomizeTabHost.onWindowVisible();
1269 if (!mWorkspaceLoading) {
1270 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001271 // We want to let Launcher draw itself at least once before we force it to build
1272 // layers on all the workspace pages, so that transitioning to Launcher from other
1273 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1274 // a true draw so we wait until the second preDraw call to be safe
1275 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001276 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001277 // We delay the layer building a bit in order to give
1278 // other message processing a time to run. In particular
1279 // this avoids a delay in hiding the IME if it was
1280 // currently shown, because doing that may involve
1281 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001282 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001283
Michael Jurka6ee21d22012-02-21 18:20:27 -08001284 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001285 return true;
1286 }
1287 });
1288 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001289 // When Launcher comes back to foreground, a different Activity might be responsible for
1290 // the app market intent, so refresh the icon
1291 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001292 clearTypedText();
1293 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001294 }
1295
1296 private void sendAdvanceMessage(long delay) {
1297 mHandler.removeMessages(ADVANCE_MSG);
1298 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1299 mHandler.sendMessageDelayed(msg, delay);
1300 mAutoAdvanceSentTime = System.currentTimeMillis();
1301 }
1302
1303 private void updateRunning() {
1304 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1305 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1306 mAutoAdvanceRunning = autoAdvanceRunning;
1307 if (autoAdvanceRunning) {
1308 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1309 sendAdvanceMessage(delay);
1310 } else {
1311 if (!mWidgetsToAdvance.isEmpty()) {
1312 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1313 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1314 }
1315 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001316 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 }
1318 }
1319 }
1320
1321 private final Handler mHandler = new Handler() {
1322 @Override
1323 public void handleMessage(Message msg) {
1324 if (msg.what == ADVANCE_MSG) {
1325 int i = 0;
1326 for (View key: mWidgetsToAdvance.keySet()) {
1327 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1328 final int delay = mAdvanceStagger * i;
1329 if (v instanceof Advanceable) {
1330 postDelayed(new Runnable() {
1331 public void run() {
1332 ((Advanceable) v).advance();
1333 }
1334 }, delay);
1335 }
1336 i++;
1337 }
1338 sendAdvanceMessage(mAdvanceInterval);
1339 }
1340 }
1341 };
1342
1343 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001344 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001345 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1346 if (v instanceof Advanceable) {
1347 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001348 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001349 updateRunning();
1350 }
1351 }
1352
1353 void removeWidgetToAutoAdvance(View hostView) {
1354 if (mWidgetsToAdvance.containsKey(hostView)) {
1355 mWidgetsToAdvance.remove(hostView);
1356 updateRunning();
1357 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 }
1359
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 launcherInfo.hostView = null;
1363 }
1364
Winson Chung93eef082012-03-23 15:59:27 -07001365 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1366 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1367 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001368 }
1369
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 public LauncherAppWidgetHost getAppWidgetHost() {
1371 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
Romain Guycbb89e42009-06-08 15:52:54 -07001373
Winson Chunga9abd0e2010-10-27 17:18:37 -07001374 public LauncherModel getModel() {
1375 return mModel;
1376 }
1377
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001379 getWindow().closeAllPanels();
1380
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001381 // Whatever we were doing is hereby canceled.
1382 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
1386 protected void onNewIntent(Intent intent) {
1387 super.onNewIntent(intent);
1388
1389 // Close the menu
1390 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001391 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001392 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001393
Joe Onorato14f122b2009-11-19 14:06:36 -08001394 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1395 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001396
Adam Cohenac56cff2011-09-28 20:45:37 -07001397 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001398 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001399 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001400 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1401 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001402 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001403 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001404
1405 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001406 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001407
1408 // If we are already on home, then just animate back to the workspace, otherwise, just
1409 // wait until onResume to set the state back to Workspace
1410 if (alreadyOnHome) {
1411 showWorkspace(true);
1412 } else {
1413 mOnResumeState = State.WORKSPACE;
1414 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001415
Joe Onorato3a8820b2009-11-10 15:06:42 -08001416 final View v = getWindow().peekDecorView();
1417 if (v != null && v.getWindowToken() != null) {
1418 InputMethodManager imm = (InputMethodManager)getSystemService(
1419 INPUT_METHOD_SERVICE);
1420 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001422
Michael Jurka35aa14d2011-07-07 17:01:08 -07001423 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001424 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001425 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428 }
1429
1430 @Override
1431 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1432 // Do not call super here
1433 mSavedInstanceState = savedInstanceState;
1434 }
1435
1436 @Override
1437 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001438 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001439 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
Michael Jurka883f55b2010-10-21 15:47:14 -07001441 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001442 // We close any open folder since it will not be re-opened, and we need to make sure
1443 // this state is reflected.
1444 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1447 mWaitingForResult) {
1448 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1449 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1450 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1451 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001452 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1453 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1454 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456
1457 if (mFolderInfo != null && mWaitingForResult) {
1458 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1459 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1460 }
Michael Jurka946ad472010-07-09 18:05:18 -07001461
Winson Chung785d2eb2011-04-14 16:08:02 -07001462 // Save the current AppsCustomize tab
1463 if (mAppsCustomizeTabHost != null) {
1464 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1465 if (currentTabTag != null) {
1466 outState.putString("apps_customize_currentTab", currentTabTag);
1467 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001468 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1469 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 @Override
1474 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001476
Winson Chunge7a03942011-08-05 15:05:12 -07001477 // Remove all pending runnables
1478 mHandler.removeMessages(ADVANCE_MSG);
1479 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001480 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001481
Winson Chungcd2b0142011-06-08 16:02:26 -07001482 // Stop callbacks from LauncherModel
1483 LauncherApplication app = ((LauncherApplication) getApplication());
1484 mModel.stopLoader();
1485 app.setLauncher(null);
1486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001488 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001490 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001492 mAppWidgetHost = null;
1493
1494 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
1496 TextKeyListener.getInstance().release();
1497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001500
1501 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001502 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001503
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001504 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001505 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1506 mWorkspace.removeAllViews();
1507 mWorkspace = null;
1508 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001509
Michael Jurka2ecf9952012-06-18 12:52:28 -07001510 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512
Adam Cohena9cf38f2011-05-02 15:36:58 -07001513 public DragController getDragController() {
1514 return mDragController;
1515 }
1516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 @Override
1518 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001519 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 super.startActivityForResult(intent, requestCode);
1521 }
1522
Winson Chung70d72102011-08-12 11:24:35 -07001523 /**
1524 * Indicates that we want global search for this activity by setting the globalSearch
1525 * argument for {@link #startSearch} to true.
1526 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001528 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001530
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001531 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 if (initialQuery == null) {
1534 // Use any text typed in the launcher as the initial query
1535 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 if (appSearchData == null) {
1538 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001539 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001541 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001542
Michael Jurkaa33411c2012-06-14 16:18:21 -07001543 startGlobalSearch(initialQuery, selectInitialQuery,
1544 appSearchData, sourceBounds);
1545 }
1546
1547 /**
1548 * Starts the global search activity. This code is a copied from SearchManager
1549 */
1550 public void startGlobalSearch(String initialQuery,
1551 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001552 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001553 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1554 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1555 if (globalSearchActivity == null) {
1556 Log.w(TAG, "No global search activity found.");
1557 return;
1558 }
1559 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1560 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1561 intent.setComponent(globalSearchActivity);
1562 // Make sure that we have a Bundle to put source in
1563 if (appSearchData == null) {
1564 appSearchData = new Bundle();
1565 } else {
1566 appSearchData = new Bundle(appSearchData);
1567 }
1568 // Set source to package name of app that starts global search, if not set already.
1569 if (!appSearchData.containsKey("source")) {
1570 appSearchData.putString("source", getPackageName());
1571 }
1572 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1573 if (!TextUtils.isEmpty(initialQuery)) {
1574 intent.putExtra(SearchManager.QUERY, initialQuery);
1575 }
1576 if (selectInitialQuery) {
1577 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1578 }
1579 intent.setSourceBounds(sourceBounds);
1580 try {
1581 startActivity(intent);
1582 } catch (ActivityNotFoundException ex) {
1583 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
Winson Chung70d72102011-08-12 11:24:35 -07001587 @Override
1588 public boolean onCreateOptionsMenu(Menu menu) {
1589 if (isWorkspaceLocked()) {
1590 return false;
1591 }
1592
1593 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001594
1595 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1596 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1597 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001598 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1599 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1600 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001601 String helpUrl = getString(R.string.help_url);
1602 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001603 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1604 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1605
Winson Chung70d72102011-08-12 11:24:35 -07001606 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1607 .setIcon(android.R.drawable.ic_menu_gallery)
1608 .setAlphabeticShortcut('W');
1609 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1610 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001611 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001612 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001613 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1614 .setIcon(android.R.drawable.ic_menu_preferences)
1615 .setIntent(settings)
1616 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001617 if (!helpUrl.isEmpty()) {
1618 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1619 .setIcon(android.R.drawable.ic_menu_help)
1620 .setIntent(help)
1621 .setAlphabeticShortcut('H');
1622 }
Winson Chung70d72102011-08-12 11:24:35 -07001623 return true;
1624 }
1625
1626 @Override
1627 public boolean onPrepareOptionsMenu(Menu menu) {
1628 super.onPrepareOptionsMenu(menu);
1629
1630 if (mAppsCustomizeTabHost.isTransitioning()) {
1631 return false;
1632 }
1633 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1634 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1635
1636 return true;
1637 }
1638
1639 @Override
1640 public boolean onOptionsItemSelected(MenuItem item) {
1641 switch (item.getItemId()) {
1642 case MENU_WALLPAPER_SETTINGS:
1643 startWallpaper();
1644 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001645 }
1646
1647 return super.onOptionsItemSelected(item);
1648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001650 @Override
1651 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001652 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001653 // Use a custom animation for launching search
1654 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
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();
Michael Jurka8b805b12012-04-18 14:23:14 -07001750 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001751 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001752 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001753 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001754 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1755 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1756 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1757 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1758 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001759 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001760 }
1761
Joe Onoratodeb98af2010-02-19 14:59:39 -08001762 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001763 // Handle case where user selected "Applications"
1764 String applicationName = getResources().getString(R.string.group_applications);
1765 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001766
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001767 if (applicationName != null && applicationName.equals(shortcutName)) {
1768 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1769 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001770
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1772 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001773 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001774 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001776 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779
Winson Chung24ab2f12010-09-16 14:10:47 -07001780 void processWallpaper(Intent intent) {
1781 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1782 }
1783
Winson Chung3d503fb2011-07-13 17:25:49 -07001784 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1785 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001786 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 folderInfo.title = getText(R.string.folder_name);
1788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001790 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1791 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001792 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793
1794 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001795 FolderIcon newFolder =
1796 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1797 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1798 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001799 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Romain Guycbb89e42009-06-08 15:52:54 -07001801
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001803 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 }
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001807 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001809 Intent chooser = Intent.createChooser(pickWallpaper,
1810 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001811 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1812 // Removed in Eclair MR1
1813// WallpaperManager wm = (WallpaperManager)
1814// getSystemService(Context.WALLPAPER_SERVICE);
1815// WallpaperInfo wi = wm.getWallpaperInfo();
1816// if (wi != null && wi.getSettingsActivity() != null) {
1817// LabeledIntent li = new LabeledIntent(getPackageName(),
1818// R.string.configure_wallpaper, 0);
1819// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1820// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1821// }
Mike Clerona0618e42009-10-22 13:55:21 -07001822 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001825 /**
1826 * Registers various content observers. The current implementation registers
1827 * only a favorites observer to keep track of the favorites applications.
1828 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001829 private void registerContentObservers() {
1830 ContentResolver resolver = getContentResolver();
1831 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1832 true, mWidgetObserver);
1833 }
1834
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 @Override
1836 public boolean dispatchKeyEvent(KeyEvent event) {
1837 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1838 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001840 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001841 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001842 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001843 dumpState();
1844 return true;
1845 }
1846 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001847 }
1848 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1849 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001850 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 return true;
1852 }
1853 }
1854
1855 return super.dispatchKeyEvent(event);
1856 }
1857
Joe Onorato88ec0992009-11-19 13:16:06 -08001858 @Override
1859 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001860 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001861 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001862 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001863 Folder openFolder = mWorkspace.getOpenFolder();
1864 if (openFolder.isEditingName()) {
1865 openFolder.dismissEditingName();
1866 } else {
1867 closeFolder();
1868 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001869 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001870 mWorkspace.exitWidgetResizeMode();
1871
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001872 // Back button is a no-op here, but give at least some feedback for the button press
1873 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001874 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001878 * Re-listen when widgets are reset.
1879 */
1880 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001881 if (mAppWidgetHost != null) {
1882 mAppWidgetHost.startListening();
1883 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001884 }
1885
1886 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1888 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001890 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001891 if (mModel != null) {
1892 mModel.unbindWorkspaceItems();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 /**
1897 * Launches the intent referred by the clicked shortcut.
1898 *
1899 * @param v The view representing the clicked shortcut.
1900 */
1901 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001902 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1903 // view has detached (it's possible for this to happen if the view is removed mid touch).
1904 if (v.getWindowToken() == null) {
1905 return;
1906 }
1907
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001908 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001909 return;
1910 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001913 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001915 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001916 int[] pos = new int[2];
1917 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001918 intent.setSourceBounds(new Rect(pos[0], pos[1],
1919 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001920
1921 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001922
1923 if (success && v instanceof BubbleTextView) {
1924 mWaitingForResume = (BubbleTextView) v;
1925 mWaitingForResume.setStayPressed(true);
1926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001928 if (v instanceof FolderIcon) {
1929 FolderIcon fi = (FolderIcon) v;
1930 handleFolderClick(fi);
1931 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001932 } else if (v == mAllAppsButton) {
1933 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001934 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001935 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001936 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
1939 }
1940
Michael Jurka0e260592010-06-30 17:07:39 -07001941 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001942 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001943 // clicking anywhere on the workspace causes the customization drawer to slide down
1944 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001945 return false;
1946 }
1947
Michael Jurkaaf442092010-06-10 17:01:57 -07001948 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001949 * Event handler for the search button
1950 *
1951 * @param v The view that was clicked.
1952 */
1953 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001954 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1955
Amith Yamasania135ba82011-08-09 17:42:01 -07001956 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001957 }
1958
1959 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001960 * Event handler for the voice button
1961 *
1962 * @param v The view that was clicked.
1963 */
1964 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001965 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001966
Michael Jurkaae65ee32012-04-30 11:45:54 -07001967 try {
1968 final SearchManager searchManager =
1969 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1970 ComponentName activityName = searchManager.getGlobalSearchActivity();
1971 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001972 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001973 if (activityName != null) {
1974 intent.setPackage(activityName.getPackageName());
1975 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001976 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001977 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001978 } catch (ActivityNotFoundException e) {
1979 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001980 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001981 startActivitySafely(null, intent, "onClickVoiceButton");
1982 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001983 }
1984
1985 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001986 * Event handler for the "grid" button that appears on the home screen, which
1987 * enters all apps mode.
1988 *
1989 * @param v The view that was clicked.
1990 */
1991 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001992 showAllApps(true);
1993 }
1994
1995 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001996 // Provide the same haptic feedback that the system offers for virtual keys.
1997 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001998 }
1999
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002000 public void onClickAppMarketButton(View v) {
2001 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002002 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002003 } else {
2004 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002005 }
2006 }
2007
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002008 void startApplicationDetailsActivity(ComponentName componentName) {
2009 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002010 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2011 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002012 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002013 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002014 }
2015
Patrick Dubroy5539af72010-09-07 15:22:01 -07002016 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2017 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2018 // System applications cannot be installed. For now, show a toast explaining that.
2019 // We may give them the option of disabling apps this way.
2020 int messageId = R.string.uninstall_system_app_text;
2021 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2022 } else {
2023 String packageName = appInfo.componentName.getPackageName();
2024 String className = appInfo.componentName.getClassName();
2025 Intent intent = new Intent(
2026 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002027 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2028 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002029 startActivity(intent);
2030 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002031 }
2032
Michael Jurka86a720e2012-05-09 11:23:23 -07002033 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002035
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002037 // Only launch using the new animation if the shortcut has not opted out (this is a
2038 // private contract between launcher and may be ignored in the future).
2039 boolean useLaunchAnimation = (v != null) &&
2040 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2041 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002042 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2043 v.getMeasuredWidth(), v.getMeasuredHeight());
2044
2045 startActivity(intent, opts.toBundle());
2046 } else {
2047 startActivity(intent);
2048 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002049 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 } catch (SecurityException e) {
2051 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002052 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002054 "or use the exported attribute for this activity. "
2055 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002057 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002059
Michael Jurka86a720e2012-05-09 11:23:23 -07002060 boolean startActivitySafely(View v, Intent intent, Object tag) {
2061 boolean success = false;
2062 try {
2063 success = startActivity(v, intent, tag);
2064 } catch (ActivityNotFoundException e) {
2065 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2066 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2067 }
2068 return success;
2069 }
2070
Romain Guy8e633c52010-03-04 12:51:36 -08002071 void startActivityForResultSafely(Intent intent, int requestCode) {
2072 try {
2073 startActivityForResult(intent, requestCode);
2074 } catch (ActivityNotFoundException e) {
2075 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2076 } catch (SecurityException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2079 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2080 "or use the exported attribute for this activity.", e);
2081 }
2082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083
Adam Cohena9cf38f2011-05-02 15:36:58 -07002084 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002085 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002086 Folder openFolder = mWorkspace.getFolderForTag(info);
2087
2088 // If the folder info reports that the associated folder is open, then verify that
2089 // it is actually opened. There have been a few instances where this gets out of sync.
2090 if (info.opened && openFolder == null) {
2091 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2092 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2093 info.opened = false;
2094 }
2095
Adam Cohenfb91f302012-06-11 15:45:18 -07002096 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 // Close any open folder
2098 closeFolder();
2099 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002100 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } else {
2102 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 int folderScreen;
2104 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002105 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 // .. and close it
2107 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002108 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // Close any folder open on the current screen
2110 closeFolder();
2111 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002112 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114 }
2115 }
2116 }
2117
Adam Cohen268c4752012-06-06 17:47:33 -07002118 /**
2119 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2120 * in the DragLayer in the exact absolute location of the original FolderIcon.
2121 */
2122 private void copyFolderIconToImage(FolderIcon fi) {
2123 final int width = fi.getMeasuredWidth();
2124 final int height = fi.getMeasuredHeight();
2125
2126 // Lazy load ImageView, Bitmap and Canvas
2127 if (mFolderIconImageView == null) {
2128 mFolderIconImageView = new ImageView(this);
2129 }
2130 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2131 mFolderIconBitmap.getHeight() != height) {
2132 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2133 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2134 }
2135
2136 DragLayer.LayoutParams lp;
2137 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2138 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2139 } else {
2140 lp = new DragLayer.LayoutParams(width, height);
2141 }
2142
2143 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2144 lp.customPosition = true;
2145 lp.x = mRectForFolderAnimation.left;
2146 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002147 lp.width = width;
2148 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002149
2150 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2151 fi.draw(mFolderIconCanvas);
2152 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002153 if (fi.getFolder() != null) {
2154 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2155 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002156 }
Adam Cohen268c4752012-06-06 17:47:33 -07002157 // Just in case this image view is still in the drag layer from a previous animation,
2158 // we remove it and re-add it.
2159 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2160 mDragLayer.removeView(mFolderIconImageView);
2161 }
2162 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002163 if (fi.getFolder() != null) {
2164 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002165 }
Adam Cohen268c4752012-06-06 17:47:33 -07002166 }
2167
Adam Cohen2801caf2011-05-13 20:57:39 -07002168 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002169 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002170 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2171 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2172 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2173
Adam Cohenc51934b2011-07-26 21:07:43 -07002174 FolderInfo info = (FolderInfo) fi.getTag();
2175 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2176 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002177 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2178 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002179 }
2180
Adam Cohen268c4752012-06-06 17:47:33 -07002181 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2182 copyFolderIconToImage(fi);
2183 fi.setVisibility(View.INVISIBLE);
2184
Michael Jurka2ecf9952012-06-18 12:52:28 -07002185 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002186 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002187 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2188 oa.start();
2189 }
2190
Adam Cohen268c4752012-06-06 17:47:33 -07002191 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002192 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002193 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2194 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2195 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2196
Adam Cohen268c4752012-06-06 17:47:33 -07002197 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002198
Adam Cohen268c4752012-06-06 17:47:33 -07002199 // We remove and re-draw the FolderIcon in-case it has changed
2200 mDragLayer.removeView(mFolderIconImageView);
2201 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002202 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002203 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002204 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002205 oa.addListener(new AnimatorListenerAdapter() {
2206 @Override
2207 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002208 if (cl != null) {
2209 cl.clearFolderLeaveBehind();
2210 // Remove the ImageView copy of the FolderIcon and make the original visible.
2211 mDragLayer.removeView(mFolderIconImageView);
2212 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002213 }
2214 }
2215 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002216 oa.start();
2217 }
2218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002220 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 * is animated relative to the specified View. If the View is null, no animation
2222 * is played.
2223 *
2224 * @param folderInfo The FolderInfo describing the folder to open.
2225 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002226 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002227 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002228 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231
Adam Cohen4554ee12011-08-03 16:13:21 -07002232 // Just verify that the folder hasn't already been added to the DragLayer.
2233 // There was a one-off crash where the folder had a parent already.
2234 if (folder.getParent() == null) {
2235 mDragLayer.addView(folder);
2236 mDragController.addDropTarget((DropTarget) folder);
2237 } else {
2238 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2239 folder.getParent() + ").");
2240 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002241 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002242 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002243 }
2244
2245 public void closeFolder() {
2246 Folder folder = mWorkspace.getOpenFolder();
2247 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002248 if (folder.isEditingName()) {
2249 folder.dismissEditingName();
2250 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002251 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002252
2253 // Dismiss the folder cling
2254 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002255 }
2256 }
2257
2258 void closeFolder(Folder folder) {
2259 folder.getInfo().opened = false;
2260
2261 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2262 if (parent != null) {
2263 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2264 shrinkAndFadeInFolderIcon(fi);
2265 }
2266 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002270 if (!isDraggingEnabled()) return false;
2271 if (isWorkspaceLocked()) return false;
2272 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273
2274 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002275 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 }
2277
Michael Jurka0280c3b2010-09-17 15:00:07 -07002278 resetAddInfo();
2279 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002281 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 return true;
2283 }
2284
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 // The hotseat touch handling does not go through Workspace, and we always allow long press
2286 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002287 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2289 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002290 if (itemUnderLongClick == null) {
2291 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2293 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002294 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002296 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002298 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 }
2300 }
2301 }
2302 return true;
2303 }
2304
Winson Chung3d503fb2011-07-13 17:25:49 -07002305 boolean isHotseatLayout(View layout) {
2306 return mHotseat != null && layout != null &&
2307 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2308 }
2309 Hotseat getHotseat() {
2310 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002311 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002312 SearchDropTargetBar getSearchBar() {
2313 return mSearchDropTargetBar;
2314 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002315
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 /**
2317 * Returns the CellLayout of the specified container at the specified screen.
2318 */
2319 CellLayout getCellLayout(long container, int screen) {
2320 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2321 if (mHotseat != null) {
2322 return mHotseat.getLayout();
2323 } else {
2324 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002325 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 } else {
2327 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002328 }
2329 }
2330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 Workspace getWorkspace() {
2332 return mWorkspace;
2333 }
2334
Daniel Sandler843e8602010-06-07 14:59:01 -04002335 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2336 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002337 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002338 }
2339
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002340 public boolean isAllAppsButtonRank(int rank) {
2341 return mHotseat.isAllAppsButtonRank(rank);
2342 }
2343
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002344 /**
2345 * Helper method for the cameraZoomIn/cameraZoomOut animations
2346 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002347 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002348 * @param scaleFactor The scale factor used for the zoom
2349 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002350 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002352 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002354
Winson Chung18f41f82012-05-09 13:28:10 -07002355 void disableWallpaperIfInAllApps() {
2356 // Only disable it if we are in all apps
2357 if (mState == State.APPS_CUSTOMIZE) {
2358 if (mAppsCustomizeTabHost != null &&
2359 !mAppsCustomizeTabHost.isTransitioning()) {
2360 updateWallpaperVisibility(false);
2361 }
2362 }
2363 }
2364
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002365 void updateWallpaperVisibility(boolean visible) {
2366 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2367 int curflags = getWindow().getAttributes().flags
2368 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2369 if (wpflags != curflags) {
2370 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2371 }
2372 }
2373
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002374 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2375 if (v instanceof LauncherTransitionable) {
2376 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2377 }
2378 }
2379
Michael Jurkabed61d22012-02-14 22:51:29 -08002380 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2381 if (v instanceof LauncherTransitionable) {
2382 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2383 }
Winson Chung70442722012-02-10 15:43:22 -08002384
2385 // Update the workspace transition step as well
2386 dispatchOnLauncherTransitionStep(v, 0f);
2387 }
2388
2389 private void dispatchOnLauncherTransitionStep(View v, float t) {
2390 if (v instanceof LauncherTransitionable) {
2391 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2392 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002393 }
2394
2395 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2396 if (v instanceof LauncherTransitionable) {
2397 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2398 }
Winson Chung70442722012-02-10 15:43:22 -08002399
2400 // Update the workspace transition step as well
2401 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002402 }
2403
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002404 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002405 * Things to test when changing the following seven functions.
2406 * - Home from workspace
2407 * - from center screen
2408 * - from other screens
2409 * - Home from all apps
2410 * - from center screen
2411 * - from other screens
2412 * - Back from all apps
2413 * - from center screen
2414 * - from other screens
2415 * - Launch app from workspace and quit
2416 * - with back
2417 * - with home
2418 * - Launch app from all apps and quit
2419 * - with back
2420 * - with home
2421 * - Go to a screen that's not the default, then all
2422 * apps, and launch and app, and go back
2423 * - with back
2424 * -with home
2425 * - On workspace, long press power and go back
2426 * - with back
2427 * - with home
2428 * - On all apps, long press power and go back
2429 * - with back
2430 * - with home
2431 * - On workspace, power off
2432 * - On all apps, power off
2433 * - Launch an app and turn off the screen while in that app
2434 * - Go back with home key
2435 * - Go back with back key TODO: make this not go to workspace
2436 * - From all apps
2437 * - From workspace
2438 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2439 * - From all apps
2440 * - From the center workspace
2441 * - From another workspace
2442 */
2443
2444 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 * Zoom the camera out from the workspace to reveal 'toView'.
2446 * Assumes that the view to show is anchored at either the very top or very bottom
2447 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002449 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002450 if (mStateAnimation != null) {
2451 mStateAnimation.cancel();
2452 mStateAnimation = null;
2453 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002455
Winson Chungf0ea4d32011-06-06 14:27:16 -07002456 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2457 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2458 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002459 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002460 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002461 final int startDelay =
2462 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002463
Michael Jurkab3e22d92011-10-31 15:58:33 -07002464 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002465
Michael Jurkad74c9842011-07-10 12:44:21 -07002466 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002467 Animator workspaceAnim =
2468 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469
2470 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002471 toView.setScaleX(scale);
2472 toView.setScaleY(scale);
2473 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2474 scaleAnim.
2475 scaleX(1f).scaleY(1f).
2476 setDuration(duration).
2477 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002478
Winson Chungf0ea4d32011-06-06 14:27:16 -07002479 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002480 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002481 final ObjectAnimator alphaAnim = ObjectAnimator
2482 .ofFloat(toView, "alpha", 0f, 1f)
2483 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002484 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002485 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2486 @Override
2487 public void onAnimationUpdate(ValueAnimator animation) {
2488 float t = (Float) animation.getAnimatedValue();
2489 dispatchOnLauncherTransitionStep(fromView, t);
2490 dispatchOnLauncherTransitionStep(toView, t);
2491 }
2492 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002493
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002494 // toView should appear right at the end of the workspace shrink
2495 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002496 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002497 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002498 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002499
2500 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002501 boolean animationCancelled = false;
2502
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002503 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002504 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002505 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002506 // Prepare the position
2507 toView.setTranslationX(0.0f);
2508 toView.setTranslationY(0.0f);
2509 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002510 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002511 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002512 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002513 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002514 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2515 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002516
2517 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2518 // Hide the workspace scrollbar
2519 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002520 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002521 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002522 if (!animationCancelled) {
2523 updateWallpaperVisibility(false);
2524 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002525
2526 // Hide the search bar
2527 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002528 }
2529
Adam Cohencff6af82011-09-13 14:51:53 -07002530 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002531 public void onAnimationCancel(Animator animation) {
2532 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002533 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 });
2535
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002536 if (workspaceAnim != null) {
2537 mStateAnimation.play(workspaceAnim);
2538 }
Michael Jurka1899a362011-11-03 13:50:45 -07002539
2540 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002541 final ViewTreeObserver observer;
2542
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002543 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2544 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002545
2546 // If any of the objects being animated haven't been measured/laid out
2547 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002548 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002549 (mWorkspace.getMeasuredWidth() == 0) ||
2550 (toView.getMeasuredWidth() == 0)) {
2551 observer = mWorkspace.getViewTreeObserver();
2552 delayAnim = true;
2553 } else {
2554 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002555 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002556
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002557 final AnimatorSet stateAnimation = mStateAnimation;
2558 final Runnable startAnimRunnable = new Runnable() {
2559 public void run() {
2560 // Check that mStateAnimation hasn't changed while
2561 // we waited for a layout/draw pass
2562 if (mStateAnimation != stateAnimation)
2563 return;
2564 setPivotsForZoom(toView, scale);
2565 dispatchOnLauncherTransitionStart(fromView, animated, false);
2566 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002567 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002568 public void run() {
2569 // Check that mStateAnimation hasn't changed while
2570 // we waited for a layout/draw pass
2571 if (mStateAnimation != stateAnimation)
2572 return;
2573 mStateAnimation.start();
2574 }
2575 });
2576 }
2577 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002578 if (delayAnim) {
2579 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2580 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002581 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002582 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002583 }
2584 };
2585 observer.addOnGlobalLayoutListener(delayedStart);
2586 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002587 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002588 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 } else {
2590 toView.setTranslationX(0.0f);
2591 toView.setTranslationY(0.0f);
2592 toView.setScaleX(1.0f);
2593 toView.setScaleY(1.0f);
2594 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002595 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002596
Michael Jurkabed61d22012-02-14 22:51:29 -08002597 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2598 // Hide the workspace scrollbar
2599 mWorkspace.hideScrollingIndicator(true);
2600 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002601
2602 // Hide the search bar
2603 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002604 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002605 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002606 dispatchOnLauncherTransitionStart(fromView, animated, false);
2607 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002608 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002609 dispatchOnLauncherTransitionStart(toView, animated, false);
2610 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002611 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002612 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002613 }
2614
2615 /**
2616 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002617 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002619 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002620 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2621 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002622
Michael Jurkab3e22d92011-10-31 15:58:33 -07002623 if (mStateAnimation != null) {
2624 mStateAnimation.cancel();
2625 mStateAnimation = null;
2626 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002627 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002628
Winson Chungf0ea4d32011-06-06 14:27:16 -07002629 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002630 final int fadeOutDuration =
2631 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002632 final float scaleFactor = (float)
2633 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2634 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002635 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002636 Animator workspaceAnim = null;
2637
2638 if (toState == State.WORKSPACE) {
2639 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2640 workspaceAnim = mWorkspace.getChangeStateAnimation(
2641 Workspace.State.NORMAL, animated, stagger);
2642 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2643 workspaceAnim = mWorkspace.getChangeStateAnimation(
2644 Workspace.State.SPRING_LOADED, animated);
2645 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002646
Michael Jurkab3e22d92011-10-31 15:58:33 -07002647 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002648 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002649 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002651 final LauncherViewPropertyAnimator scaleAnim =
2652 new LauncherViewPropertyAnimator(fromView);
2653 scaleAnim.
2654 scaleX(scaleFactor).scaleY(scaleFactor).
2655 setDuration(duration).
2656 setInterpolator(new Workspace.ZoomInInterpolator());
2657
2658 final ObjectAnimator alphaAnim = ObjectAnimator
2659 .ofFloat(fromView, "alpha", 1f, 0f)
2660 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002661 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002662 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2663 @Override
2664 public void onAnimationUpdate(ValueAnimator animation) {
2665 float t = 1f - (Float) animation.getAnimatedValue();
2666 dispatchOnLauncherTransitionStep(fromView, t);
2667 dispatchOnLauncherTransitionStep(toView, t);
2668 }
2669 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002670
Michael Jurka2ecf9952012-06-18 12:52:28 -07002671 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002672
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002673 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2674 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002675
2676 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002677 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002678 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002679 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002680 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002681 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2682 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002683 if (mWorkspace != null) {
2684 mWorkspace.hideScrollingIndicator(false);
2685 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002686 if (onCompleteRunnable != null) {
2687 onCompleteRunnable.run();
2688 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689 }
2690 });
2691
Winson Chungf0ea4d32011-06-06 14:27:16 -07002692 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002693 if (workspaceAnim != null) {
2694 mStateAnimation.play(workspaceAnim);
2695 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002696 dispatchOnLauncherTransitionStart(fromView, animated, true);
2697 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002698 final Animator stateAnimation = mStateAnimation;
2699 mWorkspace.post(new Runnable() {
2700 public void run() {
2701 if (stateAnimation != mStateAnimation)
2702 return;
2703 mStateAnimation.start();
2704 }
2705 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002706 } else {
2707 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002708 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002709 dispatchOnLauncherTransitionStart(fromView, animated, true);
2710 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002711 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002712 dispatchOnLauncherTransitionStart(toView, animated, true);
2713 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002714 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002715 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002716 }
2717
Michael Jurkae326f182011-11-21 14:05:46 -08002718 @Override
2719 public void onTrimMemory(int level) {
2720 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002721 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002722 mAppsCustomizeTabHost.onTrimMemory();
2723 }
2724 }
2725
Winson Chung18f41f82012-05-09 13:28:10 -07002726 @Override
2727 public void onWindowFocusChanged(boolean hasFocus) {
2728 if (!hasFocus) {
2729 // When another window occludes launcher (like the notification shade, or recents),
2730 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2731 updateWallpaperVisibility(true);
2732 } else {
2733 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002734 mWorkspace.postDelayed(new Runnable() {
2735 @Override
2736 public void run() {
2737 disableWallpaperIfInAllApps();
2738 }
2739 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002740 }
2741 }
2742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002743 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002744 showWorkspace(animated, null);
2745 }
2746
2747 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002748 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002749 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002751 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002752
Winson Chungc7d2b602012-05-16 17:02:20 -07002753 // Show the search bar (only animate if we were showing the drop target bar in spring
2754 // loaded mode)
2755 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2756
Michael Jurkab737ee62011-11-15 15:57:22 -08002757 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002758 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002759
2760 // Set focus to the AppsCustomize button
2761 if (mAllAppsButton != null) {
2762 mAllAppsButton.requestFocus();
2763 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002764 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002765
Michael Jurkab737ee62011-11-15 15:57:22 -08002766 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002767
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002768 // Change the state *after* we've called all the transition code
2769 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002770
Winson Chung5fb63472011-02-02 17:03:37 -08002771 // Resume the auto-advance of widgets
2772 mUserPresent = true;
2773 updateRunning();
2774
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002775 // send an accessibility event to announce the context change
2776 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002777 }
2778
Michael Jurkab3e22d92011-10-31 15:58:33 -07002779 void showAllApps(boolean animated) {
2780 if (mState != State.WORKSPACE) return;
2781
2782 showAppsCustomizeHelper(animated, false);
2783 mAppsCustomizeTabHost.requestFocus();
2784
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 // Change the state *after* we've called all the transition code
2786 mState = State.APPS_CUSTOMIZE;
2787
2788 // Pause the auto-advance of widgets until we are out of AllApps
2789 mUserPresent = false;
2790 updateRunning();
2791 closeFolder();
2792
2793 // Send an accessibility event to announce the context change
2794 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2795 }
2796
Adam Cohen7777d962011-08-18 18:58:38 -07002797 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002798 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002799 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002800 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002801 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002802 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002803 }
Adam Cohen7777d962011-08-18 18:58:38 -07002804
Adam Cohened66b2b2012-01-23 17:28:51 -08002805 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2806 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002807 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2808
Winson Chunge7a03942011-08-05 15:05:12 -07002809 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002810 @Override
2811 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002812 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002813 // Before we show workspace, hide all apps again because
2814 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2815 // clean up our state transition functions
2816 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002817 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002818 } else {
2819 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002820 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002821 }
2822 }, (extendedDelay ?
2823 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2824 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2825 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002826
Michael Jurkad3ef3062010-11-23 16:23:58 -08002827 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002828 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002829 final boolean animated = true;
2830 final boolean springLoaded = true;
2831 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002832 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002833 }
2834 // Otherwise, we are not in spring loaded mode, so don't do anything.
2835 }
2836
Michael Jurkab737ee62011-11-15 15:57:22 -08002837 void hideDockDivider() {
2838 if (mQsbDivider != null && mDockDivider != null) {
2839 mQsbDivider.setVisibility(View.INVISIBLE);
2840 mDockDivider.setVisibility(View.INVISIBLE);
2841 }
2842 }
2843
2844 void showDockDivider(boolean animated) {
2845 if (mQsbDivider != null && mDockDivider != null) {
2846 mQsbDivider.setVisibility(View.VISIBLE);
2847 mDockDivider.setVisibility(View.VISIBLE);
2848 if (mDividerAnimator != null) {
2849 mDividerAnimator.cancel();
2850 mQsbDivider.setAlpha(1f);
2851 mDockDivider.setAlpha(1f);
2852 mDividerAnimator = null;
2853 }
2854 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002855 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2856 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2857 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Michael Jurkab737ee62011-11-15 15:57:22 -08002858 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2859 mDividerAnimator.start();
2860 }
2861 }
2862 }
2863
Michael Jurkab3e22d92011-10-31 15:58:33 -07002864 void lockAllApps() {
2865 // TODO
2866 }
2867
2868 void unlockAllApps() {
2869 // TODO
2870 }
2871
Adam Cohenfc53cd22011-07-20 15:45:11 -07002872 public boolean isAllAppsCustomizeOpen() {
2873 return mState == State.APPS_CUSTOMIZE;
2874 }
2875
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002877 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002878 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002879 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002880 if (!LauncherApplication.isScreenLarge()) {
2881 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002882 if (mHotseat.getAlpha() != 1f) {
2883 int duration = mSearchDropTargetBar.getTransitionInDuration();
2884 mHotseat.animate().alpha(1f).setDuration(duration);
2885 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002886 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002887 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002888 }
2889 }
2890 }
2891
2892 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002893 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002894 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002895 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002896 if (!LauncherApplication.isScreenLarge()) {
2897 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002898 if (mHotseat.getAlpha() != 0f) {
2899 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2900 mHotseat.animate().alpha(0f).setDuration(duration);
2901 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002903 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002904 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002905 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002906 }
2907
Patrick Dubroy5f445422011-02-18 14:35:21 -08002908 /**
2909 * Add an item from all apps or customize onto the given workspace screen.
2910 * If layout is null, add to the current screen.
2911 */
2912 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002913 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002914 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002915 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002916 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002917
Winson Chungdff8ebb2011-09-08 17:25:31 -07002918 /** Maps the current orientation to an index for referencing orientation correct global icons */
2919 private int getCurrentOrientationIndexForGlobalIcons() {
2920 // default - 0, landscape - 1
2921 switch (getResources().getConfiguration().orientation) {
2922 case Configuration.ORIENTATION_LANDSCAPE:
2923 return 1;
2924 default:
2925 return 0;
2926 }
2927 }
2928
Michael Jurkaae65ee32012-04-30 11:45:54 -07002929 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002930 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002931 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002932 // Look for the toolbar icon specified in the activity meta-data
2933 Bundle metaData = packageManager.getActivityInfo(
2934 activityName, PackageManager.GET_META_DATA).metaData;
2935 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002936 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002937 if (iconResId != 0) {
2938 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002939 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002940 }
2941 }
2942 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002943 // This can happen if the activity defines an invalid drawable
2944 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2945 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002946 } catch (Resources.NotFoundException nfe) {
2947 // This can happen if the activity defines an invalid drawable
2948 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2949 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002950 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002951 return null;
2952 }
2953
2954 // if successful in getting icon, return it; otherwise, set button to use default drawable
2955 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002956 int buttonId, ComponentName activityName, int fallbackDrawableId,
2957 String toolbarResourceName) {
2958 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002959 Resources r = getResources();
2960 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2961 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002962
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002963 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002964 // If we were unable to find the icon via the meta-data, use a generic one
2965 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002966 toolbarIcon = r.getDrawable(fallbackDrawableId);
2967 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002968 if (button != null) {
2969 button.setCompoundDrawables(toolbarIcon, null, null, null);
2970 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002971 return null;
2972 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002973 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002974 if (button != null) {
2975 button.setCompoundDrawables(toolbarIcon, null, null, null);
2976 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002977 return toolbarIcon.getConstantState();
2978 }
2979 }
2980
2981 // if successful in getting icon, return it; otherwise, set button to use default drawable
2982 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002983 int buttonId, ComponentName activityName, int fallbackDrawableId,
2984 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002985 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002986 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002987
Michael Jurka19e0fc52011-07-22 18:00:21 -07002988 if (button != null) {
2989 // If we were unable to find the icon via the meta-data, use a
2990 // generic one
2991 if (toolbarIcon == null) {
2992 button.setImageResource(fallbackDrawableId);
2993 } else {
2994 button.setImageDrawable(toolbarIcon);
2995 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002996 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002997
2998 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2999
Michael Jurka0423dcf2010-10-05 14:56:18 -07003000 }
3001
Winson Chung1b884092012-06-08 17:13:02 -07003002 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003003 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003004 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003005 }
3006
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003007 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003008 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003009 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003010 }
3011
Winson Chungbb185bd2011-11-21 12:31:42 -08003012 private void invalidatePressedFocusedStates(View container, View button) {
3013 if (container instanceof HolographicLinearLayout) {
3014 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3015 layout.invalidatePressedFocusedStates();
3016 } else if (button instanceof HolographicImageView) {
3017 HolographicImageView view = (HolographicImageView) button;
3018 view.invalidatePressedFocusedStates();
3019 }
3020 }
3021
Winson Chungc51db6a2011-10-05 11:44:49 -07003022 private boolean updateGlobalSearchIcon() {
3023 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003024 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3025 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003026 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003027 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003028 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003029
Winson Chung1cad91e2011-05-25 17:41:01 -07003030 final SearchManager searchManager =
3031 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3032 ComponentName activityName = searchManager.getGlobalSearchActivity();
3033 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003034 int coi = getCurrentOrientationIndexForGlobalIcons();
3035 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003036 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3037 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3038 if (sGlobalSearchIcon[coi] == null) {
3039 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3040 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3041 TOOLBAR_ICON_METADATA_NAME);
3042 }
3043
Winson Chung649723c2011-07-06 20:41:23 -07003044 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003045 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3046 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003047 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003048 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003049 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003050 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003051 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003052 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3053 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3054 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003055 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003056 if (voiceButtonProxy != null) {
3057 voiceButtonProxy.setVisibility(View.GONE);
3058 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003059 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003060 }
3061 }
3062
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003063 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003064 final View searchButtonContainer = findViewById(R.id.search_button_container);
3065 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003066 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003067 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003068 }
3069
Winson Chungc51db6a2011-10-05 11:44:49 -07003070 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003071 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003072 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003073 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003074 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003075
Winson Chungc51db6a2011-10-05 11:44:49 -07003076 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003077 final SearchManager searchManager =
3078 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3079 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3080
3081 ComponentName activityName = null;
3082 if (globalSearchActivity != null) {
3083 // Check if the global search activity handles voice search
3084 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3085 intent.setPackage(globalSearchActivity.getPackageName());
3086 activityName = intent.resolveActivity(getPackageManager());
3087 }
3088
3089 if (activityName == null) {
3090 // Fallback: check if an activity other than the global search activity
3091 // resolves this
3092 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3093 activityName = intent.resolveActivity(getPackageManager());
3094 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003095 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003096 int coi = getCurrentOrientationIndexForGlobalIcons();
3097 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003098 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3099 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3100 if (sVoiceSearchIcon[coi] == null) {
3101 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3102 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3103 TOOLBAR_ICON_METADATA_NAME);
3104 }
Winson Chung649723c2011-07-06 20:41:23 -07003105 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003106 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003107 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003108 if (voiceButtonProxy != null) {
3109 voiceButtonProxy.setVisibility(View.VISIBLE);
3110 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003111 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003112 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003113 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003114 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003115 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003116 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003117 if (voiceButtonProxy != null) {
3118 voiceButtonProxy.setVisibility(View.GONE);
3119 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003120 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003121 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003122 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003123
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003124 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003125 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3126 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003127 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003128 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003129 }
3130
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003131 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003132 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003133 */
3134 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003135 final View marketButton = findViewById(R.id.market_button);
3136 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3137 // Find the app market activity by resolving an intent.
3138 // (If multiple app markets are installed, it will return the ResolverActivity.)
3139 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003140 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003141 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003142 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003143 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3145 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 marketButton.setVisibility(View.VISIBLE);
3147 } else {
3148 // We should hide and disable the view so that we don't try and restore the visibility
3149 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3150 marketButton.setVisibility(View.GONE);
3151 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003152 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003153 }
3154
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003155 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003156 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3157 Resources r = getResources();
3158 Drawable marketIconDrawable = d.newDrawable(r);
3159 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3160 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3161 marketIconDrawable.setBounds(0, 0, w, h);
3162
3163 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003164 }
3165
Michael Jurkad7c28052012-04-27 15:43:36 -07003166 @Override
3167 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3168 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3169 final List<CharSequence> text = event.getText();
3170 text.clear();
3171 text.add(getString(R.string.home));
3172 return result;
3173 }
3174
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003175 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003176 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003177 */
3178 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3179 @Override
3180 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003181 closeSystemDialogs();
3182 }
3183 }
3184
3185 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003186 * Receives notifications whenever the appwidgets are reset.
3187 */
3188 private class AppWidgetResetObserver extends ContentObserver {
3189 public AppWidgetResetObserver() {
3190 super(new Handler());
3191 }
3192
3193 @Override
3194 public void onChange(boolean selfChange) {
3195 onAppWidgetReset();
3196 }
3197 }
3198
3199 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003200 * If the activity is currently paused, signal that we need to re-run the loader
3201 * in onResume.
3202 *
3203 * This needs to be called from incoming places where resources might have been loaded
3204 * while we are paused. That is becaues the Configuration might be wrong
3205 * when we're not running, and if it comes back to what it was when we
3206 * were paused, we are not restarted.
3207 *
3208 * Implementation of the method from LauncherModel.Callbacks.
3209 *
3210 * @return true if we are currently paused. The caller might be able to
3211 * skip some work in that case since we will come back again.
3212 */
3213 public boolean setLoadOnResume() {
3214 if (mPaused) {
3215 Log.i(TAG, "setLoadOnResume");
3216 mOnResumeNeedsLoad = true;
3217 return true;
3218 } else {
3219 return false;
3220 }
3221 }
3222
3223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003227 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003228 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003229 } else {
3230 return SCREEN_COUNT / 2;
3231 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003233
Joe Onorato9c1289c2009-08-17 11:03:03 -04003234 /**
3235 * Refreshes the shortcuts shown on the workspace.
3236 *
3237 * Implementation of the method from LauncherModel.Callbacks.
3238 */
3239 public void startBinding() {
3240 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003241
Winson Chungf0c6ae02012-03-21 16:10:31 -07003242 mNewShortcutAnimatePage = -1;
3243 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003244 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245 int count = workspace.getChildCount();
3246 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003247 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003248 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3249 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003250 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003251 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003252 if (mHotseat != null) {
3253 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 }
3255 }
3256
3257 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 * Bind the items start-end from the list.
3259 *
3260 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003263 setLoadOnResume();
3264
Winson Chungf0c6ae02012-03-21 16:10:31 -07003265 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3266 Set<String> newApps = new HashSet<String>();
3267 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3268
3269 Workspace workspace = mWorkspace;
3270 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003271 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003272
3273 // Short circuit if we are loading dock items for a configuration which has no dock
3274 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3275 mHotseat == null) {
3276 continue;
3277 }
3278
Joe Onorato9c1289c2009-08-17 11:03:03 -04003279 switch (item.itemType) {
3280 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3281 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003282 ShortcutInfo info = (ShortcutInfo) item;
3283 String uri = info.intent.toUri(0).toString();
3284 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003285 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3286 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003287 boolean animateIconUp = false;
3288 synchronized (newApps) {
3289 if (newApps.contains(uri)) {
3290 animateIconUp = newApps.remove(uri);
3291 }
3292 }
3293 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003294 // Prepare the view to be animated up
3295 shortcut.setAlpha(0f);
3296 shortcut.setScaleX(0f);
3297 shortcut.setScaleY(0f);
3298 mNewShortcutAnimatePage = item.screen;
3299 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3300 mNewShortcutAnimateViews.add(shortcut);
3301 }
3302 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003304 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003305 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003306 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003307 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003308 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3309 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003313
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003315 }
3316
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 /**
3318 * Implementation of the method from LauncherModel.Callbacks.
3319 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003320 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003321 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003322 sFolders.clear();
3323 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325
3326 /**
3327 * Add the views for a widget to the workspace.
3328 *
3329 * Implementation of the method from LauncherModel.Callbacks.
3330 */
3331 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003332 setLoadOnResume();
3333
Daniel Sandler843e8602010-06-07 14:59:01 -04003334 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3335 if (DEBUG_WIDGETS) {
3336 Log.d(TAG, "bindAppWidget: " + item);
3337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 final Workspace workspace = mWorkspace;
3339
3340 final int appWidgetId = item.appWidgetId;
3341 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003342 if (DEBUG_WIDGETS) {
3343 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3344 }
3345
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3347
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003349 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350
Winson Chung3d503fb2011-07-13 17:25:49 -07003351 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003353 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3354
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 workspace.requestLayout();
3356
Daniel Sandler843e8602010-06-07 14:59:01 -04003357 if (DEBUG_WIDGETS) {
3358 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3359 + (SystemClock.uptimeMillis()-start) + "ms");
3360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 }
3362
3363 /**
3364 * Callback saying that there aren't any more items to bind.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 */
3368 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003369 setLoadOnResume();
3370
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 if (mSavedState != null) {
3372 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003373 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 mSavedState = null;
3376 }
3377
3378 if (mSavedInstanceState != null) {
3379 super.onRestoreInstanceState(mSavedInstanceState);
3380 mSavedInstanceState = null;
3381 }
3382
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003383 // If we received the result of any pending adds while the loader was running (e.g. the
3384 // widget configuration forced an orientation change), process them now.
3385 for (int i = 0; i < sPendingAddList.size(); i++) {
3386 completeAdd(sPendingAddList.get(i));
3387 }
3388 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389
Winson Chungc51db6a2011-10-05 11:44:49 -07003390 // Update the market app icon as necessary (the other icons will be managed in response to
3391 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003392 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003393
Winson Chungf0c6ae02012-03-21 16:10:31 -07003394 // Animate up any icons as necessary
3395 if (mVisible || mWorkspaceLoading) {
3396 Runnable newAppsRunnable = new Runnable() {
3397 @Override
3398 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003399 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003400 }
3401 };
Winson Chunga2413752012-04-03 14:22:34 -07003402
3403 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3404 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3405 if (canRunNewAppsAnimation()) {
3406 // If the user has not interacted recently, then either snap to the new page to show
3407 // the new-apps animation or just run them if they are to appear on the current page
3408 if (willSnapPage) {
3409 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3410 } else {
3411 runNewAppsAnimation(false);
3412 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003413 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003414 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003415 // are on another page (or just normally if they are added to the current page)
3416 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003417 }
3418 }
3419
3420 mWorkspaceLoading = false;
3421 }
3422
Winson Chunga2413752012-04-03 14:22:34 -07003423 private boolean canRunNewAppsAnimation() {
3424 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3425 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3426 }
3427
Winson Chungf0c6ae02012-03-21 16:10:31 -07003428 /**
3429 * Runs a new animation that scales up icons that were added while Launcher was in the
3430 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003431 *
3432 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003433 */
Winson Chunga2413752012-04-03 14:22:34 -07003434 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003435 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003436 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003437
3438 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003439 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3440 @Override
3441 public int compare(View a, View b) {
3442 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3443 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3444 int cellCountX = LauncherModel.getCellCountX();
3445 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3446 }
3447 });
Winson Chunga2413752012-04-03 14:22:34 -07003448
3449 // Animate each of the views in place (or show them immediately if requested)
3450 if (immediate) {
3451 for (View v : mNewShortcutAnimateViews) {
3452 v.setAlpha(1f);
3453 v.setScaleX(1f);
3454 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003455 }
Winson Chunga2413752012-04-03 14:22:34 -07003456 } else {
3457 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3458 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003459 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003460 PropertyValuesHolder.ofFloat("alpha", 1f),
3461 PropertyValuesHolder.ofFloat("scaleX", 1f),
3462 PropertyValuesHolder.ofFloat("scaleY", 1f));
3463 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3464 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3465 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3466 bounceAnims.add(bounceAnim);
3467 }
3468 anim.playTogether(bounceAnims);
3469 anim.addListener(new AnimatorListenerAdapter() {
3470 @Override
3471 public void onAnimationEnd(Animator animation) {
3472 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3473 }
3474 });
3475 anim.start();
3476 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003477
3478 // Clean up
3479 mNewShortcutAnimatePage = -1;
3480 mNewShortcutAnimateViews.clear();
3481 new Thread("clearNewAppsThread") {
3482 public void run() {
3483 mSharedPrefs.edit()
3484 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3485 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3486 .commit();
3487 }
3488 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003489 }
3490
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003491 @Override
3492 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003493 boolean searchVisible = updateGlobalSearchIcon();
3494 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3495 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003496 }
3497
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003498 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 * Add the icons for all apps.
3500 *
3501 * Implementation of the method from LauncherModel.Callbacks.
3502 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003503 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3504 // Remove the progress bar entirely; we could also make it GONE
3505 // but better to remove it since we know it's not going to be used
3506 View progressBar = mAppsCustomizeTabHost.
3507 findViewById(R.id.apps_customize_progress_bar);
3508 if (progressBar != null) {
3509 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003510 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003511 // We just post the call to setApps so the user sees the progress bar
3512 // disappear-- otherwise, it just looks like the progress bar froze
3513 // which doesn't look great
3514 mAppsCustomizeTabHost.post(new Runnable() {
3515 public void run() {
3516 if (mAppsCustomizeContent != null) {
3517 mAppsCustomizeContent.setApps(apps);
3518 }
3519 }
3520 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 }
3522
3523 /**
3524 * A package was installed.
3525 *
3526 * Implementation of the method from LauncherModel.Callbacks.
3527 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003528 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003529 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003530
Winson Chung785d2eb2011-04-14 16:08:02 -07003531 if (mAppsCustomizeContent != null) {
3532 mAppsCustomizeContent.addApps(apps);
3533 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 }
3535
3536 /**
3537 * A package was updated.
3538 *
3539 * Implementation of the method from LauncherModel.Callbacks.
3540 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003541 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003542 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003543 if (mWorkspace != null) {
3544 mWorkspace.updateShortcuts(apps);
3545 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003546
Winson Chung785d2eb2011-04-14 16:08:02 -07003547 if (mAppsCustomizeContent != null) {
3548 mAppsCustomizeContent.updateApps(apps);
3549 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003550 }
3551
3552 /**
3553 * A package was uninstalled.
3554 *
3555 * Implementation of the method from LauncherModel.Callbacks.
3556 */
Winson Chungcd810732012-06-18 16:45:43 -07003557 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003558 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003559 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003560 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003561
Winson Chung785d2eb2011-04-14 16:08:02 -07003562 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003563 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003564 }
Winson Chunga1820962011-10-03 16:31:06 -07003565
3566 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003567 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 }
Joe Onoratobe386092009-11-17 17:32:16 -08003569
3570 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003571 * A number of packages were updated.
3572 */
3573 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003574 if (mAppsCustomizeContent != null) {
3575 mAppsCustomizeContent.onPackagesUpdated();
3576 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003577 }
3578
Winson Chung400438b2011-01-16 17:53:48 -08003579 private int mapConfigurationOriActivityInfoOri(int configOri) {
3580 final Display d = getWindowManager().getDefaultDisplay();
3581 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3582 switch (d.getRotation()) {
3583 case Surface.ROTATION_0:
3584 case Surface.ROTATION_180:
3585 // We are currently in the same basic orientation as the natural orientation
3586 naturalOri = configOri;
3587 break;
3588 case Surface.ROTATION_90:
3589 case Surface.ROTATION_270:
3590 // We are currently in the other basic orientation to the natural orientation
3591 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3592 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3593 break;
3594 }
3595
3596 int[] oriMap = {
3597 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3598 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3599 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3600 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3601 };
3602 // Since the map starts at portrait, we need to offset if this device's natural orientation
3603 // is landscape.
3604 int indexOffset = 0;
3605 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3606 indexOffset = 1;
3607 }
3608 return oriMap[(d.getRotation() + indexOffset) % 4];
3609 }
Adam Cohen446e9402011-09-15 18:21:21 -07003610
Winson Chung4b919f82012-05-01 10:44:08 -07003611 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003612 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003613 boolean enableRotation = forceEnableRotation ||
3614 getResources().getBoolean(R.bool.allow_rotation);
3615 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003616 }
Winson Chung4b919f82012-05-01 10:44:08 -07003617 public void lockScreenOrientation() {
3618 if (isRotationEnabled()) {
3619 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3620 .getConfiguration().orientation));
3621 }
3622 }
3623 public void unlockScreenOrientation(boolean immediate) {
3624 if (isRotationEnabled()) {
3625 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003626 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003627 } else {
3628 mHandler.postDelayed(new Runnable() {
3629 public void run() {
3630 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3631 }
3632 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003633 }
Winson Chung4b919f82012-05-01 10:44:08 -07003634 }
Winson Chung400438b2011-01-16 17:53:48 -08003635 }
3636
Winson Chung82f55532011-08-09 14:14:23 -07003637 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003638 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003639 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003640 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003641
Winson Chung7d7541e2011-09-16 20:14:36 -07003642 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003643 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003644 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3645 Cling cling = (Cling) findViewById(clingId);
3646 if (cling != null) {
3647 cling.init(this, positionData);
3648 cling.setVisibility(View.VISIBLE);
3649 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3650 if (animate) {
3651 cling.buildLayer();
3652 cling.setAlpha(0f);
3653 cling.animate()
3654 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003655 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003656 .setDuration(SHOW_CLING_DURATION)
3657 .setStartDelay(delay)
3658 .start();
3659 } else {
3660 cling.setAlpha(1f);
3661 }
3662 }
3663 return cling;
3664 }
3665 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003666 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003667 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003668 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003669 anim.addListener(new AnimatorListenerAdapter() {
3670 public void onAnimationEnd(Animator animation) {
3671 cling.setVisibility(View.GONE);
3672 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003673 // We should update the shared preferences on a background thread
3674 new Thread("dismissClingThread") {
3675 public void run() {
3676 SharedPreferences.Editor editor = mSharedPrefs.edit();
3677 editor.putBoolean(flag, true);
3678 editor.commit();
3679 }
3680 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003681 };
3682 });
3683 anim.start();
3684 }
3685 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003686 private void removeCling(int id) {
3687 final View cling = findViewById(id);
3688 if (cling != null) {
3689 final ViewGroup parent = (ViewGroup) cling.getParent();
3690 parent.post(new Runnable() {
3691 @Override
3692 public void run() {
3693 parent.removeView(cling);
3694 }
3695 });
3696 }
3697 }
Michael Jurka974c3862012-05-22 22:00:31 -07003698
3699 private boolean skipCustomClingIfNoAccounts() {
3700 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3701 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3702 if (customCling) {
3703 AccountManager am = AccountManager.get(this);
3704 Account[] accounts = am.getAccountsByType("com.google");
3705 return accounts.length == 0;
3706 }
3707 return false;
3708 }
3709
Winson Chung7d7541e2011-09-16 20:14:36 -07003710 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003711 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003712 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003713 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3714 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003715 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003716 } else {
3717 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003718 }
3719 }
3720 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003721 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003722 if (isClingsEnabled() &&
3723 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003724 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003725 } else {
3726 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003727 }
3728 }
3729 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003730 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003731 if (isClingsEnabled() &&
3732 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3733 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003734 } else {
3735 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003736 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003737 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003738 }
3739 public boolean isFolderClingVisible() {
3740 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003741 if (cling != null) {
3742 return cling.getVisibility() == View.VISIBLE;
3743 }
3744 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003745 }
Winson Chung82f55532011-08-09 14:14:23 -07003746 public void dismissWorkspaceCling(View v) {
3747 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003749 }
3750 public void dismissAllAppsCling(View v) {
3751 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003752 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3753 }
3754 public void dismissFolderCling(View v) {
3755 Cling cling = (Cling) findViewById(R.id.folder_cling);
3756 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003757 }
3758
Winson Chung80baf5a2010-08-09 16:03:15 -07003759 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003760 * Prints out out state for debugging.
3761 */
3762 public void dumpState() {
3763 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003764 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003765 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3766 Log.d(TAG, "mRestoring=" + mRestoring);
3767 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3768 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003769 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003770 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003771
Winson Chung785d2eb2011-04-14 16:08:02 -07003772 if (mAppsCustomizeContent != null) {
3773 mAppsCustomizeContent.dumpState();
3774 }
Joe Onoratobe386092009-11-17 17:32:16 -08003775 Log.d(TAG, "END launcher2 dump state");
3776 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003777
3778 @Override
3779 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3780 super.dump(prefix, fd, writer, args);
3781 writer.println(" ");
3782 writer.println("Debug logs: ");
3783 for (int i = 0; i < sDumpLogs.size(); i++) {
3784 writer.println(" " + sDumpLogs.get(i));
3785 }
3786 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003787
3788 public static void dumpDebugLogsToConsole() {
3789 Log.d(TAG, "");
3790 Log.d(TAG, "*********************");
3791 Log.d(TAG, "Launcher debug logs: ");
3792 for (int i = 0; i < sDumpLogs.size(); i++) {
3793 Log.d(TAG, " " + sDumpLogs.get(i));
3794 }
3795 Log.d(TAG, "*********************");
3796 Log.d(TAG, "");
3797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003798}
Michael Jurka2763be32011-02-24 11:19:57 -08003799
Michael Jurkaabded662011-03-04 12:06:57 -08003800interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003801 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003802 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003803 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003804 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003805 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003806}