blob: 415ccacfb3848f153cb75fbe3047a2aba8970dd8 [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,
119 AllAppsView.Watcher, 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
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002264 // AllAppsView.Watcher
2265 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002266 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002267 mWorkspace.setVisibility(View.GONE);
2268 }
2269 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002270
2271 /**
2272 * Helper method for the cameraZoomIn/cameraZoomOut animations
2273 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002274 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002275 * @param scaleFactor The scale factor used for the zoom
2276 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002277 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002278 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002279 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002280 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002281
Winson Chung18f41f82012-05-09 13:28:10 -07002282 void disableWallpaperIfInAllApps() {
2283 // Only disable it if we are in all apps
2284 if (mState == State.APPS_CUSTOMIZE) {
2285 if (mAppsCustomizeTabHost != null &&
2286 !mAppsCustomizeTabHost.isTransitioning()) {
2287 updateWallpaperVisibility(false);
2288 }
2289 }
2290 }
2291
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002292 void updateWallpaperVisibility(boolean visible) {
2293 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2294 int curflags = getWindow().getAttributes().flags
2295 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2296 if (wpflags != curflags) {
2297 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2298 }
2299 }
2300
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002301 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2302 if (v instanceof LauncherTransitionable) {
2303 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2304 }
2305 }
2306
Michael Jurkabed61d22012-02-14 22:51:29 -08002307 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2308 if (v instanceof LauncherTransitionable) {
2309 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2310 }
Winson Chung70442722012-02-10 15:43:22 -08002311
2312 // Update the workspace transition step as well
2313 dispatchOnLauncherTransitionStep(v, 0f);
2314 }
2315
2316 private void dispatchOnLauncherTransitionStep(View v, float t) {
2317 if (v instanceof LauncherTransitionable) {
2318 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2319 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002320 }
2321
2322 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2323 if (v instanceof LauncherTransitionable) {
2324 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2325 }
Winson Chung70442722012-02-10 15:43:22 -08002326
2327 // Update the workspace transition step as well
2328 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002329 }
2330
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002332 * Things to test when changing the following seven functions.
2333 * - Home from workspace
2334 * - from center screen
2335 * - from other screens
2336 * - Home from all apps
2337 * - from center screen
2338 * - from other screens
2339 * - Back from all apps
2340 * - from center screen
2341 * - from other screens
2342 * - Launch app from workspace and quit
2343 * - with back
2344 * - with home
2345 * - Launch app from all apps and quit
2346 * - with back
2347 * - with home
2348 * - Go to a screen that's not the default, then all
2349 * apps, and launch and app, and go back
2350 * - with back
2351 * -with home
2352 * - On workspace, long press power and go back
2353 * - with back
2354 * - with home
2355 * - On all apps, long press power and go back
2356 * - with back
2357 * - with home
2358 * - On workspace, power off
2359 * - On all apps, power off
2360 * - Launch an app and turn off the screen while in that app
2361 * - Go back with home key
2362 * - Go back with back key TODO: make this not go to workspace
2363 * - From all apps
2364 * - From workspace
2365 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2366 * - From all apps
2367 * - From the center workspace
2368 * - From another workspace
2369 */
2370
2371 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002372 * Zoom the camera out from the workspace to reveal 'toView'.
2373 * Assumes that the view to show is anchored at either the very top or very bottom
2374 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002375 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002376 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002377 if (mStateAnimation != null) {
2378 mStateAnimation.cancel();
2379 mStateAnimation = null;
2380 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002381 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002382
Winson Chungf0ea4d32011-06-06 14:27:16 -07002383 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2384 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2385 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002386 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002387 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002388 final int startDelay =
2389 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002390
Michael Jurkab3e22d92011-10-31 15:58:33 -07002391 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002392
Michael Jurkad74c9842011-07-10 12:44:21 -07002393 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002394 Animator workspaceAnim =
2395 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002396
2397 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002398 toView.setScaleX(scale);
2399 toView.setScaleY(scale);
2400 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2401 scaleAnim.
2402 scaleX(1f).scaleY(1f).
2403 setDuration(duration).
2404 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002405
Winson Chungf0ea4d32011-06-06 14:27:16 -07002406 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002407 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002408 final ObjectAnimator alphaAnim = ObjectAnimator
2409 .ofFloat(toView, "alpha", 0f, 1f)
2410 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002411 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002412 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2413 @Override
2414 public void onAnimationUpdate(ValueAnimator animation) {
2415 float t = (Float) animation.getAnimatedValue();
2416 dispatchOnLauncherTransitionStep(fromView, t);
2417 dispatchOnLauncherTransitionStep(toView, t);
2418 }
2419 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002420
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002421 // toView should appear right at the end of the workspace shrink
2422 // animation
2423 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002424 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002425 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002426
2427 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002428 boolean animationCancelled = false;
2429
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002430 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002431 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002432 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433 // Prepare the position
2434 toView.setTranslationX(0.0f);
2435 toView.setTranslationY(0.0f);
2436 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002437 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002438 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002439 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002440 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002441 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2442 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002443
2444 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2445 // Hide the workspace scrollbar
2446 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002447 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002448 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002449 if (!animationCancelled) {
2450 updateWallpaperVisibility(false);
2451 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002452
2453 // Hide the search bar
2454 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002455 }
2456
Adam Cohencff6af82011-09-13 14:51:53 -07002457 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002458 public void onAnimationCancel(Animator animation) {
2459 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002460 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002461 });
2462
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002463 if (workspaceAnim != null) {
2464 mStateAnimation.play(workspaceAnim);
2465 }
Michael Jurka1899a362011-11-03 13:50:45 -07002466
2467 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002468 final ViewTreeObserver observer;
2469
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002470 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2471 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002472
2473 // If any of the objects being animated haven't been measured/laid out
2474 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002475 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002476 (mWorkspace.getMeasuredWidth() == 0) ||
2477 (toView.getMeasuredWidth() == 0)) {
2478 observer = mWorkspace.getViewTreeObserver();
2479 delayAnim = true;
2480 } else {
2481 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002482 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002483
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002484 final AnimatorSet stateAnimation = mStateAnimation;
2485 final Runnable startAnimRunnable = new Runnable() {
2486 public void run() {
2487 // Check that mStateAnimation hasn't changed while
2488 // we waited for a layout/draw pass
2489 if (mStateAnimation != stateAnimation)
2490 return;
2491 setPivotsForZoom(toView, scale);
2492 dispatchOnLauncherTransitionStart(fromView, animated, false);
2493 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002494 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002495 public void run() {
2496 // Check that mStateAnimation hasn't changed while
2497 // we waited for a layout/draw pass
2498 if (mStateAnimation != stateAnimation)
2499 return;
2500 mStateAnimation.start();
2501 }
2502 });
2503 }
2504 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 if (delayAnim) {
2506 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2507 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002508 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002509 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002510 }
2511 };
2512 observer.addOnGlobalLayoutListener(delayedStart);
2513 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002514 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002515 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002516 } else {
2517 toView.setTranslationX(0.0f);
2518 toView.setTranslationY(0.0f);
2519 toView.setScaleX(1.0f);
2520 toView.setScaleY(1.0f);
2521 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002522 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002523
Michael Jurkabed61d22012-02-14 22:51:29 -08002524 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2525 // Hide the workspace scrollbar
2526 mWorkspace.hideScrollingIndicator(true);
2527 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002528
2529 // Hide the search bar
2530 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002531 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002532 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002533 dispatchOnLauncherTransitionStart(fromView, animated, false);
2534 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002535 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 dispatchOnLauncherTransitionStart(toView, animated, false);
2537 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002538 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002540 }
2541
2542 /**
2543 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002544 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002545 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002546 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002547 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2548 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002549
Michael Jurkab3e22d92011-10-31 15:58:33 -07002550 if (mStateAnimation != null) {
2551 mStateAnimation.cancel();
2552 mStateAnimation = null;
2553 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002554 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002555
Winson Chungf0ea4d32011-06-06 14:27:16 -07002556 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002557 final int fadeOutDuration =
2558 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002559 final float scaleFactor = (float)
2560 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2561 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002562 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002563 Animator workspaceAnim = null;
2564
2565 if (toState == State.WORKSPACE) {
2566 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2567 workspaceAnim = mWorkspace.getChangeStateAnimation(
2568 Workspace.State.NORMAL, animated, stagger);
2569 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2570 workspaceAnim = mWorkspace.getChangeStateAnimation(
2571 Workspace.State.SPRING_LOADED, animated);
2572 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002573
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002575 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002576 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002578 final LauncherViewPropertyAnimator scaleAnim =
2579 new LauncherViewPropertyAnimator(fromView);
2580 scaleAnim.
2581 scaleX(scaleFactor).scaleY(scaleFactor).
2582 setDuration(duration).
2583 setInterpolator(new Workspace.ZoomInInterpolator());
2584
2585 final ObjectAnimator alphaAnim = ObjectAnimator
2586 .ofFloat(fromView, "alpha", 1f, 0f)
2587 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002588 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002589 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2590 @Override
2591 public void onAnimationUpdate(ValueAnimator animation) {
2592 float t = 1f - (Float) animation.getAnimatedValue();
2593 dispatchOnLauncherTransitionStep(fromView, t);
2594 dispatchOnLauncherTransitionStep(toView, t);
2595 }
2596 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002597
Michael Jurkabed61d22012-02-14 22:51:29 -08002598 mStateAnimation = new AnimatorSet();
2599
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002600 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2601 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002602
2603 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002604 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002605 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002606 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002608 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2609 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002610 if (mWorkspace != null) {
2611 mWorkspace.hideScrollingIndicator(false);
2612 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002613 if (onCompleteRunnable != null) {
2614 onCompleteRunnable.run();
2615 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 }
2617 });
2618
Winson Chungf0ea4d32011-06-06 14:27:16 -07002619 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002620 if (workspaceAnim != null) {
2621 mStateAnimation.play(workspaceAnim);
2622 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002623 dispatchOnLauncherTransitionStart(fromView, animated, true);
2624 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002625 final Animator stateAnimation = mStateAnimation;
2626 mWorkspace.post(new Runnable() {
2627 public void run() {
2628 if (stateAnimation != mStateAnimation)
2629 return;
2630 mStateAnimation.start();
2631 }
2632 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633 } else {
2634 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002635 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002636 dispatchOnLauncherTransitionStart(fromView, animated, true);
2637 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002638 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002639 dispatchOnLauncherTransitionStart(toView, animated, true);
2640 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002641 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002643 }
2644
Michael Jurkae326f182011-11-21 14:05:46 -08002645 @Override
2646 public void onTrimMemory(int level) {
2647 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002648 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002649 mAppsCustomizeTabHost.onTrimMemory();
2650 }
2651 }
2652
Winson Chung18f41f82012-05-09 13:28:10 -07002653 @Override
2654 public void onWindowFocusChanged(boolean hasFocus) {
2655 if (!hasFocus) {
2656 // When another window occludes launcher (like the notification shade, or recents),
2657 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2658 updateWallpaperVisibility(true);
2659 } else {
2660 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002661 mWorkspace.postDelayed(new Runnable() {
2662 @Override
2663 public void run() {
2664 disableWallpaperIfInAllApps();
2665 }
2666 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002667 }
2668 }
2669
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002670 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002671 showWorkspace(animated, null);
2672 }
2673
2674 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002675 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002676 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002677 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002678 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002679
Winson Chungc7d2b602012-05-16 17:02:20 -07002680 // Show the search bar (only animate if we were showing the drop target bar in spring
2681 // loaded mode)
2682 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2683
Michael Jurkab737ee62011-11-15 15:57:22 -08002684 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002685 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002686
2687 // Set focus to the AppsCustomize button
2688 if (mAllAppsButton != null) {
2689 mAllAppsButton.requestFocus();
2690 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002691 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002692
Michael Jurkab737ee62011-11-15 15:57:22 -08002693 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002694
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002695 // Change the state *after* we've called all the transition code
2696 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002697
Winson Chung5fb63472011-02-02 17:03:37 -08002698 // Resume the auto-advance of widgets
2699 mUserPresent = true;
2700 updateRunning();
2701
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002702 // send an accessibility event to announce the context change
2703 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002704 }
2705
Michael Jurkab3e22d92011-10-31 15:58:33 -07002706 void showAllApps(boolean animated) {
2707 if (mState != State.WORKSPACE) return;
2708
2709 showAppsCustomizeHelper(animated, false);
2710 mAppsCustomizeTabHost.requestFocus();
2711
Michael Jurkab3e22d92011-10-31 15:58:33 -07002712 // Change the state *after* we've called all the transition code
2713 mState = State.APPS_CUSTOMIZE;
2714
2715 // Pause the auto-advance of widgets until we are out of AllApps
2716 mUserPresent = false;
2717 updateRunning();
2718 closeFolder();
2719
2720 // Send an accessibility event to announce the context change
2721 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2722 }
2723
Adam Cohen7777d962011-08-18 18:58:38 -07002724 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002725 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002726 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002727 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002728 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002729 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002730 }
Adam Cohen7777d962011-08-18 18:58:38 -07002731
Adam Cohened66b2b2012-01-23 17:28:51 -08002732 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2733 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002734 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2735
Winson Chunge7a03942011-08-05 15:05:12 -07002736 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002737 @Override
2738 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002739 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002740 // Before we show workspace, hide all apps again because
2741 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2742 // clean up our state transition functions
2743 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002744 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002745 } else {
2746 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002747 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002748 }
2749 }, (extendedDelay ?
2750 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2751 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2752 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002753
Michael Jurkad3ef3062010-11-23 16:23:58 -08002754 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002755 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002756 final boolean animated = true;
2757 final boolean springLoaded = true;
2758 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002759 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002760 }
2761 // Otherwise, we are not in spring loaded mode, so don't do anything.
2762 }
2763
Michael Jurkab737ee62011-11-15 15:57:22 -08002764 void hideDockDivider() {
2765 if (mQsbDivider != null && mDockDivider != null) {
2766 mQsbDivider.setVisibility(View.INVISIBLE);
2767 mDockDivider.setVisibility(View.INVISIBLE);
2768 }
2769 }
2770
2771 void showDockDivider(boolean animated) {
2772 if (mQsbDivider != null && mDockDivider != null) {
2773 mQsbDivider.setVisibility(View.VISIBLE);
2774 mDockDivider.setVisibility(View.VISIBLE);
2775 if (mDividerAnimator != null) {
2776 mDividerAnimator.cancel();
2777 mQsbDivider.setAlpha(1f);
2778 mDockDivider.setAlpha(1f);
2779 mDividerAnimator = null;
2780 }
2781 if (animated) {
2782 mDividerAnimator = new AnimatorSet();
2783 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2784 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2785 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2786 mDividerAnimator.start();
2787 }
2788 }
2789 }
2790
Michael Jurkab3e22d92011-10-31 15:58:33 -07002791 void lockAllApps() {
2792 // TODO
2793 }
2794
2795 void unlockAllApps() {
2796 // TODO
2797 }
2798
Adam Cohenfc53cd22011-07-20 15:45:11 -07002799 public boolean isAllAppsCustomizeOpen() {
2800 return mState == State.APPS_CUSTOMIZE;
2801 }
2802
Winson Chungf0ea4d32011-06-06 14:27:16 -07002803 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002804 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002805 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002806 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002807 if (!LauncherApplication.isScreenLarge()) {
2808 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002809 if (mHotseat.getAlpha() != 1f) {
2810 int duration = mSearchDropTargetBar.getTransitionInDuration();
2811 mHotseat.animate().alpha(1f).setDuration(duration);
2812 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002813 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002814 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002815 }
2816 }
2817 }
2818
2819 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002820 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002821 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002823 if (!LauncherApplication.isScreenLarge()) {
2824 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002825 if (mHotseat.getAlpha() != 0f) {
2826 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2827 mHotseat.animate().alpha(0f).setDuration(duration);
2828 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002829 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002830 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002831 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002832 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002833 }
2834
Patrick Dubroy5f445422011-02-18 14:35:21 -08002835 /**
2836 * Add an item from all apps or customize onto the given workspace screen.
2837 * If layout is null, add to the current screen.
2838 */
2839 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002840 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002841 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002842 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002843 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002844
Winson Chungdff8ebb2011-09-08 17:25:31 -07002845 /** Maps the current orientation to an index for referencing orientation correct global icons */
2846 private int getCurrentOrientationIndexForGlobalIcons() {
2847 // default - 0, landscape - 1
2848 switch (getResources().getConfiguration().orientation) {
2849 case Configuration.ORIENTATION_LANDSCAPE:
2850 return 1;
2851 default:
2852 return 0;
2853 }
2854 }
2855
Michael Jurkaae65ee32012-04-30 11:45:54 -07002856 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002857 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002858 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002859 // Look for the toolbar icon specified in the activity meta-data
2860 Bundle metaData = packageManager.getActivityInfo(
2861 activityName, PackageManager.GET_META_DATA).metaData;
2862 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002863 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002864 if (iconResId != 0) {
2865 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002866 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002867 }
2868 }
2869 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002870 // This can happen if the activity defines an invalid drawable
2871 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2872 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002873 } catch (Resources.NotFoundException nfe) {
2874 // This can happen if the activity defines an invalid drawable
2875 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2876 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002877 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002878 return null;
2879 }
2880
2881 // if successful in getting icon, return it; otherwise, set button to use default drawable
2882 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002883 int buttonId, ComponentName activityName, int fallbackDrawableId,
2884 String toolbarResourceName) {
2885 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002886 Resources r = getResources();
2887 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2888 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002889
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002890 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002891 // If we were unable to find the icon via the meta-data, use a generic one
2892 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002893 toolbarIcon = r.getDrawable(fallbackDrawableId);
2894 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002895 if (button != null) {
2896 button.setCompoundDrawables(toolbarIcon, null, null, null);
2897 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002898 return null;
2899 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002900 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002901 if (button != null) {
2902 button.setCompoundDrawables(toolbarIcon, null, null, null);
2903 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002904 return toolbarIcon.getConstantState();
2905 }
2906 }
2907
2908 // if successful in getting icon, return it; otherwise, set button to use default drawable
2909 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002910 int buttonId, ComponentName activityName, int fallbackDrawableId,
2911 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002912 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002913 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002914
Michael Jurka19e0fc52011-07-22 18:00:21 -07002915 if (button != null) {
2916 // If we were unable to find the icon via the meta-data, use a
2917 // generic one
2918 if (toolbarIcon == null) {
2919 button.setImageResource(fallbackDrawableId);
2920 } else {
2921 button.setImageDrawable(toolbarIcon);
2922 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002923 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002924
2925 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2926
Michael Jurka0423dcf2010-10-05 14:56:18 -07002927 }
2928
Winson Chung1b884092012-06-08 17:13:02 -07002929 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002930 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002931 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002932 }
2933
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002934 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002935 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002936 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002937 }
2938
Winson Chungbb185bd2011-11-21 12:31:42 -08002939 private void invalidatePressedFocusedStates(View container, View button) {
2940 if (container instanceof HolographicLinearLayout) {
2941 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2942 layout.invalidatePressedFocusedStates();
2943 } else if (button instanceof HolographicImageView) {
2944 HolographicImageView view = (HolographicImageView) button;
2945 view.invalidatePressedFocusedStates();
2946 }
2947 }
2948
Winson Chungc51db6a2011-10-05 11:44:49 -07002949 private boolean updateGlobalSearchIcon() {
2950 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002951 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2952 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002953 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002954 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002955 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002956
Winson Chung1cad91e2011-05-25 17:41:01 -07002957 final SearchManager searchManager =
2958 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2959 ComponentName activityName = searchManager.getGlobalSearchActivity();
2960 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002961 int coi = getCurrentOrientationIndexForGlobalIcons();
2962 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002963 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2964 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2965 if (sGlobalSearchIcon[coi] == null) {
2966 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2967 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2968 TOOLBAR_ICON_METADATA_NAME);
2969 }
2970
Winson Chung649723c2011-07-06 20:41:23 -07002971 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002972 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2973 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002974 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002975 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002976 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002977 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002978 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002979 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2980 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2981 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002982 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07002983 if (voiceButtonProxy != null) {
2984 voiceButtonProxy.setVisibility(View.GONE);
2985 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002986 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002987 }
2988 }
2989
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002990 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002991 final View searchButtonContainer = findViewById(R.id.search_button_container);
2992 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002993 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002994 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002995 }
2996
Winson Chungc51db6a2011-10-05 11:44:49 -07002997 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002998 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002999 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003000 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003001 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003002
Winson Chungc51db6a2011-10-05 11:44:49 -07003003 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003004 final SearchManager searchManager =
3005 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3006 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3007
3008 ComponentName activityName = null;
3009 if (globalSearchActivity != null) {
3010 // Check if the global search activity handles voice search
3011 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3012 intent.setPackage(globalSearchActivity.getPackageName());
3013 activityName = intent.resolveActivity(getPackageManager());
3014 }
3015
3016 if (activityName == null) {
3017 // Fallback: check if an activity other than the global search activity
3018 // resolves this
3019 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3020 activityName = intent.resolveActivity(getPackageManager());
3021 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003022 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003023 int coi = getCurrentOrientationIndexForGlobalIcons();
3024 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003025 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3026 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3027 if (sVoiceSearchIcon[coi] == null) {
3028 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3029 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3030 TOOLBAR_ICON_METADATA_NAME);
3031 }
Winson Chung649723c2011-07-06 20:41:23 -07003032 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003033 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003034 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003035 if (voiceButtonProxy != null) {
3036 voiceButtonProxy.setVisibility(View.VISIBLE);
3037 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003038 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003039 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003040 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003041 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003042 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003043 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003044 if (voiceButtonProxy != null) {
3045 voiceButtonProxy.setVisibility(View.GONE);
3046 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003047 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003048 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003049 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003050
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003051 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003052 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3053 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003054 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003055 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003056 }
3057
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003058 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003059 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003060 */
3061 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003062 final View marketButton = findViewById(R.id.market_button);
3063 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3064 // Find the app market activity by resolving an intent.
3065 // (If multiple app markets are installed, it will return the ResolverActivity.)
3066 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003067 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003068 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003070 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003071 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3072 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003073 marketButton.setVisibility(View.VISIBLE);
3074 } else {
3075 // We should hide and disable the view so that we don't try and restore the visibility
3076 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3077 marketButton.setVisibility(View.GONE);
3078 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003079 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003080 }
3081
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003082 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003083 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3084 Resources r = getResources();
3085 Drawable marketIconDrawable = d.newDrawable(r);
3086 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3087 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3088 marketIconDrawable.setBounds(0, 0, w, h);
3089
3090 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003091 }
3092
Michael Jurkad7c28052012-04-27 15:43:36 -07003093 @Override
3094 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3095 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3096 final List<CharSequence> text = event.getText();
3097 text.clear();
3098 text.add(getString(R.string.home));
3099 return result;
3100 }
3101
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003102 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003103 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003104 */
3105 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3106 @Override
3107 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003108 closeSystemDialogs();
3109 }
3110 }
3111
3112 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003113 * Receives notifications whenever the appwidgets are reset.
3114 */
3115 private class AppWidgetResetObserver extends ContentObserver {
3116 public AppWidgetResetObserver() {
3117 super(new Handler());
3118 }
3119
3120 @Override
3121 public void onChange(boolean selfChange) {
3122 onAppWidgetReset();
3123 }
3124 }
3125
3126 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003127 * If the activity is currently paused, signal that we need to re-run the loader
3128 * in onResume.
3129 *
3130 * This needs to be called from incoming places where resources might have been loaded
3131 * while we are paused. That is becaues the Configuration might be wrong
3132 * when we're not running, and if it comes back to what it was when we
3133 * were paused, we are not restarted.
3134 *
3135 * Implementation of the method from LauncherModel.Callbacks.
3136 *
3137 * @return true if we are currently paused. The caller might be able to
3138 * skip some work in that case since we will come back again.
3139 */
3140 public boolean setLoadOnResume() {
3141 if (mPaused) {
3142 Log.i(TAG, "setLoadOnResume");
3143 mOnResumeNeedsLoad = true;
3144 return true;
3145 } else {
3146 return false;
3147 }
3148 }
3149
3150 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003154 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003155 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003156 } else {
3157 return SCREEN_COUNT / 2;
3158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003160
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 /**
3162 * Refreshes the shortcuts shown on the workspace.
3163 *
3164 * Implementation of the method from LauncherModel.Callbacks.
3165 */
3166 public void startBinding() {
3167 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003168
Winson Chungf0c6ae02012-03-21 16:10:31 -07003169 mNewShortcutAnimatePage = -1;
3170 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003171 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 int count = workspace.getChildCount();
3173 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003174 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003175 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3176 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003178 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 if (mHotseat != null) {
3180 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
3182 }
3183
3184 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 * Bind the items start-end from the list.
3186 *
3187 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003190 setLoadOnResume();
3191
Winson Chungf0c6ae02012-03-21 16:10:31 -07003192 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3193 Set<String> newApps = new HashSet<String>();
3194 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3195
3196 Workspace workspace = mWorkspace;
3197 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003199
3200 // Short circuit if we are loading dock items for a configuration which has no dock
3201 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3202 mHotseat == null) {
3203 continue;
3204 }
3205
Joe Onorato9c1289c2009-08-17 11:03:03 -04003206 switch (item.itemType) {
3207 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3208 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003209 ShortcutInfo info = (ShortcutInfo) item;
3210 String uri = info.intent.toUri(0).toString();
3211 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3213 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003214 boolean animateIconUp = false;
3215 synchronized (newApps) {
3216 if (newApps.contains(uri)) {
3217 animateIconUp = newApps.remove(uri);
3218 }
3219 }
3220 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003221 // Prepare the view to be animated up
3222 shortcut.setAlpha(0f);
3223 shortcut.setScaleX(0f);
3224 shortcut.setScaleY(0f);
3225 mNewShortcutAnimatePage = item.screen;
3226 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3227 mNewShortcutAnimateViews.add(shortcut);
3228 }
3229 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003231 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003232 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003233 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003234 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3236 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003240
Joe Onorato9c1289c2009-08-17 11:03:03 -04003241 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 }
3243
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 /**
3245 * Implementation of the method from LauncherModel.Callbacks.
3246 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003247 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003248 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003249 sFolders.clear();
3250 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252
3253 /**
3254 * Add the views for a widget to the workspace.
3255 *
3256 * Implementation of the method from LauncherModel.Callbacks.
3257 */
3258 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003259 setLoadOnResume();
3260
Daniel Sandler843e8602010-06-07 14:59:01 -04003261 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3262 if (DEBUG_WIDGETS) {
3263 Log.d(TAG, "bindAppWidget: " + item);
3264 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003265 final Workspace workspace = mWorkspace;
3266
3267 final int appWidgetId = item.appWidgetId;
3268 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003269 if (DEBUG_WIDGETS) {
3270 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3271 }
3272
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3274
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003276 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277
Winson Chung3d503fb2011-07-13 17:25:49 -07003278 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003279 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003280 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3281
Joe Onorato9c1289c2009-08-17 11:03:03 -04003282 workspace.requestLayout();
3283
Daniel Sandler843e8602010-06-07 14:59:01 -04003284 if (DEBUG_WIDGETS) {
3285 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3286 + (SystemClock.uptimeMillis()-start) + "ms");
3287 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003288 }
3289
3290 /**
3291 * Callback saying that there aren't any more items to bind.
3292 *
3293 * Implementation of the method from LauncherModel.Callbacks.
3294 */
3295 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003296 setLoadOnResume();
3297
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 if (mSavedState != null) {
3299 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003300 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 mSavedState = null;
3303 }
3304
3305 if (mSavedInstanceState != null) {
3306 super.onRestoreInstanceState(mSavedInstanceState);
3307 mSavedInstanceState = null;
3308 }
3309
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003310 // If we received the result of any pending adds while the loader was running (e.g. the
3311 // widget configuration forced an orientation change), process them now.
3312 for (int i = 0; i < sPendingAddList.size(); i++) {
3313 completeAdd(sPendingAddList.get(i));
3314 }
3315 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316
Winson Chungc51db6a2011-10-05 11:44:49 -07003317 // Update the market app icon as necessary (the other icons will be managed in response to
3318 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003319 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003320
Winson Chungf0c6ae02012-03-21 16:10:31 -07003321 // Animate up any icons as necessary
3322 if (mVisible || mWorkspaceLoading) {
3323 Runnable newAppsRunnable = new Runnable() {
3324 @Override
3325 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003326 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003327 }
3328 };
Winson Chunga2413752012-04-03 14:22:34 -07003329
3330 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3331 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3332 if (canRunNewAppsAnimation()) {
3333 // If the user has not interacted recently, then either snap to the new page to show
3334 // the new-apps animation or just run them if they are to appear on the current page
3335 if (willSnapPage) {
3336 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3337 } else {
3338 runNewAppsAnimation(false);
3339 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003340 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003341 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003342 // are on another page (or just normally if they are added to the current page)
3343 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003344 }
3345 }
3346
3347 mWorkspaceLoading = false;
3348 }
3349
Winson Chunga2413752012-04-03 14:22:34 -07003350 private boolean canRunNewAppsAnimation() {
3351 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3352 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3353 }
3354
Winson Chungf0c6ae02012-03-21 16:10:31 -07003355 /**
3356 * Runs a new animation that scales up icons that were added while Launcher was in the
3357 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003358 *
3359 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003360 */
Winson Chunga2413752012-04-03 14:22:34 -07003361 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003362 AnimatorSet anim = new AnimatorSet();
3363 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003364
3365 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003366 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3367 @Override
3368 public int compare(View a, View b) {
3369 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3370 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3371 int cellCountX = LauncherModel.getCellCountX();
3372 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3373 }
3374 });
Winson Chunga2413752012-04-03 14:22:34 -07003375
3376 // Animate each of the views in place (or show them immediately if requested)
3377 if (immediate) {
3378 for (View v : mNewShortcutAnimateViews) {
3379 v.setAlpha(1f);
3380 v.setScaleX(1f);
3381 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003382 }
Winson Chunga2413752012-04-03 14:22:34 -07003383 } else {
3384 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3385 View v = mNewShortcutAnimateViews.get(i);
3386 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3387 PropertyValuesHolder.ofFloat("alpha", 1f),
3388 PropertyValuesHolder.ofFloat("scaleX", 1f),
3389 PropertyValuesHolder.ofFloat("scaleY", 1f));
3390 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3391 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3392 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3393 bounceAnims.add(bounceAnim);
3394 }
3395 anim.playTogether(bounceAnims);
3396 anim.addListener(new AnimatorListenerAdapter() {
3397 @Override
3398 public void onAnimationEnd(Animator animation) {
3399 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3400 }
3401 });
3402 anim.start();
3403 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003404
3405 // Clean up
3406 mNewShortcutAnimatePage = -1;
3407 mNewShortcutAnimateViews.clear();
3408 new Thread("clearNewAppsThread") {
3409 public void run() {
3410 mSharedPrefs.edit()
3411 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3412 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3413 .commit();
3414 }
3415 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003416 }
3417
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003418 @Override
3419 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003420 boolean searchVisible = updateGlobalSearchIcon();
3421 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3422 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003423 }
3424
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003425 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 * Add the icons for all apps.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
3429 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003430 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3431 // Remove the progress bar entirely; we could also make it GONE
3432 // but better to remove it since we know it's not going to be used
3433 View progressBar = mAppsCustomizeTabHost.
3434 findViewById(R.id.apps_customize_progress_bar);
3435 if (progressBar != null) {
3436 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003437 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003438 // We just post the call to setApps so the user sees the progress bar
3439 // disappear-- otherwise, it just looks like the progress bar froze
3440 // which doesn't look great
3441 mAppsCustomizeTabHost.post(new Runnable() {
3442 public void run() {
3443 if (mAppsCustomizeContent != null) {
3444 mAppsCustomizeContent.setApps(apps);
3445 }
3446 }
3447 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003448 }
3449
3450 /**
3451 * A package was installed.
3452 *
3453 * Implementation of the method from LauncherModel.Callbacks.
3454 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003455 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003456 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003457
Winson Chung785d2eb2011-04-14 16:08:02 -07003458 if (mAppsCustomizeContent != null) {
3459 mAppsCustomizeContent.addApps(apps);
3460 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003461 }
3462
3463 /**
3464 * A package was updated.
3465 *
3466 * Implementation of the method from LauncherModel.Callbacks.
3467 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003468 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003469 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003470 if (mWorkspace != null) {
3471 mWorkspace.updateShortcuts(apps);
3472 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003473
Winson Chung785d2eb2011-04-14 16:08:02 -07003474 if (mAppsCustomizeContent != null) {
3475 mAppsCustomizeContent.updateApps(apps);
3476 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 }
3478
3479 /**
3480 * A package was uninstalled.
3481 *
3482 * Implementation of the method from LauncherModel.Callbacks.
3483 */
Joe Onorato36115782010-06-17 13:28:48 -04003484 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003485 if (permanent) {
3486 mWorkspace.removeItems(apps);
3487 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003488
Winson Chung785d2eb2011-04-14 16:08:02 -07003489 if (mAppsCustomizeContent != null) {
3490 mAppsCustomizeContent.removeApps(apps);
3491 }
Winson Chunga1820962011-10-03 16:31:06 -07003492
3493 // Notify the drag controller
3494 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 }
Joe Onoratobe386092009-11-17 17:32:16 -08003496
3497 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003498 * A number of packages were updated.
3499 */
3500 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003501 if (mAppsCustomizeContent != null) {
3502 mAppsCustomizeContent.onPackagesUpdated();
3503 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003504 }
3505
Winson Chung400438b2011-01-16 17:53:48 -08003506 private int mapConfigurationOriActivityInfoOri(int configOri) {
3507 final Display d = getWindowManager().getDefaultDisplay();
3508 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3509 switch (d.getRotation()) {
3510 case Surface.ROTATION_0:
3511 case Surface.ROTATION_180:
3512 // We are currently in the same basic orientation as the natural orientation
3513 naturalOri = configOri;
3514 break;
3515 case Surface.ROTATION_90:
3516 case Surface.ROTATION_270:
3517 // We are currently in the other basic orientation to the natural orientation
3518 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3519 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3520 break;
3521 }
3522
3523 int[] oriMap = {
3524 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3525 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3526 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3527 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3528 };
3529 // Since the map starts at portrait, we need to offset if this device's natural orientation
3530 // is landscape.
3531 int indexOffset = 0;
3532 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3533 indexOffset = 1;
3534 }
3535 return oriMap[(d.getRotation() + indexOffset) % 4];
3536 }
Adam Cohen446e9402011-09-15 18:21:21 -07003537
Winson Chung4b919f82012-05-01 10:44:08 -07003538 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003539 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003540 boolean enableRotation = forceEnableRotation ||
3541 getResources().getBoolean(R.bool.allow_rotation);
3542 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003543 }
Winson Chung4b919f82012-05-01 10:44:08 -07003544 public void lockScreenOrientation() {
3545 if (isRotationEnabled()) {
3546 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3547 .getConfiguration().orientation));
3548 }
3549 }
3550 public void unlockScreenOrientation(boolean immediate) {
3551 if (isRotationEnabled()) {
3552 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003553 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003554 } else {
3555 mHandler.postDelayed(new Runnable() {
3556 public void run() {
3557 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3558 }
3559 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003560 }
Winson Chung4b919f82012-05-01 10:44:08 -07003561 }
Winson Chung400438b2011-01-16 17:53:48 -08003562 }
3563
Winson Chung82f55532011-08-09 14:14:23 -07003564 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003565 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003566 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003567 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003568
Winson Chung7d7541e2011-09-16 20:14:36 -07003569 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003570 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003571 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3572 Cling cling = (Cling) findViewById(clingId);
3573 if (cling != null) {
3574 cling.init(this, positionData);
3575 cling.setVisibility(View.VISIBLE);
3576 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3577 if (animate) {
3578 cling.buildLayer();
3579 cling.setAlpha(0f);
3580 cling.animate()
3581 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003582 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003583 .setDuration(SHOW_CLING_DURATION)
3584 .setStartDelay(delay)
3585 .start();
3586 } else {
3587 cling.setAlpha(1f);
3588 }
3589 }
3590 return cling;
3591 }
3592 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003593 if (cling != null) {
3594 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003595 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003596 anim.addListener(new AnimatorListenerAdapter() {
3597 public void onAnimationEnd(Animator animation) {
3598 cling.setVisibility(View.GONE);
3599 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003600 // We should update the shared preferences on a background thread
3601 new Thread("dismissClingThread") {
3602 public void run() {
3603 SharedPreferences.Editor editor = mSharedPrefs.edit();
3604 editor.putBoolean(flag, true);
3605 editor.commit();
3606 }
3607 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003608 };
3609 });
3610 anim.start();
3611 }
3612 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003613 private void removeCling(int id) {
3614 final View cling = findViewById(id);
3615 if (cling != null) {
3616 final ViewGroup parent = (ViewGroup) cling.getParent();
3617 parent.post(new Runnable() {
3618 @Override
3619 public void run() {
3620 parent.removeView(cling);
3621 }
3622 });
3623 }
3624 }
Michael Jurka974c3862012-05-22 22:00:31 -07003625
3626 private boolean skipCustomClingIfNoAccounts() {
3627 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3628 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3629 if (customCling) {
3630 AccountManager am = AccountManager.get(this);
3631 Account[] accounts = am.getAccountsByType("com.google");
3632 return accounts.length == 0;
3633 }
3634 return false;
3635 }
3636
Winson Chung7d7541e2011-09-16 20:14:36 -07003637 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003638 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003639 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003640 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3641 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003642 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003643 } else {
3644 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003645 }
3646 }
3647 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003648 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003649 if (isClingsEnabled() &&
3650 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003651 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003652 } else {
3653 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003654 }
3655 }
3656 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003657 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003658 if (isClingsEnabled() &&
3659 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3660 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003661 } else {
3662 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003663 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003664 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003665 }
3666 public boolean isFolderClingVisible() {
3667 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003668 if (cling != null) {
3669 return cling.getVisibility() == View.VISIBLE;
3670 }
3671 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003672 }
Winson Chung82f55532011-08-09 14:14:23 -07003673 public void dismissWorkspaceCling(View v) {
3674 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003675 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003676 }
3677 public void dismissAllAppsCling(View v) {
3678 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003679 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3680 }
3681 public void dismissFolderCling(View v) {
3682 Cling cling = (Cling) findViewById(R.id.folder_cling);
3683 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003684 }
3685
Winson Chung80baf5a2010-08-09 16:03:15 -07003686 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003687 * Prints out out state for debugging.
3688 */
3689 public void dumpState() {
3690 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003691 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003692 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3693 Log.d(TAG, "mRestoring=" + mRestoring);
3694 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3695 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003696 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003697 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003698
Winson Chung785d2eb2011-04-14 16:08:02 -07003699 if (mAppsCustomizeContent != null) {
3700 mAppsCustomizeContent.dumpState();
3701 }
Joe Onoratobe386092009-11-17 17:32:16 -08003702 Log.d(TAG, "END launcher2 dump state");
3703 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003704
3705 @Override
3706 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3707 super.dump(prefix, fd, writer, args);
3708 writer.println(" ");
3709 writer.println("Debug logs: ");
3710 for (int i = 0; i < sDumpLogs.size(); i++) {
3711 writer.println(" " + sDumpLogs.get(i));
3712 }
3713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714}
Michael Jurka2763be32011-02-24 11:19:57 -08003715
Michael Jurkaabded662011-03-04 12:06:57 -08003716interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003717 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003718 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003719 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003720 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003721 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003722}