blob: 123155876bc02cafdd8cc4de88e2e8e24861c026 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070080import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080081import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080082import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070083import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070085import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070086import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080087import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070090import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070091import android.widget.TextView;
92import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070093
Adam Cohendf2cc412011-04-27 16:56:57 -070094import com.android.common.Search;
95import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070096import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080097
Adam Cohenc0dcf592011-06-01 15:30:43 -070098import java.io.DataInputStream;
99import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700100import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700101import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.FileNotFoundException;
103import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700106import java.util.Collection;
107import java.util.Collections;
108import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700110import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700111import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114/**
115 * Default launcher application.
116 */
Michael Jurka946ad472010-07-09 18:05:18 -0700117public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700118 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700119 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800120 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400124 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700125 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 private static final int REQUEST_BIND_APPWIDGET = 11;
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700148 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
149 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Winson Chung2672ff92012-05-04 16:22:30 -0700151 // The Intent extra that defines whether to ignore the launch animation
152 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
153 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
156 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700157 // Type: int
158 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
161 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
163 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: boolean
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
169 // Type: long
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700171 // Type: int
172 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
173 // Type: int
174 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
175 // Type: parcelable
176 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700178 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700179 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
180 "com.android.launcher.toolbar_search_icon";
181 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
182 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700185 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700186 private State mState = State.WORKSPACE;
187 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800188 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700191 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
192 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700193 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700194 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800197 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Winson Chunga2413752012-04-03 14:22:34 -0700199 // How long to wait before the new-shortcut animation automatically pans the workspace
200 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
201
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800202 private final BroadcastReceiver mCloseSystemDialogsReceiver
203 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800204 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private LayoutInflater mInflater;
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private View mQsbDivider;
210 private View mDockDivider;
211 private DragLayer mDragLayer;
212 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700213
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700214 private AppWidgetManager mAppWidgetManager;
215 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Michael Jurkac9d95c52011-08-29 14:03:34 -0700217 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700218 private AppWidgetProviderInfo mPendingAddWidgetInfo;
219
Michael Jurka0280c3b2010-09-17 15:00:07 -0700220 private int[] mTmpAddItemCellCoordinates = new int[2];
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private FolderInfo mFolderInfo;
223
Winson Chung3d503fb2011-07-13 17:25:49 -0700224 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800225 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700226
Winson Chungc51db6a2011-10-05 11:44:49 -0700227 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700228 private AppsCustomizeTabHost mAppsCustomizeTabHost;
229 private AppsCustomizePagedView mAppsCustomizeContent;
230 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private Bundle mSavedState;
233
234 private SpannableStringBuilder mDefaultKeySsb = null;
235
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236 private boolean mWorkspaceLoading = true;
237
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800238 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private boolean mRestoring;
240 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700241 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
243 private Bundle mSavedInstanceState;
244
Joe Onorato9c1289c2009-08-17 11:03:03 -0400245 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800246 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800247 private boolean mUserPresent = true;
248 private boolean mVisible = false;
249 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400250
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700251 private static LocaleConfiguration sLocaleConfiguration = null;
252
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700253 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700255 private Intent mAppMarketIntent = null;
256
Adam Cohended9f8d2010-11-03 13:25:16 -0700257 // Related to the auto-advancing of widgets
258 private final int ADVANCE_MSG = 1;
259 private final int mAdvanceInterval = 20000;
260 private final int mAdvanceStagger = 250;
261 private long mAutoAdvanceSentTime;
262 private long mAutoAdvanceTimeLeft = -1;
263 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
264 new HashMap<View, AppWidgetProviderInfo>();
265
Winson Chung400438b2011-01-16 17:53:48 -0800266 // Determines how long to wait after a rotation before restoring the screen orientation to
267 // match the sensor state.
268 private final int mRestoreScreenOrientationDelay = 500;
269
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700271 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
272 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
273 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800274
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700275 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
276
Winson Chung46353de2012-02-16 14:05:10 -0800277 // We only want to get the SharedPreferences once since it does an FS stat each time we get
278 // it from the context.
279 private SharedPreferences mSharedPrefs;
280
Winson Chungf0c6ae02012-03-21 16:10:31 -0700281 // Holds the page that we need to animate to, and the icon views that we need to animate up
282 // when we scroll to that page on resume.
283 private int mNewShortcutAnimatePage = -1;
284 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700285 private ImageView mFolderIconImageView;
286 private Bitmap mFolderIconBitmap;
287 private Canvas mFolderIconCanvas;
288 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700289
Michael Jurkaddd62e92011-02-16 17:49:14 -0800290 private BubbleTextView mWaitingForResume;
291
Michael Jurkac1f5d262011-09-30 19:32:27 -0700292 private Runnable mBuildLayersRunnable = new Runnable() {
293 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700294 if (mWorkspace != null) {
295 mWorkspace.buildPageHardwareLayers();
296 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700297 }
298 };
299
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800300 private static ArrayList<PendingAddArguments> sPendingAddList
301 = new ArrayList<PendingAddArguments>();
302
303 private static class PendingAddArguments {
304 int requestCode;
305 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700306 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800307 int screen;
308 int cellX;
309 int cellY;
310 }
311
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700312
313 private boolean doesFileExist(String filename) {
314 FileInputStream fis = null;
315 try {
316 fis = openFileInput(filename);
317 fis.close();
318 return true;
319 } catch (java.io.FileNotFoundException e) {
320 return false;
321 } catch (java.io.IOException e) {
322 return true;
323 }
324 }
325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 @Override
327 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700328 if (DEBUG_STRICT_MODE) {
329 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
330 .detectDiskReads()
331 .detectDiskWrites()
332 .detectNetwork() // or .detectAll() for all detectable problems
333 .penaltyLog()
334 .build());
335 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
336 .detectLeakedSqlLiteObjects()
337 .detectLeakedClosableObjects()
338 .penaltyLog()
339 .penaltyDeath()
340 .build());
341 }
342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800344 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
346 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800347 mModel = app.setLauncher(this);
348 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400349 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700351
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700352 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700353 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
354 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200357 android.os.Debug.startMethodTracing(
358 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 }
360
361 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 setContentView(R.layout.launcher);
363 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700364 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800366 registerContentObservers();
367
Joe Onorato7c312c12009-08-13 21:36:53 -0700368 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 mSavedState = savedInstanceState;
371 restoreState(mSavedState);
372
Winson Chunga12a2502010-12-20 14:41:35 -0800373 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700374 if (mAppsCustomizeContent != null) {
375 mAppsCustomizeContent.onPackagesUpdated();
376 }
Winson Chunga12a2502010-12-20 14:41:35 -0800377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 if (PROFILE_STARTUP) {
379 android.os.Debug.stopMethodTracing();
380 }
381
382 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700383 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
Michael Jurkac57b7a82011-08-09 22:02:20 -0700386 if (!mModel.isAllAppsLoaded()) {
387 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
388 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
389 }
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 // For handling default keys
392 mDefaultKeySsb = new SpannableStringBuilder();
393 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800394
395 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
396 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800397
Adam Cohenf9426d52012-06-04 17:26:21 -0700398 updateGlobalIcons();
399
400 // On large interfaces, we want the screen to auto-rotate based on the current orientation
401 unlockScreenOrientation(true);
402 }
403
404 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700405 boolean searchVisible = false;
406 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800407 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700408 int coi = getCurrentOrientationIndexForGlobalIcons();
409 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
410 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700411 updateAppMarketIcon();
412 searchVisible = updateGlobalSearchIcon();
413 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700414 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700415 if (sGlobalSearchIcon[coi] != null) {
416 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700417 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700418 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700419 if (sVoiceSearchIcon[coi] != null) {
420 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700421 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700422 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700423 if (sAppMarketIcon[coi] != null) {
424 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800425 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700426 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 }
Romain Guycbb89e42009-06-08 15:52:54 -0700428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 if (sLocaleConfiguration == null) {
431 new AsyncTask<Void, Void, LocaleConfiguration>() {
432 @Override
433 protected LocaleConfiguration doInBackground(Void... unused) {
434 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
435 readConfiguration(Launcher.this, localeConfiguration);
436 return localeConfiguration;
437 }
438
439 @Override
440 protected void onPostExecute(LocaleConfiguration result) {
441 sLocaleConfiguration = result;
442 checkForLocaleChange(); // recursive, but now with a locale configuration
443 }
444 }.execute();
445 return;
446 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 final Configuration configuration = getResources().getConfiguration();
449
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700450 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 final String locale = configuration.locale.toString();
452
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700453 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 final int mcc = configuration.mcc;
455
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 final int mnc = configuration.mnc;
458
Romain Guy84f296c2009-11-04 15:00:44 -0800459 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460
Romain Guy84f296c2009-11-04 15:00:44 -0800461 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 sLocaleConfiguration.locale = locale;
463 sLocaleConfiguration.mcc = mcc;
464 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700465
Joe Onorato0589f0f2010-02-08 13:44:00 -0800466 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700467
468 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
469 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700470 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 public void run() {
472 writeConfiguration(Launcher.this, localeConfiguration);
473 }
474 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700475 }
476 }
477
478 private static class LocaleConfiguration {
479 public String locale;
480 public int mcc = -1;
481 public int mnc = -1;
482 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700483
Romain Guy98d01652009-06-30 16:21:04 -0700484 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
485 DataInputStream in = null;
486 try {
487 in = new DataInputStream(context.openFileInput(PREFERENCES));
488 configuration.locale = in.readUTF();
489 configuration.mcc = in.readInt();
490 configuration.mnc = in.readInt();
491 } catch (FileNotFoundException e) {
492 // Ignore
493 } catch (IOException e) {
494 // Ignore
495 } finally {
496 if (in != null) {
497 try {
498 in.close();
499 } catch (IOException e) {
500 // Ignore
501 }
502 }
503 }
504 }
505
506 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
507 DataOutputStream out = null;
508 try {
509 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
510 out.writeUTF(configuration.locale);
511 out.writeInt(configuration.mcc);
512 out.writeInt(configuration.mnc);
513 out.flush();
514 } catch (FileNotFoundException e) {
515 // Ignore
516 } catch (IOException e) {
517 //noinspection ResultOfMethodCallIgnored
518 context.getFileStreamPath(PREFERENCES).delete();
519 } finally {
520 if (out != null) {
521 try {
522 out.close();
523 } catch (IOException e) {
524 // Ignore
525 }
526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 }
528 }
529
Adam Cohen716b51e2011-06-30 12:09:54 -0700530 public DragLayer getDragLayer() {
531 return mDragLayer;
532 }
533
Winson Chung36a62fe2012-05-06 18:04:42 -0700534 boolean isDraggingEnabled() {
535 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
536 // that is subsequently removed from the workspace in startBinding().
537 return !mModel.isLoadingWorkspace();
538 }
539
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 static int getScreen() {
541 synchronized (sLock) {
542 return sScreen;
543 }
544 }
545
546 static void setScreen(int screen) {
547 synchronized (sLock) {
548 sScreen = screen;
549 }
550 }
551
Winson Chung557d6ed2011-07-08 15:34:52 -0700552 /**
553 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
554 * a configuration step, this allows the proper animations to run after other transitions.
555 */
556 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700557 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 switch (args.requestCode) {
559 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700560 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
561 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800562 break;
563 case REQUEST_PICK_SHORTCUT:
564 processShortcut(args.intent);
565 break;
566 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700567 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
568 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700569 result = true;
570 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800571 case REQUEST_CREATE_APPWIDGET:
572 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800573 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700574 result = true;
575 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800576 case REQUEST_PICK_WALLPAPER:
577 // We just wanted the activity result here so we can clear mWaitingForResult
578 break;
579 }
Michael Jurka27614d22012-04-02 06:40:22 -0700580 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
581 // if you turned the screen off and then back while in All Apps, Launcher would not
582 // return to the workspace. Clearing mAddInfo.container here fixes this issue
583 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700584 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800585 }
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700588 protected void onActivityResult(
589 final int requestCode, final int resultCode, final Intent data) {
590 if (requestCode == REQUEST_BIND_APPWIDGET) {
591 int appWidgetId = data != null ?
592 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
593 if (resultCode == RESULT_CANCELED) {
594 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
595 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700596 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700597 }
598 return;
599 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700600 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
602 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700603 mWaitingForResult = false;
604
Adam Cohened66b2b2012-01-23 17:28:51 -0800605 // We have special handling for widgets
606 if (isWidgetDrop) {
607 int appWidgetId = data != null ?
608 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700609 if (appWidgetId < 0) {
610 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
611 "widget configuration activity.");
612 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
613 } else {
614 completeTwoStageWidgetDrop(resultCode, appWidgetId);
615 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 return;
617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 // The pattern used here is that a user PICKs a specific application,
620 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
623 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700624 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 final PendingAddArguments args = new PendingAddArguments();
626 args.requestCode = requestCode;
627 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700628 args.container = mPendingAddInfo.container;
629 args.screen = mPendingAddInfo.screen;
630 args.cellX = mPendingAddInfo.cellX;
631 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 if (isWorkspaceLocked()) {
633 sPendingAddList.add(args);
634 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700635 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700639 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800640 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
641 null);
642 }
643
644 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700645 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700646 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 Runnable onCompleteRunnable = null;
648 int animationType = 0;
649
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700650 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 if (resultCode == RESULT_OK) {
652 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
653 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700654 mPendingAddWidgetInfo);
655 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800656 onCompleteRunnable = new Runnable() {
657 @Override
658 public void run() {
659 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
660 mPendingAddInfo.screen, layout, null);
661 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
662 null);
663 }
664 };
665 } else if (resultCode == RESULT_CANCELED) {
666 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
667 onCompleteRunnable = new Runnable() {
668 @Override
669 public void run() {
670 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
671 null);
672 }
673 };
674 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700675 if (mDragLayer.getAnimatedView() != null) {
676 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
677 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
678 animationType, boundWidget, true);
679 } else {
680 // The animated view may be null in the case of a rotation during widget configuration
681 onCompleteRunnable.run();
682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684
685 @Override
686 protected void onResume() {
687 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700688
Winson Chungde0fb8f2012-05-08 14:37:08 -0700689 // Process any items that were added while Launcher was away
690 InstallShortcutReceiver.flushInstallQueue(this);
691
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800692 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700693 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400694 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700695 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400696 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700697 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
Winson Chunge4e50662012-01-23 14:45:13 -0800699
700 // Reset the pressed state of icons that were locked in the press state while activities
701 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800702 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800703 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800704 mWaitingForResume.setStayPressed(false);
705 }
Winson Chunge4e50662012-01-23 14:45:13 -0800706 if (mAppsCustomizeContent != null) {
707 // Resets the previous all apps icon press state
708 mAppsCustomizeContent.resetDrawableState();
709 }
Adam Cohen06dff352012-06-01 17:17:08 -0700710 // It is possible that widgets can receive updates while launcher is not in the foreground.
711 // Consequently, the widgets will be inflated in the orientation of the foreground activity
712 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
713 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700714 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700715
716 // Again, as with the above scenario, it's possible that one or more of the global icons
717 // were updated in the wrong orientation.
718 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700719 }
720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700723 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
724 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
725 // when Launcher resumes and we are still in AllApps.
726 updateWallpaperVisibility(true);
727
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700729 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800730 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700731 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 }
Romain Guycbb89e42009-06-08 15:52:54 -0700733
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700734 @Override
735 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 // Flag the loader to stop early before switching
737 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700738 if (mAppsCustomizeContent != null) {
739 mAppsCustomizeContent.surrender();
740 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800741 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700742 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800744 // We can't hide the IME if it was forced open. So don't bother
745 /*
746 @Override
747 public void onWindowFocusChanged(boolean hasFocus) {
748 super.onWindowFocusChanged(hasFocus);
749
750 if (hasFocus) {
751 final InputMethodManager inputManager = (InputMethodManager)
752 getSystemService(Context.INPUT_METHOD_SERVICE);
753 WindowManager.LayoutParams lp = getWindow().getAttributes();
754 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
755 android.os.Handler()) {
756 protected void onReceiveResult(int resultCode, Bundle resultData) {
757 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
758 }
759 });
760 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
761 }
762 }
763 */
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 private boolean acceptFilter() {
766 final InputMethodManager inputManager = (InputMethodManager)
767 getSystemService(Context.INPUT_METHOD_SERVICE);
768 return !inputManager.isFullscreenMode();
769 }
770
771 @Override
772 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700773 final int uniChar = event.getUnicodeChar();
774 final boolean handled = super.onKeyDown(keyCode, event);
775 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
776 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
778 keyCode, event);
779 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700780 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700781 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700782 // showSearchDialog()
783 // If there are multiple keystrokes before the search dialog takes focus,
784 // onSearchRequested() will be called for every keystroke,
785 // but it is idempotent, so it's fine.
786 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788 }
789
Joe Onorato8a9625e2010-01-28 15:55:35 -0800790 // Eat the long press event so the keyboard doesn't come up.
791 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
792 return true;
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 return handled;
796 }
797
Karl Rosaen138a0412009-04-23 19:00:21 -0700798 private String getTypedText() {
799 return mDefaultKeySsb.toString();
800 }
801
802 private void clearTypedText() {
803 mDefaultKeySsb.clear();
804 mDefaultKeySsb.clearSpans();
805 Selection.setSelection(mDefaultKeySsb, 0);
806 }
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700809 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
810 * State
811 */
812 private static State intToState(int stateOrdinal) {
813 State state = State.WORKSPACE;
814 final State[] stateValues = State.values();
815 for (int i = 0; i < stateValues.length; i++) {
816 if (stateValues[i].ordinal() == stateOrdinal) {
817 state = stateValues[i];
818 break;
819 }
820 }
821 return state;
822 }
823
824 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 * Restores the previous state, if it exists.
826 *
827 * @param savedState The previous state.
828 */
829 private void restoreState(Bundle savedState) {
830 if (savedState == null) {
831 return;
832 }
833
Michael Jurka883f55b2010-10-21 15:47:14 -0700834 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700835 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800836 showAllApps(false);
837 }
838
Winson Chungf0c6ae02012-03-21 16:10:31 -0700839 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700841 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
843
Winson Chung3d503fb2011-07-13 17:25:49 -0700844 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
845 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700846
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
848 mPendingAddInfo.container = pendingAddContainer;
849 mPendingAddInfo.screen = pendingAddScreen;
850 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
851 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700852 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
853 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
854 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700855 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 mRestoring = true;
857 }
858
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
861 if (renameFolder) {
862 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700863 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 mRestoring = true;
865 }
Winson Chunga12a2502010-12-20 14:41:35 -0800866
Winson Chung785d2eb2011-04-14 16:08:02 -0700867
868 // Restore the AppsCustomize tab
869 if (mAppsCustomizeTabHost != null) {
870 String curTab = savedState.getString("apps_customize_currentTab");
871 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700872 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700873 mAppsCustomizeContent.setContentType(
874 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
875 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700876 mAppsCustomizeContent.loadAssociatedPages(
877 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700878 }
879
Winson Chung5afbf7b2011-07-25 11:53:08 -0700880 int currentIndex = savedState.getInt("apps_customize_currentIndex");
881 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884
885 /**
886 * Finds all the views we need and configure them properly.
887 */
888 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700889 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400890
Winson Chung4c98d922011-05-31 16:50:48 -0700891 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
892 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800893 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
894 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895
Winson Chung4c98d922011-05-31 16:50:48 -0700896 // Setup the drag layer
897 mDragLayer.setup(this, dragController);
898
Winson Chung3d503fb2011-07-13 17:25:49 -0700899 // Setup the hotseat
900 mHotseat = (Hotseat) findViewById(R.id.hotseat);
901 if (mHotseat != null) {
902 mHotseat.setup(this);
903 }
904
Winson Chung4c98d922011-05-31 16:50:48 -0700905 // Setup the workspace
906 mWorkspace.setHapticFeedbackEnabled(false);
907 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700908 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700909 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700910
Winson Chungf0ea4d32011-06-06 14:27:16 -0700911 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700912 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700913
Winson Chungf0ea4d32011-06-06 14:27:16 -0700914 // Setup AppsCustomize
915 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
916 findViewById(R.id.apps_customize_pane);
917 mAppsCustomizeContent = (AppsCustomizePagedView)
918 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
919 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400920
Michael Jurka5130e402011-10-13 04:55:35 -0700921 // Get the all apps button
922 mAllAppsButton = findViewById(R.id.all_apps_button);
923 if (mAllAppsButton != null) {
924 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
925 @Override
926 public boolean onTouch(View v, MotionEvent event) {
927 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
928 onTouchDownAllAppsButton(v);
929 }
930 return false;
931 }
932 });
933 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700935 dragController.setDragScoller(mWorkspace);
936 dragController.setScrollView(mDragLayer);
937 dragController.setMoveTarget(mWorkspace);
938 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700939 if (mSearchDropTargetBar != null) {
940 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
944 /**
945 * Creates a view representing a shortcut.
946 *
947 * @param info The data structure describing the shortcut.
948 *
949 * @return A View inflated from R.layout.application.
950 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800951 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700953 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 /**
957 * Creates a view representing a shortcut inflated from the specified resource.
958 *
959 * @param layoutResId The id of the XML layout used to create the shortcut.
960 * @param parent The group the shortcut belongs to.
961 * @param info The data structure describing the shortcut.
962 *
963 * @return A View inflated from layoutResId.
964 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800965 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800966 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
967 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 return favorite;
970 }
971
972 /**
973 * Add an application shortcut to the workspace.
974 *
975 * @param data The intent describing the application.
976 * @param cellInfo The position on screen where to create the shortcut.
977 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700978 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700979 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700981
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 // First we check if we already know the exact location where we want to add this item.
983 if (cellX >= 0 && cellY >= 0) {
984 cellXY[0] = cellX;
985 cellXY[1] = cellY;
986 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700987 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 return;
989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800991 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992
Romain Guy73b979d2009-06-09 12:57:21 -0700993 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800996 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700997 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700998 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 } else {
1000 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002 }
Romain Guycbb89e42009-06-08 15:52:54 -07001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 /**
1005 * Add a shortcut to the workspace.
1006 *
1007 * @param data The intent describing the shortcut.
1008 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001010 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1011 int cellY) {
1012 int[] cellXY = mTmpAddItemCellCoordinates;
1013 int[] touchXY = mPendingAddInfo.dropPos;
1014 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001015
Michael Jurkad3ef3062010-11-23 16:23:58 -08001016 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001017
Adam Cohen558baaf2011-08-15 15:22:57 -07001018 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001019 if (info == null) {
1020 return;
1021 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001022 final View view = createShortcut(info);
1023
Adam Cohenfbba09b2011-07-18 21:43:05 -07001024 // First we check if we already know the exact location where we want to add this item.
1025 if (cellX >= 0 && cellY >= 0) {
1026 cellXY[0] = cellX;
1027 cellXY[1] = cellY;
1028 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001029
1030 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001031 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001032 true, null,null)) {
1033 return;
1034 }
1035 DragObject dragObject = new DragObject();
1036 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001037 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1038 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001039 return;
1040 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001041 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001042 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001043 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001044 foundCellSpan = (result != null);
1045 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001046 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001047 }
1048
1049 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001050 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 return;
1052 }
1053
Adam Cohen558baaf2011-08-15 15:22:57 -07001054 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
1056 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001057 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1058 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060 }
1061
Adam Cohen2f093b62012-04-30 18:59:53 -07001062 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1063 int minHeight) {
1064 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001065 // We want to account for the extra amount of padding that we are adding to the widget
1066 // to ensure that it gets the full amount of space that it has requested
1067 int requiredWidth = minWidth + padding.left + padding.right;
1068 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001069 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001070 }
1071
Adam Cohen2f093b62012-04-30 18:59:53 -07001072 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1073 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001074 }
1075
Adam Cohen2f093b62012-04-30 18:59:53 -07001076 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1077 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001078 }
1079
Adam Cohen2f093b62012-04-30 18:59:53 -07001080 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1081 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001082 }
1083
Adam Cohen2f093b62012-04-30 18:59:53 -07001084 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1085 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001086 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001092 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001095 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1096 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1097 if (appWidgetInfo == null) {
1098 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001101 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001102 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001103
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1105 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001108 // We have saved the position to which the widget was dragged-- this really only matters
1109 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001110 int[] cellXY = mTmpAddItemCellCoordinates;
1111 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001112 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001113 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001114 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1115 cellXY[0] = mPendingAddInfo.cellX;
1116 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001117 spanXY[0] = mPendingAddInfo.spanX;
1118 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001119 foundCellSpan = true;
1120 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001122 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001123 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1124 spanXY[1], cellXY, finalSpan);
1125 spanXY[0] = finalSpan[0];
1126 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001127 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001130 }
1131
Michael Jurka0280c3b2010-09-17 15:00:07 -07001132 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001133 if (appWidgetId != -1) {
1134 // Deleting an app widget ID is a void call but writes to disk before returning
1135 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001136 new Thread("deleteAppWidgetId") {
1137 public void run() {
1138 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1139 }
1140 }.start();
1141 }
Winson Chung93eef082012-03-23 15:59:27 -07001142 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001143 return;
1144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001147 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1148 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 launcherInfo.spanX = spanXY[0];
1150 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1152 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001155 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156
1157 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001158 if (hostView == null) {
1159 // Perform actual inflation because we're live
1160 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1161 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1162 } else {
1163 // The AppWidgetHostView has already been inflated and instantiated
1164 launcherInfo.hostView = hostView;
1165 }
Romain Guycbb89e42009-06-08 15:52:54 -07001166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001168 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001169 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001170 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001171 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001172
1173 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001175 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
Romain Guycbb89e42009-06-08 15:52:54 -07001177
Adam Cohended9f8d2010-11-03 13:25:16 -07001178 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1179 @Override
1180 public void onReceive(Context context, Intent intent) {
1181 final String action = intent.getAction();
1182 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1183 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001184 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001185 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001186
Winson Chungc100e8e2011-08-09 16:02:43 -07001187 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001188 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001189 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1190 mAppsCustomizeTabHost.reset();
1191 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001192 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1194 mUserPresent = true;
1195 updateRunning();
1196 }
1197 }
1198 };
1199
1200 @Override
1201 public void onAttachedToWindow() {
1202 super.onAttachedToWindow();
1203
1204 // Listen for broadcasts related to user-presence
1205 final IntentFilter filter = new IntentFilter();
1206 filter.addAction(Intent.ACTION_SCREEN_OFF);
1207 filter.addAction(Intent.ACTION_USER_PRESENT);
1208 registerReceiver(mReceiver, filter);
1209
Adam Cohend113e0c2010-11-11 10:48:05 -08001210 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001211 mVisible = true;
1212 }
1213
1214 @Override
1215 public void onDetachedFromWindow() {
1216 super.onDetachedFromWindow();
1217 mVisible = false;
1218
Adam Cohend113e0c2010-11-11 10:48:05 -08001219 if (mAttached) {
1220 unregisterReceiver(mReceiver);
1221 mAttached = false;
1222 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 updateRunning();
1224 }
1225
1226 public void onWindowVisibilityChanged(int visibility) {
1227 mVisible = visibility == View.VISIBLE;
1228 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001229 // The following code used to be in onResume, but it turns out onResume is called when
1230 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1231 // is a more appropriate event to handle
1232 if (mVisible) {
1233 mAppsCustomizeTabHost.onWindowVisible();
1234 if (!mWorkspaceLoading) {
1235 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001236 // We want to let Launcher draw itself at least once before we force it to build
1237 // layers on all the workspace pages, so that transitioning to Launcher from other
1238 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1239 // a true draw so we wait until the second preDraw call to be safe
1240 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001241 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001242 // We delay the layer building a bit in order to give
1243 // other message processing a time to run. In particular
1244 // this avoids a delay in hiding the IME if it was
1245 // currently shown, because doing that may involve
1246 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001247 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001248
Michael Jurka6ee21d22012-02-21 18:20:27 -08001249 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001250 return true;
1251 }
1252 });
1253 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001254 // When Launcher comes back to foreground, a different Activity might be responsible for
1255 // the app market intent, so refresh the icon
1256 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001257 clearTypedText();
1258 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001259 }
1260
1261 private void sendAdvanceMessage(long delay) {
1262 mHandler.removeMessages(ADVANCE_MSG);
1263 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1264 mHandler.sendMessageDelayed(msg, delay);
1265 mAutoAdvanceSentTime = System.currentTimeMillis();
1266 }
1267
1268 private void updateRunning() {
1269 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1270 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1271 mAutoAdvanceRunning = autoAdvanceRunning;
1272 if (autoAdvanceRunning) {
1273 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1274 sendAdvanceMessage(delay);
1275 } else {
1276 if (!mWidgetsToAdvance.isEmpty()) {
1277 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1278 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1279 }
1280 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001281 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 }
1283 }
1284 }
1285
1286 private final Handler mHandler = new Handler() {
1287 @Override
1288 public void handleMessage(Message msg) {
1289 if (msg.what == ADVANCE_MSG) {
1290 int i = 0;
1291 for (View key: mWidgetsToAdvance.keySet()) {
1292 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1293 final int delay = mAdvanceStagger * i;
1294 if (v instanceof Advanceable) {
1295 postDelayed(new Runnable() {
1296 public void run() {
1297 ((Advanceable) v).advance();
1298 }
1299 }, delay);
1300 }
1301 i++;
1302 }
1303 sendAdvanceMessage(mAdvanceInterval);
1304 }
1305 }
1306 };
1307
1308 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001309 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1311 if (v instanceof Advanceable) {
1312 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001313 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 updateRunning();
1315 }
1316 }
1317
1318 void removeWidgetToAutoAdvance(View hostView) {
1319 if (mWidgetsToAdvance.containsKey(hostView)) {
1320 mWidgetsToAdvance.remove(hostView);
1321 updateRunning();
1322 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323 }
1324
Joe Onorato9c1289c2009-08-17 11:03:03 -04001325 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001326 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 launcherInfo.hostView = null;
1328 }
1329
Winson Chung93eef082012-03-23 15:59:27 -07001330 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1331 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1332 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001333 }
1334
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001335 public LauncherAppWidgetHost getAppWidgetHost() {
1336 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Winson Chunga9abd0e2010-10-27 17:18:37 -07001339 public LauncherModel getModel() {
1340 return mModel;
1341 }
1342
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001343 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001344 getWindow().closeAllPanels();
1345
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001346 // Whatever we were doing is hereby canceled.
1347 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001348 }
1349
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 @Override
1351 protected void onNewIntent(Intent intent) {
1352 super.onNewIntent(intent);
1353
1354 // Close the menu
1355 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001356 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001357 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001358
Joe Onorato14f122b2009-11-19 14:06:36 -08001359 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1360 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001361
Adam Cohenac56cff2011-09-28 20:45:37 -07001362 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001363 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001364 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001365 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1366 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001367 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001368 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001369
1370 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001371 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001372 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001373
Joe Onorato3a8820b2009-11-10 15:06:42 -08001374 final View v = getWindow().peekDecorView();
1375 if (v != null && v.getWindowToken() != null) {
1376 InputMethodManager imm = (InputMethodManager)getSystemService(
1377 INPUT_METHOD_SERVICE);
1378 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001380
Michael Jurka35aa14d2011-07-07 17:01:08 -07001381 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001382 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001383 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386 }
1387
1388 @Override
1389 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1390 // Do not call super here
1391 mSavedInstanceState = savedInstanceState;
1392 }
1393
1394 @Override
1395 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001396 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001397 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398
Michael Jurka883f55b2010-10-21 15:47:14 -07001399 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001400 // We close any open folder since it will not be re-opened, and we need to make sure
1401 // this state is reflected.
1402 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1405 mWaitingForResult) {
1406 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1407 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1408 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1409 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001410 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1411 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1412 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
1415 if (mFolderInfo != null && mWaitingForResult) {
1416 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1417 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1418 }
Michael Jurka946ad472010-07-09 18:05:18 -07001419
Winson Chung785d2eb2011-04-14 16:08:02 -07001420 // Save the current AppsCustomize tab
1421 if (mAppsCustomizeTabHost != null) {
1422 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1423 if (currentTabTag != null) {
1424 outState.putString("apps_customize_currentTab", currentTabTag);
1425 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001426 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1427 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430
1431 @Override
1432 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Winson Chunge7a03942011-08-05 15:05:12 -07001435 // Remove all pending runnables
1436 mHandler.removeMessages(ADVANCE_MSG);
1437 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001438 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001439
Winson Chungcd2b0142011-06-08 16:02:26 -07001440 // Stop callbacks from LauncherModel
1441 LauncherApplication app = ((LauncherApplication) getApplication());
1442 mModel.stopLoader();
1443 app.setLauncher(null);
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001446 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001448 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001450 mAppWidgetHost = null;
1451
1452 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453
1454 TextKeyListener.getInstance().release();
1455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
Winson Chung3d503fb2011-07-13 17:25:49 -07001457 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001458
1459 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001460 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001461
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001462 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001463 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1464 mWorkspace.removeAllViews();
1465 mWorkspace = null;
1466 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001467
1468 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
Adam Cohena9cf38f2011-05-02 15:36:58 -07001471 public DragController getDragController() {
1472 return mDragController;
1473 }
1474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 @Override
1476 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001477 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 super.startActivityForResult(intent, requestCode);
1479 }
1480
Winson Chung70d72102011-08-12 11:24:35 -07001481 /**
1482 * Indicates that we want global search for this activity by setting the globalSearch
1483 * argument for {@link #startSearch} to true.
1484 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001486 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001488
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001489 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001490
Karl Rosaen138a0412009-04-23 19:00:21 -07001491 if (initialQuery == null) {
1492 // Use any text typed in the launcher as the initial query
1493 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 if (appSearchData == null) {
1496 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001497 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001499 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Karl Rosaen138a0412009-04-23 19:00:21 -07001501 final SearchManager searchManager =
1502 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001503 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001504 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
Winson Chung70d72102011-08-12 11:24:35 -07001507 @Override
1508 public boolean onCreateOptionsMenu(Menu menu) {
1509 if (isWorkspaceLocked()) {
1510 return false;
1511 }
1512
1513 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001514
1515 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1516 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1517 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001518 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1519 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1520 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001521 String helpUrl = getString(R.string.help_url);
1522 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001523 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1524 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1525
Winson Chung70d72102011-08-12 11:24:35 -07001526 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1527 .setIcon(android.R.drawable.ic_menu_gallery)
1528 .setAlphabeticShortcut('W');
1529 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1530 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001531 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001532 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001533 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1534 .setIcon(android.R.drawable.ic_menu_preferences)
1535 .setIntent(settings)
1536 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001537 if (!helpUrl.isEmpty()) {
1538 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1539 .setIcon(android.R.drawable.ic_menu_help)
1540 .setIntent(help)
1541 .setAlphabeticShortcut('H');
1542 }
Winson Chung70d72102011-08-12 11:24:35 -07001543 return true;
1544 }
1545
1546 @Override
1547 public boolean onPrepareOptionsMenu(Menu menu) {
1548 super.onPrepareOptionsMenu(menu);
1549
1550 if (mAppsCustomizeTabHost.isTransitioning()) {
1551 return false;
1552 }
1553 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1554 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1555
1556 return true;
1557 }
1558
1559 @Override
1560 public boolean onOptionsItemSelected(MenuItem item) {
1561 switch (item.getItemId()) {
1562 case MENU_WALLPAPER_SETTINGS:
1563 startWallpaper();
1564 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001565 }
1566
1567 return super.onOptionsItemSelected(item);
1568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001570 @Override
1571 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001572 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001573 // Use a custom animation for launching search
1574 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001575 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001576 }
1577
Joe Onorato9c1289c2009-08-17 11:03:03 -04001578 public boolean isWorkspaceLocked() {
1579 return mWorkspaceLoading || mWaitingForResult;
1580 }
1581
Michael Jurka0280c3b2010-09-17 15:00:07 -07001582 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 mPendingAddInfo.container = ItemInfo.NO_ID;
1584 mPendingAddInfo.screen = -1;
1585 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1586 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001587 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001589 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001590
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001591 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1592 AppWidgetProviderInfo appWidgetInfo) {
1593 if (appWidgetInfo.configure != null) {
1594 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001595
Bjorn Bringert7984c942009-12-09 15:38:25 +00001596 // Launch over to configure widget, if needed
1597 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001598 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001599 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001600 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001602 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001603 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1604 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001605 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001606 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 }
1608 }
Romain Guycbb89e42009-06-08 15:52:54 -07001609
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 /**
1611 * Process a shortcut drop.
1612 *
1613 * @param componentName The name of the component
1614 * @param screen The screen where it should be added
1615 * @param cell The cell it should be added to, optional
1616 * @param position The location on the screen where it was dropped, optional
1617 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1619 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001620 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001621 mPendingAddInfo.container = container;
1622 mPendingAddInfo.screen = screen;
1623 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001624
1625 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001626 mPendingAddInfo.cellX = cell[0];
1627 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001628 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001629
1630 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1631 createShortcutIntent.setComponent(componentName);
1632 processShortcut(createShortcutIntent);
1633 }
1634
Adam Cohenfbba09b2011-07-18 21:43:05 -07001635 /**
1636 * Process a widget drop.
1637 *
1638 * @param info The PendingAppWidgetInfo of the widget being added.
1639 * @param screen The screen where it should be added
1640 * @param cell The cell it should be added to, optional
1641 * @param position The location on the screen where it was dropped, optional
1642 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001643 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001644 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001645 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 mPendingAddInfo.container = info.container = container;
1647 mPendingAddInfo.screen = info.screen = screen;
1648 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001649 mPendingAddInfo.minSpanX = info.minSpanX;
1650 mPendingAddInfo.minSpanY = info.minSpanY;
1651
Adam Cohenfbba09b2011-07-18 21:43:05 -07001652 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001653 mPendingAddInfo.cellX = cell[0];
1654 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001655 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001656 if (span != null) {
1657 mPendingAddInfo.spanX = span[0];
1658 mPendingAddInfo.spanY = span[1];
1659 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001660
Adam Cohened66b2b2012-01-23 17:28:51 -08001661 AppWidgetHostView hostView = info.boundWidget;
1662 int appWidgetId;
1663 if (hostView != null) {
1664 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001665 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001666 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001667 // In this case, we either need to start an activity to get permission to bind
1668 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001669 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001670 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001671 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001672 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001673 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001674 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1675 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1676 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1677 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1678 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001679 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001680 }
1681
Joe Onoratodeb98af2010-02-19 14:59:39 -08001682 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001683 // Handle case where user selected "Applications"
1684 String applicationName = getResources().getString(R.string.group_applications);
1685 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001687 if (applicationName != null && applicationName.equals(shortcutName)) {
1688 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1689 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001690
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001691 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1692 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001693 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001694 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001695 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001696 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699
Winson Chung24ab2f12010-09-16 14:10:47 -07001700 void processWallpaper(Intent intent) {
1701 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1702 }
1703
Winson Chung3d503fb2011-07-13 17:25:49 -07001704 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1705 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001706 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 folderInfo.title = getText(R.string.folder_name);
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001710 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1711 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001712 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713
1714 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001715 FolderIcon newFolder =
1716 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1717 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1718 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001719 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Joe Onorato9c1289c2009-08-17 11:03:03 -04001722 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001723 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001724 }
1725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001727 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001729 Intent chooser = Intent.createChooser(pickWallpaper,
1730 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001731 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1732 // Removed in Eclair MR1
1733// WallpaperManager wm = (WallpaperManager)
1734// getSystemService(Context.WALLPAPER_SERVICE);
1735// WallpaperInfo wi = wm.getWallpaperInfo();
1736// if (wi != null && wi.getSettingsActivity() != null) {
1737// LabeledIntent li = new LabeledIntent(getPackageName(),
1738// R.string.configure_wallpaper, 0);
1739// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1740// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1741// }
Mike Clerona0618e42009-10-22 13:55:21 -07001742 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 }
1744
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001745 /**
1746 * Registers various content observers. The current implementation registers
1747 * only a favorites observer to keep track of the favorites applications.
1748 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001749 private void registerContentObservers() {
1750 ContentResolver resolver = getContentResolver();
1751 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1752 true, mWidgetObserver);
1753 }
1754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 @Override
1756 public boolean dispatchKeyEvent(KeyEvent event) {
1757 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1758 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001760 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001761 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001762 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001763 dumpState();
1764 return true;
1765 }
1766 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001767 }
1768 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1769 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001770 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 return true;
1772 }
1773 }
1774
1775 return super.dispatchKeyEvent(event);
1776 }
1777
Joe Onorato88ec0992009-11-19 13:16:06 -08001778 @Override
1779 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001780 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001781 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001782 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001783 Folder openFolder = mWorkspace.getOpenFolder();
1784 if (openFolder.isEditingName()) {
1785 openFolder.dismissEditingName();
1786 } else {
1787 closeFolder();
1788 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001789 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001790 mWorkspace.exitWidgetResizeMode();
1791
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001792 // Back button is a no-op here, but give at least some feedback for the button press
1793 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001794 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001795 }
1796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001798 * Re-listen when widgets are reset.
1799 */
1800 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001801 if (mAppWidgetHost != null) {
1802 mAppWidgetHost.startListening();
1803 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001804 }
1805
1806 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1808 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001811 if (mModel != null) {
1812 mModel.unbindWorkspaceItems();
1813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001815
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 /**
1817 * Launches the intent referred by the clicked shortcut.
1818 *
1819 * @param v The view representing the clicked shortcut.
1820 */
1821 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001822 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1823 // view has detached (it's possible for this to happen if the view is removed mid touch).
1824 if (v.getWindowToken() == null) {
1825 return;
1826 }
1827
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001828 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001829 return;
1830 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001833 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001835 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001836 int[] pos = new int[2];
1837 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001838 intent.setSourceBounds(new Rect(pos[0], pos[1],
1839 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001840
1841 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001842
1843 if (success && v instanceof BubbleTextView) {
1844 mWaitingForResume = (BubbleTextView) v;
1845 mWaitingForResume.setStayPressed(true);
1846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001848 if (v instanceof FolderIcon) {
1849 FolderIcon fi = (FolderIcon) v;
1850 handleFolderClick(fi);
1851 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001852 } else if (v == mAllAppsButton) {
1853 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001854 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001855 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001856 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
1859 }
1860
Michael Jurka0e260592010-06-30 17:07:39 -07001861 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001862 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001863 // clicking anywhere on the workspace causes the customization drawer to slide down
1864 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001865 return false;
1866 }
1867
Michael Jurkaaf442092010-06-10 17:01:57 -07001868 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001869 * Event handler for the search button
1870 *
1871 * @param v The view that was clicked.
1872 */
1873 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001874 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1875
Amith Yamasania135ba82011-08-09 17:42:01 -07001876 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001877 }
1878
1879 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001880 * Event handler for the voice button
1881 *
1882 * @param v The view that was clicked.
1883 */
1884 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001885 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001886
Michael Jurkaae65ee32012-04-30 11:45:54 -07001887 try {
1888 final SearchManager searchManager =
1889 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1890 ComponentName activityName = searchManager.getGlobalSearchActivity();
1891 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001893 if (activityName != null) {
1894 intent.setPackage(activityName.getPackageName());
1895 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001896 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001897 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001898 } catch (ActivityNotFoundException e) {
1899 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001900 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001901 startActivitySafely(null, intent, "onClickVoiceButton");
1902 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001903 }
1904
1905 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001906 * Event handler for the "grid" button that appears on the home screen, which
1907 * enters all apps mode.
1908 *
1909 * @param v The view that was clicked.
1910 */
1911 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001912 showAllApps(true);
1913 }
1914
1915 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001916 // Provide the same haptic feedback that the system offers for virtual keys.
1917 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001918 }
1919
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001920 public void onClickAppMarketButton(View v) {
1921 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001922 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001923 } else {
1924 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001925 }
1926 }
1927
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001928 void startApplicationDetailsActivity(ComponentName componentName) {
1929 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001930 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1931 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001932 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001933 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001934 }
1935
Patrick Dubroy5539af72010-09-07 15:22:01 -07001936 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1937 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1938 // System applications cannot be installed. For now, show a toast explaining that.
1939 // We may give them the option of disabling apps this way.
1940 int messageId = R.string.uninstall_system_app_text;
1941 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1942 } else {
1943 String packageName = appInfo.componentName.getPackageName();
1944 String className = appInfo.componentName.getClassName();
1945 Intent intent = new Intent(
1946 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001947 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1948 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001949 startActivity(intent);
1950 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001951 }
1952
Michael Jurka86a720e2012-05-09 11:23:23 -07001953 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001957 // Only launch using the new animation if the shortcut has not opted out (this is a
1958 // private contract between launcher and may be ignored in the future).
1959 boolean useLaunchAnimation = (v != null) &&
1960 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1961 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001962 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1963 v.getMeasuredWidth(), v.getMeasuredHeight());
1964
1965 startActivity(intent, opts.toBundle());
1966 } else {
1967 startActivity(intent);
1968 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001969 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 } catch (SecurityException e) {
1971 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001972 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001974 "or use the exported attribute for this activity. "
1975 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001977 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001979
Michael Jurka86a720e2012-05-09 11:23:23 -07001980 boolean startActivitySafely(View v, Intent intent, Object tag) {
1981 boolean success = false;
1982 try {
1983 success = startActivity(v, intent, tag);
1984 } catch (ActivityNotFoundException e) {
1985 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1986 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
1987 }
1988 return success;
1989 }
1990
Romain Guy8e633c52010-03-04 12:51:36 -08001991 void startActivityForResultSafely(Intent intent, int requestCode) {
1992 try {
1993 startActivityForResult(intent, requestCode);
1994 } catch (ActivityNotFoundException e) {
1995 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1996 } catch (SecurityException e) {
1997 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1998 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1999 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2000 "or use the exported attribute for this activity.", e);
2001 }
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
Adam Cohena9cf38f2011-05-02 15:36:58 -07002004 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002005 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002006 Folder openFolder = mWorkspace.getFolderForTag(info);
2007
2008 // If the folder info reports that the associated folder is open, then verify that
2009 // it is actually opened. There have been a few instances where this gets out of sync.
2010 if (info.opened && openFolder == null) {
2011 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2012 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2013 info.opened = false;
2014 }
2015
Adam Cohenfb91f302012-06-11 15:45:18 -07002016 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 // Close any open folder
2018 closeFolder();
2019 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002020 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 } else {
2022 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 int folderScreen;
2024 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002025 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 // .. and close it
2027 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002028 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 // Close any folder open on the current screen
2030 closeFolder();
2031 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002032 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034 }
2035 }
2036 }
2037
Adam Cohen268c4752012-06-06 17:47:33 -07002038 /**
2039 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2040 * in the DragLayer in the exact absolute location of the original FolderIcon.
2041 */
2042 private void copyFolderIconToImage(FolderIcon fi) {
2043 final int width = fi.getMeasuredWidth();
2044 final int height = fi.getMeasuredHeight();
2045
2046 // Lazy load ImageView, Bitmap and Canvas
2047 if (mFolderIconImageView == null) {
2048 mFolderIconImageView = new ImageView(this);
2049 }
2050 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2051 mFolderIconBitmap.getHeight() != height) {
2052 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2053 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2054 }
2055
2056 DragLayer.LayoutParams lp;
2057 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2058 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2059 } else {
2060 lp = new DragLayer.LayoutParams(width, height);
2061 }
2062
2063 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2064 lp.customPosition = true;
2065 lp.x = mRectForFolderAnimation.left;
2066 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002067 lp.width = width;
2068 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002069
2070 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2071 fi.draw(mFolderIconCanvas);
2072 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002073 if (fi.getFolder() != null) {
2074 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2075 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002076 }
Adam Cohen268c4752012-06-06 17:47:33 -07002077 // Just in case this image view is still in the drag layer from a previous animation,
2078 // we remove it and re-add it.
2079 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2080 mDragLayer.removeView(mFolderIconImageView);
2081 }
2082 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002083 if (fi.getFolder() != null) {
2084 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002085 }
Adam Cohen268c4752012-06-06 17:47:33 -07002086 }
2087
Adam Cohen2801caf2011-05-13 20:57:39 -07002088 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002089 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002090 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2091 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2092 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2093
Adam Cohenc51934b2011-07-26 21:07:43 -07002094 FolderInfo info = (FolderInfo) fi.getTag();
2095 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2096 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002097 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2098 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002099 }
2100
Adam Cohen268c4752012-06-06 17:47:33 -07002101 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2102 copyFolderIconToImage(fi);
2103 fi.setVisibility(View.INVISIBLE);
2104
2105 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2106 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002107 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2108 oa.start();
2109 }
2110
Adam Cohen268c4752012-06-06 17:47:33 -07002111 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002112 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002113 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2114 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2115 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2116
Adam Cohen268c4752012-06-06 17:47:33 -07002117 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002118
Adam Cohen268c4752012-06-06 17:47:33 -07002119 // We remove and re-draw the FolderIcon in-case it has changed
2120 mDragLayer.removeView(mFolderIconImageView);
2121 copyFolderIconToImage(fi);
Adam Cohen268c4752012-06-06 17:47:33 -07002122 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2123 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002124 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002125 oa.addListener(new AnimatorListenerAdapter() {
2126 @Override
2127 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002128 if (cl != null) {
2129 cl.clearFolderLeaveBehind();
2130 // Remove the ImageView copy of the FolderIcon and make the original visible.
2131 mDragLayer.removeView(mFolderIconImageView);
2132 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002133 }
2134 }
2135 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002136 oa.start();
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002140 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 * is animated relative to the specified View. If the View is null, no animation
2142 * is played.
2143 *
2144 * @param folderInfo The FolderInfo describing the folder to open.
2145 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002146 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002147 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002148 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149
Adam Cohena9cf38f2011-05-02 15:36:58 -07002150 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151
Adam Cohen4554ee12011-08-03 16:13:21 -07002152 // Just verify that the folder hasn't already been added to the DragLayer.
2153 // There was a one-off crash where the folder had a parent already.
2154 if (folder.getParent() == null) {
2155 mDragLayer.addView(folder);
2156 mDragController.addDropTarget((DropTarget) folder);
2157 } else {
2158 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2159 folder.getParent() + ").");
2160 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002161 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002162 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002163 }
2164
2165 public void closeFolder() {
2166 Folder folder = mWorkspace.getOpenFolder();
2167 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002168 if (folder.isEditingName()) {
2169 folder.dismissEditingName();
2170 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002171 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002172
2173 // Dismiss the folder cling
2174 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002175 }
2176 }
2177
2178 void closeFolder(Folder folder) {
2179 folder.getInfo().opened = false;
2180
2181 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2182 if (parent != null) {
2183 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2184 shrinkAndFadeInFolderIcon(fi);
2185 }
2186 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
2188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002190 if (!isDraggingEnabled()) return false;
2191 if (isWorkspaceLocked()) return false;
2192 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193
2194 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002195 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
2197
Michael Jurka0280c3b2010-09-17 15:00:07 -07002198 resetAddInfo();
2199 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002201 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 return true;
2203 }
2204
Winson Chung3d503fb2011-07-13 17:25:49 -07002205 // The hotseat touch handling does not go through Workspace, and we always allow long press
2206 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002207 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002208 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2209 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 if (itemUnderLongClick == null) {
2211 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002214 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 }
2220 }
2221 }
2222 return true;
2223 }
2224
Winson Chung3d503fb2011-07-13 17:25:49 -07002225 boolean isHotseatLayout(View layout) {
2226 return mHotseat != null && layout != null &&
2227 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2228 }
2229 Hotseat getHotseat() {
2230 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002231 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002232 SearchDropTargetBar getSearchBar() {
2233 return mSearchDropTargetBar;
2234 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002235
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 /**
2237 * Returns the CellLayout of the specified container at the specified screen.
2238 */
2239 CellLayout getCellLayout(long container, int screen) {
2240 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2241 if (mHotseat != null) {
2242 return mHotseat.getLayout();
2243 } else {
2244 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002245 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 } else {
2247 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002248 }
2249 }
2250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 Workspace getWorkspace() {
2252 return mWorkspace;
2253 }
2254
Daniel Sandler843e8602010-06-07 14:59:01 -04002255 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2256 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002257 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002258 }
2259
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002260 public boolean isAllAppsButtonRank(int rank) {
2261 return mHotseat.isAllAppsButtonRank(rank);
2262 }
2263
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002264 /**
2265 * Helper method for the cameraZoomIn/cameraZoomOut animations
2266 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002267 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002268 * @param scaleFactor The scale factor used for the zoom
2269 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002270 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002271 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002272 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002274
Winson Chung18f41f82012-05-09 13:28:10 -07002275 void disableWallpaperIfInAllApps() {
2276 // Only disable it if we are in all apps
2277 if (mState == State.APPS_CUSTOMIZE) {
2278 if (mAppsCustomizeTabHost != null &&
2279 !mAppsCustomizeTabHost.isTransitioning()) {
2280 updateWallpaperVisibility(false);
2281 }
2282 }
2283 }
2284
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002285 void updateWallpaperVisibility(boolean visible) {
2286 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2287 int curflags = getWindow().getAttributes().flags
2288 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2289 if (wpflags != curflags) {
2290 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2291 }
2292 }
2293
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002294 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2295 if (v instanceof LauncherTransitionable) {
2296 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2297 }
2298 }
2299
Michael Jurkabed61d22012-02-14 22:51:29 -08002300 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2301 if (v instanceof LauncherTransitionable) {
2302 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2303 }
Winson Chung70442722012-02-10 15:43:22 -08002304
2305 // Update the workspace transition step as well
2306 dispatchOnLauncherTransitionStep(v, 0f);
2307 }
2308
2309 private void dispatchOnLauncherTransitionStep(View v, float t) {
2310 if (v instanceof LauncherTransitionable) {
2311 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2312 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002313 }
2314
2315 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2316 if (v instanceof LauncherTransitionable) {
2317 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2318 }
Winson Chung70442722012-02-10 15:43:22 -08002319
2320 // Update the workspace transition step as well
2321 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002322 }
2323
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002324 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002325 * Things to test when changing the following seven functions.
2326 * - Home from workspace
2327 * - from center screen
2328 * - from other screens
2329 * - Home from all apps
2330 * - from center screen
2331 * - from other screens
2332 * - Back from all apps
2333 * - from center screen
2334 * - from other screens
2335 * - Launch app from workspace and quit
2336 * - with back
2337 * - with home
2338 * - Launch app from all apps and quit
2339 * - with back
2340 * - with home
2341 * - Go to a screen that's not the default, then all
2342 * apps, and launch and app, and go back
2343 * - with back
2344 * -with home
2345 * - On workspace, long press power and go back
2346 * - with back
2347 * - with home
2348 * - On all apps, long press power and go back
2349 * - with back
2350 * - with home
2351 * - On workspace, power off
2352 * - On all apps, power off
2353 * - Launch an app and turn off the screen while in that app
2354 * - Go back with home key
2355 * - Go back with back key TODO: make this not go to workspace
2356 * - From all apps
2357 * - From workspace
2358 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2359 * - From all apps
2360 * - From the center workspace
2361 * - From another workspace
2362 */
2363
2364 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002365 * Zoom the camera out from the workspace to reveal 'toView'.
2366 * Assumes that the view to show is anchored at either the very top or very bottom
2367 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002368 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002369 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002370 if (mStateAnimation != null) {
2371 mStateAnimation.cancel();
2372 mStateAnimation = null;
2373 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002374 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002375
Winson Chungf0ea4d32011-06-06 14:27:16 -07002376 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2377 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2378 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002379 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002380 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002381 final int startDelay =
2382 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002383
Michael Jurkab3e22d92011-10-31 15:58:33 -07002384 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002385
Michael Jurkad74c9842011-07-10 12:44:21 -07002386 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387 Animator workspaceAnim =
2388 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002389
2390 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002391 toView.setScaleX(scale);
2392 toView.setScaleY(scale);
2393 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2394 scaleAnim.
2395 scaleX(1f).scaleY(1f).
2396 setDuration(duration).
2397 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002398
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002400 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002401 final ObjectAnimator alphaAnim = ObjectAnimator
2402 .ofFloat(toView, "alpha", 0f, 1f)
2403 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002404 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002405 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2406 @Override
2407 public void onAnimationUpdate(ValueAnimator animation) {
2408 float t = (Float) animation.getAnimatedValue();
2409 dispatchOnLauncherTransitionStep(fromView, t);
2410 dispatchOnLauncherTransitionStep(toView, t);
2411 }
2412 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002413
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002414 // toView should appear right at the end of the workspace shrink
2415 // animation
2416 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002417 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002418 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002419
2420 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002421 boolean animationCancelled = false;
2422
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002423 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002424 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002425 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 // Prepare the position
2427 toView.setTranslationX(0.0f);
2428 toView.setTranslationY(0.0f);
2429 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002430 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002432 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002433 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002434 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2435 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002436
2437 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2438 // Hide the workspace scrollbar
2439 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002440 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002441 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002442 if (!animationCancelled) {
2443 updateWallpaperVisibility(false);
2444 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002445
2446 // Hide the search bar
2447 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002448 }
2449
Adam Cohencff6af82011-09-13 14:51:53 -07002450 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002451 public void onAnimationCancel(Animator animation) {
2452 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002453 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002454 });
2455
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002456 if (workspaceAnim != null) {
2457 mStateAnimation.play(workspaceAnim);
2458 }
Michael Jurka1899a362011-11-03 13:50:45 -07002459
2460 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002461 final ViewTreeObserver observer;
2462
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002463 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2464 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002465
2466 // If any of the objects being animated haven't been measured/laid out
2467 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002468 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002469 (mWorkspace.getMeasuredWidth() == 0) ||
2470 (toView.getMeasuredWidth() == 0)) {
2471 observer = mWorkspace.getViewTreeObserver();
2472 delayAnim = true;
2473 } else {
2474 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002475 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002476
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002477 final AnimatorSet stateAnimation = mStateAnimation;
2478 final Runnable startAnimRunnable = new Runnable() {
2479 public void run() {
2480 // Check that mStateAnimation hasn't changed while
2481 // we waited for a layout/draw pass
2482 if (mStateAnimation != stateAnimation)
2483 return;
2484 setPivotsForZoom(toView, scale);
2485 dispatchOnLauncherTransitionStart(fromView, animated, false);
2486 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002487 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002488 public void run() {
2489 // Check that mStateAnimation hasn't changed while
2490 // we waited for a layout/draw pass
2491 if (mStateAnimation != stateAnimation)
2492 return;
2493 mStateAnimation.start();
2494 }
2495 });
2496 }
2497 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002498 if (delayAnim) {
2499 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2500 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002501 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002502 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002503 }
2504 };
2505 observer.addOnGlobalLayoutListener(delayedStart);
2506 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002507 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002508 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002509 } else {
2510 toView.setTranslationX(0.0f);
2511 toView.setTranslationY(0.0f);
2512 toView.setScaleX(1.0f);
2513 toView.setScaleY(1.0f);
2514 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002515 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002516
Michael Jurkabed61d22012-02-14 22:51:29 -08002517 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2518 // Hide the workspace scrollbar
2519 mWorkspace.hideScrollingIndicator(true);
2520 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002521
2522 // Hide the search bar
2523 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002524 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002525 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002526 dispatchOnLauncherTransitionStart(fromView, animated, false);
2527 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002528 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002529 dispatchOnLauncherTransitionStart(toView, animated, false);
2530 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002531 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002533 }
2534
2535 /**
2536 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002537 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002539 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002540 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2541 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002542
Michael Jurkab3e22d92011-10-31 15:58:33 -07002543 if (mStateAnimation != null) {
2544 mStateAnimation.cancel();
2545 mStateAnimation = null;
2546 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002547 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002548
Winson Chungf0ea4d32011-06-06 14:27:16 -07002549 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002550 final int fadeOutDuration =
2551 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 final float scaleFactor = (float)
2553 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2554 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002555 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002556 Animator workspaceAnim = null;
2557
2558 if (toState == State.WORKSPACE) {
2559 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2560 workspaceAnim = mWorkspace.getChangeStateAnimation(
2561 Workspace.State.NORMAL, animated, stagger);
2562 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2563 workspaceAnim = mWorkspace.getChangeStateAnimation(
2564 Workspace.State.SPRING_LOADED, animated);
2565 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002566
Michael Jurkab3e22d92011-10-31 15:58:33 -07002567 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002568 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002569 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002570 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002571 final LauncherViewPropertyAnimator scaleAnim =
2572 new LauncherViewPropertyAnimator(fromView);
2573 scaleAnim.
2574 scaleX(scaleFactor).scaleY(scaleFactor).
2575 setDuration(duration).
2576 setInterpolator(new Workspace.ZoomInInterpolator());
2577
2578 final ObjectAnimator alphaAnim = ObjectAnimator
2579 .ofFloat(fromView, "alpha", 1f, 0f)
2580 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002581 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002582 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2583 @Override
2584 public void onAnimationUpdate(ValueAnimator animation) {
2585 float t = 1f - (Float) animation.getAnimatedValue();
2586 dispatchOnLauncherTransitionStep(fromView, t);
2587 dispatchOnLauncherTransitionStep(toView, t);
2588 }
2589 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002590
Michael Jurkabed61d22012-02-14 22:51:29 -08002591 mStateAnimation = new AnimatorSet();
2592
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002593 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2594 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002595
2596 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002597 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002598 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002599 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002600 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002601 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2602 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002603 if (mWorkspace != null) {
2604 mWorkspace.hideScrollingIndicator(false);
2605 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002606 if (onCompleteRunnable != null) {
2607 onCompleteRunnable.run();
2608 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002609 }
2610 });
2611
Winson Chungf0ea4d32011-06-06 14:27:16 -07002612 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002613 if (workspaceAnim != null) {
2614 mStateAnimation.play(workspaceAnim);
2615 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002616 dispatchOnLauncherTransitionStart(fromView, animated, true);
2617 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002618 final Animator stateAnimation = mStateAnimation;
2619 mWorkspace.post(new Runnable() {
2620 public void run() {
2621 if (stateAnimation != mStateAnimation)
2622 return;
2623 mStateAnimation.start();
2624 }
2625 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 } else {
2627 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002628 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002629 dispatchOnLauncherTransitionStart(fromView, animated, true);
2630 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002631 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002632 dispatchOnLauncherTransitionStart(toView, animated, true);
2633 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002634 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002636 }
2637
Michael Jurkae326f182011-11-21 14:05:46 -08002638 @Override
2639 public void onTrimMemory(int level) {
2640 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002641 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002642 mAppsCustomizeTabHost.onTrimMemory();
2643 }
2644 }
2645
Winson Chung18f41f82012-05-09 13:28:10 -07002646 @Override
2647 public void onWindowFocusChanged(boolean hasFocus) {
2648 if (!hasFocus) {
2649 // When another window occludes launcher (like the notification shade, or recents),
2650 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2651 updateWallpaperVisibility(true);
2652 } else {
2653 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002654 mWorkspace.postDelayed(new Runnable() {
2655 @Override
2656 public void run() {
2657 disableWallpaperIfInAllApps();
2658 }
2659 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002660 }
2661 }
2662
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002663 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002664 showWorkspace(animated, null);
2665 }
2666
2667 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002668 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002669 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002670 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002671 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002672
Winson Chungc7d2b602012-05-16 17:02:20 -07002673 // Show the search bar (only animate if we were showing the drop target bar in spring
2674 // loaded mode)
2675 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2676
Michael Jurkab737ee62011-11-15 15:57:22 -08002677 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002678 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002679
2680 // Set focus to the AppsCustomize button
2681 if (mAllAppsButton != null) {
2682 mAllAppsButton.requestFocus();
2683 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002684 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002685
Michael Jurkab737ee62011-11-15 15:57:22 -08002686 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002687
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002688 // Change the state *after* we've called all the transition code
2689 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002690
Winson Chung5fb63472011-02-02 17:03:37 -08002691 // Resume the auto-advance of widgets
2692 mUserPresent = true;
2693 updateRunning();
2694
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002695 // send an accessibility event to announce the context change
2696 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002697 }
2698
Michael Jurkab3e22d92011-10-31 15:58:33 -07002699 void showAllApps(boolean animated) {
2700 if (mState != State.WORKSPACE) return;
2701
2702 showAppsCustomizeHelper(animated, false);
2703 mAppsCustomizeTabHost.requestFocus();
2704
Michael Jurkab3e22d92011-10-31 15:58:33 -07002705 // Change the state *after* we've called all the transition code
2706 mState = State.APPS_CUSTOMIZE;
2707
2708 // Pause the auto-advance of widgets until we are out of AllApps
2709 mUserPresent = false;
2710 updateRunning();
2711 closeFolder();
2712
2713 // Send an accessibility event to announce the context change
2714 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2715 }
2716
Adam Cohen7777d962011-08-18 18:58:38 -07002717 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002718 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002719 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002720 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002721 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002722 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002723 }
Adam Cohen7777d962011-08-18 18:58:38 -07002724
Adam Cohened66b2b2012-01-23 17:28:51 -08002725 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2726 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002727 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2728
Winson Chunge7a03942011-08-05 15:05:12 -07002729 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002730 @Override
2731 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002732 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002733 // Before we show workspace, hide all apps again because
2734 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2735 // clean up our state transition functions
2736 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002737 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002738 } else {
2739 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002740 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002741 }
2742 }, (extendedDelay ?
2743 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2744 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2745 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002746
Michael Jurkad3ef3062010-11-23 16:23:58 -08002747 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002748 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002749 final boolean animated = true;
2750 final boolean springLoaded = true;
2751 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002752 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002753 }
2754 // Otherwise, we are not in spring loaded mode, so don't do anything.
2755 }
2756
Michael Jurkab737ee62011-11-15 15:57:22 -08002757 void hideDockDivider() {
2758 if (mQsbDivider != null && mDockDivider != null) {
2759 mQsbDivider.setVisibility(View.INVISIBLE);
2760 mDockDivider.setVisibility(View.INVISIBLE);
2761 }
2762 }
2763
2764 void showDockDivider(boolean animated) {
2765 if (mQsbDivider != null && mDockDivider != null) {
2766 mQsbDivider.setVisibility(View.VISIBLE);
2767 mDockDivider.setVisibility(View.VISIBLE);
2768 if (mDividerAnimator != null) {
2769 mDividerAnimator.cancel();
2770 mQsbDivider.setAlpha(1f);
2771 mDockDivider.setAlpha(1f);
2772 mDividerAnimator = null;
2773 }
2774 if (animated) {
2775 mDividerAnimator = new AnimatorSet();
2776 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2777 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2778 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2779 mDividerAnimator.start();
2780 }
2781 }
2782 }
2783
Michael Jurkab3e22d92011-10-31 15:58:33 -07002784 void lockAllApps() {
2785 // TODO
2786 }
2787
2788 void unlockAllApps() {
2789 // TODO
2790 }
2791
Adam Cohenfc53cd22011-07-20 15:45:11 -07002792 public boolean isAllAppsCustomizeOpen() {
2793 return mState == State.APPS_CUSTOMIZE;
2794 }
2795
Winson Chungf0ea4d32011-06-06 14:27:16 -07002796 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002797 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002798 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002799 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002800 if (!LauncherApplication.isScreenLarge()) {
2801 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002802 if (mHotseat.getAlpha() != 1f) {
2803 int duration = mSearchDropTargetBar.getTransitionInDuration();
2804 mHotseat.animate().alpha(1f).setDuration(duration);
2805 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002806 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002807 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002808 }
2809 }
2810 }
2811
2812 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002813 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002814 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002815 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002816 if (!LauncherApplication.isScreenLarge()) {
2817 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002818 if (mHotseat.getAlpha() != 0f) {
2819 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2820 mHotseat.animate().alpha(0f).setDuration(duration);
2821 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002822 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002823 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002824 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002825 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002826 }
2827
Patrick Dubroy5f445422011-02-18 14:35:21 -08002828 /**
2829 * Add an item from all apps or customize onto the given workspace screen.
2830 * If layout is null, add to the current screen.
2831 */
2832 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002833 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002834 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002835 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002836 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002837
Winson Chungdff8ebb2011-09-08 17:25:31 -07002838 /** Maps the current orientation to an index for referencing orientation correct global icons */
2839 private int getCurrentOrientationIndexForGlobalIcons() {
2840 // default - 0, landscape - 1
2841 switch (getResources().getConfiguration().orientation) {
2842 case Configuration.ORIENTATION_LANDSCAPE:
2843 return 1;
2844 default:
2845 return 0;
2846 }
2847 }
2848
Michael Jurkaae65ee32012-04-30 11:45:54 -07002849 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002850 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002851 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002852 // Look for the toolbar icon specified in the activity meta-data
2853 Bundle metaData = packageManager.getActivityInfo(
2854 activityName, PackageManager.GET_META_DATA).metaData;
2855 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002856 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002857 if (iconResId != 0) {
2858 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002859 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002860 }
2861 }
2862 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002863 // This can happen if the activity defines an invalid drawable
2864 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2865 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002866 } catch (Resources.NotFoundException nfe) {
2867 // This can happen if the activity defines an invalid drawable
2868 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2869 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002870 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002871 return null;
2872 }
2873
2874 // if successful in getting icon, return it; otherwise, set button to use default drawable
2875 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002876 int buttonId, ComponentName activityName, int fallbackDrawableId,
2877 String toolbarResourceName) {
2878 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002879 Resources r = getResources();
2880 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2881 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002882
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002883 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002884 // If we were unable to find the icon via the meta-data, use a generic one
2885 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002886 toolbarIcon = r.getDrawable(fallbackDrawableId);
2887 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002888 if (button != null) {
2889 button.setCompoundDrawables(toolbarIcon, null, null, null);
2890 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002891 return null;
2892 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002893 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002894 if (button != null) {
2895 button.setCompoundDrawables(toolbarIcon, null, null, null);
2896 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002897 return toolbarIcon.getConstantState();
2898 }
2899 }
2900
2901 // if successful in getting icon, return it; otherwise, set button to use default drawable
2902 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002903 int buttonId, ComponentName activityName, int fallbackDrawableId,
2904 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002905 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002906 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002907
Michael Jurka19e0fc52011-07-22 18:00:21 -07002908 if (button != null) {
2909 // If we were unable to find the icon via the meta-data, use a
2910 // generic one
2911 if (toolbarIcon == null) {
2912 button.setImageResource(fallbackDrawableId);
2913 } else {
2914 button.setImageDrawable(toolbarIcon);
2915 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002916 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002917
2918 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2919
Michael Jurka0423dcf2010-10-05 14:56:18 -07002920 }
2921
Winson Chung1b884092012-06-08 17:13:02 -07002922 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002923 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002924 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002925 }
2926
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002927 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002928 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002929 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002930 }
2931
Winson Chungbb185bd2011-11-21 12:31:42 -08002932 private void invalidatePressedFocusedStates(View container, View button) {
2933 if (container instanceof HolographicLinearLayout) {
2934 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2935 layout.invalidatePressedFocusedStates();
2936 } else if (button instanceof HolographicImageView) {
2937 HolographicImageView view = (HolographicImageView) button;
2938 view.invalidatePressedFocusedStates();
2939 }
2940 }
2941
Winson Chungc51db6a2011-10-05 11:44:49 -07002942 private boolean updateGlobalSearchIcon() {
2943 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002944 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2945 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002946 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002947 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002948 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002949
Winson Chung1cad91e2011-05-25 17:41:01 -07002950 final SearchManager searchManager =
2951 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2952 ComponentName activityName = searchManager.getGlobalSearchActivity();
2953 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002954 int coi = getCurrentOrientationIndexForGlobalIcons();
2955 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002956 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2957 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2958 if (sGlobalSearchIcon[coi] == null) {
2959 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2960 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2961 TOOLBAR_ICON_METADATA_NAME);
2962 }
2963
Winson Chung649723c2011-07-06 20:41:23 -07002964 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002965 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2966 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002967 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002968 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002969 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002970 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002971 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002972 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2973 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2974 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002975 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002976 if (voiceButtonProxy != null) {
2977 voiceButtonProxy.setVisibility(View.GONE);
2978 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002979 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002980 }
2981 }
2982
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002983 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002984 final View searchButtonContainer = findViewById(R.id.search_button_container);
2985 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002986 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002987 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002988 }
2989
Winson Chungc51db6a2011-10-05 11:44:49 -07002990 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002991 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002992 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002993 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002994 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002995
Winson Chungc51db6a2011-10-05 11:44:49 -07002996 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07002997 final SearchManager searchManager =
2998 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2999 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3000
3001 ComponentName activityName = null;
3002 if (globalSearchActivity != null) {
3003 // Check if the global search activity handles voice search
3004 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3005 intent.setPackage(globalSearchActivity.getPackageName());
3006 activityName = intent.resolveActivity(getPackageManager());
3007 }
3008
3009 if (activityName == null) {
3010 // Fallback: check if an activity other than the global search activity
3011 // resolves this
3012 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3013 activityName = intent.resolveActivity(getPackageManager());
3014 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003015 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003016 int coi = getCurrentOrientationIndexForGlobalIcons();
3017 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003018 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3019 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3020 if (sVoiceSearchIcon[coi] == null) {
3021 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3022 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3023 TOOLBAR_ICON_METADATA_NAME);
3024 }
Winson Chung649723c2011-07-06 20:41:23 -07003025 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003026 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003027 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003028 if (voiceButtonProxy != null) {
3029 voiceButtonProxy.setVisibility(View.VISIBLE);
3030 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003031 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003032 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003033 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003034 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003035 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003036 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003037 if (voiceButtonProxy != null) {
3038 voiceButtonProxy.setVisibility(View.GONE);
3039 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003040 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003041 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003042 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003043
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003044 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003045 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3046 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003047 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003048 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003049 }
3050
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003051 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003052 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003053 */
3054 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003055 final View marketButton = findViewById(R.id.market_button);
3056 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3057 // Find the app market activity by resolving an intent.
3058 // (If multiple app markets are installed, it will return the ResolverActivity.)
3059 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003060 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003061 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003062 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003063 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003064 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3065 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003066 marketButton.setVisibility(View.VISIBLE);
3067 } else {
3068 // We should hide and disable the view so that we don't try and restore the visibility
3069 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3070 marketButton.setVisibility(View.GONE);
3071 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003072 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003073 }
3074
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003075 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003076 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3077 Resources r = getResources();
3078 Drawable marketIconDrawable = d.newDrawable(r);
3079 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3080 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3081 marketIconDrawable.setBounds(0, 0, w, h);
3082
3083 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003084 }
3085
Michael Jurkad7c28052012-04-27 15:43:36 -07003086 @Override
3087 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3088 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3089 final List<CharSequence> text = event.getText();
3090 text.clear();
3091 text.add(getString(R.string.home));
3092 return result;
3093 }
3094
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003095 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003096 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003097 */
3098 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3099 @Override
3100 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003101 closeSystemDialogs();
3102 }
3103 }
3104
3105 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003106 * Receives notifications whenever the appwidgets are reset.
3107 */
3108 private class AppWidgetResetObserver extends ContentObserver {
3109 public AppWidgetResetObserver() {
3110 super(new Handler());
3111 }
3112
3113 @Override
3114 public void onChange(boolean selfChange) {
3115 onAppWidgetReset();
3116 }
3117 }
3118
3119 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003120 * If the activity is currently paused, signal that we need to re-run the loader
3121 * in onResume.
3122 *
3123 * This needs to be called from incoming places where resources might have been loaded
3124 * while we are paused. That is becaues the Configuration might be wrong
3125 * when we're not running, and if it comes back to what it was when we
3126 * were paused, we are not restarted.
3127 *
3128 * Implementation of the method from LauncherModel.Callbacks.
3129 *
3130 * @return true if we are currently paused. The caller might be able to
3131 * skip some work in that case since we will come back again.
3132 */
3133 public boolean setLoadOnResume() {
3134 if (mPaused) {
3135 Log.i(TAG, "setLoadOnResume");
3136 mOnResumeNeedsLoad = true;
3137 return true;
3138 } else {
3139 return false;
3140 }
3141 }
3142
3143 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003147 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003148 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003149 } else {
3150 return SCREEN_COUNT / 2;
3151 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003153
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 /**
3155 * Refreshes the shortcuts shown on the workspace.
3156 *
3157 * Implementation of the method from LauncherModel.Callbacks.
3158 */
3159 public void startBinding() {
3160 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003161
Winson Chungf0c6ae02012-03-21 16:10:31 -07003162 mNewShortcutAnimatePage = -1;
3163 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003164 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 int count = workspace.getChildCount();
3166 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003167 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003168 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3169 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003170 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003171 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003172 if (mHotseat != null) {
3173 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
3175 }
3176
3177 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 * Bind the items start-end from the list.
3179 *
3180 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003183 setLoadOnResume();
3184
Winson Chungf0c6ae02012-03-21 16:10:31 -07003185 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3186 Set<String> newApps = new HashSet<String>();
3187 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3188
3189 Workspace workspace = mWorkspace;
3190 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003192
3193 // Short circuit if we are loading dock items for a configuration which has no dock
3194 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3195 mHotseat == null) {
3196 continue;
3197 }
3198
Joe Onorato9c1289c2009-08-17 11:03:03 -04003199 switch (item.itemType) {
3200 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3201 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003202 ShortcutInfo info = (ShortcutInfo) item;
3203 String uri = info.intent.toUri(0).toString();
3204 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3206 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003207 boolean animateIconUp = false;
3208 synchronized (newApps) {
3209 if (newApps.contains(uri)) {
3210 animateIconUp = newApps.remove(uri);
3211 }
3212 }
3213 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003214 // Prepare the view to be animated up
3215 shortcut.setAlpha(0f);
3216 shortcut.setScaleX(0f);
3217 shortcut.setScaleY(0f);
3218 mNewShortcutAnimatePage = item.screen;
3219 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3220 mNewShortcutAnimateViews.add(shortcut);
3221 }
3222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003224 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003225 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003226 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003227 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003228 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3229 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003231 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003233
Joe Onorato9c1289c2009-08-17 11:03:03 -04003234 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 }
3236
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 /**
3238 * Implementation of the method from LauncherModel.Callbacks.
3239 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003240 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003241 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003242 sFolders.clear();
3243 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245
3246 /**
3247 * Add the views for a widget to the workspace.
3248 *
3249 * Implementation of the method from LauncherModel.Callbacks.
3250 */
3251 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003252 setLoadOnResume();
3253
Daniel Sandler843e8602010-06-07 14:59:01 -04003254 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3255 if (DEBUG_WIDGETS) {
3256 Log.d(TAG, "bindAppWidget: " + item);
3257 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 final Workspace workspace = mWorkspace;
3259
3260 final int appWidgetId = item.appWidgetId;
3261 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003262 if (DEBUG_WIDGETS) {
3263 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3264 }
3265
Joe Onorato9c1289c2009-08-17 11:03:03 -04003266 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3267
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003269 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003270
Winson Chung3d503fb2011-07-13 17:25:49 -07003271 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003273 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3274
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 workspace.requestLayout();
3276
Daniel Sandler843e8602010-06-07 14:59:01 -04003277 if (DEBUG_WIDGETS) {
3278 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3279 + (SystemClock.uptimeMillis()-start) + "ms");
3280 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 }
3282
3283 /**
3284 * Callback saying that there aren't any more items to bind.
3285 *
3286 * Implementation of the method from LauncherModel.Callbacks.
3287 */
3288 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003289 setLoadOnResume();
3290
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 if (mSavedState != null) {
3292 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003293 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 mSavedState = null;
3296 }
3297
3298 if (mSavedInstanceState != null) {
3299 super.onRestoreInstanceState(mSavedInstanceState);
3300 mSavedInstanceState = null;
3301 }
3302
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003303 // If we received the result of any pending adds while the loader was running (e.g. the
3304 // widget configuration forced an orientation change), process them now.
3305 for (int i = 0; i < sPendingAddList.size(); i++) {
3306 completeAdd(sPendingAddList.get(i));
3307 }
3308 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003309
Winson Chungc51db6a2011-10-05 11:44:49 -07003310 // Update the market app icon as necessary (the other icons will be managed in response to
3311 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003312 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003313
Winson Chungf0c6ae02012-03-21 16:10:31 -07003314 // Animate up any icons as necessary
3315 if (mVisible || mWorkspaceLoading) {
3316 Runnable newAppsRunnable = new Runnable() {
3317 @Override
3318 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003319 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003320 }
3321 };
Winson Chunga2413752012-04-03 14:22:34 -07003322
3323 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3324 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3325 if (canRunNewAppsAnimation()) {
3326 // If the user has not interacted recently, then either snap to the new page to show
3327 // the new-apps animation or just run them if they are to appear on the current page
3328 if (willSnapPage) {
3329 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3330 } else {
3331 runNewAppsAnimation(false);
3332 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003333 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003334 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003335 // are on another page (or just normally if they are added to the current page)
3336 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003337 }
3338 }
3339
3340 mWorkspaceLoading = false;
3341 }
3342
Winson Chunga2413752012-04-03 14:22:34 -07003343 private boolean canRunNewAppsAnimation() {
3344 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3345 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3346 }
3347
Winson Chungf0c6ae02012-03-21 16:10:31 -07003348 /**
3349 * Runs a new animation that scales up icons that were added while Launcher was in the
3350 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003351 *
3352 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003353 */
Winson Chunga2413752012-04-03 14:22:34 -07003354 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003355 AnimatorSet anim = new AnimatorSet();
3356 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003357
3358 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003359 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3360 @Override
3361 public int compare(View a, View b) {
3362 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3363 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3364 int cellCountX = LauncherModel.getCellCountX();
3365 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3366 }
3367 });
Winson Chunga2413752012-04-03 14:22:34 -07003368
3369 // Animate each of the views in place (or show them immediately if requested)
3370 if (immediate) {
3371 for (View v : mNewShortcutAnimateViews) {
3372 v.setAlpha(1f);
3373 v.setScaleX(1f);
3374 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003375 }
Winson Chunga2413752012-04-03 14:22:34 -07003376 } else {
3377 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3378 View v = mNewShortcutAnimateViews.get(i);
3379 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3380 PropertyValuesHolder.ofFloat("alpha", 1f),
3381 PropertyValuesHolder.ofFloat("scaleX", 1f),
3382 PropertyValuesHolder.ofFloat("scaleY", 1f));
3383 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3384 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3385 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3386 bounceAnims.add(bounceAnim);
3387 }
3388 anim.playTogether(bounceAnims);
3389 anim.addListener(new AnimatorListenerAdapter() {
3390 @Override
3391 public void onAnimationEnd(Animator animation) {
3392 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3393 }
3394 });
3395 anim.start();
3396 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003397
3398 // Clean up
3399 mNewShortcutAnimatePage = -1;
3400 mNewShortcutAnimateViews.clear();
3401 new Thread("clearNewAppsThread") {
3402 public void run() {
3403 mSharedPrefs.edit()
3404 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3405 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3406 .commit();
3407 }
3408 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003409 }
3410
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003411 @Override
3412 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003413 boolean searchVisible = updateGlobalSearchIcon();
3414 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3415 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003416 }
3417
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003418 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 * Add the icons for all apps.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003423 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3424 // Remove the progress bar entirely; we could also make it GONE
3425 // but better to remove it since we know it's not going to be used
3426 View progressBar = mAppsCustomizeTabHost.
3427 findViewById(R.id.apps_customize_progress_bar);
3428 if (progressBar != null) {
3429 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003430 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003431 // We just post the call to setApps so the user sees the progress bar
3432 // disappear-- otherwise, it just looks like the progress bar froze
3433 // which doesn't look great
3434 mAppsCustomizeTabHost.post(new Runnable() {
3435 public void run() {
3436 if (mAppsCustomizeContent != null) {
3437 mAppsCustomizeContent.setApps(apps);
3438 }
3439 }
3440 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
3442
3443 /**
3444 * A package was installed.
3445 *
3446 * Implementation of the method from LauncherModel.Callbacks.
3447 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003448 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003449 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003450
Winson Chung785d2eb2011-04-14 16:08:02 -07003451 if (mAppsCustomizeContent != null) {
3452 mAppsCustomizeContent.addApps(apps);
3453 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 }
3455
3456 /**
3457 * A package was updated.
3458 *
3459 * Implementation of the method from LauncherModel.Callbacks.
3460 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003461 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003462 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003463 if (mWorkspace != null) {
3464 mWorkspace.updateShortcuts(apps);
3465 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003466
Winson Chung785d2eb2011-04-14 16:08:02 -07003467 if (mAppsCustomizeContent != null) {
3468 mAppsCustomizeContent.updateApps(apps);
3469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003470 }
3471
3472 /**
3473 * A package was uninstalled.
3474 *
3475 * Implementation of the method from LauncherModel.Callbacks.
3476 */
Winson Chungcd810732012-06-18 16:45:43 -07003477 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003478 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003479 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003480 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003481
Winson Chung785d2eb2011-04-14 16:08:02 -07003482 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003483 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003484 }
Winson Chunga1820962011-10-03 16:31:06 -07003485
3486 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003487 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 }
Joe Onoratobe386092009-11-17 17:32:16 -08003489
3490 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003491 * A number of packages were updated.
3492 */
3493 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003494 if (mAppsCustomizeContent != null) {
3495 mAppsCustomizeContent.onPackagesUpdated();
3496 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003497 }
3498
Winson Chung400438b2011-01-16 17:53:48 -08003499 private int mapConfigurationOriActivityInfoOri(int configOri) {
3500 final Display d = getWindowManager().getDefaultDisplay();
3501 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3502 switch (d.getRotation()) {
3503 case Surface.ROTATION_0:
3504 case Surface.ROTATION_180:
3505 // We are currently in the same basic orientation as the natural orientation
3506 naturalOri = configOri;
3507 break;
3508 case Surface.ROTATION_90:
3509 case Surface.ROTATION_270:
3510 // We are currently in the other basic orientation to the natural orientation
3511 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3512 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3513 break;
3514 }
3515
3516 int[] oriMap = {
3517 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3518 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3519 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3520 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3521 };
3522 // Since the map starts at portrait, we need to offset if this device's natural orientation
3523 // is landscape.
3524 int indexOffset = 0;
3525 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3526 indexOffset = 1;
3527 }
3528 return oriMap[(d.getRotation() + indexOffset) % 4];
3529 }
Adam Cohen446e9402011-09-15 18:21:21 -07003530
Winson Chung4b919f82012-05-01 10:44:08 -07003531 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003532 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003533 boolean enableRotation = forceEnableRotation ||
3534 getResources().getBoolean(R.bool.allow_rotation);
3535 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003536 }
Winson Chung4b919f82012-05-01 10:44:08 -07003537 public void lockScreenOrientation() {
3538 if (isRotationEnabled()) {
3539 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3540 .getConfiguration().orientation));
3541 }
3542 }
3543 public void unlockScreenOrientation(boolean immediate) {
3544 if (isRotationEnabled()) {
3545 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003546 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003547 } else {
3548 mHandler.postDelayed(new Runnable() {
3549 public void run() {
3550 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3551 }
3552 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003553 }
Winson Chung4b919f82012-05-01 10:44:08 -07003554 }
Winson Chung400438b2011-01-16 17:53:48 -08003555 }
3556
Winson Chung82f55532011-08-09 14:14:23 -07003557 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003558 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003559 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003560 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003561
Winson Chung7d7541e2011-09-16 20:14:36 -07003562 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003563 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003564 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3565 Cling cling = (Cling) findViewById(clingId);
3566 if (cling != null) {
3567 cling.init(this, positionData);
3568 cling.setVisibility(View.VISIBLE);
3569 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3570 if (animate) {
3571 cling.buildLayer();
3572 cling.setAlpha(0f);
3573 cling.animate()
3574 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003575 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003576 .setDuration(SHOW_CLING_DURATION)
3577 .setStartDelay(delay)
3578 .start();
3579 } else {
3580 cling.setAlpha(1f);
3581 }
3582 }
3583 return cling;
3584 }
3585 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003586 if (cling != null) {
3587 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003588 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003589 anim.addListener(new AnimatorListenerAdapter() {
3590 public void onAnimationEnd(Animator animation) {
3591 cling.setVisibility(View.GONE);
3592 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003593 // We should update the shared preferences on a background thread
3594 new Thread("dismissClingThread") {
3595 public void run() {
3596 SharedPreferences.Editor editor = mSharedPrefs.edit();
3597 editor.putBoolean(flag, true);
3598 editor.commit();
3599 }
3600 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003601 };
3602 });
3603 anim.start();
3604 }
3605 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003606 private void removeCling(int id) {
3607 final View cling = findViewById(id);
3608 if (cling != null) {
3609 final ViewGroup parent = (ViewGroup) cling.getParent();
3610 parent.post(new Runnable() {
3611 @Override
3612 public void run() {
3613 parent.removeView(cling);
3614 }
3615 });
3616 }
3617 }
Michael Jurka974c3862012-05-22 22:00:31 -07003618
3619 private boolean skipCustomClingIfNoAccounts() {
3620 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3621 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3622 if (customCling) {
3623 AccountManager am = AccountManager.get(this);
3624 Account[] accounts = am.getAccountsByType("com.google");
3625 return accounts.length == 0;
3626 }
3627 return false;
3628 }
3629
Winson Chung7d7541e2011-09-16 20:14:36 -07003630 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003631 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003632 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003633 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3634 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003635 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003636 } else {
3637 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003638 }
3639 }
3640 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003641 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003642 if (isClingsEnabled() &&
3643 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003644 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003645 } else {
3646 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003647 }
3648 }
3649 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003650 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003651 if (isClingsEnabled() &&
3652 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3653 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003654 } else {
3655 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003656 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003657 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003658 }
3659 public boolean isFolderClingVisible() {
3660 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003661 if (cling != null) {
3662 return cling.getVisibility() == View.VISIBLE;
3663 }
3664 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003665 }
Winson Chung82f55532011-08-09 14:14:23 -07003666 public void dismissWorkspaceCling(View v) {
3667 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003668 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003669 }
3670 public void dismissAllAppsCling(View v) {
3671 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003672 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3673 }
3674 public void dismissFolderCling(View v) {
3675 Cling cling = (Cling) findViewById(R.id.folder_cling);
3676 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003677 }
3678
Winson Chung80baf5a2010-08-09 16:03:15 -07003679 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003680 * Prints out out state for debugging.
3681 */
3682 public void dumpState() {
3683 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003684 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003685 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3686 Log.d(TAG, "mRestoring=" + mRestoring);
3687 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3688 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003689 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003690 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003691
Winson Chung785d2eb2011-04-14 16:08:02 -07003692 if (mAppsCustomizeContent != null) {
3693 mAppsCustomizeContent.dumpState();
3694 }
Joe Onoratobe386092009-11-17 17:32:16 -08003695 Log.d(TAG, "END launcher2 dump state");
3696 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003697
3698 @Override
3699 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3700 super.dump(prefix, fd, writer, args);
3701 writer.println(" ");
3702 writer.println("Debug logs: ");
3703 for (int i = 0; i < sDumpLogs.size(); i++) {
3704 writer.println(" " + sDumpLogs.get(i));
3705 }
3706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003707}
Michael Jurka2763be32011-02-24 11:19:57 -08003708
Michael Jurkaabded662011-03-04 12:06:57 -08003709interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003710 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003711 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003712 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003713 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003714 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003715}