blob: dbc3f2dc695286a8189d5da9726f59c6413eda67 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
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;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700108import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700122 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
Winson Chung70d72102011-08-12 11:24:35 -0700124 private static final int MENU_GROUP_WALLPAPER = 1;
125 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
126 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700127 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
128 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Michael Jurka8b805b12012-04-18 14:23:14 -0700137 private static final int REQUEST_BIND_APPWIDGET = 11;
138
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
140
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800141 static final int SCREEN_COUNT = 5;
142 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
153 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700164 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
165
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700166 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700168 private State mState = State.WORKSPACE;
169 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800170 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700171
Joe Onorato9c1289c2009-08-17 11:03:03 -0400172 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700173 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
174 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700175 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700176 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800179 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chunga2413752012-04-03 14:22:34 -0700181 // How long to wait before the new-shortcut animation automatically pans the workspace
182 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
183
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800184 private final BroadcastReceiver mCloseSystemDialogsReceiver
185 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800186 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private LayoutInflater mInflater;
189
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800191 private View mQsbDivider;
192 private View mDockDivider;
193 private DragLayer mDragLayer;
194 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Michael Jurkac9d95c52011-08-29 14:03:34 -0700199 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 private int[] mTmpAddItemCellCoordinates = new int[2];
201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private FolderInfo mFolderInfo;
203
Winson Chung3d503fb2011-07-13 17:25:49 -0700204 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800205 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700206
Winson Chungc51db6a2011-10-05 11:44:49 -0700207 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700208 private AppsCustomizeTabHost mAppsCustomizeTabHost;
209 private AppsCustomizePagedView mAppsCustomizeContent;
210 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212 private Bundle mSavedState;
213
214 private SpannableStringBuilder mDefaultKeySsb = null;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private boolean mWorkspaceLoading = true;
217
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800218 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private boolean mRestoring;
220 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700221 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222
223 private Bundle mSavedInstanceState;
224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800226 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800227 private boolean mUserPresent = true;
228 private boolean mVisible = false;
229 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700231 private static LocaleConfiguration sLocaleConfiguration = null;
232
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700235 private Intent mAppMarketIntent = null;
236
Adam Cohended9f8d2010-11-03 13:25:16 -0700237 // Related to the auto-advancing of widgets
238 private final int ADVANCE_MSG = 1;
239 private final int mAdvanceInterval = 20000;
240 private final int mAdvanceStagger = 250;
241 private long mAutoAdvanceSentTime;
242 private long mAutoAdvanceTimeLeft = -1;
243 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
244 new HashMap<View, AppWidgetProviderInfo>();
245
Winson Chung400438b2011-01-16 17:53:48 -0800246 // Determines how long to wait after a rotation before restoring the screen orientation to
247 // match the sensor state.
248 private final int mRestoreScreenOrientationDelay = 500;
249
Michael Jurka4ef207b2010-11-29 17:05:45 -0800250 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700251 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
252 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
253 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700255 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Michael Jurka8b805b12012-04-18 14:23:14 -0700256 PendingAddWidgetInfo mWidgetBeingBoundOrConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700257
Winson Chung46353de2012-02-16 14:05:10 -0800258 // We only want to get the SharedPreferences once since it does an FS stat each time we get
259 // it from the context.
260 private SharedPreferences mSharedPrefs;
261
Winson Chungf0c6ae02012-03-21 16:10:31 -0700262 // Holds the page that we need to animate to, and the icon views that we need to animate up
263 // when we scroll to that page on resume.
264 private int mNewShortcutAnimatePage = -1;
265 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700266
Michael Jurkaddd62e92011-02-16 17:49:14 -0800267 private BubbleTextView mWaitingForResume;
268
Michael Jurkac1f5d262011-09-30 19:32:27 -0700269 private Runnable mBuildLayersRunnable = new Runnable() {
270 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700271 if (mWorkspace != null) {
272 mWorkspace.buildPageHardwareLayers();
273 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700274 }
275 };
276
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800277 private static ArrayList<PendingAddArguments> sPendingAddList
278 = new ArrayList<PendingAddArguments>();
279
280 private static class PendingAddArguments {
281 int requestCode;
282 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700283 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800284 int screen;
285 int cellX;
286 int cellY;
287 }
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 @Override
290 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700291 if (DEBUG_STRICT_MODE) {
292 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
293 .detectDiskReads()
294 .detectDiskWrites()
295 .detectNetwork() // or .detectAll() for all detectable problems
296 .penaltyLog()
297 .build());
298 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
299 .detectLeakedSqlLiteObjects()
300 .detectLeakedClosableObjects()
301 .penaltyLog()
302 .penaltyDeath()
303 .build());
304 }
305
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
309 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800310 mModel = app.setLauncher(this);
311 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400312 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700315 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700316 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
317 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200320 android.os.Debug.startMethodTracing(
321 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 }
323
324 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 setContentView(R.layout.launcher);
326 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700327 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800328
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800329 registerContentObservers();
330
Joe Onorato7c312c12009-08-13 21:36:53 -0700331 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 mSavedState = savedInstanceState;
334 restoreState(mSavedState);
335
Winson Chunga12a2502010-12-20 14:41:35 -0800336 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700337 if (mAppsCustomizeContent != null) {
338 mAppsCustomizeContent.onPackagesUpdated();
339 }
Winson Chunga12a2502010-12-20 14:41:35 -0800340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 if (PROFILE_STARTUP) {
342 android.os.Debug.stopMethodTracing();
343 }
344
345 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
348
Michael Jurkac57b7a82011-08-09 22:02:20 -0700349 if (!mModel.isAllAppsLoaded()) {
350 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
351 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 // For handling default keys
355 mDefaultKeySsb = new SpannableStringBuilder();
356 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800357
358 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
359 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800360
Winson Chungc51db6a2011-10-05 11:44:49 -0700361 boolean searchVisible = false;
362 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800363 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700364 int coi = getCurrentOrientationIndexForGlobalIcons();
365 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
366 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700367 updateAppMarketIcon();
368 searchVisible = updateGlobalSearchIcon();
369 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700370 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700371 if (sGlobalSearchIcon[coi] != null) {
372 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700373 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sVoiceSearchIcon[coi] != null) {
376 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 if (sAppMarketIcon[coi] != null) {
380 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800381 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700382 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700383
Jim Miller14091b12012-04-24 19:27:54 -0700384 final String forceEnableRotation =
Adam Cohen23a4d302011-12-01 17:26:44 -0800385 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
386
Adam Cohencb3f98e2012-04-24 14:24:35 -0700387 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
388
Adam Cohen446e9402011-09-15 18:21:21 -0700389 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700390 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700391 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 }
Romain Guycbb89e42009-06-08 15:52:54 -0700394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 if (sLocaleConfiguration == null) {
397 new AsyncTask<Void, Void, LocaleConfiguration>() {
398 @Override
399 protected LocaleConfiguration doInBackground(Void... unused) {
400 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
401 readConfiguration(Launcher.this, localeConfiguration);
402 return localeConfiguration;
403 }
404
405 @Override
406 protected void onPostExecute(LocaleConfiguration result) {
407 sLocaleConfiguration = result;
408 checkForLocaleChange(); // recursive, but now with a locale configuration
409 }
410 }.execute();
411 return;
412 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 final Configuration configuration = getResources().getConfiguration();
415
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 final String locale = configuration.locale.toString();
418
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700419 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 final int mcc = configuration.mcc;
421
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 final int mnc = configuration.mnc;
424
Romain Guy84f296c2009-11-04 15:00:44 -0800425 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426
Romain Guy84f296c2009-11-04 15:00:44 -0800427 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428 sLocaleConfiguration.locale = locale;
429 sLocaleConfiguration.mcc = mcc;
430 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700431
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700433
434 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
435 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700436 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700437 public void run() {
438 writeConfiguration(Launcher.this, localeConfiguration);
439 }
440 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700441 }
442 }
443
444 private static class LocaleConfiguration {
445 public String locale;
446 public int mcc = -1;
447 public int mnc = -1;
448 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700449
Romain Guy98d01652009-06-30 16:21:04 -0700450 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
451 DataInputStream in = null;
452 try {
453 in = new DataInputStream(context.openFileInput(PREFERENCES));
454 configuration.locale = in.readUTF();
455 configuration.mcc = in.readInt();
456 configuration.mnc = in.readInt();
457 } catch (FileNotFoundException e) {
458 // Ignore
459 } catch (IOException e) {
460 // Ignore
461 } finally {
462 if (in != null) {
463 try {
464 in.close();
465 } catch (IOException e) {
466 // Ignore
467 }
468 }
469 }
470 }
471
472 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
473 DataOutputStream out = null;
474 try {
475 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
476 out.writeUTF(configuration.locale);
477 out.writeInt(configuration.mcc);
478 out.writeInt(configuration.mnc);
479 out.flush();
480 } catch (FileNotFoundException e) {
481 // Ignore
482 } catch (IOException e) {
483 //noinspection ResultOfMethodCallIgnored
484 context.getFileStreamPath(PREFERENCES).delete();
485 } finally {
486 if (out != null) {
487 try {
488 out.close();
489 } catch (IOException e) {
490 // Ignore
491 }
492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 }
494 }
495
Adam Cohen716b51e2011-06-30 12:09:54 -0700496 public DragLayer getDragLayer() {
497 return mDragLayer;
498 }
499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 static int getScreen() {
501 synchronized (sLock) {
502 return sScreen;
503 }
504 }
505
506 static void setScreen(int screen) {
507 synchronized (sLock) {
508 sScreen = screen;
509 }
510 }
511
Winson Chung557d6ed2011-07-08 15:34:52 -0700512 /**
513 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
514 * a configuration step, this allows the proper animations to run after other transitions.
515 */
516 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700517 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 switch (args.requestCode) {
519 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700520 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
521 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800522 break;
523 case REQUEST_PICK_SHORTCUT:
524 processShortcut(args.intent);
525 break;
526 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700527 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
528 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700529 result = true;
530 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800531 case REQUEST_PICK_APPWIDGET:
532 addAppWidgetFromPick(args.intent);
533 break;
534 case REQUEST_CREATE_APPWIDGET:
535 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800536 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700537 result = true;
538 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800539 case REQUEST_PICK_WALLPAPER:
540 // We just wanted the activity result here so we can clear mWaitingForResult
541 break;
542 }
Michael Jurka27614d22012-04-02 06:40:22 -0700543 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
544 // if you turned the screen off and then back while in All Apps, Launcher would not
545 // return to the workspace. Clearing mAddInfo.container here fixes this issue
546 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700547 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800548 }
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700551 protected void onActivityResult(
552 final int requestCode, final int resultCode, final Intent data) {
553 if (requestCode == REQUEST_BIND_APPWIDGET) {
554 int appWidgetId = data != null ?
555 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
556 if (resultCode == RESULT_CANCELED) {
557 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
558 } else if (resultCode == RESULT_OK) {
559 addAppWidgetImpl(appWidgetId, mWidgetBeingBoundOrConfigured);
560 }
561 return;
562 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700563 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800564 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
565 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700566 mWaitingForResult = false;
567
Adam Cohened66b2b2012-01-23 17:28:51 -0800568 // We have special handling for widgets
569 if (isWidgetDrop) {
570 int appWidgetId = data != null ?
571 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700572 if (appWidgetId < 0) {
573 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
574 "widget configuration activity.");
575 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
576 } else {
577 completeTwoStageWidgetDrop(resultCode, appWidgetId);
578 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 return;
580 }
581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 // The pattern used here is that a user PICKs a specific application,
583 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
586 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700587 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 final PendingAddArguments args = new PendingAddArguments();
589 args.requestCode = requestCode;
590 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700591 args.container = mPendingAddInfo.container;
592 args.screen = mPendingAddInfo.screen;
593 args.cellX = mPendingAddInfo.cellX;
594 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800595 if (isWorkspaceLocked()) {
596 sPendingAddList.add(args);
597 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700598 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800601 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700602 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800603 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
604 null);
605 }
606
607 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700608 CellLayout cellLayout =
609 (CellLayout) mWorkspace.getChildAt(mWidgetBeingBoundOrConfigured.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800610 Runnable onCompleteRunnable = null;
611 int animationType = 0;
612
613 if (resultCode == RESULT_OK) {
614 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
615 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Michael Jurka8b805b12012-04-18 14:23:14 -0700616 mWidgetBeingBoundOrConfigured.info);
617 mWidgetBeingBoundOrConfigured.boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800618 onCompleteRunnable = new Runnable() {
619 @Override
620 public void run() {
621 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
622 mPendingAddInfo.screen, layout, null);
623 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
624 null);
625 }
626 };
627 } else if (resultCode == RESULT_CANCELED) {
628 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
629 onCompleteRunnable = new Runnable() {
630 @Override
631 public void run() {
632 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
633 null);
634 }
635 };
636 }
Michael Jurka8b805b12012-04-18 14:23:14 -0700637 mWorkspace.animateWidgetDrop(mWidgetBeingBoundOrConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800638 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Michael Jurka8b805b12012-04-18 14:23:14 -0700639 animationType, mWidgetBeingBoundOrConfigured.boundWidget, true);
640 mWidgetBeingBoundOrConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 }
642
643 @Override
644 protected void onResume() {
645 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700646
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800647 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700648 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400649 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700650 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400651 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700652 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
Winson Chunge4e50662012-01-23 14:45:13 -0800654
655 // Reset the pressed state of icons that were locked in the press state while activities
656 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800657 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800658 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800659 mWaitingForResume.setStayPressed(false);
660 }
Winson Chunge4e50662012-01-23 14:45:13 -0800661 if (mAppsCustomizeContent != null) {
662 // Resets the previous all apps icon press state
663 mAppsCustomizeContent.resetDrawableState();
664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
667 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668 protected void onPause() {
669 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700670 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800671 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700672 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700673 }
Romain Guycbb89e42009-06-08 15:52:54 -0700674
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700675 @Override
676 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400677 // Flag the loader to stop early before switching
678 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 if (mAppsCustomizeContent != null) {
680 mAppsCustomizeContent.surrender();
681 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800682 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700683 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800685 // We can't hide the IME if it was forced open. So don't bother
686 /*
687 @Override
688 public void onWindowFocusChanged(boolean hasFocus) {
689 super.onWindowFocusChanged(hasFocus);
690
691 if (hasFocus) {
692 final InputMethodManager inputManager = (InputMethodManager)
693 getSystemService(Context.INPUT_METHOD_SERVICE);
694 WindowManager.LayoutParams lp = getWindow().getAttributes();
695 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
696 android.os.Handler()) {
697 protected void onReceiveResult(int resultCode, Bundle resultData) {
698 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
699 }
700 });
701 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
702 }
703 }
704 */
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 private boolean acceptFilter() {
707 final InputMethodManager inputManager = (InputMethodManager)
708 getSystemService(Context.INPUT_METHOD_SERVICE);
709 return !inputManager.isFullscreenMode();
710 }
711
712 @Override
713 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700714 final int uniChar = event.getUnicodeChar();
715 final boolean handled = super.onKeyDown(keyCode, event);
716 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
717 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
719 keyCode, event);
720 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700721 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700722 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700723 // showSearchDialog()
724 // If there are multiple keystrokes before the search dialog takes focus,
725 // onSearchRequested() will be called for every keystroke,
726 // but it is idempotent, so it's fine.
727 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729 }
730
Joe Onorato8a9625e2010-01-28 15:55:35 -0800731 // Eat the long press event so the keyboard doesn't come up.
732 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
733 return true;
734 }
735
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 return handled;
737 }
738
Karl Rosaen138a0412009-04-23 19:00:21 -0700739 private String getTypedText() {
740 return mDefaultKeySsb.toString();
741 }
742
743 private void clearTypedText() {
744 mDefaultKeySsb.clear();
745 mDefaultKeySsb.clearSpans();
746 Selection.setSelection(mDefaultKeySsb, 0);
747 }
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700750 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
751 * State
752 */
753 private static State intToState(int stateOrdinal) {
754 State state = State.WORKSPACE;
755 final State[] stateValues = State.values();
756 for (int i = 0; i < stateValues.length; i++) {
757 if (stateValues[i].ordinal() == stateOrdinal) {
758 state = stateValues[i];
759 break;
760 }
761 }
762 return state;
763 }
764
765 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 * Restores the previous state, if it exists.
767 *
768 * @param savedState The previous state.
769 */
770 private void restoreState(Bundle savedState) {
771 if (savedState == null) {
772 return;
773 }
774
Michael Jurka883f55b2010-10-21 15:47:14 -0700775 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700776 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800777 showAllApps(false);
778 }
779
Winson Chungf0c6ae02012-03-21 16:10:31 -0700780 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700782 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784
Winson Chung3d503fb2011-07-13 17:25:49 -0700785 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
786 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787
Winson Chung3d503fb2011-07-13 17:25:49 -0700788 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
789 mPendingAddInfo.container = pendingAddContainer;
790 mPendingAddInfo.screen = pendingAddScreen;
791 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
792 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 mRestoring = true;
794 }
795
796 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
797 if (renameFolder) {
798 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700799 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 mRestoring = true;
801 }
Winson Chunga12a2502010-12-20 14:41:35 -0800802
Winson Chung785d2eb2011-04-14 16:08:02 -0700803
804 // Restore the AppsCustomize tab
805 if (mAppsCustomizeTabHost != null) {
806 String curTab = savedState.getString("apps_customize_currentTab");
807 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700808 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700809 mAppsCustomizeContent.setContentType(
810 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
811 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700812 mAppsCustomizeContent.loadAssociatedPages(
813 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 }
815
Winson Chung5afbf7b2011-07-25 11:53:08 -0700816 int currentIndex = savedState.getInt("apps_customize_currentIndex");
817 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 }
820
821 /**
822 * Finds all the views we need and configure them properly.
823 */
824 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700825 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400826
Winson Chung4c98d922011-05-31 16:50:48 -0700827 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
828 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800829 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
830 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
Winson Chung4c98d922011-05-31 16:50:48 -0700832 // Setup the drag layer
833 mDragLayer.setup(this, dragController);
834
Winson Chung3d503fb2011-07-13 17:25:49 -0700835 // Setup the hotseat
836 mHotseat = (Hotseat) findViewById(R.id.hotseat);
837 if (mHotseat != null) {
838 mHotseat.setup(this);
839 }
840
Winson Chung4c98d922011-05-31 16:50:48 -0700841 // Setup the workspace
842 mWorkspace.setHapticFeedbackEnabled(false);
843 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700844 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700845 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700846
Winson Chungf0ea4d32011-06-06 14:27:16 -0700847 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700848 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Setup AppsCustomize
851 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
852 findViewById(R.id.apps_customize_pane);
853 mAppsCustomizeContent = (AppsCustomizePagedView)
854 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700855 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700856 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400857
Michael Jurka5130e402011-10-13 04:55:35 -0700858 // Get the all apps button
859 mAllAppsButton = findViewById(R.id.all_apps_button);
860 if (mAllAppsButton != null) {
861 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
862 @Override
863 public boolean onTouch(View v, MotionEvent event) {
864 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
865 onTouchDownAllAppsButton(v);
866 }
867 return false;
868 }
869 });
870 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700872 dragController.setDragScoller(mWorkspace);
873 dragController.setScrollView(mDragLayer);
874 dragController.setMoveTarget(mWorkspace);
875 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700876 if (mSearchDropTargetBar != null) {
877 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
881 /**
882 * Creates a view representing a shortcut.
883 *
884 * @param info The data structure describing the shortcut.
885 *
886 * @return A View inflated from R.layout.application.
887 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800888 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700890 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892
893 /**
894 * Creates a view representing a shortcut inflated from the specified resource.
895 *
896 * @param layoutResId The id of the XML layout used to create the shortcut.
897 * @param parent The group the shortcut belongs to.
898 * @param info The data structure describing the shortcut.
899 *
900 * @return A View inflated from layoutResId.
901 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800902 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800903 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
904 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 return favorite;
907 }
908
909 /**
910 * Add an application shortcut to the workspace.
911 *
912 * @param data The intent describing the application.
913 * @param cellInfo The position on screen where to create the shortcut.
914 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700915 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700916 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700917 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700918
Adam Cohenfbba09b2011-07-18 21:43:05 -0700919 // First we check if we already know the exact location where we want to add this item.
920 if (cellX >= 0 && cellY >= 0) {
921 cellXY[0] = cellX;
922 cellXY[1] = cellY;
923 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700924 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700925 return;
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800928 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800929
Romain Guy73b979d2009-06-09 12:57:21 -0700930 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800931 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800933 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700935 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800936 } else {
937 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939 }
Romain Guycbb89e42009-06-08 15:52:54 -0700940
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 /**
942 * Add a shortcut to the workspace.
943 *
944 * @param data The intent describing the shortcut.
945 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
948 int cellY) {
949 int[] cellXY = mTmpAddItemCellCoordinates;
950 int[] touchXY = mPendingAddInfo.dropPos;
951 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700952
Michael Jurkad3ef3062010-11-23 16:23:58 -0800953 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700954
Adam Cohen558baaf2011-08-15 15:22:57 -0700955 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800956 if (info == null) {
957 return;
958 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700959 final View view = createShortcut(info);
960
Adam Cohenfbba09b2011-07-18 21:43:05 -0700961 // First we check if we already know the exact location where we want to add this item.
962 if (cellX >= 0 && cellY >= 0) {
963 cellXY[0] = cellX;
964 cellXY[1] = cellY;
965 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700966
967 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800968 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700969 true, null,null)) {
970 return;
971 }
972 DragObject dragObject = new DragObject();
973 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800974 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
975 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700976 return;
977 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700978 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800979 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800981 foundCellSpan = (result != null);
982 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800984 }
985
986 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700987 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 return;
989 }
990
Adam Cohen558baaf2011-08-15 15:22:57 -0700991 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
993 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700994 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
995 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997 }
998
Winson Chunga5c96362012-04-12 14:04:41 -0700999 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -08001000 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001001 // We want to account for the extra amount of padding that we are adding to the widget
1002 // to ensure that it gets the full amount of space that it has requested
1003 int requiredWidth = minWidth + padding.left + padding.right;
1004 int requiredHeight = minHeight + padding.top + padding.bottom;
1005 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
1006 }
1007
Winson Chunga5c96362012-04-12 14:04:41 -07001008 int[] getSpanForWidget(AppWidgetProviderInfo info) {
1009 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001010 }
1011
Winson Chunga5c96362012-04-12 14:04:41 -07001012 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1013 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001014 }
1015
Winson Chunga5c96362012-04-12 14:04:41 -07001016 int[] getSpanForWidget(PendingAddWidgetInfo info) {
1017 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001018 }
1019
Winson Chunga5c96362012-04-12 14:04:41 -07001020 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001021 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001022 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 }
1024
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001028 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001031 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1032 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1033 if (appWidgetInfo == null) {
1034 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001039
Winson Chunga5c96362012-04-12 14:04:41 -07001040 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1041 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001044 // We have saved the position to which the widget was dragged-- this really only matters
1045 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001046 int[] cellXY = mTmpAddItemCellCoordinates;
1047 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001048 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001050 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1051 cellXY[0] = mPendingAddInfo.cellX;
1052 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001053 spanXY[0] = mPendingAddInfo.spanX;
1054 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001055 foundCellSpan = true;
1056 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001059 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1060 spanXY[1], cellXY, finalSpan);
1061 spanXY[0] = finalSpan[0];
1062 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001063 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001064 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001065 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001066 }
1067
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001069 if (appWidgetId != -1) {
1070 // Deleting an app widget ID is a void call but writes to disk before returning
1071 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001072 new Thread("deleteAppWidgetId") {
1073 public void run() {
1074 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1075 }
1076 }.start();
1077 }
Winson Chung93eef082012-03-23 15:59:27 -07001078 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001079 return;
1080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001083 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1084 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 launcherInfo.spanX = spanXY[0];
1086 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001087 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1088 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092
1093 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001094 if (hostView == null) {
1095 // Perform actual inflation because we're live
1096 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1097 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1098 } else {
1099 // The AppWidgetHostView has already been inflated and instantiated
1100 launcherInfo.hostView = hostView;
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001104 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohena897f392012-04-27 18:12:05 -07001105 AppWidgetResizeFrame.updateWidgetSizeRanges(launcherInfo.hostView,
1106 this, launcherInfo.spanX, launcherInfo.spanY);
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001109
1110 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001112 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1116 @Override
1117 public void onReceive(Context context, Intent intent) {
1118 final String action = intent.getAction();
1119 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1120 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001121 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001123
Winson Chungc100e8e2011-08-09 16:02:43 -07001124 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001125 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001126 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1127 mAppsCustomizeTabHost.reset();
1128 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001129 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1131 mUserPresent = true;
1132 updateRunning();
1133 }
1134 }
1135 };
1136
1137 @Override
1138 public void onAttachedToWindow() {
1139 super.onAttachedToWindow();
1140
1141 // Listen for broadcasts related to user-presence
1142 final IntentFilter filter = new IntentFilter();
1143 filter.addAction(Intent.ACTION_SCREEN_OFF);
1144 filter.addAction(Intent.ACTION_USER_PRESENT);
1145 registerReceiver(mReceiver, filter);
1146
Adam Cohend113e0c2010-11-11 10:48:05 -08001147 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001148 mVisible = true;
1149 }
1150
1151 @Override
1152 public void onDetachedFromWindow() {
1153 super.onDetachedFromWindow();
1154 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001155 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001156
Adam Cohend113e0c2010-11-11 10:48:05 -08001157 if (mAttached) {
1158 unregisterReceiver(mReceiver);
1159 mAttached = false;
1160 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001161 updateRunning();
1162 }
1163
1164 public void onWindowVisibilityChanged(int visibility) {
1165 mVisible = visibility == View.VISIBLE;
1166 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001167 // The following code used to be in onResume, but it turns out onResume is called when
1168 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1169 // is a more appropriate event to handle
1170 if (mVisible) {
1171 mAppsCustomizeTabHost.onWindowVisible();
1172 if (!mWorkspaceLoading) {
1173 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001174 // We want to let Launcher draw itself at least once before we force it to build
1175 // layers on all the workspace pages, so that transitioning to Launcher from other
1176 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1177 // a true draw so we wait until the second preDraw call to be safe
1178 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001179 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001180 // We delay the layer building a bit in order to give
1181 // other message processing a time to run. In particular
1182 // this avoids a delay in hiding the IME if it was
1183 // currently shown, because doing that may involve
1184 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001185 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001186 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001187 return true;
1188 }
1189 });
1190 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001191 // When Launcher comes back to foreground, a different Activity might be responsible for
1192 // the app market intent, so refresh the icon
1193 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001194 clearTypedText();
1195 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001196 }
1197
1198 private void sendAdvanceMessage(long delay) {
1199 mHandler.removeMessages(ADVANCE_MSG);
1200 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1201 mHandler.sendMessageDelayed(msg, delay);
1202 mAutoAdvanceSentTime = System.currentTimeMillis();
1203 }
1204
1205 private void updateRunning() {
1206 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1207 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1208 mAutoAdvanceRunning = autoAdvanceRunning;
1209 if (autoAdvanceRunning) {
1210 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1211 sendAdvanceMessage(delay);
1212 } else {
1213 if (!mWidgetsToAdvance.isEmpty()) {
1214 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1215 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1216 }
1217 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001218 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001219 }
1220 }
1221 }
1222
1223 private final Handler mHandler = new Handler() {
1224 @Override
1225 public void handleMessage(Message msg) {
1226 if (msg.what == ADVANCE_MSG) {
1227 int i = 0;
1228 for (View key: mWidgetsToAdvance.keySet()) {
1229 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1230 final int delay = mAdvanceStagger * i;
1231 if (v instanceof Advanceable) {
1232 postDelayed(new Runnable() {
1233 public void run() {
1234 ((Advanceable) v).advance();
1235 }
1236 }, delay);
1237 }
1238 i++;
1239 }
1240 sendAdvanceMessage(mAdvanceInterval);
1241 }
1242 }
1243 };
1244
1245 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001246 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1248 if (v instanceof Advanceable) {
1249 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001250 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 updateRunning();
1252 }
1253 }
1254
1255 void removeWidgetToAutoAdvance(View hostView) {
1256 if (mWidgetsToAdvance.containsKey(hostView)) {
1257 mWidgetsToAdvance.remove(hostView);
1258 updateRunning();
1259 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 }
1261
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 launcherInfo.hostView = null;
1265 }
1266
Winson Chung93eef082012-03-23 15:59:27 -07001267 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1268 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1269 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 }
1271
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001272 public LauncherAppWidgetHost getAppWidgetHost() {
1273 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Romain Guycbb89e42009-06-08 15:52:54 -07001275
Winson Chunga9abd0e2010-10-27 17:18:37 -07001276 public LauncherModel getModel() {
1277 return mModel;
1278 }
1279
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001280 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001281 getWindow().closeAllPanels();
1282
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001283 // Whatever we were doing is hereby canceled.
1284 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 @Override
1288 protected void onNewIntent(Intent intent) {
1289 super.onNewIntent(intent);
1290
1291 // Close the menu
1292 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001293 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001294 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001295
Joe Onorato14f122b2009-11-19 14:06:36 -08001296 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1297 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001298
Adam Cohenac56cff2011-09-28 20:45:37 -07001299 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001300 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001301 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001302 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1303 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001304 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001305 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001306
1307 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001308 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001309 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001310
Joe Onorato3a8820b2009-11-10 15:06:42 -08001311 final View v = getWindow().peekDecorView();
1312 if (v != null && v.getWindowToken() != null) {
1313 InputMethodManager imm = (InputMethodManager)getSystemService(
1314 INPUT_METHOD_SERVICE);
1315 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001317
Michael Jurka35aa14d2011-07-07 17:01:08 -07001318 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001319 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001320 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323 }
1324
1325 @Override
1326 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1327 // Do not call super here
1328 mSavedInstanceState = savedInstanceState;
1329 }
1330
1331 @Override
1332 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001333 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001334 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
Michael Jurka883f55b2010-10-21 15:47:14 -07001336 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001337 // We close any open folder since it will not be re-opened, and we need to make sure
1338 // this state is reflected.
1339 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340
Winson Chung3d503fb2011-07-13 17:25:49 -07001341 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1342 mWaitingForResult) {
1343 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1344 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1345 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1346 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348
1349 if (mFolderInfo != null && mWaitingForResult) {
1350 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1351 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1352 }
Michael Jurka946ad472010-07-09 18:05:18 -07001353
Winson Chung785d2eb2011-04-14 16:08:02 -07001354 // Save the current AppsCustomize tab
1355 if (mAppsCustomizeTabHost != null) {
1356 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1357 if (currentTabTag != null) {
1358 outState.putString("apps_customize_currentTab", currentTabTag);
1359 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001360 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1361 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
1365 @Override
1366 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001368
Winson Chunge7a03942011-08-05 15:05:12 -07001369 // Remove all pending runnables
1370 mHandler.removeMessages(ADVANCE_MSG);
1371 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001372 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001373
Winson Chungcd2b0142011-06-08 16:02:26 -07001374 // Stop callbacks from LauncherModel
1375 LauncherApplication app = ((LauncherApplication) getApplication());
1376 mModel.stopLoader();
1377 app.setLauncher(null);
1378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001380 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001382 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001384 mAppWidgetHost = null;
1385
1386 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
1388 TextKeyListener.getInstance().release();
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001392
1393 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001394 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001395
1396 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1397 mWorkspace.removeAllViews();
1398 mWorkspace = null;
1399 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001400
1401 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
Adam Cohena9cf38f2011-05-02 15:36:58 -07001404 public DragController getDragController() {
1405 return mDragController;
1406 }
1407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 @Override
1409 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001410 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 super.startActivityForResult(intent, requestCode);
1412 }
1413
Winson Chung70d72102011-08-12 11:24:35 -07001414 /**
1415 * Indicates that we want global search for this activity by setting the globalSearch
1416 * argument for {@link #startSearch} to true.
1417 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001419 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001421
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001422 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Karl Rosaen138a0412009-04-23 19:00:21 -07001424 if (initialQuery == null) {
1425 // Use any text typed in the launcher as the initial query
1426 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 if (appSearchData == null) {
1429 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001430 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001432 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Karl Rosaen138a0412009-04-23 19:00:21 -07001434 final SearchManager searchManager =
1435 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001436 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001437 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
Winson Chung70d72102011-08-12 11:24:35 -07001440 @Override
1441 public boolean onCreateOptionsMenu(Menu menu) {
1442 if (isWorkspaceLocked()) {
1443 return false;
1444 }
1445
1446 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001447
1448 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1449 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1450 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001451 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1452 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1453 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001454 String helpUrl = getString(R.string.help_url);
1455 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001456 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1457 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1458
Winson Chung70d72102011-08-12 11:24:35 -07001459 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1460 .setIcon(android.R.drawable.ic_menu_gallery)
1461 .setAlphabeticShortcut('W');
1462 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1463 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001464 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001465 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001466 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1467 .setIcon(android.R.drawable.ic_menu_preferences)
1468 .setIntent(settings)
1469 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001470 if (!helpUrl.isEmpty()) {
1471 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1472 .setIcon(android.R.drawable.ic_menu_help)
1473 .setIntent(help)
1474 .setAlphabeticShortcut('H');
1475 }
Winson Chung70d72102011-08-12 11:24:35 -07001476 return true;
1477 }
1478
1479 @Override
1480 public boolean onPrepareOptionsMenu(Menu menu) {
1481 super.onPrepareOptionsMenu(menu);
1482
1483 if (mAppsCustomizeTabHost.isTransitioning()) {
1484 return false;
1485 }
1486 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1487 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1488
1489 return true;
1490 }
1491
1492 @Override
1493 public boolean onOptionsItemSelected(MenuItem item) {
1494 switch (item.getItemId()) {
1495 case MENU_WALLPAPER_SETTINGS:
1496 startWallpaper();
1497 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001498 }
1499
1500 return super.onOptionsItemSelected(item);
1501 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001503 @Override
1504 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001505 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001506 // Use a custom animation for launching search
1507 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001508 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001509 }
1510
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 public boolean isWorkspaceLocked() {
1512 return mWorkspaceLoading || mWaitingForResult;
1513 }
1514
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 mPendingAddInfo.container = ItemInfo.NO_ID;
1517 mPendingAddInfo.screen = -1;
1518 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1519 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001521 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001522 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001523
Michael Jurkaaf442092010-06-10 17:01:57 -07001524 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001525 // TODO: catch bad widget exception when sent
1526 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001527 // TODO: Is this log message meaningful?
1528 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001529 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001530 }
1531
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1533 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001534 if (appWidget.configure != null) {
1535 // Launch over to configure widget, if needed
1536 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1537 intent.setComponent(appWidget.configure);
1538 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001539 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001540 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001541 intent.putExtra(InstallWidgetReceiver.
1542 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001543
1544 final String mimeType = info.mimeType;
1545 final ClipData clipData = (ClipData) info.configurationData;
1546 final ClipDescription clipDesc = clipData.getDescription();
1547 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1548 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001549 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001550 final CharSequence stringData = item.getText();
1551 final Uri uriData = item.getUri();
1552 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001553 final String key = InstallWidgetReceiver.
1554 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001555 if (uriData != null) {
1556 intent.putExtra(key, uriData);
1557 } else if (intentData != null) {
1558 intent.putExtra(key, intentData);
1559 } else if (stringData != null) {
1560 intent.putExtra(key, stringData);
1561 }
1562 break;
1563 }
1564 }
1565 }
Winson Chung55cef262010-10-28 14:14:18 -07001566 }
Romain Guy8e633c52010-03-04 12:51:36 -08001567 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Michael Jurka8b805b12012-04-18 14:23:14 -07001568 mWidgetBeingBoundOrConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001570 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001572 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001573 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 }
1575 }
Romain Guycbb89e42009-06-08 15:52:54 -07001576
Adam Cohenfbba09b2011-07-18 21:43:05 -07001577 /**
1578 * Process a shortcut drop.
1579 *
1580 * @param componentName The name of the component
1581 * @param screen The screen where it should be added
1582 * @param cell The cell it should be added to, optional
1583 * @param position The location on the screen where it was dropped, optional
1584 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001585 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1586 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001587 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 mPendingAddInfo.container = container;
1589 mPendingAddInfo.screen = screen;
1590 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591
1592 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001593 mPendingAddInfo.cellX = cell[0];
1594 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001595 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596
1597 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1598 createShortcutIntent.setComponent(componentName);
1599 processShortcut(createShortcutIntent);
1600 }
1601
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602 /**
1603 * Process a widget drop.
1604 *
1605 * @param info The PendingAppWidgetInfo of the widget being added.
1606 * @param screen The screen where it should be added
1607 * @param cell The cell it should be added to, optional
1608 * @param position The location on the screen where it was dropped, optional
1609 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001610 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001611 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001612 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001613 mPendingAddInfo.container = info.container = container;
1614 mPendingAddInfo.screen = info.screen = screen;
1615 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001616 mPendingAddInfo.minSpanX = info.minSpanX;
1617 mPendingAddInfo.minSpanY = info.minSpanY;
1618
Adam Cohenfbba09b2011-07-18 21:43:05 -07001619 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001620 mPendingAddInfo.cellX = cell[0];
1621 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001622 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 if (span != null) {
1624 mPendingAddInfo.spanX = span[0];
1625 mPendingAddInfo.spanY = span[1];
1626 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001627
Adam Cohened66b2b2012-01-23 17:28:51 -08001628 AppWidgetHostView hostView = info.boundWidget;
1629 int appWidgetId;
1630 if (hostView != null) {
1631 appWidgetId = hostView.getAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001632 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 } else {
1634 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001635 mWidgetBeingBoundOrConfigured = info;
1636 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
1637 addAppWidgetImpl(appWidgetId, info);
1638 } else {
1639 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1640 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1641 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1642 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1643 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001644 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001645 }
1646
Joe Onoratodeb98af2010-02-19 14:59:39 -08001647 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001648 // Handle case where user selected "Applications"
1649 String applicationName = getResources().getString(R.string.group_applications);
1650 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 if (applicationName != null && applicationName.equals(shortcutName)) {
1653 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1654 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001655
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1657 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001658 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001659 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001660 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001661 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
1664
Winson Chung24ab2f12010-09-16 14:10:47 -07001665 void processWallpaper(Intent intent) {
1666 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1667 }
1668
Winson Chung3d503fb2011-07-13 17:25:49 -07001669 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1670 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001671 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 folderInfo.title = getText(R.string.folder_name);
1673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001675 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1676 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001677 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678
1679 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 FolderIcon newFolder =
1681 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1682 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1683 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001684 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 }
Romain Guycbb89e42009-06-08 15:52:54 -07001686
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001688 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001692 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001694 Intent chooser = Intent.createChooser(pickWallpaper,
1695 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001696 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1697 // Removed in Eclair MR1
1698// WallpaperManager wm = (WallpaperManager)
1699// getSystemService(Context.WALLPAPER_SERVICE);
1700// WallpaperInfo wi = wm.getWallpaperInfo();
1701// if (wi != null && wi.getSettingsActivity() != null) {
1702// LabeledIntent li = new LabeledIntent(getPackageName(),
1703// R.string.configure_wallpaper, 0);
1704// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1705// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1706// }
Mike Clerona0618e42009-10-22 13:55:21 -07001707 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
1709
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001710 /**
1711 * Registers various content observers. The current implementation registers
1712 * only a favorites observer to keep track of the favorites applications.
1713 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001714 private void registerContentObservers() {
1715 ContentResolver resolver = getContentResolver();
1716 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1717 true, mWidgetObserver);
1718 }
1719
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 @Override
1721 public boolean dispatchKeyEvent(KeyEvent event) {
1722 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1723 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001725 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001726 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001727 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001728 dumpState();
1729 return true;
1730 }
1731 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001732 }
1733 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1734 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001735 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 return true;
1737 }
1738 }
1739
1740 return super.dispatchKeyEvent(event);
1741 }
1742
Joe Onorato88ec0992009-11-19 13:16:06 -08001743 @Override
1744 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001745 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001746 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001747 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001748 Folder openFolder = mWorkspace.getOpenFolder();
1749 if (openFolder.isEditingName()) {
1750 openFolder.dismissEditingName();
1751 } else {
1752 closeFolder();
1753 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001754 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001755 mWorkspace.exitWidgetResizeMode();
1756
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001757 // Back button is a no-op here, but give at least some feedback for the button press
1758 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001759 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001760 }
1761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001763 * Re-listen when widgets are reset.
1764 */
1765 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001766 if (mAppWidgetHost != null) {
1767 mAppWidgetHost.startListening();
1768 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001769 }
1770
1771 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1773 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001775 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001776 if (mModel != null) {
1777 mModel.unbindWorkspaceItems();
1778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 /**
1782 * Launches the intent referred by the clicked shortcut.
1783 *
1784 * @param v The view representing the clicked shortcut.
1785 */
1786 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001787 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1788 // view has detached (it's possible for this to happen if the view is removed mid touch).
1789 if (v.getWindowToken() == null) {
1790 return;
1791 }
1792
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001793 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001794 return;
1795 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001796
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001798 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001800 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001801 int[] pos = new int[2];
1802 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001803 intent.setSourceBounds(new Rect(pos[0], pos[1],
1804 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001805
1806 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001807
1808 if (success && v instanceof BubbleTextView) {
1809 mWaitingForResume = (BubbleTextView) v;
1810 mWaitingForResume.setStayPressed(true);
1811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001813 if (v instanceof FolderIcon) {
1814 FolderIcon fi = (FolderIcon) v;
1815 handleFolderClick(fi);
1816 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001817 } else if (v == mAllAppsButton) {
1818 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001819 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001820 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001821 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824 }
1825
Michael Jurka0e260592010-06-30 17:07:39 -07001826 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001827 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001828 // clicking anywhere on the workspace causes the customization drawer to slide down
1829 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001830 return false;
1831 }
1832
Michael Jurkaaf442092010-06-10 17:01:57 -07001833 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001834 * Event handler for the search button
1835 *
1836 * @param v The view that was clicked.
1837 */
1838 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001839 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1840
Amith Yamasania135ba82011-08-09 17:42:01 -07001841 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001842 }
1843
1844 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001845 * Event handler for the voice button
1846 *
1847 * @param v The view that was clicked.
1848 */
1849 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001850 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001851
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001852 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001853 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001854 startActivitySafely(null, intent, "onClickVoiceButton");
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001855 }
1856
1857 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001858 * Event handler for the "grid" button that appears on the home screen, which
1859 * enters all apps mode.
1860 *
1861 * @param v The view that was clicked.
1862 */
1863 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001864 showAllApps(true);
1865 }
1866
1867 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001868 // Provide the same haptic feedback that the system offers for virtual keys.
1869 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001870 }
1871
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001872 public void onClickAppMarketButton(View v) {
1873 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001874 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001875 } else {
1876 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001877 }
1878 }
1879
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001880 void startApplicationDetailsActivity(ComponentName componentName) {
1881 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001882 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1883 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001884 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001885 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001886 }
1887
Patrick Dubroy5539af72010-09-07 15:22:01 -07001888 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1889 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1890 // System applications cannot be installed. For now, show a toast explaining that.
1891 // We may give them the option of disabling apps this way.
1892 int messageId = R.string.uninstall_system_app_text;
1893 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1894 } else {
1895 String packageName = appInfo.componentName.getPackageName();
1896 String className = appInfo.componentName.getClassName();
1897 Intent intent = new Intent(
1898 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001899 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1900 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001901 startActivity(intent);
1902 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001903 }
1904
Winson Chungc7450e32012-04-17 17:34:08 -07001905 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001909 if (v != null) {
1910 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1911 v.getMeasuredWidth(), v.getMeasuredHeight());
1912
1913 startActivity(intent, opts.toBundle());
1914 } else {
1915 startActivity(intent);
1916 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001917 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 } catch (ActivityNotFoundException e) {
1919 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001920 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 } catch (SecurityException e) {
1922 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001923 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001925 "or use the exported attribute for this activity. "
1926 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001928 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001930
Romain Guy8e633c52010-03-04 12:51:36 -08001931 void startActivityForResultSafely(Intent intent, int requestCode) {
1932 try {
1933 startActivityForResult(intent, requestCode);
1934 } catch (ActivityNotFoundException e) {
1935 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1936 } catch (SecurityException e) {
1937 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1938 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1939 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1940 "or use the exported attribute for this activity.", e);
1941 }
1942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 private void handleFolderClick(FolderIcon folderIcon) {
1945 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001946 Folder openFolder = mWorkspace.getFolderForTag(info);
1947
1948 // If the folder info reports that the associated folder is open, then verify that
1949 // it is actually opened. There have been a few instances where this gets out of sync.
1950 if (info.opened && openFolder == null) {
1951 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1952 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1953 info.opened = false;
1954 }
1955
Adam Cohena9cf38f2011-05-02 15:36:58 -07001956 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 // Close any open folder
1958 closeFolder();
1959 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001960 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 } else {
1962 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 int folderScreen;
1964 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001965 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 // .. and close it
1967 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001968 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // Close any folder open on the current screen
1970 closeFolder();
1971 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001972 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
1975 }
1976 }
1977
Adam Cohen2801caf2011-05-13 20:57:39 -07001978 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001979 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001980 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1981 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1982 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1983
Adam Cohenc51934b2011-07-26 21:07:43 -07001984 FolderInfo info = (FolderInfo) fi.getTag();
1985 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1986 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001987 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1988 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001989 }
1990
Adam Cohen2801caf2011-05-13 20:57:39 -07001991 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1992 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1993 oa.start();
1994 }
1995
1996 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001997 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001998 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1999 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2000 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2001
Adam Cohenc51934b2011-07-26 21:07:43 -07002002 FolderInfo info = (FolderInfo) fi.getTag();
2003 CellLayout cl = null;
2004 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2005 cl = (CellLayout) fi.getParent().getParent();
2006 }
2007
2008 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07002009 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
2010 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002011 oa.addListener(new AnimatorListenerAdapter() {
2012 @Override
2013 public void onAnimationEnd(Animator animation) {
2014 if (layout != null) {
2015 layout.clearFolderLeaveBehind();
2016 }
2017 }
2018 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002019 oa.start();
2020 }
2021
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002023 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 * is animated relative to the specified View. If the View is null, no animation
2025 * is played.
2026 *
2027 * @param folderInfo The FolderInfo describing the folder to open.
2028 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002029 public void openFolder(FolderIcon folderIcon) {
2030 Folder folder = folderIcon.mFolder;
2031 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032
Adam Cohen2801caf2011-05-13 20:57:39 -07002033 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002034 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035
Adam Cohen4554ee12011-08-03 16:13:21 -07002036 // Just verify that the folder hasn't already been added to the DragLayer.
2037 // There was a one-off crash where the folder had a parent already.
2038 if (folder.getParent() == null) {
2039 mDragLayer.addView(folder);
2040 mDragController.addDropTarget((DropTarget) folder);
2041 } else {
2042 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2043 folder.getParent() + ").");
2044 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002045 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002046 }
2047
2048 public void closeFolder() {
2049 Folder folder = mWorkspace.getOpenFolder();
2050 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002051 if (folder.isEditingName()) {
2052 folder.dismissEditingName();
2053 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002054 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002055
2056 // Dismiss the folder cling
2057 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002058 }
2059 }
2060
2061 void closeFolder(Folder folder) {
2062 folder.getInfo().opened = false;
2063
2064 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2065 if (parent != null) {
2066 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2067 shrinkAndFadeInFolderIcon(fi);
2068 }
2069 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002073
Patrick Dubroye708c522011-03-01 16:03:43 -08002074 if (mState != State.WORKSPACE) {
2075 return false;
2076 }
2077
Joe Onorato9c1289c2009-08-17 11:03:03 -04002078 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 return false;
2080 }
2081
2082 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002083 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 }
2085
Michael Jurka0280c3b2010-09-17 15:00:07 -07002086 resetAddInfo();
2087 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002089 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 return true;
2091 }
2092
Winson Chung3d503fb2011-07-13 17:25:49 -07002093 // The hotseat touch handling does not go through Workspace, and we always allow long press
2094 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002095 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002096 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2097 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002098 if (itemUnderLongClick == null) {
2099 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002100 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2101 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002102 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002104 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002106 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108 }
2109 }
2110 return true;
2111 }
2112
Winson Chung3d503fb2011-07-13 17:25:49 -07002113 boolean isHotseatLayout(View layout) {
2114 return mHotseat != null && layout != null &&
2115 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2116 }
2117 Hotseat getHotseat() {
2118 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002119 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002120 SearchDropTargetBar getSearchBar() {
2121 return mSearchDropTargetBar;
2122 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002123
Winson Chung3d503fb2011-07-13 17:25:49 -07002124 /**
2125 * Returns the CellLayout of the specified container at the specified screen.
2126 */
2127 CellLayout getCellLayout(long container, int screen) {
2128 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2129 if (mHotseat != null) {
2130 return mHotseat.getLayout();
2131 } else {
2132 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002133 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002134 } else {
2135 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002136 }
2137 }
2138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 Workspace getWorkspace() {
2140 return mWorkspace;
2141 }
2142
Daniel Sandler843e8602010-06-07 14:59:01 -04002143 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2144 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002145 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002146 }
2147
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002148 public boolean isAllAppsButtonRank(int rank) {
2149 return mHotseat.isAllAppsButtonRank(rank);
2150 }
2151
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002152 // AllAppsView.Watcher
2153 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002154 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002155 mWorkspace.setVisibility(View.GONE);
2156 }
2157 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002158
2159 /**
2160 * Helper method for the cameraZoomIn/cameraZoomOut animations
2161 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002162 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002163 * @param scaleFactor The scale factor used for the zoom
2164 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002165 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002166 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002167 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002168 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002169
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002170 void updateWallpaperVisibility(boolean visible) {
2171 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2172 int curflags = getWindow().getAttributes().flags
2173 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2174 if (wpflags != curflags) {
2175 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2176 }
2177 }
2178
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002179 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2180 if (v instanceof LauncherTransitionable) {
2181 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2182 }
2183 }
2184
Michael Jurkabed61d22012-02-14 22:51:29 -08002185 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2186 if (v instanceof LauncherTransitionable) {
2187 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2188 }
Winson Chung70442722012-02-10 15:43:22 -08002189
2190 // Update the workspace transition step as well
2191 dispatchOnLauncherTransitionStep(v, 0f);
2192 }
2193
2194 private void dispatchOnLauncherTransitionStep(View v, float t) {
2195 if (v instanceof LauncherTransitionable) {
2196 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2197 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002198 }
2199
2200 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2201 if (v instanceof LauncherTransitionable) {
2202 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2203 }
Winson Chung70442722012-02-10 15:43:22 -08002204
2205 // Update the workspace transition step as well
2206 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002207 }
2208
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002209 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002210 * Things to test when changing the following seven functions.
2211 * - Home from workspace
2212 * - from center screen
2213 * - from other screens
2214 * - Home from all apps
2215 * - from center screen
2216 * - from other screens
2217 * - Back from all apps
2218 * - from center screen
2219 * - from other screens
2220 * - Launch app from workspace and quit
2221 * - with back
2222 * - with home
2223 * - Launch app from all apps and quit
2224 * - with back
2225 * - with home
2226 * - Go to a screen that's not the default, then all
2227 * apps, and launch and app, and go back
2228 * - with back
2229 * -with home
2230 * - On workspace, long press power and go back
2231 * - with back
2232 * - with home
2233 * - On all apps, long press power and go back
2234 * - with back
2235 * - with home
2236 * - On workspace, power off
2237 * - On all apps, power off
2238 * - Launch an app and turn off the screen while in that app
2239 * - Go back with home key
2240 * - Go back with back key TODO: make this not go to workspace
2241 * - From all apps
2242 * - From workspace
2243 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2244 * - From all apps
2245 * - From the center workspace
2246 * - From another workspace
2247 */
2248
2249 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002250 * Zoom the camera out from the workspace to reveal 'toView'.
2251 * Assumes that the view to show is anchored at either the very top or very bottom
2252 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002253 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002254 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002255 if (mStateAnimation != null) {
2256 mStateAnimation.cancel();
2257 mStateAnimation = null;
2258 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002259 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002260
Winson Chungf0ea4d32011-06-06 14:27:16 -07002261 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2262 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2263 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002264 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002265 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002266 final int startDelay =
2267 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002268
Michael Jurkab3e22d92011-10-31 15:58:33 -07002269 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002270
Michael Jurkad74c9842011-07-10 12:44:21 -07002271 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002272 Animator workspaceAnim =
2273 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002274
2275 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002276 toView.setScaleX(scale);
2277 toView.setScaleY(scale);
2278 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2279 scaleAnim.
2280 scaleX(1f).scaleY(1f).
2281 setDuration(duration).
2282 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002283
Winson Chungf0ea4d32011-06-06 14:27:16 -07002284 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002285 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002286 final ObjectAnimator alphaAnim = ObjectAnimator
2287 .ofFloat(toView, "alpha", 0f, 1f)
2288 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002289 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002290 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2291 @Override
2292 public void onAnimationUpdate(ValueAnimator animation) {
2293 float t = (Float) animation.getAnimatedValue();
2294 dispatchOnLauncherTransitionStep(fromView, t);
2295 dispatchOnLauncherTransitionStep(toView, t);
2296 }
2297 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002298
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002299 // toView should appear right at the end of the workspace shrink
2300 // animation
2301 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002302 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002303 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002304
2305 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002306 boolean animationCancelled = false;
2307
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002308 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002309 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002310 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002311 // Prepare the position
2312 toView.setTranslationX(0.0f);
2313 toView.setTranslationY(0.0f);
2314 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002315 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002316 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002317 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002318 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002319 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2320 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002321
2322 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2323 // Hide the workspace scrollbar
2324 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002325 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002326 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002327 if (!animationCancelled) {
2328 updateWallpaperVisibility(false);
2329 }
2330 }
2331
Adam Cohencff6af82011-09-13 14:51:53 -07002332 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002333 public void onAnimationCancel(Animator animation) {
2334 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002335 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002336 });
2337
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002338 if (workspaceAnim != null) {
2339 mStateAnimation.play(workspaceAnim);
2340 }
Michael Jurka1899a362011-11-03 13:50:45 -07002341
2342 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002343 final ViewTreeObserver observer;
2344
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002345 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2346 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002347
2348 // If any of the objects being animated haven't been measured/laid out
2349 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002350 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002351 (mWorkspace.getMeasuredWidth() == 0) ||
2352 (toView.getMeasuredWidth() == 0)) {
2353 observer = mWorkspace.getViewTreeObserver();
2354 delayAnim = true;
2355 } else {
2356 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002357 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002358
2359 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002360 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002361 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2362 public void onGlobalLayout() {
2363 mWorkspace.post(new Runnable() {
2364 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002365 // Check that mStateAnimation hasn't changed while
2366 // we waited for a layout pass
2367 if (mStateAnimation == stateAnimation) {
2368 // Need to update pivots for zoom if layout changed
2369 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002370 dispatchOnLauncherTransitionStart(fromView, animated, false);
2371 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka49779a12012-01-16 04:10:08 -08002372 mStateAnimation.start();
2373 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002374 }
2375 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002376 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002377 }
2378 };
2379 observer.addOnGlobalLayoutListener(delayedStart);
2380 } else {
2381 setPivotsForZoom(toView, scale);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002382 dispatchOnLauncherTransitionStart(fromView, animated, false);
2383 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka1899a362011-11-03 13:50:45 -07002384 mStateAnimation.start();
2385 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002386 } else {
2387 toView.setTranslationX(0.0f);
2388 toView.setTranslationY(0.0f);
2389 toView.setScaleX(1.0f);
2390 toView.setScaleY(1.0f);
2391 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002392 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002393
Michael Jurkabed61d22012-02-14 22:51:29 -08002394 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2395 // Hide the workspace scrollbar
2396 mWorkspace.hideScrollingIndicator(true);
2397 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002398 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002399 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002400 dispatchOnLauncherTransitionStart(fromView, animated, false);
2401 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002402 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002403 dispatchOnLauncherTransitionStart(toView, animated, false);
2404 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002405 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002407 }
2408
2409 /**
2410 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002411 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002412 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002413 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002414 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2415 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002416
Michael Jurkab3e22d92011-10-31 15:58:33 -07002417 if (mStateAnimation != null) {
2418 mStateAnimation.cancel();
2419 mStateAnimation = null;
2420 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002421 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002422
Winson Chungf0ea4d32011-06-06 14:27:16 -07002423 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002424 final int fadeOutDuration =
2425 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 final float scaleFactor = (float)
2427 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2428 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002429 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002430 Animator workspaceAnim = null;
2431
2432 if (toState == State.WORKSPACE) {
2433 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2434 workspaceAnim = mWorkspace.getChangeStateAnimation(
2435 Workspace.State.NORMAL, animated, stagger);
2436 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2437 workspaceAnim = mWorkspace.getChangeStateAnimation(
2438 Workspace.State.SPRING_LOADED, animated);
2439 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002440
Michael Jurkab3e22d92011-10-31 15:58:33 -07002441 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002442 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002443 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002445 final LauncherViewPropertyAnimator scaleAnim =
2446 new LauncherViewPropertyAnimator(fromView);
2447 scaleAnim.
2448 scaleX(scaleFactor).scaleY(scaleFactor).
2449 setDuration(duration).
2450 setInterpolator(new Workspace.ZoomInInterpolator());
2451
2452 final ObjectAnimator alphaAnim = ObjectAnimator
2453 .ofFloat(fromView, "alpha", 1f, 0f)
2454 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002455 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002456 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2457 @Override
2458 public void onAnimationUpdate(ValueAnimator animation) {
2459 float t = 1f - (Float) animation.getAnimatedValue();
2460 dispatchOnLauncherTransitionStep(fromView, t);
2461 dispatchOnLauncherTransitionStep(toView, t);
2462 }
2463 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002464
Michael Jurkabed61d22012-02-14 22:51:29 -08002465 mStateAnimation = new AnimatorSet();
2466
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002467 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2468 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002469
2470 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002471 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002472 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002473 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002474 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002475 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2476 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002477 if (mWorkspace != null) {
2478 mWorkspace.hideScrollingIndicator(false);
2479 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002480 if (onCompleteRunnable != null) {
2481 onCompleteRunnable.run();
2482 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002483 }
2484 });
2485
Winson Chungf0ea4d32011-06-06 14:27:16 -07002486 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002487 if (workspaceAnim != null) {
2488 mStateAnimation.play(workspaceAnim);
2489 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002490 dispatchOnLauncherTransitionStart(fromView, animated, true);
2491 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002492 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 } else {
2494 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002495 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002496 dispatchOnLauncherTransitionStart(fromView, animated, true);
2497 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002498 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002499 dispatchOnLauncherTransitionStart(toView, animated, true);
2500 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002501 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002502 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002503 }
2504
Michael Jurkae326f182011-11-21 14:05:46 -08002505 @Override
2506 public void onTrimMemory(int level) {
2507 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002508 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002509 mAppsCustomizeTabHost.onTrimMemory();
2510 }
2511 }
2512
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002513 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002514 showWorkspace(animated, null);
2515 }
2516
2517 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002518 if (mState != State.WORKSPACE) {
2519 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002520 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002521
2522 // Show the search bar and hotseat
2523 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002524 // We only need to animate in the dock divider if we're going from spring loaded mode
2525 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002526
2527 // Set focus to the AppsCustomize button
2528 if (mAllAppsButton != null) {
2529 mAllAppsButton.requestFocus();
2530 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002531 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002532
Michael Jurkab737ee62011-11-15 15:57:22 -08002533 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002534
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002535 // Change the state *after* we've called all the transition code
2536 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002537
Winson Chung5fb63472011-02-02 17:03:37 -08002538 // Resume the auto-advance of widgets
2539 mUserPresent = true;
2540 updateRunning();
2541
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002542 // send an accessibility event to announce the context change
2543 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002544 }
2545
Michael Jurkab3e22d92011-10-31 15:58:33 -07002546 void showAllApps(boolean animated) {
2547 if (mState != State.WORKSPACE) return;
2548
2549 showAppsCustomizeHelper(animated, false);
2550 mAppsCustomizeTabHost.requestFocus();
2551
2552 // Hide the search bar and hotseat
2553 mSearchDropTargetBar.hideSearchBar(animated);
2554
2555 // Change the state *after* we've called all the transition code
2556 mState = State.APPS_CUSTOMIZE;
2557
2558 // Pause the auto-advance of widgets until we are out of AllApps
2559 mUserPresent = false;
2560 updateRunning();
2561 closeFolder();
2562
2563 // Send an accessibility event to announce the context change
2564 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2565 }
2566
Adam Cohen7777d962011-08-18 18:58:38 -07002567 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002568 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002569 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002570 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002571 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002572 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002573 }
Adam Cohen7777d962011-08-18 18:58:38 -07002574
Adam Cohened66b2b2012-01-23 17:28:51 -08002575 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2576 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002577 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2578
Winson Chunge7a03942011-08-05 15:05:12 -07002579 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002580 @Override
2581 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002582 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002583 // Before we show workspace, hide all apps again because
2584 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2585 // clean up our state transition functions
2586 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002587 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002588 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002589 } else {
2590 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002591 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002592 }
2593 }, (extendedDelay ?
2594 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2595 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2596 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002597
Michael Jurkad3ef3062010-11-23 16:23:58 -08002598 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002599 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002600 final boolean animated = true;
2601 final boolean springLoaded = true;
2602 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002603 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002604 }
2605 // Otherwise, we are not in spring loaded mode, so don't do anything.
2606 }
2607
Michael Jurkab737ee62011-11-15 15:57:22 -08002608 void hideDockDivider() {
2609 if (mQsbDivider != null && mDockDivider != null) {
2610 mQsbDivider.setVisibility(View.INVISIBLE);
2611 mDockDivider.setVisibility(View.INVISIBLE);
2612 }
2613 }
2614
2615 void showDockDivider(boolean animated) {
2616 if (mQsbDivider != null && mDockDivider != null) {
2617 mQsbDivider.setVisibility(View.VISIBLE);
2618 mDockDivider.setVisibility(View.VISIBLE);
2619 if (mDividerAnimator != null) {
2620 mDividerAnimator.cancel();
2621 mQsbDivider.setAlpha(1f);
2622 mDockDivider.setAlpha(1f);
2623 mDividerAnimator = null;
2624 }
2625 if (animated) {
2626 mDividerAnimator = new AnimatorSet();
2627 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2628 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2629 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2630 mDividerAnimator.start();
2631 }
2632 }
2633 }
2634
Michael Jurkab3e22d92011-10-31 15:58:33 -07002635 void lockAllApps() {
2636 // TODO
2637 }
2638
2639 void unlockAllApps() {
2640 // TODO
2641 }
2642
Adam Cohenfc53cd22011-07-20 15:45:11 -07002643 public boolean isAllAppsCustomizeOpen() {
2644 return mState == State.APPS_CUSTOMIZE;
2645 }
2646
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002648 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002649 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002650 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002651 if (!LauncherApplication.isScreenLarge()) {
2652 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002653 if (mHotseat.getAlpha() != 1f) {
2654 int duration = mSearchDropTargetBar.getTransitionInDuration();
2655 mHotseat.animate().alpha(1f).setDuration(duration);
2656 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002657 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002658 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002659 }
2660 }
2661 }
2662
2663 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002664 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002665 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002666 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002667 if (!LauncherApplication.isScreenLarge()) {
2668 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002669 if (mHotseat.getAlpha() != 0f) {
2670 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2671 mHotseat.animate().alpha(0f).setDuration(duration);
2672 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002673 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002674 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002675 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002676 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002677 }
2678
Patrick Dubroy5f445422011-02-18 14:35:21 -08002679 /**
2680 * Add an item from all apps or customize onto the given workspace screen.
2681 * If layout is null, add to the current screen.
2682 */
2683 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002684 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002685 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002686 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002687 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002688
Winson Chungdff8ebb2011-09-08 17:25:31 -07002689 /** Maps the current orientation to an index for referencing orientation correct global icons */
2690 private int getCurrentOrientationIndexForGlobalIcons() {
2691 // default - 0, landscape - 1
2692 switch (getResources().getConfiguration().orientation) {
2693 case Configuration.ORIENTATION_LANDSCAPE:
2694 return 1;
2695 default:
2696 return 0;
2697 }
2698 }
2699
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002700 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002701 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002702 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002703 // Look for the toolbar icon specified in the activity meta-data
2704 Bundle metaData = packageManager.getActivityInfo(
2705 activityName, PackageManager.GET_META_DATA).metaData;
2706 if (metaData != null) {
2707 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2708 if (iconResId != 0) {
2709 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002710 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002711 }
2712 }
2713 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002714 // This can happen if the activity defines an invalid drawable
2715 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2716 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002717 } catch (Resources.NotFoundException nfe) {
2718 // This can happen if the activity defines an invalid drawable
2719 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2720 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002721 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002722 return null;
2723 }
2724
2725 // if successful in getting icon, return it; otherwise, set button to use default drawable
2726 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2727 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002728 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002729 Resources r = getResources();
2730 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2731 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002732
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002733 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002734 // If we were unable to find the icon via the meta-data, use a generic one
2735 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002736 toolbarIcon = r.getDrawable(fallbackDrawableId);
2737 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002738 if (button != null) {
2739 button.setCompoundDrawables(toolbarIcon, null, null, null);
2740 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002741 return null;
2742 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002743 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002744 if (button != null) {
2745 button.setCompoundDrawables(toolbarIcon, null, null, null);
2746 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002747 return toolbarIcon.getConstantState();
2748 }
2749 }
2750
2751 // if successful in getting icon, return it; otherwise, set button to use default drawable
2752 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2753 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2754 ImageView button = (ImageView) findViewById(buttonId);
2755 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2756
Michael Jurka19e0fc52011-07-22 18:00:21 -07002757 if (button != null) {
2758 // If we were unable to find the icon via the meta-data, use a
2759 // generic one
2760 if (toolbarIcon == null) {
2761 button.setImageResource(fallbackDrawableId);
2762 } else {
2763 button.setImageDrawable(toolbarIcon);
2764 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002765 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002766
2767 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2768
Michael Jurka0423dcf2010-10-05 14:56:18 -07002769 }
2770
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002771 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2772 TextView button = (TextView) findViewById(buttonId);
2773 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2774 }
2775
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002776 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002777 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002778 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002779 }
2780
Winson Chungbb185bd2011-11-21 12:31:42 -08002781 private void invalidatePressedFocusedStates(View container, View button) {
2782 if (container instanceof HolographicLinearLayout) {
2783 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2784 layout.invalidatePressedFocusedStates();
2785 } else if (button instanceof HolographicImageView) {
2786 HolographicImageView view = (HolographicImageView) button;
2787 view.invalidatePressedFocusedStates();
2788 }
2789 }
2790
Winson Chungc51db6a2011-10-05 11:44:49 -07002791 private boolean updateGlobalSearchIcon() {
2792 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2794 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002795 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002796 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002797 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002798
Winson Chung1cad91e2011-05-25 17:41:01 -07002799 final SearchManager searchManager =
2800 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2801 ComponentName activityName = searchManager.getGlobalSearchActivity();
2802 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002803 int coi = getCurrentOrientationIndexForGlobalIcons();
2804 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002805 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002806 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002807 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2808 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002809 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002810 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002811 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002812 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002813 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002814 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2815 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2816 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002817 voiceButton.setVisibility(View.GONE);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002818 voiceButtonProxy.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002819 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002820 }
2821 }
2822
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002823 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002824 final View searchButtonContainer = findViewById(R.id.search_button_container);
2825 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002826 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002827 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002828 }
2829
Winson Chungc51db6a2011-10-05 11:44:49 -07002830 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002831 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002832 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002833 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002834 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002835
Winson Chungc51db6a2011-10-05 11:44:49 -07002836 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002837 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2838 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002839 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002840 int coi = getCurrentOrientationIndexForGlobalIcons();
2841 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002842 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002843 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002844 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002845 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07002846 if (voiceButtonProxy != null) {
2847 voiceButtonProxy.setVisibility(View.VISIBLE);
2848 }
Winson Chungbb185bd2011-11-21 12:31:42 -08002849 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002850 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002851 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002852 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002853 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002854 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07002855 if (voiceButtonProxy != null) {
2856 voiceButtonProxy.setVisibility(View.GONE);
2857 }
Winson Chungc51db6a2011-10-05 11:44:49 -07002858 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002859 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002860 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002861
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002862 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002863 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2864 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002865 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002866 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002867 }
2868
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002869 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002870 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002871 */
2872 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002873 final View marketButton = findViewById(R.id.market_button);
2874 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2875 // Find the app market activity by resolving an intent.
2876 // (If multiple app markets are installed, it will return the ResolverActivity.)
2877 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002878 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002879 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002880 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002881 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002882 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002883 marketButton.setVisibility(View.VISIBLE);
2884 } else {
2885 // We should hide and disable the view so that we don't try and restore the visibility
2886 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2887 marketButton.setVisibility(View.GONE);
2888 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002889 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002890 }
2891
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002892 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002893 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002894 }
2895
Michael Jurkad7c28052012-04-27 15:43:36 -07002896 @Override
2897 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2898 boolean result = super.dispatchPopulateAccessibilityEvent(event);
2899 final List<CharSequence> text = event.getText();
2900 text.clear();
2901 text.add(getString(R.string.home));
2902 return result;
2903 }
2904
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002905 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002906 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002907 */
2908 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2909 @Override
2910 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002911 closeSystemDialogs();
2912 }
2913 }
2914
2915 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002916 * Receives notifications whenever the appwidgets are reset.
2917 */
2918 private class AppWidgetResetObserver extends ContentObserver {
2919 public AppWidgetResetObserver() {
2920 super(new Handler());
2921 }
2922
2923 @Override
2924 public void onChange(boolean selfChange) {
2925 onAppWidgetReset();
2926 }
2927 }
2928
2929 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002930 * If the activity is currently paused, signal that we need to re-run the loader
2931 * in onResume.
2932 *
2933 * This needs to be called from incoming places where resources might have been loaded
2934 * while we are paused. That is becaues the Configuration might be wrong
2935 * when we're not running, and if it comes back to what it was when we
2936 * were paused, we are not restarted.
2937 *
2938 * Implementation of the method from LauncherModel.Callbacks.
2939 *
2940 * @return true if we are currently paused. The caller might be able to
2941 * skip some work in that case since we will come back again.
2942 */
2943 public boolean setLoadOnResume() {
2944 if (mPaused) {
2945 Log.i(TAG, "setLoadOnResume");
2946 mOnResumeNeedsLoad = true;
2947 return true;
2948 } else {
2949 return false;
2950 }
2951 }
2952
2953 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002957 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002958 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002959 } else {
2960 return SCREEN_COUNT / 2;
2961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002963
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 /**
2965 * Refreshes the shortcuts shown on the workspace.
2966 *
2967 * Implementation of the method from LauncherModel.Callbacks.
2968 */
2969 public void startBinding() {
2970 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002971
Winson Chungf0c6ae02012-03-21 16:10:31 -07002972 mNewShortcutAnimatePage = -1;
2973 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002974 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 int count = workspace.getChildCount();
2976 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002977 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002978 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2979 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08002981 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002982 if (mHotseat != null) {
2983 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 }
2985 }
2986
2987 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 * Bind the items start-end from the list.
2989 *
2990 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002993 setLoadOnResume();
2994
Winson Chungf0c6ae02012-03-21 16:10:31 -07002995 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2996 Set<String> newApps = new HashSet<String>();
2997 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2998
2999 Workspace workspace = mWorkspace;
3000 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003002
3003 // Short circuit if we are loading dock items for a configuration which has no dock
3004 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3005 mHotseat == null) {
3006 continue;
3007 }
3008
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 switch (item.itemType) {
3010 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3011 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003012 ShortcutInfo info = (ShortcutInfo) item;
3013 String uri = info.intent.toUri(0).toString();
3014 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003015 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3016 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003017 if (newApps.contains(uri)) {
3018 newApps.remove(uri);
3019
3020 // Prepare the view to be animated up
3021 shortcut.setAlpha(0f);
3022 shortcut.setScaleX(0f);
3023 shortcut.setScaleY(0f);
3024 mNewShortcutAnimatePage = item.screen;
3025 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3026 mNewShortcutAnimateViews.add(shortcut);
3027 }
3028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003030 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003031 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003032 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003033 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3035 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003039
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 }
3042
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 /**
3044 * Implementation of the method from LauncherModel.Callbacks.
3045 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003046 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003047 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003048 sFolders.clear();
3049 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051
3052 /**
3053 * Add the views for a widget to the workspace.
3054 *
3055 * Implementation of the method from LauncherModel.Callbacks.
3056 */
3057 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003058 setLoadOnResume();
3059
Daniel Sandler843e8602010-06-07 14:59:01 -04003060 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3061 if (DEBUG_WIDGETS) {
3062 Log.d(TAG, "bindAppWidget: " + item);
3063 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 final Workspace workspace = mWorkspace;
3065
3066 final int appWidgetId = item.appWidgetId;
3067 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003068 if (DEBUG_WIDGETS) {
3069 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3070 }
3071
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3073
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3075 item.hostView.setTag(item);
3076
Winson Chung3d503fb2011-07-13 17:25:49 -07003077 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 item.cellY, item.spanX, item.spanY, false);
3079
Adam Cohended9f8d2010-11-03 13:25:16 -07003080 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3081
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 workspace.requestLayout();
3083
Daniel Sandler843e8602010-06-07 14:59:01 -04003084 if (DEBUG_WIDGETS) {
3085 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3086 + (SystemClock.uptimeMillis()-start) + "ms");
3087 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003088 }
3089
3090 /**
3091 * Callback saying that there aren't any more items to bind.
3092 *
3093 * Implementation of the method from LauncherModel.Callbacks.
3094 */
3095 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003096 setLoadOnResume();
3097
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 if (mSavedState != null) {
3099 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003100 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 mSavedState = null;
3103 }
3104
3105 if (mSavedInstanceState != null) {
3106 super.onRestoreInstanceState(mSavedInstanceState);
3107 mSavedInstanceState = null;
3108 }
3109
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003110 // If we received the result of any pending adds while the loader was running (e.g. the
3111 // widget configuration forced an orientation change), process them now.
3112 for (int i = 0; i < sPendingAddList.size(); i++) {
3113 completeAdd(sPendingAddList.get(i));
3114 }
3115 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116
Winson Chungc51db6a2011-10-05 11:44:49 -07003117 // Update the market app icon as necessary (the other icons will be managed in response to
3118 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003119 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003120
Winson Chungf0c6ae02012-03-21 16:10:31 -07003121 // Animate up any icons as necessary
3122 if (mVisible || mWorkspaceLoading) {
3123 Runnable newAppsRunnable = new Runnable() {
3124 @Override
3125 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003126 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003127 }
3128 };
Winson Chunga2413752012-04-03 14:22:34 -07003129
3130 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3131 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3132 if (canRunNewAppsAnimation()) {
3133 // If the user has not interacted recently, then either snap to the new page to show
3134 // the new-apps animation or just run them if they are to appear on the current page
3135 if (willSnapPage) {
3136 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3137 } else {
3138 runNewAppsAnimation(false);
3139 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003140 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003141 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003142 // are on another page (or just normally if they are added to the current page)
3143 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003144 }
3145 }
3146
3147 mWorkspaceLoading = false;
3148 }
3149
Winson Chunga2413752012-04-03 14:22:34 -07003150 private boolean canRunNewAppsAnimation() {
3151 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3152 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3153 }
3154
Winson Chungf0c6ae02012-03-21 16:10:31 -07003155 /**
3156 * Runs a new animation that scales up icons that were added while Launcher was in the
3157 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003158 *
3159 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003160 */
Winson Chunga2413752012-04-03 14:22:34 -07003161 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003162 AnimatorSet anim = new AnimatorSet();
3163 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003164
3165 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003166 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3167 @Override
3168 public int compare(View a, View b) {
3169 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3170 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3171 int cellCountX = LauncherModel.getCellCountX();
3172 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3173 }
3174 });
Winson Chunga2413752012-04-03 14:22:34 -07003175
3176 // Animate each of the views in place (or show them immediately if requested)
3177 if (immediate) {
3178 for (View v : mNewShortcutAnimateViews) {
3179 v.setAlpha(1f);
3180 v.setScaleX(1f);
3181 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003182 }
Winson Chunga2413752012-04-03 14:22:34 -07003183 } else {
3184 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3185 View v = mNewShortcutAnimateViews.get(i);
3186 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3187 PropertyValuesHolder.ofFloat("alpha", 1f),
3188 PropertyValuesHolder.ofFloat("scaleX", 1f),
3189 PropertyValuesHolder.ofFloat("scaleY", 1f));
3190 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3191 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3192 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3193 bounceAnims.add(bounceAnim);
3194 }
3195 anim.playTogether(bounceAnims);
3196 anim.addListener(new AnimatorListenerAdapter() {
3197 @Override
3198 public void onAnimationEnd(Animator animation) {
3199 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3200 }
3201 });
3202 anim.start();
3203 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003204
3205 // Clean up
3206 mNewShortcutAnimatePage = -1;
3207 mNewShortcutAnimateViews.clear();
3208 new Thread("clearNewAppsThread") {
3209 public void run() {
3210 mSharedPrefs.edit()
3211 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3212 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3213 .commit();
3214 }
3215 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003216 }
3217
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003218 @Override
3219 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003220 boolean searchVisible = updateGlobalSearchIcon();
3221 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3222 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003223 }
3224
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003225 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 * Add the icons for all apps.
3227 *
3228 * Implementation of the method from LauncherModel.Callbacks.
3229 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003230 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3231 // Remove the progress bar entirely; we could also make it GONE
3232 // but better to remove it since we know it's not going to be used
3233 View progressBar = mAppsCustomizeTabHost.
3234 findViewById(R.id.apps_customize_progress_bar);
3235 if (progressBar != null) {
3236 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003237 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003238 // We just post the call to setApps so the user sees the progress bar
3239 // disappear-- otherwise, it just looks like the progress bar froze
3240 // which doesn't look great
3241 mAppsCustomizeTabHost.post(new Runnable() {
3242 public void run() {
3243 if (mAppsCustomizeContent != null) {
3244 mAppsCustomizeContent.setApps(apps);
3245 }
3246 }
3247 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 }
3249
3250 /**
3251 * A package was installed.
3252 *
3253 * Implementation of the method from LauncherModel.Callbacks.
3254 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003255 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003256 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003257
Winson Chung785d2eb2011-04-14 16:08:02 -07003258 if (mAppsCustomizeContent != null) {
3259 mAppsCustomizeContent.addApps(apps);
3260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 }
3262
3263 /**
3264 * A package was updated.
3265 *
3266 * Implementation of the method from LauncherModel.Callbacks.
3267 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003268 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003269 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003270 if (mWorkspace != null) {
3271 mWorkspace.updateShortcuts(apps);
3272 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003273
Winson Chung785d2eb2011-04-14 16:08:02 -07003274 if (mAppsCustomizeContent != null) {
3275 mAppsCustomizeContent.updateApps(apps);
3276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 }
3278
3279 /**
3280 * A package was uninstalled.
3281 *
3282 * Implementation of the method from LauncherModel.Callbacks.
3283 */
Joe Onorato36115782010-06-17 13:28:48 -04003284 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003285 if (permanent) {
3286 mWorkspace.removeItems(apps);
3287 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003288
Winson Chung785d2eb2011-04-14 16:08:02 -07003289 if (mAppsCustomizeContent != null) {
3290 mAppsCustomizeContent.removeApps(apps);
3291 }
Winson Chunga1820962011-10-03 16:31:06 -07003292
3293 // Notify the drag controller
3294 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 }
Joe Onoratobe386092009-11-17 17:32:16 -08003296
3297 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003298 * A number of packages were updated.
3299 */
3300 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003301 if (mAppsCustomizeContent != null) {
3302 mAppsCustomizeContent.onPackagesUpdated();
3303 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003304 }
3305
Winson Chung400438b2011-01-16 17:53:48 -08003306 private int mapConfigurationOriActivityInfoOri(int configOri) {
3307 final Display d = getWindowManager().getDefaultDisplay();
3308 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3309 switch (d.getRotation()) {
3310 case Surface.ROTATION_0:
3311 case Surface.ROTATION_180:
3312 // We are currently in the same basic orientation as the natural orientation
3313 naturalOri = configOri;
3314 break;
3315 case Surface.ROTATION_90:
3316 case Surface.ROTATION_270:
3317 // We are currently in the other basic orientation to the natural orientation
3318 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3319 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3320 break;
3321 }
3322
3323 int[] oriMap = {
3324 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3325 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3326 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3327 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3328 };
3329 // Since the map starts at portrait, we need to offset if this device's natural orientation
3330 // is landscape.
3331 int indexOffset = 0;
3332 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3333 indexOffset = 1;
3334 }
3335 return oriMap[(d.getRotation() + indexOffset) % 4];
3336 }
Adam Cohen446e9402011-09-15 18:21:21 -07003337
Winson Chung641d71d2012-04-26 15:58:01 -07003338 public void lockScreenOrientation() {
3339 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3340 .getConfiguration().orientation));
Winson Chung400438b2011-01-16 17:53:48 -08003341 }
Winson Chung641d71d2012-04-26 15:58:01 -07003342 public void unlockScreenOrientation() {
3343 mHandler.postDelayed(new Runnable() {
3344 public void run() {
3345 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3346 }
3347 }, mRestoreScreenOrientationDelay);
Winson Chung400438b2011-01-16 17:53:48 -08003348 }
3349
Winson Chung82f55532011-08-09 14:14:23 -07003350 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003351 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003352 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003353 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003354
Winson Chung7d7541e2011-09-16 20:14:36 -07003355 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003356 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003357 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3358 Cling cling = (Cling) findViewById(clingId);
3359 if (cling != null) {
3360 cling.init(this, positionData);
3361 cling.setVisibility(View.VISIBLE);
3362 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3363 if (animate) {
3364 cling.buildLayer();
3365 cling.setAlpha(0f);
3366 cling.animate()
3367 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003368 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003369 .setDuration(SHOW_CLING_DURATION)
3370 .setStartDelay(delay)
3371 .start();
3372 } else {
3373 cling.setAlpha(1f);
3374 }
3375 }
3376 return cling;
3377 }
3378 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003379 if (cling != null) {
3380 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003381 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003382 anim.addListener(new AnimatorListenerAdapter() {
3383 public void onAnimationEnd(Animator animation) {
3384 cling.setVisibility(View.GONE);
3385 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003386 // We should update the shared preferences on a background thread
3387 new Thread("dismissClingThread") {
3388 public void run() {
3389 SharedPreferences.Editor editor = mSharedPrefs.edit();
3390 editor.putBoolean(flag, true);
3391 editor.commit();
3392 }
3393 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003394 };
3395 });
3396 anim.start();
3397 }
3398 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003399 private void removeCling(int id) {
3400 final View cling = findViewById(id);
3401 if (cling != null) {
3402 final ViewGroup parent = (ViewGroup) cling.getParent();
3403 parent.post(new Runnable() {
3404 @Override
3405 public void run() {
3406 parent.removeView(cling);
3407 }
3408 });
3409 }
3410 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003411 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003412 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003413 if (isClingsEnabled() &&
3414 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003415 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003416 } else {
3417 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003418 }
3419 }
3420 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003421 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003422 if (isClingsEnabled() &&
3423 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003424 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003425 } else {
3426 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003427 }
3428 }
3429 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003430 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003431 if (isClingsEnabled() &&
3432 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3433 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003434 } else {
3435 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003436 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003437 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003438 }
3439 public boolean isFolderClingVisible() {
3440 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003441 if (cling != null) {
3442 return cling.getVisibility() == View.VISIBLE;
3443 }
3444 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003445 }
Winson Chung82f55532011-08-09 14:14:23 -07003446 public void dismissWorkspaceCling(View v) {
3447 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003448 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003449 }
3450 public void dismissAllAppsCling(View v) {
3451 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003452 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3453 }
3454 public void dismissFolderCling(View v) {
3455 Cling cling = (Cling) findViewById(R.id.folder_cling);
3456 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003457 }
3458
Winson Chung80baf5a2010-08-09 16:03:15 -07003459 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003460 * Prints out out state for debugging.
3461 */
3462 public void dumpState() {
3463 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003464 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003465 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3466 Log.d(TAG, "mRestoring=" + mRestoring);
3467 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3468 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003469 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003470 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003471
Winson Chung785d2eb2011-04-14 16:08:02 -07003472 if (mAppsCustomizeContent != null) {
3473 mAppsCustomizeContent.dumpState();
3474 }
Joe Onoratobe386092009-11-17 17:32:16 -08003475 Log.d(TAG, "END launcher2 dump state");
3476 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003477
3478 @Override
3479 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3480 super.dump(prefix, fd, writer, args);
3481 writer.println(" ");
3482 writer.println("Debug logs: ");
3483 for (int i = 0; i < sDumpLogs.size(); i++) {
3484 writer.println(" " + sDumpLogs.get(i));
3485 }
3486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003487}
Michael Jurka2763be32011-02-24 11:19:57 -08003488
Michael Jurkaabded662011-03-04 12:06:57 -08003489interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003490 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003491 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003492 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003493 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003494 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003495}