blob: d8275312684f75136fa2fb65126213a2096d52c5 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080088import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070097import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700102import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
Winson Chung70d72102011-08-12 11:24:35 -0700128 private static final int MENU_GROUP_WALLPAPER = 1;
129 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
130 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700131 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
132 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 private static final int REQUEST_BIND_APPWIDGET = 11;
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700149 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
150 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Winson Chung2672ff92012-05-04 16:22:30 -0700152 // The Intent extra that defines whether to ignore the launch animation
153 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
154 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700158 // Type: int
159 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
162 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
164 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: boolean
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
170 // Type: long
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700172 // Type: int
173 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
174 // Type: int
175 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
176 // Type: parcelable
177 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700180 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
181 "com.android.launcher.toolbar_search_icon";
182 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
183 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700186 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800189 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700192 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
193 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700194 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800198 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Winson Chunga2413752012-04-03 14:22:34 -0700200 // How long to wait before the new-shortcut animation automatically pans the workspace
201 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
202
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800203 private final BroadcastReceiver mCloseSystemDialogsReceiver
204 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800205 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private LayoutInflater mInflater;
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private View mQsbDivider;
211 private View mDockDivider;
212 private DragLayer mDragLayer;
213 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private AppWidgetManager mAppWidgetManager;
216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurkac9d95c52011-08-29 14:03:34 -0700218 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700219 private AppWidgetProviderInfo mPendingAddWidgetInfo;
220
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221 private int[] mTmpAddItemCellCoordinates = new int[2];
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private FolderInfo mFolderInfo;
224
Winson Chung3d503fb2011-07-13 17:25:49 -0700225 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800226 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700227
Winson Chungc51db6a2011-10-05 11:44:49 -0700228 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700229 private AppsCustomizeTabHost mAppsCustomizeTabHost;
230 private AppsCustomizePagedView mAppsCustomizeContent;
231 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700234 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
235 // scroll issues (because the workspace may not have been measured yet) and extra work.
236 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
237 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
239 private SpannableStringBuilder mDefaultKeySsb = null;
240
Joe Onorato9c1289c2009-08-17 11:03:03 -0400241 private boolean mWorkspaceLoading = true;
242
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800243 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private boolean mRestoring;
245 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700246 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247
Winson Chung4a2afa32012-07-19 14:53:05 -0700248 // Keep track of whether the user has left launcher
249 private static boolean sPausedFromUserAction = false;
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private Bundle mSavedInstanceState;
252
Joe Onorato9c1289c2009-08-17 11:03:03 -0400253 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800255 private boolean mUserPresent = true;
256 private boolean mVisible = false;
257 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static LocaleConfiguration sLocaleConfiguration = null;
260
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700261 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700279 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Adam Cohen1462de32012-07-24 22:34:36 -0700283 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
284
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700285 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
286
Winson Chung46353de2012-02-16 14:05:10 -0800287 // We only want to get the SharedPreferences once since it does an FS stat each time we get
288 // it from the context.
289 private SharedPreferences mSharedPrefs;
290
Winson Chungf0c6ae02012-03-21 16:10:31 -0700291 // Holds the page that we need to animate to, and the icon views that we need to animate up
292 // when we scroll to that page on resume.
293 private int mNewShortcutAnimatePage = -1;
294 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700295 private ImageView mFolderIconImageView;
296 private Bitmap mFolderIconBitmap;
297 private Canvas mFolderIconCanvas;
298 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700299
Michael Jurkaddd62e92011-02-16 17:49:14 -0800300 private BubbleTextView mWaitingForResume;
301
Michael Jurkac1f5d262011-09-30 19:32:27 -0700302 private Runnable mBuildLayersRunnable = new Runnable() {
303 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700304 if (mWorkspace != null) {
305 mWorkspace.buildPageHardwareLayers();
306 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700307 }
308 };
309
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800310 private static ArrayList<PendingAddArguments> sPendingAddList
311 = new ArrayList<PendingAddArguments>();
312
313 private static class PendingAddArguments {
314 int requestCode;
315 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700316 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800317 int screen;
318 int cellX;
319 int cellY;
320 }
321
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700322
323 private boolean doesFileExist(String filename) {
324 FileInputStream fis = null;
325 try {
326 fis = openFileInput(filename);
327 fis.close();
328 return true;
329 } catch (java.io.FileNotFoundException e) {
330 return false;
331 } catch (java.io.IOException e) {
332 return true;
333 }
334 }
335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 @Override
337 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700338 if (DEBUG_STRICT_MODE) {
339 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
340 .detectDiskReads()
341 .detectDiskWrites()
342 .detectNetwork() // or .detectAll() for all detectable problems
343 .penaltyLog()
344 .build());
345 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
346 .detectLeakedSqlLiteObjects()
347 .detectLeakedClosableObjects()
348 .penaltyLog()
349 .penaltyDeath()
350 .build());
351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800354 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700355 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
356 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800357 mModel = app.setLauncher(this);
358 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400359 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700361
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
364 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700366 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
367 // this also ensures that any synchronous binding below doesn't re-trigger another
368 // LauncherModel load.
369 mPaused = false;
370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200372 android.os.Debug.startMethodTracing(
373 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 }
375
376 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 setContentView(R.layout.launcher);
378 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700379 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800381 registerContentObservers();
382
Joe Onorato7c312c12009-08-13 21:36:53 -0700383 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 mSavedState = savedInstanceState;
386 restoreState(mSavedState);
387
Winson Chunga12a2502010-12-20 14:41:35 -0800388 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700389 if (mAppsCustomizeContent != null) {
390 mAppsCustomizeContent.onPackagesUpdated();
391 }
Winson Chunga12a2502010-12-20 14:41:35 -0800392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 if (PROFILE_STARTUP) {
394 android.os.Debug.stopMethodTracing();
395 }
396
397 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700398 if (sPausedFromUserAction) {
399 // If the user leaves launcher, then we should just load items asynchronously when
400 // they return.
401 mModel.startLoader(true, -1);
402 } else {
403 // We only load the page synchronously if the user rotates (or triggers a
404 // configuration change) while launcher is in the foreground
405 mModel.startLoader(true, mWorkspace.getCurrentPage());
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Michael Jurkac57b7a82011-08-09 22:02:20 -0700409 if (!mModel.isAllAppsLoaded()) {
410 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
411 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 // For handling default keys
415 mDefaultKeySsb = new SpannableStringBuilder();
416 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800417
418 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
419 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800420
Adam Cohenf9426d52012-06-04 17:26:21 -0700421 updateGlobalIcons();
422
423 // On large interfaces, we want the screen to auto-rotate based on the current orientation
424 unlockScreenOrientation(true);
425 }
426
Winson Chung4a2afa32012-07-19 14:53:05 -0700427 protected void onUserLeaveHint() {
428 super.onUserLeaveHint();
429 sPausedFromUserAction = true;
430 }
431
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700433 boolean searchVisible = false;
434 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700436 int coi = getCurrentOrientationIndexForGlobalIcons();
437 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
438 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700439 updateAppMarketIcon();
440 searchVisible = updateGlobalSearchIcon();
441 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700443 if (sGlobalSearchIcon[coi] != null) {
444 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sVoiceSearchIcon[coi] != null) {
448 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sAppMarketIcon[coi] != null) {
452 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800453 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700454 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 }
Romain Guycbb89e42009-06-08 15:52:54 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700458 if (sLocaleConfiguration == null) {
459 new AsyncTask<Void, Void, LocaleConfiguration>() {
460 @Override
461 protected LocaleConfiguration doInBackground(Void... unused) {
462 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
463 readConfiguration(Launcher.this, localeConfiguration);
464 return localeConfiguration;
465 }
466
467 @Override
468 protected void onPostExecute(LocaleConfiguration result) {
469 sLocaleConfiguration = result;
470 checkForLocaleChange(); // recursive, but now with a locale configuration
471 }
472 }.execute();
473 return;
474 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 final Configuration configuration = getResources().getConfiguration();
477
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final String locale = configuration.locale.toString();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final int mcc = configuration.mcc;
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mnc = configuration.mnc;
486
Romain Guy84f296c2009-11-04 15:00:44 -0800487 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488
Romain Guy84f296c2009-11-04 15:00:44 -0800489 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 sLocaleConfiguration.locale = locale;
491 sLocaleConfiguration.mcc = mcc;
492 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700493
Joe Onorato0589f0f2010-02-08 13:44:00 -0800494 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495
496 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
497 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700498 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700499 public void run() {
500 writeConfiguration(Launcher.this, localeConfiguration);
501 }
502 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700503 }
504 }
505
506 private static class LocaleConfiguration {
507 public String locale;
508 public int mcc = -1;
509 public int mnc = -1;
510 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700511
Romain Guy98d01652009-06-30 16:21:04 -0700512 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
513 DataInputStream in = null;
514 try {
515 in = new DataInputStream(context.openFileInput(PREFERENCES));
516 configuration.locale = in.readUTF();
517 configuration.mcc = in.readInt();
518 configuration.mnc = in.readInt();
519 } catch (FileNotFoundException e) {
520 // Ignore
521 } catch (IOException e) {
522 // Ignore
523 } finally {
524 if (in != null) {
525 try {
526 in.close();
527 } catch (IOException e) {
528 // Ignore
529 }
530 }
531 }
532 }
533
534 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
535 DataOutputStream out = null;
536 try {
537 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
538 out.writeUTF(configuration.locale);
539 out.writeInt(configuration.mcc);
540 out.writeInt(configuration.mnc);
541 out.flush();
542 } catch (FileNotFoundException e) {
543 // Ignore
544 } catch (IOException e) {
545 //noinspection ResultOfMethodCallIgnored
546 context.getFileStreamPath(PREFERENCES).delete();
547 } finally {
548 if (out != null) {
549 try {
550 out.close();
551 } catch (IOException e) {
552 // Ignore
553 }
554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
556 }
557
Adam Cohen716b51e2011-06-30 12:09:54 -0700558 public DragLayer getDragLayer() {
559 return mDragLayer;
560 }
561
Winson Chung36a62fe2012-05-06 18:04:42 -0700562 boolean isDraggingEnabled() {
563 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
564 // that is subsequently removed from the workspace in startBinding().
565 return !mModel.isLoadingWorkspace();
566 }
567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 static int getScreen() {
569 synchronized (sLock) {
570 return sScreen;
571 }
572 }
573
574 static void setScreen(int screen) {
575 synchronized (sLock) {
576 sScreen = screen;
577 }
578 }
579
Winson Chung557d6ed2011-07-08 15:34:52 -0700580 /**
581 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
582 * a configuration step, this allows the proper animations to run after other transitions.
583 */
584 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700585 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800586 switch (args.requestCode) {
587 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700588 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
589 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800590 break;
591 case REQUEST_PICK_SHORTCUT:
592 processShortcut(args.intent);
593 break;
594 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700595 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
596 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700597 result = true;
598 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800599 case REQUEST_CREATE_APPWIDGET:
600 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 result = true;
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_PICK_WALLPAPER:
605 // We just wanted the activity result here so we can clear mWaitingForResult
606 break;
607 }
Michael Jurka27614d22012-04-02 06:40:22 -0700608 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
609 // if you turned the screen off and then back while in All Apps, Launcher would not
610 // return to the workspace. Clearing mAddInfo.container here fixes this issue
611 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700612 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800613 }
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 protected void onActivityResult(
617 final int requestCode, final int resultCode, final Intent data) {
618 if (requestCode == REQUEST_BIND_APPWIDGET) {
619 int appWidgetId = data != null ?
620 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
621 if (resultCode == RESULT_CANCELED) {
622 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
623 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700624 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700625 }
626 return;
627 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700628 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800629 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
630 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700631 mWaitingForResult = false;
632
Adam Cohened66b2b2012-01-23 17:28:51 -0800633 // We have special handling for widgets
634 if (isWidgetDrop) {
635 int appWidgetId = data != null ?
636 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700637 if (appWidgetId < 0) {
638 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
639 "widget configuration activity.");
640 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
641 } else {
642 completeTwoStageWidgetDrop(resultCode, appWidgetId);
643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 return;
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 // The pattern used here is that a user PICKs a specific application,
648 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
651 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700652 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800653 final PendingAddArguments args = new PendingAddArguments();
654 args.requestCode = requestCode;
655 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700656 args.container = mPendingAddInfo.container;
657 args.screen = mPendingAddInfo.screen;
658 args.cellX = mPendingAddInfo.cellX;
659 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 if (isWorkspaceLocked()) {
661 sPendingAddList.add(args);
662 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700663 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800666 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700667 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
669 null);
670 }
671
672 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700673 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700674 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800675 Runnable onCompleteRunnable = null;
676 int animationType = 0;
677
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700678 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 if (resultCode == RESULT_OK) {
680 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
681 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700682 mPendingAddWidgetInfo);
683 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 onCompleteRunnable = new Runnable() {
685 @Override
686 public void run() {
687 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
688 mPendingAddInfo.screen, layout, null);
689 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
690 null);
691 }
692 };
693 } else if (resultCode == RESULT_CANCELED) {
694 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
695 onCompleteRunnable = new Runnable() {
696 @Override
697 public void run() {
698 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
699 null);
700 }
701 };
702 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700703 if (mDragLayer.getAnimatedView() != null) {
704 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
705 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
706 animationType, boundWidget, true);
707 } else {
708 // The animated view may be null in the case of a rotation during widget configuration
709 onCompleteRunnable.run();
710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 }
712
713 @Override
714 protected void onResume() {
715 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700716
Winson Chung4a2afa32012-07-19 14:53:05 -0700717 // Restore the previous launcher state
718 if (mOnResumeState == State.WORKSPACE) {
719 showWorkspace(false);
720 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
721 showAllApps(false);
722 }
723 mOnResumeState = State.NONE;
724
Winson Chungde0fb8f2012-05-08 14:37:08 -0700725 // Process any items that were added while Launcher was away
726 InstallShortcutReceiver.flushInstallQueue(this);
727
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800728 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700729 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700730 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700732 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700734 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
Winson Chunge4e50662012-01-23 14:45:13 -0800736
737 // Reset the pressed state of icons that were locked in the press state while activities
738 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800739 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800740 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800741 mWaitingForResume.setStayPressed(false);
742 }
Winson Chunge4e50662012-01-23 14:45:13 -0800743 if (mAppsCustomizeContent != null) {
744 // Resets the previous all apps icon press state
745 mAppsCustomizeContent.resetDrawableState();
746 }
Adam Cohen06dff352012-06-01 17:17:08 -0700747 // It is possible that widgets can receive updates while launcher is not in the foreground.
748 // Consequently, the widgets will be inflated in the orientation of the foreground activity
749 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
750 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700751 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700752
753 // Again, as with the above scenario, it's possible that one or more of the global icons
754 // were updated in the wrong orientation.
755 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700756 }
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700759 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700760 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
761 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
762 // when Launcher resumes and we are still in AllApps.
763 updateWallpaperVisibility(true);
764
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700766 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800767 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700768 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700769 }
Romain Guycbb89e42009-06-08 15:52:54 -0700770
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700771 @Override
772 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 // Flag the loader to stop early before switching
774 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 if (mAppsCustomizeContent != null) {
776 mAppsCustomizeContent.surrender();
777 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800778 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700779 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700780
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800781 // We can't hide the IME if it was forced open. So don't bother
782 /*
783 @Override
784 public void onWindowFocusChanged(boolean hasFocus) {
785 super.onWindowFocusChanged(hasFocus);
786
787 if (hasFocus) {
788 final InputMethodManager inputManager = (InputMethodManager)
789 getSystemService(Context.INPUT_METHOD_SERVICE);
790 WindowManager.LayoutParams lp = getWindow().getAttributes();
791 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
792 android.os.Handler()) {
793 protected void onReceiveResult(int resultCode, Bundle resultData) {
794 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
795 }
796 });
797 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
798 }
799 }
800 */
801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 private boolean acceptFilter() {
803 final InputMethodManager inputManager = (InputMethodManager)
804 getSystemService(Context.INPUT_METHOD_SERVICE);
805 return !inputManager.isFullscreenMode();
806 }
807
808 @Override
809 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700810 final int uniChar = event.getUnicodeChar();
811 final boolean handled = super.onKeyDown(keyCode, event);
812 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
813 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
815 keyCode, event);
816 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700817 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700818 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700819 // showSearchDialog()
820 // If there are multiple keystrokes before the search dialog takes focus,
821 // onSearchRequested() will be called for every keystroke,
822 // but it is idempotent, so it's fine.
823 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 }
825 }
826
Joe Onorato8a9625e2010-01-28 15:55:35 -0800827 // Eat the long press event so the keyboard doesn't come up.
828 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
829 return true;
830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 return handled;
833 }
834
Karl Rosaen138a0412009-04-23 19:00:21 -0700835 private String getTypedText() {
836 return mDefaultKeySsb.toString();
837 }
838
839 private void clearTypedText() {
840 mDefaultKeySsb.clear();
841 mDefaultKeySsb.clearSpans();
842 Selection.setSelection(mDefaultKeySsb, 0);
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700846 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
847 * State
848 */
849 private static State intToState(int stateOrdinal) {
850 State state = State.WORKSPACE;
851 final State[] stateValues = State.values();
852 for (int i = 0; i < stateValues.length; i++) {
853 if (stateValues[i].ordinal() == stateOrdinal) {
854 state = stateValues[i];
855 break;
856 }
857 }
858 return state;
859 }
860
861 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 * Restores the previous state, if it exists.
863 *
864 * @param savedState The previous state.
865 */
866 private void restoreState(Bundle savedState) {
867 if (savedState == null) {
868 return;
869 }
870
Michael Jurka883f55b2010-10-21 15:47:14 -0700871 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700872 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700873 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800874 }
875
Winson Chungf0c6ae02012-03-21 16:10:31 -0700876 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700878 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
882 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700883
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
885 mPendingAddInfo.container = pendingAddContainer;
886 mPendingAddInfo.screen = pendingAddScreen;
887 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
888 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700889 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
890 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
891 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700892 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 mRestoring = true;
894 }
895
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
898 if (renameFolder) {
899 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700900 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 mRestoring = true;
902 }
Winson Chunga12a2502010-12-20 14:41:35 -0800903
Winson Chung785d2eb2011-04-14 16:08:02 -0700904
905 // Restore the AppsCustomize tab
906 if (mAppsCustomizeTabHost != null) {
907 String curTab = savedState.getString("apps_customize_currentTab");
908 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700909 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700910 mAppsCustomizeTabHost.getContentTypeForTabTag(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);
Winson Chung0e721a42012-08-02 17:40:30 -0700955
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700957 dragController.setDragScoller(mWorkspace);
958 dragController.setScrollView(mDragLayer);
959 dragController.setMoveTarget(mWorkspace);
960 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700961 if (mSearchDropTargetBar != null) {
962 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 /**
967 * Creates a view representing a shortcut.
968 *
969 * @param info The data structure describing the shortcut.
970 *
971 * @return A View inflated from R.layout.application.
972 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800973 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700975 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Creates a view representing a shortcut inflated from the specified resource.
980 *
981 * @param layoutResId The id of the XML layout used to create the shortcut.
982 * @param parent The group the shortcut belongs to.
983 * @param info The data structure describing the shortcut.
984 *
985 * @return A View inflated from layoutResId.
986 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800988 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
989 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 return favorite;
992 }
993
994 /**
995 * Add an application shortcut to the workspace.
996 *
997 * @param data The intent describing the application.
998 * @param cellInfo The position on screen where to create the shortcut.
999 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001001 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001003
Adam Cohenfbba09b2011-07-18 21:43:05 -07001004 // First we check if we already know the exact location where we want to add this item.
1005 if (cellX >= 0 && cellY >= 0) {
1006 cellXY[0] = cellX;
1007 cellXY[1] = cellY;
1008 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001009 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 return;
1011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001013 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014
Romain Guy73b979d2009-06-09 12:57:21 -07001015 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001018 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001020 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 } else {
1022 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024 }
Romain Guycbb89e42009-06-08 15:52:54 -07001025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
1027 * Add a shortcut to the workspace.
1028 *
1029 * @param data The intent describing the shortcut.
1030 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1033 int cellY) {
1034 int[] cellXY = mTmpAddItemCellCoordinates;
1035 int[] touchXY = mPendingAddInfo.dropPos;
1036 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001037
Michael Jurkad3ef3062010-11-23 16:23:58 -08001038 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001039
Adam Cohen558baaf2011-08-15 15:22:57 -07001040 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001041 if (info == null) {
1042 return;
1043 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001044 final View view = createShortcut(info);
1045
Adam Cohenfbba09b2011-07-18 21:43:05 -07001046 // First we check if we already know the exact location where we want to add this item.
1047 if (cellX >= 0 && cellY >= 0) {
1048 cellXY[0] = cellX;
1049 cellXY[1] = cellY;
1050 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001051
1052 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001053 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001054 true, null,null)) {
1055 return;
1056 }
1057 DragObject dragObject = new DragObject();
1058 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001059 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1060 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001061 return;
1062 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001063 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001064 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001066 foundCellSpan = (result != null);
1067 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001068 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001069 }
1070
1071 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001072 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 return;
1074 }
1075
Adam Cohen558baaf2011-08-15 15:22:57 -07001076 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
1078 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001079 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1080 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082 }
1083
Adam Cohen2f093b62012-04-30 18:59:53 -07001084 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1085 int minHeight) {
1086 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001087 // We want to account for the extra amount of padding that we are adding to the widget
1088 // to ensure that it gets the full amount of space that it has requested
1089 int requiredWidth = minWidth + padding.left + padding.right;
1090 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001091 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001092 }
1093
Adam Cohen2f093b62012-04-30 18:59:53 -07001094 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1095 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001096 }
1097
Adam Cohen2f093b62012-04-30 18:59:53 -07001098 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1099 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001100 }
1101
Adam Cohen2f093b62012-04-30 18:59:53 -07001102 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1103 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001104 }
1105
Adam Cohen2f093b62012-04-30 18:59:53 -07001106 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1107 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001108 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001109 }
1110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001114 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001117 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1118 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1119 if (appWidgetInfo == null) {
1120 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001124 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001125
Adam Cohen2f093b62012-04-30 18:59:53 -07001126 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1127 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001130 // We have saved the position to which the widget was dragged-- this really only matters
1131 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001132 int[] cellXY = mTmpAddItemCellCoordinates;
1133 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001134 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1137 cellXY[0] = mPendingAddInfo.cellX;
1138 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 spanXY[0] = mPendingAddInfo.spanX;
1140 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001141 foundCellSpan = true;
1142 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001144 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1146 spanXY[1], cellXY, finalSpan);
1147 spanXY[0] = finalSpan[0];
1148 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001152 }
1153
Michael Jurka0280c3b2010-09-17 15:00:07 -07001154 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001155 if (appWidgetId != -1) {
1156 // Deleting an app widget ID is a void call but writes to disk before returning
1157 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001158 new Thread("deleteAppWidgetId") {
1159 public void run() {
1160 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1161 }
1162 }.start();
1163 }
Winson Chung93eef082012-03-23 15:59:27 -07001164 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001165 return;
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001168 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001169 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1170 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001171 launcherInfo.spanX = spanXY[0];
1172 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1174 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001177 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
1179 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001180 if (hostView == null) {
1181 // Perform actual inflation because we're live
1182 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1183 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1184 } else {
1185 // The AppWidgetHostView has already been inflated and instantiated
1186 launcherInfo.hostView = hostView;
1187 }
Romain Guycbb89e42009-06-08 15:52:54 -07001188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001190 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001191 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001194
1195 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001197 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
Adam Cohended9f8d2010-11-03 13:25:16 -07001200 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1201 @Override
1202 public void onReceive(Context context, Intent intent) {
1203 final String action = intent.getAction();
1204 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1205 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001206 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001207 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001208
Winson Chungc100e8e2011-08-09 16:02:43 -07001209 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001210 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001211 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1212 mAppsCustomizeTabHost.reset();
1213 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001215 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1216 mUserPresent = true;
1217 updateRunning();
1218 }
1219 }
1220 };
1221
1222 @Override
1223 public void onAttachedToWindow() {
1224 super.onAttachedToWindow();
1225
1226 // Listen for broadcasts related to user-presence
1227 final IntentFilter filter = new IntentFilter();
1228 filter.addAction(Intent.ACTION_SCREEN_OFF);
1229 filter.addAction(Intent.ACTION_USER_PRESENT);
1230 registerReceiver(mReceiver, filter);
1231
Adam Cohend113e0c2010-11-11 10:48:05 -08001232 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 mVisible = true;
1234 }
1235
1236 @Override
1237 public void onDetachedFromWindow() {
1238 super.onDetachedFromWindow();
1239 mVisible = false;
1240
Adam Cohend113e0c2010-11-11 10:48:05 -08001241 if (mAttached) {
1242 unregisterReceiver(mReceiver);
1243 mAttached = false;
1244 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 updateRunning();
1246 }
1247
1248 public void onWindowVisibilityChanged(int visibility) {
1249 mVisible = visibility == View.VISIBLE;
1250 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001251 // The following code used to be in onResume, but it turns out onResume is called when
1252 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1253 // is a more appropriate event to handle
1254 if (mVisible) {
1255 mAppsCustomizeTabHost.onWindowVisible();
1256 if (!mWorkspaceLoading) {
1257 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001258 // We want to let Launcher draw itself at least once before we force it to build
1259 // layers on all the workspace pages, so that transitioning to Launcher from other
1260 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1261 // a true draw so we wait until the second preDraw call to be safe
1262 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001263 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001264 // We delay the layer building a bit in order to give
1265 // other message processing a time to run. In particular
1266 // this avoids a delay in hiding the IME if it was
1267 // currently shown, because doing that may involve
1268 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001269 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001270
Michael Jurka6ee21d22012-02-21 18:20:27 -08001271 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001272 return true;
1273 }
1274 });
1275 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001276 // When Launcher comes back to foreground, a different Activity might be responsible for
1277 // the app market intent, so refresh the icon
1278 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001279 clearTypedText();
1280 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 }
1282
1283 private void sendAdvanceMessage(long delay) {
1284 mHandler.removeMessages(ADVANCE_MSG);
1285 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1286 mHandler.sendMessageDelayed(msg, delay);
1287 mAutoAdvanceSentTime = System.currentTimeMillis();
1288 }
1289
1290 private void updateRunning() {
1291 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1292 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1293 mAutoAdvanceRunning = autoAdvanceRunning;
1294 if (autoAdvanceRunning) {
1295 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1296 sendAdvanceMessage(delay);
1297 } else {
1298 if (!mWidgetsToAdvance.isEmpty()) {
1299 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1300 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1301 }
1302 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001303 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 }
1305 }
1306 }
1307
1308 private final Handler mHandler = new Handler() {
1309 @Override
1310 public void handleMessage(Message msg) {
1311 if (msg.what == ADVANCE_MSG) {
1312 int i = 0;
1313 for (View key: mWidgetsToAdvance.keySet()) {
1314 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1315 final int delay = mAdvanceStagger * i;
1316 if (v instanceof Advanceable) {
1317 postDelayed(new Runnable() {
1318 public void run() {
1319 ((Advanceable) v).advance();
1320 }
1321 }, delay);
1322 }
1323 i++;
1324 }
1325 sendAdvanceMessage(mAdvanceInterval);
1326 }
1327 }
1328 };
1329
1330 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001331 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1333 if (v instanceof Advanceable) {
1334 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001335 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001336 updateRunning();
1337 }
1338 }
1339
1340 void removeWidgetToAutoAdvance(View hostView) {
1341 if (mWidgetsToAdvance.containsKey(hostView)) {
1342 mWidgetsToAdvance.remove(hostView);
1343 updateRunning();
1344 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 }
1346
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001348 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 launcherInfo.hostView = null;
1350 }
1351
Winson Chung93eef082012-03-23 15:59:27 -07001352 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1353 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1354 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 }
1356
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001357 public LauncherAppWidgetHost getAppWidgetHost() {
1358 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Winson Chunga9abd0e2010-10-27 17:18:37 -07001361 public LauncherModel getModel() {
1362 return mModel;
1363 }
1364
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001365 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001366 getWindow().closeAllPanels();
1367
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001368 // Whatever we were doing is hereby canceled.
1369 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001370 }
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 @Override
1373 protected void onNewIntent(Intent intent) {
1374 super.onNewIntent(intent);
1375
1376 // Close the menu
1377 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001378 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001379 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001380
Joe Onorato14f122b2009-11-19 14:06:36 -08001381 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1382 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001383
Adam Cohenac56cff2011-09-28 20:45:37 -07001384 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001385 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001386 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001387 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1388 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001389 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001390 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001391
1392 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001393 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001394
1395 // If we are already on home, then just animate back to the workspace, otherwise, just
1396 // wait until onResume to set the state back to Workspace
1397 if (alreadyOnHome) {
1398 showWorkspace(true);
1399 } else {
1400 mOnResumeState = State.WORKSPACE;
1401 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001402
Joe Onorato3a8820b2009-11-10 15:06:42 -08001403 final View v = getWindow().peekDecorView();
1404 if (v != null && v.getWindowToken() != null) {
1405 InputMethodManager imm = (InputMethodManager)getSystemService(
1406 INPUT_METHOD_SERVICE);
1407 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001409
Michael Jurka35aa14d2011-07-07 17:01:08 -07001410 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001411 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001412 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415 }
1416
1417 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001418 public void onRestoreInstanceState(Bundle state) {
1419 super.onRestoreInstanceState(state);
1420 for (int page: mSynchronouslyBoundPages) {
1421 mWorkspace.restoreInstanceStateForChild(page);
1422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424
1425 @Override
1426 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001427 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001428 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429
Michael Jurka883f55b2010-10-21 15:47:14 -07001430 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001431 // We close any open folder since it will not be re-opened, and we need to make sure
1432 // this state is reflected.
1433 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1436 mWaitingForResult) {
1437 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1438 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1439 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001441 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1442 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1443 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
1446 if (mFolderInfo != null && mWaitingForResult) {
1447 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1448 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1449 }
Michael Jurka946ad472010-07-09 18:05:18 -07001450
Winson Chung785d2eb2011-04-14 16:08:02 -07001451 // Save the current AppsCustomize tab
1452 if (mAppsCustomizeTabHost != null) {
1453 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1454 if (currentTabTag != null) {
1455 outState.putString("apps_customize_currentTab", currentTabTag);
1456 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001457 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1458 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
1462 @Override
1463 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Winson Chunge7a03942011-08-05 15:05:12 -07001466 // Remove all pending runnables
1467 mHandler.removeMessages(ADVANCE_MSG);
1468 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001469 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001470
Winson Chungcd2b0142011-06-08 16:02:26 -07001471 // Stop callbacks from LauncherModel
1472 LauncherApplication app = ((LauncherApplication) getApplication());
1473 mModel.stopLoader();
1474 app.setLauncher(null);
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001477 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001479 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001481 mAppWidgetHost = null;
1482
1483 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 TextKeyListener.getInstance().release();
1486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
Winson Chung3d503fb2011-07-13 17:25:49 -07001488 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001489
1490 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001491 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001492
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001493 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1495 mWorkspace.removeAllViews();
1496 mWorkspace = null;
1497 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001498
Michael Jurka2ecf9952012-06-18 12:52:28 -07001499 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
Adam Cohena9cf38f2011-05-02 15:36:58 -07001502 public DragController getDragController() {
1503 return mDragController;
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 @Override
1507 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001508 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 super.startActivityForResult(intent, requestCode);
1510 }
1511
Winson Chung70d72102011-08-12 11:24:35 -07001512 /**
1513 * Indicates that we want global search for this activity by setting the globalSearch
1514 * argument for {@link #startSearch} to true.
1515 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001517 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001519
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001520 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Karl Rosaen138a0412009-04-23 19:00:21 -07001522 if (initialQuery == null) {
1523 // Use any text typed in the launcher as the initial query
1524 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 if (appSearchData == null) {
1527 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001528 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001530 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Michael Jurkaa33411c2012-06-14 16:18:21 -07001532 startGlobalSearch(initialQuery, selectInitialQuery,
1533 appSearchData, sourceBounds);
1534 }
1535
1536 /**
1537 * Starts the global search activity. This code is a copied from SearchManager
1538 */
1539 public void startGlobalSearch(String initialQuery,
1540 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001541 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001542 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1543 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1544 if (globalSearchActivity == null) {
1545 Log.w(TAG, "No global search activity found.");
1546 return;
1547 }
1548 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1549 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1550 intent.setComponent(globalSearchActivity);
1551 // Make sure that we have a Bundle to put source in
1552 if (appSearchData == null) {
1553 appSearchData = new Bundle();
1554 } else {
1555 appSearchData = new Bundle(appSearchData);
1556 }
1557 // Set source to package name of app that starts global search, if not set already.
1558 if (!appSearchData.containsKey("source")) {
1559 appSearchData.putString("source", getPackageName());
1560 }
1561 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1562 if (!TextUtils.isEmpty(initialQuery)) {
1563 intent.putExtra(SearchManager.QUERY, initialQuery);
1564 }
1565 if (selectInitialQuery) {
1566 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1567 }
1568 intent.setSourceBounds(sourceBounds);
1569 try {
1570 startActivity(intent);
1571 } catch (ActivityNotFoundException ex) {
1572 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 }
1575
Winson Chung70d72102011-08-12 11:24:35 -07001576 @Override
1577 public boolean onCreateOptionsMenu(Menu menu) {
1578 if (isWorkspaceLocked()) {
1579 return false;
1580 }
1581
1582 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001583
1584 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1585 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1586 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001587 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1588 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1589 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001590 String helpUrl = getString(R.string.help_url);
1591 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001592 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1593 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1594
Winson Chung70d72102011-08-12 11:24:35 -07001595 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1596 .setIcon(android.R.drawable.ic_menu_gallery)
1597 .setAlphabeticShortcut('W');
1598 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1599 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001600 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001601 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001602 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1603 .setIcon(android.R.drawable.ic_menu_preferences)
1604 .setIntent(settings)
1605 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001606 if (!helpUrl.isEmpty()) {
1607 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1608 .setIcon(android.R.drawable.ic_menu_help)
1609 .setIntent(help)
1610 .setAlphabeticShortcut('H');
1611 }
Winson Chung70d72102011-08-12 11:24:35 -07001612 return true;
1613 }
1614
1615 @Override
1616 public boolean onPrepareOptionsMenu(Menu menu) {
1617 super.onPrepareOptionsMenu(menu);
1618
1619 if (mAppsCustomizeTabHost.isTransitioning()) {
1620 return false;
1621 }
1622 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1623 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1624
1625 return true;
1626 }
1627
1628 @Override
1629 public boolean onOptionsItemSelected(MenuItem item) {
1630 switch (item.getItemId()) {
1631 case MENU_WALLPAPER_SETTINGS:
1632 startWallpaper();
1633 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001634 }
1635
1636 return super.onOptionsItemSelected(item);
1637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001639 @Override
1640 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001641 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001642 // Use a custom animation for launching search
1643 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001644 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001645 }
1646
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647 public boolean isWorkspaceLocked() {
1648 return mWorkspaceLoading || mWaitingForResult;
1649 }
1650
Michael Jurka0280c3b2010-09-17 15:00:07 -07001651 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001652 mPendingAddInfo.container = ItemInfo.NO_ID;
1653 mPendingAddInfo.screen = -1;
1654 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1655 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001656 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001658 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001659
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001660 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1661 AppWidgetProviderInfo appWidgetInfo) {
1662 if (appWidgetInfo.configure != null) {
1663 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001664
Bjorn Bringert7984c942009-12-09 15:38:25 +00001665 // Launch over to configure widget, if needed
1666 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001667 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001668 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001669 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001671 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001672 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1673 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001674 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001675 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
1677 }
Romain Guycbb89e42009-06-08 15:52:54 -07001678
Adam Cohenfbba09b2011-07-18 21:43:05 -07001679 /**
1680 * Process a shortcut drop.
1681 *
1682 * @param componentName The name of the component
1683 * @param screen The screen where it should be added
1684 * @param cell The cell it should be added to, optional
1685 * @param position The location on the screen where it was dropped, optional
1686 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1688 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001689 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 mPendingAddInfo.container = container;
1691 mPendingAddInfo.screen = screen;
1692 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001693
1694 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001695 mPendingAddInfo.cellX = cell[0];
1696 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001697 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001698
1699 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1700 createShortcutIntent.setComponent(componentName);
1701 processShortcut(createShortcutIntent);
1702 }
1703
Adam Cohenfbba09b2011-07-18 21:43:05 -07001704 /**
1705 * Process a widget drop.
1706 *
1707 * @param info The PendingAppWidgetInfo of the widget being added.
1708 * @param screen The screen where it should be added
1709 * @param cell The cell it should be added to, optional
1710 * @param position The location on the screen where it was dropped, optional
1711 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001712 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001713 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001714 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001715 mPendingAddInfo.container = info.container = container;
1716 mPendingAddInfo.screen = info.screen = screen;
1717 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001718 mPendingAddInfo.minSpanX = info.minSpanX;
1719 mPendingAddInfo.minSpanY = info.minSpanY;
1720
Adam Cohenfbba09b2011-07-18 21:43:05 -07001721 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 mPendingAddInfo.cellX = cell[0];
1723 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001724 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001725 if (span != null) {
1726 mPendingAddInfo.spanX = span[0];
1727 mPendingAddInfo.spanY = span[1];
1728 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001729
Adam Cohened66b2b2012-01-23 17:28:51 -08001730 AppWidgetHostView hostView = info.boundWidget;
1731 int appWidgetId;
1732 if (hostView != null) {
1733 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001734 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001735 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001736 // In this case, we either need to start an activity to get permission to bind
1737 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001738 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001739 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001740 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001741 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001742 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001743 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1744 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1745 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1746 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1747 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001748 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001749 }
1750
Joe Onoratodeb98af2010-02-19 14:59:39 -08001751 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 // Handle case where user selected "Applications"
1753 String applicationName = getResources().getString(R.string.group_applications);
1754 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 if (applicationName != null && applicationName.equals(shortcutName)) {
1757 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1758 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001759
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1761 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001762 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001763 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001764 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001765 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
1768
Winson Chung24ab2f12010-09-16 14:10:47 -07001769 void processWallpaper(Intent intent) {
1770 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1771 }
1772
Winson Chung3d503fb2011-07-13 17:25:49 -07001773 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1774 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001775 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 folderInfo.title = getText(R.string.folder_name);
1777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001779 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1780 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001781 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782
1783 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001784 FolderIcon newFolder =
1785 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1786 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1787 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001788 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Joe Onorato9c1289c2009-08-17 11:03:03 -04001791 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001792 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001793 }
1794
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001796 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001798 Intent chooser = Intent.createChooser(pickWallpaper,
1799 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001800 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1801 // Removed in Eclair MR1
1802// WallpaperManager wm = (WallpaperManager)
1803// getSystemService(Context.WALLPAPER_SERVICE);
1804// WallpaperInfo wi = wm.getWallpaperInfo();
1805// if (wi != null && wi.getSettingsActivity() != null) {
1806// LabeledIntent li = new LabeledIntent(getPackageName(),
1807// R.string.configure_wallpaper, 0);
1808// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1809// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1810// }
Mike Clerona0618e42009-10-22 13:55:21 -07001811 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001814 /**
1815 * Registers various content observers. The current implementation registers
1816 * only a favorites observer to keep track of the favorites applications.
1817 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001818 private void registerContentObservers() {
1819 ContentResolver resolver = getContentResolver();
1820 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1821 true, mWidgetObserver);
1822 }
1823
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 @Override
1825 public boolean dispatchKeyEvent(KeyEvent event) {
1826 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1827 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001829 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001830 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001831 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001832 dumpState();
1833 return true;
1834 }
1835 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001836 }
1837 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1838 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001839 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 return true;
1841 }
1842 }
1843
1844 return super.dispatchKeyEvent(event);
1845 }
1846
Joe Onorato88ec0992009-11-19 13:16:06 -08001847 @Override
1848 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001849 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001850 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001851 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001852 Folder openFolder = mWorkspace.getOpenFolder();
1853 if (openFolder.isEditingName()) {
1854 openFolder.dismissEditingName();
1855 } else {
1856 closeFolder();
1857 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001858 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001859 mWorkspace.exitWidgetResizeMode();
1860
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001861 // Back button is a no-op here, but give at least some feedback for the button press
1862 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001863 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001867 * Re-listen when widgets are reset.
1868 */
1869 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001870 if (mAppWidgetHost != null) {
1871 mAppWidgetHost.startListening();
1872 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001873 }
1874
1875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1877 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001879 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001880 if (mModel != null) {
1881 mModel.unbindWorkspaceItems();
1882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 /**
1886 * Launches the intent referred by the clicked shortcut.
1887 *
1888 * @param v The view representing the clicked shortcut.
1889 */
1890 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001891 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1892 // view has detached (it's possible for this to happen if the view is removed mid touch).
1893 if (v.getWindowToken() == null) {
1894 return;
1895 }
1896
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001897 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001898 return;
1899 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001902 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001904 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001905 int[] pos = new int[2];
1906 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001907 intent.setSourceBounds(new Rect(pos[0], pos[1],
1908 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001909
1910 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001911
1912 if (success && v instanceof BubbleTextView) {
1913 mWaitingForResume = (BubbleTextView) v;
1914 mWaitingForResume.setStayPressed(true);
1915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001917 if (v instanceof FolderIcon) {
1918 FolderIcon fi = (FolderIcon) v;
1919 handleFolderClick(fi);
1920 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001921 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001922 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001923 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001924 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001925 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
1928 }
1929
Michael Jurka0e260592010-06-30 17:07:39 -07001930 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001931 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 // clicking anywhere on the workspace causes the customization drawer to slide down
1933 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001934 return false;
1935 }
1936
Michael Jurkaaf442092010-06-10 17:01:57 -07001937 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001938 * Event handler for the search button
1939 *
1940 * @param v The view that was clicked.
1941 */
1942 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001943 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1944
Amith Yamasania135ba82011-08-09 17:42:01 -07001945 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001946 }
1947
1948 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001949 * Event handler for the voice button
1950 *
1951 * @param v The view that was clicked.
1952 */
1953 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001954 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001955
Michael Jurkaae65ee32012-04-30 11:45:54 -07001956 try {
1957 final SearchManager searchManager =
1958 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1959 ComponentName activityName = searchManager.getGlobalSearchActivity();
1960 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001961 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001962 if (activityName != null) {
1963 intent.setPackage(activityName.getPackageName());
1964 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001965 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001966 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001967 } catch (ActivityNotFoundException e) {
1968 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001969 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001970 startActivitySafely(null, intent, "onClickVoiceButton");
1971 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001972 }
1973
1974 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001975 * Event handler for the "grid" button that appears on the home screen, which
1976 * enters all apps mode.
1977 *
1978 * @param v The view that was clicked.
1979 */
1980 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001981 showAllApps(true);
1982 }
1983
1984 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001985 // Provide the same haptic feedback that the system offers for virtual keys.
1986 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001987 }
1988
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001989 public void onClickAppMarketButton(View v) {
1990 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001991 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001992 } else {
1993 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001994 }
1995 }
1996
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001997 void startApplicationDetailsActivity(ComponentName componentName) {
1998 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001999 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2000 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002001 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002002 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002003 }
2004
Patrick Dubroy5539af72010-09-07 15:22:01 -07002005 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2006 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2007 // System applications cannot be installed. For now, show a toast explaining that.
2008 // We may give them the option of disabling apps this way.
2009 int messageId = R.string.uninstall_system_app_text;
2010 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2011 } else {
2012 String packageName = appInfo.componentName.getPackageName();
2013 String className = appInfo.componentName.getClassName();
2014 Intent intent = new Intent(
2015 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002016 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2017 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002018 startActivity(intent);
2019 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002020 }
2021
Michael Jurka86a720e2012-05-09 11:23:23 -07002022 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002026 // Only launch using the new animation if the shortcut has not opted out (this is a
2027 // private contract between launcher and may be ignored in the future).
2028 boolean useLaunchAnimation = (v != null) &&
2029 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2030 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002031 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2032 v.getMeasuredWidth(), v.getMeasuredHeight());
2033
2034 startActivity(intent, opts.toBundle());
2035 } else {
2036 startActivity(intent);
2037 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002038 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } catch (SecurityException e) {
2040 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002041 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002043 "or use the exported attribute for this activity. "
2044 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002046 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002048
Michael Jurka86a720e2012-05-09 11:23:23 -07002049 boolean startActivitySafely(View v, Intent intent, Object tag) {
2050 boolean success = false;
2051 try {
2052 success = startActivity(v, intent, tag);
2053 } catch (ActivityNotFoundException e) {
2054 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2055 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2056 }
2057 return success;
2058 }
2059
Romain Guy8e633c52010-03-04 12:51:36 -08002060 void startActivityForResultSafely(Intent intent, int requestCode) {
2061 try {
2062 startActivityForResult(intent, requestCode);
2063 } catch (ActivityNotFoundException e) {
2064 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2065 } catch (SecurityException e) {
2066 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2067 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2068 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2069 "or use the exported attribute for this activity.", e);
2070 }
2071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072
Adam Cohena9cf38f2011-05-02 15:36:58 -07002073 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002074 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002075 Folder openFolder = mWorkspace.getFolderForTag(info);
2076
2077 // If the folder info reports that the associated folder is open, then verify that
2078 // it is actually opened. There have been a few instances where this gets out of sync.
2079 if (info.opened && openFolder == null) {
2080 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2081 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2082 info.opened = false;
2083 }
2084
Adam Cohenfb91f302012-06-11 15:45:18 -07002085 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 // Close any open folder
2087 closeFolder();
2088 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002089 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 } else {
2091 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 int folderScreen;
2093 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002094 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 // .. and close it
2096 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002097 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 // Close any folder open on the current screen
2099 closeFolder();
2100 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002101 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103 }
2104 }
2105 }
2106
Adam Cohen268c4752012-06-06 17:47:33 -07002107 /**
2108 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2109 * in the DragLayer in the exact absolute location of the original FolderIcon.
2110 */
2111 private void copyFolderIconToImage(FolderIcon fi) {
2112 final int width = fi.getMeasuredWidth();
2113 final int height = fi.getMeasuredHeight();
2114
2115 // Lazy load ImageView, Bitmap and Canvas
2116 if (mFolderIconImageView == null) {
2117 mFolderIconImageView = new ImageView(this);
2118 }
2119 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2120 mFolderIconBitmap.getHeight() != height) {
2121 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2122 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2123 }
2124
2125 DragLayer.LayoutParams lp;
2126 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2127 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2128 } else {
2129 lp = new DragLayer.LayoutParams(width, height);
2130 }
2131
Adam Cohen307fe232012-08-16 17:55:58 -07002132 // The layout from which the folder is being opened may be scaled, adjust the starting
2133 // view size by this scale factor.
2134 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002135 lp.customPosition = true;
2136 lp.x = mRectForFolderAnimation.left;
2137 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002138 lp.width = (int) (scale * width);
2139 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002140
2141 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2142 fi.draw(mFolderIconCanvas);
2143 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002144 if (fi.getFolder() != null) {
2145 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2146 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002147 }
Adam Cohen268c4752012-06-06 17:47:33 -07002148 // Just in case this image view is still in the drag layer from a previous animation,
2149 // we remove it and re-add it.
2150 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2151 mDragLayer.removeView(mFolderIconImageView);
2152 }
2153 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002154 if (fi.getFolder() != null) {
2155 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002156 }
Adam Cohen268c4752012-06-06 17:47:33 -07002157 }
2158
Adam Cohen2801caf2011-05-13 20:57:39 -07002159 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002160 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002161 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2162 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2163 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2164
Adam Cohenc51934b2011-07-26 21:07:43 -07002165 FolderInfo info = (FolderInfo) fi.getTag();
2166 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2167 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002168 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2169 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002170 }
2171
Adam Cohen268c4752012-06-06 17:47:33 -07002172 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2173 copyFolderIconToImage(fi);
2174 fi.setVisibility(View.INVISIBLE);
2175
Michael Jurka2ecf9952012-06-18 12:52:28 -07002176 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002177 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002178 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2179 oa.start();
2180 }
2181
Adam Cohen268c4752012-06-06 17:47:33 -07002182 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002183 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002184 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2185 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2186 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2187
Adam Cohen268c4752012-06-06 17:47:33 -07002188 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002189
Adam Cohen268c4752012-06-06 17:47:33 -07002190 // We remove and re-draw the FolderIcon in-case it has changed
2191 mDragLayer.removeView(mFolderIconImageView);
2192 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002193 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002194 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002195 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002196 oa.addListener(new AnimatorListenerAdapter() {
2197 @Override
2198 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002199 if (cl != null) {
2200 cl.clearFolderLeaveBehind();
2201 // Remove the ImageView copy of the FolderIcon and make the original visible.
2202 mDragLayer.removeView(mFolderIconImageView);
2203 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002204 }
2205 }
2206 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002207 oa.start();
2208 }
2209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002211 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 * is animated relative to the specified View. If the View is null, no animation
2213 * is played.
2214 *
2215 * @param folderInfo The FolderInfo describing the folder to open.
2216 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002217 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002218 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002219 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220
Adam Cohena9cf38f2011-05-02 15:36:58 -07002221 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222
Adam Cohen4554ee12011-08-03 16:13:21 -07002223 // Just verify that the folder hasn't already been added to the DragLayer.
2224 // There was a one-off crash where the folder had a parent already.
2225 if (folder.getParent() == null) {
2226 mDragLayer.addView(folder);
2227 mDragController.addDropTarget((DropTarget) folder);
2228 } else {
2229 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2230 folder.getParent() + ").");
2231 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002232 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002233 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002234 }
2235
2236 public void closeFolder() {
2237 Folder folder = mWorkspace.getOpenFolder();
2238 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002239 if (folder.isEditingName()) {
2240 folder.dismissEditingName();
2241 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002242 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002243
2244 // Dismiss the folder cling
2245 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002246 }
2247 }
2248
2249 void closeFolder(Folder folder) {
2250 folder.getInfo().opened = false;
2251
2252 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2253 if (parent != null) {
2254 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2255 shrinkAndFadeInFolderIcon(fi);
2256 }
2257 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
2259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002261 if (!isDraggingEnabled()) return false;
2262 if (isWorkspaceLocked()) return false;
2263 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264
2265 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002266 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 resetAddInfo();
2270 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002272 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 return true;
2274 }
2275
Winson Chung3d503fb2011-07-13 17:25:49 -07002276 // The hotseat touch handling does not go through Workspace, and we always allow long press
2277 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002278 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2280 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 if (itemUnderLongClick == null) {
2282 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002283 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2284 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002285 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002287 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 }
2291 }
2292 }
2293 return true;
2294 }
2295
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 boolean isHotseatLayout(View layout) {
2297 return mHotseat != null && layout != null &&
2298 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2299 }
2300 Hotseat getHotseat() {
2301 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002302 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002303 SearchDropTargetBar getSearchBar() {
2304 return mSearchDropTargetBar;
2305 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002306
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 /**
2308 * Returns the CellLayout of the specified container at the specified screen.
2309 */
2310 CellLayout getCellLayout(long container, int screen) {
2311 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2312 if (mHotseat != null) {
2313 return mHotseat.getLayout();
2314 } else {
2315 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 } else {
2318 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002319 }
2320 }
2321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 Workspace getWorkspace() {
2323 return mWorkspace;
2324 }
2325
Daniel Sandler843e8602010-06-07 14:59:01 -04002326 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2327 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002328 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002329 }
2330
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002331 public boolean isAllAppsButtonRank(int rank) {
2332 return mHotseat.isAllAppsButtonRank(rank);
2333 }
2334
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002335 /**
2336 * Helper method for the cameraZoomIn/cameraZoomOut animations
2337 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002338 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002339 * @param scaleFactor The scale factor used for the zoom
2340 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002341 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002343 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002344 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002345
Winson Chung18f41f82012-05-09 13:28:10 -07002346 void disableWallpaperIfInAllApps() {
2347 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002348 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002349 if (mAppsCustomizeTabHost != null &&
2350 !mAppsCustomizeTabHost.isTransitioning()) {
2351 updateWallpaperVisibility(false);
2352 }
2353 }
2354 }
2355
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002356 void updateWallpaperVisibility(boolean visible) {
2357 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2358 int curflags = getWindow().getAttributes().flags
2359 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2360 if (wpflags != curflags) {
2361 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2362 }
2363 }
2364
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002365 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2366 if (v instanceof LauncherTransitionable) {
2367 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2368 }
2369 }
2370
Michael Jurkabed61d22012-02-14 22:51:29 -08002371 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2372 if (v instanceof LauncherTransitionable) {
2373 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2374 }
Winson Chung70442722012-02-10 15:43:22 -08002375
2376 // Update the workspace transition step as well
2377 dispatchOnLauncherTransitionStep(v, 0f);
2378 }
2379
2380 private void dispatchOnLauncherTransitionStep(View v, float t) {
2381 if (v instanceof LauncherTransitionable) {
2382 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2383 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002384 }
2385
2386 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2387 if (v instanceof LauncherTransitionable) {
2388 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2389 }
Winson Chung70442722012-02-10 15:43:22 -08002390
2391 // Update the workspace transition step as well
2392 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002393 }
2394
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002395 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002396 * Things to test when changing the following seven functions.
2397 * - Home from workspace
2398 * - from center screen
2399 * - from other screens
2400 * - Home from all apps
2401 * - from center screen
2402 * - from other screens
2403 * - Back from all apps
2404 * - from center screen
2405 * - from other screens
2406 * - Launch app from workspace and quit
2407 * - with back
2408 * - with home
2409 * - Launch app from all apps and quit
2410 * - with back
2411 * - with home
2412 * - Go to a screen that's not the default, then all
2413 * apps, and launch and app, and go back
2414 * - with back
2415 * -with home
2416 * - On workspace, long press power and go back
2417 * - with back
2418 * - with home
2419 * - On all apps, long press power and go back
2420 * - with back
2421 * - with home
2422 * - On workspace, power off
2423 * - On all apps, power off
2424 * - Launch an app and turn off the screen while in that app
2425 * - Go back with home key
2426 * - Go back with back key TODO: make this not go to workspace
2427 * - From all apps
2428 * - From workspace
2429 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2430 * - From all apps
2431 * - From the center workspace
2432 * - From another workspace
2433 */
2434
2435 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 * Zoom the camera out from the workspace to reveal 'toView'.
2437 * Assumes that the view to show is anchored at either the very top or very bottom
2438 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002439 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002440 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002441 if (mStateAnimation != null) {
2442 mStateAnimation.cancel();
2443 mStateAnimation = null;
2444 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002446
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2448 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2449 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002450 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002451 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002452 final int startDelay =
2453 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002454
Michael Jurkab3e22d92011-10-31 15:58:33 -07002455 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456
Michael Jurkad74c9842011-07-10 12:44:21 -07002457 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002458 Animator workspaceAnim =
2459 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002460
2461 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002462 toView.setScaleX(scale);
2463 toView.setScaleY(scale);
2464 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2465 scaleAnim.
2466 scaleX(1f).scaleY(1f).
2467 setDuration(duration).
2468 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002469
Winson Chungf0ea4d32011-06-06 14:27:16 -07002470 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002471 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002472 final ObjectAnimator alphaAnim = ObjectAnimator
2473 .ofFloat(toView, "alpha", 0f, 1f)
2474 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002475 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002476 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2477 @Override
2478 public void onAnimationUpdate(ValueAnimator animation) {
2479 float t = (Float) animation.getAnimatedValue();
2480 dispatchOnLauncherTransitionStep(fromView, t);
2481 dispatchOnLauncherTransitionStep(toView, t);
2482 }
2483 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002484
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002485 // toView should appear right at the end of the workspace shrink
2486 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002487 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002488 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002489 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002490
2491 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002492 boolean animationCancelled = false;
2493
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002494 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002495 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002496 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002497 // Prepare the position
2498 toView.setTranslationX(0.0f);
2499 toView.setTranslationY(0.0f);
2500 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002501 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002502 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002503 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002504 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002505 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2506 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002507
2508 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2509 // Hide the workspace scrollbar
2510 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002511 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002512 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002513 if (!animationCancelled) {
2514 updateWallpaperVisibility(false);
2515 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002516
2517 // Hide the search bar
2518 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002519 }
2520
Adam Cohencff6af82011-09-13 14:51:53 -07002521 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002522 public void onAnimationCancel(Animator animation) {
2523 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002524 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 });
2526
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002527 if (workspaceAnim != null) {
2528 mStateAnimation.play(workspaceAnim);
2529 }
Michael Jurka1899a362011-11-03 13:50:45 -07002530
2531 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002532 final ViewTreeObserver observer;
2533
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002534 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2535 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002536
2537 // If any of the objects being animated haven't been measured/laid out
2538 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002539 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002540 (mWorkspace.getMeasuredWidth() == 0) ||
2541 (toView.getMeasuredWidth() == 0)) {
2542 observer = mWorkspace.getViewTreeObserver();
2543 delayAnim = true;
2544 } else {
2545 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002546 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002547
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002548 final AnimatorSet stateAnimation = mStateAnimation;
2549 final Runnable startAnimRunnable = new Runnable() {
2550 public void run() {
2551 // Check that mStateAnimation hasn't changed while
2552 // we waited for a layout/draw pass
2553 if (mStateAnimation != stateAnimation)
2554 return;
2555 setPivotsForZoom(toView, scale);
2556 dispatchOnLauncherTransitionStart(fromView, animated, false);
2557 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002558 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002559 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 mStateAnimation.start();
2565 }
2566 });
2567 }
2568 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002569 if (delayAnim) {
2570 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2571 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002572 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002573 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002574 }
2575 };
2576 observer.addOnGlobalLayoutListener(delayedStart);
2577 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002578 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002579 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 } else {
2581 toView.setTranslationX(0.0f);
2582 toView.setTranslationY(0.0f);
2583 toView.setScaleX(1.0f);
2584 toView.setScaleY(1.0f);
2585 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002586 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002587
Michael Jurkabed61d22012-02-14 22:51:29 -08002588 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2589 // Hide the workspace scrollbar
2590 mWorkspace.hideScrollingIndicator(true);
2591 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002592
2593 // Hide the search bar
2594 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002595 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002596 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002597 dispatchOnLauncherTransitionStart(fromView, animated, false);
2598 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002599 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002600 dispatchOnLauncherTransitionStart(toView, animated, false);
2601 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002602 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002603 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002604 }
2605
2606 /**
2607 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002610 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002611 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2612 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002613
Michael Jurkab3e22d92011-10-31 15:58:33 -07002614 if (mStateAnimation != null) {
2615 mStateAnimation.cancel();
2616 mStateAnimation = null;
2617 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002618 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002619
Winson Chungf0ea4d32011-06-06 14:27:16 -07002620 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002621 final int fadeOutDuration =
2622 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 final float scaleFactor = (float)
2624 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2625 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002626 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002627 Animator workspaceAnim = null;
2628
2629 if (toState == State.WORKSPACE) {
2630 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2631 workspaceAnim = mWorkspace.getChangeStateAnimation(
2632 Workspace.State.NORMAL, animated, stagger);
2633 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2634 workspaceAnim = mWorkspace.getChangeStateAnimation(
2635 Workspace.State.SPRING_LOADED, animated);
2636 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002637
Michael Jurkab3e22d92011-10-31 15:58:33 -07002638 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002639 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002640 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002641 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002642 final LauncherViewPropertyAnimator scaleAnim =
2643 new LauncherViewPropertyAnimator(fromView);
2644 scaleAnim.
2645 scaleX(scaleFactor).scaleY(scaleFactor).
2646 setDuration(duration).
2647 setInterpolator(new Workspace.ZoomInInterpolator());
2648
2649 final ObjectAnimator alphaAnim = ObjectAnimator
2650 .ofFloat(fromView, "alpha", 1f, 0f)
2651 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002652 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002653 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2654 @Override
2655 public void onAnimationUpdate(ValueAnimator animation) {
2656 float t = 1f - (Float) animation.getAnimatedValue();
2657 dispatchOnLauncherTransitionStep(fromView, t);
2658 dispatchOnLauncherTransitionStep(toView, t);
2659 }
2660 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002661
Michael Jurka2ecf9952012-06-18 12:52:28 -07002662 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002663
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002664 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2665 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002666
2667 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002668 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002669 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002670 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002671 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002672 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2673 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002674 if (mWorkspace != null) {
2675 mWorkspace.hideScrollingIndicator(false);
2676 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002677 if (onCompleteRunnable != null) {
2678 onCompleteRunnable.run();
2679 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002680 }
2681 });
2682
Winson Chungf0ea4d32011-06-06 14:27:16 -07002683 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002684 if (workspaceAnim != null) {
2685 mStateAnimation.play(workspaceAnim);
2686 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002687 dispatchOnLauncherTransitionStart(fromView, animated, true);
2688 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002689 final Animator stateAnimation = mStateAnimation;
2690 mWorkspace.post(new Runnable() {
2691 public void run() {
2692 if (stateAnimation != mStateAnimation)
2693 return;
2694 mStateAnimation.start();
2695 }
2696 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002697 } else {
2698 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002699 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002700 dispatchOnLauncherTransitionStart(fromView, animated, true);
2701 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002702 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002703 dispatchOnLauncherTransitionStart(toView, animated, true);
2704 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002705 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002706 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002707 }
2708
Michael Jurkae326f182011-11-21 14:05:46 -08002709 @Override
2710 public void onTrimMemory(int level) {
2711 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002712 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002713 mAppsCustomizeTabHost.onTrimMemory();
2714 }
2715 }
2716
Winson Chung18f41f82012-05-09 13:28:10 -07002717 @Override
2718 public void onWindowFocusChanged(boolean hasFocus) {
2719 if (!hasFocus) {
2720 // When another window occludes launcher (like the notification shade, or recents),
2721 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2722 updateWallpaperVisibility(true);
2723 } else {
2724 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002725 mWorkspace.postDelayed(new Runnable() {
2726 @Override
2727 public void run() {
2728 disableWallpaperIfInAllApps();
2729 }
2730 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002731 }
2732 }
2733
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002734 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002735 showWorkspace(animated, null);
2736 }
2737
2738 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002739 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002740 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002741 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002742 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002743
Winson Chungc7d2b602012-05-16 17:02:20 -07002744 // Show the search bar (only animate if we were showing the drop target bar in spring
2745 // loaded mode)
2746 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2747
Michael Jurkab737ee62011-11-15 15:57:22 -08002748 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002749 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750
2751 // Set focus to the AppsCustomize button
2752 if (mAllAppsButton != null) {
2753 mAllAppsButton.requestFocus();
2754 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002755 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002756
Michael Jurkab737ee62011-11-15 15:57:22 -08002757 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002758
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002759 // Change the state *after* we've called all the transition code
2760 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002761
Winson Chung5fb63472011-02-02 17:03:37 -08002762 // Resume the auto-advance of widgets
2763 mUserPresent = true;
2764 updateRunning();
2765
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002766 // send an accessibility event to announce the context change
2767 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002768 }
2769
Michael Jurkab3e22d92011-10-31 15:58:33 -07002770 void showAllApps(boolean animated) {
2771 if (mState != State.WORKSPACE) return;
2772
2773 showAppsCustomizeHelper(animated, false);
2774 mAppsCustomizeTabHost.requestFocus();
2775
Michael Jurkab3e22d92011-10-31 15:58:33 -07002776 // Change the state *after* we've called all the transition code
2777 mState = State.APPS_CUSTOMIZE;
2778
2779 // Pause the auto-advance of widgets until we are out of AllApps
2780 mUserPresent = false;
2781 updateRunning();
2782 closeFolder();
2783
2784 // Send an accessibility event to announce the context change
2785 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2786 }
2787
Adam Cohen7777d962011-08-18 18:58:38 -07002788 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002789 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002790 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002791 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002792 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002793 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002794 }
Adam Cohen7777d962011-08-18 18:58:38 -07002795
Adam Cohened66b2b2012-01-23 17:28:51 -08002796 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2797 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002798 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2799
Winson Chunge7a03942011-08-05 15:05:12 -07002800 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002801 @Override
2802 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002803 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002804 // Before we show workspace, hide all apps again because
2805 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2806 // clean up our state transition functions
2807 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002808 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002809 } else {
2810 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002811 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002812 }
2813 }, (extendedDelay ?
2814 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2815 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2816 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002817
Michael Jurkad3ef3062010-11-23 16:23:58 -08002818 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002819 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002820 final boolean animated = true;
2821 final boolean springLoaded = true;
2822 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002823 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002824 }
2825 // Otherwise, we are not in spring loaded mode, so don't do anything.
2826 }
2827
Michael Jurkab737ee62011-11-15 15:57:22 -08002828 void hideDockDivider() {
2829 if (mQsbDivider != null && mDockDivider != null) {
2830 mQsbDivider.setVisibility(View.INVISIBLE);
2831 mDockDivider.setVisibility(View.INVISIBLE);
2832 }
2833 }
2834
2835 void showDockDivider(boolean animated) {
2836 if (mQsbDivider != null && mDockDivider != null) {
2837 mQsbDivider.setVisibility(View.VISIBLE);
2838 mDockDivider.setVisibility(View.VISIBLE);
2839 if (mDividerAnimator != null) {
2840 mDividerAnimator.cancel();
2841 mQsbDivider.setAlpha(1f);
2842 mDockDivider.setAlpha(1f);
2843 mDividerAnimator = null;
2844 }
2845 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002846 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2847 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2848 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Michael Jurkab737ee62011-11-15 15:57:22 -08002849 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2850 mDividerAnimator.start();
2851 }
2852 }
2853 }
2854
Michael Jurkab3e22d92011-10-31 15:58:33 -07002855 void lockAllApps() {
2856 // TODO
2857 }
2858
2859 void unlockAllApps() {
2860 // TODO
2861 }
2862
Winson Chungf0ea4d32011-06-06 14:27:16 -07002863 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002864 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002865 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002866 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002867 if (!LauncherApplication.isScreenLarge()) {
2868 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002869 if (mHotseat.getAlpha() != 1f) {
2870 int duration = mSearchDropTargetBar.getTransitionInDuration();
2871 mHotseat.animate().alpha(1f).setDuration(duration);
2872 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002873 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002874 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002875 }
2876 }
2877 }
2878
2879 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002880 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002881 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002882 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002883 if (!LauncherApplication.isScreenLarge()) {
2884 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002885 if (mHotseat.getAlpha() != 0f) {
2886 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2887 mHotseat.animate().alpha(0f).setDuration(duration);
2888 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002892 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002893 }
2894
Patrick Dubroy5f445422011-02-18 14:35:21 -08002895 /**
2896 * Add an item from all apps or customize onto the given workspace screen.
2897 * If layout is null, add to the current screen.
2898 */
2899 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002900 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002901 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002902 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002903 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002904
Winson Chungdff8ebb2011-09-08 17:25:31 -07002905 /** Maps the current orientation to an index for referencing orientation correct global icons */
2906 private int getCurrentOrientationIndexForGlobalIcons() {
2907 // default - 0, landscape - 1
2908 switch (getResources().getConfiguration().orientation) {
2909 case Configuration.ORIENTATION_LANDSCAPE:
2910 return 1;
2911 default:
2912 return 0;
2913 }
2914 }
2915
Michael Jurkaae65ee32012-04-30 11:45:54 -07002916 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002917 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002918 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002919 // Look for the toolbar icon specified in the activity meta-data
2920 Bundle metaData = packageManager.getActivityInfo(
2921 activityName, PackageManager.GET_META_DATA).metaData;
2922 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002923 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002924 if (iconResId != 0) {
2925 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002926 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002927 }
2928 }
2929 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002930 // This can happen if the activity defines an invalid drawable
2931 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2932 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002933 } catch (Resources.NotFoundException nfe) {
2934 // This can happen if the activity defines an invalid drawable
2935 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2936 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002937 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002938 return null;
2939 }
2940
2941 // if successful in getting icon, return it; otherwise, set button to use default drawable
2942 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002943 int buttonId, ComponentName activityName, int fallbackDrawableId,
2944 String toolbarResourceName) {
2945 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002946 Resources r = getResources();
2947 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2948 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002949
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002950 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002951 // If we were unable to find the icon via the meta-data, use a generic one
2952 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002953 toolbarIcon = r.getDrawable(fallbackDrawableId);
2954 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002955 if (button != null) {
2956 button.setCompoundDrawables(toolbarIcon, null, null, null);
2957 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002958 return null;
2959 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002960 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002961 if (button != null) {
2962 button.setCompoundDrawables(toolbarIcon, null, null, null);
2963 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002964 return toolbarIcon.getConstantState();
2965 }
2966 }
2967
2968 // if successful in getting icon, return it; otherwise, set button to use default drawable
2969 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002970 int buttonId, ComponentName activityName, int fallbackDrawableId,
2971 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002972 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002973 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002974
Michael Jurka19e0fc52011-07-22 18:00:21 -07002975 if (button != null) {
2976 // If we were unable to find the icon via the meta-data, use a
2977 // generic one
2978 if (toolbarIcon == null) {
2979 button.setImageResource(fallbackDrawableId);
2980 } else {
2981 button.setImageDrawable(toolbarIcon);
2982 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002983 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002984
2985 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2986
Michael Jurka0423dcf2010-10-05 14:56:18 -07002987 }
2988
Winson Chung1b884092012-06-08 17:13:02 -07002989 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002990 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002991 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002992 }
2993
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002994 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002995 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002996 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002997 }
2998
Winson Chungbb185bd2011-11-21 12:31:42 -08002999 private void invalidatePressedFocusedStates(View container, View button) {
3000 if (container instanceof HolographicLinearLayout) {
3001 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3002 layout.invalidatePressedFocusedStates();
3003 } else if (button instanceof HolographicImageView) {
3004 HolographicImageView view = (HolographicImageView) button;
3005 view.invalidatePressedFocusedStates();
3006 }
3007 }
3008
Winson Chungc51db6a2011-10-05 11:44:49 -07003009 private boolean updateGlobalSearchIcon() {
3010 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003011 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003012 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003013 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003014 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003015
Winson Chung1cad91e2011-05-25 17:41:01 -07003016 final SearchManager searchManager =
3017 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3018 ComponentName activityName = searchManager.getGlobalSearchActivity();
3019 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003020 int coi = getCurrentOrientationIndexForGlobalIcons();
3021 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003022 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3023 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3024 if (sGlobalSearchIcon[coi] == null) {
3025 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3026 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3027 TOOLBAR_ICON_METADATA_NAME);
3028 }
3029
Winson Chungc51db6a2011-10-05 11:44:49 -07003030 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3031 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003032 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003033 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003034 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003035 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003036 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3037 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3038 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003039 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003040 if (voiceButtonProxy != null) {
3041 voiceButtonProxy.setVisibility(View.GONE);
3042 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003043 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003044 }
3045 }
3046
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003047 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003048 final View searchButtonContainer = findViewById(R.id.search_button_container);
3049 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003050 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003051 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003052 }
3053
Winson Chungc51db6a2011-10-05 11:44:49 -07003054 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003055 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003056 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003057 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003058
Winson Chungc51db6a2011-10-05 11:44:49 -07003059 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003060 final SearchManager searchManager =
3061 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3062 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3063
3064 ComponentName activityName = null;
3065 if (globalSearchActivity != null) {
3066 // Check if the global search activity handles voice search
3067 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3068 intent.setPackage(globalSearchActivity.getPackageName());
3069 activityName = intent.resolveActivity(getPackageManager());
3070 }
3071
3072 if (activityName == null) {
3073 // Fallback: check if an activity other than the global search activity
3074 // resolves this
3075 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3076 activityName = intent.resolveActivity(getPackageManager());
3077 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003078 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003079 int coi = getCurrentOrientationIndexForGlobalIcons();
3080 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003081 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3082 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3083 if (sVoiceSearchIcon[coi] == null) {
3084 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3085 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3086 TOOLBAR_ICON_METADATA_NAME);
3087 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003088 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003089 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003090 if (voiceButtonProxy != null) {
3091 voiceButtonProxy.setVisibility(View.VISIBLE);
3092 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003093 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003094 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003095 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003096 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003097 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003098 if (voiceButtonProxy != null) {
3099 voiceButtonProxy.setVisibility(View.GONE);
3100 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003101 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003102 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003103 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003105 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003106 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3107 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003108 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003109 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003110 }
3111
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003112 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003113 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003114 */
3115 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003116 final View marketButton = findViewById(R.id.market_button);
3117 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3118 // Find the app market activity by resolving an intent.
3119 // (If multiple app markets are installed, it will return the ResolverActivity.)
3120 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003121 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003122 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003123 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003124 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003125 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3126 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003127 marketButton.setVisibility(View.VISIBLE);
3128 } else {
3129 // We should hide and disable the view so that we don't try and restore the visibility
3130 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3131 marketButton.setVisibility(View.GONE);
3132 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003133 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003134 }
3135
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003136 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003137 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3138 Resources r = getResources();
3139 Drawable marketIconDrawable = d.newDrawable(r);
3140 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3141 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3142 marketIconDrawable.setBounds(0, 0, w, h);
3143
3144 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003145 }
3146
Michael Jurkad7c28052012-04-27 15:43:36 -07003147 @Override
3148 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3149 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3150 final List<CharSequence> text = event.getText();
3151 text.clear();
3152 text.add(getString(R.string.home));
3153 return result;
3154 }
3155
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003156 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003157 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003158 */
3159 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3160 @Override
3161 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003162 closeSystemDialogs();
3163 }
3164 }
3165
3166 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003167 * Receives notifications whenever the appwidgets are reset.
3168 */
3169 private class AppWidgetResetObserver extends ContentObserver {
3170 public AppWidgetResetObserver() {
3171 super(new Handler());
3172 }
3173
3174 @Override
3175 public void onChange(boolean selfChange) {
3176 onAppWidgetReset();
3177 }
3178 }
3179
3180 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003181 * If the activity is currently paused, signal that we need to re-run the loader
3182 * in onResume.
3183 *
3184 * This needs to be called from incoming places where resources might have been loaded
3185 * while we are paused. That is becaues the Configuration might be wrong
3186 * when we're not running, and if it comes back to what it was when we
3187 * were paused, we are not restarted.
3188 *
3189 * Implementation of the method from LauncherModel.Callbacks.
3190 *
3191 * @return true if we are currently paused. The caller might be able to
3192 * skip some work in that case since we will come back again.
3193 */
3194 public boolean setLoadOnResume() {
3195 if (mPaused) {
3196 Log.i(TAG, "setLoadOnResume");
3197 mOnResumeNeedsLoad = true;
3198 return true;
3199 } else {
3200 return false;
3201 }
3202 }
3203
3204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003207 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003208 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003209 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003210 } else {
3211 return SCREEN_COUNT / 2;
3212 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003214
Joe Onorato9c1289c2009-08-17 11:03:03 -04003215 /**
3216 * Refreshes the shortcuts shown on the workspace.
3217 *
3218 * Implementation of the method from LauncherModel.Callbacks.
3219 */
3220 public void startBinding() {
3221 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003222
Winson Chungf0c6ae02012-03-21 16:10:31 -07003223 mNewShortcutAnimatePage = -1;
3224 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003225 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 int count = workspace.getChildCount();
3227 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003228 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003229 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3230 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003231 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003232 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003233 if (mHotseat != null) {
3234 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 }
3236 }
3237
3238 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003239 * Bind the items start-end from the list.
3240 *
3241 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003244 setLoadOnResume();
3245
Winson Chungf0c6ae02012-03-21 16:10:31 -07003246 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3247 Set<String> newApps = new HashSet<String>();
3248 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3249
3250 Workspace workspace = mWorkspace;
3251 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003253
3254 // Short circuit if we are loading dock items for a configuration which has no dock
3255 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3256 mHotseat == null) {
3257 continue;
3258 }
3259
Joe Onorato9c1289c2009-08-17 11:03:03 -04003260 switch (item.itemType) {
3261 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3262 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003263 ShortcutInfo info = (ShortcutInfo) item;
3264 String uri = info.intent.toUri(0).toString();
3265 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003266 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3267 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003268 boolean animateIconUp = false;
3269 synchronized (newApps) {
3270 if (newApps.contains(uri)) {
3271 animateIconUp = newApps.remove(uri);
3272 }
3273 }
3274 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003275 // Prepare the view to be animated up
3276 shortcut.setAlpha(0f);
3277 shortcut.setScaleX(0f);
3278 shortcut.setScaleY(0f);
3279 mNewShortcutAnimatePage = item.screen;
3280 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3281 mNewShortcutAnimateViews.add(shortcut);
3282 }
3283 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003285 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003286 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003287 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003288 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003289 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3290 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003293 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003294
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003296 }
3297
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 /**
3299 * Implementation of the method from LauncherModel.Callbacks.
3300 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003301 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003302 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003303 sFolders.clear();
3304 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003305 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306
3307 /**
3308 * Add the views for a widget to the workspace.
3309 *
3310 * Implementation of the method from LauncherModel.Callbacks.
3311 */
3312 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003313 setLoadOnResume();
3314
Daniel Sandler843e8602010-06-07 14:59:01 -04003315 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3316 if (DEBUG_WIDGETS) {
3317 Log.d(TAG, "bindAppWidget: " + item);
3318 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 final Workspace workspace = mWorkspace;
3320
3321 final int appWidgetId = item.appWidgetId;
3322 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003323 if (DEBUG_WIDGETS) {
3324 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3325 }
3326
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3328
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003330 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331
Winson Chung3d503fb2011-07-13 17:25:49 -07003332 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003334 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3335
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 workspace.requestLayout();
3337
Daniel Sandler843e8602010-06-07 14:59:01 -04003338 if (DEBUG_WIDGETS) {
3339 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3340 + (SystemClock.uptimeMillis()-start) + "ms");
3341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 }
3343
Adam Cohen1462de32012-07-24 22:34:36 -07003344 public void onPageBoundSynchronously(int page) {
3345 mSynchronouslyBoundPages.add(page);
3346 }
3347
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 /**
3349 * Callback saying that there aren't any more items to bind.
3350 *
3351 * Implementation of the method from LauncherModel.Callbacks.
3352 */
3353 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003354 setLoadOnResume();
3355
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 if (mSavedState != null) {
3357 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003358 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 mSavedState = null;
3361 }
3362
Adam Cohen1462de32012-07-24 22:34:36 -07003363 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003365 // If we received the result of any pending adds while the loader was running (e.g. the
3366 // widget configuration forced an orientation change), process them now.
3367 for (int i = 0; i < sPendingAddList.size(); i++) {
3368 completeAdd(sPendingAddList.get(i));
3369 }
3370 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371
Winson Chungc51db6a2011-10-05 11:44:49 -07003372 // Update the market app icon as necessary (the other icons will be managed in response to
3373 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003374 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003375
Winson Chungf0c6ae02012-03-21 16:10:31 -07003376 // Animate up any icons as necessary
3377 if (mVisible || mWorkspaceLoading) {
3378 Runnable newAppsRunnable = new Runnable() {
3379 @Override
3380 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003381 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003382 }
3383 };
Winson Chunga2413752012-04-03 14:22:34 -07003384
3385 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3386 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3387 if (canRunNewAppsAnimation()) {
3388 // If the user has not interacted recently, then either snap to the new page to show
3389 // the new-apps animation or just run them if they are to appear on the current page
3390 if (willSnapPage) {
3391 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3392 } else {
3393 runNewAppsAnimation(false);
3394 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003395 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003396 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003397 // are on another page (or just normally if they are added to the current page)
3398 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003399 }
3400 }
3401
3402 mWorkspaceLoading = false;
3403 }
3404
Winson Chunga2413752012-04-03 14:22:34 -07003405 private boolean canRunNewAppsAnimation() {
3406 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3407 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3408 }
3409
Winson Chungf0c6ae02012-03-21 16:10:31 -07003410 /**
3411 * Runs a new animation that scales up icons that were added while Launcher was in the
3412 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003413 *
3414 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003415 */
Winson Chunga2413752012-04-03 14:22:34 -07003416 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003417 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003418 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003419
3420 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003421 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3422 @Override
3423 public int compare(View a, View b) {
3424 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3425 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3426 int cellCountX = LauncherModel.getCellCountX();
3427 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3428 }
3429 });
Winson Chunga2413752012-04-03 14:22:34 -07003430
3431 // Animate each of the views in place (or show them immediately if requested)
3432 if (immediate) {
3433 for (View v : mNewShortcutAnimateViews) {
3434 v.setAlpha(1f);
3435 v.setScaleX(1f);
3436 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 }
Winson Chunga2413752012-04-03 14:22:34 -07003438 } else {
3439 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3440 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003441 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003442 PropertyValuesHolder.ofFloat("alpha", 1f),
3443 PropertyValuesHolder.ofFloat("scaleX", 1f),
3444 PropertyValuesHolder.ofFloat("scaleY", 1f));
3445 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3446 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3447 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3448 bounceAnims.add(bounceAnim);
3449 }
3450 anim.playTogether(bounceAnims);
3451 anim.addListener(new AnimatorListenerAdapter() {
3452 @Override
3453 public void onAnimationEnd(Animator animation) {
3454 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3455 }
3456 });
3457 anim.start();
3458 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003459
3460 // Clean up
3461 mNewShortcutAnimatePage = -1;
3462 mNewShortcutAnimateViews.clear();
3463 new Thread("clearNewAppsThread") {
3464 public void run() {
3465 mSharedPrefs.edit()
3466 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3467 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3468 .commit();
3469 }
3470 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003471 }
3472
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003473 @Override
3474 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003475 boolean searchVisible = updateGlobalSearchIcon();
3476 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3477 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003478 }
3479
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003480 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 * Add the icons for all apps.
3482 *
3483 * Implementation of the method from LauncherModel.Callbacks.
3484 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003485 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003486 Runnable setAllAppsRunnable = new Runnable() {
3487 public void run() {
3488 if (mAppsCustomizeContent != null) {
3489 mAppsCustomizeContent.setApps(apps);
3490 }
3491 }
3492 };
3493
Michael Jurkac57b7a82011-08-09 22:02:20 -07003494 // Remove the progress bar entirely; we could also make it GONE
3495 // but better to remove it since we know it's not going to be used
3496 View progressBar = mAppsCustomizeTabHost.
3497 findViewById(R.id.apps_customize_progress_bar);
3498 if (progressBar != null) {
3499 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003500
3501 // We just post the call to setApps so the user sees the progress bar
3502 // disappear-- otherwise, it just looks like the progress bar froze
3503 // which doesn't look great
3504 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3505 } else {
3506 // If we did not initialize the spinner in onCreate, then we can directly set the
3507 // list of applications without waiting for any progress bars views to be hidden.
3508 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
3511
3512 /**
3513 * A package was installed.
3514 *
3515 * Implementation of the method from LauncherModel.Callbacks.
3516 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003517 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003518 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003519
Winson Chung785d2eb2011-04-14 16:08:02 -07003520 if (mAppsCustomizeContent != null) {
3521 mAppsCustomizeContent.addApps(apps);
3522 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 }
3524
3525 /**
3526 * A package was updated.
3527 *
3528 * Implementation of the method from LauncherModel.Callbacks.
3529 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003530 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003531 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003532 if (mWorkspace != null) {
3533 mWorkspace.updateShortcuts(apps);
3534 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003535
Winson Chung785d2eb2011-04-14 16:08:02 -07003536 if (mAppsCustomizeContent != null) {
3537 mAppsCustomizeContent.updateApps(apps);
3538 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003539 }
3540
3541 /**
3542 * A package was uninstalled.
3543 *
3544 * Implementation of the method from LauncherModel.Callbacks.
3545 */
Winson Chungcd810732012-06-18 16:45:43 -07003546 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003547 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003548 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003550
Winson Chung785d2eb2011-04-14 16:08:02 -07003551 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003552 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003553 }
Winson Chunga1820962011-10-03 16:31:06 -07003554
3555 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003556 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 }
Joe Onoratobe386092009-11-17 17:32:16 -08003558
3559 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003560 * A number of packages were updated.
3561 */
3562 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003563 if (mAppsCustomizeContent != null) {
3564 mAppsCustomizeContent.onPackagesUpdated();
3565 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003566 }
3567
Winson Chung400438b2011-01-16 17:53:48 -08003568 private int mapConfigurationOriActivityInfoOri(int configOri) {
3569 final Display d = getWindowManager().getDefaultDisplay();
3570 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3571 switch (d.getRotation()) {
3572 case Surface.ROTATION_0:
3573 case Surface.ROTATION_180:
3574 // We are currently in the same basic orientation as the natural orientation
3575 naturalOri = configOri;
3576 break;
3577 case Surface.ROTATION_90:
3578 case Surface.ROTATION_270:
3579 // We are currently in the other basic orientation to the natural orientation
3580 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3581 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3582 break;
3583 }
3584
3585 int[] oriMap = {
3586 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3587 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3588 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3589 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3590 };
3591 // Since the map starts at portrait, we need to offset if this device's natural orientation
3592 // is landscape.
3593 int indexOffset = 0;
3594 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3595 indexOffset = 1;
3596 }
3597 return oriMap[(d.getRotation() + indexOffset) % 4];
3598 }
Adam Cohen446e9402011-09-15 18:21:21 -07003599
Winson Chung4b919f82012-05-01 10:44:08 -07003600 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003601 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003602 boolean enableRotation = forceEnableRotation ||
3603 getResources().getBoolean(R.bool.allow_rotation);
3604 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003605 }
Winson Chung4b919f82012-05-01 10:44:08 -07003606 public void lockScreenOrientation() {
3607 if (isRotationEnabled()) {
3608 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3609 .getConfiguration().orientation));
3610 }
3611 }
3612 public void unlockScreenOrientation(boolean immediate) {
3613 if (isRotationEnabled()) {
3614 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003615 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003616 } else {
3617 mHandler.postDelayed(new Runnable() {
3618 public void run() {
3619 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3620 }
3621 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003622 }
Winson Chung4b919f82012-05-01 10:44:08 -07003623 }
Winson Chung400438b2011-01-16 17:53:48 -08003624 }
3625
Winson Chung82f55532011-08-09 14:14:23 -07003626 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003627 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003628 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003629 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003630
Winson Chung7d7541e2011-09-16 20:14:36 -07003631 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003632 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003633 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3634 Cling cling = (Cling) findViewById(clingId);
3635 if (cling != null) {
3636 cling.init(this, positionData);
3637 cling.setVisibility(View.VISIBLE);
3638 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3639 if (animate) {
3640 cling.buildLayer();
3641 cling.setAlpha(0f);
3642 cling.animate()
3643 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003644 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003645 .setDuration(SHOW_CLING_DURATION)
3646 .setStartDelay(delay)
3647 .start();
3648 } else {
3649 cling.setAlpha(1f);
3650 }
3651 }
3652 return cling;
3653 }
3654 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003655 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003656 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003657 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003658 anim.addListener(new AnimatorListenerAdapter() {
3659 public void onAnimationEnd(Animator animation) {
3660 cling.setVisibility(View.GONE);
3661 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003662 // We should update the shared preferences on a background thread
3663 new Thread("dismissClingThread") {
3664 public void run() {
3665 SharedPreferences.Editor editor = mSharedPrefs.edit();
3666 editor.putBoolean(flag, true);
3667 editor.commit();
3668 }
3669 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003670 };
3671 });
3672 anim.start();
3673 }
3674 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003675 private void removeCling(int id) {
3676 final View cling = findViewById(id);
3677 if (cling != null) {
3678 final ViewGroup parent = (ViewGroup) cling.getParent();
3679 parent.post(new Runnable() {
3680 @Override
3681 public void run() {
3682 parent.removeView(cling);
3683 }
3684 });
3685 }
3686 }
Michael Jurka974c3862012-05-22 22:00:31 -07003687
3688 private boolean skipCustomClingIfNoAccounts() {
3689 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3690 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3691 if (customCling) {
3692 AccountManager am = AccountManager.get(this);
3693 Account[] accounts = am.getAccountsByType("com.google");
3694 return accounts.length == 0;
3695 }
3696 return false;
3697 }
3698
Winson Chung7d7541e2011-09-16 20:14:36 -07003699 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003700 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003701 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003702 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3703 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003704 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003705 } else {
3706 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003707 }
3708 }
3709 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003710 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003711 if (isClingsEnabled() &&
3712 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003713 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003714 } else {
3715 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003716 }
3717 }
3718 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003719 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003720 if (isClingsEnabled() &&
3721 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3722 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003723 } else {
3724 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003725 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003726 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003727 }
3728 public boolean isFolderClingVisible() {
3729 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003730 if (cling != null) {
3731 return cling.getVisibility() == View.VISIBLE;
3732 }
3733 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003734 }
Winson Chung82f55532011-08-09 14:14:23 -07003735 public void dismissWorkspaceCling(View v) {
3736 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003737 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003738 }
3739 public void dismissAllAppsCling(View v) {
3740 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003741 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3742 }
3743 public void dismissFolderCling(View v) {
3744 Cling cling = (Cling) findViewById(R.id.folder_cling);
3745 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003746 }
3747
Winson Chung80baf5a2010-08-09 16:03:15 -07003748 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003749 * Prints out out state for debugging.
3750 */
3751 public void dumpState() {
3752 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003753 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003754 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3755 Log.d(TAG, "mRestoring=" + mRestoring);
3756 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3757 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003758 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003759 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003760
Winson Chung785d2eb2011-04-14 16:08:02 -07003761 if (mAppsCustomizeContent != null) {
3762 mAppsCustomizeContent.dumpState();
3763 }
Joe Onoratobe386092009-11-17 17:32:16 -08003764 Log.d(TAG, "END launcher2 dump state");
3765 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003766
3767 @Override
3768 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3769 super.dump(prefix, fd, writer, args);
3770 writer.println(" ");
3771 writer.println("Debug logs: ");
3772 for (int i = 0; i < sDumpLogs.size(); i++) {
3773 writer.println(" " + sDumpLogs.get(i));
3774 }
3775 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003776
3777 public static void dumpDebugLogsToConsole() {
3778 Log.d(TAG, "");
3779 Log.d(TAG, "*********************");
3780 Log.d(TAG, "Launcher debug logs: ");
3781 for (int i = 0; i < sDumpLogs.size(); i++) {
3782 Log.d(TAG, " " + sDumpLogs.get(i));
3783 }
3784 Log.d(TAG, "*********************");
3785 Log.d(TAG, "");
3786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003787}
Michael Jurka2763be32011-02-24 11:19:57 -08003788
Michael Jurkaabded662011-03-04 12:06:57 -08003789interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003790 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003791 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003792 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003793 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003794 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003795}