blob: e6b237b20d79b9fe1f113ce1c1bcf1304d1a439f [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 Chungadf0c182012-08-23 14:59:07 -0700454 if (mSearchDropTargetBar != null) {
455 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
Romain Guycbb89e42009-06-08 15:52:54 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 if (sLocaleConfiguration == null) {
461 new AsyncTask<Void, Void, LocaleConfiguration>() {
462 @Override
463 protected LocaleConfiguration doInBackground(Void... unused) {
464 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
465 readConfiguration(Launcher.this, localeConfiguration);
466 return localeConfiguration;
467 }
468
469 @Override
470 protected void onPostExecute(LocaleConfiguration result) {
471 sLocaleConfiguration = result;
472 checkForLocaleChange(); // recursive, but now with a locale configuration
473 }
474 }.execute();
475 return;
476 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final Configuration configuration = getResources().getConfiguration();
479
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final String locale = configuration.locale.toString();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final int mcc = configuration.mcc;
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mnc = configuration.mnc;
488
Romain Guy84f296c2009-11-04 15:00:44 -0800489 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
Romain Guy84f296c2009-11-04 15:00:44 -0800491 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 sLocaleConfiguration.locale = locale;
493 sLocaleConfiguration.mcc = mcc;
494 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700495
Joe Onorato0589f0f2010-02-08 13:44:00 -0800496 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497
498 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
499 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700500 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 public void run() {
502 writeConfiguration(Launcher.this, localeConfiguration);
503 }
504 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700505 }
506 }
507
508 private static class LocaleConfiguration {
509 public String locale;
510 public int mcc = -1;
511 public int mnc = -1;
512 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700513
Romain Guy98d01652009-06-30 16:21:04 -0700514 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
515 DataInputStream in = null;
516 try {
517 in = new DataInputStream(context.openFileInput(PREFERENCES));
518 configuration.locale = in.readUTF();
519 configuration.mcc = in.readInt();
520 configuration.mnc = in.readInt();
521 } catch (FileNotFoundException e) {
522 // Ignore
523 } catch (IOException e) {
524 // Ignore
525 } finally {
526 if (in != null) {
527 try {
528 in.close();
529 } catch (IOException e) {
530 // Ignore
531 }
532 }
533 }
534 }
535
536 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
537 DataOutputStream out = null;
538 try {
539 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
540 out.writeUTF(configuration.locale);
541 out.writeInt(configuration.mcc);
542 out.writeInt(configuration.mnc);
543 out.flush();
544 } catch (FileNotFoundException e) {
545 // Ignore
546 } catch (IOException e) {
547 //noinspection ResultOfMethodCallIgnored
548 context.getFileStreamPath(PREFERENCES).delete();
549 } finally {
550 if (out != null) {
551 try {
552 out.close();
553 } catch (IOException e) {
554 // Ignore
555 }
556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558 }
559
Adam Cohen716b51e2011-06-30 12:09:54 -0700560 public DragLayer getDragLayer() {
561 return mDragLayer;
562 }
563
Winson Chung36a62fe2012-05-06 18:04:42 -0700564 boolean isDraggingEnabled() {
565 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
566 // that is subsequently removed from the workspace in startBinding().
567 return !mModel.isLoadingWorkspace();
568 }
569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 static int getScreen() {
571 synchronized (sLock) {
572 return sScreen;
573 }
574 }
575
576 static void setScreen(int screen) {
577 synchronized (sLock) {
578 sScreen = screen;
579 }
580 }
581
Winson Chung557d6ed2011-07-08 15:34:52 -0700582 /**
583 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
584 * a configuration step, this allows the proper animations to run after other transitions.
585 */
586 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700587 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 switch (args.requestCode) {
589 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700590 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
591 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 break;
593 case REQUEST_PICK_SHORTCUT:
594 processShortcut(args.intent);
595 break;
596 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700597 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
598 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700599 result = true;
600 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 case REQUEST_CREATE_APPWIDGET:
602 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700604 result = true;
605 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 case REQUEST_PICK_WALLPAPER:
607 // We just wanted the activity result here so we can clear mWaitingForResult
608 break;
609 }
Michael Jurka27614d22012-04-02 06:40:22 -0700610 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
611 // if you turned the screen off and then back while in All Apps, Launcher would not
612 // return to the workspace. Clearing mAddInfo.container here fixes this issue
613 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700614 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800615 }
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700618 protected void onActivityResult(
619 final int requestCode, final int resultCode, final Intent data) {
620 if (requestCode == REQUEST_BIND_APPWIDGET) {
621 int appWidgetId = data != null ?
622 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
623 if (resultCode == RESULT_CANCELED) {
624 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
625 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700626 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700627 }
628 return;
629 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700630 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800631 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
632 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700633 mWaitingForResult = false;
634
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 // We have special handling for widgets
636 if (isWidgetDrop) {
637 int appWidgetId = data != null ?
638 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700639 if (appWidgetId < 0) {
640 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
641 "widget configuration activity.");
642 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
643 } else {
644 completeTwoStageWidgetDrop(resultCode, appWidgetId);
645 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 return;
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 // The pattern used here is that a user PICKs a specific application,
650 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
653 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700654 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800655 final PendingAddArguments args = new PendingAddArguments();
656 args.requestCode = requestCode;
657 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700658 args.container = mPendingAddInfo.container;
659 args.screen = mPendingAddInfo.screen;
660 args.cellX = mPendingAddInfo.cellX;
661 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800662 if (isWorkspaceLocked()) {
663 sPendingAddList.add(args);
664 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700665 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800670 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
671 null);
672 }
673
674 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700676 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800677 Runnable onCompleteRunnable = null;
678 int animationType = 0;
679
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 if (resultCode == RESULT_OK) {
682 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
683 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 mPendingAddWidgetInfo);
685 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 onCompleteRunnable = new Runnable() {
687 @Override
688 public void run() {
689 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
690 mPendingAddInfo.screen, layout, null);
691 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
692 null);
693 }
694 };
695 } else if (resultCode == RESULT_CANCELED) {
696 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
697 onCompleteRunnable = new Runnable() {
698 @Override
699 public void run() {
700 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
701 null);
702 }
703 };
704 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700705 if (mDragLayer.getAnimatedView() != null) {
706 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
707 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
708 animationType, boundWidget, true);
709 } else {
710 // The animated view may be null in the case of a rotation during widget configuration
711 onCompleteRunnable.run();
712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714
715 @Override
716 protected void onResume() {
717 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700718
Winson Chung4a2afa32012-07-19 14:53:05 -0700719 // Restore the previous launcher state
720 if (mOnResumeState == State.WORKSPACE) {
721 showWorkspace(false);
722 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
723 showAllApps(false);
724 }
725 mOnResumeState = State.NONE;
726
Winson Chungde0fb8f2012-05-08 14:37:08 -0700727 // Process any items that were added while Launcher was away
728 InstallShortcutReceiver.flushInstallQueue(this);
729
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800730 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700731 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700732 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700734 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700736 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
Winson Chunge4e50662012-01-23 14:45:13 -0800738
739 // Reset the pressed state of icons that were locked in the press state while activities
740 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800741 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800742 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800743 mWaitingForResume.setStayPressed(false);
744 }
Winson Chunge4e50662012-01-23 14:45:13 -0800745 if (mAppsCustomizeContent != null) {
746 // Resets the previous all apps icon press state
747 mAppsCustomizeContent.resetDrawableState();
748 }
Adam Cohen06dff352012-06-01 17:17:08 -0700749 // It is possible that widgets can receive updates while launcher is not in the foreground.
750 // Consequently, the widgets will be inflated in the orientation of the foreground activity
751 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
752 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700753 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700754
755 // Again, as with the above scenario, it's possible that one or more of the global icons
756 // were updated in the wrong orientation.
757 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700758 }
759
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700761 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700762 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
763 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
764 // when Launcher resumes and we are still in AllApps.
765 updateWallpaperVisibility(true);
766
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700768 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800769 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700770 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 }
Romain Guycbb89e42009-06-08 15:52:54 -0700772
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700773 @Override
774 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400775 // Flag the loader to stop early before switching
776 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700777 if (mAppsCustomizeContent != null) {
778 mAppsCustomizeContent.surrender();
779 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800780 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700781 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700782
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800783 // We can't hide the IME if it was forced open. So don't bother
784 /*
785 @Override
786 public void onWindowFocusChanged(boolean hasFocus) {
787 super.onWindowFocusChanged(hasFocus);
788
789 if (hasFocus) {
790 final InputMethodManager inputManager = (InputMethodManager)
791 getSystemService(Context.INPUT_METHOD_SERVICE);
792 WindowManager.LayoutParams lp = getWindow().getAttributes();
793 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
794 android.os.Handler()) {
795 protected void onReceiveResult(int resultCode, Bundle resultData) {
796 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
797 }
798 });
799 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
800 }
801 }
802 */
803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 private boolean acceptFilter() {
805 final InputMethodManager inputManager = (InputMethodManager)
806 getSystemService(Context.INPUT_METHOD_SERVICE);
807 return !inputManager.isFullscreenMode();
808 }
809
810 @Override
811 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700812 final int uniChar = event.getUnicodeChar();
813 final boolean handled = super.onKeyDown(keyCode, event);
814 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
815 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
817 keyCode, event);
818 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700819 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700820 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700821 // showSearchDialog()
822 // If there are multiple keystrokes before the search dialog takes focus,
823 // onSearchRequested() will be called for every keystroke,
824 // but it is idempotent, so it's fine.
825 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 }
827 }
828
Joe Onorato8a9625e2010-01-28 15:55:35 -0800829 // Eat the long press event so the keyboard doesn't come up.
830 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
831 return true;
832 }
833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 return handled;
835 }
836
Karl Rosaen138a0412009-04-23 19:00:21 -0700837 private String getTypedText() {
838 return mDefaultKeySsb.toString();
839 }
840
841 private void clearTypedText() {
842 mDefaultKeySsb.clear();
843 mDefaultKeySsb.clearSpans();
844 Selection.setSelection(mDefaultKeySsb, 0);
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700848 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
849 * State
850 */
851 private static State intToState(int stateOrdinal) {
852 State state = State.WORKSPACE;
853 final State[] stateValues = State.values();
854 for (int i = 0; i < stateValues.length; i++) {
855 if (stateValues[i].ordinal() == stateOrdinal) {
856 state = stateValues[i];
857 break;
858 }
859 }
860 return state;
861 }
862
863 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 * Restores the previous state, if it exists.
865 *
866 * @param savedState The previous state.
867 */
868 private void restoreState(Bundle savedState) {
869 if (savedState == null) {
870 return;
871 }
872
Michael Jurka883f55b2010-10-21 15:47:14 -0700873 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700874 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700875 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800876 }
877
Winson Chungf0c6ae02012-03-21 16:10:31 -0700878 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700880 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
882
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
884 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700885
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
887 mPendingAddInfo.container = pendingAddContainer;
888 mPendingAddInfo.screen = pendingAddScreen;
889 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
890 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700891 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
892 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
893 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700894 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 mRestoring = true;
896 }
897
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
900 if (renameFolder) {
901 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700902 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 mRestoring = true;
904 }
Winson Chunga12a2502010-12-20 14:41:35 -0800905
Winson Chung785d2eb2011-04-14 16:08:02 -0700906
907 // Restore the AppsCustomize tab
908 if (mAppsCustomizeTabHost != null) {
909 String curTab = savedState.getString("apps_customize_currentTab");
910 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700911 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700912 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700913 mAppsCustomizeContent.loadAssociatedPages(
914 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700915 }
916
Winson Chung5afbf7b2011-07-25 11:53:08 -0700917 int currentIndex = savedState.getInt("apps_customize_currentIndex");
918 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 }
921
922 /**
923 * Finds all the views we need and configure them properly.
924 */
925 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700926 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400927
Winson Chung4c98d922011-05-31 16:50:48 -0700928 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
929 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800930 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
931 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932
Winson Chung4c98d922011-05-31 16:50:48 -0700933 // Setup the drag layer
934 mDragLayer.setup(this, dragController);
935
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 // Setup the hotseat
937 mHotseat = (Hotseat) findViewById(R.id.hotseat);
938 if (mHotseat != null) {
939 mHotseat.setup(this);
940 }
941
Winson Chung4c98d922011-05-31 16:50:48 -0700942 // Setup the workspace
943 mWorkspace.setHapticFeedbackEnabled(false);
944 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700945 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700946 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700947
Winson Chungf0ea4d32011-06-06 14:27:16 -0700948 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700949 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700950
Winson Chungf0ea4d32011-06-06 14:27:16 -0700951 // Setup AppsCustomize
952 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
953 findViewById(R.id.apps_customize_pane);
954 mAppsCustomizeContent = (AppsCustomizePagedView)
955 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
956 mAppsCustomizeContent.setup(this, dragController);
Winson Chung0e721a42012-08-02 17:40:30 -0700957
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700959 dragController.setDragScoller(mWorkspace);
960 dragController.setScrollView(mDragLayer);
961 dragController.setMoveTarget(mWorkspace);
962 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700963 if (mSearchDropTargetBar != null) {
964 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 }
967
968 /**
969 * Creates a view representing a shortcut.
970 *
971 * @param info The data structure describing the shortcut.
972 *
973 * @return A View inflated from R.layout.application.
974 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800975 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700977 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
979
980 /**
981 * Creates a view representing a shortcut inflated from the specified resource.
982 *
983 * @param layoutResId The id of the XML layout used to create the shortcut.
984 * @param parent The group the shortcut belongs to.
985 * @param info The data structure describing the shortcut.
986 *
987 * @return A View inflated from layoutResId.
988 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800990 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
991 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 return favorite;
994 }
995
996 /**
997 * Add an application shortcut to the workspace.
998 *
999 * @param data The intent describing the application.
1000 * @param cellInfo The position on screen where to create the shortcut.
1001 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001003 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001004 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001005
Adam Cohenfbba09b2011-07-18 21:43:05 -07001006 // First we check if we already know the exact location where we want to add this item.
1007 if (cellX >= 0 && cellY >= 0) {
1008 cellXY[0] = cellX;
1009 cellXY[1] = cellY;
1010 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001011 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 return;
1013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001015 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016
Romain Guy73b979d2009-06-09 12:57:21 -07001017 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001018 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001020 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001021 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001022 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001023 } else {
1024 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026 }
Romain Guycbb89e42009-06-08 15:52:54 -07001027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 /**
1029 * Add a shortcut to the workspace.
1030 *
1031 * @param data The intent describing the shortcut.
1032 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001034 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1035 int cellY) {
1036 int[] cellXY = mTmpAddItemCellCoordinates;
1037 int[] touchXY = mPendingAddInfo.dropPos;
1038 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Michael Jurkad3ef3062010-11-23 16:23:58 -08001040 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001041
Adam Cohen558baaf2011-08-15 15:22:57 -07001042 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001043 if (info == null) {
1044 return;
1045 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001046 final View view = createShortcut(info);
1047
Adam Cohenfbba09b2011-07-18 21:43:05 -07001048 // First we check if we already know the exact location where we want to add this item.
1049 if (cellX >= 0 && cellY >= 0) {
1050 cellXY[0] = cellX;
1051 cellXY[1] = cellY;
1052 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001053
1054 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001055 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001056 true, null,null)) {
1057 return;
1058 }
1059 DragObject dragObject = new DragObject();
1060 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001061 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1062 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001063 return;
1064 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001065 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001066 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001068 foundCellSpan = (result != null);
1069 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001070 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001071 }
1072
1073 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001074 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 return;
1076 }
1077
Adam Cohen558baaf2011-08-15 15:22:57 -07001078 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079
1080 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001081 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1082 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
1084 }
1085
Adam Cohen2f093b62012-04-30 18:59:53 -07001086 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1087 int minHeight) {
1088 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001089 // We want to account for the extra amount of padding that we are adding to the widget
1090 // to ensure that it gets the full amount of space that it has requested
1091 int requiredWidth = minWidth + padding.left + padding.right;
1092 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001093 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001094 }
1095
Adam Cohen2f093b62012-04-30 18:59:53 -07001096 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1097 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001098 }
1099
Adam Cohen2f093b62012-04-30 18:59:53 -07001100 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1101 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001102 }
1103
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1105 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001106 }
1107
Adam Cohen2f093b62012-04-30 18:59:53 -07001108 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1109 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001110 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001111 }
1112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001116 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001119 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1120 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1121 if (appWidgetInfo == null) {
1122 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001126 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001127
Adam Cohen2f093b62012-04-30 18:59:53 -07001128 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1129 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001132 // We have saved the position to which the widget was dragged-- this really only matters
1133 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001134 int[] cellXY = mTmpAddItemCellCoordinates;
1135 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001136 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001137 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001138 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1139 cellXY[0] = mPendingAddInfo.cellX;
1140 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001141 spanXY[0] = mPendingAddInfo.spanX;
1142 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001143 foundCellSpan = true;
1144 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001145 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001146 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001147 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1148 spanXY[1], cellXY, finalSpan);
1149 spanXY[0] = finalSpan[0];
1150 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001151 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001153 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001154 }
1155
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001157 if (appWidgetId != -1) {
1158 // Deleting an app widget ID is a void call but writes to disk before returning
1159 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001160 new Thread("deleteAppWidgetId") {
1161 public void run() {
1162 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1163 }
1164 }.start();
1165 }
Winson Chung93eef082012-03-23 15:59:27 -07001166 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001167 return;
1168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001170 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001171 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1172 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001173 launcherInfo.spanX = spanXY[0];
1174 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001175 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1176 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001179 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
1181 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001182 if (hostView == null) {
1183 // Perform actual inflation because we're live
1184 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1185 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1186 } else {
1187 // The AppWidgetHostView has already been inflated and instantiated
1188 launcherInfo.hostView = hostView;
1189 }
Romain Guycbb89e42009-06-08 15:52:54 -07001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001192 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001193 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001194 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001195 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001196
1197 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001199 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 }
Romain Guycbb89e42009-06-08 15:52:54 -07001201
Adam Cohended9f8d2010-11-03 13:25:16 -07001202 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1203 @Override
1204 public void onReceive(Context context, Intent intent) {
1205 final String action = intent.getAction();
1206 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1207 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001208 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001209 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001210
Winson Chungc100e8e2011-08-09 16:02:43 -07001211 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001212 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001213 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1214 mAppsCustomizeTabHost.reset();
1215 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001216 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001217 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1218 mUserPresent = true;
1219 updateRunning();
1220 }
1221 }
1222 };
1223
1224 @Override
1225 public void onAttachedToWindow() {
1226 super.onAttachedToWindow();
1227
1228 // Listen for broadcasts related to user-presence
1229 final IntentFilter filter = new IntentFilter();
1230 filter.addAction(Intent.ACTION_SCREEN_OFF);
1231 filter.addAction(Intent.ACTION_USER_PRESENT);
1232 registerReceiver(mReceiver, filter);
1233
Adam Cohend113e0c2010-11-11 10:48:05 -08001234 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001235 mVisible = true;
1236 }
1237
1238 @Override
1239 public void onDetachedFromWindow() {
1240 super.onDetachedFromWindow();
1241 mVisible = false;
1242
Adam Cohend113e0c2010-11-11 10:48:05 -08001243 if (mAttached) {
1244 unregisterReceiver(mReceiver);
1245 mAttached = false;
1246 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 updateRunning();
1248 }
1249
1250 public void onWindowVisibilityChanged(int visibility) {
1251 mVisible = visibility == View.VISIBLE;
1252 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001253 // The following code used to be in onResume, but it turns out onResume is called when
1254 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1255 // is a more appropriate event to handle
1256 if (mVisible) {
1257 mAppsCustomizeTabHost.onWindowVisible();
1258 if (!mWorkspaceLoading) {
1259 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001260 // We want to let Launcher draw itself at least once before we force it to build
1261 // layers on all the workspace pages, so that transitioning to Launcher from other
1262 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1263 // a true draw so we wait until the second preDraw call to be safe
1264 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001265 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001266 // We delay the layer building a bit in order to give
1267 // other message processing a time to run. In particular
1268 // this avoids a delay in hiding the IME if it was
1269 // currently shown, because doing that may involve
1270 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001271 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001272
Michael Jurka6ee21d22012-02-21 18:20:27 -08001273 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001274 return true;
1275 }
1276 });
1277 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001278 // When Launcher comes back to foreground, a different Activity might be responsible for
1279 // the app market intent, so refresh the icon
1280 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001281 clearTypedText();
1282 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001283 }
1284
1285 private void sendAdvanceMessage(long delay) {
1286 mHandler.removeMessages(ADVANCE_MSG);
1287 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1288 mHandler.sendMessageDelayed(msg, delay);
1289 mAutoAdvanceSentTime = System.currentTimeMillis();
1290 }
1291
1292 private void updateRunning() {
1293 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1294 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1295 mAutoAdvanceRunning = autoAdvanceRunning;
1296 if (autoAdvanceRunning) {
1297 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1298 sendAdvanceMessage(delay);
1299 } else {
1300 if (!mWidgetsToAdvance.isEmpty()) {
1301 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1302 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1303 }
1304 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001305 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 }
1307 }
1308 }
1309
1310 private final Handler mHandler = new Handler() {
1311 @Override
1312 public void handleMessage(Message msg) {
1313 if (msg.what == ADVANCE_MSG) {
1314 int i = 0;
1315 for (View key: mWidgetsToAdvance.keySet()) {
1316 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1317 final int delay = mAdvanceStagger * i;
1318 if (v instanceof Advanceable) {
1319 postDelayed(new Runnable() {
1320 public void run() {
1321 ((Advanceable) v).advance();
1322 }
1323 }, delay);
1324 }
1325 i++;
1326 }
1327 sendAdvanceMessage(mAdvanceInterval);
1328 }
1329 }
1330 };
1331
1332 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001333 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1335 if (v instanceof Advanceable) {
1336 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001337 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001338 updateRunning();
1339 }
1340 }
1341
1342 void removeWidgetToAutoAdvance(View hostView) {
1343 if (mWidgetsToAdvance.containsKey(hostView)) {
1344 mWidgetsToAdvance.remove(hostView);
1345 updateRunning();
1346 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347 }
1348
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001350 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001351 launcherInfo.hostView = null;
1352 }
1353
Winson Chung93eef082012-03-23 15:59:27 -07001354 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1355 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1356 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001357 }
1358
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001359 public LauncherAppWidgetHost getAppWidgetHost() {
1360 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
Romain Guycbb89e42009-06-08 15:52:54 -07001362
Winson Chunga9abd0e2010-10-27 17:18:37 -07001363 public LauncherModel getModel() {
1364 return mModel;
1365 }
1366
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001367 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001368 getWindow().closeAllPanels();
1369
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001370 // Whatever we were doing is hereby canceled.
1371 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 }
1373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 @Override
1375 protected void onNewIntent(Intent intent) {
1376 super.onNewIntent(intent);
1377
1378 // Close the menu
1379 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001380 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001381 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001382
Joe Onorato14f122b2009-11-19 14:06:36 -08001383 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1384 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001385
Adam Cohenac56cff2011-09-28 20:45:37 -07001386 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001387 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001388 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001389 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1390 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001391 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001392 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001393
1394 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001395 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001396
1397 // If we are already on home, then just animate back to the workspace, otherwise, just
1398 // wait until onResume to set the state back to Workspace
1399 if (alreadyOnHome) {
1400 showWorkspace(true);
1401 } else {
1402 mOnResumeState = State.WORKSPACE;
1403 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001404
Joe Onorato3a8820b2009-11-10 15:06:42 -08001405 final View v = getWindow().peekDecorView();
1406 if (v != null && v.getWindowToken() != null) {
1407 InputMethodManager imm = (InputMethodManager)getSystemService(
1408 INPUT_METHOD_SERVICE);
1409 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001411
Michael Jurka35aa14d2011-07-07 17:01:08 -07001412 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001413 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001414 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001415 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417 }
1418
1419 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001420 public void onRestoreInstanceState(Bundle state) {
1421 super.onRestoreInstanceState(state);
1422 for (int page: mSynchronouslyBoundPages) {
1423 mWorkspace.restoreInstanceStateForChild(page);
1424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426
1427 @Override
1428 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001429 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001430 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
Michael Jurka883f55b2010-10-21 15:47:14 -07001432 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001433 // We close any open folder since it will not be re-opened, and we need to make sure
1434 // this state is reflected.
1435 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436
Winson Chung3d503fb2011-07-13 17:25:49 -07001437 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1438 mWaitingForResult) {
1439 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1441 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1442 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001443 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1444 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1445 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447
1448 if (mFolderInfo != null && mWaitingForResult) {
1449 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1450 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1451 }
Michael Jurka946ad472010-07-09 18:05:18 -07001452
Winson Chung785d2eb2011-04-14 16:08:02 -07001453 // Save the current AppsCustomize tab
1454 if (mAppsCustomizeTabHost != null) {
1455 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1456 if (currentTabTag != null) {
1457 outState.putString("apps_customize_currentTab", currentTabTag);
1458 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001459 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1460 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 @Override
1465 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001467
Winson Chunge7a03942011-08-05 15:05:12 -07001468 // Remove all pending runnables
1469 mHandler.removeMessages(ADVANCE_MSG);
1470 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001471 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001472
Winson Chungcd2b0142011-06-08 16:02:26 -07001473 // Stop callbacks from LauncherModel
1474 LauncherApplication app = ((LauncherApplication) getApplication());
1475 mModel.stopLoader();
1476 app.setLauncher(null);
1477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001479 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001481 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001483 mAppWidgetHost = null;
1484
1485 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
1487 TextKeyListener.getInstance().release();
1488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
Winson Chung3d503fb2011-07-13 17:25:49 -07001490 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001491
1492 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001493 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001495 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001496 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1497 mWorkspace.removeAllViews();
1498 mWorkspace = null;
1499 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001500
Michael Jurka2ecf9952012-06-18 12:52:28 -07001501 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 }
1503
Adam Cohena9cf38f2011-05-02 15:36:58 -07001504 public DragController getDragController() {
1505 return mDragController;
1506 }
1507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 @Override
1509 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001510 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 super.startActivityForResult(intent, requestCode);
1512 }
1513
Winson Chung70d72102011-08-12 11:24:35 -07001514 /**
1515 * Indicates that we want global search for this activity by setting the globalSearch
1516 * argument for {@link #startSearch} to true.
1517 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001519 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001521
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001522 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001523
Karl Rosaen138a0412009-04-23 19:00:21 -07001524 if (initialQuery == null) {
1525 // Use any text typed in the launcher as the initial query
1526 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 if (appSearchData == null) {
1529 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001530 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
Winson Chungadf0c182012-08-23 14:59:07 -07001532 Rect sourceBounds = new Rect();
1533 if (mSearchDropTargetBar != null) {
1534 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1535 }
Romain Guycbb89e42009-06-08 15:52:54 -07001536
Michael Jurkaa33411c2012-06-14 16:18:21 -07001537 startGlobalSearch(initialQuery, selectInitialQuery,
1538 appSearchData, sourceBounds);
1539 }
1540
1541 /**
1542 * Starts the global search activity. This code is a copied from SearchManager
1543 */
1544 public void startGlobalSearch(String initialQuery,
1545 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001546 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001547 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1548 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1549 if (globalSearchActivity == null) {
1550 Log.w(TAG, "No global search activity found.");
1551 return;
1552 }
1553 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1554 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1555 intent.setComponent(globalSearchActivity);
1556 // Make sure that we have a Bundle to put source in
1557 if (appSearchData == null) {
1558 appSearchData = new Bundle();
1559 } else {
1560 appSearchData = new Bundle(appSearchData);
1561 }
1562 // Set source to package name of app that starts global search, if not set already.
1563 if (!appSearchData.containsKey("source")) {
1564 appSearchData.putString("source", getPackageName());
1565 }
1566 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1567 if (!TextUtils.isEmpty(initialQuery)) {
1568 intent.putExtra(SearchManager.QUERY, initialQuery);
1569 }
1570 if (selectInitialQuery) {
1571 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1572 }
1573 intent.setSourceBounds(sourceBounds);
1574 try {
1575 startActivity(intent);
1576 } catch (ActivityNotFoundException ex) {
1577 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
1580
Winson Chung70d72102011-08-12 11:24:35 -07001581 @Override
1582 public boolean onCreateOptionsMenu(Menu menu) {
1583 if (isWorkspaceLocked()) {
1584 return false;
1585 }
1586
1587 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001588
1589 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1590 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1591 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001592 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1593 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1594 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001595 String helpUrl = getString(R.string.help_url);
1596 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001597 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1598 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1599
Winson Chung70d72102011-08-12 11:24:35 -07001600 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1601 .setIcon(android.R.drawable.ic_menu_gallery)
1602 .setAlphabeticShortcut('W');
1603 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1604 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001605 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001606 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001607 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1608 .setIcon(android.R.drawable.ic_menu_preferences)
1609 .setIntent(settings)
1610 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001611 if (!helpUrl.isEmpty()) {
1612 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1613 .setIcon(android.R.drawable.ic_menu_help)
1614 .setIntent(help)
1615 .setAlphabeticShortcut('H');
1616 }
Winson Chung70d72102011-08-12 11:24:35 -07001617 return true;
1618 }
1619
1620 @Override
1621 public boolean onPrepareOptionsMenu(Menu menu) {
1622 super.onPrepareOptionsMenu(menu);
1623
1624 if (mAppsCustomizeTabHost.isTransitioning()) {
1625 return false;
1626 }
1627 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1628 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1629
1630 return true;
1631 }
1632
1633 @Override
1634 public boolean onOptionsItemSelected(MenuItem item) {
1635 switch (item.getItemId()) {
1636 case MENU_WALLPAPER_SETTINGS:
1637 startWallpaper();
1638 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001639 }
1640
1641 return super.onOptionsItemSelected(item);
1642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001644 @Override
1645 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001646 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001647 // Use a custom animation for launching search
1648 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001649 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001650 }
1651
Joe Onorato9c1289c2009-08-17 11:03:03 -04001652 public boolean isWorkspaceLocked() {
1653 return mWorkspaceLoading || mWaitingForResult;
1654 }
1655
Michael Jurka0280c3b2010-09-17 15:00:07 -07001656 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 mPendingAddInfo.container = ItemInfo.NO_ID;
1658 mPendingAddInfo.screen = -1;
1659 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1660 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001661 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001662 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001664
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001665 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1666 AppWidgetProviderInfo appWidgetInfo) {
1667 if (appWidgetInfo.configure != null) {
1668 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001669
Bjorn Bringert7984c942009-12-09 15:38:25 +00001670 // Launch over to configure widget, if needed
1671 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001672 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001673 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001674 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001676 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001677 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1678 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001679 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001680 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682 }
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Adam Cohenfbba09b2011-07-18 21:43:05 -07001684 /**
1685 * Process a shortcut drop.
1686 *
1687 * @param componentName The name of the component
1688 * @param screen The screen where it should be added
1689 * @param cell The cell it should be added to, optional
1690 * @param position The location on the screen where it was dropped, optional
1691 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1693 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001695 mPendingAddInfo.container = container;
1696 mPendingAddInfo.screen = screen;
1697 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001698
1699 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 mPendingAddInfo.cellX = cell[0];
1701 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001702 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001703
1704 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1705 createShortcutIntent.setComponent(componentName);
1706 processShortcut(createShortcutIntent);
1707 }
1708
Adam Cohenfbba09b2011-07-18 21:43:05 -07001709 /**
1710 * Process a widget drop.
1711 *
1712 * @param info The PendingAppWidgetInfo of the widget being added.
1713 * @param screen The screen where it should be added
1714 * @param cell The cell it should be added to, optional
1715 * @param position The location on the screen where it was dropped, optional
1716 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001717 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001718 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001719 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001720 mPendingAddInfo.container = info.container = container;
1721 mPendingAddInfo.screen = info.screen = screen;
1722 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001723 mPendingAddInfo.minSpanX = info.minSpanX;
1724 mPendingAddInfo.minSpanY = info.minSpanY;
1725
Adam Cohenfbba09b2011-07-18 21:43:05 -07001726 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001727 mPendingAddInfo.cellX = cell[0];
1728 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001729 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001730 if (span != null) {
1731 mPendingAddInfo.spanX = span[0];
1732 mPendingAddInfo.spanY = span[1];
1733 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001734
Adam Cohened66b2b2012-01-23 17:28:51 -08001735 AppWidgetHostView hostView = info.boundWidget;
1736 int appWidgetId;
1737 if (hostView != null) {
1738 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001739 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001740 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001741 // In this case, we either need to start an activity to get permission to bind
1742 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001743 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001744 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001745 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001746 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001747 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001748 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1749 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1750 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1751 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1752 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001753 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001754 }
1755
Joe Onoratodeb98af2010-02-19 14:59:39 -08001756 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001757 // Handle case where user selected "Applications"
1758 String applicationName = getResources().getString(R.string.group_applications);
1759 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001761 if (applicationName != null && applicationName.equals(shortcutName)) {
1762 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1763 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001764
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1766 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001767 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001768 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001770 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773
Winson Chung24ab2f12010-09-16 14:10:47 -07001774 void processWallpaper(Intent intent) {
1775 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1776 }
1777
Winson Chung3d503fb2011-07-13 17:25:49 -07001778 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1779 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001780 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 folderInfo.title = getText(R.string.folder_name);
1782
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001784 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1785 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001786 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787
1788 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001789 FolderIcon newFolder =
1790 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1791 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1792 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001793 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
Romain Guycbb89e42009-06-08 15:52:54 -07001795
Joe Onorato9c1289c2009-08-17 11:03:03 -04001796 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001797 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 }
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001801 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001803 Intent chooser = Intent.createChooser(pickWallpaper,
1804 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001805 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1806 // Removed in Eclair MR1
1807// WallpaperManager wm = (WallpaperManager)
1808// getSystemService(Context.WALLPAPER_SERVICE);
1809// WallpaperInfo wi = wm.getWallpaperInfo();
1810// if (wi != null && wi.getSettingsActivity() != null) {
1811// LabeledIntent li = new LabeledIntent(getPackageName(),
1812// R.string.configure_wallpaper, 0);
1813// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1814// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1815// }
Mike Clerona0618e42009-10-22 13:55:21 -07001816 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001819 /**
1820 * Registers various content observers. The current implementation registers
1821 * only a favorites observer to keep track of the favorites applications.
1822 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001823 private void registerContentObservers() {
1824 ContentResolver resolver = getContentResolver();
1825 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1826 true, mWidgetObserver);
1827 }
1828
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 @Override
1830 public boolean dispatchKeyEvent(KeyEvent event) {
1831 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1832 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001834 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001835 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001836 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001837 dumpState();
1838 return true;
1839 }
1840 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001841 }
1842 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1843 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001844 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 return true;
1846 }
1847 }
1848
1849 return super.dispatchKeyEvent(event);
1850 }
1851
Joe Onorato88ec0992009-11-19 13:16:06 -08001852 @Override
1853 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001854 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001855 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001856 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001857 Folder openFolder = mWorkspace.getOpenFolder();
1858 if (openFolder.isEditingName()) {
1859 openFolder.dismissEditingName();
1860 } else {
1861 closeFolder();
1862 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001863 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001864 mWorkspace.exitWidgetResizeMode();
1865
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001866 // Back button is a no-op here, but give at least some feedback for the button press
1867 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001868 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001872 * Re-listen when widgets are reset.
1873 */
1874 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001875 if (mAppWidgetHost != null) {
1876 mAppWidgetHost.startListening();
1877 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001878 }
1879
1880 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001881 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1882 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001885 if (mModel != null) {
1886 mModel.unbindWorkspaceItems();
1887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 /**
1891 * Launches the intent referred by the clicked shortcut.
1892 *
1893 * @param v The view representing the clicked shortcut.
1894 */
1895 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001896 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1897 // view has detached (it's possible for this to happen if the view is removed mid touch).
1898 if (v.getWindowToken() == null) {
1899 return;
1900 }
1901
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001902 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001903 return;
1904 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001907 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001909 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001910 int[] pos = new int[2];
1911 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001912 intent.setSourceBounds(new Rect(pos[0], pos[1],
1913 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001914
1915 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001916
1917 if (success && v instanceof BubbleTextView) {
1918 mWaitingForResume = (BubbleTextView) v;
1919 mWaitingForResume.setStayPressed(true);
1920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001922 if (v instanceof FolderIcon) {
1923 FolderIcon fi = (FolderIcon) v;
1924 handleFolderClick(fi);
1925 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001926 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001927 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001928 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001929 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001930 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 }
1933 }
1934
Michael Jurka0e260592010-06-30 17:07:39 -07001935 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001936 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001937 // clicking anywhere on the workspace causes the customization drawer to slide down
1938 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001939 return false;
1940 }
1941
Michael Jurkaaf442092010-06-10 17:01:57 -07001942 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001943 * Event handler for the search button
1944 *
1945 * @param v The view that was clicked.
1946 */
1947 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001948 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1949
Amith Yamasania135ba82011-08-09 17:42:01 -07001950 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001951 }
1952
1953 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001954 * Event handler for the voice button
1955 *
1956 * @param v The view that was clicked.
1957 */
1958 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001959 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001960
Michael Jurkaae65ee32012-04-30 11:45:54 -07001961 try {
1962 final SearchManager searchManager =
1963 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1964 ComponentName activityName = searchManager.getGlobalSearchActivity();
1965 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001966 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001967 if (activityName != null) {
1968 intent.setPackage(activityName.getPackageName());
1969 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001970 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001971 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001972 } catch (ActivityNotFoundException e) {
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 startActivitySafely(null, intent, "onClickVoiceButton");
1976 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001977 }
1978
1979 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001980 * Event handler for the "grid" button that appears on the home screen, which
1981 * enters all apps mode.
1982 *
1983 * @param v The view that was clicked.
1984 */
1985 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001986 showAllApps(true);
1987 }
1988
1989 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001990 // Provide the same haptic feedback that the system offers for virtual keys.
1991 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001992 }
1993
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001994 public void onClickAppMarketButton(View v) {
1995 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001996 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001997 } else {
1998 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001999 }
2000 }
2001
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002002 void startApplicationDetailsActivity(ComponentName componentName) {
2003 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002004 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2005 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002006 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002007 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002008 }
2009
Patrick Dubroy5539af72010-09-07 15:22:01 -07002010 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2011 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2012 // System applications cannot be installed. For now, show a toast explaining that.
2013 // We may give them the option of disabling apps this way.
2014 int messageId = R.string.uninstall_system_app_text;
2015 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2016 } else {
2017 String packageName = appInfo.componentName.getPackageName();
2018 String className = appInfo.componentName.getClassName();
2019 Intent intent = new Intent(
2020 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002021 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2022 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002023 startActivity(intent);
2024 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002025 }
2026
Michael Jurka86a720e2012-05-09 11:23:23 -07002027 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002031 // Only launch using the new animation if the shortcut has not opted out (this is a
2032 // private contract between launcher and may be ignored in the future).
2033 boolean useLaunchAnimation = (v != null) &&
2034 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2035 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002036 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2037 v.getMeasuredWidth(), v.getMeasuredHeight());
2038
2039 startActivity(intent, opts.toBundle());
2040 } else {
2041 startActivity(intent);
2042 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002043 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 } catch (SecurityException e) {
2045 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002048 "or use the exported attribute for this activity. "
2049 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002051 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002053
Michael Jurka86a720e2012-05-09 11:23:23 -07002054 boolean startActivitySafely(View v, Intent intent, Object tag) {
2055 boolean success = false;
2056 try {
2057 success = startActivity(v, intent, tag);
2058 } catch (ActivityNotFoundException e) {
2059 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2060 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2061 }
2062 return success;
2063 }
2064
Romain Guy8e633c52010-03-04 12:51:36 -08002065 void startActivityForResultSafely(Intent intent, int requestCode) {
2066 try {
2067 startActivityForResult(intent, requestCode);
2068 } catch (ActivityNotFoundException e) {
2069 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2070 } catch (SecurityException e) {
2071 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2072 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2073 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2074 "or use the exported attribute for this activity.", e);
2075 }
2076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077
Adam Cohena9cf38f2011-05-02 15:36:58 -07002078 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002079 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002080 Folder openFolder = mWorkspace.getFolderForTag(info);
2081
2082 // If the folder info reports that the associated folder is open, then verify that
2083 // it is actually opened. There have been a few instances where this gets out of sync.
2084 if (info.opened && openFolder == null) {
2085 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2086 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2087 info.opened = false;
2088 }
2089
Adam Cohenfb91f302012-06-11 15:45:18 -07002090 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 // Close any open folder
2092 closeFolder();
2093 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002094 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 } else {
2096 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 int folderScreen;
2098 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002099 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 // .. and close it
2101 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002102 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 // Close any folder open on the current screen
2104 closeFolder();
2105 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002106 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108 }
2109 }
2110 }
2111
Adam Cohen268c4752012-06-06 17:47:33 -07002112 /**
2113 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2114 * in the DragLayer in the exact absolute location of the original FolderIcon.
2115 */
2116 private void copyFolderIconToImage(FolderIcon fi) {
2117 final int width = fi.getMeasuredWidth();
2118 final int height = fi.getMeasuredHeight();
2119
2120 // Lazy load ImageView, Bitmap and Canvas
2121 if (mFolderIconImageView == null) {
2122 mFolderIconImageView = new ImageView(this);
2123 }
2124 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2125 mFolderIconBitmap.getHeight() != height) {
2126 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2127 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2128 }
2129
2130 DragLayer.LayoutParams lp;
2131 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2132 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2133 } else {
2134 lp = new DragLayer.LayoutParams(width, height);
2135 }
2136
2137 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2138 lp.customPosition = true;
2139 lp.x = mRectForFolderAnimation.left;
2140 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002141 lp.width = width;
2142 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002143
2144 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2145 fi.draw(mFolderIconCanvas);
2146 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002147 if (fi.getFolder() != null) {
2148 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2149 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002150 }
Adam Cohen268c4752012-06-06 17:47:33 -07002151 // Just in case this image view is still in the drag layer from a previous animation,
2152 // we remove it and re-add it.
2153 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2154 mDragLayer.removeView(mFolderIconImageView);
2155 }
2156 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002157 if (fi.getFolder() != null) {
2158 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002159 }
Adam Cohen268c4752012-06-06 17:47:33 -07002160 }
2161
Adam Cohen2801caf2011-05-13 20:57:39 -07002162 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002163 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002164 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2165 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2166 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2167
Adam Cohenc51934b2011-07-26 21:07:43 -07002168 FolderInfo info = (FolderInfo) fi.getTag();
2169 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2170 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002171 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2172 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002173 }
2174
Adam Cohen268c4752012-06-06 17:47:33 -07002175 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2176 copyFolderIconToImage(fi);
2177 fi.setVisibility(View.INVISIBLE);
2178
Michael Jurka2ecf9952012-06-18 12:52:28 -07002179 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002180 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002181 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2182 oa.start();
2183 }
2184
Adam Cohen268c4752012-06-06 17:47:33 -07002185 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002186 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002187 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2188 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2189 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2190
Adam Cohen268c4752012-06-06 17:47:33 -07002191 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002192
Adam Cohen268c4752012-06-06 17:47:33 -07002193 // We remove and re-draw the FolderIcon in-case it has changed
2194 mDragLayer.removeView(mFolderIconImageView);
2195 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002196 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002197 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002198 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002199 oa.addListener(new AnimatorListenerAdapter() {
2200 @Override
2201 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002202 if (cl != null) {
2203 cl.clearFolderLeaveBehind();
2204 // Remove the ImageView copy of the FolderIcon and make the original visible.
2205 mDragLayer.removeView(mFolderIconImageView);
2206 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002207 }
2208 }
2209 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002210 oa.start();
2211 }
2212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002214 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 * is animated relative to the specified View. If the View is null, no animation
2216 * is played.
2217 *
2218 * @param folderInfo The FolderInfo describing the folder to open.
2219 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002220 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002221 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002222 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223
Adam Cohena9cf38f2011-05-02 15:36:58 -07002224 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225
Adam Cohen4554ee12011-08-03 16:13:21 -07002226 // Just verify that the folder hasn't already been added to the DragLayer.
2227 // There was a one-off crash where the folder had a parent already.
2228 if (folder.getParent() == null) {
2229 mDragLayer.addView(folder);
2230 mDragController.addDropTarget((DropTarget) folder);
2231 } else {
2232 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2233 folder.getParent() + ").");
2234 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002235 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002236 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002237 }
2238
2239 public void closeFolder() {
2240 Folder folder = mWorkspace.getOpenFolder();
2241 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002242 if (folder.isEditingName()) {
2243 folder.dismissEditingName();
2244 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002245 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002246
2247 // Dismiss the folder cling
2248 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002249 }
2250 }
2251
2252 void closeFolder(Folder folder) {
2253 folder.getInfo().opened = false;
2254
2255 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2256 if (parent != null) {
2257 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2258 shrinkAndFadeInFolderIcon(fi);
2259 }
2260 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 }
2262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002264 if (!isDraggingEnabled()) return false;
2265 if (isWorkspaceLocked()) return false;
2266 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267
2268 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002269 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270 }
2271
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 resetAddInfo();
2273 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002275 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 return true;
2277 }
2278
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 // The hotseat touch handling does not go through Workspace, and we always allow long press
2280 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002281 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2283 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 if (itemUnderLongClick == null) {
2285 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002286 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2287 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002288 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002290 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 }
2294 }
2295 }
2296 return true;
2297 }
2298
Winson Chung3d503fb2011-07-13 17:25:49 -07002299 boolean isHotseatLayout(View layout) {
2300 return mHotseat != null && layout != null &&
2301 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2302 }
2303 Hotseat getHotseat() {
2304 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002305 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002306 SearchDropTargetBar getSearchBar() {
2307 return mSearchDropTargetBar;
2308 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002309
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 /**
2311 * Returns the CellLayout of the specified container at the specified screen.
2312 */
2313 CellLayout getCellLayout(long container, int screen) {
2314 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2315 if (mHotseat != null) {
2316 return mHotseat.getLayout();
2317 } else {
2318 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 } else {
2321 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002322 }
2323 }
2324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 Workspace getWorkspace() {
2326 return mWorkspace;
2327 }
2328
Daniel Sandler843e8602010-06-07 14:59:01 -04002329 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2330 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002331 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002332 }
2333
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002334 public boolean isAllAppsButtonRank(int rank) {
2335 return mHotseat.isAllAppsButtonRank(rank);
2336 }
2337
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 /**
2339 * Helper method for the cameraZoomIn/cameraZoomOut animations
2340 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002341 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 * @param scaleFactor The scale factor used for the zoom
2343 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002344 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002345 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002346 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002348
Winson Chung18f41f82012-05-09 13:28:10 -07002349 void disableWallpaperIfInAllApps() {
2350 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002351 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002352 if (mAppsCustomizeTabHost != null &&
2353 !mAppsCustomizeTabHost.isTransitioning()) {
2354 updateWallpaperVisibility(false);
2355 }
2356 }
2357 }
2358
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002359 void updateWallpaperVisibility(boolean visible) {
2360 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2361 int curflags = getWindow().getAttributes().flags
2362 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2363 if (wpflags != curflags) {
2364 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2365 }
2366 }
2367
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002368 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2369 if (v instanceof LauncherTransitionable) {
2370 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2371 }
2372 }
2373
Michael Jurkabed61d22012-02-14 22:51:29 -08002374 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2375 if (v instanceof LauncherTransitionable) {
2376 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2377 }
Winson Chung70442722012-02-10 15:43:22 -08002378
2379 // Update the workspace transition step as well
2380 dispatchOnLauncherTransitionStep(v, 0f);
2381 }
2382
2383 private void dispatchOnLauncherTransitionStep(View v, float t) {
2384 if (v instanceof LauncherTransitionable) {
2385 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2386 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002387 }
2388
2389 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2390 if (v instanceof LauncherTransitionable) {
2391 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2392 }
Winson Chung70442722012-02-10 15:43:22 -08002393
2394 // Update the workspace transition step as well
2395 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002396 }
2397
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002398 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002399 * Things to test when changing the following seven functions.
2400 * - Home from workspace
2401 * - from center screen
2402 * - from other screens
2403 * - Home from all apps
2404 * - from center screen
2405 * - from other screens
2406 * - Back from all apps
2407 * - from center screen
2408 * - from other screens
2409 * - Launch app from workspace and quit
2410 * - with back
2411 * - with home
2412 * - Launch app from all apps and quit
2413 * - with back
2414 * - with home
2415 * - Go to a screen that's not the default, then all
2416 * apps, and launch and app, and go back
2417 * - with back
2418 * -with home
2419 * - On workspace, long press power and go back
2420 * - with back
2421 * - with home
2422 * - On all apps, long press power and go back
2423 * - with back
2424 * - with home
2425 * - On workspace, power off
2426 * - On all apps, power off
2427 * - Launch an app and turn off the screen while in that app
2428 * - Go back with home key
2429 * - Go back with back key TODO: make this not go to workspace
2430 * - From all apps
2431 * - From workspace
2432 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2433 * - From all apps
2434 * - From the center workspace
2435 * - From another workspace
2436 */
2437
2438 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002439 * Zoom the camera out from the workspace to reveal 'toView'.
2440 * Assumes that the view to show is anchored at either the very top or very bottom
2441 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002443 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002444 if (mStateAnimation != null) {
2445 mStateAnimation.cancel();
2446 mStateAnimation = null;
2447 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002448 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002449
Winson Chungf0ea4d32011-06-06 14:27:16 -07002450 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2451 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2452 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002453 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002454 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002455 final int startDelay =
2456 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002457
Michael Jurkab3e22d92011-10-31 15:58:33 -07002458 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459
Michael Jurkad74c9842011-07-10 12:44:21 -07002460 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002461 Animator workspaceAnim =
2462 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463
2464 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002465 toView.setScaleX(scale);
2466 toView.setScaleY(scale);
2467 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2468 scaleAnim.
2469 scaleX(1f).scaleY(1f).
2470 setDuration(duration).
2471 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002472
Winson Chungf0ea4d32011-06-06 14:27:16 -07002473 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002474 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002475 final ObjectAnimator alphaAnim = ObjectAnimator
2476 .ofFloat(toView, "alpha", 0f, 1f)
2477 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002478 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002479 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2480 @Override
2481 public void onAnimationUpdate(ValueAnimator animation) {
2482 float t = (Float) animation.getAnimatedValue();
2483 dispatchOnLauncherTransitionStep(fromView, t);
2484 dispatchOnLauncherTransitionStep(toView, t);
2485 }
2486 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002487
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002488 // toView should appear right at the end of the workspace shrink
2489 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002490 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002491 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002492 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002493
2494 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002495 boolean animationCancelled = false;
2496
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002497 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002498 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002499 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 // Prepare the position
2501 toView.setTranslationX(0.0f);
2502 toView.setTranslationY(0.0f);
2503 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002504 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002505 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002506 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002507 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002508 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2509 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002510
2511 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2512 // Hide the workspace scrollbar
2513 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002514 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002515 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002516 if (!animationCancelled) {
2517 updateWallpaperVisibility(false);
2518 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002519
2520 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002521 if (mSearchDropTargetBar != null) {
2522 mSearchDropTargetBar.hideSearchBar(false);
2523 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002524 }
2525
Adam Cohencff6af82011-09-13 14:51:53 -07002526 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002527 public void onAnimationCancel(Animator animation) {
2528 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002529 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 });
2531
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002532 if (workspaceAnim != null) {
2533 mStateAnimation.play(workspaceAnim);
2534 }
Michael Jurka1899a362011-11-03 13:50:45 -07002535
2536 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002537 final ViewTreeObserver observer;
2538
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002539 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2540 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002541
2542 // If any of the objects being animated haven't been measured/laid out
2543 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002544 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002545 (mWorkspace.getMeasuredWidth() == 0) ||
2546 (toView.getMeasuredWidth() == 0)) {
2547 observer = mWorkspace.getViewTreeObserver();
2548 delayAnim = true;
2549 } else {
2550 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002551 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002552
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002553 final AnimatorSet stateAnimation = mStateAnimation;
2554 final Runnable startAnimRunnable = new Runnable() {
2555 public void run() {
2556 // Check that mStateAnimation hasn't changed while
2557 // we waited for a layout/draw pass
2558 if (mStateAnimation != stateAnimation)
2559 return;
2560 setPivotsForZoom(toView, scale);
2561 dispatchOnLauncherTransitionStart(fromView, animated, false);
2562 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002563 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002564 public void run() {
2565 // Check that mStateAnimation hasn't changed while
2566 // we waited for a layout/draw pass
2567 if (mStateAnimation != stateAnimation)
2568 return;
2569 mStateAnimation.start();
2570 }
2571 });
2572 }
2573 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002574 if (delayAnim) {
2575 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2576 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002577 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002578 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002579 }
2580 };
2581 observer.addOnGlobalLayoutListener(delayedStart);
2582 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002583 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002584 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 } else {
2586 toView.setTranslationX(0.0f);
2587 toView.setTranslationY(0.0f);
2588 toView.setScaleX(1.0f);
2589 toView.setScaleY(1.0f);
2590 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002591 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002592
Michael Jurkabed61d22012-02-14 22:51:29 -08002593 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2594 // Hide the workspace scrollbar
2595 mWorkspace.hideScrollingIndicator(true);
2596 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002597
2598 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002599 if (mSearchDropTargetBar != null) {
2600 mSearchDropTargetBar.hideSearchBar(false);
2601 }
Michael Jurkaabded662011-03-04 12:06:57 -08002602 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002603 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002604 dispatchOnLauncherTransitionStart(fromView, animated, false);
2605 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002606 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002607 dispatchOnLauncherTransitionStart(toView, animated, false);
2608 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002609 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002610 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002611 }
2612
2613 /**
2614 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002615 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002617 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002618 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2619 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002620
Michael Jurkab3e22d92011-10-31 15:58:33 -07002621 if (mStateAnimation != null) {
2622 mStateAnimation.cancel();
2623 mStateAnimation = null;
2624 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002625 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002626
Winson Chungf0ea4d32011-06-06 14:27:16 -07002627 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002628 final int fadeOutDuration =
2629 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002630 final float scaleFactor = (float)
2631 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2632 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002633 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002634 Animator workspaceAnim = null;
2635
2636 if (toState == State.WORKSPACE) {
2637 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2638 workspaceAnim = mWorkspace.getChangeStateAnimation(
2639 Workspace.State.NORMAL, animated, stagger);
2640 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2641 workspaceAnim = mWorkspace.getChangeStateAnimation(
2642 Workspace.State.SPRING_LOADED, animated);
2643 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002644
Michael Jurkab3e22d92011-10-31 15:58:33 -07002645 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002646 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002647 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002648 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002649 final LauncherViewPropertyAnimator scaleAnim =
2650 new LauncherViewPropertyAnimator(fromView);
2651 scaleAnim.
2652 scaleX(scaleFactor).scaleY(scaleFactor).
2653 setDuration(duration).
2654 setInterpolator(new Workspace.ZoomInInterpolator());
2655
2656 final ObjectAnimator alphaAnim = ObjectAnimator
2657 .ofFloat(fromView, "alpha", 1f, 0f)
2658 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002659 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002660 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2661 @Override
2662 public void onAnimationUpdate(ValueAnimator animation) {
2663 float t = 1f - (Float) animation.getAnimatedValue();
2664 dispatchOnLauncherTransitionStep(fromView, t);
2665 dispatchOnLauncherTransitionStep(toView, t);
2666 }
2667 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002668
Michael Jurka2ecf9952012-06-18 12:52:28 -07002669 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002670
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002671 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2672 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002673
2674 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002675 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002676 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002677 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002679 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2680 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002681 if (mWorkspace != null) {
2682 mWorkspace.hideScrollingIndicator(false);
2683 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002684 if (onCompleteRunnable != null) {
2685 onCompleteRunnable.run();
2686 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 }
2688 });
2689
Winson Chungf0ea4d32011-06-06 14:27:16 -07002690 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002691 if (workspaceAnim != null) {
2692 mStateAnimation.play(workspaceAnim);
2693 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002694 dispatchOnLauncherTransitionStart(fromView, animated, true);
2695 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002696 final Animator stateAnimation = mStateAnimation;
2697 mWorkspace.post(new Runnable() {
2698 public void run() {
2699 if (stateAnimation != mStateAnimation)
2700 return;
2701 mStateAnimation.start();
2702 }
2703 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002704 } else {
2705 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002706 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002707 dispatchOnLauncherTransitionStart(fromView, animated, true);
2708 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002709 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002710 dispatchOnLauncherTransitionStart(toView, animated, true);
2711 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002712 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002713 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002714 }
2715
Michael Jurkae326f182011-11-21 14:05:46 -08002716 @Override
2717 public void onTrimMemory(int level) {
2718 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002719 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002720 mAppsCustomizeTabHost.onTrimMemory();
2721 }
2722 }
2723
Winson Chung18f41f82012-05-09 13:28:10 -07002724 @Override
2725 public void onWindowFocusChanged(boolean hasFocus) {
2726 if (!hasFocus) {
2727 // When another window occludes launcher (like the notification shade, or recents),
2728 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2729 updateWallpaperVisibility(true);
2730 } else {
2731 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002732 mWorkspace.postDelayed(new Runnable() {
2733 @Override
2734 public void run() {
2735 disableWallpaperIfInAllApps();
2736 }
2737 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002738 }
2739 }
2740
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002741 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002742 showWorkspace(animated, null);
2743 }
2744
2745 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002746 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002747 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002748 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002749 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002750
Winson Chungc7d2b602012-05-16 17:02:20 -07002751 // Show the search bar (only animate if we were showing the drop target bar in spring
2752 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002753 if (mSearchDropTargetBar != null) {
2754 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2755 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002756
Michael Jurkab737ee62011-11-15 15:57:22 -08002757 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002758 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002759
2760 // Set focus to the AppsCustomize button
2761 if (mAllAppsButton != null) {
2762 mAllAppsButton.requestFocus();
2763 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002764 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002765
Michael Jurkab737ee62011-11-15 15:57:22 -08002766 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002767
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002768 // Change the state *after* we've called all the transition code
2769 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002770
Winson Chung5fb63472011-02-02 17:03:37 -08002771 // Resume the auto-advance of widgets
2772 mUserPresent = true;
2773 updateRunning();
2774
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002775 // send an accessibility event to announce the context change
2776 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002777 }
2778
Michael Jurkab3e22d92011-10-31 15:58:33 -07002779 void showAllApps(boolean animated) {
2780 if (mState != State.WORKSPACE) return;
2781
2782 showAppsCustomizeHelper(animated, false);
2783 mAppsCustomizeTabHost.requestFocus();
2784
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 // Change the state *after* we've called all the transition code
2786 mState = State.APPS_CUSTOMIZE;
2787
2788 // Pause the auto-advance of widgets until we are out of AllApps
2789 mUserPresent = false;
2790 updateRunning();
2791 closeFolder();
2792
2793 // Send an accessibility event to announce the context change
2794 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2795 }
2796
Adam Cohen7777d962011-08-18 18:58:38 -07002797 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002798 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002799 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002800 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002801 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002802 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002803 }
Adam Cohen7777d962011-08-18 18:58:38 -07002804
Adam Cohened66b2b2012-01-23 17:28:51 -08002805 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2806 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002807 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2808
Winson Chunge7a03942011-08-05 15:05:12 -07002809 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002810 @Override
2811 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002812 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002813 // Before we show workspace, hide all apps again because
2814 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2815 // clean up our state transition functions
2816 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002817 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002818 } else {
2819 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002820 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002821 }
2822 }, (extendedDelay ?
2823 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2824 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2825 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002826
Michael Jurkad3ef3062010-11-23 16:23:58 -08002827 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002828 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002829 final boolean animated = true;
2830 final boolean springLoaded = true;
2831 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002832 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002833 }
2834 // Otherwise, we are not in spring loaded mode, so don't do anything.
2835 }
2836
Michael Jurkab737ee62011-11-15 15:57:22 -08002837 void hideDockDivider() {
2838 if (mQsbDivider != null && mDockDivider != null) {
2839 mQsbDivider.setVisibility(View.INVISIBLE);
2840 mDockDivider.setVisibility(View.INVISIBLE);
2841 }
2842 }
2843
2844 void showDockDivider(boolean animated) {
2845 if (mQsbDivider != null && mDockDivider != null) {
2846 mQsbDivider.setVisibility(View.VISIBLE);
2847 mDockDivider.setVisibility(View.VISIBLE);
2848 if (mDividerAnimator != null) {
2849 mDividerAnimator.cancel();
2850 mQsbDivider.setAlpha(1f);
2851 mDockDivider.setAlpha(1f);
2852 mDividerAnimator = null;
2853 }
2854 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002855 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2856 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2857 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002858 int duration = 0;
2859 if (mSearchDropTargetBar != null) {
2860 duration = mSearchDropTargetBar.getTransitionInDuration();
2861 }
2862 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002863 mDividerAnimator.start();
2864 }
2865 }
2866 }
2867
Michael Jurkab3e22d92011-10-31 15:58:33 -07002868 void lockAllApps() {
2869 // TODO
2870 }
2871
2872 void unlockAllApps() {
2873 // TODO
2874 }
2875
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002877 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002878 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002879 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002880 if (!LauncherApplication.isScreenLarge()) {
2881 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002882 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002883 int duration = 0;
2884 if (mSearchDropTargetBar != null) {
2885 duration = mSearchDropTargetBar.getTransitionInDuration();
2886 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002887 mHotseat.animate().alpha(1f).setDuration(duration);
2888 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 }
2892 }
2893 }
2894
2895 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002896 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002897 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002898 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002899 if (!LauncherApplication.isScreenLarge()) {
2900 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002901 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002902 int duration = 0;
2903 if (mSearchDropTargetBar != null) {
2904 duration = mSearchDropTargetBar.getTransitionOutDuration();
2905 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002906 mHotseat.animate().alpha(0f).setDuration(duration);
2907 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002908 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002909 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002910 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002911 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002912 }
2913
Patrick Dubroy5f445422011-02-18 14:35:21 -08002914 /**
2915 * Add an item from all apps or customize onto the given workspace screen.
2916 * If layout is null, add to the current screen.
2917 */
2918 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002919 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002920 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002921 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002922 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002923
Winson Chungdff8ebb2011-09-08 17:25:31 -07002924 /** Maps the current orientation to an index for referencing orientation correct global icons */
2925 private int getCurrentOrientationIndexForGlobalIcons() {
2926 // default - 0, landscape - 1
2927 switch (getResources().getConfiguration().orientation) {
2928 case Configuration.ORIENTATION_LANDSCAPE:
2929 return 1;
2930 default:
2931 return 0;
2932 }
2933 }
2934
Michael Jurkaae65ee32012-04-30 11:45:54 -07002935 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002936 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002937 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002938 // Look for the toolbar icon specified in the activity meta-data
2939 Bundle metaData = packageManager.getActivityInfo(
2940 activityName, PackageManager.GET_META_DATA).metaData;
2941 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002942 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002943 if (iconResId != 0) {
2944 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002945 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002946 }
2947 }
2948 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002949 // This can happen if the activity defines an invalid drawable
2950 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2951 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002952 } catch (Resources.NotFoundException nfe) {
2953 // This can happen if the activity defines an invalid drawable
2954 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2955 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002956 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002957 return null;
2958 }
2959
2960 // if successful in getting icon, return it; otherwise, set button to use default drawable
2961 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002962 int buttonId, ComponentName activityName, int fallbackDrawableId,
2963 String toolbarResourceName) {
2964 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002965 Resources r = getResources();
2966 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2967 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002968
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002969 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002970 // If we were unable to find the icon via the meta-data, use a generic one
2971 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002972 toolbarIcon = r.getDrawable(fallbackDrawableId);
2973 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002974 if (button != null) {
2975 button.setCompoundDrawables(toolbarIcon, null, null, null);
2976 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002977 return null;
2978 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002979 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002980 if (button != null) {
2981 button.setCompoundDrawables(toolbarIcon, null, null, null);
2982 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002983 return toolbarIcon.getConstantState();
2984 }
2985 }
2986
2987 // if successful in getting icon, return it; otherwise, set button to use default drawable
2988 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002989 int buttonId, ComponentName activityName, int fallbackDrawableId,
2990 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002991 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002992 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002993
Michael Jurka19e0fc52011-07-22 18:00:21 -07002994 if (button != null) {
2995 // If we were unable to find the icon via the meta-data, use a
2996 // generic one
2997 if (toolbarIcon == null) {
2998 button.setImageResource(fallbackDrawableId);
2999 } else {
3000 button.setImageDrawable(toolbarIcon);
3001 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003002 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003003
3004 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3005
Michael Jurka0423dcf2010-10-05 14:56:18 -07003006 }
3007
Winson Chung1b884092012-06-08 17:13:02 -07003008 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003009 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003010 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003011 }
3012
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003013 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003014 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003015 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003016 }
3017
Winson Chungbb185bd2011-11-21 12:31:42 -08003018 private void invalidatePressedFocusedStates(View container, View button) {
3019 if (container instanceof HolographicLinearLayout) {
3020 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3021 layout.invalidatePressedFocusedStates();
3022 } else if (button instanceof HolographicImageView) {
3023 HolographicImageView view = (HolographicImageView) button;
3024 view.invalidatePressedFocusedStates();
3025 }
3026 }
3027
Winson Chungc51db6a2011-10-05 11:44:49 -07003028 private boolean updateGlobalSearchIcon() {
3029 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003030 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003031 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003032 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003033 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003034
Winson Chung1cad91e2011-05-25 17:41:01 -07003035 final SearchManager searchManager =
3036 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3037 ComponentName activityName = searchManager.getGlobalSearchActivity();
3038 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003039 int coi = getCurrentOrientationIndexForGlobalIcons();
3040 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003041 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3042 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3043 if (sGlobalSearchIcon[coi] == null) {
3044 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3045 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3046 TOOLBAR_ICON_METADATA_NAME);
3047 }
3048
Winson Chungc51db6a2011-10-05 11:44:49 -07003049 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3050 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003051 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003052 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003053 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003054 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003055 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3056 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3057 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003058 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003059 if (voiceButtonProxy != null) {
3060 voiceButtonProxy.setVisibility(View.GONE);
3061 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003062 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003063 }
3064 }
3065
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003066 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003067 final View searchButtonContainer = findViewById(R.id.search_button_container);
3068 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003069 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003070 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003071 }
3072
Winson Chungc51db6a2011-10-05 11:44:49 -07003073 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003074 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003075 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003076 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003077
Winson Chungc51db6a2011-10-05 11:44:49 -07003078 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003079 final SearchManager searchManager =
3080 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3081 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3082
3083 ComponentName activityName = null;
3084 if (globalSearchActivity != null) {
3085 // Check if the global search activity handles voice search
3086 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3087 intent.setPackage(globalSearchActivity.getPackageName());
3088 activityName = intent.resolveActivity(getPackageManager());
3089 }
3090
3091 if (activityName == null) {
3092 // Fallback: check if an activity other than the global search activity
3093 // resolves this
3094 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3095 activityName = intent.resolveActivity(getPackageManager());
3096 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003097 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003098 int coi = getCurrentOrientationIndexForGlobalIcons();
3099 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003100 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3101 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3102 if (sVoiceSearchIcon[coi] == null) {
3103 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3104 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3105 TOOLBAR_ICON_METADATA_NAME);
3106 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003107 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003108 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003109 if (voiceButtonProxy != null) {
3110 voiceButtonProxy.setVisibility(View.VISIBLE);
3111 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003112 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003113 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003114 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003115 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003116 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003117 if (voiceButtonProxy != null) {
3118 voiceButtonProxy.setVisibility(View.GONE);
3119 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003120 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003121 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003122 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003123
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003124 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003125 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3126 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003127 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003128 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003129 }
3130
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003131 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003132 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003133 */
3134 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003135 final View marketButton = findViewById(R.id.market_button);
3136 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3137 // Find the app market activity by resolving an intent.
3138 // (If multiple app markets are installed, it will return the ResolverActivity.)
3139 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003140 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003141 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003142 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003143 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003144 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3145 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 marketButton.setVisibility(View.VISIBLE);
3147 } else {
3148 // We should hide and disable the view so that we don't try and restore the visibility
3149 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3150 marketButton.setVisibility(View.GONE);
3151 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003152 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003153 }
3154
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003155 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003156 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3157 Resources r = getResources();
3158 Drawable marketIconDrawable = d.newDrawable(r);
3159 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3160 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3161 marketIconDrawable.setBounds(0, 0, w, h);
3162
3163 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003164 }
3165
Michael Jurkad7c28052012-04-27 15:43:36 -07003166 @Override
3167 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3168 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3169 final List<CharSequence> text = event.getText();
3170 text.clear();
3171 text.add(getString(R.string.home));
3172 return result;
3173 }
3174
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003175 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003176 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003177 */
3178 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3179 @Override
3180 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003181 closeSystemDialogs();
3182 }
3183 }
3184
3185 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003186 * Receives notifications whenever the appwidgets are reset.
3187 */
3188 private class AppWidgetResetObserver extends ContentObserver {
3189 public AppWidgetResetObserver() {
3190 super(new Handler());
3191 }
3192
3193 @Override
3194 public void onChange(boolean selfChange) {
3195 onAppWidgetReset();
3196 }
3197 }
3198
3199 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003200 * If the activity is currently paused, signal that we need to re-run the loader
3201 * in onResume.
3202 *
3203 * This needs to be called from incoming places where resources might have been loaded
3204 * while we are paused. That is becaues the Configuration might be wrong
3205 * when we're not running, and if it comes back to what it was when we
3206 * were paused, we are not restarted.
3207 *
3208 * Implementation of the method from LauncherModel.Callbacks.
3209 *
3210 * @return true if we are currently paused. The caller might be able to
3211 * skip some work in that case since we will come back again.
3212 */
3213 public boolean setLoadOnResume() {
3214 if (mPaused) {
3215 Log.i(TAG, "setLoadOnResume");
3216 mOnResumeNeedsLoad = true;
3217 return true;
3218 } else {
3219 return false;
3220 }
3221 }
3222
3223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003227 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003228 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003229 } else {
3230 return SCREEN_COUNT / 2;
3231 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003233
Joe Onorato9c1289c2009-08-17 11:03:03 -04003234 /**
3235 * Refreshes the shortcuts shown on the workspace.
3236 *
3237 * Implementation of the method from LauncherModel.Callbacks.
3238 */
3239 public void startBinding() {
3240 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003241
Winson Chungf0c6ae02012-03-21 16:10:31 -07003242 mNewShortcutAnimatePage = -1;
3243 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003244 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245 int count = workspace.getChildCount();
3246 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003247 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003248 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3249 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003250 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003251 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003252 if (mHotseat != null) {
3253 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 }
3255 }
3256
3257 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 * Bind the items start-end from the list.
3259 *
3260 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003263 setLoadOnResume();
3264
Winson Chungf0c6ae02012-03-21 16:10:31 -07003265 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3266 Set<String> newApps = new HashSet<String>();
3267 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3268
3269 Workspace workspace = mWorkspace;
3270 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003271 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003272
3273 // Short circuit if we are loading dock items for a configuration which has no dock
3274 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3275 mHotseat == null) {
3276 continue;
3277 }
3278
Joe Onorato9c1289c2009-08-17 11:03:03 -04003279 switch (item.itemType) {
3280 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3281 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003282 ShortcutInfo info = (ShortcutInfo) item;
3283 String uri = info.intent.toUri(0).toString();
3284 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003285 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3286 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003287 boolean animateIconUp = false;
3288 synchronized (newApps) {
3289 if (newApps.contains(uri)) {
3290 animateIconUp = newApps.remove(uri);
3291 }
3292 }
3293 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003294 // Prepare the view to be animated up
3295 shortcut.setAlpha(0f);
3296 shortcut.setScaleX(0f);
3297 shortcut.setScaleY(0f);
3298 mNewShortcutAnimatePage = item.screen;
3299 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3300 mNewShortcutAnimateViews.add(shortcut);
3301 }
3302 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003304 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003305 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003306 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003307 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003308 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3309 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003312 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003313
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003315 }
3316
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 /**
3318 * Implementation of the method from LauncherModel.Callbacks.
3319 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003320 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003321 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003322 sFolders.clear();
3323 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325
3326 /**
3327 * Add the views for a widget to the workspace.
3328 *
3329 * Implementation of the method from LauncherModel.Callbacks.
3330 */
3331 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003332 setLoadOnResume();
3333
Daniel Sandler843e8602010-06-07 14:59:01 -04003334 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3335 if (DEBUG_WIDGETS) {
3336 Log.d(TAG, "bindAppWidget: " + item);
3337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 final Workspace workspace = mWorkspace;
3339
3340 final int appWidgetId = item.appWidgetId;
3341 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003342 if (DEBUG_WIDGETS) {
3343 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3344 }
3345
Joe Onorato9c1289c2009-08-17 11:03:03 -04003346 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3347
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003349 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350
Winson Chung3d503fb2011-07-13 17:25:49 -07003351 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003353 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3354
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355 workspace.requestLayout();
3356
Daniel Sandler843e8602010-06-07 14:59:01 -04003357 if (DEBUG_WIDGETS) {
3358 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3359 + (SystemClock.uptimeMillis()-start) + "ms");
3360 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 }
3362
Adam Cohen1462de32012-07-24 22:34:36 -07003363 public void onPageBoundSynchronously(int page) {
3364 mSynchronouslyBoundPages.add(page);
3365 }
3366
Joe Onorato9c1289c2009-08-17 11:03:03 -04003367 /**
3368 * Callback saying that there aren't any more items to bind.
3369 *
3370 * Implementation of the method from LauncherModel.Callbacks.
3371 */
3372 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003373 setLoadOnResume();
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 if (mSavedState != null) {
3376 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003377 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 mSavedState = null;
3380 }
3381
Adam Cohen1462de32012-07-24 22:34:36 -07003382 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003384 // If we received the result of any pending adds while the loader was running (e.g. the
3385 // widget configuration forced an orientation change), process them now.
3386 for (int i = 0; i < sPendingAddList.size(); i++) {
3387 completeAdd(sPendingAddList.get(i));
3388 }
3389 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390
Winson Chungc51db6a2011-10-05 11:44:49 -07003391 // Update the market app icon as necessary (the other icons will be managed in response to
3392 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003393 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003394
Winson Chungf0c6ae02012-03-21 16:10:31 -07003395 // Animate up any icons as necessary
3396 if (mVisible || mWorkspaceLoading) {
3397 Runnable newAppsRunnable = new Runnable() {
3398 @Override
3399 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003400 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003401 }
3402 };
Winson Chunga2413752012-04-03 14:22:34 -07003403
3404 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3405 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3406 if (canRunNewAppsAnimation()) {
3407 // If the user has not interacted recently, then either snap to the new page to show
3408 // the new-apps animation or just run them if they are to appear on the current page
3409 if (willSnapPage) {
3410 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3411 } else {
3412 runNewAppsAnimation(false);
3413 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003414 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003415 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003416 // are on another page (or just normally if they are added to the current page)
3417 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003418 }
3419 }
3420
3421 mWorkspaceLoading = false;
3422 }
3423
Winson Chunga2413752012-04-03 14:22:34 -07003424 private boolean canRunNewAppsAnimation() {
3425 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3426 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3427 }
3428
Winson Chungf0c6ae02012-03-21 16:10:31 -07003429 /**
3430 * Runs a new animation that scales up icons that were added while Launcher was in the
3431 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003432 *
3433 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003434 */
Winson Chunga2413752012-04-03 14:22:34 -07003435 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003436 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003437 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003438
3439 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003440 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3441 @Override
3442 public int compare(View a, View b) {
3443 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3444 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3445 int cellCountX = LauncherModel.getCellCountX();
3446 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3447 }
3448 });
Winson Chunga2413752012-04-03 14:22:34 -07003449
3450 // Animate each of the views in place (or show them immediately if requested)
3451 if (immediate) {
3452 for (View v : mNewShortcutAnimateViews) {
3453 v.setAlpha(1f);
3454 v.setScaleX(1f);
3455 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003456 }
Winson Chunga2413752012-04-03 14:22:34 -07003457 } else {
3458 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3459 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003460 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003461 PropertyValuesHolder.ofFloat("alpha", 1f),
3462 PropertyValuesHolder.ofFloat("scaleX", 1f),
3463 PropertyValuesHolder.ofFloat("scaleY", 1f));
3464 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3465 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3466 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3467 bounceAnims.add(bounceAnim);
3468 }
3469 anim.playTogether(bounceAnims);
3470 anim.addListener(new AnimatorListenerAdapter() {
3471 @Override
3472 public void onAnimationEnd(Animator animation) {
3473 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3474 }
3475 });
3476 anim.start();
3477 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003478
3479 // Clean up
3480 mNewShortcutAnimatePage = -1;
3481 mNewShortcutAnimateViews.clear();
3482 new Thread("clearNewAppsThread") {
3483 public void run() {
3484 mSharedPrefs.edit()
3485 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3486 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3487 .commit();
3488 }
3489 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003490 }
3491
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003492 @Override
3493 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003494 boolean searchVisible = updateGlobalSearchIcon();
3495 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003496 if (mSearchDropTargetBar != null) {
3497 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3498 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003499 }
3500
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003501 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 * Add the icons for all apps.
3503 *
3504 * Implementation of the method from LauncherModel.Callbacks.
3505 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003506 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003507 Runnable setAllAppsRunnable = new Runnable() {
3508 public void run() {
3509 if (mAppsCustomizeContent != null) {
3510 mAppsCustomizeContent.setApps(apps);
3511 }
3512 }
3513 };
3514
Michael Jurkac57b7a82011-08-09 22:02:20 -07003515 // Remove the progress bar entirely; we could also make it GONE
3516 // but better to remove it since we know it's not going to be used
3517 View progressBar = mAppsCustomizeTabHost.
3518 findViewById(R.id.apps_customize_progress_bar);
3519 if (progressBar != null) {
3520 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003521
3522 // We just post the call to setApps so the user sees the progress bar
3523 // disappear-- otherwise, it just looks like the progress bar froze
3524 // which doesn't look great
3525 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3526 } else {
3527 // If we did not initialize the spinner in onCreate, then we can directly set the
3528 // list of applications without waiting for any progress bars views to be hidden.
3529 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003530 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 }
3532
3533 /**
3534 * A package was installed.
3535 *
3536 * Implementation of the method from LauncherModel.Callbacks.
3537 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003538 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003539 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003540
Winson Chung785d2eb2011-04-14 16:08:02 -07003541 if (mAppsCustomizeContent != null) {
3542 mAppsCustomizeContent.addApps(apps);
3543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 }
3545
3546 /**
3547 * A package was updated.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003551 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003552 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003553 if (mWorkspace != null) {
3554 mWorkspace.updateShortcuts(apps);
3555 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003556
Winson Chung785d2eb2011-04-14 16:08:02 -07003557 if (mAppsCustomizeContent != null) {
3558 mAppsCustomizeContent.updateApps(apps);
3559 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003560 }
3561
3562 /**
3563 * A package was uninstalled.
3564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
3566 */
Winson Chungcd810732012-06-18 16:45:43 -07003567 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003568 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003569 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003570 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003571
Winson Chung785d2eb2011-04-14 16:08:02 -07003572 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003573 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003574 }
Winson Chunga1820962011-10-03 16:31:06 -07003575
3576 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003577 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003578 }
Joe Onoratobe386092009-11-17 17:32:16 -08003579
3580 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003581 * A number of packages were updated.
3582 */
3583 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003584 if (mAppsCustomizeContent != null) {
3585 mAppsCustomizeContent.onPackagesUpdated();
3586 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003587 }
3588
Winson Chung400438b2011-01-16 17:53:48 -08003589 private int mapConfigurationOriActivityInfoOri(int configOri) {
3590 final Display d = getWindowManager().getDefaultDisplay();
3591 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3592 switch (d.getRotation()) {
3593 case Surface.ROTATION_0:
3594 case Surface.ROTATION_180:
3595 // We are currently in the same basic orientation as the natural orientation
3596 naturalOri = configOri;
3597 break;
3598 case Surface.ROTATION_90:
3599 case Surface.ROTATION_270:
3600 // We are currently in the other basic orientation to the natural orientation
3601 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3602 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3603 break;
3604 }
3605
3606 int[] oriMap = {
3607 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3608 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3609 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3610 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3611 };
3612 // Since the map starts at portrait, we need to offset if this device's natural orientation
3613 // is landscape.
3614 int indexOffset = 0;
3615 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3616 indexOffset = 1;
3617 }
3618 return oriMap[(d.getRotation() + indexOffset) % 4];
3619 }
Adam Cohen446e9402011-09-15 18:21:21 -07003620
Winson Chung4b919f82012-05-01 10:44:08 -07003621 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003622 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003623 boolean enableRotation = forceEnableRotation ||
3624 getResources().getBoolean(R.bool.allow_rotation);
3625 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003626 }
Winson Chung4b919f82012-05-01 10:44:08 -07003627 public void lockScreenOrientation() {
3628 if (isRotationEnabled()) {
3629 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3630 .getConfiguration().orientation));
3631 }
3632 }
3633 public void unlockScreenOrientation(boolean immediate) {
3634 if (isRotationEnabled()) {
3635 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003636 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003637 } else {
3638 mHandler.postDelayed(new Runnable() {
3639 public void run() {
3640 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3641 }
3642 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003643 }
Winson Chung4b919f82012-05-01 10:44:08 -07003644 }
Winson Chung400438b2011-01-16 17:53:48 -08003645 }
3646
Winson Chung82f55532011-08-09 14:14:23 -07003647 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003648 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003649 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003650 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003651
Winson Chung7d7541e2011-09-16 20:14:36 -07003652 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003653 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003654 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3655 Cling cling = (Cling) findViewById(clingId);
3656 if (cling != null) {
3657 cling.init(this, positionData);
3658 cling.setVisibility(View.VISIBLE);
3659 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3660 if (animate) {
3661 cling.buildLayer();
3662 cling.setAlpha(0f);
3663 cling.animate()
3664 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003665 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003666 .setDuration(SHOW_CLING_DURATION)
3667 .setStartDelay(delay)
3668 .start();
3669 } else {
3670 cling.setAlpha(1f);
3671 }
3672 }
3673 return cling;
3674 }
3675 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003676 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003677 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003678 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003679 anim.addListener(new AnimatorListenerAdapter() {
3680 public void onAnimationEnd(Animator animation) {
3681 cling.setVisibility(View.GONE);
3682 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003683 // We should update the shared preferences on a background thread
3684 new Thread("dismissClingThread") {
3685 public void run() {
3686 SharedPreferences.Editor editor = mSharedPrefs.edit();
3687 editor.putBoolean(flag, true);
3688 editor.commit();
3689 }
3690 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003691 };
3692 });
3693 anim.start();
3694 }
3695 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003696 private void removeCling(int id) {
3697 final View cling = findViewById(id);
3698 if (cling != null) {
3699 final ViewGroup parent = (ViewGroup) cling.getParent();
3700 parent.post(new Runnable() {
3701 @Override
3702 public void run() {
3703 parent.removeView(cling);
3704 }
3705 });
3706 }
3707 }
Michael Jurka974c3862012-05-22 22:00:31 -07003708
3709 private boolean skipCustomClingIfNoAccounts() {
3710 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3711 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3712 if (customCling) {
3713 AccountManager am = AccountManager.get(this);
3714 Account[] accounts = am.getAccountsByType("com.google");
3715 return accounts.length == 0;
3716 }
3717 return false;
3718 }
3719
Winson Chung7d7541e2011-09-16 20:14:36 -07003720 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003721 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003722 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003723 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3724 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003725 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003726 } else {
3727 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003728 }
3729 }
3730 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003731 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003732 if (isClingsEnabled() &&
3733 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003734 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003735 } else {
3736 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003737 }
3738 }
3739 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003740 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003741 if (isClingsEnabled() &&
3742 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3743 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003744 } else {
3745 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003746 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003747 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 }
3749 public boolean isFolderClingVisible() {
3750 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003751 if (cling != null) {
3752 return cling.getVisibility() == View.VISIBLE;
3753 }
3754 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003755 }
Winson Chung82f55532011-08-09 14:14:23 -07003756 public void dismissWorkspaceCling(View v) {
3757 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003758 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003759 }
3760 public void dismissAllAppsCling(View v) {
3761 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003762 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3763 }
3764 public void dismissFolderCling(View v) {
3765 Cling cling = (Cling) findViewById(R.id.folder_cling);
3766 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003767 }
3768
Winson Chung80baf5a2010-08-09 16:03:15 -07003769 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003770 * Prints out out state for debugging.
3771 */
3772 public void dumpState() {
3773 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003774 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003775 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3776 Log.d(TAG, "mRestoring=" + mRestoring);
3777 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3778 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003779 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003780 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003781
Winson Chung785d2eb2011-04-14 16:08:02 -07003782 if (mAppsCustomizeContent != null) {
3783 mAppsCustomizeContent.dumpState();
3784 }
Joe Onoratobe386092009-11-17 17:32:16 -08003785 Log.d(TAG, "END launcher2 dump state");
3786 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003787
3788 @Override
3789 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3790 super.dump(prefix, fd, writer, args);
3791 writer.println(" ");
3792 writer.println("Debug logs: ");
3793 for (int i = 0; i < sDumpLogs.size(); i++) {
3794 writer.println(" " + sDumpLogs.get(i));
3795 }
3796 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003797
3798 public static void dumpDebugLogsToConsole() {
3799 Log.d(TAG, "");
3800 Log.d(TAG, "*********************");
3801 Log.d(TAG, "Launcher debug logs: ");
3802 for (int i = 0; i < sDumpLogs.size(); i++) {
3803 Log.d(TAG, " " + sDumpLogs.get(i));
3804 }
3805 Log.d(TAG, "*********************");
3806 Log.d(TAG, "");
3807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003808}
Michael Jurka2763be32011-02-24 11:19:57 -08003809
Michael Jurkaabded662011-03-04 12:06:57 -08003810interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003811 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003812 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003813 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003814 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003815 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003816}