blob: 6464a7d7885dc40f5bf79157b04b7c0ebc1dc72a [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 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700527 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 }
529
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800531 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700532 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
534 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700535 mWaitingForResult = false;
536
Adam Cohened66b2b2012-01-23 17:28:51 -0800537 // We have special handling for widgets
538 if (isWidgetDrop) {
539 int appWidgetId = data != null ?
540 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
541 completeTwoStageWidgetDrop(resultCode, appWidgetId);
542 return;
543 }
544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 // The pattern used here is that a user PICKs a specific application,
546 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
549 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700550 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800551 final PendingAddArguments args = new PendingAddArguments();
552 args.requestCode = requestCode;
553 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700554 args.container = mPendingAddInfo.container;
555 args.screen = mPendingAddInfo.screen;
556 args.cellX = mPendingAddInfo.cellX;
557 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 if (isWorkspaceLocked()) {
559 sPendingAddList.add(args);
560 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700561 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800564 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700565 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800566 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
567 null);
568 }
569
570 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
571 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
572 Runnable onCompleteRunnable = null;
573 int animationType = 0;
574
575 if (resultCode == RESULT_OK) {
576 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
577 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
578 mWidgetBeingConfigured.info);
579 mWidgetBeingConfigured.boundWidget = layout;
580 onCompleteRunnable = new Runnable() {
581 @Override
582 public void run() {
583 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
584 mPendingAddInfo.screen, layout, null);
585 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
586 null);
587 }
588 };
589 } else if (resultCode == RESULT_CANCELED) {
590 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
591 onCompleteRunnable = new Runnable() {
592 @Override
593 public void run() {
594 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
595 null);
596 }
597 };
598 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800599 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800601 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800602 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 @Override
606 protected void onResume() {
607 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700608
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800609 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700610 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700612 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700614 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
Winson Chunge4e50662012-01-23 14:45:13 -0800616
617 // Reset the pressed state of icons that were locked in the press state while activities
618 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800619 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800620 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800621 mWaitingForResume.setStayPressed(false);
622 }
Winson Chunge4e50662012-01-23 14:45:13 -0800623 if (mAppsCustomizeContent != null) {
624 // Resets the previous all apps icon press state
625 mAppsCustomizeContent.resetDrawableState();
626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628
629 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700630 protected void onPause() {
631 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700632 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800633 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634 }
Romain Guycbb89e42009-06-08 15:52:54 -0700635
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700636 @Override
637 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 // Flag the loader to stop early before switching
639 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700640 if (mAppsCustomizeContent != null) {
641 mAppsCustomizeContent.surrender();
642 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800643 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700644 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700645
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800646 // We can't hide the IME if it was forced open. So don't bother
647 /*
648 @Override
649 public void onWindowFocusChanged(boolean hasFocus) {
650 super.onWindowFocusChanged(hasFocus);
651
652 if (hasFocus) {
653 final InputMethodManager inputManager = (InputMethodManager)
654 getSystemService(Context.INPUT_METHOD_SERVICE);
655 WindowManager.LayoutParams lp = getWindow().getAttributes();
656 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
657 android.os.Handler()) {
658 protected void onReceiveResult(int resultCode, Bundle resultData) {
659 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
660 }
661 });
662 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
663 }
664 }
665 */
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 private boolean acceptFilter() {
668 final InputMethodManager inputManager = (InputMethodManager)
669 getSystemService(Context.INPUT_METHOD_SERVICE);
670 return !inputManager.isFullscreenMode();
671 }
672
673 @Override
674 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700675 final int uniChar = event.getUnicodeChar();
676 final boolean handled = super.onKeyDown(keyCode, event);
677 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
678 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
680 keyCode, event);
681 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700682 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700683 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700684 // showSearchDialog()
685 // If there are multiple keystrokes before the search dialog takes focus,
686 // onSearchRequested() will be called for every keystroke,
687 // but it is idempotent, so it's fine.
688 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690 }
691
Joe Onorato8a9625e2010-01-28 15:55:35 -0800692 // Eat the long press event so the keyboard doesn't come up.
693 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
694 return true;
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 return handled;
698 }
699
Karl Rosaen138a0412009-04-23 19:00:21 -0700700 private String getTypedText() {
701 return mDefaultKeySsb.toString();
702 }
703
704 private void clearTypedText() {
705 mDefaultKeySsb.clear();
706 mDefaultKeySsb.clearSpans();
707 Selection.setSelection(mDefaultKeySsb, 0);
708 }
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700711 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
712 * State
713 */
714 private static State intToState(int stateOrdinal) {
715 State state = State.WORKSPACE;
716 final State[] stateValues = State.values();
717 for (int i = 0; i < stateValues.length; i++) {
718 if (stateValues[i].ordinal() == stateOrdinal) {
719 state = stateValues[i];
720 break;
721 }
722 }
723 return state;
724 }
725
726 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 * Restores the previous state, if it exists.
728 *
729 * @param savedState The previous state.
730 */
731 private void restoreState(Bundle savedState) {
732 if (savedState == null) {
733 return;
734 }
735
Michael Jurka883f55b2010-10-21 15:47:14 -0700736 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700737 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800738 showAllApps(false);
739 }
740
Winson Chungf0c6ae02012-03-21 16:10:31 -0700741 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700743 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
747 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700748
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
750 mPendingAddInfo.container = pendingAddContainer;
751 mPendingAddInfo.screen = pendingAddScreen;
752 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
753 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 mRestoring = true;
755 }
756
757 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
758 if (renameFolder) {
759 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700760 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 mRestoring = true;
762 }
Winson Chunga12a2502010-12-20 14:41:35 -0800763
Winson Chung785d2eb2011-04-14 16:08:02 -0700764
765 // Restore the AppsCustomize tab
766 if (mAppsCustomizeTabHost != null) {
767 String curTab = savedState.getString("apps_customize_currentTab");
768 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700769 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700770 mAppsCustomizeContent.setContentType(
771 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
772 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700773 mAppsCustomizeContent.loadAssociatedPages(
774 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 }
776
Winson Chung5afbf7b2011-07-25 11:53:08 -0700777 int currentIndex = savedState.getInt("apps_customize_currentIndex");
778 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 }
781
782 /**
783 * Finds all the views we need and configure them properly.
784 */
785 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700786 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400787
Winson Chung4c98d922011-05-31 16:50:48 -0700788 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
789 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800790 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
791 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792
Winson Chung4c98d922011-05-31 16:50:48 -0700793 // Setup the drag layer
794 mDragLayer.setup(this, dragController);
795
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 // Setup the hotseat
797 mHotseat = (Hotseat) findViewById(R.id.hotseat);
798 if (mHotseat != null) {
799 mHotseat.setup(this);
800 }
801
Winson Chung4c98d922011-05-31 16:50:48 -0700802 // Setup the workspace
803 mWorkspace.setHapticFeedbackEnabled(false);
804 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700805 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700806 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700807
Winson Chungf0ea4d32011-06-06 14:27:16 -0700808 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700809 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700810
Winson Chungf0ea4d32011-06-06 14:27:16 -0700811 // Setup AppsCustomize
812 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
813 findViewById(R.id.apps_customize_pane);
814 mAppsCustomizeContent = (AppsCustomizePagedView)
815 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
816 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400817
Michael Jurka5130e402011-10-13 04:55:35 -0700818 // Get the all apps button
819 mAllAppsButton = findViewById(R.id.all_apps_button);
820 if (mAllAppsButton != null) {
821 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
822 @Override
823 public boolean onTouch(View v, MotionEvent event) {
824 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
825 onTouchDownAllAppsButton(v);
826 }
827 return false;
828 }
829 });
830 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700832 dragController.setDragScoller(mWorkspace);
833 dragController.setScrollView(mDragLayer);
834 dragController.setMoveTarget(mWorkspace);
835 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700836 if (mSearchDropTargetBar != null) {
837 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840
841 /**
842 * Creates a view representing a shortcut.
843 *
844 * @param info The data structure describing the shortcut.
845 *
846 * @return A View inflated from R.layout.application.
847 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800848 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700850 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852
853 /**
854 * Creates a view representing a shortcut inflated from the specified resource.
855 *
856 * @param layoutResId The id of the XML layout used to create the shortcut.
857 * @param parent The group the shortcut belongs to.
858 * @param info The data structure describing the shortcut.
859 *
860 * @return A View inflated from layoutResId.
861 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800862 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800863 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
864 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 return favorite;
867 }
868
869 /**
870 * Add an application shortcut to the workspace.
871 *
872 * @param data The intent describing the application.
873 * @param cellInfo The position on screen where to create the shortcut.
874 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878
Adam Cohenfbba09b2011-07-18 21:43:05 -0700879 // First we check if we already know the exact location where we want to add this item.
880 if (cellX >= 0 && cellY >= 0) {
881 cellXY[0] = cellX;
882 cellXY[1] = cellY;
883 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700884 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700885 return;
886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800888 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800889
Romain Guy73b979d2009-06-09 12:57:21 -0700890 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800891 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700895 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800896 } else {
897 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899 }
Romain Guycbb89e42009-06-08 15:52:54 -0700900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 /**
902 * Add a shortcut to the workspace.
903 *
904 * @param data The intent describing the shortcut.
905 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700907 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
908 int cellY) {
909 int[] cellXY = mTmpAddItemCellCoordinates;
910 int[] touchXY = mPendingAddInfo.dropPos;
911 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Michael Jurkad3ef3062010-11-23 16:23:58 -0800913 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700914
Adam Cohen558baaf2011-08-15 15:22:57 -0700915 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800916 if (info == null) {
917 return;
918 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700919 final View view = createShortcut(info);
920
Adam Cohenfbba09b2011-07-18 21:43:05 -0700921 // First we check if we already know the exact location where we want to add this item.
922 if (cellX >= 0 && cellY >= 0) {
923 cellXY[0] = cellX;
924 cellXY[1] = cellY;
925 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700926
927 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800928 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700929 true, null,null)) {
930 return;
931 }
932 DragObject dragObject = new DragObject();
933 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800934 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
935 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700936 return;
937 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700938 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800939 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800941 foundCellSpan = (result != null);
942 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700943 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800944 }
945
946 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700947 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700948 return;
949 }
950
Adam Cohen558baaf2011-08-15 15:22:57 -0700951 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952
953 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
955 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957 }
958
Adam Cohenf814aa02011-09-01 13:48:05 -0700959 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
960 if (spanXY == null) {
961 spanXY = new int[2];
962 }
963
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800964 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700965 // We want to account for the extra amount of padding that we are adding to the widget
966 // to ensure that it gets the full amount of space that it has requested
967 int requiredWidth = minWidth + padding.left + padding.right;
968 int requiredHeight = minHeight + padding.top + padding.bottom;
969 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
970 }
971
972 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
973 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
974 }
975
Adam Cohend41fbf52012-02-16 23:53:59 -0800976 int[] getMinSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
Adam Cohencbf47e32011-09-16 17:32:37 -0700977 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
978 }
979
Adam Cohenf814aa02011-09-01 13:48:05 -0700980 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
981 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
982 }
983
Adam Cohend41fbf52012-02-16 23:53:59 -0800984 int[] getMinSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
985 return getSpanForWidget(info.componentName, info.minResizeWidth,
986 info.minResizeHeight, spanXY);
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700992 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800995 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
996 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
997 if (appWidgetInfo == null) {
998 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
999 }
Romain Guycbb89e42009-06-08 15:52:54 -07001000
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001001 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001003
Adam Cohend41fbf52012-02-16 23:53:59 -08001004 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001005 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001008 // We have saved the position to which the widget was dragged-- this really only matters
1009 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001010 int[] cellXY = mTmpAddItemCellCoordinates;
1011 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001012 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001013 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1015 cellXY[0] = mPendingAddInfo.cellX;
1016 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001017 spanXY[0] = mPendingAddInfo.spanX;
1018 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001019 foundCellSpan = true;
1020 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001021 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1024 spanXY[1], cellXY, finalSpan);
1025 spanXY[0] = finalSpan[0];
1026 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001027 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001029 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001030 }
1031
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001033 if (appWidgetId != -1) {
1034 // Deleting an app widget ID is a void call but writes to disk before returning
1035 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001036 new Thread("deleteAppWidgetId") {
1037 public void run() {
1038 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1039 }
1040 }.start();
1041 }
Winson Chung93eef082012-03-23 15:59:27 -07001042 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001043 return;
1044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 launcherInfo.spanX = spanXY[0];
1049 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001050 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1051 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001054 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
1056 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001057 if (hostView == null) {
1058 // Perform actual inflation because we're live
1059 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1060 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1061 } else {
1062 // The AppWidgetHostView has already been inflated and instantiated
1063 launcherInfo.hostView = hostView;
1064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001067 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001070
1071 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001073 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Adam Cohended9f8d2010-11-03 13:25:16 -07001076 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1077 @Override
1078 public void onReceive(Context context, Intent intent) {
1079 final String action = intent.getAction();
1080 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1081 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001082 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001083 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001084
Winson Chungc100e8e2011-08-09 16:02:43 -07001085 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001086 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001087 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1088 mAppsCustomizeTabHost.reset();
1089 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001091 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1092 mUserPresent = true;
1093 updateRunning();
1094 }
1095 }
1096 };
1097
1098 @Override
1099 public void onAttachedToWindow() {
1100 super.onAttachedToWindow();
1101
1102 // Listen for broadcasts related to user-presence
1103 final IntentFilter filter = new IntentFilter();
1104 filter.addAction(Intent.ACTION_SCREEN_OFF);
1105 filter.addAction(Intent.ACTION_USER_PRESENT);
1106 registerReceiver(mReceiver, filter);
1107
Adam Cohend113e0c2010-11-11 10:48:05 -08001108 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001109 mVisible = true;
1110 }
1111
1112 @Override
1113 public void onDetachedFromWindow() {
1114 super.onDetachedFromWindow();
1115 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001116 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001117
Adam Cohend113e0c2010-11-11 10:48:05 -08001118 if (mAttached) {
1119 unregisterReceiver(mReceiver);
1120 mAttached = false;
1121 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 updateRunning();
1123 }
1124
1125 public void onWindowVisibilityChanged(int visibility) {
1126 mVisible = visibility == View.VISIBLE;
1127 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001128 // The following code used to be in onResume, but it turns out onResume is called when
1129 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1130 // is a more appropriate event to handle
1131 if (mVisible) {
1132 mAppsCustomizeTabHost.onWindowVisible();
1133 if (!mWorkspaceLoading) {
1134 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001135 // We want to let Launcher draw itself at least once before we force it to build
1136 // layers on all the workspace pages, so that transitioning to Launcher from other
1137 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1138 // a true draw so we wait until the second preDraw call to be safe
1139 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001140 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001141 // We delay the layer building a bit in order to give
1142 // other message processing a time to run. In particular
1143 // this avoids a delay in hiding the IME if it was
1144 // currently shown, because doing that may involve
1145 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001146 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001147 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001148 return true;
1149 }
1150 });
1151 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001152 // When Launcher comes back to foreground, a different Activity might be responsible for
1153 // the app market intent, so refresh the icon
1154 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001155 clearTypedText();
1156 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 }
1158
1159 private void sendAdvanceMessage(long delay) {
1160 mHandler.removeMessages(ADVANCE_MSG);
1161 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1162 mHandler.sendMessageDelayed(msg, delay);
1163 mAutoAdvanceSentTime = System.currentTimeMillis();
1164 }
1165
1166 private void updateRunning() {
1167 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1168 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1169 mAutoAdvanceRunning = autoAdvanceRunning;
1170 if (autoAdvanceRunning) {
1171 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1172 sendAdvanceMessage(delay);
1173 } else {
1174 if (!mWidgetsToAdvance.isEmpty()) {
1175 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1176 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1177 }
1178 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001179 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001180 }
1181 }
1182 }
1183
1184 private final Handler mHandler = new Handler() {
1185 @Override
1186 public void handleMessage(Message msg) {
1187 if (msg.what == ADVANCE_MSG) {
1188 int i = 0;
1189 for (View key: mWidgetsToAdvance.keySet()) {
1190 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1191 final int delay = mAdvanceStagger * i;
1192 if (v instanceof Advanceable) {
1193 postDelayed(new Runnable() {
1194 public void run() {
1195 ((Advanceable) v).advance();
1196 }
1197 }, delay);
1198 }
1199 i++;
1200 }
1201 sendAdvanceMessage(mAdvanceInterval);
1202 }
1203 }
1204 };
1205
1206 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001207 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001208 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1209 if (v instanceof Advanceable) {
1210 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001211 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 updateRunning();
1213 }
1214 }
1215
1216 void removeWidgetToAutoAdvance(View hostView) {
1217 if (mWidgetsToAdvance.containsKey(hostView)) {
1218 mWidgetsToAdvance.remove(hostView);
1219 updateRunning();
1220 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 }
1222
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001224 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 launcherInfo.hostView = null;
1226 }
1227
Winson Chung93eef082012-03-23 15:59:27 -07001228 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1229 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1230 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 }
1232
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 public LauncherAppWidgetHost getAppWidgetHost() {
1234 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 public LauncherModel getModel() {
1238 return mModel;
1239 }
1240
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001241 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001242 getWindow().closeAllPanels();
1243
Winson Chung87acb482011-08-23 17:28:05 -07001244 /**
1245 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001246 try {
1247 dismissDialog(DIALOG_CREATE_SHORTCUT);
1248 // Unlock the workspace if the dialog was showing
1249 } catch (Exception e) {
1250 // An exception is thrown if the dialog is not visible, which is fine
1251 }
1252
1253 try {
1254 dismissDialog(DIALOG_RENAME_FOLDER);
1255 // Unlock the workspace if the dialog was showing
1256 } catch (Exception e) {
1257 // An exception is thrown if the dialog is not visible, which is fine
1258 }
Winson Chung87acb482011-08-23 17:28:05 -07001259 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001260
1261 // Whatever we were doing is hereby canceled.
1262 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
1266 protected void onNewIntent(Intent intent) {
1267 super.onNewIntent(intent);
1268
1269 // Close the menu
1270 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001271 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001272 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001273
Joe Onorato14f122b2009-11-19 14:06:36 -08001274 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1275 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001276
Adam Cohenac56cff2011-09-28 20:45:37 -07001277 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001278 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001279 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001280 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1281 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001282 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001283 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001284
1285 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001286 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001287 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001288
Joe Onorato3a8820b2009-11-10 15:06:42 -08001289 final View v = getWindow().peekDecorView();
1290 if (v != null && v.getWindowToken() != null) {
1291 InputMethodManager imm = (InputMethodManager)getSystemService(
1292 INPUT_METHOD_SERVICE);
1293 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001295
Michael Jurka35aa14d2011-07-07 17:01:08 -07001296 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001297 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001298 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302
1303 @Override
1304 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1305 // Do not call super here
1306 mSavedInstanceState = savedInstanceState;
1307 }
1308
1309 @Override
1310 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001311 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001312 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
Michael Jurka883f55b2010-10-21 15:47:14 -07001314 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001315 // We close any open folder since it will not be re-opened, and we need to make sure
1316 // this state is reflected.
1317 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1320 mWaitingForResult) {
1321 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1322 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1323 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1324 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 if (mFolderInfo != null && mWaitingForResult) {
1328 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1329 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1330 }
Michael Jurka946ad472010-07-09 18:05:18 -07001331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 // Save the current AppsCustomize tab
1333 if (mAppsCustomizeTabHost != null) {
1334 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1335 if (currentTabTag != null) {
1336 outState.putString("apps_customize_currentTab", currentTabTag);
1337 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001338 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1339 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 @Override
1344 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001346
Winson Chunge7a03942011-08-05 15:05:12 -07001347 // Remove all pending runnables
1348 mHandler.removeMessages(ADVANCE_MSG);
1349 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001350 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001351
Winson Chungcd2b0142011-06-08 16:02:26 -07001352 // Stop callbacks from LauncherModel
1353 LauncherApplication app = ((LauncherApplication) getApplication());
1354 mModel.stopLoader();
1355 app.setLauncher(null);
1356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001358 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001360 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001362 mAppWidgetHost = null;
1363
1364 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
1366 TextKeyListener.getInstance().release();
1367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001370
1371 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001372 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001373
1374 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1375 mWorkspace.removeAllViews();
1376 mWorkspace = null;
1377 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001378
1379 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381
Adam Cohena9cf38f2011-05-02 15:36:58 -07001382 public DragController getDragController() {
1383 return mDragController;
1384 }
1385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 @Override
1387 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001388 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 super.startActivityForResult(intent, requestCode);
1390 }
1391
Winson Chung70d72102011-08-12 11:24:35 -07001392 /**
1393 * Indicates that we want global search for this activity by setting the globalSearch
1394 * argument for {@link #startSearch} to true.
1395 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001397 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001399
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001400 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Karl Rosaen138a0412009-04-23 19:00:21 -07001402 if (initialQuery == null) {
1403 // Use any text typed in the launcher as the initial query
1404 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 if (appSearchData == null) {
1407 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001408 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001410 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Karl Rosaen138a0412009-04-23 19:00:21 -07001412 final SearchManager searchManager =
1413 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001415 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
Winson Chung70d72102011-08-12 11:24:35 -07001418 @Override
1419 public boolean onCreateOptionsMenu(Menu menu) {
1420 if (isWorkspaceLocked()) {
1421 return false;
1422 }
1423
1424 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001425
1426 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1427 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1428 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001429 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1430 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1431 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001432 String helpUrl = getString(R.string.help_url);
1433 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001434 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1435 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1436
Winson Chung70d72102011-08-12 11:24:35 -07001437 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1438 .setIcon(android.R.drawable.ic_menu_gallery)
1439 .setAlphabeticShortcut('W');
1440 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1441 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001442 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001443 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001444 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1445 .setIcon(android.R.drawable.ic_menu_preferences)
1446 .setIntent(settings)
1447 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001448 if (!helpUrl.isEmpty()) {
1449 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1450 .setIcon(android.R.drawable.ic_menu_help)
1451 .setIntent(help)
1452 .setAlphabeticShortcut('H');
1453 }
Winson Chung70d72102011-08-12 11:24:35 -07001454 return true;
1455 }
1456
1457 @Override
1458 public boolean onPrepareOptionsMenu(Menu menu) {
1459 super.onPrepareOptionsMenu(menu);
1460
1461 if (mAppsCustomizeTabHost.isTransitioning()) {
1462 return false;
1463 }
1464 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1465 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1466
1467 return true;
1468 }
1469
1470 @Override
1471 public boolean onOptionsItemSelected(MenuItem item) {
1472 switch (item.getItemId()) {
1473 case MENU_WALLPAPER_SETTINGS:
1474 startWallpaper();
1475 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001476 }
1477
1478 return super.onOptionsItemSelected(item);
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001481 @Override
1482 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001483 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001484 // Use a custom animation for launching search
1485 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001486 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001487 }
1488
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 public boolean isWorkspaceLocked() {
1490 return mWorkspaceLoading || mWaitingForResult;
1491 }
1492
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001494 mPendingAddInfo.container = ItemInfo.NO_ID;
1495 mPendingAddInfo.screen = -1;
1496 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1497 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001498 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001500 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001501
Michael Jurkaaf442092010-06-10 17:01:57 -07001502 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 // TODO: catch bad widget exception when sent
1504 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001505 // TODO: Is this log message meaningful?
1506 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001507 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001508 }
1509
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1511 final AppWidgetProviderInfo appWidget = info.info;
1512 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001513 if (appWidget.configure != null) {
1514 // Launch over to configure widget, if needed
1515 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1516 intent.setComponent(appWidget.configure);
1517 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001518 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001519 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 intent.putExtra(InstallWidgetReceiver.
1521 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001522
1523 final String mimeType = info.mimeType;
1524 final ClipData clipData = (ClipData) info.configurationData;
1525 final ClipDescription clipDesc = clipData.getDescription();
1526 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1527 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001528 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001529 final CharSequence stringData = item.getText();
1530 final Uri uriData = item.getUri();
1531 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 final String key = InstallWidgetReceiver.
1533 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001534 if (uriData != null) {
1535 intent.putExtra(key, uriData);
1536 } else if (intentData != null) {
1537 intent.putExtra(key, intentData);
1538 } else if (stringData != null) {
1539 intent.putExtra(key, stringData);
1540 }
1541 break;
1542 }
1543 }
1544 }
Winson Chung55cef262010-10-28 14:14:18 -07001545 }
Romain Guy8e633c52010-03-04 12:51:36 -08001546 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001549 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001551 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 /**
1557 * Process a shortcut drop.
1558 *
1559 * @param componentName The name of the component
1560 * @param screen The screen where it should be added
1561 * @param cell The cell it should be added to, optional
1562 * @param position The location on the screen where it was dropped, optional
1563 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1565 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001567 mPendingAddInfo.container = container;
1568 mPendingAddInfo.screen = screen;
1569 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001570
1571 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 mPendingAddInfo.cellX = cell[0];
1573 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001575
1576 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1577 createShortcutIntent.setComponent(componentName);
1578 processShortcut(createShortcutIntent);
1579 }
1580
Adam Cohenfbba09b2011-07-18 21:43:05 -07001581 /**
1582 * Process a widget drop.
1583 *
1584 * @param info The PendingAppWidgetInfo of the widget being added.
1585 * @param screen The screen where it should be added
1586 * @param cell The cell it should be added to, optional
1587 * @param position The location on the screen where it was dropped, optional
1588 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 mPendingAddInfo.container = info.container = container;
1593 mPendingAddInfo.screen = info.screen = screen;
1594 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001595 mPendingAddInfo.minSpanX = info.minSpanX;
1596 mPendingAddInfo.minSpanY = info.minSpanY;
1597
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001599 mPendingAddInfo.cellX = cell[0];
1600 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001601 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001602 if (span != null) {
1603 mPendingAddInfo.spanX = span[0];
1604 mPendingAddInfo.spanY = span[1];
1605 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001606
Adam Cohened66b2b2012-01-23 17:28:51 -08001607 AppWidgetHostView hostView = info.boundWidget;
1608 int appWidgetId;
1609 if (hostView != null) {
1610 appWidgetId = hostView.getAppWidgetId();
1611 } else {
1612 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1613 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1614 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001615 addAppWidgetImpl(appWidgetId, info);
1616 }
1617
Joe Onoratodeb98af2010-02-19 14:59:39 -08001618 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001619 // Handle case where user selected "Applications"
1620 String applicationName = getResources().getString(R.string.group_applications);
1621 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001622
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001623 if (applicationName != null && applicationName.equals(shortcutName)) {
1624 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1625 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001627 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1628 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001629 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001630 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001631 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001632 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
Winson Chung24ab2f12010-09-16 14:10:47 -07001636 void processWallpaper(Intent intent) {
1637 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1638 }
1639
Winson Chung3d503fb2011-07-13 17:25:49 -07001640 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1641 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001642 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 folderInfo.title = getText(R.string.folder_name);
1644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1647 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001648 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
1650 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001651 FolderIcon newFolder =
1652 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1653 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1654 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001655 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001659 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001663 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001665 Intent chooser = Intent.createChooser(pickWallpaper,
1666 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001667 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1668 // Removed in Eclair MR1
1669// WallpaperManager wm = (WallpaperManager)
1670// getSystemService(Context.WALLPAPER_SERVICE);
1671// WallpaperInfo wi = wm.getWallpaperInfo();
1672// if (wi != null && wi.getSettingsActivity() != null) {
1673// LabeledIntent li = new LabeledIntent(getPackageName(),
1674// R.string.configure_wallpaper, 0);
1675// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1676// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1677// }
Mike Clerona0618e42009-10-22 13:55:21 -07001678 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001681 /**
1682 * Registers various content observers. The current implementation registers
1683 * only a favorites observer to keep track of the favorites applications.
1684 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001685 private void registerContentObservers() {
1686 ContentResolver resolver = getContentResolver();
1687 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1688 true, mWidgetObserver);
1689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 @Override
1692 public boolean dispatchKeyEvent(KeyEvent event) {
1693 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1694 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001696 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001697 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001698 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001699 dumpState();
1700 return true;
1701 }
1702 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001703 }
1704 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1705 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001706 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 return true;
1708 }
1709 }
1710
1711 return super.dispatchKeyEvent(event);
1712 }
1713
Joe Onorato88ec0992009-11-19 13:16:06 -08001714 @Override
1715 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001716 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001717 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001718 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001719 Folder openFolder = mWorkspace.getOpenFolder();
1720 if (openFolder.isEditingName()) {
1721 openFolder.dismissEditingName();
1722 } else {
1723 closeFolder();
1724 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001725 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001726 mWorkspace.exitWidgetResizeMode();
1727
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001728 // Back button is a no-op here, but give at least some feedback for the button press
1729 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001731 }
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001734 * Re-listen when widgets are reset.
1735 */
1736 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001737 if (mAppWidgetHost != null) {
1738 mAppWidgetHost.startListening();
1739 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001740 }
1741
1742 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1744 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001747 if (mModel != null) {
1748 mModel.unbindWorkspaceItems();
1749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 /**
1753 * Launches the intent referred by the clicked shortcut.
1754 *
1755 * @param v The view representing the clicked shortcut.
1756 */
1757 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001758 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1759 // view has detached (it's possible for this to happen if the view is removed mid touch).
1760 if (v.getWindowToken() == null) {
1761 return;
1762 }
1763
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001764 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001765 return;
1766 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001769 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001771 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001772 int[] pos = new int[2];
1773 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001774 intent.setSourceBounds(new Rect(pos[0], pos[1],
1775 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001776 boolean success = startActivitySafely(intent, tag);
1777
1778 if (success && v instanceof BubbleTextView) {
1779 mWaitingForResume = (BubbleTextView) v;
1780 mWaitingForResume.setStayPressed(true);
1781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001783 if (v instanceof FolderIcon) {
1784 FolderIcon fi = (FolderIcon) v;
1785 handleFolderClick(fi);
1786 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001787 } else if (v == mAllAppsButton) {
1788 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001789 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001790 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001791 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794 }
1795
Michael Jurka0e260592010-06-30 17:07:39 -07001796 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001797 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001798 // clicking anywhere on the workspace causes the customization drawer to slide down
1799 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001800 return false;
1801 }
1802
Michael Jurkaaf442092010-06-10 17:01:57 -07001803 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001804 * Event handler for the search button
1805 *
1806 * @param v The view that was clicked.
1807 */
1808 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1810
Amith Yamasania135ba82011-08-09 17:42:01 -07001811 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001812 }
1813
1814 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001815 * Event handler for the voice button
1816 *
1817 * @param v The view that was clicked.
1818 */
1819 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001820 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001821
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001822 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001823 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001824 startActivity(intent);
1825 }
1826
1827 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001828 * Event handler for the "grid" button that appears on the home screen, which
1829 * enters all apps mode.
1830 *
1831 * @param v The view that was clicked.
1832 */
1833 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001834 showAllApps(true);
1835 }
1836
1837 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001838 // Provide the same haptic feedback that the system offers for virtual keys.
1839 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001840 }
1841
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001842 public void onClickAppMarketButton(View v) {
1843 if (mAppMarketIntent != null) {
1844 startActivitySafely(mAppMarketIntent, "app market");
1845 }
1846 }
1847
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001848 void startApplicationDetailsActivity(ComponentName componentName) {
1849 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001850 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1851 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001852 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001853 startActivity(intent);
1854 }
1855
Patrick Dubroy5539af72010-09-07 15:22:01 -07001856 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1857 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1858 // System applications cannot be installed. For now, show a toast explaining that.
1859 // We may give them the option of disabling apps this way.
1860 int messageId = R.string.uninstall_system_app_text;
1861 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1862 } else {
1863 String packageName = appInfo.componentName.getPackageName();
1864 String className = appInfo.componentName.getClassName();
1865 Intent intent = new Intent(
1866 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001867 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1868 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001869 startActivity(intent);
1870 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001871 }
1872
Michael Jurkaddd62e92011-02-16 17:49:14 -08001873 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1875 try {
1876 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001877 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } catch (ActivityNotFoundException e) {
1879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001880 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 } catch (SecurityException e) {
1882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001883 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001885 "or use the exported attribute for this activity. "
1886 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001888 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001890
Romain Guy8e633c52010-03-04 12:51:36 -08001891 void startActivityForResultSafely(Intent intent, int requestCode) {
1892 try {
1893 startActivityForResult(intent, requestCode);
1894 } catch (ActivityNotFoundException e) {
1895 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1896 } catch (SecurityException e) {
1897 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1898 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1899 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1900 "or use the exported attribute for this activity.", e);
1901 }
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
Adam Cohena9cf38f2011-05-02 15:36:58 -07001904 private void handleFolderClick(FolderIcon folderIcon) {
1905 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001906 Folder openFolder = mWorkspace.getFolderForTag(info);
1907
1908 // If the folder info reports that the associated folder is open, then verify that
1909 // it is actually opened. There have been a few instances where this gets out of sync.
1910 if (info.opened && openFolder == null) {
1911 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1912 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1913 info.opened = false;
1914 }
1915
Adam Cohena9cf38f2011-05-02 15:36:58 -07001916 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 // Close any open folder
1918 closeFolder();
1919 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001920 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 } else {
1922 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 int folderScreen;
1924 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001925 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 // .. and close it
1927 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001928 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 // Close any folder open on the current screen
1930 closeFolder();
1931 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001932 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934 }
1935 }
1936 }
1937
Adam Cohen2801caf2011-05-13 20:57:39 -07001938 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001939 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001940 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1941 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1942 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1943
Adam Cohenc51934b2011-07-26 21:07:43 -07001944 FolderInfo info = (FolderInfo) fi.getTag();
1945 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1946 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001947 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1948 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001949 }
1950
Adam Cohen2801caf2011-05-13 20:57:39 -07001951 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1952 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1953 oa.start();
1954 }
1955
1956 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001957 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001958 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1959 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1960 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1961
Adam Cohenc51934b2011-07-26 21:07:43 -07001962 FolderInfo info = (FolderInfo) fi.getTag();
1963 CellLayout cl = null;
1964 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1965 cl = (CellLayout) fi.getParent().getParent();
1966 }
1967
1968 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001969 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1970 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001971 oa.addListener(new AnimatorListenerAdapter() {
1972 @Override
1973 public void onAnimationEnd(Animator animation) {
1974 if (layout != null) {
1975 layout.clearFolderLeaveBehind();
1976 }
1977 }
1978 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001979 oa.start();
1980 }
1981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001983 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 * is animated relative to the specified View. If the View is null, no animation
1985 * is played.
1986 *
1987 * @param folderInfo The FolderInfo describing the folder to open.
1988 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001989 public void openFolder(FolderIcon folderIcon) {
1990 Folder folder = folderIcon.mFolder;
1991 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Adam Cohen2801caf2011-05-13 20:57:39 -07001993 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001994 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995
Adam Cohen4554ee12011-08-03 16:13:21 -07001996 // Just verify that the folder hasn't already been added to the DragLayer.
1997 // There was a one-off crash where the folder had a parent already.
1998 if (folder.getParent() == null) {
1999 mDragLayer.addView(folder);
2000 mDragController.addDropTarget((DropTarget) folder);
2001 } else {
2002 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2003 folder.getParent() + ").");
2004 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002005 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002006 }
2007
2008 public void closeFolder() {
2009 Folder folder = mWorkspace.getOpenFolder();
2010 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002011 if (folder.isEditingName()) {
2012 folder.dismissEditingName();
2013 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002014 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002015
2016 // Dismiss the folder cling
2017 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002018 }
2019 }
2020
2021 void closeFolder(Folder folder) {
2022 folder.getInfo().opened = false;
2023
2024 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2025 if (parent != null) {
2026 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2027 shrinkAndFadeInFolderIcon(fi);
2028 }
2029 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 }
2031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002033
Patrick Dubroye708c522011-03-01 16:03:43 -08002034 if (mState != State.WORKSPACE) {
2035 return false;
2036 }
2037
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 return false;
2040 }
2041
2042 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002043 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
2045
Michael Jurka0280c3b2010-09-17 15:00:07 -07002046 resetAddInfo();
2047 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002049 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 return true;
2051 }
2052
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 // The hotseat touch handling does not go through Workspace, and we always allow long press
2054 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002055 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002056 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2057 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002058 if (itemUnderLongClick == null) {
2059 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002060 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2061 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002062 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002064 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068 }
2069 }
2070 return true;
2071 }
2072
Winson Chung3d503fb2011-07-13 17:25:49 -07002073 boolean isHotseatLayout(View layout) {
2074 return mHotseat != null && layout != null &&
2075 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2076 }
2077 Hotseat getHotseat() {
2078 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002079 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002080 SearchDropTargetBar getSearchBar() {
2081 return mSearchDropTargetBar;
2082 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002083
Winson Chung3d503fb2011-07-13 17:25:49 -07002084 /**
2085 * Returns the CellLayout of the specified container at the specified screen.
2086 */
2087 CellLayout getCellLayout(long container, int screen) {
2088 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2089 if (mHotseat != null) {
2090 return mHotseat.getLayout();
2091 } else {
2092 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002093 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 } else {
2095 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002096 }
2097 }
2098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 Workspace getWorkspace() {
2100 return mWorkspace;
2101 }
2102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 @Override
2104 protected Dialog onCreateDialog(int id) {
2105 switch (id) {
2106 case DIALOG_CREATE_SHORTCUT:
2107 return new CreateShortcut().createDialog();
2108 case DIALOG_RENAME_FOLDER:
2109 return new RenameFolder().createDialog();
2110 }
2111
2112 return super.onCreateDialog(id);
2113 }
2114
2115 @Override
2116 protected void onPrepareDialog(int id, Dialog dialog) {
2117 switch (id) {
2118 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 break;
2120 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002121 if (mFolderInfo != null) {
2122 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2123 final CharSequence text = mFolderInfo.title;
2124 input.setText(text);
2125 input.setSelection(0, text.length());
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 break;
2128 }
2129 }
2130
2131 void showRenameDialog(FolderInfo info) {
2132 mFolderInfo = info;
2133 mWaitingForResult = true;
2134 showDialog(DIALOG_RENAME_FOLDER);
2135 }
2136
Adam Cohenfbba09b2011-07-18 21:43:05 -07002137 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002138 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002139 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2140 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 mWaitingForResult = true;
2142 showDialog(DIALOG_CREATE_SHORTCUT);
2143 }
2144
2145 private class RenameFolder {
2146 private EditText mInput;
2147
2148 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2150 mInput = (EditText) layout.findViewById(R.id.folder_name);
2151
2152 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2153 builder.setIcon(0);
2154 builder.setTitle(getString(R.string.rename_folder_title));
2155 builder.setCancelable(true);
2156 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2157 public void onCancel(DialogInterface dialog) {
2158 cleanup();
2159 }
2160 });
2161 builder.setNegativeButton(getString(R.string.cancel_action),
2162 new Dialog.OnClickListener() {
2163 public void onClick(DialogInterface dialog, int which) {
2164 cleanup();
2165 }
2166 }
2167 );
2168 builder.setPositiveButton(getString(R.string.rename_action),
2169 new Dialog.OnClickListener() {
2170 public void onClick(DialogInterface dialog, int which) {
2171 changeFolderName();
2172 }
2173 }
2174 );
2175 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002176
2177 final AlertDialog dialog = builder.create();
2178 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2179 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002180 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002181 mInput.requestFocus();
2182 InputMethodManager inputManager = (InputMethodManager)
2183 getSystemService(Context.INPUT_METHOD_SERVICE);
2184 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002185 }
2186 });
2187
2188 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 }
2190
2191 private void changeFolderName() {
2192 final String name = mInput.getText().toString();
2193 if (!TextUtils.isEmpty(name)) {
2194 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002195 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 mFolderInfo.title = name;
2197 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2198
Joe Onorato9c1289c2009-08-17 11:03:03 -04002199 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002200 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002201 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 } else {
2203 final FolderIcon folderIcon = (FolderIcon)
2204 mWorkspace.getViewForTag(mFolderInfo);
2205 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002206 // TODO: At some point we'll probably want some version of setting
2207 // the text for a folder icon.
2208 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 getWorkspace().requestLayout();
2210 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002211 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002212 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002213 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 }
2216 }
2217 cleanup();
2218 }
2219
2220 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 dismissDialog(DIALOG_RENAME_FOLDER);
2222 mWaitingForResult = false;
2223 mFolderInfo = null;
2224 }
2225 }
2226
Daniel Sandler843e8602010-06-07 14:59:01 -04002227 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2228 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002229 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002230 }
2231
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002232 public boolean isAllAppsButtonRank(int rank) {
2233 return mHotseat.isAllAppsButtonRank(rank);
2234 }
2235
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002236 // AllAppsView.Watcher
2237 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002238 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002239 mWorkspace.setVisibility(View.GONE);
2240 }
2241 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002242
2243 /**
2244 * Helper method for the cameraZoomIn/cameraZoomOut animations
2245 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002246 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002247 * @param scaleFactor The scale factor used for the zoom
2248 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002249 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002250 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002251 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002252 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002253
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002254 void updateWallpaperVisibility(boolean visible) {
2255 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2256 int curflags = getWindow().getAttributes().flags
2257 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2258 if (wpflags != curflags) {
2259 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2260 }
2261 }
2262
Michael Jurkabed61d22012-02-14 22:51:29 -08002263 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2264 if (v instanceof LauncherTransitionable) {
2265 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2266 }
Winson Chung70442722012-02-10 15:43:22 -08002267
2268 // Update the workspace transition step as well
2269 dispatchOnLauncherTransitionStep(v, 0f);
2270 }
2271
2272 private void dispatchOnLauncherTransitionStep(View v, float t) {
2273 if (v instanceof LauncherTransitionable) {
2274 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2275 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002276 }
2277
2278 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2279 if (v instanceof LauncherTransitionable) {
2280 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2281 }
Winson Chung70442722012-02-10 15:43:22 -08002282
2283 // Update the workspace transition step as well
2284 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002285 }
2286
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002287 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002288 * Things to test when changing the following seven functions.
2289 * - Home from workspace
2290 * - from center screen
2291 * - from other screens
2292 * - Home from all apps
2293 * - from center screen
2294 * - from other screens
2295 * - Back from all apps
2296 * - from center screen
2297 * - from other screens
2298 * - Launch app from workspace and quit
2299 * - with back
2300 * - with home
2301 * - Launch app from all apps and quit
2302 * - with back
2303 * - with home
2304 * - Go to a screen that's not the default, then all
2305 * apps, and launch and app, and go back
2306 * - with back
2307 * -with home
2308 * - On workspace, long press power and go back
2309 * - with back
2310 * - with home
2311 * - On all apps, long press power and go back
2312 * - with back
2313 * - with home
2314 * - On workspace, power off
2315 * - On all apps, power off
2316 * - Launch an app and turn off the screen while in that app
2317 * - Go back with home key
2318 * - Go back with back key TODO: make this not go to workspace
2319 * - From all apps
2320 * - From workspace
2321 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2322 * - From all apps
2323 * - From the center workspace
2324 * - From another workspace
2325 */
2326
2327 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002328 * Zoom the camera out from the workspace to reveal 'toView'.
2329 * Assumes that the view to show is anchored at either the very top or very bottom
2330 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002332 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002333 if (mStateAnimation != null) {
2334 mStateAnimation.cancel();
2335 mStateAnimation = null;
2336 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002337 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002338 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002339
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2341 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2342 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002343 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002344 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002345 final int startDelay =
2346 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002347
Michael Jurkab3e22d92011-10-31 15:58:33 -07002348 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349
Michael Jurkad74c9842011-07-10 12:44:21 -07002350 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002351 Animator workspaceAnim =
2352 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002353
2354 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002355 toView.setScaleX(scale);
2356 toView.setScaleY(scale);
2357 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2358 scaleAnim.
2359 scaleX(1f).scaleY(1f).
2360 setDuration(duration).
2361 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002362
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002364 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002365 final ObjectAnimator alphaAnim = ObjectAnimator
2366 .ofFloat(toView, "alpha", 0f, 1f)
2367 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002368 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002369 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2370 @Override
2371 public void onAnimationUpdate(ValueAnimator animation) {
2372 float t = (Float) animation.getAnimatedValue();
2373 dispatchOnLauncherTransitionStep(fromView, t);
2374 dispatchOnLauncherTransitionStep(toView, t);
2375 }
2376 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002377
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002378 // toView should appear right at the end of the workspace shrink
2379 // animation
2380 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002381 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002382 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002383
2384 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002385 boolean animationCancelled = false;
2386
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002387 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002388 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002389 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002390 // Prepare the position
2391 toView.setTranslationX(0.0f);
2392 toView.setTranslationY(0.0f);
2393 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002394 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002395 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002396 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002397 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002398 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2399 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002400
2401 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2402 // Hide the workspace scrollbar
2403 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002404 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002405 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002406 if (!animationCancelled) {
2407 updateWallpaperVisibility(false);
2408 }
2409 }
2410
Adam Cohencff6af82011-09-13 14:51:53 -07002411 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002412 public void onAnimationCancel(Animator animation) {
2413 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002414 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 });
2416
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002417 if (workspaceAnim != null) {
2418 mStateAnimation.play(workspaceAnim);
2419 }
Michael Jurka1899a362011-11-03 13:50:45 -07002420
2421 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002422 final ViewTreeObserver observer;
2423
Michael Jurkabed61d22012-02-14 22:51:29 -08002424 dispatchOnLauncherTransitionStart(fromView, animated, false);
2425 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002426
2427 // If any of the objects being animated haven't been measured/laid out
2428 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002429 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002430 (mWorkspace.getMeasuredWidth() == 0) ||
2431 (toView.getMeasuredWidth() == 0)) {
2432 observer = mWorkspace.getViewTreeObserver();
2433 delayAnim = true;
2434 } else {
2435 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002436 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002437
2438 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002439 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002440 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2441 public void onGlobalLayout() {
2442 mWorkspace.post(new Runnable() {
2443 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002444 // Check that mStateAnimation hasn't changed while
2445 // we waited for a layout pass
2446 if (mStateAnimation == stateAnimation) {
2447 // Need to update pivots for zoom if layout changed
2448 setPivotsForZoom(toView, scale);
2449 mStateAnimation.start();
2450 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002451 }
2452 });
2453 observer.removeGlobalOnLayoutListener(this);
2454 }
2455 };
2456 observer.addOnGlobalLayoutListener(delayedStart);
2457 } else {
2458 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002459 mStateAnimation.start();
2460 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002461 } else {
2462 toView.setTranslationX(0.0f);
2463 toView.setTranslationY(0.0f);
2464 toView.setScaleX(1.0f);
2465 toView.setScaleY(1.0f);
2466 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002467 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002468
Michael Jurkabed61d22012-02-14 22:51:29 -08002469 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2470 // Hide the workspace scrollbar
2471 mWorkspace.hideScrollingIndicator(true);
2472 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002473 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002474 dispatchOnLauncherTransitionStart(fromView, animated, false);
2475 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2476 dispatchOnLauncherTransitionStart(toView, animated, false);
2477 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002478 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002480 }
2481
2482 /**
2483 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002484 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002485 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002486 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002487 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2488 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002489
Michael Jurkab3e22d92011-10-31 15:58:33 -07002490 if (mStateAnimation != null) {
2491 mStateAnimation.cancel();
2492 mStateAnimation = null;
2493 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002494 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002495
Winson Chungf0ea4d32011-06-06 14:27:16 -07002496 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002497 final int fadeOutDuration =
2498 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002499 final float scaleFactor = (float)
2500 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2501 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002502 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002503 Animator workspaceAnim = null;
2504
2505 if (toState == State.WORKSPACE) {
2506 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2507 workspaceAnim = mWorkspace.getChangeStateAnimation(
2508 Workspace.State.NORMAL, animated, stagger);
2509 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2510 workspaceAnim = mWorkspace.getChangeStateAnimation(
2511 Workspace.State.SPRING_LOADED, animated);
2512 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002513
Michael Jurkab3e22d92011-10-31 15:58:33 -07002514 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002515 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002516 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002518 final float oldScaleX = fromView.getScaleX();
2519 final float oldScaleY = fromView.getScaleY();
2520
Michael Jurka159b4cc2012-01-17 03:00:35 -08002521 final LauncherViewPropertyAnimator scaleAnim =
2522 new LauncherViewPropertyAnimator(fromView);
2523 scaleAnim.
2524 scaleX(scaleFactor).scaleY(scaleFactor).
2525 setDuration(duration).
2526 setInterpolator(new Workspace.ZoomInInterpolator());
2527
2528 final ObjectAnimator alphaAnim = ObjectAnimator
2529 .ofFloat(fromView, "alpha", 1f, 0f)
2530 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002531 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002532 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2533 @Override
2534 public void onAnimationUpdate(ValueAnimator animation) {
2535 float t = 1f - (Float) animation.getAnimatedValue();
2536 dispatchOnLauncherTransitionStep(fromView, t);
2537 dispatchOnLauncherTransitionStep(toView, t);
2538 }
2539 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002540
Michael Jurkabed61d22012-02-14 22:51:29 -08002541 mStateAnimation = new AnimatorSet();
2542
2543 dispatchOnLauncherTransitionStart(fromView, animated, true);
2544 dispatchOnLauncherTransitionStart(toView, animated, true);
2545
2546 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002547 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002548 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002549 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002551 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2552 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002553 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002554 if (onCompleteRunnable != null) {
2555 onCompleteRunnable.run();
2556 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 }
2558 });
2559
Winson Chungf0ea4d32011-06-06 14:27:16 -07002560 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002561 if (workspaceAnim != null) {
2562 mStateAnimation.play(workspaceAnim);
2563 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002564 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002565 } else {
2566 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002567 dispatchOnLauncherTransitionStart(fromView, animated, true);
2568 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2569 dispatchOnLauncherTransitionStart(toView, animated, true);
2570 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002571 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002573 }
2574
Michael Jurkae326f182011-11-21 14:05:46 -08002575 @Override
2576 public void onTrimMemory(int level) {
2577 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002578 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002579 mAppsCustomizeTabHost.onTrimMemory();
2580 }
2581 }
2582
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002583 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002584 showWorkspace(animated, null);
2585 }
2586
2587 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002588 if (mState != State.WORKSPACE) {
2589 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002590 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002591
2592 // Show the search bar and hotseat
2593 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002594 // We only need to animate in the dock divider if we're going from spring loaded mode
2595 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002596
2597 // Set focus to the AppsCustomize button
2598 if (mAllAppsButton != null) {
2599 mAllAppsButton.requestFocus();
2600 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002601 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002602
Michael Jurkab737ee62011-11-15 15:57:22 -08002603 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002604
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002605 // Change the state *after* we've called all the transition code
2606 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002607
Winson Chung5fb63472011-02-02 17:03:37 -08002608 // Resume the auto-advance of widgets
2609 mUserPresent = true;
2610 updateRunning();
2611
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002612 // send an accessibility event to announce the context change
2613 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002614 }
2615
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616 void showAllApps(boolean animated) {
2617 if (mState != State.WORKSPACE) return;
2618
2619 showAppsCustomizeHelper(animated, false);
2620 mAppsCustomizeTabHost.requestFocus();
2621
2622 // Hide the search bar and hotseat
2623 mSearchDropTargetBar.hideSearchBar(animated);
2624
2625 // Change the state *after* we've called all the transition code
2626 mState = State.APPS_CUSTOMIZE;
2627
2628 // Pause the auto-advance of widgets until we are out of AllApps
2629 mUserPresent = false;
2630 updateRunning();
2631 closeFolder();
2632
2633 // Send an accessibility event to announce the context change
2634 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2635 }
2636
Adam Cohen7777d962011-08-18 18:58:38 -07002637 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002638 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002639 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002640 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002641 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002642 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002643 }
Adam Cohen7777d962011-08-18 18:58:38 -07002644
Adam Cohened66b2b2012-01-23 17:28:51 -08002645 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2646 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002647 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2648
Winson Chunge7a03942011-08-05 15:05:12 -07002649 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002650 @Override
2651 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002652 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002653 // Before we show workspace, hide all apps again because
2654 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2655 // clean up our state transition functions
2656 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002657 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002658 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002659 } else {
2660 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002661 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002662 }
2663 }, (extendedDelay ?
2664 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2665 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2666 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002667
Michael Jurkad3ef3062010-11-23 16:23:58 -08002668 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002669 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002670 final boolean animated = true;
2671 final boolean springLoaded = true;
2672 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002673 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002674 }
2675 // Otherwise, we are not in spring loaded mode, so don't do anything.
2676 }
2677
Michael Jurkab737ee62011-11-15 15:57:22 -08002678 void hideDockDivider() {
2679 if (mQsbDivider != null && mDockDivider != null) {
2680 mQsbDivider.setVisibility(View.INVISIBLE);
2681 mDockDivider.setVisibility(View.INVISIBLE);
2682 }
2683 }
2684
2685 void showDockDivider(boolean animated) {
2686 if (mQsbDivider != null && mDockDivider != null) {
2687 mQsbDivider.setVisibility(View.VISIBLE);
2688 mDockDivider.setVisibility(View.VISIBLE);
2689 if (mDividerAnimator != null) {
2690 mDividerAnimator.cancel();
2691 mQsbDivider.setAlpha(1f);
2692 mDockDivider.setAlpha(1f);
2693 mDividerAnimator = null;
2694 }
2695 if (animated) {
2696 mDividerAnimator = new AnimatorSet();
2697 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2698 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2699 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2700 mDividerAnimator.start();
2701 }
2702 }
2703 }
2704
Michael Jurkab3e22d92011-10-31 15:58:33 -07002705 void lockAllApps() {
2706 // TODO
2707 }
2708
2709 void unlockAllApps() {
2710 // TODO
2711 }
2712
Adam Cohenfc53cd22011-07-20 15:45:11 -07002713 public boolean isAllAppsCustomizeOpen() {
2714 return mState == State.APPS_CUSTOMIZE;
2715 }
2716
Winson Chungf0ea4d32011-06-06 14:27:16 -07002717 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002718 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002719 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002720 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002721 if (!LauncherApplication.isScreenLarge()) {
2722 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002723 if (mHotseat.getAlpha() != 1f) {
2724 int duration = mSearchDropTargetBar.getTransitionInDuration();
2725 mHotseat.animate().alpha(1f).setDuration(duration);
2726 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002727 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002728 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002729 }
2730 }
2731 }
2732
2733 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002734 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002735 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002736 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002737 if (!LauncherApplication.isScreenLarge()) {
2738 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002739 if (mHotseat.getAlpha() != 0f) {
2740 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2741 mHotseat.animate().alpha(0f).setDuration(duration);
2742 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002743 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002744 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002745 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002746 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002747 }
2748
Patrick Dubroy5f445422011-02-18 14:35:21 -08002749 /**
2750 * Add an item from all apps or customize onto the given workspace screen.
2751 * If layout is null, add to the current screen.
2752 */
2753 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002754 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002755 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002756 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002757 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002758
Winson Chungdff8ebb2011-09-08 17:25:31 -07002759 /** Maps the current orientation to an index for referencing orientation correct global icons */
2760 private int getCurrentOrientationIndexForGlobalIcons() {
2761 // default - 0, landscape - 1
2762 switch (getResources().getConfiguration().orientation) {
2763 case Configuration.ORIENTATION_LANDSCAPE:
2764 return 1;
2765 default:
2766 return 0;
2767 }
2768 }
2769
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002770 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002771 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002772 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002773 // Look for the toolbar icon specified in the activity meta-data
2774 Bundle metaData = packageManager.getActivityInfo(
2775 activityName, PackageManager.GET_META_DATA).metaData;
2776 if (metaData != null) {
2777 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2778 if (iconResId != 0) {
2779 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002780 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002781 }
2782 }
2783 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002784 // This can happen if the activity defines an invalid drawable
2785 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2786 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002787 } catch (Resources.NotFoundException nfe) {
2788 // This can happen if the activity defines an invalid drawable
2789 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2790 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002791 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002792 return null;
2793 }
2794
2795 // if successful in getting icon, return it; otherwise, set button to use default drawable
2796 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2797 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002798 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002799 Resources r = getResources();
2800 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2801 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002802
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002803 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002804 // If we were unable to find the icon via the meta-data, use a generic one
2805 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002806 toolbarIcon = r.getDrawable(fallbackDrawableId);
2807 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002808 if (button != null) {
2809 button.setCompoundDrawables(toolbarIcon, null, null, null);
2810 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002811 return null;
2812 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002813 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 toolbarIcon.getConstantState();
2818 }
2819 }
2820
2821 // if successful in getting icon, return it; otherwise, set button to use default drawable
2822 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2823 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2824 ImageView button = (ImageView) findViewById(buttonId);
2825 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2826
Michael Jurka19e0fc52011-07-22 18:00:21 -07002827 if (button != null) {
2828 // If we were unable to find the icon via the meta-data, use a
2829 // generic one
2830 if (toolbarIcon == null) {
2831 button.setImageResource(fallbackDrawableId);
2832 } else {
2833 button.setImageDrawable(toolbarIcon);
2834 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002835 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002836
2837 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2838
Michael Jurka0423dcf2010-10-05 14:56:18 -07002839 }
2840
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002841 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2842 TextView button = (TextView) findViewById(buttonId);
2843 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2844 }
2845
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002846 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002847 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002848 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002849 }
2850
Winson Chungbb185bd2011-11-21 12:31:42 -08002851 private void invalidatePressedFocusedStates(View container, View button) {
2852 if (container instanceof HolographicLinearLayout) {
2853 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2854 layout.invalidatePressedFocusedStates();
2855 } else if (button instanceof HolographicImageView) {
2856 HolographicImageView view = (HolographicImageView) button;
2857 view.invalidatePressedFocusedStates();
2858 }
2859 }
2860
Winson Chungc51db6a2011-10-05 11:44:49 -07002861 private boolean updateGlobalSearchIcon() {
2862 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002863 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2864 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002865 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002866 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002867
Winson Chung1cad91e2011-05-25 17:41:01 -07002868 final SearchManager searchManager =
2869 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2870 ComponentName activityName = searchManager.getGlobalSearchActivity();
2871 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002872 int coi = getCurrentOrientationIndexForGlobalIcons();
2873 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002874 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002875 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002876 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2877 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002878 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002879 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002880 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002881 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002882 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002883 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2884 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2885 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002886 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002887 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002888 }
2889 }
2890
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002891 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002892 final View searchButtonContainer = findViewById(R.id.search_button_container);
2893 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002894 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002895 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002896 }
2897
Winson Chungc51db6a2011-10-05 11:44:49 -07002898 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002899 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002901 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002902
Winson Chungc51db6a2011-10-05 11:44:49 -07002903 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002904 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2905 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002906 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002907 int coi = getCurrentOrientationIndexForGlobalIcons();
2908 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002909 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002910 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002911 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002912 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002913 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002914 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002915 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002916 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002917 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002918 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002919 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002920 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002921 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002922
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002923 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002924 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2925 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002926 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002927 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002928 }
2929
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002930 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002931 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002932 */
2933 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002934 final View marketButton = findViewById(R.id.market_button);
2935 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2936 // Find the app market activity by resolving an intent.
2937 // (If multiple app markets are installed, it will return the ResolverActivity.)
2938 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002939 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002940 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002941 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002942 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002943 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002944 marketButton.setVisibility(View.VISIBLE);
2945 } else {
2946 // We should hide and disable the view so that we don't try and restore the visibility
2947 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2948 marketButton.setVisibility(View.GONE);
2949 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002950 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002951 }
2952
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002953 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002954 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002955 }
2956
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002957 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 * Displays the shortcut creation dialog and launches, if necessary, the
2959 * appropriate activity.
2960 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002961 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002962 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2963 DialogInterface.OnShowListener {
2964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002969
Winson Chung55b65502011-05-26 12:03:43 -07002970 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2971 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002972 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 AlertDialog dialog = builder.create();
2975 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002976 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002977 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 return dialog;
2980 }
2981
2982 public void onCancel(DialogInterface dialog) {
2983 mWaitingForResult = false;
2984 cleanup();
2985 }
2986
Romain Guycbb89e42009-06-08 15:52:54 -07002987 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002988 mWaitingForResult = false;
2989 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002990 }
2991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002993 try {
2994 dismissDialog(DIALOG_CREATE_SHORTCUT);
2995 } catch (Exception e) {
2996 // An exception is thrown if the dialog is not visible, which is fine
2997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002998 }
2999
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003000 /**
3001 * Handle the action clicked in the "Add to home" dialog.
3002 */
3003 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003004 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003005
Winson Chung55b65502011-05-26 12:03:43 -07003006 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3007 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003008 case AddAdapter.ITEM_APPLICATION: {
3009 if (mAppsCustomizeTabHost != null) {
3010 mAppsCustomizeTabHost.selectAppsTab();
3011 }
3012 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003013 break;
3014 }
Winson Chung55b65502011-05-26 12:03:43 -07003015 case AddAdapter.ITEM_APPWIDGET: {
3016 if (mAppsCustomizeTabHost != null) {
3017 mAppsCustomizeTabHost.selectWidgetsTab();
3018 }
3019 showAllApps(true);
3020 break;
3021 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003022 case AddAdapter.ITEM_WALLPAPER: {
3023 startWallpaper();
3024 break;
3025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 }
3027 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003028
3029 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003030 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 }
3033
3034 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003035 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003036 */
3037 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3038 @Override
3039 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003040 closeSystemDialogs();
3041 }
3042 }
3043
3044 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003045 * Receives notifications whenever the appwidgets are reset.
3046 */
3047 private class AppWidgetResetObserver extends ContentObserver {
3048 public AppWidgetResetObserver() {
3049 super(new Handler());
3050 }
3051
3052 @Override
3053 public void onChange(boolean selfChange) {
3054 onAppWidgetReset();
3055 }
3056 }
3057
3058 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003059 * If the activity is currently paused, signal that we need to re-run the loader
3060 * in onResume.
3061 *
3062 * This needs to be called from incoming places where resources might have been loaded
3063 * while we are paused. That is becaues the Configuration might be wrong
3064 * when we're not running, and if it comes back to what it was when we
3065 * were paused, we are not restarted.
3066 *
3067 * Implementation of the method from LauncherModel.Callbacks.
3068 *
3069 * @return true if we are currently paused. The caller might be able to
3070 * skip some work in that case since we will come back again.
3071 */
3072 public boolean setLoadOnResume() {
3073 if (mPaused) {
3074 Log.i(TAG, "setLoadOnResume");
3075 mOnResumeNeedsLoad = true;
3076 return true;
3077 } else {
3078 return false;
3079 }
3080 }
3081
3082 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003084 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003086 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003087 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003088 } else {
3089 return SCREEN_COUNT / 2;
3090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003092
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 /**
3094 * Refreshes the shortcuts shown on the workspace.
3095 *
3096 * Implementation of the method from LauncherModel.Callbacks.
3097 */
3098 public void startBinding() {
3099 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003100
Winson Chungf0c6ae02012-03-21 16:10:31 -07003101 mNewShortcutAnimatePage = -1;
3102 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003103 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 int count = workspace.getChildCount();
3105 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003106 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003107 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3108 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003110 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 if (mHotseat != null) {
3112 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 }
3114 }
3115
3116 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 * Bind the items start-end from the list.
3118 *
3119 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003122 setLoadOnResume();
3123
Winson Chungf0c6ae02012-03-21 16:10:31 -07003124 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3125 Set<String> newApps = new HashSet<String>();
3126 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3127
3128 Workspace workspace = mWorkspace;
3129 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003131
3132 // Short circuit if we are loading dock items for a configuration which has no dock
3133 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3134 mHotseat == null) {
3135 continue;
3136 }
3137
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 switch (item.itemType) {
3139 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3140 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003141 ShortcutInfo info = (ShortcutInfo) item;
3142 String uri = info.intent.toUri(0).toString();
3143 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003144 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3145 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003146 if (newApps.contains(uri)) {
3147 newApps.remove(uri);
3148
3149 // Prepare the view to be animated up
3150 shortcut.setAlpha(0f);
3151 shortcut.setScaleX(0f);
3152 shortcut.setScaleY(0f);
3153 mNewShortcutAnimatePage = item.screen;
3154 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3155 mNewShortcutAnimateViews.add(shortcut);
3156 }
3157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003159 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003161 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003162 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003163 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3164 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003168
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 }
3171
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 /**
3173 * Implementation of the method from LauncherModel.Callbacks.
3174 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003175 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003176 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003177 sFolders.clear();
3178 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180
3181 /**
3182 * Add the views for a widget to the workspace.
3183 *
3184 * Implementation of the method from LauncherModel.Callbacks.
3185 */
3186 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003187 setLoadOnResume();
3188
Daniel Sandler843e8602010-06-07 14:59:01 -04003189 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3190 if (DEBUG_WIDGETS) {
3191 Log.d(TAG, "bindAppWidget: " + item);
3192 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 final Workspace workspace = mWorkspace;
3194
3195 final int appWidgetId = item.appWidgetId;
3196 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003197 if (DEBUG_WIDGETS) {
3198 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3199 }
3200
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3202
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3204 item.hostView.setTag(item);
3205
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003207 item.cellY, item.spanX, item.spanY, false);
3208
Adam Cohended9f8d2010-11-03 13:25:16 -07003209 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3210
Joe Onorato9c1289c2009-08-17 11:03:03 -04003211 workspace.requestLayout();
3212
Daniel Sandler843e8602010-06-07 14:59:01 -04003213 if (DEBUG_WIDGETS) {
3214 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3215 + (SystemClock.uptimeMillis()-start) + "ms");
3216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217 }
3218
3219 /**
3220 * Callback saying that there aren't any more items to bind.
3221 *
3222 * Implementation of the method from LauncherModel.Callbacks.
3223 */
3224 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003225 setLoadOnResume();
3226
Joe Onorato9c1289c2009-08-17 11:03:03 -04003227 if (mSavedState != null) {
3228 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003229 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003231 mSavedState = null;
3232 }
3233
3234 if (mSavedInstanceState != null) {
3235 super.onRestoreInstanceState(mSavedInstanceState);
3236 mSavedInstanceState = null;
3237 }
3238
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003239 // If we received the result of any pending adds while the loader was running (e.g. the
3240 // widget configuration forced an orientation change), process them now.
3241 for (int i = 0; i < sPendingAddList.size(); i++) {
3242 completeAdd(sPendingAddList.get(i));
3243 }
3244 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245
Winson Chungc51db6a2011-10-05 11:44:49 -07003246 // Update the market app icon as necessary (the other icons will be managed in response to
3247 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003248 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003249
Winson Chungf0c6ae02012-03-21 16:10:31 -07003250 // Animate up any icons as necessary
3251 if (mVisible || mWorkspaceLoading) {
3252 Runnable newAppsRunnable = new Runnable() {
3253 @Override
3254 public void run() {
3255 runNewAppsAnimation();
3256 }
3257 };
3258 if (mNewShortcutAnimatePage > -1 &&
3259 mNewShortcutAnimatePage != mWorkspace.getCurrentPage()) {
3260 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3261 } else {
3262 newAppsRunnable.run();
3263 }
3264 }
3265
3266 mWorkspaceLoading = false;
3267 }
3268
3269 /**
3270 * Runs a new animation that scales up icons that were added while Launcher was in the
3271 * background.
3272 */
3273 private void runNewAppsAnimation() {
3274 AnimatorSet anim = new AnimatorSet();
3275 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3276 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3277 @Override
3278 public int compare(View a, View b) {
3279 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3280 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3281 int cellCountX = LauncherModel.getCellCountX();
3282 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3283 }
3284 });
3285 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3286 View v = mNewShortcutAnimateViews.get(i);
3287 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3288 PropertyValuesHolder.ofFloat("alpha", 1f),
3289 PropertyValuesHolder.ofFloat("scaleX", 1f),
3290 PropertyValuesHolder.ofFloat("scaleY", 1f));
3291 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3292 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3293 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3294 bounceAnims.add(bounceAnim);
3295 }
3296 anim.playTogether(bounceAnims);
3297 anim.addListener(new AnimatorListenerAdapter() {
3298 @Override
3299 public void onAnimationEnd(Animator animation) {
3300 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3301 }
3302 });
3303 anim.start();
3304
3305 // Clean up
3306 mNewShortcutAnimatePage = -1;
3307 mNewShortcutAnimateViews.clear();
3308 new Thread("clearNewAppsThread") {
3309 public void run() {
3310 mSharedPrefs.edit()
3311 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3312 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3313 .commit();
3314 }
3315 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003316 }
3317
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003318 @Override
3319 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003320 boolean searchVisible = updateGlobalSearchIcon();
3321 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3322 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003323 }
3324
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003325 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003326 * Add the icons for all apps.
3327 *
3328 * Implementation of the method from LauncherModel.Callbacks.
3329 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003330 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3331 // Remove the progress bar entirely; we could also make it GONE
3332 // but better to remove it since we know it's not going to be used
3333 View progressBar = mAppsCustomizeTabHost.
3334 findViewById(R.id.apps_customize_progress_bar);
3335 if (progressBar != null) {
3336 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003337 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003338 // We just post the call to setApps so the user sees the progress bar
3339 // disappear-- otherwise, it just looks like the progress bar froze
3340 // which doesn't look great
3341 mAppsCustomizeTabHost.post(new Runnable() {
3342 public void run() {
3343 if (mAppsCustomizeContent != null) {
3344 mAppsCustomizeContent.setApps(apps);
3345 }
3346 }
3347 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 }
3349
3350 /**
3351 * A package was installed.
3352 *
3353 * Implementation of the method from LauncherModel.Callbacks.
3354 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003355 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003356 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003358
Winson Chung785d2eb2011-04-14 16:08:02 -07003359 if (mAppsCustomizeContent != null) {
3360 mAppsCustomizeContent.addApps(apps);
3361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 }
3363
3364 /**
3365 * A package was updated.
3366 *
3367 * Implementation of the method from LauncherModel.Callbacks.
3368 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003369 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003370 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003371 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003372 if (mWorkspace != null) {
3373 mWorkspace.updateShortcuts(apps);
3374 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003375
Winson Chung785d2eb2011-04-14 16:08:02 -07003376 if (mAppsCustomizeContent != null) {
3377 mAppsCustomizeContent.updateApps(apps);
3378 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 }
3380
3381 /**
3382 * A package was uninstalled.
3383 *
3384 * Implementation of the method from LauncherModel.Callbacks.
3385 */
Joe Onorato36115782010-06-17 13:28:48 -04003386 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003388 if (permanent) {
3389 mWorkspace.removeItems(apps);
3390 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391
Winson Chung785d2eb2011-04-14 16:08:02 -07003392 if (mAppsCustomizeContent != null) {
3393 mAppsCustomizeContent.removeApps(apps);
3394 }
Winson Chunga1820962011-10-03 16:31:06 -07003395
3396 // Notify the drag controller
3397 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 }
Joe Onoratobe386092009-11-17 17:32:16 -08003399
3400 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003401 * A number of packages were updated.
3402 */
3403 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003404 if (mAppsCustomizeContent != null) {
3405 mAppsCustomizeContent.onPackagesUpdated();
3406 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003407 }
3408
Winson Chung400438b2011-01-16 17:53:48 -08003409 private int mapConfigurationOriActivityInfoOri(int configOri) {
3410 final Display d = getWindowManager().getDefaultDisplay();
3411 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3412 switch (d.getRotation()) {
3413 case Surface.ROTATION_0:
3414 case Surface.ROTATION_180:
3415 // We are currently in the same basic orientation as the natural orientation
3416 naturalOri = configOri;
3417 break;
3418 case Surface.ROTATION_90:
3419 case Surface.ROTATION_270:
3420 // We are currently in the other basic orientation to the natural orientation
3421 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3422 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3423 break;
3424 }
3425
3426 int[] oriMap = {
3427 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3428 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3429 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3430 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3431 };
3432 // Since the map starts at portrait, we need to offset if this device's natural orientation
3433 // is landscape.
3434 int indexOffset = 0;
3435 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3436 indexOffset = 1;
3437 }
3438 return oriMap[(d.getRotation() + indexOffset) % 4];
3439 }
Adam Cohen446e9402011-09-15 18:21:21 -07003440
3441 public void lockScreenOrientationOnLargeUI() {
3442 if (LauncherApplication.isScreenLarge()) {
3443 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3444 .getConfiguration().orientation));
3445 }
Winson Chung400438b2011-01-16 17:53:48 -08003446 }
Adam Cohen446e9402011-09-15 18:21:21 -07003447 public void unlockScreenOrientationOnLargeUI() {
3448 if (LauncherApplication.isScreenLarge()) {
3449 mHandler.postDelayed(new Runnable() {
3450 public void run() {
3451 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3452 }
3453 }, mRestoreScreenOrientationDelay);
3454 }
Winson Chung400438b2011-01-16 17:53:48 -08003455 }
3456
Winson Chung82f55532011-08-09 14:14:23 -07003457 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003458 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003459 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003460 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003461
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003463 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003464 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3465 Cling cling = (Cling) findViewById(clingId);
3466 if (cling != null) {
3467 cling.init(this, positionData);
3468 cling.setVisibility(View.VISIBLE);
3469 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3470 if (animate) {
3471 cling.buildLayer();
3472 cling.setAlpha(0f);
3473 cling.animate()
3474 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003475 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003476 .setDuration(SHOW_CLING_DURATION)
3477 .setStartDelay(delay)
3478 .start();
3479 } else {
3480 cling.setAlpha(1f);
3481 }
3482 }
3483 return cling;
3484 }
3485 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003486 if (cling != null) {
3487 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003488 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003489 anim.addListener(new AnimatorListenerAdapter() {
3490 public void onAnimationEnd(Animator animation) {
3491 cling.setVisibility(View.GONE);
3492 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003493 // We should update the shared preferences on a background thread
3494 new Thread("dismissClingThread") {
3495 public void run() {
3496 SharedPreferences.Editor editor = mSharedPrefs.edit();
3497 editor.putBoolean(flag, true);
3498 editor.commit();
3499 }
3500 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003501 };
3502 });
3503 anim.start();
3504 }
3505 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003506 private void removeCling(int id) {
3507 final View cling = findViewById(id);
3508 if (cling != null) {
3509 final ViewGroup parent = (ViewGroup) cling.getParent();
3510 parent.post(new Runnable() {
3511 @Override
3512 public void run() {
3513 parent.removeView(cling);
3514 }
3515 });
3516 }
3517 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003518 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003519 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003520 if (isClingsEnabled() &&
3521 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003522 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003523 } else {
3524 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003525 }
3526 }
3527 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003528 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003529 if (isClingsEnabled() &&
3530 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003531 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003532 } else {
3533 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003534 }
3535 }
3536 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003537 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003538 if (isClingsEnabled() &&
3539 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3540 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003541 } else {
3542 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003543 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003544 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003545 }
3546 public boolean isFolderClingVisible() {
3547 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003548 if (cling != null) {
3549 return cling.getVisibility() == View.VISIBLE;
3550 }
3551 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003552 }
Winson Chung82f55532011-08-09 14:14:23 -07003553 public void dismissWorkspaceCling(View v) {
3554 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003555 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003556 }
3557 public void dismissAllAppsCling(View v) {
3558 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003559 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3560 }
3561 public void dismissFolderCling(View v) {
3562 Cling cling = (Cling) findViewById(R.id.folder_cling);
3563 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003564 }
3565
Winson Chung80baf5a2010-08-09 16:03:15 -07003566 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003567 * Prints out out state for debugging.
3568 */
3569 public void dumpState() {
3570 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003571 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003572 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3573 Log.d(TAG, "mRestoring=" + mRestoring);
3574 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3575 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003576 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003577 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003578
Winson Chung785d2eb2011-04-14 16:08:02 -07003579 if (mAppsCustomizeContent != null) {
3580 mAppsCustomizeContent.dumpState();
3581 }
Joe Onoratobe386092009-11-17 17:32:16 -08003582 Log.d(TAG, "END launcher2 dump state");
3583 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003584
3585 @Override
3586 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3587 super.dump(prefix, fd, writer, args);
3588 writer.println(" ");
3589 writer.println("Debug logs: ");
3590 for (int i = 0; i < sDumpLogs.size(); i++) {
3591 writer.println(" " + sDumpLogs.get(i));
3592 }
3593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594}
Michael Jurka2763be32011-02-24 11:19:57 -08003595
Michael Jurkaabded662011-03-04 12:06:57 -08003596interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003597 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003598 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003599 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003600 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003601}