blob: b3ce968849b3923f4e48a17d7bc57e59196a9d57 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040055import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Winson Chungf0c6ae02012-03-21 16:10:31 -070058import android.os.Debug;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Winson Chungf0c6ae02012-03-21 16:10:31 -070088import android.view.animation.BounceInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Adam Cohendf2cc412011-04-27 16:56:57 -070097import com.android.common.Search;
98import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070099import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800149 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
157 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
167
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700168 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
169
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800174 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700177 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
178 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700179 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700180 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800183 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800185 private final BroadcastReceiver mCloseSystemDialogsReceiver
186 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800187 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private LayoutInflater mInflater;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private View mQsbDivider;
193 private View mDockDivider;
194 private DragLayer mDragLayer;
195 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Michael Jurkac9d95c52011-08-29 14:03:34 -0700200 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 private int[] mTmpAddItemCellCoordinates = new int[2];
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private FolderInfo mFolderInfo;
204
Winson Chung3d503fb2011-07-13 17:25:49 -0700205 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800206 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700207
Winson Chungc51db6a2011-10-05 11:44:49 -0700208 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700209 private AppsCustomizeTabHost mAppsCustomizeTabHost;
210 private AppsCustomizePagedView mAppsCustomizeContent;
211 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private Bundle mSavedState;
214
215 private SpannableStringBuilder mDefaultKeySsb = null;
216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 private boolean mWorkspaceLoading = true;
218
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800219 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private boolean mRestoring;
221 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700222 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
224 private Bundle mSavedInstanceState;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800228 private boolean mUserPresent = true;
229 private boolean mVisible = false;
230 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700232 private static LocaleConfiguration sLocaleConfiguration = null;
233
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700234 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700236 private Intent mAppMarketIntent = null;
237
Adam Cohended9f8d2010-11-03 13:25:16 -0700238 // Related to the auto-advancing of widgets
239 private final int ADVANCE_MSG = 1;
240 private final int mAdvanceInterval = 20000;
241 private final int mAdvanceStagger = 250;
242 private long mAutoAdvanceSentTime;
243 private long mAutoAdvanceTimeLeft = -1;
244 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
245 new HashMap<View, AppWidgetProviderInfo>();
246
Winson Chung400438b2011-01-16 17:53:48 -0800247 // Determines how long to wait after a rotation before restoring the screen orientation to
248 // match the sensor state.
249 private final int mRestoreScreenOrientationDelay = 500;
250
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700252 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
253 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
254 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800255
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700256 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800257 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Winson Chungf0c6ae02012-03-21 16:10:31 -0700263 // Holds the page that we need to animate to, and the icon views that we need to animate up
264 // when we scroll to that page on resume.
265 private int mNewShortcutAnimatePage = -1;
266 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700267
Michael Jurkaddd62e92011-02-16 17:49:14 -0800268 private BubbleTextView mWaitingForResume;
269
Michael Jurkac1f5d262011-09-30 19:32:27 -0700270 private Runnable mBuildLayersRunnable = new Runnable() {
271 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700272 if (mWorkspace != null) {
273 mWorkspace.buildPageHardwareLayers();
274 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700275 }
276 };
277
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800278 private static ArrayList<PendingAddArguments> sPendingAddList
279 = new ArrayList<PendingAddArguments>();
280
281 private static class PendingAddArguments {
282 int requestCode;
283 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700284 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285 int screen;
286 int cellX;
287 int cellY;
288 }
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700294 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
295 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 mModel = app.setLauncher(this);
297 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400298 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700300
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200306 android.os.Debug.startMethodTracing(
307 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
310 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 setContentView(R.layout.launcher);
312 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700313 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800315 registerContentObservers();
316
Joe Onorato7c312c12009-08-13 21:36:53 -0700317 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 mSavedState = savedInstanceState;
320 restoreState(mSavedState);
321
Winson Chunga12a2502010-12-20 14:41:35 -0800322 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700323 if (mAppsCustomizeContent != null) {
324 mAppsCustomizeContent.onPackagesUpdated();
325 }
Winson Chunga12a2502010-12-20 14:41:35 -0800326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 if (PROFILE_STARTUP) {
328 android.os.Debug.stopMethodTracing();
329 }
330
331 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700332 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 }
334
Michael Jurkac57b7a82011-08-09 22:02:20 -0700335 if (!mModel.isAllAppsLoaded()) {
336 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
337 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 // For handling default keys
341 mDefaultKeySsb = new SpannableStringBuilder();
342 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800343
344 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
345 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800346
Winson Chungc51db6a2011-10-05 11:44:49 -0700347 boolean searchVisible = false;
348 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800349 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700350 int coi = getCurrentOrientationIndexForGlobalIcons();
351 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
352 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700353 updateAppMarketIcon();
354 searchVisible = updateGlobalSearchIcon();
355 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700356 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700357 if (sGlobalSearchIcon[coi] != null) {
358 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700359 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700361 if (sVoiceSearchIcon[coi] != null) {
362 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700363 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700364 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700365 if (sAppMarketIcon[coi] != null) {
366 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700368 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700369
Adam Cohen23a4d302011-12-01 17:26:44 -0800370 final String forceEnableRotation =
371 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
372
Adam Cohen446e9402011-09-15 18:21:21 -0700373 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800374 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700375 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
Romain Guycbb89e42009-06-08 15:52:54 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417
418 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
419 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700420 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 public void run() {
422 writeConfiguration(Launcher.this, localeConfiguration);
423 }
424 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700425 }
426 }
427
428 private static class LocaleConfiguration {
429 public String locale;
430 public int mcc = -1;
431 public int mnc = -1;
432 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700433
Romain Guy98d01652009-06-30 16:21:04 -0700434 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
435 DataInputStream in = null;
436 try {
437 in = new DataInputStream(context.openFileInput(PREFERENCES));
438 configuration.locale = in.readUTF();
439 configuration.mcc = in.readInt();
440 configuration.mnc = in.readInt();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 // Ignore
445 } finally {
446 if (in != null) {
447 try {
448 in.close();
449 } catch (IOException e) {
450 // Ignore
451 }
452 }
453 }
454 }
455
456 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
457 DataOutputStream out = null;
458 try {
459 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
460 out.writeUTF(configuration.locale);
461 out.writeInt(configuration.mcc);
462 out.writeInt(configuration.mnc);
463 out.flush();
464 } catch (FileNotFoundException e) {
465 // Ignore
466 } catch (IOException e) {
467 //noinspection ResultOfMethodCallIgnored
468 context.getFileStreamPath(PREFERENCES).delete();
469 } finally {
470 if (out != null) {
471 try {
472 out.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478 }
479
Adam Cohen716b51e2011-06-30 12:09:54 -0700480 public DragLayer getDragLayer() {
481 return mDragLayer;
482 }
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 static int getScreen() {
485 synchronized (sLock) {
486 return sScreen;
487 }
488 }
489
490 static void setScreen(int screen) {
491 synchronized (sLock) {
492 sScreen = screen;
493 }
494 }
495
Winson Chung557d6ed2011-07-08 15:34:52 -0700496 /**
497 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
498 * a configuration step, this allows the proper animations to run after other transitions.
499 */
500 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700501 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 switch (args.requestCode) {
503 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700504 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
505 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 break;
507 case REQUEST_PICK_SHORTCUT:
508 processShortcut(args.intent);
509 break;
510 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700511 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
512 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700513 result = true;
514 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_PICK_APPWIDGET:
516 addAppWidgetFromPick(args.intent);
517 break;
518 case REQUEST_CREATE_APPWIDGET:
519 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800520 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700521 result = true;
522 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 case REQUEST_PICK_WALLPAPER:
524 // We just wanted the activity result here so we can clear mWaitingForResult
525 break;
526 }
Michael Jurka27614d22012-04-02 06:40:22 -0700527 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
528 // if you turned the screen off and then back while in All Apps, Launcher would not
529 // return to the workspace. Clearing mAddInfo.container here fixes this issue
530 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800532 }
533
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800535 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700536 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800537 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
538 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700539 mWaitingForResult = false;
540
Adam Cohened66b2b2012-01-23 17:28:51 -0800541 // We have special handling for widgets
542 if (isWidgetDrop) {
543 int appWidgetId = data != null ?
544 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
545 completeTwoStageWidgetDrop(resultCode, appWidgetId);
546 return;
547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 // The pattern used here is that a user PICKs a specific application,
550 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
553 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700554 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800555 final PendingAddArguments args = new PendingAddArguments();
556 args.requestCode = requestCode;
557 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700558 args.container = mPendingAddInfo.container;
559 args.screen = mPendingAddInfo.screen;
560 args.cellX = mPendingAddInfo.cellX;
561 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800562 if (isWorkspaceLocked()) {
563 sPendingAddList.add(args);
564 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700565 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800568 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700569 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800570 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
571 null);
572 }
573
574 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
575 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
576 Runnable onCompleteRunnable = null;
577 int animationType = 0;
578
579 if (resultCode == RESULT_OK) {
580 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
581 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
582 mWidgetBeingConfigured.info);
583 mWidgetBeingConfigured.boundWidget = layout;
584 onCompleteRunnable = new Runnable() {
585 @Override
586 public void run() {
587 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
588 mPendingAddInfo.screen, layout, null);
589 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
590 null);
591 }
592 };
593 } else if (resultCode == RESULT_CANCELED) {
594 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
595 onCompleteRunnable = new Runnable() {
596 @Override
597 public void run() {
598 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
599 null);
600 }
601 };
602 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800603 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800604 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800605 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800606 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 }
608
609 @Override
610 protected void onResume() {
611 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700612
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800613 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700614 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400615 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700616 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400617 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700618 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
Winson Chunge4e50662012-01-23 14:45:13 -0800620
621 // Reset the pressed state of icons that were locked in the press state while activities
622 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800623 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800624 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800625 mWaitingForResume.setStayPressed(false);
626 }
Winson Chunge4e50662012-01-23 14:45:13 -0800627 if (mAppsCustomizeContent != null) {
628 // Resets the previous all apps icon press state
629 mAppsCustomizeContent.resetDrawableState();
630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
632
633 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634 protected void onPause() {
635 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700636 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800637 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700638 }
Romain Guycbb89e42009-06-08 15:52:54 -0700639
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700640 @Override
641 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400642 // Flag the loader to stop early before switching
643 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700644 if (mAppsCustomizeContent != null) {
645 mAppsCustomizeContent.surrender();
646 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800647 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700648 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700649
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800650 // We can't hide the IME if it was forced open. So don't bother
651 /*
652 @Override
653 public void onWindowFocusChanged(boolean hasFocus) {
654 super.onWindowFocusChanged(hasFocus);
655
656 if (hasFocus) {
657 final InputMethodManager inputManager = (InputMethodManager)
658 getSystemService(Context.INPUT_METHOD_SERVICE);
659 WindowManager.LayoutParams lp = getWindow().getAttributes();
660 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
661 android.os.Handler()) {
662 protected void onReceiveResult(int resultCode, Bundle resultData) {
663 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
664 }
665 });
666 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
667 }
668 }
669 */
670
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 private boolean acceptFilter() {
672 final InputMethodManager inputManager = (InputMethodManager)
673 getSystemService(Context.INPUT_METHOD_SERVICE);
674 return !inputManager.isFullscreenMode();
675 }
676
677 @Override
678 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700679 final int uniChar = event.getUnicodeChar();
680 final boolean handled = super.onKeyDown(keyCode, event);
681 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
682 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
684 keyCode, event);
685 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700686 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700687 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700688 // showSearchDialog()
689 // If there are multiple keystrokes before the search dialog takes focus,
690 // onSearchRequested() will be called for every keystroke,
691 // but it is idempotent, so it's fine.
692 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694 }
695
Joe Onorato8a9625e2010-01-28 15:55:35 -0800696 // Eat the long press event so the keyboard doesn't come up.
697 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
698 return true;
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 return handled;
702 }
703
Karl Rosaen138a0412009-04-23 19:00:21 -0700704 private String getTypedText() {
705 return mDefaultKeySsb.toString();
706 }
707
708 private void clearTypedText() {
709 mDefaultKeySsb.clear();
710 mDefaultKeySsb.clearSpans();
711 Selection.setSelection(mDefaultKeySsb, 0);
712 }
713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700715 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
716 * State
717 */
718 private static State intToState(int stateOrdinal) {
719 State state = State.WORKSPACE;
720 final State[] stateValues = State.values();
721 for (int i = 0; i < stateValues.length; i++) {
722 if (stateValues[i].ordinal() == stateOrdinal) {
723 state = stateValues[i];
724 break;
725 }
726 }
727 return state;
728 }
729
730 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 * Restores the previous state, if it exists.
732 *
733 * @param savedState The previous state.
734 */
735 private void restoreState(Bundle savedState) {
736 if (savedState == null) {
737 return;
738 }
739
Michael Jurka883f55b2010-10-21 15:47:14 -0700740 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700741 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800742 showAllApps(false);
743 }
744
Winson Chungf0c6ae02012-03-21 16:10:31 -0700745 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700747 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749
Winson Chung3d503fb2011-07-13 17:25:49 -0700750 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
751 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700752
Winson Chung3d503fb2011-07-13 17:25:49 -0700753 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
754 mPendingAddInfo.container = pendingAddContainer;
755 mPendingAddInfo.screen = pendingAddScreen;
756 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
757 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 mRestoring = true;
759 }
760
761 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
762 if (renameFolder) {
763 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700764 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 mRestoring = true;
766 }
Winson Chunga12a2502010-12-20 14:41:35 -0800767
Winson Chung785d2eb2011-04-14 16:08:02 -0700768
769 // Restore the AppsCustomize tab
770 if (mAppsCustomizeTabHost != null) {
771 String curTab = savedState.getString("apps_customize_currentTab");
772 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700773 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700774 mAppsCustomizeContent.setContentType(
775 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
776 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700777 mAppsCustomizeContent.loadAssociatedPages(
778 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700779 }
780
Winson Chung5afbf7b2011-07-25 11:53:08 -0700781 int currentIndex = savedState.getInt("apps_customize_currentIndex");
782 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
785
786 /**
787 * Finds all the views we need and configure them properly.
788 */
789 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700790 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400791
Winson Chung4c98d922011-05-31 16:50:48 -0700792 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
793 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800794 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
795 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796
Winson Chung4c98d922011-05-31 16:50:48 -0700797 // Setup the drag layer
798 mDragLayer.setup(this, dragController);
799
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 // Setup the hotseat
801 mHotseat = (Hotseat) findViewById(R.id.hotseat);
802 if (mHotseat != null) {
803 mHotseat.setup(this);
804 }
805
Winson Chung4c98d922011-05-31 16:50:48 -0700806 // Setup the workspace
807 mWorkspace.setHapticFeedbackEnabled(false);
808 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700809 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700810 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700811
Winson Chungf0ea4d32011-06-06 14:27:16 -0700812 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700813 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700814
Winson Chungf0ea4d32011-06-06 14:27:16 -0700815 // Setup AppsCustomize
816 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
817 findViewById(R.id.apps_customize_pane);
818 mAppsCustomizeContent = (AppsCustomizePagedView)
819 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
820 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400821
Michael Jurka5130e402011-10-13 04:55:35 -0700822 // Get the all apps button
823 mAllAppsButton = findViewById(R.id.all_apps_button);
824 if (mAllAppsButton != null) {
825 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
826 @Override
827 public boolean onTouch(View v, MotionEvent event) {
828 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
829 onTouchDownAllAppsButton(v);
830 }
831 return false;
832 }
833 });
834 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700835 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700836 dragController.setDragScoller(mWorkspace);
837 dragController.setScrollView(mDragLayer);
838 dragController.setMoveTarget(mWorkspace);
839 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700840 if (mSearchDropTargetBar != null) {
841 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
844
845 /**
846 * Creates a view representing a shortcut.
847 *
848 * @param info The data structure describing the shortcut.
849 *
850 * @return A View inflated from R.layout.application.
851 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800852 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700854 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
856
857 /**
858 * Creates a view representing a shortcut inflated from the specified resource.
859 *
860 * @param layoutResId The id of the XML layout used to create the shortcut.
861 * @param parent The group the shortcut belongs to.
862 * @param info The data structure describing the shortcut.
863 *
864 * @return A View inflated from layoutResId.
865 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800866 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800867 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
868 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 return favorite;
871 }
872
873 /**
874 * Add an application shortcut to the workspace.
875 *
876 * @param data The intent describing the application.
877 * @param cellInfo The position on screen where to create the shortcut.
878 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700879 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700882
Adam Cohenfbba09b2011-07-18 21:43:05 -0700883 // First we check if we already know the exact location where we want to add this item.
884 if (cellX >= 0 && cellY >= 0) {
885 cellXY[0] = cellX;
886 cellXY[1] = cellY;
887 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700888 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889 return;
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800892 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893
Romain Guy73b979d2009-06-09 12:57:21 -0700894 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800895 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800897 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700898 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700899 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800900 } else {
901 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903 }
Romain Guycbb89e42009-06-08 15:52:54 -0700904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 /**
906 * Add a shortcut to the workspace.
907 *
908 * @param data The intent describing the shortcut.
909 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
912 int cellY) {
913 int[] cellXY = mTmpAddItemCellCoordinates;
914 int[] touchXY = mPendingAddInfo.dropPos;
915 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700916
Michael Jurkad3ef3062010-11-23 16:23:58 -0800917 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700918
Adam Cohen558baaf2011-08-15 15:22:57 -0700919 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800920 if (info == null) {
921 return;
922 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700923 final View view = createShortcut(info);
924
Adam Cohenfbba09b2011-07-18 21:43:05 -0700925 // First we check if we already know the exact location where we want to add this item.
926 if (cellX >= 0 && cellY >= 0) {
927 cellXY[0] = cellX;
928 cellXY[1] = cellY;
929 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700930
931 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800932 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700933 true, null,null)) {
934 return;
935 }
936 DragObject dragObject = new DragObject();
937 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800938 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
939 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700940 return;
941 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700942 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800943 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700944 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800945 foundCellSpan = (result != null);
946 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700947 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800948 }
949
950 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700951 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700952 return;
953 }
954
Adam Cohen558baaf2011-08-15 15:22:57 -0700955 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956
957 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
959 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961 }
962
Adam Cohenf814aa02011-09-01 13:48:05 -0700963 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
964 if (spanXY == null) {
965 spanXY = new int[2];
966 }
967
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800968 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700969 // We want to account for the extra amount of padding that we are adding to the widget
970 // to ensure that it gets the full amount of space that it has requested
971 int requiredWidth = minWidth + padding.left + padding.right;
972 int requiredHeight = minHeight + padding.top + padding.bottom;
973 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
974 }
975
976 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
977 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
978 }
979
Adam Cohend41fbf52012-02-16 23:53:59 -0800980 int[] getMinSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
Adam Cohencbf47e32011-09-16 17:32:37 -0700981 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
982 }
983
Adam Cohenf814aa02011-09-01 13:48:05 -0700984 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
985 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
986 }
987
Adam Cohend41fbf52012-02-16 23:53:59 -0800988 int[] getMinSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
989 return getSpanForWidget(info.componentName, info.minResizeWidth,
990 info.minResizeHeight, spanXY);
991 }
992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700994 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700996 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700997 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800999 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1000 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1001 if (appWidgetInfo == null) {
1002 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1003 }
Romain Guycbb89e42009-06-08 15:52:54 -07001004
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001005 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001007
Adam Cohend41fbf52012-02-16 23:53:59 -08001008 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001009 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001012 // We have saved the position to which the widget was dragged-- this really only matters
1013 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 int[] cellXY = mTmpAddItemCellCoordinates;
1015 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001016 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1019 cellXY[0] = mPendingAddInfo.cellX;
1020 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001021 spanXY[0] = mPendingAddInfo.spanX;
1022 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001023 foundCellSpan = true;
1024 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001025 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001026 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001027 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1028 spanXY[1], cellXY, finalSpan);
1029 spanXY[0] = finalSpan[0];
1030 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001031 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001033 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001034 }
1035
Michael Jurka0280c3b2010-09-17 15:00:07 -07001036 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001037 if (appWidgetId != -1) {
1038 // Deleting an app widget ID is a void call but writes to disk before returning
1039 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001040 new Thread("deleteAppWidgetId") {
1041 public void run() {
1042 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1043 }
1044 }.start();
1045 }
Winson Chung93eef082012-03-23 15:59:27 -07001046 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001047 return;
1048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001051 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 launcherInfo.spanX = spanXY[0];
1053 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001054 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1055 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059
1060 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001061 if (hostView == null) {
1062 // Perform actual inflation because we're live
1063 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1064 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1065 } else {
1066 // The AppWidgetHostView has already been inflated and instantiated
1067 launcherInfo.hostView = hostView;
1068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001071 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001072 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001073 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001074
1075 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001077 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Adam Cohended9f8d2010-11-03 13:25:16 -07001080 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1081 @Override
1082 public void onReceive(Context context, Intent intent) {
1083 final String action = intent.getAction();
1084 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1085 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001086 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001087 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001088
Winson Chungc100e8e2011-08-09 16:02:43 -07001089 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001090 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001091 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1092 mAppsCustomizeTabHost.reset();
1093 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001094 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001095 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1096 mUserPresent = true;
1097 updateRunning();
1098 }
1099 }
1100 };
1101
1102 @Override
1103 public void onAttachedToWindow() {
1104 super.onAttachedToWindow();
1105
1106 // Listen for broadcasts related to user-presence
1107 final IntentFilter filter = new IntentFilter();
1108 filter.addAction(Intent.ACTION_SCREEN_OFF);
1109 filter.addAction(Intent.ACTION_USER_PRESENT);
1110 registerReceiver(mReceiver, filter);
1111
Adam Cohend113e0c2010-11-11 10:48:05 -08001112 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 mVisible = true;
1114 }
1115
1116 @Override
1117 public void onDetachedFromWindow() {
1118 super.onDetachedFromWindow();
1119 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001120 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001121
Adam Cohend113e0c2010-11-11 10:48:05 -08001122 if (mAttached) {
1123 unregisterReceiver(mReceiver);
1124 mAttached = false;
1125 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001126 updateRunning();
1127 }
1128
1129 public void onWindowVisibilityChanged(int visibility) {
1130 mVisible = visibility == View.VISIBLE;
1131 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001132 // The following code used to be in onResume, but it turns out onResume is called when
1133 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1134 // is a more appropriate event to handle
1135 if (mVisible) {
1136 mAppsCustomizeTabHost.onWindowVisible();
1137 if (!mWorkspaceLoading) {
1138 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001139 // We want to let Launcher draw itself at least once before we force it to build
1140 // layers on all the workspace pages, so that transitioning to Launcher from other
1141 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1142 // a true draw so we wait until the second preDraw call to be safe
1143 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001144 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001145 // We delay the layer building a bit in order to give
1146 // other message processing a time to run. In particular
1147 // this avoids a delay in hiding the IME if it was
1148 // currently shown, because doing that may involve
1149 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001150 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001151 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001152 return true;
1153 }
1154 });
1155 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001156 // When Launcher comes back to foreground, a different Activity might be responsible for
1157 // the app market intent, so refresh the icon
1158 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001159 clearTypedText();
1160 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001161 }
1162
1163 private void sendAdvanceMessage(long delay) {
1164 mHandler.removeMessages(ADVANCE_MSG);
1165 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1166 mHandler.sendMessageDelayed(msg, delay);
1167 mAutoAdvanceSentTime = System.currentTimeMillis();
1168 }
1169
1170 private void updateRunning() {
1171 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1172 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1173 mAutoAdvanceRunning = autoAdvanceRunning;
1174 if (autoAdvanceRunning) {
1175 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1176 sendAdvanceMessage(delay);
1177 } else {
1178 if (!mWidgetsToAdvance.isEmpty()) {
1179 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1180 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1181 }
1182 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001183 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 }
1185 }
1186 }
1187
1188 private final Handler mHandler = new Handler() {
1189 @Override
1190 public void handleMessage(Message msg) {
1191 if (msg.what == ADVANCE_MSG) {
1192 int i = 0;
1193 for (View key: mWidgetsToAdvance.keySet()) {
1194 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1195 final int delay = mAdvanceStagger * i;
1196 if (v instanceof Advanceable) {
1197 postDelayed(new Runnable() {
1198 public void run() {
1199 ((Advanceable) v).advance();
1200 }
1201 }, delay);
1202 }
1203 i++;
1204 }
1205 sendAdvanceMessage(mAdvanceInterval);
1206 }
1207 }
1208 };
1209
1210 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001211 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1213 if (v instanceof Advanceable) {
1214 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001215 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001216 updateRunning();
1217 }
1218 }
1219
1220 void removeWidgetToAutoAdvance(View hostView) {
1221 if (mWidgetsToAdvance.containsKey(hostView)) {
1222 mWidgetsToAdvance.remove(hostView);
1223 updateRunning();
1224 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 }
1226
Joe Onorato9c1289c2009-08-17 11:03:03 -04001227 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001228 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001229 launcherInfo.hostView = null;
1230 }
1231
Winson Chung93eef082012-03-23 15:59:27 -07001232 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1233 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1234 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001235 }
1236
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001237 public LauncherAppWidgetHost getAppWidgetHost() {
1238 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 }
Romain Guycbb89e42009-06-08 15:52:54 -07001240
Winson Chunga9abd0e2010-10-27 17:18:37 -07001241 public LauncherModel getModel() {
1242 return mModel;
1243 }
1244
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001245 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001246 getWindow().closeAllPanels();
1247
Winson Chung87acb482011-08-23 17:28:05 -07001248 /**
1249 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001250 try {
1251 dismissDialog(DIALOG_CREATE_SHORTCUT);
1252 // Unlock the workspace if the dialog was showing
1253 } catch (Exception e) {
1254 // An exception is thrown if the dialog is not visible, which is fine
1255 }
1256
1257 try {
1258 dismissDialog(DIALOG_RENAME_FOLDER);
1259 // Unlock the workspace if the dialog was showing
1260 } catch (Exception e) {
1261 // An exception is thrown if the dialog is not visible, which is fine
1262 }
Winson Chung87acb482011-08-23 17:28:05 -07001263 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001264
1265 // Whatever we were doing is hereby canceled.
1266 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 @Override
1270 protected void onNewIntent(Intent intent) {
1271 super.onNewIntent(intent);
1272
1273 // Close the menu
1274 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001275 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001276 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001277
Joe Onorato14f122b2009-11-19 14:06:36 -08001278 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1279 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001280
Adam Cohenac56cff2011-09-28 20:45:37 -07001281 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001282 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001283 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001284 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1285 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001286 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001287 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001288
1289 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001290 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001291 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001292
Joe Onorato3a8820b2009-11-10 15:06:42 -08001293 final View v = getWindow().peekDecorView();
1294 if (v != null && v.getWindowToken() != null) {
1295 InputMethodManager imm = (InputMethodManager)getSystemService(
1296 INPUT_METHOD_SERVICE);
1297 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001299
Michael Jurka35aa14d2011-07-07 17:01:08 -07001300 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001301 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001302 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305 }
1306
1307 @Override
1308 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1309 // Do not call super here
1310 mSavedInstanceState = savedInstanceState;
1311 }
1312
1313 @Override
1314 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001315 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001316 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317
Michael Jurka883f55b2010-10-21 15:47:14 -07001318 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001319 // We close any open folder since it will not be re-opened, and we need to make sure
1320 // this state is reflected.
1321 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1324 mWaitingForResult) {
1325 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1326 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1327 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1328 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 }
1330
1331 if (mFolderInfo != null && mWaitingForResult) {
1332 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1333 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1334 }
Michael Jurka946ad472010-07-09 18:05:18 -07001335
Winson Chung785d2eb2011-04-14 16:08:02 -07001336 // Save the current AppsCustomize tab
1337 if (mAppsCustomizeTabHost != null) {
1338 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1339 if (currentTabTag != null) {
1340 outState.putString("apps_customize_currentTab", currentTabTag);
1341 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001342 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1343 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 @Override
1348 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001350
Winson Chunge7a03942011-08-05 15:05:12 -07001351 // Remove all pending runnables
1352 mHandler.removeMessages(ADVANCE_MSG);
1353 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001354 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001355
Winson Chungcd2b0142011-06-08 16:02:26 -07001356 // Stop callbacks from LauncherModel
1357 LauncherApplication app = ((LauncherApplication) getApplication());
1358 mModel.stopLoader();
1359 app.setLauncher(null);
1360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001362 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001364 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001366 mAppWidgetHost = null;
1367
1368 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
1370 TextKeyListener.getInstance().release();
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372
Winson Chung3d503fb2011-07-13 17:25:49 -07001373 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001374
1375 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001376 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001377
1378 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1379 mWorkspace.removeAllViews();
1380 mWorkspace = null;
1381 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001382
1383 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
Adam Cohena9cf38f2011-05-02 15:36:58 -07001386 public DragController getDragController() {
1387 return mDragController;
1388 }
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 @Override
1391 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001392 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 super.startActivityForResult(intent, requestCode);
1394 }
1395
Winson Chung70d72102011-08-12 11:24:35 -07001396 /**
1397 * Indicates that we want global search for this activity by setting the globalSearch
1398 * argument for {@link #startSearch} to true.
1399 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001401 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001403
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001404 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001405
Karl Rosaen138a0412009-04-23 19:00:21 -07001406 if (initialQuery == null) {
1407 // Use any text typed in the launcher as the initial query
1408 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001409 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 if (appSearchData == null) {
1411 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001412 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001414 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001415
Karl Rosaen138a0412009-04-23 19:00:21 -07001416 final SearchManager searchManager =
1417 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001418 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001419 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421
Winson Chung70d72102011-08-12 11:24:35 -07001422 @Override
1423 public boolean onCreateOptionsMenu(Menu menu) {
1424 if (isWorkspaceLocked()) {
1425 return false;
1426 }
1427
1428 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001429
1430 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1431 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1432 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001433 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1434 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1435 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001436 String helpUrl = getString(R.string.help_url);
1437 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001438 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1439 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1440
Winson Chung70d72102011-08-12 11:24:35 -07001441 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1442 .setIcon(android.R.drawable.ic_menu_gallery)
1443 .setAlphabeticShortcut('W');
1444 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1445 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001446 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001447 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001448 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1449 .setIcon(android.R.drawable.ic_menu_preferences)
1450 .setIntent(settings)
1451 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001452 if (!helpUrl.isEmpty()) {
1453 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1454 .setIcon(android.R.drawable.ic_menu_help)
1455 .setIntent(help)
1456 .setAlphabeticShortcut('H');
1457 }
Winson Chung70d72102011-08-12 11:24:35 -07001458 return true;
1459 }
1460
1461 @Override
1462 public boolean onPrepareOptionsMenu(Menu menu) {
1463 super.onPrepareOptionsMenu(menu);
1464
1465 if (mAppsCustomizeTabHost.isTransitioning()) {
1466 return false;
1467 }
1468 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1469 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1470
1471 return true;
1472 }
1473
1474 @Override
1475 public boolean onOptionsItemSelected(MenuItem item) {
1476 switch (item.getItemId()) {
1477 case MENU_WALLPAPER_SETTINGS:
1478 startWallpaper();
1479 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001480 }
1481
1482 return super.onOptionsItemSelected(item);
1483 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001485 @Override
1486 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001487 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001488 // Use a custom animation for launching search
1489 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001490 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001491 }
1492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 public boolean isWorkspaceLocked() {
1494 return mWorkspaceLoading || mWaitingForResult;
1495 }
1496
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 mPendingAddInfo.container = ItemInfo.NO_ID;
1499 mPendingAddInfo.screen = -1;
1500 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1501 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001502 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001505
Michael Jurkaaf442092010-06-10 17:01:57 -07001506 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 // TODO: catch bad widget exception when sent
1508 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001509 // TODO: Is this log message meaningful?
1510 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001511 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001512 }
1513
Adam Cohened66b2b2012-01-23 17:28:51 -08001514 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1515 final AppWidgetProviderInfo appWidget = info.info;
1516 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001517 if (appWidget.configure != null) {
1518 // Launch over to configure widget, if needed
1519 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1520 intent.setComponent(appWidget.configure);
1521 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001522 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001523 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 intent.putExtra(InstallWidgetReceiver.
1525 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001526
1527 final String mimeType = info.mimeType;
1528 final ClipData clipData = (ClipData) info.configurationData;
1529 final ClipDescription clipDesc = clipData.getDescription();
1530 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1531 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001532 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001533 final CharSequence stringData = item.getText();
1534 final Uri uriData = item.getUri();
1535 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001536 final String key = InstallWidgetReceiver.
1537 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001538 if (uriData != null) {
1539 intent.putExtra(key, uriData);
1540 } else if (intentData != null) {
1541 intent.putExtra(key, intentData);
1542 } else if (stringData != null) {
1543 intent.putExtra(key, stringData);
1544 }
1545 break;
1546 }
1547 }
1548 }
Winson Chung55cef262010-10-28 14:14:18 -07001549 }
Romain Guy8e633c52010-03-04 12:51:36 -08001550 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001553 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001554 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001555 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001556 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
1558 }
Romain Guycbb89e42009-06-08 15:52:54 -07001559
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 /**
1561 * Process a shortcut drop.
1562 *
1563 * @param componentName The name of the component
1564 * @param screen The screen where it should be added
1565 * @param cell The cell it should be added to, optional
1566 * @param position The location on the screen where it was dropped, optional
1567 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1569 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001571 mPendingAddInfo.container = container;
1572 mPendingAddInfo.screen = screen;
1573 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574
1575 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001576 mPendingAddInfo.cellX = cell[0];
1577 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001578 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001579
1580 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1581 createShortcutIntent.setComponent(componentName);
1582 processShortcut(createShortcutIntent);
1583 }
1584
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 /**
1586 * Process a widget drop.
1587 *
1588 * @param info The PendingAppWidgetInfo of the widget being added.
1589 * @param screen The screen where it should be added
1590 * @param cell The cell it should be added to, optional
1591 * @param position The location on the screen where it was dropped, optional
1592 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001593 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001594 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001595 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001596 mPendingAddInfo.container = info.container = container;
1597 mPendingAddInfo.screen = info.screen = screen;
1598 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001599 mPendingAddInfo.minSpanX = info.minSpanX;
1600 mPendingAddInfo.minSpanY = info.minSpanY;
1601
Adam Cohenfbba09b2011-07-18 21:43:05 -07001602 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 mPendingAddInfo.cellX = cell[0];
1604 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001605 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001606 if (span != null) {
1607 mPendingAddInfo.spanX = span[0];
1608 mPendingAddInfo.spanY = span[1];
1609 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 AppWidgetHostView hostView = info.boundWidget;
1612 int appWidgetId;
1613 if (hostView != null) {
1614 appWidgetId = hostView.getAppWidgetId();
1615 } else {
1616 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1617 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1618 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001619 addAppWidgetImpl(appWidgetId, info);
1620 }
1621
Joe Onoratodeb98af2010-02-19 14:59:39 -08001622 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001623 // Handle case where user selected "Applications"
1624 String applicationName = getResources().getString(R.string.group_applications);
1625 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001627 if (applicationName != null && applicationName.equals(shortcutName)) {
1628 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1629 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001630
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001631 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1632 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001633 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001634 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001635 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001636 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639
Winson Chung24ab2f12010-09-16 14:10:47 -07001640 void processWallpaper(Intent intent) {
1641 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1642 }
1643
Winson Chung3d503fb2011-07-13 17:25:49 -07001644 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1645 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001646 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 folderInfo.title = getText(R.string.folder_name);
1648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001650 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1651 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001652 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
1654 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 FolderIcon newFolder =
1656 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1657 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1658 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001659 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 }
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001663 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 }
1665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001667 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001669 Intent chooser = Intent.createChooser(pickWallpaper,
1670 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001671 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1672 // Removed in Eclair MR1
1673// WallpaperManager wm = (WallpaperManager)
1674// getSystemService(Context.WALLPAPER_SERVICE);
1675// WallpaperInfo wi = wm.getWallpaperInfo();
1676// if (wi != null && wi.getSettingsActivity() != null) {
1677// LabeledIntent li = new LabeledIntent(getPackageName(),
1678// R.string.configure_wallpaper, 0);
1679// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1680// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1681// }
Mike Clerona0618e42009-10-22 13:55:21 -07001682 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
1684
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001685 /**
1686 * Registers various content observers. The current implementation registers
1687 * only a favorites observer to keep track of the favorites applications.
1688 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001689 private void registerContentObservers() {
1690 ContentResolver resolver = getContentResolver();
1691 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1692 true, mWidgetObserver);
1693 }
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 @Override
1696 public boolean dispatchKeyEvent(KeyEvent event) {
1697 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1698 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001700 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001701 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001702 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001703 dumpState();
1704 return true;
1705 }
1706 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001707 }
1708 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1709 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001710 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 return true;
1712 }
1713 }
1714
1715 return super.dispatchKeyEvent(event);
1716 }
1717
Joe Onorato88ec0992009-11-19 13:16:06 -08001718 @Override
1719 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001720 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001721 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001722 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001723 Folder openFolder = mWorkspace.getOpenFolder();
1724 if (openFolder.isEditingName()) {
1725 openFolder.dismissEditingName();
1726 } else {
1727 closeFolder();
1728 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001729 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001730 mWorkspace.exitWidgetResizeMode();
1731
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001732 // Back button is a no-op here, but give at least some feedback for the button press
1733 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001734 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001735 }
1736
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001738 * Re-listen when widgets are reset.
1739 */
1740 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001741 if (mAppWidgetHost != null) {
1742 mAppWidgetHost.startListening();
1743 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001744 }
1745
1746 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001747 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1748 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001750 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001751 if (mModel != null) {
1752 mModel.unbindWorkspaceItems();
1753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 /**
1757 * Launches the intent referred by the clicked shortcut.
1758 *
1759 * @param v The view representing the clicked shortcut.
1760 */
1761 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001762 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1763 // view has detached (it's possible for this to happen if the view is removed mid touch).
1764 if (v.getWindowToken() == null) {
1765 return;
1766 }
1767
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001768 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001769 return;
1770 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001773 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001775 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001776 int[] pos = new int[2];
1777 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001778 intent.setSourceBounds(new Rect(pos[0], pos[1],
1779 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001780 boolean success = startActivitySafely(intent, tag);
1781
1782 if (success && v instanceof BubbleTextView) {
1783 mWaitingForResume = (BubbleTextView) v;
1784 mWaitingForResume.setStayPressed(true);
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001787 if (v instanceof FolderIcon) {
1788 FolderIcon fi = (FolderIcon) v;
1789 handleFolderClick(fi);
1790 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001791 } else if (v == mAllAppsButton) {
1792 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001793 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001794 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001795 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798 }
1799
Michael Jurka0e260592010-06-30 17:07:39 -07001800 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001801 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001802 // clicking anywhere on the workspace causes the customization drawer to slide down
1803 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001804 return false;
1805 }
1806
Michael Jurkaaf442092010-06-10 17:01:57 -07001807 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001808 * Event handler for the search button
1809 *
1810 * @param v The view that was clicked.
1811 */
1812 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1814
Amith Yamasania135ba82011-08-09 17:42:01 -07001815 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001816 }
1817
1818 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001819 * Event handler for the voice button
1820 *
1821 * @param v The view that was clicked.
1822 */
1823 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001825
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001826 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001827 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001828 startActivity(intent);
1829 }
1830
1831 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001832 * Event handler for the "grid" button that appears on the home screen, which
1833 * enters all apps mode.
1834 *
1835 * @param v The view that was clicked.
1836 */
1837 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001838 showAllApps(true);
1839 }
1840
1841 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001842 // Provide the same haptic feedback that the system offers for virtual keys.
1843 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001844 }
1845
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001846 public void onClickAppMarketButton(View v) {
1847 if (mAppMarketIntent != null) {
1848 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001849 } else {
1850 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001851 }
1852 }
1853
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001854 void startApplicationDetailsActivity(ComponentName componentName) {
1855 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001856 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1857 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001858 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001859 startActivity(intent);
1860 }
1861
Patrick Dubroy5539af72010-09-07 15:22:01 -07001862 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1863 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1864 // System applications cannot be installed. For now, show a toast explaining that.
1865 // We may give them the option of disabling apps this way.
1866 int messageId = R.string.uninstall_system_app_text;
1867 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1868 } else {
1869 String packageName = appInfo.componentName.getPackageName();
1870 String className = appInfo.componentName.getClassName();
1871 Intent intent = new Intent(
1872 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001873 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1874 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001875 startActivity(intent);
1876 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001877 }
1878
Michael Jurkaddd62e92011-02-16 17:49:14 -08001879 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1881 try {
1882 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001883 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 } catch (ActivityNotFoundException e) {
1885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001886 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 } catch (SecurityException e) {
1888 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001889 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001891 "or use the exported attribute for this activity. "
1892 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001894 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001896
Romain Guy8e633c52010-03-04 12:51:36 -08001897 void startActivityForResultSafely(Intent intent, int requestCode) {
1898 try {
1899 startActivityForResult(intent, requestCode);
1900 } catch (ActivityNotFoundException e) {
1901 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1902 } catch (SecurityException e) {
1903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1904 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1905 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1906 "or use the exported attribute for this activity.", e);
1907 }
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909
Adam Cohena9cf38f2011-05-02 15:36:58 -07001910 private void handleFolderClick(FolderIcon folderIcon) {
1911 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001912 Folder openFolder = mWorkspace.getFolderForTag(info);
1913
1914 // If the folder info reports that the associated folder is open, then verify that
1915 // it is actually opened. There have been a few instances where this gets out of sync.
1916 if (info.opened && openFolder == null) {
1917 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1918 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1919 info.opened = false;
1920 }
1921
Adam Cohena9cf38f2011-05-02 15:36:58 -07001922 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // Close any open folder
1924 closeFolder();
1925 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001926 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 } else {
1928 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 int folderScreen;
1930 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001931 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 // .. and close it
1933 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001934 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 // Close any folder open on the current screen
1936 closeFolder();
1937 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001938 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940 }
1941 }
1942 }
1943
Adam Cohen2801caf2011-05-13 20:57:39 -07001944 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001945 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001946 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1947 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1948 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1949
Adam Cohenc51934b2011-07-26 21:07:43 -07001950 FolderInfo info = (FolderInfo) fi.getTag();
1951 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1952 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001953 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1954 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001955 }
1956
Adam Cohen2801caf2011-05-13 20:57:39 -07001957 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1958 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1959 oa.start();
1960 }
1961
1962 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001963 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001964 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1965 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1966 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1967
Adam Cohenc51934b2011-07-26 21:07:43 -07001968 FolderInfo info = (FolderInfo) fi.getTag();
1969 CellLayout cl = null;
1970 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1971 cl = (CellLayout) fi.getParent().getParent();
1972 }
1973
1974 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001975 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1976 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001977 oa.addListener(new AnimatorListenerAdapter() {
1978 @Override
1979 public void onAnimationEnd(Animator animation) {
1980 if (layout != null) {
1981 layout.clearFolderLeaveBehind();
1982 }
1983 }
1984 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001985 oa.start();
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001989 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 * is animated relative to the specified View. If the View is null, no animation
1991 * is played.
1992 *
1993 * @param folderInfo The FolderInfo describing the folder to open.
1994 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 public void openFolder(FolderIcon folderIcon) {
1996 Folder folder = folderIcon.mFolder;
1997 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002000 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
Adam Cohen4554ee12011-08-03 16:13:21 -07002002 // Just verify that the folder hasn't already been added to the DragLayer.
2003 // There was a one-off crash where the folder had a parent already.
2004 if (folder.getParent() == null) {
2005 mDragLayer.addView(folder);
2006 mDragController.addDropTarget((DropTarget) folder);
2007 } else {
2008 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2009 folder.getParent() + ").");
2010 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002012 }
2013
2014 public void closeFolder() {
2015 Folder folder = mWorkspace.getOpenFolder();
2016 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002017 if (folder.isEditingName()) {
2018 folder.dismissEditingName();
2019 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002020 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002021
2022 // Dismiss the folder cling
2023 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002024 }
2025 }
2026
2027 void closeFolder(Folder folder) {
2028 folder.getInfo().opened = false;
2029
2030 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2031 if (parent != null) {
2032 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2033 shrinkAndFadeInFolderIcon(fi);
2034 }
2035 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002039
Patrick Dubroye708c522011-03-01 16:03:43 -08002040 if (mState != State.WORKSPACE) {
2041 return false;
2042 }
2043
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 return false;
2046 }
2047
2048 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002049 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
2051
Michael Jurka0280c3b2010-09-17 15:00:07 -07002052 resetAddInfo();
2053 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002055 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 return true;
2057 }
2058
Winson Chung3d503fb2011-07-13 17:25:49 -07002059 // The hotseat touch handling does not go through Workspace, and we always allow long press
2060 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002061 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002062 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2063 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002064 if (itemUnderLongClick == null) {
2065 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2067 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002068 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002070 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002072 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074 }
2075 }
2076 return true;
2077 }
2078
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 boolean isHotseatLayout(View layout) {
2080 return mHotseat != null && layout != null &&
2081 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2082 }
2083 Hotseat getHotseat() {
2084 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002085 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002086 SearchDropTargetBar getSearchBar() {
2087 return mSearchDropTargetBar;
2088 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002089
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 /**
2091 * Returns the CellLayout of the specified container at the specified screen.
2092 */
2093 CellLayout getCellLayout(long container, int screen) {
2094 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2095 if (mHotseat != null) {
2096 return mHotseat.getLayout();
2097 } else {
2098 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002099 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 } else {
2101 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002102 }
2103 }
2104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 Workspace getWorkspace() {
2106 return mWorkspace;
2107 }
2108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 @Override
2110 protected Dialog onCreateDialog(int id) {
2111 switch (id) {
2112 case DIALOG_CREATE_SHORTCUT:
2113 return new CreateShortcut().createDialog();
2114 case DIALOG_RENAME_FOLDER:
2115 return new RenameFolder().createDialog();
2116 }
2117
2118 return super.onCreateDialog(id);
2119 }
2120
2121 @Override
2122 protected void onPrepareDialog(int id, Dialog dialog) {
2123 switch (id) {
2124 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 break;
2126 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002127 if (mFolderInfo != null) {
2128 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2129 final CharSequence text = mFolderInfo.title;
2130 input.setText(text);
2131 input.setSelection(0, text.length());
2132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 break;
2134 }
2135 }
2136
2137 void showRenameDialog(FolderInfo info) {
2138 mFolderInfo = info;
2139 mWaitingForResult = true;
2140 showDialog(DIALOG_RENAME_FOLDER);
2141 }
2142
Adam Cohenfbba09b2011-07-18 21:43:05 -07002143 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002144 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002145 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2146 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 mWaitingForResult = true;
2148 showDialog(DIALOG_CREATE_SHORTCUT);
2149 }
2150
2151 private class RenameFolder {
2152 private EditText mInput;
2153
2154 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2156 mInput = (EditText) layout.findViewById(R.id.folder_name);
2157
2158 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2159 builder.setIcon(0);
2160 builder.setTitle(getString(R.string.rename_folder_title));
2161 builder.setCancelable(true);
2162 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2163 public void onCancel(DialogInterface dialog) {
2164 cleanup();
2165 }
2166 });
2167 builder.setNegativeButton(getString(R.string.cancel_action),
2168 new Dialog.OnClickListener() {
2169 public void onClick(DialogInterface dialog, int which) {
2170 cleanup();
2171 }
2172 }
2173 );
2174 builder.setPositiveButton(getString(R.string.rename_action),
2175 new Dialog.OnClickListener() {
2176 public void onClick(DialogInterface dialog, int which) {
2177 changeFolderName();
2178 }
2179 }
2180 );
2181 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002182
2183 final AlertDialog dialog = builder.create();
2184 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2185 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002186 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002187 mInput.requestFocus();
2188 InputMethodManager inputManager = (InputMethodManager)
2189 getSystemService(Context.INPUT_METHOD_SERVICE);
2190 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002191 }
2192 });
2193
2194 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
2196
2197 private void changeFolderName() {
2198 final String name = mInput.getText().toString();
2199 if (!TextUtils.isEmpty(name)) {
2200 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002201 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 mFolderInfo.title = name;
2203 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2204
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002206 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002207 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 } else {
2209 final FolderIcon folderIcon = (FolderIcon)
2210 mWorkspace.getViewForTag(mFolderInfo);
2211 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002212 // TODO: At some point we'll probably want some version of setting
2213 // the text for a folder icon.
2214 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215 getWorkspace().requestLayout();
2216 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002217 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002219 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 }
2221 }
2222 }
2223 cleanup();
2224 }
2225
2226 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 dismissDialog(DIALOG_RENAME_FOLDER);
2228 mWaitingForResult = false;
2229 mFolderInfo = null;
2230 }
2231 }
2232
Daniel Sandler843e8602010-06-07 14:59:01 -04002233 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2234 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002235 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002236 }
2237
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002238 public boolean isAllAppsButtonRank(int rank) {
2239 return mHotseat.isAllAppsButtonRank(rank);
2240 }
2241
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002242 // AllAppsView.Watcher
2243 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002244 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002245 mWorkspace.setVisibility(View.GONE);
2246 }
2247 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002248
2249 /**
2250 * Helper method for the cameraZoomIn/cameraZoomOut animations
2251 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002252 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002253 * @param scaleFactor The scale factor used for the zoom
2254 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002255 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002256 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002257 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002258 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002259
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002260 void updateWallpaperVisibility(boolean visible) {
2261 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2262 int curflags = getWindow().getAttributes().flags
2263 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2264 if (wpflags != curflags) {
2265 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2266 }
2267 }
2268
Michael Jurkabed61d22012-02-14 22:51:29 -08002269 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2270 if (v instanceof LauncherTransitionable) {
2271 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2272 }
Winson Chung70442722012-02-10 15:43:22 -08002273
2274 // Update the workspace transition step as well
2275 dispatchOnLauncherTransitionStep(v, 0f);
2276 }
2277
2278 private void dispatchOnLauncherTransitionStep(View v, float t) {
2279 if (v instanceof LauncherTransitionable) {
2280 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2281 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002282 }
2283
2284 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2285 if (v instanceof LauncherTransitionable) {
2286 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2287 }
Winson Chung70442722012-02-10 15:43:22 -08002288
2289 // Update the workspace transition step as well
2290 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002291 }
2292
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002293 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002294 * Things to test when changing the following seven functions.
2295 * - Home from workspace
2296 * - from center screen
2297 * - from other screens
2298 * - Home from all apps
2299 * - from center screen
2300 * - from other screens
2301 * - Back from all apps
2302 * - from center screen
2303 * - from other screens
2304 * - Launch app from workspace and quit
2305 * - with back
2306 * - with home
2307 * - Launch app from all apps and quit
2308 * - with back
2309 * - with home
2310 * - Go to a screen that's not the default, then all
2311 * apps, and launch and app, and go back
2312 * - with back
2313 * -with home
2314 * - On workspace, long press power and go back
2315 * - with back
2316 * - with home
2317 * - On all apps, long press power and go back
2318 * - with back
2319 * - with home
2320 * - On workspace, power off
2321 * - On all apps, power off
2322 * - Launch an app and turn off the screen while in that app
2323 * - Go back with home key
2324 * - Go back with back key TODO: make this not go to workspace
2325 * - From all apps
2326 * - From workspace
2327 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2328 * - From all apps
2329 * - From the center workspace
2330 * - From another workspace
2331 */
2332
2333 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002334 * Zoom the camera out from the workspace to reveal 'toView'.
2335 * Assumes that the view to show is anchored at either the very top or very bottom
2336 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002337 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002338 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002339 if (mStateAnimation != null) {
2340 mStateAnimation.cancel();
2341 mStateAnimation = null;
2342 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002343 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002344 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002345
Winson Chungf0ea4d32011-06-06 14:27:16 -07002346 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2347 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2348 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002349 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002351 final int startDelay =
2352 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002353
Michael Jurkab3e22d92011-10-31 15:58:33 -07002354 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355
Michael Jurkad74c9842011-07-10 12:44:21 -07002356 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002357 Animator workspaceAnim =
2358 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002359
2360 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002361 toView.setScaleX(scale);
2362 toView.setScaleY(scale);
2363 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2364 scaleAnim.
2365 scaleX(1f).scaleY(1f).
2366 setDuration(duration).
2367 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002368
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002370 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002371 final ObjectAnimator alphaAnim = ObjectAnimator
2372 .ofFloat(toView, "alpha", 0f, 1f)
2373 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002375 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2376 @Override
2377 public void onAnimationUpdate(ValueAnimator animation) {
2378 float t = (Float) animation.getAnimatedValue();
2379 dispatchOnLauncherTransitionStep(fromView, t);
2380 dispatchOnLauncherTransitionStep(toView, t);
2381 }
2382 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002383
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002384 // toView should appear right at the end of the workspace shrink
2385 // animation
2386 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002388 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002389
2390 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002391 boolean animationCancelled = false;
2392
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002393 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002394 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002395 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002396 // Prepare the position
2397 toView.setTranslationX(0.0f);
2398 toView.setTranslationY(0.0f);
2399 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002400 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002401 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002402 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002403 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002404 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2405 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002406
2407 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2408 // Hide the workspace scrollbar
2409 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002410 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002411 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002412 if (!animationCancelled) {
2413 updateWallpaperVisibility(false);
2414 }
2415 }
2416
Adam Cohencff6af82011-09-13 14:51:53 -07002417 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002418 public void onAnimationCancel(Animator animation) {
2419 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002420 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002421 });
2422
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002423 if (workspaceAnim != null) {
2424 mStateAnimation.play(workspaceAnim);
2425 }
Michael Jurka1899a362011-11-03 13:50:45 -07002426
2427 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002428 final ViewTreeObserver observer;
2429
Michael Jurkabed61d22012-02-14 22:51:29 -08002430 dispatchOnLauncherTransitionStart(fromView, animated, false);
2431 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002432
2433 // If any of the objects being animated haven't been measured/laid out
2434 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002435 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002436 (mWorkspace.getMeasuredWidth() == 0) ||
2437 (toView.getMeasuredWidth() == 0)) {
2438 observer = mWorkspace.getViewTreeObserver();
2439 delayAnim = true;
2440 } else {
2441 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002442 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002443
2444 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002445 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002446 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2447 public void onGlobalLayout() {
2448 mWorkspace.post(new Runnable() {
2449 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002450 // Check that mStateAnimation hasn't changed while
2451 // we waited for a layout pass
2452 if (mStateAnimation == stateAnimation) {
2453 // Need to update pivots for zoom if layout changed
2454 setPivotsForZoom(toView, scale);
2455 mStateAnimation.start();
2456 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002457 }
2458 });
2459 observer.removeGlobalOnLayoutListener(this);
2460 }
2461 };
2462 observer.addOnGlobalLayoutListener(delayedStart);
2463 } else {
2464 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002465 mStateAnimation.start();
2466 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002467 } else {
2468 toView.setTranslationX(0.0f);
2469 toView.setTranslationY(0.0f);
2470 toView.setScaleX(1.0f);
2471 toView.setScaleY(1.0f);
2472 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002473 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002474
Michael Jurkabed61d22012-02-14 22:51:29 -08002475 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2476 // Hide the workspace scrollbar
2477 mWorkspace.hideScrollingIndicator(true);
2478 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002479 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002480 dispatchOnLauncherTransitionStart(fromView, animated, false);
2481 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2482 dispatchOnLauncherTransitionStart(toView, animated, false);
2483 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002484 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002485 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002486 }
2487
2488 /**
2489 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002490 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002491 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002492 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002493 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2494 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002495
Michael Jurkab3e22d92011-10-31 15:58:33 -07002496 if (mStateAnimation != null) {
2497 mStateAnimation.cancel();
2498 mStateAnimation = null;
2499 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002500 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002501
Winson Chungf0ea4d32011-06-06 14:27:16 -07002502 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002503 final int fadeOutDuration =
2504 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002505 final float scaleFactor = (float)
2506 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2507 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002508 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002509 Animator workspaceAnim = null;
2510
2511 if (toState == State.WORKSPACE) {
2512 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2513 workspaceAnim = mWorkspace.getChangeStateAnimation(
2514 Workspace.State.NORMAL, animated, stagger);
2515 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2516 workspaceAnim = mWorkspace.getChangeStateAnimation(
2517 Workspace.State.SPRING_LOADED, animated);
2518 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002519
Michael Jurkab3e22d92011-10-31 15:58:33 -07002520 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002521 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002522 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002524 final float oldScaleX = fromView.getScaleX();
2525 final float oldScaleY = fromView.getScaleY();
2526
Michael Jurka159b4cc2012-01-17 03:00:35 -08002527 final LauncherViewPropertyAnimator scaleAnim =
2528 new LauncherViewPropertyAnimator(fromView);
2529 scaleAnim.
2530 scaleX(scaleFactor).scaleY(scaleFactor).
2531 setDuration(duration).
2532 setInterpolator(new Workspace.ZoomInInterpolator());
2533
2534 final ObjectAnimator alphaAnim = ObjectAnimator
2535 .ofFloat(fromView, "alpha", 1f, 0f)
2536 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002537 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002538 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2539 @Override
2540 public void onAnimationUpdate(ValueAnimator animation) {
2541 float t = 1f - (Float) animation.getAnimatedValue();
2542 dispatchOnLauncherTransitionStep(fromView, t);
2543 dispatchOnLauncherTransitionStep(toView, t);
2544 }
2545 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002546
Michael Jurkabed61d22012-02-14 22:51:29 -08002547 mStateAnimation = new AnimatorSet();
2548
2549 dispatchOnLauncherTransitionStart(fromView, animated, true);
2550 dispatchOnLauncherTransitionStart(toView, animated, true);
2551
2552 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002553 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002554 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002555 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002556 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002557 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2558 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002559 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002560 if (onCompleteRunnable != null) {
2561 onCompleteRunnable.run();
2562 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002563 }
2564 });
2565
Winson Chungf0ea4d32011-06-06 14:27:16 -07002566 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002567 if (workspaceAnim != null) {
2568 mStateAnimation.play(workspaceAnim);
2569 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002570 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 } else {
2572 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002573 dispatchOnLauncherTransitionStart(fromView, animated, true);
2574 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2575 dispatchOnLauncherTransitionStart(toView, animated, true);
2576 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002577 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002579 }
2580
Michael Jurkae326f182011-11-21 14:05:46 -08002581 @Override
2582 public void onTrimMemory(int level) {
2583 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002584 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002585 mAppsCustomizeTabHost.onTrimMemory();
2586 }
2587 }
2588
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002589 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002590 showWorkspace(animated, null);
2591 }
2592
2593 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002594 if (mState != State.WORKSPACE) {
2595 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002596 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002597
2598 // Show the search bar and hotseat
2599 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002600 // We only need to animate in the dock divider if we're going from spring loaded mode
2601 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002602
2603 // Set focus to the AppsCustomize button
2604 if (mAllAppsButton != null) {
2605 mAllAppsButton.requestFocus();
2606 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002607 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002608
Michael Jurkab737ee62011-11-15 15:57:22 -08002609 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002610
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002611 // Change the state *after* we've called all the transition code
2612 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002613
Winson Chung5fb63472011-02-02 17:03:37 -08002614 // Resume the auto-advance of widgets
2615 mUserPresent = true;
2616 updateRunning();
2617
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002618 // send an accessibility event to announce the context change
2619 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002620 }
2621
Michael Jurkab3e22d92011-10-31 15:58:33 -07002622 void showAllApps(boolean animated) {
2623 if (mState != State.WORKSPACE) return;
2624
2625 showAppsCustomizeHelper(animated, false);
2626 mAppsCustomizeTabHost.requestFocus();
2627
2628 // Hide the search bar and hotseat
2629 mSearchDropTargetBar.hideSearchBar(animated);
2630
2631 // Change the state *after* we've called all the transition code
2632 mState = State.APPS_CUSTOMIZE;
2633
2634 // Pause the auto-advance of widgets until we are out of AllApps
2635 mUserPresent = false;
2636 updateRunning();
2637 closeFolder();
2638
2639 // Send an accessibility event to announce the context change
2640 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2641 }
2642
Adam Cohen7777d962011-08-18 18:58:38 -07002643 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002644 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002645 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002646 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002647 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002648 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002649 }
Adam Cohen7777d962011-08-18 18:58:38 -07002650
Adam Cohened66b2b2012-01-23 17:28:51 -08002651 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2652 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002653 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2654
Winson Chunge7a03942011-08-05 15:05:12 -07002655 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002656 @Override
2657 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002658 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002659 // Before we show workspace, hide all apps again because
2660 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2661 // clean up our state transition functions
2662 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002663 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002664 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002665 } else {
2666 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002667 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002668 }
2669 }, (extendedDelay ?
2670 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2671 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2672 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002673
Michael Jurkad3ef3062010-11-23 16:23:58 -08002674 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002675 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002676 final boolean animated = true;
2677 final boolean springLoaded = true;
2678 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002679 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002680 }
2681 // Otherwise, we are not in spring loaded mode, so don't do anything.
2682 }
2683
Michael Jurkab737ee62011-11-15 15:57:22 -08002684 void hideDockDivider() {
2685 if (mQsbDivider != null && mDockDivider != null) {
2686 mQsbDivider.setVisibility(View.INVISIBLE);
2687 mDockDivider.setVisibility(View.INVISIBLE);
2688 }
2689 }
2690
2691 void showDockDivider(boolean animated) {
2692 if (mQsbDivider != null && mDockDivider != null) {
2693 mQsbDivider.setVisibility(View.VISIBLE);
2694 mDockDivider.setVisibility(View.VISIBLE);
2695 if (mDividerAnimator != null) {
2696 mDividerAnimator.cancel();
2697 mQsbDivider.setAlpha(1f);
2698 mDockDivider.setAlpha(1f);
2699 mDividerAnimator = null;
2700 }
2701 if (animated) {
2702 mDividerAnimator = new AnimatorSet();
2703 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2704 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2705 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2706 mDividerAnimator.start();
2707 }
2708 }
2709 }
2710
Michael Jurkab3e22d92011-10-31 15:58:33 -07002711 void lockAllApps() {
2712 // TODO
2713 }
2714
2715 void unlockAllApps() {
2716 // TODO
2717 }
2718
Adam Cohenfc53cd22011-07-20 15:45:11 -07002719 public boolean isAllAppsCustomizeOpen() {
2720 return mState == State.APPS_CUSTOMIZE;
2721 }
2722
Winson Chungf0ea4d32011-06-06 14:27:16 -07002723 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002724 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002725 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002726 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002727 if (!LauncherApplication.isScreenLarge()) {
2728 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002729 if (mHotseat.getAlpha() != 1f) {
2730 int duration = mSearchDropTargetBar.getTransitionInDuration();
2731 mHotseat.animate().alpha(1f).setDuration(duration);
2732 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002733 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002734 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002735 }
2736 }
2737 }
2738
2739 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002740 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002741 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002742 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002743 if (!LauncherApplication.isScreenLarge()) {
2744 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002745 if (mHotseat.getAlpha() != 0f) {
2746 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2747 mHotseat.animate().alpha(0f).setDuration(duration);
2748 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002749 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002750 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002752 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002753 }
2754
Patrick Dubroy5f445422011-02-18 14:35:21 -08002755 /**
2756 * Add an item from all apps or customize onto the given workspace screen.
2757 * If layout is null, add to the current screen.
2758 */
2759 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002760 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002761 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002762 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002763 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002764
Winson Chungdff8ebb2011-09-08 17:25:31 -07002765 /** Maps the current orientation to an index for referencing orientation correct global icons */
2766 private int getCurrentOrientationIndexForGlobalIcons() {
2767 // default - 0, landscape - 1
2768 switch (getResources().getConfiguration().orientation) {
2769 case Configuration.ORIENTATION_LANDSCAPE:
2770 return 1;
2771 default:
2772 return 0;
2773 }
2774 }
2775
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002776 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002777 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002778 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779 // Look for the toolbar icon specified in the activity meta-data
2780 Bundle metaData = packageManager.getActivityInfo(
2781 activityName, PackageManager.GET_META_DATA).metaData;
2782 if (metaData != null) {
2783 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2784 if (iconResId != 0) {
2785 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002786 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002787 }
2788 }
2789 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002790 // This can happen if the activity defines an invalid drawable
2791 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2792 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002793 } catch (Resources.NotFoundException nfe) {
2794 // This can happen if the activity defines an invalid drawable
2795 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2796 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002797 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002798 return null;
2799 }
2800
2801 // if successful in getting icon, return it; otherwise, set button to use default drawable
2802 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2803 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002804 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002805 Resources r = getResources();
2806 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2807 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002808
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002809 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002810 // If we were unable to find the icon via the meta-data, use a generic one
2811 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002812 toolbarIcon = r.getDrawable(fallbackDrawableId);
2813 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002814 if (button != null) {
2815 button.setCompoundDrawables(toolbarIcon, null, null, null);
2816 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002817 return null;
2818 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002819 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002820 if (button != null) {
2821 button.setCompoundDrawables(toolbarIcon, null, null, null);
2822 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002823 return toolbarIcon.getConstantState();
2824 }
2825 }
2826
2827 // if successful in getting icon, return it; otherwise, set button to use default drawable
2828 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2829 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2830 ImageView button = (ImageView) findViewById(buttonId);
2831 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2832
Michael Jurka19e0fc52011-07-22 18:00:21 -07002833 if (button != null) {
2834 // If we were unable to find the icon via the meta-data, use a
2835 // generic one
2836 if (toolbarIcon == null) {
2837 button.setImageResource(fallbackDrawableId);
2838 } else {
2839 button.setImageDrawable(toolbarIcon);
2840 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002841 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002842
2843 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2844
Michael Jurka0423dcf2010-10-05 14:56:18 -07002845 }
2846
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002847 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2848 TextView button = (TextView) findViewById(buttonId);
2849 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2850 }
2851
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002852 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002853 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002854 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002855 }
2856
Winson Chungbb185bd2011-11-21 12:31:42 -08002857 private void invalidatePressedFocusedStates(View container, View button) {
2858 if (container instanceof HolographicLinearLayout) {
2859 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2860 layout.invalidatePressedFocusedStates();
2861 } else if (button instanceof HolographicImageView) {
2862 HolographicImageView view = (HolographicImageView) button;
2863 view.invalidatePressedFocusedStates();
2864 }
2865 }
2866
Winson Chungc51db6a2011-10-05 11:44:49 -07002867 private boolean updateGlobalSearchIcon() {
2868 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002869 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2870 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002871 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002872 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002873
Winson Chung1cad91e2011-05-25 17:41:01 -07002874 final SearchManager searchManager =
2875 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2876 ComponentName activityName = searchManager.getGlobalSearchActivity();
2877 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002878 int coi = getCurrentOrientationIndexForGlobalIcons();
2879 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002880 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002881 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2883 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002884 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002885 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002886 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002887 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002888 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002889 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2890 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2891 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002892 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002893 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002894 }
2895 }
2896
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002897 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002898 final View searchButtonContainer = findViewById(R.id.search_button_container);
2899 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002900 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002901 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002902 }
2903
Winson Chungc51db6a2011-10-05 11:44:49 -07002904 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002905 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002906 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002907 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002908
Winson Chungc51db6a2011-10-05 11:44:49 -07002909 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002910 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2911 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002912 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002913 int coi = getCurrentOrientationIndexForGlobalIcons();
2914 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002915 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002916 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002917 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002918 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002919 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002920 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002921 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002922 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002923 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002924 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002925 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002926 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002927 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002928
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002929 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002930 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2931 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002932 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002933 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002934 }
2935
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002936 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002937 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002938 */
2939 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002940 final View marketButton = findViewById(R.id.market_button);
2941 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2942 // Find the app market activity by resolving an intent.
2943 // (If multiple app markets are installed, it will return the ResolverActivity.)
2944 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002945 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002946 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002947 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002948 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002949 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002950 marketButton.setVisibility(View.VISIBLE);
2951 } else {
2952 // We should hide and disable the view so that we don't try and restore the visibility
2953 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2954 marketButton.setVisibility(View.GONE);
2955 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002956 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002957 }
2958
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002959 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002960 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002961 }
2962
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002963 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 * Displays the shortcut creation dialog and launches, if necessary, the
2965 * appropriate activity.
2966 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002967 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002968 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2969 DialogInterface.OnShowListener {
2970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002975
Winson Chung55b65502011-05-26 12:03:43 -07002976 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2977 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002978 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 AlertDialog dialog = builder.create();
2981 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002982 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002983 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 return dialog;
2986 }
2987
2988 public void onCancel(DialogInterface dialog) {
2989 mWaitingForResult = false;
2990 cleanup();
2991 }
2992
Romain Guycbb89e42009-06-08 15:52:54 -07002993 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002994 mWaitingForResult = false;
2995 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002996 }
2997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002998 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002999 try {
3000 dismissDialog(DIALOG_CREATE_SHORTCUT);
3001 } catch (Exception e) {
3002 // An exception is thrown if the dialog is not visible, which is fine
3003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 }
3005
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003006 /**
3007 * Handle the action clicked in the "Add to home" dialog.
3008 */
3009 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003010 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003011
Winson Chung55b65502011-05-26 12:03:43 -07003012 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3013 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003014 case AddAdapter.ITEM_APPLICATION: {
3015 if (mAppsCustomizeTabHost != null) {
3016 mAppsCustomizeTabHost.selectAppsTab();
3017 }
3018 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003019 break;
3020 }
Winson Chung55b65502011-05-26 12:03:43 -07003021 case AddAdapter.ITEM_APPWIDGET: {
3022 if (mAppsCustomizeTabHost != null) {
3023 mAppsCustomizeTabHost.selectWidgetsTab();
3024 }
3025 showAllApps(true);
3026 break;
3027 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003028 case AddAdapter.ITEM_WALLPAPER: {
3029 startWallpaper();
3030 break;
3031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 }
3033 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003034
3035 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003036 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039
3040 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003041 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003042 */
3043 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3044 @Override
3045 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003046 closeSystemDialogs();
3047 }
3048 }
3049
3050 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003051 * Receives notifications whenever the appwidgets are reset.
3052 */
3053 private class AppWidgetResetObserver extends ContentObserver {
3054 public AppWidgetResetObserver() {
3055 super(new Handler());
3056 }
3057
3058 @Override
3059 public void onChange(boolean selfChange) {
3060 onAppWidgetReset();
3061 }
3062 }
3063
3064 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003065 * If the activity is currently paused, signal that we need to re-run the loader
3066 * in onResume.
3067 *
3068 * This needs to be called from incoming places where resources might have been loaded
3069 * while we are paused. That is becaues the Configuration might be wrong
3070 * when we're not running, and if it comes back to what it was when we
3071 * were paused, we are not restarted.
3072 *
3073 * Implementation of the method from LauncherModel.Callbacks.
3074 *
3075 * @return true if we are currently paused. The caller might be able to
3076 * skip some work in that case since we will come back again.
3077 */
3078 public boolean setLoadOnResume() {
3079 if (mPaused) {
3080 Log.i(TAG, "setLoadOnResume");
3081 mOnResumeNeedsLoad = true;
3082 return true;
3083 } else {
3084 return false;
3085 }
3086 }
3087
3088 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003092 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003093 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003094 } else {
3095 return SCREEN_COUNT / 2;
3096 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003098
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 /**
3100 * Refreshes the shortcuts shown on the workspace.
3101 *
3102 * Implementation of the method from LauncherModel.Callbacks.
3103 */
3104 public void startBinding() {
3105 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003106
Winson Chungf0c6ae02012-03-21 16:10:31 -07003107 mNewShortcutAnimatePage = -1;
3108 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003109 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 int count = workspace.getChildCount();
3111 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003112 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003113 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3114 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003116 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003117 if (mHotseat != null) {
3118 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 }
3120 }
3121
3122 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 * Bind the items start-end from the list.
3124 *
3125 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003128 setLoadOnResume();
3129
Winson Chungf0c6ae02012-03-21 16:10:31 -07003130 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3131 Set<String> newApps = new HashSet<String>();
3132 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3133
3134 Workspace workspace = mWorkspace;
3135 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003137
3138 // Short circuit if we are loading dock items for a configuration which has no dock
3139 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3140 mHotseat == null) {
3141 continue;
3142 }
3143
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 switch (item.itemType) {
3145 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3146 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003147 ShortcutInfo info = (ShortcutInfo) item;
3148 String uri = info.intent.toUri(0).toString();
3149 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003150 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3151 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003152 if (newApps.contains(uri)) {
3153 newApps.remove(uri);
3154
3155 // Prepare the view to be animated up
3156 shortcut.setAlpha(0f);
3157 shortcut.setScaleX(0f);
3158 shortcut.setScaleY(0f);
3159 mNewShortcutAnimatePage = item.screen;
3160 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3161 mNewShortcutAnimateViews.add(shortcut);
3162 }
3163 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003165 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003167 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003168 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3170 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003174
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 }
3177
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 /**
3179 * Implementation of the method from LauncherModel.Callbacks.
3180 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003181 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003182 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003183 sFolders.clear();
3184 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186
3187 /**
3188 * Add the views for a widget to the workspace.
3189 *
3190 * Implementation of the method from LauncherModel.Callbacks.
3191 */
3192 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003193 setLoadOnResume();
3194
Daniel Sandler843e8602010-06-07 14:59:01 -04003195 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3196 if (DEBUG_WIDGETS) {
3197 Log.d(TAG, "bindAppWidget: " + item);
3198 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003199 final Workspace workspace = mWorkspace;
3200
3201 final int appWidgetId = item.appWidgetId;
3202 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003203 if (DEBUG_WIDGETS) {
3204 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3205 }
3206
Joe Onorato9c1289c2009-08-17 11:03:03 -04003207 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3208
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3210 item.hostView.setTag(item);
3211
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 item.cellY, item.spanX, item.spanY, false);
3214
Adam Cohended9f8d2010-11-03 13:25:16 -07003215 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3216
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217 workspace.requestLayout();
3218
Daniel Sandler843e8602010-06-07 14:59:01 -04003219 if (DEBUG_WIDGETS) {
3220 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3221 + (SystemClock.uptimeMillis()-start) + "ms");
3222 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 }
3224
3225 /**
3226 * Callback saying that there aren't any more items to bind.
3227 *
3228 * Implementation of the method from LauncherModel.Callbacks.
3229 */
3230 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003231 setLoadOnResume();
3232
Joe Onorato9c1289c2009-08-17 11:03:03 -04003233 if (mSavedState != null) {
3234 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003235 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 mSavedState = null;
3238 }
3239
3240 if (mSavedInstanceState != null) {
3241 super.onRestoreInstanceState(mSavedInstanceState);
3242 mSavedInstanceState = null;
3243 }
3244
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003245 // If we received the result of any pending adds while the loader was running (e.g. the
3246 // widget configuration forced an orientation change), process them now.
3247 for (int i = 0; i < sPendingAddList.size(); i++) {
3248 completeAdd(sPendingAddList.get(i));
3249 }
3250 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003251
Winson Chungc51db6a2011-10-05 11:44:49 -07003252 // Update the market app icon as necessary (the other icons will be managed in response to
3253 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003254 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003255
Winson Chungf0c6ae02012-03-21 16:10:31 -07003256 // Animate up any icons as necessary
3257 if (mVisible || mWorkspaceLoading) {
3258 Runnable newAppsRunnable = new Runnable() {
3259 @Override
3260 public void run() {
3261 runNewAppsAnimation();
3262 }
3263 };
3264 if (mNewShortcutAnimatePage > -1 &&
3265 mNewShortcutAnimatePage != mWorkspace.getCurrentPage()) {
3266 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3267 } else {
3268 newAppsRunnable.run();
3269 }
3270 }
3271
3272 mWorkspaceLoading = false;
3273 }
3274
3275 /**
3276 * Runs a new animation that scales up icons that were added while Launcher was in the
3277 * background.
3278 */
3279 private void runNewAppsAnimation() {
3280 AnimatorSet anim = new AnimatorSet();
3281 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3282 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3283 @Override
3284 public int compare(View a, View b) {
3285 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3286 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3287 int cellCountX = LauncherModel.getCellCountX();
3288 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3289 }
3290 });
3291 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3292 View v = mNewShortcutAnimateViews.get(i);
3293 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3294 PropertyValuesHolder.ofFloat("alpha", 1f),
3295 PropertyValuesHolder.ofFloat("scaleX", 1f),
3296 PropertyValuesHolder.ofFloat("scaleY", 1f));
3297 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3298 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3299 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3300 bounceAnims.add(bounceAnim);
3301 }
3302 anim.playTogether(bounceAnims);
3303 anim.addListener(new AnimatorListenerAdapter() {
3304 @Override
3305 public void onAnimationEnd(Animator animation) {
3306 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3307 }
3308 });
3309 anim.start();
3310
3311 // Clean up
3312 mNewShortcutAnimatePage = -1;
3313 mNewShortcutAnimateViews.clear();
3314 new Thread("clearNewAppsThread") {
3315 public void run() {
3316 mSharedPrefs.edit()
3317 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3318 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3319 .commit();
3320 }
3321 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003322 }
3323
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003324 @Override
3325 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 boolean searchVisible = updateGlobalSearchIcon();
3327 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3328 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003329 }
3330
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003332 * Add the icons for all apps.
3333 *
3334 * Implementation of the method from LauncherModel.Callbacks.
3335 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003336 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3337 // Remove the progress bar entirely; we could also make it GONE
3338 // but better to remove it since we know it's not going to be used
3339 View progressBar = mAppsCustomizeTabHost.
3340 findViewById(R.id.apps_customize_progress_bar);
3341 if (progressBar != null) {
3342 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003343 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003344 // We just post the call to setApps so the user sees the progress bar
3345 // disappear-- otherwise, it just looks like the progress bar froze
3346 // which doesn't look great
3347 mAppsCustomizeTabHost.post(new Runnable() {
3348 public void run() {
3349 if (mAppsCustomizeContent != null) {
3350 mAppsCustomizeContent.setApps(apps);
3351 }
3352 }
3353 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003354 }
3355
3356 /**
3357 * A package was installed.
3358 *
3359 * Implementation of the method from LauncherModel.Callbacks.
3360 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003361 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003362 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003364
Winson Chung785d2eb2011-04-14 16:08:02 -07003365 if (mAppsCustomizeContent != null) {
3366 mAppsCustomizeContent.addApps(apps);
3367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 }
3369
3370 /**
3371 * A package was updated.
3372 *
3373 * Implementation of the method from LauncherModel.Callbacks.
3374 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003375 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003376 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003378 if (mWorkspace != null) {
3379 mWorkspace.updateShortcuts(apps);
3380 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003381
Winson Chung785d2eb2011-04-14 16:08:02 -07003382 if (mAppsCustomizeContent != null) {
3383 mAppsCustomizeContent.updateApps(apps);
3384 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 }
3386
3387 /**
3388 * A package was uninstalled.
3389 *
3390 * Implementation of the method from LauncherModel.Callbacks.
3391 */
Joe Onorato36115782010-06-17 13:28:48 -04003392 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003394 if (permanent) {
3395 mWorkspace.removeItems(apps);
3396 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003397
Winson Chung785d2eb2011-04-14 16:08:02 -07003398 if (mAppsCustomizeContent != null) {
3399 mAppsCustomizeContent.removeApps(apps);
3400 }
Winson Chunga1820962011-10-03 16:31:06 -07003401
3402 // Notify the drag controller
3403 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 }
Joe Onoratobe386092009-11-17 17:32:16 -08003405
3406 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003407 * A number of packages were updated.
3408 */
3409 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003410 if (mAppsCustomizeContent != null) {
3411 mAppsCustomizeContent.onPackagesUpdated();
3412 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003413 }
3414
Winson Chung400438b2011-01-16 17:53:48 -08003415 private int mapConfigurationOriActivityInfoOri(int configOri) {
3416 final Display d = getWindowManager().getDefaultDisplay();
3417 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3418 switch (d.getRotation()) {
3419 case Surface.ROTATION_0:
3420 case Surface.ROTATION_180:
3421 // We are currently in the same basic orientation as the natural orientation
3422 naturalOri = configOri;
3423 break;
3424 case Surface.ROTATION_90:
3425 case Surface.ROTATION_270:
3426 // We are currently in the other basic orientation to the natural orientation
3427 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3428 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3429 break;
3430 }
3431
3432 int[] oriMap = {
3433 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3434 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3435 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3436 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3437 };
3438 // Since the map starts at portrait, we need to offset if this device's natural orientation
3439 // is landscape.
3440 int indexOffset = 0;
3441 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3442 indexOffset = 1;
3443 }
3444 return oriMap[(d.getRotation() + indexOffset) % 4];
3445 }
Adam Cohen446e9402011-09-15 18:21:21 -07003446
3447 public void lockScreenOrientationOnLargeUI() {
3448 if (LauncherApplication.isScreenLarge()) {
3449 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3450 .getConfiguration().orientation));
3451 }
Winson Chung400438b2011-01-16 17:53:48 -08003452 }
Adam Cohen446e9402011-09-15 18:21:21 -07003453 public void unlockScreenOrientationOnLargeUI() {
3454 if (LauncherApplication.isScreenLarge()) {
3455 mHandler.postDelayed(new Runnable() {
3456 public void run() {
3457 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3458 }
3459 }, mRestoreScreenOrientationDelay);
3460 }
Winson Chung400438b2011-01-16 17:53:48 -08003461 }
3462
Winson Chung82f55532011-08-09 14:14:23 -07003463 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003464 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003465 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003466 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003467
Winson Chung7d7541e2011-09-16 20:14:36 -07003468 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003469 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003470 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3471 Cling cling = (Cling) findViewById(clingId);
3472 if (cling != null) {
3473 cling.init(this, positionData);
3474 cling.setVisibility(View.VISIBLE);
3475 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3476 if (animate) {
3477 cling.buildLayer();
3478 cling.setAlpha(0f);
3479 cling.animate()
3480 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003481 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003482 .setDuration(SHOW_CLING_DURATION)
3483 .setStartDelay(delay)
3484 .start();
3485 } else {
3486 cling.setAlpha(1f);
3487 }
3488 }
3489 return cling;
3490 }
3491 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003492 if (cling != null) {
3493 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003494 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003495 anim.addListener(new AnimatorListenerAdapter() {
3496 public void onAnimationEnd(Animator animation) {
3497 cling.setVisibility(View.GONE);
3498 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003499 // We should update the shared preferences on a background thread
3500 new Thread("dismissClingThread") {
3501 public void run() {
3502 SharedPreferences.Editor editor = mSharedPrefs.edit();
3503 editor.putBoolean(flag, true);
3504 editor.commit();
3505 }
3506 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003507 };
3508 });
3509 anim.start();
3510 }
3511 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003512 private void removeCling(int id) {
3513 final View cling = findViewById(id);
3514 if (cling != null) {
3515 final ViewGroup parent = (ViewGroup) cling.getParent();
3516 parent.post(new Runnable() {
3517 @Override
3518 public void run() {
3519 parent.removeView(cling);
3520 }
3521 });
3522 }
3523 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003524 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003525 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003526 if (isClingsEnabled() &&
3527 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003528 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003529 } else {
3530 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003531 }
3532 }
3533 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003534 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003535 if (isClingsEnabled() &&
3536 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003537 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003538 } else {
3539 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003540 }
3541 }
3542 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003543 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003544 if (isClingsEnabled() &&
3545 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3546 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003547 } else {
3548 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003549 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003550 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003551 }
3552 public boolean isFolderClingVisible() {
3553 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003554 if (cling != null) {
3555 return cling.getVisibility() == View.VISIBLE;
3556 }
3557 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003558 }
Winson Chung82f55532011-08-09 14:14:23 -07003559 public void dismissWorkspaceCling(View v) {
3560 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003561 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003562 }
3563 public void dismissAllAppsCling(View v) {
3564 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003565 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3566 }
3567 public void dismissFolderCling(View v) {
3568 Cling cling = (Cling) findViewById(R.id.folder_cling);
3569 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003570 }
3571
Winson Chung80baf5a2010-08-09 16:03:15 -07003572 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003573 * Prints out out state for debugging.
3574 */
3575 public void dumpState() {
3576 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003577 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003578 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3579 Log.d(TAG, "mRestoring=" + mRestoring);
3580 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3581 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003582 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003583 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003584
Winson Chung785d2eb2011-04-14 16:08:02 -07003585 if (mAppsCustomizeContent != null) {
3586 mAppsCustomizeContent.dumpState();
3587 }
Joe Onoratobe386092009-11-17 17:32:16 -08003588 Log.d(TAG, "END launcher2 dump state");
3589 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003590
3591 @Override
3592 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3593 super.dump(prefix, fd, writer, args);
3594 writer.println(" ");
3595 writer.println("Debug logs: ");
3596 for (int i = 0; i < sDumpLogs.size(); i++) {
3597 writer.println(" " + sDumpLogs.get(i));
3598 }
3599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003600}
Michael Jurka2763be32011-02-24 11:19:57 -08003601
Michael Jurkaabded662011-03-04 12:06:57 -08003602interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003603 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003604 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003605 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003606 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003607}