blob: 9999e0fe3eaaa0e066c511e23724fe4986e2cac7 [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);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400955
Michael Jurka5130e402011-10-13 04:55:35 -0700956 // Get the all apps button
957 mAllAppsButton = findViewById(R.id.all_apps_button);
958 if (mAllAppsButton != null) {
959 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
960 @Override
961 public boolean onTouch(View v, MotionEvent event) {
962 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
963 onTouchDownAllAppsButton(v);
964 }
965 return false;
966 }
967 });
968 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700970 dragController.setDragScoller(mWorkspace);
971 dragController.setScrollView(mDragLayer);
972 dragController.setMoveTarget(mWorkspace);
973 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700974 if (mSearchDropTargetBar != null) {
975 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
978
979 /**
980 * Creates a view representing a shortcut.
981 *
982 * @param info The data structure describing the shortcut.
983 *
984 * @return A View inflated from R.layout.application.
985 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800986 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700988 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 }
990
991 /**
992 * Creates a view representing a shortcut inflated from the specified resource.
993 *
994 * @param layoutResId The id of the XML layout used to create the shortcut.
995 * @param parent The group the shortcut belongs to.
996 * @param info The data structure describing the shortcut.
997 *
998 * @return A View inflated from layoutResId.
999 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001001 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1002 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 return favorite;
1005 }
1006
1007 /**
1008 * Add an application shortcut to the workspace.
1009 *
1010 * @param data The intent describing the application.
1011 * @param cellInfo The position on screen where to create the shortcut.
1012 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016
Adam Cohenfbba09b2011-07-18 21:43:05 -07001017 // First we check if we already know the exact location where we want to add this item.
1018 if (cellX >= 0 && cellY >= 0) {
1019 cellXY[0] = cellX;
1020 cellXY[1] = cellY;
1021 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001022 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001023 return;
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001026 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027
Romain Guy73b979d2009-06-09 12:57:21 -07001028 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001029 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001031 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001033 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001034 } else {
1035 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037 }
Romain Guycbb89e42009-06-08 15:52:54 -07001038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 /**
1040 * Add a shortcut to the workspace.
1041 *
1042 * @param data The intent describing the shortcut.
1043 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1046 int cellY) {
1047 int[] cellXY = mTmpAddItemCellCoordinates;
1048 int[] touchXY = mPendingAddInfo.dropPos;
1049 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Michael Jurkad3ef3062010-11-23 16:23:58 -08001051 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001052
Adam Cohen558baaf2011-08-15 15:22:57 -07001053 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001054 if (info == null) {
1055 return;
1056 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001057 final View view = createShortcut(info);
1058
Adam Cohenfbba09b2011-07-18 21:43:05 -07001059 // First we check if we already know the exact location where we want to add this item.
1060 if (cellX >= 0 && cellY >= 0) {
1061 cellXY[0] = cellX;
1062 cellXY[1] = cellY;
1063 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001064
1065 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001066 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001067 true, null,null)) {
1068 return;
1069 }
1070 DragObject dragObject = new DragObject();
1071 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001072 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1073 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001074 return;
1075 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001077 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001078 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001079 foundCellSpan = (result != null);
1080 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001081 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001082 }
1083
1084 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001085 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 return;
1087 }
1088
Adam Cohen558baaf2011-08-15 15:22:57 -07001089 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001092 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1093 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 }
1095 }
1096
Adam Cohen2f093b62012-04-30 18:59:53 -07001097 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1098 int minHeight) {
1099 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001100 // We want to account for the extra amount of padding that we are adding to the widget
1101 // to ensure that it gets the full amount of space that it has requested
1102 int requiredWidth = minWidth + padding.left + padding.right;
1103 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001105 }
1106
Adam Cohen2f093b62012-04-30 18:59:53 -07001107 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1108 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001109 }
1110
Adam Cohen2f093b62012-04-30 18:59:53 -07001111 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1112 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001113 }
1114
Adam Cohen2f093b62012-04-30 18:59:53 -07001115 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1116 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001117 }
1118
Adam Cohen2f093b62012-04-30 18:59:53 -07001119 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1120 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001121 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001122 }
1123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001127 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001128 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001130 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1131 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1132 if (appWidgetInfo == null) {
1133 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1134 }
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001136 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001138
Adam Cohen2f093b62012-04-30 18:59:53 -07001139 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1140 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001143 // We have saved the position to which the widget was dragged-- this really only matters
1144 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001145 int[] cellXY = mTmpAddItemCellCoordinates;
1146 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001149 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1150 cellXY[0] = mPendingAddInfo.cellX;
1151 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 spanXY[0] = mPendingAddInfo.spanX;
1153 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001154 foundCellSpan = true;
1155 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001157 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001158 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1159 spanXY[1], cellXY, finalSpan);
1160 spanXY[0] = finalSpan[0];
1161 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001162 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001164 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001165 }
1166
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001168 if (appWidgetId != -1) {
1169 // Deleting an app widget ID is a void call but writes to disk before returning
1170 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001171 new Thread("deleteAppWidgetId") {
1172 public void run() {
1173 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1174 }
1175 }.start();
1176 }
Winson Chung93eef082012-03-23 15:59:27 -07001177 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001178 return;
1179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001182 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1183 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001184 launcherInfo.spanX = spanXY[0];
1185 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001186 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1187 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
1192 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001193 if (hostView == null) {
1194 // Perform actual inflation because we're live
1195 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1196 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1197 } else {
1198 // The AppWidgetHostView has already been inflated and instantiated
1199 launcherInfo.hostView = hostView;
1200 }
Romain Guycbb89e42009-06-08 15:52:54 -07001201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001204 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001205 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001206 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001207
1208 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001210 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
Romain Guycbb89e42009-06-08 15:52:54 -07001212
Adam Cohended9f8d2010-11-03 13:25:16 -07001213 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1214 @Override
1215 public void onReceive(Context context, Intent intent) {
1216 final String action = intent.getAction();
1217 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1218 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001219 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001220 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001221
Winson Chungc100e8e2011-08-09 16:02:43 -07001222 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001223 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001224 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1225 mAppsCustomizeTabHost.reset();
1226 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001227 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001228 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1229 mUserPresent = true;
1230 updateRunning();
1231 }
1232 }
1233 };
1234
1235 @Override
1236 public void onAttachedToWindow() {
1237 super.onAttachedToWindow();
1238
1239 // Listen for broadcasts related to user-presence
1240 final IntentFilter filter = new IntentFilter();
1241 filter.addAction(Intent.ACTION_SCREEN_OFF);
1242 filter.addAction(Intent.ACTION_USER_PRESENT);
1243 registerReceiver(mReceiver, filter);
1244
Adam Cohend113e0c2010-11-11 10:48:05 -08001245 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 mVisible = true;
1247 }
1248
1249 @Override
1250 public void onDetachedFromWindow() {
1251 super.onDetachedFromWindow();
1252 mVisible = false;
1253
Adam Cohend113e0c2010-11-11 10:48:05 -08001254 if (mAttached) {
1255 unregisterReceiver(mReceiver);
1256 mAttached = false;
1257 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001258 updateRunning();
1259 }
1260
1261 public void onWindowVisibilityChanged(int visibility) {
1262 mVisible = visibility == View.VISIBLE;
1263 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001264 // The following code used to be in onResume, but it turns out onResume is called when
1265 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1266 // is a more appropriate event to handle
1267 if (mVisible) {
1268 mAppsCustomizeTabHost.onWindowVisible();
1269 if (!mWorkspaceLoading) {
1270 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001271 // We want to let Launcher draw itself at least once before we force it to build
1272 // layers on all the workspace pages, so that transitioning to Launcher from other
1273 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1274 // a true draw so we wait until the second preDraw call to be safe
1275 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001276 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001277 // We delay the layer building a bit in order to give
1278 // other message processing a time to run. In particular
1279 // this avoids a delay in hiding the IME if it was
1280 // currently shown, because doing that may involve
1281 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001282 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001283
Michael Jurka6ee21d22012-02-21 18:20:27 -08001284 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001285 return true;
1286 }
1287 });
1288 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001289 // When Launcher comes back to foreground, a different Activity might be responsible for
1290 // the app market intent, so refresh the icon
1291 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001292 clearTypedText();
1293 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001294 }
1295
1296 private void sendAdvanceMessage(long delay) {
1297 mHandler.removeMessages(ADVANCE_MSG);
1298 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1299 mHandler.sendMessageDelayed(msg, delay);
1300 mAutoAdvanceSentTime = System.currentTimeMillis();
1301 }
1302
1303 private void updateRunning() {
1304 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1305 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1306 mAutoAdvanceRunning = autoAdvanceRunning;
1307 if (autoAdvanceRunning) {
1308 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1309 sendAdvanceMessage(delay);
1310 } else {
1311 if (!mWidgetsToAdvance.isEmpty()) {
1312 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1313 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1314 }
1315 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001316 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001317 }
1318 }
1319 }
1320
1321 private final Handler mHandler = new Handler() {
1322 @Override
1323 public void handleMessage(Message msg) {
1324 if (msg.what == ADVANCE_MSG) {
1325 int i = 0;
1326 for (View key: mWidgetsToAdvance.keySet()) {
1327 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1328 final int delay = mAdvanceStagger * i;
1329 if (v instanceof Advanceable) {
1330 postDelayed(new Runnable() {
1331 public void run() {
1332 ((Advanceable) v).advance();
1333 }
1334 }, delay);
1335 }
1336 i++;
1337 }
1338 sendAdvanceMessage(mAdvanceInterval);
1339 }
1340 }
1341 };
1342
1343 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001344 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001345 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1346 if (v instanceof Advanceable) {
1347 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001348 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001349 updateRunning();
1350 }
1351 }
1352
1353 void removeWidgetToAutoAdvance(View hostView) {
1354 if (mWidgetsToAdvance.containsKey(hostView)) {
1355 mWidgetsToAdvance.remove(hostView);
1356 updateRunning();
1357 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 }
1359
Joe Onorato9c1289c2009-08-17 11:03:03 -04001360 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 launcherInfo.hostView = null;
1363 }
1364
Winson Chung93eef082012-03-23 15:59:27 -07001365 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1366 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1367 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001368 }
1369
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001370 public LauncherAppWidgetHost getAppWidgetHost() {
1371 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
Romain Guycbb89e42009-06-08 15:52:54 -07001373
Winson Chunga9abd0e2010-10-27 17:18:37 -07001374 public LauncherModel getModel() {
1375 return mModel;
1376 }
1377
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001379 getWindow().closeAllPanels();
1380
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001381 // Whatever we were doing is hereby canceled.
1382 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
1386 protected void onNewIntent(Intent intent) {
1387 super.onNewIntent(intent);
1388
1389 // Close the menu
1390 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001391 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001392 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001393
Joe Onorato14f122b2009-11-19 14:06:36 -08001394 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1395 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001396
Adam Cohenac56cff2011-09-28 20:45:37 -07001397 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001398 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001399 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001400 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1401 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001402 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001403 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001404
1405 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001406 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001407
1408 // If we are already on home, then just animate back to the workspace, otherwise, just
1409 // wait until onResume to set the state back to Workspace
1410 if (alreadyOnHome) {
1411 showWorkspace(true);
1412 } else {
1413 mOnResumeState = State.WORKSPACE;
1414 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001415
Joe Onorato3a8820b2009-11-10 15:06:42 -08001416 final View v = getWindow().peekDecorView();
1417 if (v != null && v.getWindowToken() != null) {
1418 InputMethodManager imm = (InputMethodManager)getSystemService(
1419 INPUT_METHOD_SERVICE);
1420 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001422
Michael Jurka35aa14d2011-07-07 17:01:08 -07001423 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001424 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001425 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001426 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 }
1428 }
1429
1430 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001431 public void onRestoreInstanceState(Bundle state) {
1432 super.onRestoreInstanceState(state);
1433 for (int page: mSynchronouslyBoundPages) {
1434 mWorkspace.restoreInstanceStateForChild(page);
1435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437
1438 @Override
1439 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001440 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001441 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
Michael Jurka883f55b2010-10-21 15:47:14 -07001443 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001444 // We close any open folder since it will not be re-opened, and we need to make sure
1445 // this state is reflected.
1446 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1449 mWaitingForResult) {
1450 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1451 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1452 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1453 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001454 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1455 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1456 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 }
1458
1459 if (mFolderInfo != null && mWaitingForResult) {
1460 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1461 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1462 }
Michael Jurka946ad472010-07-09 18:05:18 -07001463
Winson Chung785d2eb2011-04-14 16:08:02 -07001464 // Save the current AppsCustomize tab
1465 if (mAppsCustomizeTabHost != null) {
1466 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1467 if (currentTabTag != null) {
1468 outState.putString("apps_customize_currentTab", currentTabTag);
1469 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001470 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1471 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474
1475 @Override
1476 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001478
Winson Chunge7a03942011-08-05 15:05:12 -07001479 // Remove all pending runnables
1480 mHandler.removeMessages(ADVANCE_MSG);
1481 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001482 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001483
Winson Chungcd2b0142011-06-08 16:02:26 -07001484 // Stop callbacks from LauncherModel
1485 LauncherApplication app = ((LauncherApplication) getApplication());
1486 mModel.stopLoader();
1487 app.setLauncher(null);
1488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001490 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001492 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494 mAppWidgetHost = null;
1495
1496 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497
1498 TextKeyListener.getInstance().release();
1499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
Winson Chung3d503fb2011-07-13 17:25:49 -07001501 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001502
1503 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001504 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001505
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001506 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001507 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1508 mWorkspace.removeAllViews();
1509 mWorkspace = null;
1510 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001511
Michael Jurka2ecf9952012-06-18 12:52:28 -07001512 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 }
1514
Adam Cohena9cf38f2011-05-02 15:36:58 -07001515 public DragController getDragController() {
1516 return mDragController;
1517 }
1518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 @Override
1520 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001521 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 super.startActivityForResult(intent, requestCode);
1523 }
1524
Winson Chung70d72102011-08-12 11:24:35 -07001525 /**
1526 * Indicates that we want global search for this activity by setting the globalSearch
1527 * argument for {@link #startSearch} to true.
1528 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001530 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001532
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001533 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001534
Karl Rosaen138a0412009-04-23 19:00:21 -07001535 if (initialQuery == null) {
1536 // Use any text typed in the launcher as the initial query
1537 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 if (appSearchData == null) {
1540 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001541 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001543 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Michael Jurkaa33411c2012-06-14 16:18:21 -07001545 startGlobalSearch(initialQuery, selectInitialQuery,
1546 appSearchData, sourceBounds);
1547 }
1548
1549 /**
1550 * Starts the global search activity. This code is a copied from SearchManager
1551 */
1552 public void startGlobalSearch(String initialQuery,
1553 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1556 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1557 if (globalSearchActivity == null) {
1558 Log.w(TAG, "No global search activity found.");
1559 return;
1560 }
1561 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1562 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1563 intent.setComponent(globalSearchActivity);
1564 // Make sure that we have a Bundle to put source in
1565 if (appSearchData == null) {
1566 appSearchData = new Bundle();
1567 } else {
1568 appSearchData = new Bundle(appSearchData);
1569 }
1570 // Set source to package name of app that starts global search, if not set already.
1571 if (!appSearchData.containsKey("source")) {
1572 appSearchData.putString("source", getPackageName());
1573 }
1574 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1575 if (!TextUtils.isEmpty(initialQuery)) {
1576 intent.putExtra(SearchManager.QUERY, initialQuery);
1577 }
1578 if (selectInitialQuery) {
1579 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1580 }
1581 intent.setSourceBounds(sourceBounds);
1582 try {
1583 startActivity(intent);
1584 } catch (ActivityNotFoundException ex) {
1585 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
Winson Chung70d72102011-08-12 11:24:35 -07001589 @Override
1590 public boolean onCreateOptionsMenu(Menu menu) {
1591 if (isWorkspaceLocked()) {
1592 return false;
1593 }
1594
1595 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001596
1597 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1598 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1599 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001600 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1601 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1602 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001603 String helpUrl = getString(R.string.help_url);
1604 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001605 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1606 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1607
Winson Chung70d72102011-08-12 11:24:35 -07001608 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1609 .setIcon(android.R.drawable.ic_menu_gallery)
1610 .setAlphabeticShortcut('W');
1611 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1612 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001613 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001614 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001615 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1616 .setIcon(android.R.drawable.ic_menu_preferences)
1617 .setIntent(settings)
1618 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001619 if (!helpUrl.isEmpty()) {
1620 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1621 .setIcon(android.R.drawable.ic_menu_help)
1622 .setIntent(help)
1623 .setAlphabeticShortcut('H');
1624 }
Winson Chung70d72102011-08-12 11:24:35 -07001625 return true;
1626 }
1627
1628 @Override
1629 public boolean onPrepareOptionsMenu(Menu menu) {
1630 super.onPrepareOptionsMenu(menu);
1631
1632 if (mAppsCustomizeTabHost.isTransitioning()) {
1633 return false;
1634 }
1635 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1636 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1637
1638 return true;
1639 }
1640
1641 @Override
1642 public boolean onOptionsItemSelected(MenuItem item) {
1643 switch (item.getItemId()) {
1644 case MENU_WALLPAPER_SETTINGS:
1645 startWallpaper();
1646 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001647 }
1648
1649 return super.onOptionsItemSelected(item);
1650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001652 @Override
1653 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001654 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001655 // Use a custom animation for launching search
1656 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001658 }
1659
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 public boolean isWorkspaceLocked() {
1661 return mWorkspaceLoading || mWaitingForResult;
1662 }
1663
Michael Jurka0280c3b2010-09-17 15:00:07 -07001664 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 mPendingAddInfo.container = ItemInfo.NO_ID;
1666 mPendingAddInfo.screen = -1;
1667 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1668 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001669 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001671 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001672
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001673 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1674 AppWidgetProviderInfo appWidgetInfo) {
1675 if (appWidgetInfo.configure != null) {
1676 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001677
Bjorn Bringert7984c942009-12-09 15:38:25 +00001678 // Launch over to configure widget, if needed
1679 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001680 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001681 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001682 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001684 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001685 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1686 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001687 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001688 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
1690 }
Romain Guycbb89e42009-06-08 15:52:54 -07001691
Adam Cohenfbba09b2011-07-18 21:43:05 -07001692 /**
1693 * Process a shortcut drop.
1694 *
1695 * @param componentName The name of the component
1696 * @param screen The screen where it should be added
1697 * @param cell The cell it should be added to, optional
1698 * @param position The location on the screen where it was dropped, optional
1699 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1701 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001703 mPendingAddInfo.container = container;
1704 mPendingAddInfo.screen = screen;
1705 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001706
1707 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001708 mPendingAddInfo.cellX = cell[0];
1709 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001710 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001711
1712 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1713 createShortcutIntent.setComponent(componentName);
1714 processShortcut(createShortcutIntent);
1715 }
1716
Adam Cohenfbba09b2011-07-18 21:43:05 -07001717 /**
1718 * Process a widget drop.
1719 *
1720 * @param info The PendingAppWidgetInfo of the widget being added.
1721 * @param screen The screen where it should be added
1722 * @param cell The cell it should be added to, optional
1723 * @param position The location on the screen where it was dropped, optional
1724 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001725 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001726 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001727 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001728 mPendingAddInfo.container = info.container = container;
1729 mPendingAddInfo.screen = info.screen = screen;
1730 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001731 mPendingAddInfo.minSpanX = info.minSpanX;
1732 mPendingAddInfo.minSpanY = info.minSpanY;
1733
Adam Cohenfbba09b2011-07-18 21:43:05 -07001734 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001735 mPendingAddInfo.cellX = cell[0];
1736 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001737 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001738 if (span != null) {
1739 mPendingAddInfo.spanX = span[0];
1740 mPendingAddInfo.spanY = span[1];
1741 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001742
Adam Cohened66b2b2012-01-23 17:28:51 -08001743 AppWidgetHostView hostView = info.boundWidget;
1744 int appWidgetId;
1745 if (hostView != null) {
1746 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001747 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001748 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001749 // In this case, we either need to start an activity to get permission to bind
1750 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001751 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001752 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001753 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001754 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001755 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001756 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1757 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1758 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1759 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1760 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001761 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001762 }
1763
Joe Onoratodeb98af2010-02-19 14:59:39 -08001764 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 // Handle case where user selected "Applications"
1766 String applicationName = getResources().getString(R.string.group_applications);
1767 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 if (applicationName != null && applicationName.equals(shortcutName)) {
1770 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1771 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1774 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001775 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001776 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001778 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781
Winson Chung24ab2f12010-09-16 14:10:47 -07001782 void processWallpaper(Intent intent) {
1783 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1784 }
1785
Winson Chung3d503fb2011-07-13 17:25:49 -07001786 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1787 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001788 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 folderInfo.title = getText(R.string.folder_name);
1790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001792 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1793 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001794 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795
1796 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001797 FolderIcon newFolder =
1798 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1799 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1800 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001801 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001805 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 }
1807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001809 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001811 Intent chooser = Intent.createChooser(pickWallpaper,
1812 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001813 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1814 // Removed in Eclair MR1
1815// WallpaperManager wm = (WallpaperManager)
1816// getSystemService(Context.WALLPAPER_SERVICE);
1817// WallpaperInfo wi = wm.getWallpaperInfo();
1818// if (wi != null && wi.getSettingsActivity() != null) {
1819// LabeledIntent li = new LabeledIntent(getPackageName(),
1820// R.string.configure_wallpaper, 0);
1821// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1822// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1823// }
Mike Clerona0618e42009-10-22 13:55:21 -07001824 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001827 /**
1828 * Registers various content observers. The current implementation registers
1829 * only a favorites observer to keep track of the favorites applications.
1830 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001831 private void registerContentObservers() {
1832 ContentResolver resolver = getContentResolver();
1833 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1834 true, mWidgetObserver);
1835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 @Override
1838 public boolean dispatchKeyEvent(KeyEvent event) {
1839 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1840 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001842 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001843 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001844 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001845 dumpState();
1846 return true;
1847 }
1848 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001849 }
1850 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1851 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001852 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 return true;
1854 }
1855 }
1856
1857 return super.dispatchKeyEvent(event);
1858 }
1859
Joe Onorato88ec0992009-11-19 13:16:06 -08001860 @Override
1861 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001862 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001863 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001864 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001865 Folder openFolder = mWorkspace.getOpenFolder();
1866 if (openFolder.isEditingName()) {
1867 openFolder.dismissEditingName();
1868 } else {
1869 closeFolder();
1870 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001871 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001872 mWorkspace.exitWidgetResizeMode();
1873
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001874 // Back button is a no-op here, but give at least some feedback for the button press
1875 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001876 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001877 }
1878
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001880 * Re-listen when widgets are reset.
1881 */
1882 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001883 if (mAppWidgetHost != null) {
1884 mAppWidgetHost.startListening();
1885 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001886 }
1887
1888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1890 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001892 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001893 if (mModel != null) {
1894 mModel.unbindWorkspaceItems();
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 /**
1899 * Launches the intent referred by the clicked shortcut.
1900 *
1901 * @param v The view representing the clicked shortcut.
1902 */
1903 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001904 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1905 // view has detached (it's possible for this to happen if the view is removed mid touch).
1906 if (v.getWindowToken() == null) {
1907 return;
1908 }
1909
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001910 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001911 return;
1912 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001915 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001917 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001918 int[] pos = new int[2];
1919 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001920 intent.setSourceBounds(new Rect(pos[0], pos[1],
1921 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001922
1923 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001924
1925 if (success && v instanceof BubbleTextView) {
1926 mWaitingForResume = (BubbleTextView) v;
1927 mWaitingForResume.setStayPressed(true);
1928 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001930 if (v instanceof FolderIcon) {
1931 FolderIcon fi = (FolderIcon) v;
1932 handleFolderClick(fi);
1933 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001934 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001935 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001936 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001937 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001938 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941 }
1942
Michael Jurka0e260592010-06-30 17:07:39 -07001943 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001944 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001945 // clicking anywhere on the workspace causes the customization drawer to slide down
1946 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001947 return false;
1948 }
1949
Michael Jurkaaf442092010-06-10 17:01:57 -07001950 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001951 * Event handler for the search button
1952 *
1953 * @param v The view that was clicked.
1954 */
1955 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001956 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1957
Amith Yamasania135ba82011-08-09 17:42:01 -07001958 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001959 }
1960
1961 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001962 * Event handler for the voice button
1963 *
1964 * @param v The view that was clicked.
1965 */
1966 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001967 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001968
Michael Jurkaae65ee32012-04-30 11:45:54 -07001969 try {
1970 final SearchManager searchManager =
1971 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1972 ComponentName activityName = searchManager.getGlobalSearchActivity();
1973 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001974 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001975 if (activityName != null) {
1976 intent.setPackage(activityName.getPackageName());
1977 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001978 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001979 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001980 } catch (ActivityNotFoundException e) {
1981 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001982 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001983 startActivitySafely(null, intent, "onClickVoiceButton");
1984 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001985 }
1986
1987 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001988 * Event handler for the "grid" button that appears on the home screen, which
1989 * enters all apps mode.
1990 *
1991 * @param v The view that was clicked.
1992 */
1993 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001994 showAllApps(true);
1995 }
1996
1997 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001998 // Provide the same haptic feedback that the system offers for virtual keys.
1999 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002000 }
2001
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002002 public void onClickAppMarketButton(View v) {
2003 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002004 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002005 } else {
2006 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002007 }
2008 }
2009
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002010 void startApplicationDetailsActivity(ComponentName componentName) {
2011 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002012 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2013 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002014 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002015 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002016 }
2017
Patrick Dubroy5539af72010-09-07 15:22:01 -07002018 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2019 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2020 // System applications cannot be installed. For now, show a toast explaining that.
2021 // We may give them the option of disabling apps this way.
2022 int messageId = R.string.uninstall_system_app_text;
2023 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2024 } else {
2025 String packageName = appInfo.componentName.getPackageName();
2026 String className = appInfo.componentName.getClassName();
2027 Intent intent = new Intent(
2028 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002029 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2030 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002031 startActivity(intent);
2032 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002033 }
2034
Michael Jurka86a720e2012-05-09 11:23:23 -07002035 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002039 // Only launch using the new animation if the shortcut has not opted out (this is a
2040 // private contract between launcher and may be ignored in the future).
2041 boolean useLaunchAnimation = (v != null) &&
2042 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2043 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002044 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2045 v.getMeasuredWidth(), v.getMeasuredHeight());
2046
2047 startActivity(intent, opts.toBundle());
2048 } else {
2049 startActivity(intent);
2050 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002051 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 } catch (SecurityException e) {
2053 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002054 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002056 "or use the exported attribute for this activity. "
2057 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002059 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002061
Michael Jurka86a720e2012-05-09 11:23:23 -07002062 boolean startActivitySafely(View v, Intent intent, Object tag) {
2063 boolean success = false;
2064 try {
2065 success = startActivity(v, intent, tag);
2066 } catch (ActivityNotFoundException e) {
2067 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2068 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2069 }
2070 return success;
2071 }
2072
Romain Guy8e633c52010-03-04 12:51:36 -08002073 void startActivityForResultSafely(Intent intent, int requestCode) {
2074 try {
2075 startActivityForResult(intent, requestCode);
2076 } catch (ActivityNotFoundException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 } catch (SecurityException e) {
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2081 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2082 "or use the exported attribute for this activity.", e);
2083 }
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
Adam Cohena9cf38f2011-05-02 15:36:58 -07002086 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002087 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002088 Folder openFolder = mWorkspace.getFolderForTag(info);
2089
2090 // If the folder info reports that the associated folder is open, then verify that
2091 // it is actually opened. There have been a few instances where this gets out of sync.
2092 if (info.opened && openFolder == null) {
2093 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2094 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2095 info.opened = false;
2096 }
2097
Adam Cohenfb91f302012-06-11 15:45:18 -07002098 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // Close any open folder
2100 closeFolder();
2101 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002102 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } else {
2104 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 int folderScreen;
2106 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002107 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 // .. and close it
2109 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002110 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 // Close any folder open on the current screen
2112 closeFolder();
2113 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002114 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116 }
2117 }
2118 }
2119
Adam Cohen268c4752012-06-06 17:47:33 -07002120 /**
2121 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2122 * in the DragLayer in the exact absolute location of the original FolderIcon.
2123 */
2124 private void copyFolderIconToImage(FolderIcon fi) {
2125 final int width = fi.getMeasuredWidth();
2126 final int height = fi.getMeasuredHeight();
2127
2128 // Lazy load ImageView, Bitmap and Canvas
2129 if (mFolderIconImageView == null) {
2130 mFolderIconImageView = new ImageView(this);
2131 }
2132 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2133 mFolderIconBitmap.getHeight() != height) {
2134 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2135 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2136 }
2137
2138 DragLayer.LayoutParams lp;
2139 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2140 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2141 } else {
2142 lp = new DragLayer.LayoutParams(width, height);
2143 }
2144
2145 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2146 lp.customPosition = true;
2147 lp.x = mRectForFolderAnimation.left;
2148 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002149 lp.width = width;
2150 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002151
2152 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2153 fi.draw(mFolderIconCanvas);
2154 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002155 if (fi.getFolder() != null) {
2156 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2157 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002158 }
Adam Cohen268c4752012-06-06 17:47:33 -07002159 // Just in case this image view is still in the drag layer from a previous animation,
2160 // we remove it and re-add it.
2161 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2162 mDragLayer.removeView(mFolderIconImageView);
2163 }
2164 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002165 if (fi.getFolder() != null) {
2166 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002167 }
Adam Cohen268c4752012-06-06 17:47:33 -07002168 }
2169
Adam Cohen2801caf2011-05-13 20:57:39 -07002170 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002171 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002172 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2173 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2174 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2175
Adam Cohenc51934b2011-07-26 21:07:43 -07002176 FolderInfo info = (FolderInfo) fi.getTag();
2177 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2178 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002179 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2180 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002181 }
2182
Adam Cohen268c4752012-06-06 17:47:33 -07002183 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2184 copyFolderIconToImage(fi);
2185 fi.setVisibility(View.INVISIBLE);
2186
Michael Jurka2ecf9952012-06-18 12:52:28 -07002187 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002188 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002189 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2190 oa.start();
2191 }
2192
Adam Cohen268c4752012-06-06 17:47:33 -07002193 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002194 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002195 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2196 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2197 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2198
Adam Cohen268c4752012-06-06 17:47:33 -07002199 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002200
Adam Cohen268c4752012-06-06 17:47:33 -07002201 // We remove and re-draw the FolderIcon in-case it has changed
2202 mDragLayer.removeView(mFolderIconImageView);
2203 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002204 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002205 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002206 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002207 oa.addListener(new AnimatorListenerAdapter() {
2208 @Override
2209 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002210 if (cl != null) {
2211 cl.clearFolderLeaveBehind();
2212 // Remove the ImageView copy of the FolderIcon and make the original visible.
2213 mDragLayer.removeView(mFolderIconImageView);
2214 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002215 }
2216 }
2217 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002218 oa.start();
2219 }
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002222 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 * is animated relative to the specified View. If the View is null, no animation
2224 * is played.
2225 *
2226 * @param folderInfo The FolderInfo describing the folder to open.
2227 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002228 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002229 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231
Adam Cohena9cf38f2011-05-02 15:36:58 -07002232 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233
Adam Cohen4554ee12011-08-03 16:13:21 -07002234 // Just verify that the folder hasn't already been added to the DragLayer.
2235 // There was a one-off crash where the folder had a parent already.
2236 if (folder.getParent() == null) {
2237 mDragLayer.addView(folder);
2238 mDragController.addDropTarget((DropTarget) folder);
2239 } else {
2240 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2241 folder.getParent() + ").");
2242 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002243 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002244 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002245 }
2246
2247 public void closeFolder() {
2248 Folder folder = mWorkspace.getOpenFolder();
2249 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002250 if (folder.isEditingName()) {
2251 folder.dismissEditingName();
2252 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002253 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002254
2255 // Dismiss the folder cling
2256 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002257 }
2258 }
2259
2260 void closeFolder(Folder folder) {
2261 folder.getInfo().opened = false;
2262
2263 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2264 if (parent != null) {
2265 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2266 shrinkAndFadeInFolderIcon(fi);
2267 }
2268 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 }
2270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002272 if (!isDraggingEnabled()) return false;
2273 if (isWorkspaceLocked()) return false;
2274 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
2276 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002277 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 }
2279
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280 resetAddInfo();
2281 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002283 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 return true;
2285 }
2286
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 // The hotseat touch handling does not go through Workspace, and we always allow long press
2288 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2291 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 if (itemUnderLongClick == null) {
2293 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2295 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002296 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002298 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002300 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
2302 }
2303 }
2304 return true;
2305 }
2306
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 boolean isHotseatLayout(View layout) {
2308 return mHotseat != null && layout != null &&
2309 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2310 }
2311 Hotseat getHotseat() {
2312 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002313 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002314 SearchDropTargetBar getSearchBar() {
2315 return mSearchDropTargetBar;
2316 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002317
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 /**
2319 * Returns the CellLayout of the specified container at the specified screen.
2320 */
2321 CellLayout getCellLayout(long container, int screen) {
2322 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2323 if (mHotseat != null) {
2324 return mHotseat.getLayout();
2325 } else {
2326 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002327 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 } else {
2329 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002330 }
2331 }
2332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 Workspace getWorkspace() {
2334 return mWorkspace;
2335 }
2336
Daniel Sandler843e8602010-06-07 14:59:01 -04002337 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2338 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002339 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002340 }
2341
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002342 public boolean isAllAppsButtonRank(int rank) {
2343 return mHotseat.isAllAppsButtonRank(rank);
2344 }
2345
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 /**
2347 * Helper method for the cameraZoomIn/cameraZoomOut animations
2348 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002349 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002350 * @param scaleFactor The scale factor used for the zoom
2351 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002352 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002354 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002356
Winson Chung18f41f82012-05-09 13:28:10 -07002357 void disableWallpaperIfInAllApps() {
2358 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002359 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002360 if (mAppsCustomizeTabHost != null &&
2361 !mAppsCustomizeTabHost.isTransitioning()) {
2362 updateWallpaperVisibility(false);
2363 }
2364 }
2365 }
2366
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002367 void updateWallpaperVisibility(boolean visible) {
2368 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2369 int curflags = getWindow().getAttributes().flags
2370 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2371 if (wpflags != curflags) {
2372 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2373 }
2374 }
2375
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002376 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2377 if (v instanceof LauncherTransitionable) {
2378 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2379 }
2380 }
2381
Michael Jurkabed61d22012-02-14 22:51:29 -08002382 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2383 if (v instanceof LauncherTransitionable) {
2384 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2385 }
Winson Chung70442722012-02-10 15:43:22 -08002386
2387 // Update the workspace transition step as well
2388 dispatchOnLauncherTransitionStep(v, 0f);
2389 }
2390
2391 private void dispatchOnLauncherTransitionStep(View v, float t) {
2392 if (v instanceof LauncherTransitionable) {
2393 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2394 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002395 }
2396
2397 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2398 if (v instanceof LauncherTransitionable) {
2399 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2400 }
Winson Chung70442722012-02-10 15:43:22 -08002401
2402 // Update the workspace transition step as well
2403 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002404 }
2405
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002406 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002407 * Things to test when changing the following seven functions.
2408 * - Home from workspace
2409 * - from center screen
2410 * - from other screens
2411 * - Home from all apps
2412 * - from center screen
2413 * - from other screens
2414 * - Back from all apps
2415 * - from center screen
2416 * - from other screens
2417 * - Launch app from workspace and quit
2418 * - with back
2419 * - with home
2420 * - Launch app from all apps and quit
2421 * - with back
2422 * - with home
2423 * - Go to a screen that's not the default, then all
2424 * apps, and launch and app, and go back
2425 * - with back
2426 * -with home
2427 * - On workspace, long press power and go back
2428 * - with back
2429 * - with home
2430 * - On all apps, long press power and go back
2431 * - with back
2432 * - with home
2433 * - On workspace, power off
2434 * - On all apps, power off
2435 * - Launch an app and turn off the screen while in that app
2436 * - Go back with home key
2437 * - Go back with back key TODO: make this not go to workspace
2438 * - From all apps
2439 * - From workspace
2440 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2441 * - From all apps
2442 * - From the center workspace
2443 * - From another workspace
2444 */
2445
2446 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002447 * Zoom the camera out from the workspace to reveal 'toView'.
2448 * Assumes that the view to show is anchored at either the very top or very bottom
2449 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002451 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002452 if (mStateAnimation != null) {
2453 mStateAnimation.cancel();
2454 mStateAnimation = null;
2455 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002457
Winson Chungf0ea4d32011-06-06 14:27:16 -07002458 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2459 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2460 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002461 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002462 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002463 final int startDelay =
2464 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002465
Michael Jurkab3e22d92011-10-31 15:58:33 -07002466 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002467
Michael Jurkad74c9842011-07-10 12:44:21 -07002468 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002469 Animator workspaceAnim =
2470 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002471
2472 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002473 toView.setScaleX(scale);
2474 toView.setScaleY(scale);
2475 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2476 scaleAnim.
2477 scaleX(1f).scaleY(1f).
2478 setDuration(duration).
2479 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002480
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002482 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002483 final ObjectAnimator alphaAnim = ObjectAnimator
2484 .ofFloat(toView, "alpha", 0f, 1f)
2485 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002486 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002487 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2488 @Override
2489 public void onAnimationUpdate(ValueAnimator animation) {
2490 float t = (Float) animation.getAnimatedValue();
2491 dispatchOnLauncherTransitionStep(fromView, t);
2492 dispatchOnLauncherTransitionStep(toView, t);
2493 }
2494 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002495
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002496 // toView should appear right at the end of the workspace shrink
2497 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002498 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002499 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002500 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002501
2502 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002503 boolean animationCancelled = false;
2504
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002505 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002506 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002507 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002508 // Prepare the position
2509 toView.setTranslationX(0.0f);
2510 toView.setTranslationY(0.0f);
2511 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002512 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002513 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002514 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002515 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002516 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2517 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002518
2519 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2520 // Hide the workspace scrollbar
2521 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002522 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002523 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002524 if (!animationCancelled) {
2525 updateWallpaperVisibility(false);
2526 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002527
2528 // Hide the search bar
2529 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002530 }
2531
Adam Cohencff6af82011-09-13 14:51:53 -07002532 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002533 public void onAnimationCancel(Animator animation) {
2534 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002535 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002536 });
2537
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002538 if (workspaceAnim != null) {
2539 mStateAnimation.play(workspaceAnim);
2540 }
Michael Jurka1899a362011-11-03 13:50:45 -07002541
2542 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002543 final ViewTreeObserver observer;
2544
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002545 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2546 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002547
2548 // If any of the objects being animated haven't been measured/laid out
2549 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002550 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002551 (mWorkspace.getMeasuredWidth() == 0) ||
2552 (toView.getMeasuredWidth() == 0)) {
2553 observer = mWorkspace.getViewTreeObserver();
2554 delayAnim = true;
2555 } else {
2556 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002557 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002558
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002559 final AnimatorSet stateAnimation = mStateAnimation;
2560 final Runnable startAnimRunnable = new Runnable() {
2561 public void run() {
2562 // Check that mStateAnimation hasn't changed while
2563 // we waited for a layout/draw pass
2564 if (mStateAnimation != stateAnimation)
2565 return;
2566 setPivotsForZoom(toView, scale);
2567 dispatchOnLauncherTransitionStart(fromView, animated, false);
2568 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002569 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002570 public void run() {
2571 // Check that mStateAnimation hasn't changed while
2572 // we waited for a layout/draw pass
2573 if (mStateAnimation != stateAnimation)
2574 return;
2575 mStateAnimation.start();
2576 }
2577 });
2578 }
2579 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002580 if (delayAnim) {
2581 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2582 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002583 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002584 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002585 }
2586 };
2587 observer.addOnGlobalLayoutListener(delayedStart);
2588 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002589 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002590 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002591 } else {
2592 toView.setTranslationX(0.0f);
2593 toView.setTranslationY(0.0f);
2594 toView.setScaleX(1.0f);
2595 toView.setScaleY(1.0f);
2596 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002597 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002598
Michael Jurkabed61d22012-02-14 22:51:29 -08002599 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2600 // Hide the workspace scrollbar
2601 mWorkspace.hideScrollingIndicator(true);
2602 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002603
2604 // Hide the search bar
2605 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002606 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002607 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002608 dispatchOnLauncherTransitionStart(fromView, animated, false);
2609 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002610 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002611 dispatchOnLauncherTransitionStart(toView, animated, false);
2612 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002613 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 }
2616
2617 /**
2618 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002619 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002621 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002622 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2623 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002624
Michael Jurkab3e22d92011-10-31 15:58:33 -07002625 if (mStateAnimation != null) {
2626 mStateAnimation.cancel();
2627 mStateAnimation = null;
2628 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002629 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002630
Winson Chungf0ea4d32011-06-06 14:27:16 -07002631 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002632 final int fadeOutDuration =
2633 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002634 final float scaleFactor = (float)
2635 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2636 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002637 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002638 Animator workspaceAnim = null;
2639
2640 if (toState == State.WORKSPACE) {
2641 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2642 workspaceAnim = mWorkspace.getChangeStateAnimation(
2643 Workspace.State.NORMAL, animated, stagger);
2644 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2645 workspaceAnim = mWorkspace.getChangeStateAnimation(
2646 Workspace.State.SPRING_LOADED, animated);
2647 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002648
Michael Jurkab3e22d92011-10-31 15:58:33 -07002649 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002650 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002651 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002653 final LauncherViewPropertyAnimator scaleAnim =
2654 new LauncherViewPropertyAnimator(fromView);
2655 scaleAnim.
2656 scaleX(scaleFactor).scaleY(scaleFactor).
2657 setDuration(duration).
2658 setInterpolator(new Workspace.ZoomInInterpolator());
2659
2660 final ObjectAnimator alphaAnim = ObjectAnimator
2661 .ofFloat(fromView, "alpha", 1f, 0f)
2662 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002663 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002664 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2665 @Override
2666 public void onAnimationUpdate(ValueAnimator animation) {
2667 float t = 1f - (Float) animation.getAnimatedValue();
2668 dispatchOnLauncherTransitionStep(fromView, t);
2669 dispatchOnLauncherTransitionStep(toView, t);
2670 }
2671 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002672
Michael Jurka2ecf9952012-06-18 12:52:28 -07002673 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002674
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002675 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2676 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002677
2678 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002679 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002680 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002681 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002682 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002683 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2684 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002685 if (mWorkspace != null) {
2686 mWorkspace.hideScrollingIndicator(false);
2687 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002688 if (onCompleteRunnable != null) {
2689 onCompleteRunnable.run();
2690 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 }
2692 });
2693
Winson Chungf0ea4d32011-06-06 14:27:16 -07002694 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002695 if (workspaceAnim != null) {
2696 mStateAnimation.play(workspaceAnim);
2697 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002698 dispatchOnLauncherTransitionStart(fromView, animated, true);
2699 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002700 final Animator stateAnimation = mStateAnimation;
2701 mWorkspace.post(new Runnable() {
2702 public void run() {
2703 if (stateAnimation != mStateAnimation)
2704 return;
2705 mStateAnimation.start();
2706 }
2707 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002708 } else {
2709 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002710 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002711 dispatchOnLauncherTransitionStart(fromView, animated, true);
2712 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002713 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002714 dispatchOnLauncherTransitionStart(toView, animated, true);
2715 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002716 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002718 }
2719
Michael Jurkae326f182011-11-21 14:05:46 -08002720 @Override
2721 public void onTrimMemory(int level) {
2722 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002723 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002724 mAppsCustomizeTabHost.onTrimMemory();
2725 }
2726 }
2727
Winson Chung18f41f82012-05-09 13:28:10 -07002728 @Override
2729 public void onWindowFocusChanged(boolean hasFocus) {
2730 if (!hasFocus) {
2731 // When another window occludes launcher (like the notification shade, or recents),
2732 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2733 updateWallpaperVisibility(true);
2734 } else {
2735 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002736 mWorkspace.postDelayed(new Runnable() {
2737 @Override
2738 public void run() {
2739 disableWallpaperIfInAllApps();
2740 }
2741 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002742 }
2743 }
2744
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002745 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002746 showWorkspace(animated, null);
2747 }
2748
2749 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002751 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002752 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002753 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002754
Winson Chungc7d2b602012-05-16 17:02:20 -07002755 // Show the search bar (only animate if we were showing the drop target bar in spring
2756 // loaded mode)
2757 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2758
Michael Jurkab737ee62011-11-15 15:57:22 -08002759 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002760 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002761
2762 // Set focus to the AppsCustomize button
2763 if (mAllAppsButton != null) {
2764 mAllAppsButton.requestFocus();
2765 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002766 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002767
Michael Jurkab737ee62011-11-15 15:57:22 -08002768 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002769
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002770 // Change the state *after* we've called all the transition code
2771 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002772
Winson Chung5fb63472011-02-02 17:03:37 -08002773 // Resume the auto-advance of widgets
2774 mUserPresent = true;
2775 updateRunning();
2776
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002777 // send an accessibility event to announce the context change
2778 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002779 }
2780
Michael Jurkab3e22d92011-10-31 15:58:33 -07002781 void showAllApps(boolean animated) {
2782 if (mState != State.WORKSPACE) return;
2783
2784 showAppsCustomizeHelper(animated, false);
2785 mAppsCustomizeTabHost.requestFocus();
2786
Michael Jurkab3e22d92011-10-31 15:58:33 -07002787 // Change the state *after* we've called all the transition code
2788 mState = State.APPS_CUSTOMIZE;
2789
2790 // Pause the auto-advance of widgets until we are out of AllApps
2791 mUserPresent = false;
2792 updateRunning();
2793 closeFolder();
2794
2795 // Send an accessibility event to announce the context change
2796 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2797 }
2798
Adam Cohen7777d962011-08-18 18:58:38 -07002799 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002800 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002801 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002802 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002803 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002804 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002805 }
Adam Cohen7777d962011-08-18 18:58:38 -07002806
Adam Cohened66b2b2012-01-23 17:28:51 -08002807 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2808 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002809 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2810
Winson Chunge7a03942011-08-05 15:05:12 -07002811 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002812 @Override
2813 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002814 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002815 // Before we show workspace, hide all apps again because
2816 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2817 // clean up our state transition functions
2818 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002819 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002820 } else {
2821 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002822 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002823 }
2824 }, (extendedDelay ?
2825 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2826 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2827 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002828
Michael Jurkad3ef3062010-11-23 16:23:58 -08002829 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002830 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002831 final boolean animated = true;
2832 final boolean springLoaded = true;
2833 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002834 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002835 }
2836 // Otherwise, we are not in spring loaded mode, so don't do anything.
2837 }
2838
Michael Jurkab737ee62011-11-15 15:57:22 -08002839 void hideDockDivider() {
2840 if (mQsbDivider != null && mDockDivider != null) {
2841 mQsbDivider.setVisibility(View.INVISIBLE);
2842 mDockDivider.setVisibility(View.INVISIBLE);
2843 }
2844 }
2845
2846 void showDockDivider(boolean animated) {
2847 if (mQsbDivider != null && mDockDivider != null) {
2848 mQsbDivider.setVisibility(View.VISIBLE);
2849 mDockDivider.setVisibility(View.VISIBLE);
2850 if (mDividerAnimator != null) {
2851 mDividerAnimator.cancel();
2852 mQsbDivider.setAlpha(1f);
2853 mDockDivider.setAlpha(1f);
2854 mDividerAnimator = null;
2855 }
2856 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002857 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2858 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2859 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Michael Jurkab737ee62011-11-15 15:57:22 -08002860 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2861 mDividerAnimator.start();
2862 }
2863 }
2864 }
2865
Michael Jurkab3e22d92011-10-31 15:58:33 -07002866 void lockAllApps() {
2867 // TODO
2868 }
2869
2870 void unlockAllApps() {
2871 // TODO
2872 }
2873
Winson Chungf0ea4d32011-06-06 14:27:16 -07002874 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002875 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002877 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002878 if (!LauncherApplication.isScreenLarge()) {
2879 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002880 if (mHotseat.getAlpha() != 1f) {
2881 int duration = mSearchDropTargetBar.getTransitionInDuration();
2882 mHotseat.animate().alpha(1f).setDuration(duration);
2883 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002884 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002885 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002886 }
2887 }
2888 }
2889
2890 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002891 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002892 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002893 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002894 if (!LauncherApplication.isScreenLarge()) {
2895 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002896 if (mHotseat.getAlpha() != 0f) {
2897 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2898 mHotseat.animate().alpha(0f).setDuration(duration);
2899 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002900 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002903 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002904 }
2905
Patrick Dubroy5f445422011-02-18 14:35:21 -08002906 /**
2907 * Add an item from all apps or customize onto the given workspace screen.
2908 * If layout is null, add to the current screen.
2909 */
2910 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002911 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002912 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002913 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002914 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002915
Winson Chungdff8ebb2011-09-08 17:25:31 -07002916 /** Maps the current orientation to an index for referencing orientation correct global icons */
2917 private int getCurrentOrientationIndexForGlobalIcons() {
2918 // default - 0, landscape - 1
2919 switch (getResources().getConfiguration().orientation) {
2920 case Configuration.ORIENTATION_LANDSCAPE:
2921 return 1;
2922 default:
2923 return 0;
2924 }
2925 }
2926
Michael Jurkaae65ee32012-04-30 11:45:54 -07002927 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002928 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002929 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002930 // Look for the toolbar icon specified in the activity meta-data
2931 Bundle metaData = packageManager.getActivityInfo(
2932 activityName, PackageManager.GET_META_DATA).metaData;
2933 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002934 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002935 if (iconResId != 0) {
2936 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002937 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002938 }
2939 }
2940 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002941 // This can happen if the activity defines an invalid drawable
2942 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2943 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002944 } catch (Resources.NotFoundException nfe) {
2945 // This can happen if the activity defines an invalid drawable
2946 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2947 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002948 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002949 return null;
2950 }
2951
2952 // if successful in getting icon, return it; otherwise, set button to use default drawable
2953 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002954 int buttonId, ComponentName activityName, int fallbackDrawableId,
2955 String toolbarResourceName) {
2956 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002957 Resources r = getResources();
2958 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2959 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002960
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002961 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002962 // If we were unable to find the icon via the meta-data, use a generic one
2963 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002964 toolbarIcon = r.getDrawable(fallbackDrawableId);
2965 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002966 if (button != null) {
2967 button.setCompoundDrawables(toolbarIcon, null, null, null);
2968 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002969 return null;
2970 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002971 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002972 if (button != null) {
2973 button.setCompoundDrawables(toolbarIcon, null, null, null);
2974 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002975 return toolbarIcon.getConstantState();
2976 }
2977 }
2978
2979 // if successful in getting icon, return it; otherwise, set button to use default drawable
2980 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002981 int buttonId, ComponentName activityName, int fallbackDrawableId,
2982 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002983 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002984 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002985
Michael Jurka19e0fc52011-07-22 18:00:21 -07002986 if (button != null) {
2987 // If we were unable to find the icon via the meta-data, use a
2988 // generic one
2989 if (toolbarIcon == null) {
2990 button.setImageResource(fallbackDrawableId);
2991 } else {
2992 button.setImageDrawable(toolbarIcon);
2993 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002994 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002995
2996 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2997
Michael Jurka0423dcf2010-10-05 14:56:18 -07002998 }
2999
Winson Chung1b884092012-06-08 17:13:02 -07003000 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003001 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003002 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003003 }
3004
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003005 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003006 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003007 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003008 }
3009
Winson Chungbb185bd2011-11-21 12:31:42 -08003010 private void invalidatePressedFocusedStates(View container, View button) {
3011 if (container instanceof HolographicLinearLayout) {
3012 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3013 layout.invalidatePressedFocusedStates();
3014 } else if (button instanceof HolographicImageView) {
3015 HolographicImageView view = (HolographicImageView) button;
3016 view.invalidatePressedFocusedStates();
3017 }
3018 }
3019
Winson Chungc51db6a2011-10-05 11:44:49 -07003020 private boolean updateGlobalSearchIcon() {
3021 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003022 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3023 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003024 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003025 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003026 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003027
Winson Chung1cad91e2011-05-25 17:41:01 -07003028 final SearchManager searchManager =
3029 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3030 ComponentName activityName = searchManager.getGlobalSearchActivity();
3031 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003032 int coi = getCurrentOrientationIndexForGlobalIcons();
3033 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003034 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3035 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3036 if (sGlobalSearchIcon[coi] == null) {
3037 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3038 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3039 TOOLBAR_ICON_METADATA_NAME);
3040 }
3041
Winson Chung649723c2011-07-06 20:41:23 -07003042 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003043 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3044 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003045 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003046 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003047 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003048 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003049 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003050 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3051 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3052 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003053 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003054 if (voiceButtonProxy != null) {
3055 voiceButtonProxy.setVisibility(View.GONE);
3056 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003057 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003058 }
3059 }
3060
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003061 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003062 final View searchButtonContainer = findViewById(R.id.search_button_container);
3063 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003064 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003065 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003066 }
3067
Winson Chungc51db6a2011-10-05 11:44:49 -07003068 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003069 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003070 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003071 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003072 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003073
Winson Chungc51db6a2011-10-05 11:44:49 -07003074 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003075 final SearchManager searchManager =
3076 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3077 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3078
3079 ComponentName activityName = null;
3080 if (globalSearchActivity != null) {
3081 // Check if the global search activity handles voice search
3082 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3083 intent.setPackage(globalSearchActivity.getPackageName());
3084 activityName = intent.resolveActivity(getPackageManager());
3085 }
3086
3087 if (activityName == null) {
3088 // Fallback: check if an activity other than the global search activity
3089 // resolves this
3090 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3091 activityName = intent.resolveActivity(getPackageManager());
3092 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003093 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003094 int coi = getCurrentOrientationIndexForGlobalIcons();
3095 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003096 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3097 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3098 if (sVoiceSearchIcon[coi] == null) {
3099 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3100 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3101 TOOLBAR_ICON_METADATA_NAME);
3102 }
Winson Chung649723c2011-07-06 20:41:23 -07003103 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003104 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003105 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003106 if (voiceButtonProxy != null) {
3107 voiceButtonProxy.setVisibility(View.VISIBLE);
3108 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003109 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003110 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003111 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003112 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003113 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003114 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003115 if (voiceButtonProxy != null) {
3116 voiceButtonProxy.setVisibility(View.GONE);
3117 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003118 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003119 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003120 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003121
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003122 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003123 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3124 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003125 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003126 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003127 }
3128
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003129 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003130 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003131 */
3132 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003133 final View marketButton = findViewById(R.id.market_button);
3134 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3135 // Find the app market activity by resolving an intent.
3136 // (If multiple app markets are installed, it will return the ResolverActivity.)
3137 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003138 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003139 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003140 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003141 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003142 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3143 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003144 marketButton.setVisibility(View.VISIBLE);
3145 } else {
3146 // We should hide and disable the view so that we don't try and restore the visibility
3147 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3148 marketButton.setVisibility(View.GONE);
3149 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003151 }
3152
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003153 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003154 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3155 Resources r = getResources();
3156 Drawable marketIconDrawable = d.newDrawable(r);
3157 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3158 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3159 marketIconDrawable.setBounds(0, 0, w, h);
3160
3161 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003162 }
3163
Michael Jurkad7c28052012-04-27 15:43:36 -07003164 @Override
3165 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3166 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3167 final List<CharSequence> text = event.getText();
3168 text.clear();
3169 text.add(getString(R.string.home));
3170 return result;
3171 }
3172
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003173 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003174 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003175 */
3176 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3177 @Override
3178 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003179 closeSystemDialogs();
3180 }
3181 }
3182
3183 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003184 * Receives notifications whenever the appwidgets are reset.
3185 */
3186 private class AppWidgetResetObserver extends ContentObserver {
3187 public AppWidgetResetObserver() {
3188 super(new Handler());
3189 }
3190
3191 @Override
3192 public void onChange(boolean selfChange) {
3193 onAppWidgetReset();
3194 }
3195 }
3196
3197 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003198 * If the activity is currently paused, signal that we need to re-run the loader
3199 * in onResume.
3200 *
3201 * This needs to be called from incoming places where resources might have been loaded
3202 * while we are paused. That is becaues the Configuration might be wrong
3203 * when we're not running, and if it comes back to what it was when we
3204 * were paused, we are not restarted.
3205 *
3206 * Implementation of the method from LauncherModel.Callbacks.
3207 *
3208 * @return true if we are currently paused. The caller might be able to
3209 * skip some work in that case since we will come back again.
3210 */
3211 public boolean setLoadOnResume() {
3212 if (mPaused) {
3213 Log.i(TAG, "setLoadOnResume");
3214 mOnResumeNeedsLoad = true;
3215 return true;
3216 } else {
3217 return false;
3218 }
3219 }
3220
3221 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003222 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003225 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003226 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003227 } else {
3228 return SCREEN_COUNT / 2;
3229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003231
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 /**
3233 * Refreshes the shortcuts shown on the workspace.
3234 *
3235 * Implementation of the method from LauncherModel.Callbacks.
3236 */
3237 public void startBinding() {
3238 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003239
Winson Chungf0c6ae02012-03-21 16:10:31 -07003240 mNewShortcutAnimatePage = -1;
3241 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003242 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 int count = workspace.getChildCount();
3244 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003245 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003246 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3247 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003249 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003250 if (mHotseat != null) {
3251 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 }
3253 }
3254
3255 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 * Bind the items start-end from the list.
3257 *
3258 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003259 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003260 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003261 setLoadOnResume();
3262
Winson Chungf0c6ae02012-03-21 16:10:31 -07003263 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3264 Set<String> newApps = new HashSet<String>();
3265 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3266
3267 Workspace workspace = mWorkspace;
3268 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003270
3271 // Short circuit if we are loading dock items for a configuration which has no dock
3272 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3273 mHotseat == null) {
3274 continue;
3275 }
3276
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 switch (item.itemType) {
3278 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3279 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003280 ShortcutInfo info = (ShortcutInfo) item;
3281 String uri = info.intent.toUri(0).toString();
3282 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003283 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3284 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003285 boolean animateIconUp = false;
3286 synchronized (newApps) {
3287 if (newApps.contains(uri)) {
3288 animateIconUp = newApps.remove(uri);
3289 }
3290 }
3291 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003292 // Prepare the view to be animated up
3293 shortcut.setAlpha(0f);
3294 shortcut.setScaleX(0f);
3295 shortcut.setScaleY(0f);
3296 mNewShortcutAnimatePage = item.screen;
3297 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3298 mNewShortcutAnimateViews.add(shortcut);
3299 }
3300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003302 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003303 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003304 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003305 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003306 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3307 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003308 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003309 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003310 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003311
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003313 }
3314
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 /**
3316 * Implementation of the method from LauncherModel.Callbacks.
3317 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003318 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003319 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003320 sFolders.clear();
3321 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323
3324 /**
3325 * Add the views for a widget to the workspace.
3326 *
3327 * Implementation of the method from LauncherModel.Callbacks.
3328 */
3329 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003330 setLoadOnResume();
3331
Daniel Sandler843e8602010-06-07 14:59:01 -04003332 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3333 if (DEBUG_WIDGETS) {
3334 Log.d(TAG, "bindAppWidget: " + item);
3335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 final Workspace workspace = mWorkspace;
3337
3338 final int appWidgetId = item.appWidgetId;
3339 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003340 if (DEBUG_WIDGETS) {
3341 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3342 }
3343
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3345
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003347 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348
Winson Chung3d503fb2011-07-13 17:25:49 -07003349 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003351 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3352
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 workspace.requestLayout();
3354
Daniel Sandler843e8602010-06-07 14:59:01 -04003355 if (DEBUG_WIDGETS) {
3356 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3357 + (SystemClock.uptimeMillis()-start) + "ms");
3358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 }
3360
Adam Cohen1462de32012-07-24 22:34:36 -07003361 public void onPageBoundSynchronously(int page) {
3362 mSynchronouslyBoundPages.add(page);
3363 }
3364
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 /**
3366 * Callback saying that there aren't any more items to bind.
3367 *
3368 * Implementation of the method from LauncherModel.Callbacks.
3369 */
3370 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003371 setLoadOnResume();
3372
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 if (mSavedState != null) {
3374 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003375 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003376 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 mSavedState = null;
3378 }
3379
Adam Cohen1462de32012-07-24 22:34:36 -07003380 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003382 // If we received the result of any pending adds while the loader was running (e.g. the
3383 // widget configuration forced an orientation change), process them now.
3384 for (int i = 0; i < sPendingAddList.size(); i++) {
3385 completeAdd(sPendingAddList.get(i));
3386 }
3387 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388
Winson Chungc51db6a2011-10-05 11:44:49 -07003389 // Update the market app icon as necessary (the other icons will be managed in response to
3390 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003391 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003392
Winson Chungf0c6ae02012-03-21 16:10:31 -07003393 // Animate up any icons as necessary
3394 if (mVisible || mWorkspaceLoading) {
3395 Runnable newAppsRunnable = new Runnable() {
3396 @Override
3397 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003398 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003399 }
3400 };
Winson Chunga2413752012-04-03 14:22:34 -07003401
3402 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3403 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3404 if (canRunNewAppsAnimation()) {
3405 // If the user has not interacted recently, then either snap to the new page to show
3406 // the new-apps animation or just run them if they are to appear on the current page
3407 if (willSnapPage) {
3408 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3409 } else {
3410 runNewAppsAnimation(false);
3411 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003412 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003413 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003414 // are on another page (or just normally if they are added to the current page)
3415 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003416 }
3417 }
3418
3419 mWorkspaceLoading = false;
3420 }
3421
Winson Chunga2413752012-04-03 14:22:34 -07003422 private boolean canRunNewAppsAnimation() {
3423 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3424 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3425 }
3426
Winson Chungf0c6ae02012-03-21 16:10:31 -07003427 /**
3428 * Runs a new animation that scales up icons that were added while Launcher was in the
3429 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003430 *
3431 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003432 */
Winson Chunga2413752012-04-03 14:22:34 -07003433 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003434 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003435 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003436
3437 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003438 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3439 @Override
3440 public int compare(View a, View b) {
3441 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3442 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3443 int cellCountX = LauncherModel.getCellCountX();
3444 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3445 }
3446 });
Winson Chunga2413752012-04-03 14:22:34 -07003447
3448 // Animate each of the views in place (or show them immediately if requested)
3449 if (immediate) {
3450 for (View v : mNewShortcutAnimateViews) {
3451 v.setAlpha(1f);
3452 v.setScaleX(1f);
3453 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003454 }
Winson Chunga2413752012-04-03 14:22:34 -07003455 } else {
3456 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3457 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003458 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003459 PropertyValuesHolder.ofFloat("alpha", 1f),
3460 PropertyValuesHolder.ofFloat("scaleX", 1f),
3461 PropertyValuesHolder.ofFloat("scaleY", 1f));
3462 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3463 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3464 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3465 bounceAnims.add(bounceAnim);
3466 }
3467 anim.playTogether(bounceAnims);
3468 anim.addListener(new AnimatorListenerAdapter() {
3469 @Override
3470 public void onAnimationEnd(Animator animation) {
3471 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3472 }
3473 });
3474 anim.start();
3475 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003476
3477 // Clean up
3478 mNewShortcutAnimatePage = -1;
3479 mNewShortcutAnimateViews.clear();
3480 new Thread("clearNewAppsThread") {
3481 public void run() {
3482 mSharedPrefs.edit()
3483 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3484 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3485 .commit();
3486 }
3487 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003488 }
3489
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003490 @Override
3491 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003492 boolean searchVisible = updateGlobalSearchIcon();
3493 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3494 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003495 }
3496
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003497 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 * Add the icons for all apps.
3499 *
3500 * Implementation of the method from LauncherModel.Callbacks.
3501 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003502 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003503 Runnable setAllAppsRunnable = new Runnable() {
3504 public void run() {
3505 if (mAppsCustomizeContent != null) {
3506 mAppsCustomizeContent.setApps(apps);
3507 }
3508 }
3509 };
3510
Michael Jurkac57b7a82011-08-09 22:02:20 -07003511 // Remove the progress bar entirely; we could also make it GONE
3512 // but better to remove it since we know it's not going to be used
3513 View progressBar = mAppsCustomizeTabHost.
3514 findViewById(R.id.apps_customize_progress_bar);
3515 if (progressBar != null) {
3516 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003517
3518 // We just post the call to setApps so the user sees the progress bar
3519 // disappear-- otherwise, it just looks like the progress bar froze
3520 // which doesn't look great
3521 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3522 } else {
3523 // If we did not initialize the spinner in onCreate, then we can directly set the
3524 // list of applications without waiting for any progress bars views to be hidden.
3525 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003526 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
3528
3529 /**
3530 * A package was installed.
3531 *
3532 * Implementation of the method from LauncherModel.Callbacks.
3533 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003534 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003535 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003536
Winson Chung785d2eb2011-04-14 16:08:02 -07003537 if (mAppsCustomizeContent != null) {
3538 mAppsCustomizeContent.addApps(apps);
3539 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 }
3541
3542 /**
3543 * A package was updated.
3544 *
3545 * Implementation of the method from LauncherModel.Callbacks.
3546 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003547 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003548 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003549 if (mWorkspace != null) {
3550 mWorkspace.updateShortcuts(apps);
3551 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003552
Winson Chung785d2eb2011-04-14 16:08:02 -07003553 if (mAppsCustomizeContent != null) {
3554 mAppsCustomizeContent.updateApps(apps);
3555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 }
3557
3558 /**
3559 * A package was uninstalled.
3560 *
3561 * Implementation of the method from LauncherModel.Callbacks.
3562 */
Winson Chungcd810732012-06-18 16:45:43 -07003563 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003564 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003565 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003566 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003567
Winson Chung785d2eb2011-04-14 16:08:02 -07003568 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003569 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003570 }
Winson Chunga1820962011-10-03 16:31:06 -07003571
3572 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003573 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 }
Joe Onoratobe386092009-11-17 17:32:16 -08003575
3576 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003577 * A number of packages were updated.
3578 */
3579 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003580 if (mAppsCustomizeContent != null) {
3581 mAppsCustomizeContent.onPackagesUpdated();
3582 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003583 }
3584
Winson Chung400438b2011-01-16 17:53:48 -08003585 private int mapConfigurationOriActivityInfoOri(int configOri) {
3586 final Display d = getWindowManager().getDefaultDisplay();
3587 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3588 switch (d.getRotation()) {
3589 case Surface.ROTATION_0:
3590 case Surface.ROTATION_180:
3591 // We are currently in the same basic orientation as the natural orientation
3592 naturalOri = configOri;
3593 break;
3594 case Surface.ROTATION_90:
3595 case Surface.ROTATION_270:
3596 // We are currently in the other basic orientation to the natural orientation
3597 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3598 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3599 break;
3600 }
3601
3602 int[] oriMap = {
3603 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3604 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3605 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3606 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3607 };
3608 // Since the map starts at portrait, we need to offset if this device's natural orientation
3609 // is landscape.
3610 int indexOffset = 0;
3611 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3612 indexOffset = 1;
3613 }
3614 return oriMap[(d.getRotation() + indexOffset) % 4];
3615 }
Adam Cohen446e9402011-09-15 18:21:21 -07003616
Winson Chung4b919f82012-05-01 10:44:08 -07003617 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003618 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003619 boolean enableRotation = forceEnableRotation ||
3620 getResources().getBoolean(R.bool.allow_rotation);
3621 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003622 }
Winson Chung4b919f82012-05-01 10:44:08 -07003623 public void lockScreenOrientation() {
3624 if (isRotationEnabled()) {
3625 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3626 .getConfiguration().orientation));
3627 }
3628 }
3629 public void unlockScreenOrientation(boolean immediate) {
3630 if (isRotationEnabled()) {
3631 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003632 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003633 } else {
3634 mHandler.postDelayed(new Runnable() {
3635 public void run() {
3636 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3637 }
3638 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003639 }
Winson Chung4b919f82012-05-01 10:44:08 -07003640 }
Winson Chung400438b2011-01-16 17:53:48 -08003641 }
3642
Winson Chung82f55532011-08-09 14:14:23 -07003643 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003644 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003645 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003646 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003647
Winson Chung7d7541e2011-09-16 20:14:36 -07003648 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003649 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003650 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3651 Cling cling = (Cling) findViewById(clingId);
3652 if (cling != null) {
3653 cling.init(this, positionData);
3654 cling.setVisibility(View.VISIBLE);
3655 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3656 if (animate) {
3657 cling.buildLayer();
3658 cling.setAlpha(0f);
3659 cling.animate()
3660 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003661 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003662 .setDuration(SHOW_CLING_DURATION)
3663 .setStartDelay(delay)
3664 .start();
3665 } else {
3666 cling.setAlpha(1f);
3667 }
3668 }
3669 return cling;
3670 }
3671 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003672 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003673 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003674 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003675 anim.addListener(new AnimatorListenerAdapter() {
3676 public void onAnimationEnd(Animator animation) {
3677 cling.setVisibility(View.GONE);
3678 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003679 // We should update the shared preferences on a background thread
3680 new Thread("dismissClingThread") {
3681 public void run() {
3682 SharedPreferences.Editor editor = mSharedPrefs.edit();
3683 editor.putBoolean(flag, true);
3684 editor.commit();
3685 }
3686 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003687 };
3688 });
3689 anim.start();
3690 }
3691 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003692 private void removeCling(int id) {
3693 final View cling = findViewById(id);
3694 if (cling != null) {
3695 final ViewGroup parent = (ViewGroup) cling.getParent();
3696 parent.post(new Runnable() {
3697 @Override
3698 public void run() {
3699 parent.removeView(cling);
3700 }
3701 });
3702 }
3703 }
Michael Jurka974c3862012-05-22 22:00:31 -07003704
3705 private boolean skipCustomClingIfNoAccounts() {
3706 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3707 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3708 if (customCling) {
3709 AccountManager am = AccountManager.get(this);
3710 Account[] accounts = am.getAccountsByType("com.google");
3711 return accounts.length == 0;
3712 }
3713 return false;
3714 }
3715
Winson Chung7d7541e2011-09-16 20:14:36 -07003716 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003717 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003718 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003719 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3720 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003721 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003722 } else {
3723 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003724 }
3725 }
3726 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003727 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003728 if (isClingsEnabled() &&
3729 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003730 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003731 } else {
3732 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003733 }
3734 }
3735 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003736 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003737 if (isClingsEnabled() &&
3738 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3739 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003740 } else {
3741 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003742 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003743 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003744 }
3745 public boolean isFolderClingVisible() {
3746 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003747 if (cling != null) {
3748 return cling.getVisibility() == View.VISIBLE;
3749 }
3750 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003751 }
Winson Chung82f55532011-08-09 14:14:23 -07003752 public void dismissWorkspaceCling(View v) {
3753 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003754 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003755 }
3756 public void dismissAllAppsCling(View v) {
3757 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003758 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3759 }
3760 public void dismissFolderCling(View v) {
3761 Cling cling = (Cling) findViewById(R.id.folder_cling);
3762 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003763 }
3764
Winson Chung80baf5a2010-08-09 16:03:15 -07003765 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003766 * Prints out out state for debugging.
3767 */
3768 public void dumpState() {
3769 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003770 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003771 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3772 Log.d(TAG, "mRestoring=" + mRestoring);
3773 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3774 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003775 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003776 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003777
Winson Chung785d2eb2011-04-14 16:08:02 -07003778 if (mAppsCustomizeContent != null) {
3779 mAppsCustomizeContent.dumpState();
3780 }
Joe Onoratobe386092009-11-17 17:32:16 -08003781 Log.d(TAG, "END launcher2 dump state");
3782 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003783
3784 @Override
3785 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3786 super.dump(prefix, fd, writer, args);
3787 writer.println(" ");
3788 writer.println("Debug logs: ");
3789 for (int i = 0; i < sDumpLogs.size(); i++) {
3790 writer.println(" " + sDumpLogs.get(i));
3791 }
3792 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003793
3794 public static void dumpDebugLogsToConsole() {
3795 Log.d(TAG, "");
3796 Log.d(TAG, "*********************");
3797 Log.d(TAG, "Launcher debug logs: ");
3798 for (int i = 0; i < sDumpLogs.size(); i++) {
3799 Log.d(TAG, " " + sDumpLogs.get(i));
3800 }
3801 Log.d(TAG, "*********************");
3802 Log.d(TAG, "");
3803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804}
Michael Jurka2763be32011-02-24 11:19:57 -08003805
Michael Jurkaabded662011-03-04 12:06:57 -08003806interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003807 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003808 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003809 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003810 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003811 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003812}