blob: 03c5c08a60a813dfb92992c02dd03ad46cf2ff65 [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;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020058import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070061import android.os.StrictMode;
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;
Adam Cohenf16e5712011-01-13 13:31:45 -080088import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070093import android.widget.TextView;
94import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070095
Adam Cohendf2cc412011-04-27 16:56:57 -070096import com.android.common.Search;
97import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070098import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
112import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114/**
115 * Default launcher application.
116 */
Michael Jurka946ad472010-07-09 18:05:18 -0700117public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700118 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
119 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800120 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400124 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700125 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
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
Winson Chunga2413752012-04-03 14:22:34 -0700185 // How long to wait before the new-shortcut animation automatically pans the workspace
186 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800195 private View mQsbDivider;
196 private View mDockDivider;
197 private DragLayer mDragLayer;
198 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private AppWidgetManager mAppWidgetManager;
201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Michael Jurkac9d95c52011-08-29 14:03:34 -0700203 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Winson Chung3d503fb2011-07-13 17:25:49 -0700208 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800209 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700210
Winson Chungc51db6a2011-10-05 11:44:49 -0700211 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700212 private AppsCustomizeTabHost mAppsCustomizeTabHost;
213 private AppsCustomizePagedView mAppsCustomizeContent;
214 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700239 private Intent mAppMarketIntent = null;
240
Adam Cohended9f8d2010-11-03 13:25:16 -0700241 // Related to the auto-advancing of widgets
242 private final int ADVANCE_MSG = 1;
243 private final int mAdvanceInterval = 20000;
244 private final int mAdvanceStagger = 250;
245 private long mAutoAdvanceSentTime;
246 private long mAutoAdvanceTimeLeft = -1;
247 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
248 new HashMap<View, AppWidgetProviderInfo>();
249
Winson Chung400438b2011-01-16 17:53:48 -0800250 // Determines how long to wait after a rotation before restoring the screen orientation to
251 // match the sensor state.
252 private final int mRestoreScreenOrientationDelay = 500;
253
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700255 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
256 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
257 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800258
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700259 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800260 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700261
Winson Chung46353de2012-02-16 14:05:10 -0800262 // We only want to get the SharedPreferences once since it does an FS stat each time we get
263 // it from the context.
264 private SharedPreferences mSharedPrefs;
265
Winson Chungf0c6ae02012-03-21 16:10:31 -0700266 // Holds the page that we need to animate to, and the icon views that we need to animate up
267 // when we scroll to that page on resume.
268 private int mNewShortcutAnimatePage = -1;
269 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700270
Michael Jurkaddd62e92011-02-16 17:49:14 -0800271 private BubbleTextView mWaitingForResume;
272
Michael Jurkac1f5d262011-09-30 19:32:27 -0700273 private Runnable mBuildLayersRunnable = new Runnable() {
274 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700275 if (mWorkspace != null) {
276 mWorkspace.buildPageHardwareLayers();
277 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700278 }
279 };
280
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281 private static ArrayList<PendingAddArguments> sPendingAddList
282 = new ArrayList<PendingAddArguments>();
283
284 private static class PendingAddArguments {
285 int requestCode;
286 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700287 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800288 int screen;
289 int cellX;
290 int cellY;
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 @Override
294 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700295 if (DEBUG_STRICT_MODE) {
296 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
297 .detectDiskReads()
298 .detectDiskWrites()
299 .detectNetwork() // or .detectAll() for all detectable problems
300 .penaltyLog()
301 .build());
302 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
303 .detectLeakedSqlLiteObjects()
304 .detectLeakedClosableObjects()
305 .penaltyLog()
306 .penaltyDeath()
307 .build());
308 }
309
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800311 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
313 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800314 mModel = app.setLauncher(this);
315 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400316 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700318
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700319 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700320 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
321 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200324 android.os.Debug.startMethodTracing(
325 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 }
327
328 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 setContentView(R.layout.launcher);
330 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700331 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800333 registerContentObservers();
334
Joe Onorato7c312c12009-08-13 21:36:53 -0700335 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 mSavedState = savedInstanceState;
338 restoreState(mSavedState);
339
Winson Chunga12a2502010-12-20 14:41:35 -0800340 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700341 if (mAppsCustomizeContent != null) {
342 mAppsCustomizeContent.onPackagesUpdated();
343 }
Winson Chunga12a2502010-12-20 14:41:35 -0800344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 if (PROFILE_STARTUP) {
346 android.os.Debug.stopMethodTracing();
347 }
348
349 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 }
352
Michael Jurkac57b7a82011-08-09 22:02:20 -0700353 if (!mModel.isAllAppsLoaded()) {
354 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
355 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
356 }
357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 // For handling default keys
359 mDefaultKeySsb = new SpannableStringBuilder();
360 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800361
362 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
363 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800364
Winson Chungc51db6a2011-10-05 11:44:49 -0700365 boolean searchVisible = false;
366 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700368 int coi = getCurrentOrientationIndexForGlobalIcons();
369 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
370 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700371 updateAppMarketIcon();
372 searchVisible = updateGlobalSearchIcon();
373 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sGlobalSearchIcon[coi] != null) {
376 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700377 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700378 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700379 if (sVoiceSearchIcon[coi] != null) {
380 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700381 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700382 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700383 if (sAppMarketIcon[coi] != null) {
384 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800385 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700386 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700387
Adam Cohen23a4d302011-12-01 17:26:44 -0800388 final String forceEnableRotation =
389 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
390
Adam Cohen446e9402011-09-15 18:21:21 -0700391 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800392 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700393 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
394 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 }
Romain Guycbb89e42009-06-08 15:52:54 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700398 if (sLocaleConfiguration == null) {
399 new AsyncTask<Void, Void, LocaleConfiguration>() {
400 @Override
401 protected LocaleConfiguration doInBackground(Void... unused) {
402 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
403 readConfiguration(Launcher.this, localeConfiguration);
404 return localeConfiguration;
405 }
406
407 @Override
408 protected void onPostExecute(LocaleConfiguration result) {
409 sLocaleConfiguration = result;
410 checkForLocaleChange(); // recursive, but now with a locale configuration
411 }
412 }.execute();
413 return;
414 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final Configuration configuration = getResources().getConfiguration();
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final String locale = configuration.locale.toString();
420
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 final int mcc = configuration.mcc;
423
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 final int mnc = configuration.mnc;
426
Romain Guy84f296c2009-11-04 15:00:44 -0800427 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428
Romain Guy84f296c2009-11-04 15:00:44 -0800429 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 sLocaleConfiguration.locale = locale;
431 sLocaleConfiguration.mcc = mcc;
432 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700433
Joe Onorato0589f0f2010-02-08 13:44:00 -0800434 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700435
436 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
437 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700438 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700439 public void run() {
440 writeConfiguration(Launcher.this, localeConfiguration);
441 }
442 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700443 }
444 }
445
446 private static class LocaleConfiguration {
447 public String locale;
448 public int mcc = -1;
449 public int mnc = -1;
450 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700451
Romain Guy98d01652009-06-30 16:21:04 -0700452 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
453 DataInputStream in = null;
454 try {
455 in = new DataInputStream(context.openFileInput(PREFERENCES));
456 configuration.locale = in.readUTF();
457 configuration.mcc = in.readInt();
458 configuration.mnc = in.readInt();
459 } catch (FileNotFoundException e) {
460 // Ignore
461 } catch (IOException e) {
462 // Ignore
463 } finally {
464 if (in != null) {
465 try {
466 in.close();
467 } catch (IOException e) {
468 // Ignore
469 }
470 }
471 }
472 }
473
474 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
475 DataOutputStream out = null;
476 try {
477 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
478 out.writeUTF(configuration.locale);
479 out.writeInt(configuration.mcc);
480 out.writeInt(configuration.mnc);
481 out.flush();
482 } catch (FileNotFoundException e) {
483 // Ignore
484 } catch (IOException e) {
485 //noinspection ResultOfMethodCallIgnored
486 context.getFileStreamPath(PREFERENCES).delete();
487 } finally {
488 if (out != null) {
489 try {
490 out.close();
491 } catch (IOException e) {
492 // Ignore
493 }
494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 }
496 }
497
Adam Cohen716b51e2011-06-30 12:09:54 -0700498 public DragLayer getDragLayer() {
499 return mDragLayer;
500 }
501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 static int getScreen() {
503 synchronized (sLock) {
504 return sScreen;
505 }
506 }
507
508 static void setScreen(int screen) {
509 synchronized (sLock) {
510 sScreen = screen;
511 }
512 }
513
Winson Chung557d6ed2011-07-08 15:34:52 -0700514 /**
515 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
516 * a configuration step, this allows the proper animations to run after other transitions.
517 */
518 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700519 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800520 switch (args.requestCode) {
521 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700522 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
523 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800524 break;
525 case REQUEST_PICK_SHORTCUT:
526 processShortcut(args.intent);
527 break;
528 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700529 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
530 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700531 result = true;
532 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800533 case REQUEST_PICK_APPWIDGET:
534 addAppWidgetFromPick(args.intent);
535 break;
536 case REQUEST_CREATE_APPWIDGET:
537 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800538 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700539 result = true;
540 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800541 case REQUEST_PICK_WALLPAPER:
542 // We just wanted the activity result here so we can clear mWaitingForResult
543 break;
544 }
Michael Jurka27614d22012-04-02 06:40:22 -0700545 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
546 // if you turned the screen off and then back while in All Apps, Launcher would not
547 // return to the workspace. Clearing mAddInfo.container here fixes this issue
548 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700549 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800553 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700554 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800555 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
556 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700557 mWaitingForResult = false;
558
Adam Cohened66b2b2012-01-23 17:28:51 -0800559 // We have special handling for widgets
560 if (isWidgetDrop) {
561 int appWidgetId = data != null ?
562 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
563 completeTwoStageWidgetDrop(resultCode, appWidgetId);
564 return;
565 }
566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 // The pattern used here is that a user PICKs a specific application,
568 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700569
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
571 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700572 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800573 final PendingAddArguments args = new PendingAddArguments();
574 args.requestCode = requestCode;
575 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700576 args.container = mPendingAddInfo.container;
577 args.screen = mPendingAddInfo.screen;
578 args.cellX = mPendingAddInfo.cellX;
579 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800580 if (isWorkspaceLocked()) {
581 sPendingAddList.add(args);
582 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700583 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800586 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700587 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800588 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
589 null);
590 }
591
592 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
593 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
594 Runnable onCompleteRunnable = null;
595 int animationType = 0;
596
597 if (resultCode == RESULT_OK) {
598 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
599 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
600 mWidgetBeingConfigured.info);
601 mWidgetBeingConfigured.boundWidget = layout;
602 onCompleteRunnable = new Runnable() {
603 @Override
604 public void run() {
605 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
606 mPendingAddInfo.screen, layout, null);
607 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
608 null);
609 }
610 };
611 } else if (resultCode == RESULT_CANCELED) {
612 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
613 onCompleteRunnable = new Runnable() {
614 @Override
615 public void run() {
616 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
617 null);
618 }
619 };
620 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800621 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800623 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800624 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
626
627 @Override
628 protected void onResume() {
629 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700630
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800631 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700632 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400633 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700634 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400635 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700636 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
Winson Chunge4e50662012-01-23 14:45:13 -0800638
639 // Reset the pressed state of icons that were locked in the press state while activities
640 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800641 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800642 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800643 mWaitingForResume.setStayPressed(false);
644 }
Winson Chunge4e50662012-01-23 14:45:13 -0800645 if (mAppsCustomizeContent != null) {
646 // Resets the previous all apps icon press state
647 mAppsCustomizeContent.resetDrawableState();
648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
650
651 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700652 protected void onPause() {
653 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700654 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800655 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700656 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700657 }
Romain Guycbb89e42009-06-08 15:52:54 -0700658
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700659 @Override
660 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 // Flag the loader to stop early before switching
662 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700663 if (mAppsCustomizeContent != null) {
664 mAppsCustomizeContent.surrender();
665 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800666 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700667 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700668
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800669 // We can't hide the IME if it was forced open. So don't bother
670 /*
671 @Override
672 public void onWindowFocusChanged(boolean hasFocus) {
673 super.onWindowFocusChanged(hasFocus);
674
675 if (hasFocus) {
676 final InputMethodManager inputManager = (InputMethodManager)
677 getSystemService(Context.INPUT_METHOD_SERVICE);
678 WindowManager.LayoutParams lp = getWindow().getAttributes();
679 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
680 android.os.Handler()) {
681 protected void onReceiveResult(int resultCode, Bundle resultData) {
682 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
683 }
684 });
685 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
686 }
687 }
688 */
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 private boolean acceptFilter() {
691 final InputMethodManager inputManager = (InputMethodManager)
692 getSystemService(Context.INPUT_METHOD_SERVICE);
693 return !inputManager.isFullscreenMode();
694 }
695
696 @Override
697 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700698 final int uniChar = event.getUnicodeChar();
699 final boolean handled = super.onKeyDown(keyCode, event);
700 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
701 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
703 keyCode, event);
704 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700705 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700706 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700707 // showSearchDialog()
708 // If there are multiple keystrokes before the search dialog takes focus,
709 // onSearchRequested() will be called for every keystroke,
710 // but it is idempotent, so it's fine.
711 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713 }
714
Joe Onorato8a9625e2010-01-28 15:55:35 -0800715 // Eat the long press event so the keyboard doesn't come up.
716 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
717 return true;
718 }
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 return handled;
721 }
722
Karl Rosaen138a0412009-04-23 19:00:21 -0700723 private String getTypedText() {
724 return mDefaultKeySsb.toString();
725 }
726
727 private void clearTypedText() {
728 mDefaultKeySsb.clear();
729 mDefaultKeySsb.clearSpans();
730 Selection.setSelection(mDefaultKeySsb, 0);
731 }
732
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700734 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
735 * State
736 */
737 private static State intToState(int stateOrdinal) {
738 State state = State.WORKSPACE;
739 final State[] stateValues = State.values();
740 for (int i = 0; i < stateValues.length; i++) {
741 if (stateValues[i].ordinal() == stateOrdinal) {
742 state = stateValues[i];
743 break;
744 }
745 }
746 return state;
747 }
748
749 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 * Restores the previous state, if it exists.
751 *
752 * @param savedState The previous state.
753 */
754 private void restoreState(Bundle savedState) {
755 if (savedState == null) {
756 return;
757 }
758
Michael Jurka883f55b2010-10-21 15:47:14 -0700759 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700760 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800761 showAllApps(false);
762 }
763
Winson Chungf0c6ae02012-03-21 16:10:31 -0700764 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700766 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
Winson Chung3d503fb2011-07-13 17:25:49 -0700769 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
770 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771
Winson Chung3d503fb2011-07-13 17:25:49 -0700772 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
773 mPendingAddInfo.container = pendingAddContainer;
774 mPendingAddInfo.screen = pendingAddScreen;
775 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
776 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 mRestoring = true;
778 }
779
780 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
781 if (renameFolder) {
782 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700783 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 mRestoring = true;
785 }
Winson Chunga12a2502010-12-20 14:41:35 -0800786
Winson Chung785d2eb2011-04-14 16:08:02 -0700787
788 // Restore the AppsCustomize tab
789 if (mAppsCustomizeTabHost != null) {
790 String curTab = savedState.getString("apps_customize_currentTab");
791 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700792 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700793 mAppsCustomizeContent.setContentType(
794 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
795 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700796 mAppsCustomizeContent.loadAssociatedPages(
797 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700798 }
799
Winson Chung5afbf7b2011-07-25 11:53:08 -0700800 int currentIndex = savedState.getInt("apps_customize_currentIndex");
801 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700802 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
804
805 /**
806 * Finds all the views we need and configure them properly.
807 */
808 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700809 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400810
Winson Chung4c98d922011-05-31 16:50:48 -0700811 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
812 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800813 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
814 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815
Winson Chung4c98d922011-05-31 16:50:48 -0700816 // Setup the drag layer
817 mDragLayer.setup(this, dragController);
818
Winson Chung3d503fb2011-07-13 17:25:49 -0700819 // Setup the hotseat
820 mHotseat = (Hotseat) findViewById(R.id.hotseat);
821 if (mHotseat != null) {
822 mHotseat.setup(this);
823 }
824
Winson Chung4c98d922011-05-31 16:50:48 -0700825 // Setup the workspace
826 mWorkspace.setHapticFeedbackEnabled(false);
827 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700828 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700829 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700830
Winson Chungf0ea4d32011-06-06 14:27:16 -0700831 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700832 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700833
Winson Chungf0ea4d32011-06-06 14:27:16 -0700834 // Setup AppsCustomize
835 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
836 findViewById(R.id.apps_customize_pane);
837 mAppsCustomizeContent = (AppsCustomizePagedView)
838 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
839 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400840
Michael Jurka5130e402011-10-13 04:55:35 -0700841 // Get the all apps button
842 mAllAppsButton = findViewById(R.id.all_apps_button);
843 if (mAllAppsButton != null) {
844 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
845 @Override
846 public boolean onTouch(View v, MotionEvent event) {
847 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
848 onTouchDownAllAppsButton(v);
849 }
850 return false;
851 }
852 });
853 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700855 dragController.setDragScoller(mWorkspace);
856 dragController.setScrollView(mDragLayer);
857 dragController.setMoveTarget(mWorkspace);
858 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700859 if (mSearchDropTargetBar != null) {
860 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
863
864 /**
865 * Creates a view representing a shortcut.
866 *
867 * @param info The data structure describing the shortcut.
868 *
869 * @return A View inflated from R.layout.application.
870 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800871 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700873 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875
876 /**
877 * Creates a view representing a shortcut inflated from the specified resource.
878 *
879 * @param layoutResId The id of the XML layout used to create the shortcut.
880 * @param parent The group the shortcut belongs to.
881 * @param info The data structure describing the shortcut.
882 *
883 * @return A View inflated from layoutResId.
884 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800885 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800886 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
887 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 return favorite;
890 }
891
892 /**
893 * Add an application shortcut to the workspace.
894 *
895 * @param data The intent describing the application.
896 * @param cellInfo The position on screen where to create the shortcut.
897 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700898 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700899 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700900 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700901
Adam Cohenfbba09b2011-07-18 21:43:05 -0700902 // First we check if we already know the exact location where we want to add this item.
903 if (cellX >= 0 && cellY >= 0) {
904 cellXY[0] = cellX;
905 cellXY[1] = cellY;
906 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700907 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700908 return;
909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800911 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800912
Romain Guy73b979d2009-06-09 12:57:21 -0700913 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800914 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800916 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700917 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700918 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800919 } else {
920 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
922 }
Romain Guycbb89e42009-06-08 15:52:54 -0700923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 /**
925 * Add a shortcut to the workspace.
926 *
927 * @param data The intent describing the shortcut.
928 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700930 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
931 int cellY) {
932 int[] cellXY = mTmpAddItemCellCoordinates;
933 int[] touchXY = mPendingAddInfo.dropPos;
934 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700935
Michael Jurkad3ef3062010-11-23 16:23:58 -0800936 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700937
Adam Cohen558baaf2011-08-15 15:22:57 -0700938 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800939 if (info == null) {
940 return;
941 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700942 final View view = createShortcut(info);
943
Adam Cohenfbba09b2011-07-18 21:43:05 -0700944 // First we check if we already know the exact location where we want to add this item.
945 if (cellX >= 0 && cellY >= 0) {
946 cellXY[0] = cellX;
947 cellXY[1] = cellY;
948 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700949
950 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800951 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700952 true, null,null)) {
953 return;
954 }
955 DragObject dragObject = new DragObject();
956 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800957 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
958 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700959 return;
960 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700961 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800962 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800964 foundCellSpan = (result != null);
965 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700966 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800967 }
968
969 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700970 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700971 return;
972 }
973
Adam Cohen558baaf2011-08-15 15:22:57 -0700974 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975
976 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700977 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
978 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980 }
981
Winson Chunga5c96362012-04-12 14:04:41 -0700982 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800983 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700984 // We want to account for the extra amount of padding that we are adding to the widget
985 // to ensure that it gets the full amount of space that it has requested
986 int requiredWidth = minWidth + padding.left + padding.right;
987 int requiredHeight = minHeight + padding.top + padding.bottom;
988 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
989 }
990
Winson Chunga5c96362012-04-12 14:04:41 -0700991 int[] getSpanForWidget(AppWidgetProviderInfo info) {
992 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -0700993 }
994
Winson Chunga5c96362012-04-12 14:04:41 -0700995 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
996 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -0700997 }
998
Winson Chunga5c96362012-04-12 14:04:41 -0700999 int[] getSpanForWidget(PendingAddWidgetInfo info) {
1000 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001001 }
1002
Winson Chunga5c96362012-04-12 14:04:41 -07001003 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -08001004 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001005 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001006 }
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001009 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001011 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001012 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001014 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1015 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1016 if (appWidgetInfo == null) {
1017 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1018 }
Romain Guycbb89e42009-06-08 15:52:54 -07001019
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001021 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001022
Winson Chunga5c96362012-04-12 14:04:41 -07001023 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1024 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001027 // We have saved the position to which the widget was dragged-- this really only matters
1028 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001029 int[] cellXY = mTmpAddItemCellCoordinates;
1030 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001031 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001033 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1034 cellXY[0] = mPendingAddInfo.cellX;
1035 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001036 spanXY[0] = mPendingAddInfo.spanX;
1037 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001038 foundCellSpan = true;
1039 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001040 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001041 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001042 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1043 spanXY[1], cellXY, finalSpan);
1044 spanXY[0] = finalSpan[0];
1045 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001046 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001048 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001049 }
1050
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001052 if (appWidgetId != -1) {
1053 // Deleting an app widget ID is a void call but writes to disk before returning
1054 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001055 new Thread("deleteAppWidgetId") {
1056 public void run() {
1057 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1058 }
1059 }.start();
1060 }
Winson Chung93eef082012-03-23 15:59:27 -07001061 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001062 return;
1063 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001066 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 launcherInfo.spanX = spanXY[0];
1068 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001069 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1070 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001073 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074
1075 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001076 if (hostView == null) {
1077 // Perform actual inflation because we're live
1078 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1079 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1080 } else {
1081 // The AppWidgetHostView has already been inflated and instantiated
1082 launcherInfo.hostView = hostView;
1083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001086 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001087 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001089
1090 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001092 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Adam Cohended9f8d2010-11-03 13:25:16 -07001095 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1096 @Override
1097 public void onReceive(Context context, Intent intent) {
1098 final String action = intent.getAction();
1099 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1100 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001101 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001102 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001103
Winson Chungc100e8e2011-08-09 16:02:43 -07001104 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001105 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001106 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1107 mAppsCustomizeTabHost.reset();
1108 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001109 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001110 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1111 mUserPresent = true;
1112 updateRunning();
1113 }
1114 }
1115 };
1116
1117 @Override
1118 public void onAttachedToWindow() {
1119 super.onAttachedToWindow();
1120
1121 // Listen for broadcasts related to user-presence
1122 final IntentFilter filter = new IntentFilter();
1123 filter.addAction(Intent.ACTION_SCREEN_OFF);
1124 filter.addAction(Intent.ACTION_USER_PRESENT);
1125 registerReceiver(mReceiver, filter);
1126
Adam Cohend113e0c2010-11-11 10:48:05 -08001127 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 mVisible = true;
1129 }
1130
1131 @Override
1132 public void onDetachedFromWindow() {
1133 super.onDetachedFromWindow();
1134 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001135 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001136
Adam Cohend113e0c2010-11-11 10:48:05 -08001137 if (mAttached) {
1138 unregisterReceiver(mReceiver);
1139 mAttached = false;
1140 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 updateRunning();
1142 }
1143
1144 public void onWindowVisibilityChanged(int visibility) {
1145 mVisible = visibility == View.VISIBLE;
1146 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001147 // The following code used to be in onResume, but it turns out onResume is called when
1148 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1149 // is a more appropriate event to handle
1150 if (mVisible) {
1151 mAppsCustomizeTabHost.onWindowVisible();
1152 if (!mWorkspaceLoading) {
1153 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001154 // We want to let Launcher draw itself at least once before we force it to build
1155 // layers on all the workspace pages, so that transitioning to Launcher from other
1156 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1157 // a true draw so we wait until the second preDraw call to be safe
1158 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001159 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001160 // We delay the layer building a bit in order to give
1161 // other message processing a time to run. In particular
1162 // this avoids a delay in hiding the IME if it was
1163 // currently shown, because doing that may involve
1164 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001165 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001166 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001167 return true;
1168 }
1169 });
1170 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001171 // When Launcher comes back to foreground, a different Activity might be responsible for
1172 // the app market intent, so refresh the icon
1173 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001174 clearTypedText();
1175 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001176 }
1177
1178 private void sendAdvanceMessage(long delay) {
1179 mHandler.removeMessages(ADVANCE_MSG);
1180 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1181 mHandler.sendMessageDelayed(msg, delay);
1182 mAutoAdvanceSentTime = System.currentTimeMillis();
1183 }
1184
1185 private void updateRunning() {
1186 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1187 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1188 mAutoAdvanceRunning = autoAdvanceRunning;
1189 if (autoAdvanceRunning) {
1190 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1191 sendAdvanceMessage(delay);
1192 } else {
1193 if (!mWidgetsToAdvance.isEmpty()) {
1194 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1195 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1196 }
1197 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001198 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001199 }
1200 }
1201 }
1202
1203 private final Handler mHandler = new Handler() {
1204 @Override
1205 public void handleMessage(Message msg) {
1206 if (msg.what == ADVANCE_MSG) {
1207 int i = 0;
1208 for (View key: mWidgetsToAdvance.keySet()) {
1209 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1210 final int delay = mAdvanceStagger * i;
1211 if (v instanceof Advanceable) {
1212 postDelayed(new Runnable() {
1213 public void run() {
1214 ((Advanceable) v).advance();
1215 }
1216 }, delay);
1217 }
1218 i++;
1219 }
1220 sendAdvanceMessage(mAdvanceInterval);
1221 }
1222 }
1223 };
1224
1225 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001226 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001227 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1228 if (v instanceof Advanceable) {
1229 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001230 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 updateRunning();
1232 }
1233 }
1234
1235 void removeWidgetToAutoAdvance(View hostView) {
1236 if (mWidgetsToAdvance.containsKey(hostView)) {
1237 mWidgetsToAdvance.remove(hostView);
1238 updateRunning();
1239 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 }
1241
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 launcherInfo.hostView = null;
1245 }
1246
Winson Chung93eef082012-03-23 15:59:27 -07001247 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1248 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1249 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001250 }
1251
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001252 public LauncherAppWidgetHost getAppWidgetHost() {
1253 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Winson Chunga9abd0e2010-10-27 17:18:37 -07001256 public LauncherModel getModel() {
1257 return mModel;
1258 }
1259
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001260 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001261 getWindow().closeAllPanels();
1262
Winson Chung87acb482011-08-23 17:28:05 -07001263 /**
1264 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001265 try {
1266 dismissDialog(DIALOG_CREATE_SHORTCUT);
1267 // Unlock the workspace if the dialog was showing
1268 } catch (Exception e) {
1269 // An exception is thrown if the dialog is not visible, which is fine
1270 }
1271
1272 try {
1273 dismissDialog(DIALOG_RENAME_FOLDER);
1274 // Unlock the workspace if the dialog was showing
1275 } catch (Exception e) {
1276 // An exception is thrown if the dialog is not visible, which is fine
1277 }
Winson Chung87acb482011-08-23 17:28:05 -07001278 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001279
1280 // Whatever we were doing is hereby canceled.
1281 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001282 }
1283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 @Override
1285 protected void onNewIntent(Intent intent) {
1286 super.onNewIntent(intent);
1287
1288 // Close the menu
1289 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001290 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001291 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001292
Joe Onorato14f122b2009-11-19 14:06:36 -08001293 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1294 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001295
Adam Cohenac56cff2011-09-28 20:45:37 -07001296 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001297 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001298 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001299 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1300 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001301 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001302 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001303
1304 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001305 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001306 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001307
Joe Onorato3a8820b2009-11-10 15:06:42 -08001308 final View v = getWindow().peekDecorView();
1309 if (v != null && v.getWindowToken() != null) {
1310 InputMethodManager imm = (InputMethodManager)getSystemService(
1311 INPUT_METHOD_SERVICE);
1312 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001314
Michael Jurka35aa14d2011-07-07 17:01:08 -07001315 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001316 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001317 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320 }
1321
1322 @Override
1323 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1324 // Do not call super here
1325 mSavedInstanceState = savedInstanceState;
1326 }
1327
1328 @Override
1329 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001330 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001331 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332
Michael Jurka883f55b2010-10-21 15:47:14 -07001333 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001334 // We close any open folder since it will not be re-opened, and we need to make sure
1335 // this state is reflected.
1336 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337
Winson Chung3d503fb2011-07-13 17:25:49 -07001338 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1339 mWaitingForResult) {
1340 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1341 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1342 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1343 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 if (mFolderInfo != null && mWaitingForResult) {
1347 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1348 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1349 }
Michael Jurka946ad472010-07-09 18:05:18 -07001350
Winson Chung785d2eb2011-04-14 16:08:02 -07001351 // Save the current AppsCustomize tab
1352 if (mAppsCustomizeTabHost != null) {
1353 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1354 if (currentTabTag != null) {
1355 outState.putString("apps_customize_currentTab", currentTabTag);
1356 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001357 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1358 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 @Override
1363 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001365
Winson Chunge7a03942011-08-05 15:05:12 -07001366 // Remove all pending runnables
1367 mHandler.removeMessages(ADVANCE_MSG);
1368 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001369 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001370
Winson Chungcd2b0142011-06-08 16:02:26 -07001371 // Stop callbacks from LauncherModel
1372 LauncherApplication app = ((LauncherApplication) getApplication());
1373 mModel.stopLoader();
1374 app.setLauncher(null);
1375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001377 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001379 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001381 mAppWidgetHost = null;
1382
1383 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384
1385 TextKeyListener.getInstance().release();
1386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001389
1390 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001391 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001392
1393 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1394 mWorkspace.removeAllViews();
1395 mWorkspace = null;
1396 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001397
1398 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400
Adam Cohena9cf38f2011-05-02 15:36:58 -07001401 public DragController getDragController() {
1402 return mDragController;
1403 }
1404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 @Override
1406 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001407 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 super.startActivityForResult(intent, requestCode);
1409 }
1410
Winson Chung70d72102011-08-12 11:24:35 -07001411 /**
1412 * Indicates that we want global search for this activity by setting the globalSearch
1413 * argument for {@link #startSearch} to true.
1414 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001416 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001418
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001419 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001420
Karl Rosaen138a0412009-04-23 19:00:21 -07001421 if (initialQuery == null) {
1422 // Use any text typed in the launcher as the initial query
1423 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 if (appSearchData == null) {
1426 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001427 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001429 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Karl Rosaen138a0412009-04-23 19:00:21 -07001431 final SearchManager searchManager =
1432 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001433 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001434 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436
Winson Chung70d72102011-08-12 11:24:35 -07001437 @Override
1438 public boolean onCreateOptionsMenu(Menu menu) {
1439 if (isWorkspaceLocked()) {
1440 return false;
1441 }
1442
1443 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001444
1445 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1446 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1447 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001448 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1449 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1450 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001451 String helpUrl = getString(R.string.help_url);
1452 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001453 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1454 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1455
Winson Chung70d72102011-08-12 11:24:35 -07001456 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1457 .setIcon(android.R.drawable.ic_menu_gallery)
1458 .setAlphabeticShortcut('W');
1459 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1460 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001461 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001462 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001463 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1464 .setIcon(android.R.drawable.ic_menu_preferences)
1465 .setIntent(settings)
1466 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001467 if (!helpUrl.isEmpty()) {
1468 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1469 .setIcon(android.R.drawable.ic_menu_help)
1470 .setIntent(help)
1471 .setAlphabeticShortcut('H');
1472 }
Winson Chung70d72102011-08-12 11:24:35 -07001473 return true;
1474 }
1475
1476 @Override
1477 public boolean onPrepareOptionsMenu(Menu menu) {
1478 super.onPrepareOptionsMenu(menu);
1479
1480 if (mAppsCustomizeTabHost.isTransitioning()) {
1481 return false;
1482 }
1483 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1484 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1485
1486 return true;
1487 }
1488
1489 @Override
1490 public boolean onOptionsItemSelected(MenuItem item) {
1491 switch (item.getItemId()) {
1492 case MENU_WALLPAPER_SETTINGS:
1493 startWallpaper();
1494 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001495 }
1496
1497 return super.onOptionsItemSelected(item);
1498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001500 @Override
1501 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001502 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001503 // Use a custom animation for launching search
1504 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001505 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001506 }
1507
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 public boolean isWorkspaceLocked() {
1509 return mWorkspaceLoading || mWaitingForResult;
1510 }
1511
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 mPendingAddInfo.container = ItemInfo.NO_ID;
1514 mPendingAddInfo.screen = -1;
1515 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1516 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001517 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001518 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001520
Michael Jurkaaf442092010-06-10 17:01:57 -07001521 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001522 // TODO: catch bad widget exception when sent
1523 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001524 // TODO: Is this log message meaningful?
1525 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001526 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001527 }
1528
Adam Cohened66b2b2012-01-23 17:28:51 -08001529 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1530 final AppWidgetProviderInfo appWidget = info.info;
1531 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001532 if (appWidget.configure != null) {
1533 // Launch over to configure widget, if needed
1534 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1535 intent.setComponent(appWidget.configure);
1536 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001537 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001538 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001539 intent.putExtra(InstallWidgetReceiver.
1540 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001541
1542 final String mimeType = info.mimeType;
1543 final ClipData clipData = (ClipData) info.configurationData;
1544 final ClipDescription clipDesc = clipData.getDescription();
1545 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1546 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001547 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001548 final CharSequence stringData = item.getText();
1549 final Uri uriData = item.getUri();
1550 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001551 final String key = InstallWidgetReceiver.
1552 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001553 if (uriData != null) {
1554 intent.putExtra(key, uriData);
1555 } else if (intentData != null) {
1556 intent.putExtra(key, intentData);
1557 } else if (stringData != null) {
1558 intent.putExtra(key, stringData);
1559 }
1560 break;
1561 }
1562 }
1563 }
Winson Chung55cef262010-10-28 14:14:18 -07001564 }
Romain Guy8e633c52010-03-04 12:51:36 -08001565 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001566 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001568 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001569 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001570 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001571 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573 }
Romain Guycbb89e42009-06-08 15:52:54 -07001574
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 /**
1576 * Process a shortcut drop.
1577 *
1578 * @param componentName The name of the component
1579 * @param screen The screen where it should be added
1580 * @param cell The cell it should be added to, optional
1581 * @param position The location on the screen where it was dropped, optional
1582 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1584 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001586 mPendingAddInfo.container = container;
1587 mPendingAddInfo.screen = screen;
1588 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001589
1590 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001591 mPendingAddInfo.cellX = cell[0];
1592 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594
1595 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1596 createShortcutIntent.setComponent(componentName);
1597 processShortcut(createShortcutIntent);
1598 }
1599
Adam Cohenfbba09b2011-07-18 21:43:05 -07001600 /**
1601 * Process a widget drop.
1602 *
1603 * @param info The PendingAppWidgetInfo of the widget being added.
1604 * @param screen The screen where it should be added
1605 * @param cell The cell it should be added to, optional
1606 * @param position The location on the screen where it was dropped, optional
1607 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001608 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001609 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001611 mPendingAddInfo.container = info.container = container;
1612 mPendingAddInfo.screen = info.screen = screen;
1613 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001614 mPendingAddInfo.minSpanX = info.minSpanX;
1615 mPendingAddInfo.minSpanY = info.minSpanY;
1616
Adam Cohenfbba09b2011-07-18 21:43:05 -07001617 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001618 mPendingAddInfo.cellX = cell[0];
1619 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001620 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 if (span != null) {
1622 mPendingAddInfo.spanX = span[0];
1623 mPendingAddInfo.spanY = span[1];
1624 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001625
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 AppWidgetHostView hostView = info.boundWidget;
1627 int appWidgetId;
1628 if (hostView != null) {
1629 appWidgetId = hostView.getAppWidgetId();
1630 } else {
1631 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1632 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1633 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001634 addAppWidgetImpl(appWidgetId, info);
1635 }
1636
Joe Onoratodeb98af2010-02-19 14:59:39 -08001637 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001638 // Handle case where user selected "Applications"
1639 String applicationName = getResources().getString(R.string.group_applications);
1640 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 if (applicationName != null && applicationName.equals(shortcutName)) {
1643 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1644 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1647 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001648 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001649 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001651 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
1654
Winson Chung24ab2f12010-09-16 14:10:47 -07001655 void processWallpaper(Intent intent) {
1656 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1657 }
1658
Winson Chung3d503fb2011-07-13 17:25:49 -07001659 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1660 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001661 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 folderInfo.title = getText(R.string.folder_name);
1663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1666 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001667 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668
1669 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 FolderIcon newFolder =
1671 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1672 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1673 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001674 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
Romain Guycbb89e42009-06-08 15:52:54 -07001676
Joe Onorato9c1289c2009-08-17 11:03:03 -04001677 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001678 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001679 }
1680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001682 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001684 Intent chooser = Intent.createChooser(pickWallpaper,
1685 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001686 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1687 // Removed in Eclair MR1
1688// WallpaperManager wm = (WallpaperManager)
1689// getSystemService(Context.WALLPAPER_SERVICE);
1690// WallpaperInfo wi = wm.getWallpaperInfo();
1691// if (wi != null && wi.getSettingsActivity() != null) {
1692// LabeledIntent li = new LabeledIntent(getPackageName(),
1693// R.string.configure_wallpaper, 0);
1694// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1695// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1696// }
Mike Clerona0618e42009-10-22 13:55:21 -07001697 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 }
1699
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001700 /**
1701 * Registers various content observers. The current implementation registers
1702 * only a favorites observer to keep track of the favorites applications.
1703 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001704 private void registerContentObservers() {
1705 ContentResolver resolver = getContentResolver();
1706 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1707 true, mWidgetObserver);
1708 }
1709
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 @Override
1711 public boolean dispatchKeyEvent(KeyEvent event) {
1712 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1713 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001715 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001716 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001717 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001718 dumpState();
1719 return true;
1720 }
1721 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001722 }
1723 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1724 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001725 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 return true;
1727 }
1728 }
1729
1730 return super.dispatchKeyEvent(event);
1731 }
1732
Joe Onorato88ec0992009-11-19 13:16:06 -08001733 @Override
1734 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001735 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001736 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001737 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001738 Folder openFolder = mWorkspace.getOpenFolder();
1739 if (openFolder.isEditingName()) {
1740 openFolder.dismissEditingName();
1741 } else {
1742 closeFolder();
1743 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001744 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001745 mWorkspace.exitWidgetResizeMode();
1746
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001747 // Back button is a no-op here, but give at least some feedback for the button press
1748 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001749 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001750 }
1751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001753 * Re-listen when widgets are reset.
1754 */
1755 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001756 if (mAppWidgetHost != null) {
1757 mAppWidgetHost.startListening();
1758 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001759 }
1760
1761 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001762 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1763 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001765 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001766 if (mModel != null) {
1767 mModel.unbindWorkspaceItems();
1768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001770
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 /**
1772 * Launches the intent referred by the clicked shortcut.
1773 *
1774 * @param v The view representing the clicked shortcut.
1775 */
1776 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001777 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1778 // view has detached (it's possible for this to happen if the view is removed mid touch).
1779 if (v.getWindowToken() == null) {
1780 return;
1781 }
1782
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001783 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001784 return;
1785 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001788 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001790 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001791 int[] pos = new int[2];
1792 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001793 intent.setSourceBounds(new Rect(pos[0], pos[1],
1794 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001795 boolean success = startActivitySafely(intent, tag);
1796
1797 if (success && v instanceof BubbleTextView) {
1798 mWaitingForResume = (BubbleTextView) v;
1799 mWaitingForResume.setStayPressed(true);
1800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001802 if (v instanceof FolderIcon) {
1803 FolderIcon fi = (FolderIcon) v;
1804 handleFolderClick(fi);
1805 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001806 } else if (v == mAllAppsButton) {
1807 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001808 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001809 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001810 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
1814
Michael Jurka0e260592010-06-30 17:07:39 -07001815 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001816 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001817 // clicking anywhere on the workspace causes the customization drawer to slide down
1818 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001819 return false;
1820 }
1821
Michael Jurkaaf442092010-06-10 17:01:57 -07001822 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001823 * Event handler for the search button
1824 *
1825 * @param v The view that was clicked.
1826 */
1827 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001828 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1829
Amith Yamasania135ba82011-08-09 17:42:01 -07001830 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001831 }
1832
1833 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001834 * Event handler for the voice button
1835 *
1836 * @param v The view that was clicked.
1837 */
1838 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001839 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001840
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001841 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001842 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001843 startActivity(intent);
1844 }
1845
1846 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001847 * Event handler for the "grid" button that appears on the home screen, which
1848 * enters all apps mode.
1849 *
1850 * @param v The view that was clicked.
1851 */
1852 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001853 showAllApps(true);
1854 }
1855
1856 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001857 // Provide the same haptic feedback that the system offers for virtual keys.
1858 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001859 }
1860
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001861 public void onClickAppMarketButton(View v) {
1862 if (mAppMarketIntent != null) {
1863 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001864 } else {
1865 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001866 }
1867 }
1868
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001869 void startApplicationDetailsActivity(ComponentName componentName) {
1870 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001871 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1872 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001873 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001874 startActivity(intent);
1875 }
1876
Patrick Dubroy5539af72010-09-07 15:22:01 -07001877 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1878 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1879 // System applications cannot be installed. For now, show a toast explaining that.
1880 // We may give them the option of disabling apps this way.
1881 int messageId = R.string.uninstall_system_app_text;
1882 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1883 } else {
1884 String packageName = appInfo.componentName.getPackageName();
1885 String className = appInfo.componentName.getClassName();
1886 Intent intent = new Intent(
1887 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001888 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1889 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001890 startActivity(intent);
1891 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001892 }
1893
Michael Jurkaddd62e92011-02-16 17:49:14 -08001894 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1896 try {
1897 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001898 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } catch (ActivityNotFoundException e) {
1900 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001901 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 } catch (SecurityException e) {
1903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001904 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001906 "or use the exported attribute for this activity. "
1907 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001909 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001911
Romain Guy8e633c52010-03-04 12:51:36 -08001912 void startActivityForResultSafely(Intent intent, int requestCode) {
1913 try {
1914 startActivityForResult(intent, requestCode);
1915 } catch (ActivityNotFoundException e) {
1916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1917 } catch (SecurityException e) {
1918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1919 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1920 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1921 "or use the exported attribute for this activity.", e);
1922 }
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924
Adam Cohena9cf38f2011-05-02 15:36:58 -07001925 private void handleFolderClick(FolderIcon folderIcon) {
1926 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001927 Folder openFolder = mWorkspace.getFolderForTag(info);
1928
1929 // If the folder info reports that the associated folder is open, then verify that
1930 // it is actually opened. There have been a few instances where this gets out of sync.
1931 if (info.opened && openFolder == null) {
1932 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1933 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1934 info.opened = false;
1935 }
1936
Adam Cohena9cf38f2011-05-02 15:36:58 -07001937 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 // Close any open folder
1939 closeFolder();
1940 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001941 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 } else {
1943 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 int folderScreen;
1945 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001946 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 // .. and close it
1948 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001949 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // Close any folder open on the current screen
1951 closeFolder();
1952 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001953 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955 }
1956 }
1957 }
1958
Adam Cohen2801caf2011-05-13 20:57:39 -07001959 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001960 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001961 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1962 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1963 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1964
Adam Cohenc51934b2011-07-26 21:07:43 -07001965 FolderInfo info = (FolderInfo) fi.getTag();
1966 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1967 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001968 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1969 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001970 }
1971
Adam Cohen2801caf2011-05-13 20:57:39 -07001972 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1973 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1974 oa.start();
1975 }
1976
1977 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001978 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001979 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1980 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1981 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1982
Adam Cohenc51934b2011-07-26 21:07:43 -07001983 FolderInfo info = (FolderInfo) fi.getTag();
1984 CellLayout cl = null;
1985 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1986 cl = (CellLayout) fi.getParent().getParent();
1987 }
1988
1989 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001990 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1991 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001992 oa.addListener(new AnimatorListenerAdapter() {
1993 @Override
1994 public void onAnimationEnd(Animator animation) {
1995 if (layout != null) {
1996 layout.clearFolderLeaveBehind();
1997 }
1998 }
1999 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002000 oa.start();
2001 }
2002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002004 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 * is animated relative to the specified View. If the View is null, no animation
2006 * is played.
2007 *
2008 * @param folderInfo The FolderInfo describing the folder to open.
2009 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002010 public void openFolder(FolderIcon folderIcon) {
2011 Folder folder = folderIcon.mFolder;
2012 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
Adam Cohen2801caf2011-05-13 20:57:39 -07002014 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002015 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016
Adam Cohen4554ee12011-08-03 16:13:21 -07002017 // Just verify that the folder hasn't already been added to the DragLayer.
2018 // There was a one-off crash where the folder had a parent already.
2019 if (folder.getParent() == null) {
2020 mDragLayer.addView(folder);
2021 mDragController.addDropTarget((DropTarget) folder);
2022 } else {
2023 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2024 folder.getParent() + ").");
2025 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002026 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002027 }
2028
2029 public void closeFolder() {
2030 Folder folder = mWorkspace.getOpenFolder();
2031 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002032 if (folder.isEditingName()) {
2033 folder.dismissEditingName();
2034 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002035 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002036
2037 // Dismiss the folder cling
2038 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002039 }
2040 }
2041
2042 void closeFolder(Folder folder) {
2043 folder.getInfo().opened = false;
2044
2045 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2046 if (parent != null) {
2047 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2048 shrinkAndFadeInFolderIcon(fi);
2049 }
2050 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002054
Patrick Dubroye708c522011-03-01 16:03:43 -08002055 if (mState != State.WORKSPACE) {
2056 return false;
2057 }
2058
Joe Onorato9c1289c2009-08-17 11:03:03 -04002059 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 return false;
2061 }
2062
2063 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002064 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
Michael Jurka0280c3b2010-09-17 15:00:07 -07002067 resetAddInfo();
2068 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002070 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 return true;
2072 }
2073
Winson Chung3d503fb2011-07-13 17:25:49 -07002074 // The hotseat touch handling does not go through Workspace, and we always allow long press
2075 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002076 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002077 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2078 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002079 if (itemUnderLongClick == null) {
2080 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002081 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2082 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002083 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002087 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 }
2089 }
2090 }
2091 return true;
2092 }
2093
Winson Chung3d503fb2011-07-13 17:25:49 -07002094 boolean isHotseatLayout(View layout) {
2095 return mHotseat != null && layout != null &&
2096 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2097 }
2098 Hotseat getHotseat() {
2099 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002100 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002101 SearchDropTargetBar getSearchBar() {
2102 return mSearchDropTargetBar;
2103 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002104
Winson Chung3d503fb2011-07-13 17:25:49 -07002105 /**
2106 * Returns the CellLayout of the specified container at the specified screen.
2107 */
2108 CellLayout getCellLayout(long container, int screen) {
2109 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2110 if (mHotseat != null) {
2111 return mHotseat.getLayout();
2112 } else {
2113 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002114 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002115 } else {
2116 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002117 }
2118 }
2119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 Workspace getWorkspace() {
2121 return mWorkspace;
2122 }
2123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 @Override
2125 protected Dialog onCreateDialog(int id) {
2126 switch (id) {
2127 case DIALOG_CREATE_SHORTCUT:
2128 return new CreateShortcut().createDialog();
2129 case DIALOG_RENAME_FOLDER:
2130 return new RenameFolder().createDialog();
2131 }
2132
2133 return super.onCreateDialog(id);
2134 }
2135
2136 @Override
2137 protected void onPrepareDialog(int id, Dialog dialog) {
2138 switch (id) {
2139 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 break;
2141 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002142 if (mFolderInfo != null) {
2143 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2144 final CharSequence text = mFolderInfo.title;
2145 input.setText(text);
2146 input.setSelection(0, text.length());
2147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 break;
2149 }
2150 }
2151
2152 void showRenameDialog(FolderInfo info) {
2153 mFolderInfo = info;
2154 mWaitingForResult = true;
2155 showDialog(DIALOG_RENAME_FOLDER);
2156 }
2157
Adam Cohenfbba09b2011-07-18 21:43:05 -07002158 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002159 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002160 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2161 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 mWaitingForResult = true;
2163 showDialog(DIALOG_CREATE_SHORTCUT);
2164 }
2165
2166 private class RenameFolder {
2167 private EditText mInput;
2168
2169 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2171 mInput = (EditText) layout.findViewById(R.id.folder_name);
2172
2173 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2174 builder.setIcon(0);
2175 builder.setTitle(getString(R.string.rename_folder_title));
2176 builder.setCancelable(true);
2177 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2178 public void onCancel(DialogInterface dialog) {
2179 cleanup();
2180 }
2181 });
2182 builder.setNegativeButton(getString(R.string.cancel_action),
2183 new Dialog.OnClickListener() {
2184 public void onClick(DialogInterface dialog, int which) {
2185 cleanup();
2186 }
2187 }
2188 );
2189 builder.setPositiveButton(getString(R.string.rename_action),
2190 new Dialog.OnClickListener() {
2191 public void onClick(DialogInterface dialog, int which) {
2192 changeFolderName();
2193 }
2194 }
2195 );
2196 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002197
2198 final AlertDialog dialog = builder.create();
2199 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2200 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002201 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002202 mInput.requestFocus();
2203 InputMethodManager inputManager = (InputMethodManager)
2204 getSystemService(Context.INPUT_METHOD_SERVICE);
2205 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002206 }
2207 });
2208
2209 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211
2212 private void changeFolderName() {
2213 final String name = mInput.getText().toString();
2214 if (!TextUtils.isEmpty(name)) {
2215 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002216 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 mFolderInfo.title = name;
2218 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2219
Joe Onorato9c1289c2009-08-17 11:03:03 -04002220 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002221 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002222 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 } else {
2224 final FolderIcon folderIcon = (FolderIcon)
2225 mWorkspace.getViewForTag(mFolderInfo);
2226 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002227 // TODO: At some point we'll probably want some version of setting
2228 // the text for a folder icon.
2229 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230 getWorkspace().requestLayout();
2231 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002232 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002234 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236 }
2237 }
2238 cleanup();
2239 }
2240
2241 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 dismissDialog(DIALOG_RENAME_FOLDER);
2243 mWaitingForResult = false;
2244 mFolderInfo = null;
2245 }
2246 }
2247
Daniel Sandler843e8602010-06-07 14:59:01 -04002248 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2249 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002250 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002251 }
2252
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002253 public boolean isAllAppsButtonRank(int rank) {
2254 return mHotseat.isAllAppsButtonRank(rank);
2255 }
2256
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002257 // AllAppsView.Watcher
2258 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002259 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002260 mWorkspace.setVisibility(View.GONE);
2261 }
2262 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002263
2264 /**
2265 * Helper method for the cameraZoomIn/cameraZoomOut animations
2266 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002267 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002268 * @param scaleFactor The scale factor used for the zoom
2269 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002270 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002271 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002272 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002274
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002275 void updateWallpaperVisibility(boolean visible) {
2276 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2277 int curflags = getWindow().getAttributes().flags
2278 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2279 if (wpflags != curflags) {
2280 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2281 }
2282 }
2283
Michael Jurkabed61d22012-02-14 22:51:29 -08002284 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2285 if (v instanceof LauncherTransitionable) {
2286 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2287 }
Winson Chung70442722012-02-10 15:43:22 -08002288
2289 // Update the workspace transition step as well
2290 dispatchOnLauncherTransitionStep(v, 0f);
2291 }
2292
2293 private void dispatchOnLauncherTransitionStep(View v, float t) {
2294 if (v instanceof LauncherTransitionable) {
2295 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2296 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002297 }
2298
2299 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2300 if (v instanceof LauncherTransitionable) {
2301 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2302 }
Winson Chung70442722012-02-10 15:43:22 -08002303
2304 // Update the workspace transition step as well
2305 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002306 }
2307
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002308 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002309 * Things to test when changing the following seven functions.
2310 * - Home from workspace
2311 * - from center screen
2312 * - from other screens
2313 * - Home from all apps
2314 * - from center screen
2315 * - from other screens
2316 * - Back from all apps
2317 * - from center screen
2318 * - from other screens
2319 * - Launch app from workspace and quit
2320 * - with back
2321 * - with home
2322 * - Launch app from all apps and quit
2323 * - with back
2324 * - with home
2325 * - Go to a screen that's not the default, then all
2326 * apps, and launch and app, and go back
2327 * - with back
2328 * -with home
2329 * - On workspace, long press power and go back
2330 * - with back
2331 * - with home
2332 * - On all apps, long press power and go back
2333 * - with back
2334 * - with home
2335 * - On workspace, power off
2336 * - On all apps, power off
2337 * - Launch an app and turn off the screen while in that app
2338 * - Go back with home key
2339 * - Go back with back key TODO: make this not go to workspace
2340 * - From all apps
2341 * - From workspace
2342 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2343 * - From all apps
2344 * - From the center workspace
2345 * - From another workspace
2346 */
2347
2348 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002349 * Zoom the camera out from the workspace to reveal 'toView'.
2350 * Assumes that the view to show is anchored at either the very top or very bottom
2351 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002352 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002353 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002354 if (mStateAnimation != null) {
2355 mStateAnimation.cancel();
2356 mStateAnimation = null;
2357 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002358 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002359
Winson Chungf0ea4d32011-06-06 14:27:16 -07002360 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2361 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2362 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002363 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002364 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002365 final int startDelay =
2366 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002367
Michael Jurkab3e22d92011-10-31 15:58:33 -07002368 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002369
Michael Jurkad74c9842011-07-10 12:44:21 -07002370 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002371 Animator workspaceAnim =
2372 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002373
2374 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002375 toView.setScaleX(scale);
2376 toView.setScaleY(scale);
2377 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2378 scaleAnim.
2379 scaleX(1f).scaleY(1f).
2380 setDuration(duration).
2381 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002382
Winson Chungf0ea4d32011-06-06 14:27:16 -07002383 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002384 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002385 final ObjectAnimator alphaAnim = ObjectAnimator
2386 .ofFloat(toView, "alpha", 0f, 1f)
2387 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002389 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2390 @Override
2391 public void onAnimationUpdate(ValueAnimator animation) {
2392 float t = (Float) animation.getAnimatedValue();
2393 dispatchOnLauncherTransitionStep(fromView, t);
2394 dispatchOnLauncherTransitionStep(toView, t);
2395 }
2396 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002397
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398 // toView should appear right at the end of the workspace shrink
2399 // animation
2400 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002401 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002402 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002403
2404 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002405 boolean animationCancelled = false;
2406
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002407 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002408 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002409 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410 // Prepare the position
2411 toView.setTranslationX(0.0f);
2412 toView.setTranslationY(0.0f);
2413 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002414 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002416 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002417 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002418 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2419 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002420
2421 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2422 // Hide the workspace scrollbar
2423 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002424 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002425 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002426 if (!animationCancelled) {
2427 updateWallpaperVisibility(false);
2428 }
2429 }
2430
Adam Cohencff6af82011-09-13 14:51:53 -07002431 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002432 public void onAnimationCancel(Animator animation) {
2433 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002434 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435 });
2436
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002437 if (workspaceAnim != null) {
2438 mStateAnimation.play(workspaceAnim);
2439 }
Michael Jurka1899a362011-11-03 13:50:45 -07002440
2441 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002442 final ViewTreeObserver observer;
2443
Michael Jurkabed61d22012-02-14 22:51:29 -08002444 dispatchOnLauncherTransitionStart(fromView, animated, false);
2445 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002446
2447 // If any of the objects being animated haven't been measured/laid out
2448 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002449 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002450 (mWorkspace.getMeasuredWidth() == 0) ||
2451 (toView.getMeasuredWidth() == 0)) {
2452 observer = mWorkspace.getViewTreeObserver();
2453 delayAnim = true;
2454 } else {
2455 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002456 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002457
2458 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002459 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002460 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2461 public void onGlobalLayout() {
2462 mWorkspace.post(new Runnable() {
2463 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002464 // Check that mStateAnimation hasn't changed while
2465 // we waited for a layout pass
2466 if (mStateAnimation == stateAnimation) {
2467 // Need to update pivots for zoom if layout changed
2468 setPivotsForZoom(toView, scale);
2469 mStateAnimation.start();
2470 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002471 }
2472 });
2473 observer.removeGlobalOnLayoutListener(this);
2474 }
2475 };
2476 observer.addOnGlobalLayoutListener(delayedStart);
2477 } else {
2478 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002479 mStateAnimation.start();
2480 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002481 } else {
2482 toView.setTranslationX(0.0f);
2483 toView.setTranslationY(0.0f);
2484 toView.setScaleX(1.0f);
2485 toView.setScaleY(1.0f);
2486 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002487 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002488
Michael Jurkabed61d22012-02-14 22:51:29 -08002489 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2490 // Hide the workspace scrollbar
2491 mWorkspace.hideScrollingIndicator(true);
2492 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002493 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 dispatchOnLauncherTransitionStart(fromView, animated, false);
2495 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2496 dispatchOnLauncherTransitionStart(toView, animated, false);
2497 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002498 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002500 }
2501
2502 /**
2503 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002504 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002506 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002507 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2508 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002509
Michael Jurkab3e22d92011-10-31 15:58:33 -07002510 if (mStateAnimation != null) {
2511 mStateAnimation.cancel();
2512 mStateAnimation = null;
2513 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002514 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002515
Winson Chungf0ea4d32011-06-06 14:27:16 -07002516 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002517 final int fadeOutDuration =
2518 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002519 final float scaleFactor = (float)
2520 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2521 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002522 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002523 Animator workspaceAnim = null;
2524
2525 if (toState == State.WORKSPACE) {
2526 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2527 workspaceAnim = mWorkspace.getChangeStateAnimation(
2528 Workspace.State.NORMAL, animated, stagger);
2529 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2530 workspaceAnim = mWorkspace.getChangeStateAnimation(
2531 Workspace.State.SPRING_LOADED, animated);
2532 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002533
Michael Jurkab3e22d92011-10-31 15:58:33 -07002534 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002535 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002536 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002538 final LauncherViewPropertyAnimator scaleAnim =
2539 new LauncherViewPropertyAnimator(fromView);
2540 scaleAnim.
2541 scaleX(scaleFactor).scaleY(scaleFactor).
2542 setDuration(duration).
2543 setInterpolator(new Workspace.ZoomInInterpolator());
2544
2545 final ObjectAnimator alphaAnim = ObjectAnimator
2546 .ofFloat(fromView, "alpha", 1f, 0f)
2547 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002548 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002549 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2550 @Override
2551 public void onAnimationUpdate(ValueAnimator animation) {
2552 float t = 1f - (Float) animation.getAnimatedValue();
2553 dispatchOnLauncherTransitionStep(fromView, t);
2554 dispatchOnLauncherTransitionStep(toView, t);
2555 }
2556 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002557
Michael Jurkabed61d22012-02-14 22:51:29 -08002558 mStateAnimation = new AnimatorSet();
2559
2560 dispatchOnLauncherTransitionStart(fromView, animated, true);
2561 dispatchOnLauncherTransitionStart(toView, animated, true);
2562
2563 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002564 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002565 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002566 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002568 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2569 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002570 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002571 if (onCompleteRunnable != null) {
2572 onCompleteRunnable.run();
2573 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002574 }
2575 });
2576
Winson Chungf0ea4d32011-06-06 14:27:16 -07002577 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002578 if (workspaceAnim != null) {
2579 mStateAnimation.play(workspaceAnim);
2580 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002581 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 } else {
2583 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002584 dispatchOnLauncherTransitionStart(fromView, animated, true);
2585 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2586 dispatchOnLauncherTransitionStart(toView, animated, true);
2587 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002588 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002590 }
2591
Michael Jurkae326f182011-11-21 14:05:46 -08002592 @Override
2593 public void onTrimMemory(int level) {
2594 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002595 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002596 mAppsCustomizeTabHost.onTrimMemory();
2597 }
2598 }
2599
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002600 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002601 showWorkspace(animated, null);
2602 }
2603
2604 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002605 if (mState != State.WORKSPACE) {
2606 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002607 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608
2609 // Show the search bar and hotseat
2610 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002611 // We only need to animate in the dock divider if we're going from spring loaded mode
2612 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002613
2614 // Set focus to the AppsCustomize button
2615 if (mAllAppsButton != null) {
2616 mAllAppsButton.requestFocus();
2617 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002618 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002619
Michael Jurkab737ee62011-11-15 15:57:22 -08002620 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002621
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002622 // Change the state *after* we've called all the transition code
2623 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002624
Winson Chung5fb63472011-02-02 17:03:37 -08002625 // Resume the auto-advance of widgets
2626 mUserPresent = true;
2627 updateRunning();
2628
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002629 // send an accessibility event to announce the context change
2630 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002631 }
2632
Michael Jurkab3e22d92011-10-31 15:58:33 -07002633 void showAllApps(boolean animated) {
2634 if (mState != State.WORKSPACE) return;
2635
2636 showAppsCustomizeHelper(animated, false);
2637 mAppsCustomizeTabHost.requestFocus();
2638
2639 // Hide the search bar and hotseat
2640 mSearchDropTargetBar.hideSearchBar(animated);
2641
2642 // Change the state *after* we've called all the transition code
2643 mState = State.APPS_CUSTOMIZE;
2644
2645 // Pause the auto-advance of widgets until we are out of AllApps
2646 mUserPresent = false;
2647 updateRunning();
2648 closeFolder();
2649
2650 // Send an accessibility event to announce the context change
2651 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2652 }
2653
Adam Cohen7777d962011-08-18 18:58:38 -07002654 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002655 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002656 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002657 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002658 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002659 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002660 }
Adam Cohen7777d962011-08-18 18:58:38 -07002661
Adam Cohened66b2b2012-01-23 17:28:51 -08002662 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2663 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002664 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2665
Winson Chunge7a03942011-08-05 15:05:12 -07002666 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002667 @Override
2668 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002669 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002670 // Before we show workspace, hide all apps again because
2671 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2672 // clean up our state transition functions
2673 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002674 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002675 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002676 } else {
2677 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002678 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002679 }
2680 }, (extendedDelay ?
2681 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2682 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2683 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002684
Michael Jurkad3ef3062010-11-23 16:23:58 -08002685 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002686 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002687 final boolean animated = true;
2688 final boolean springLoaded = true;
2689 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002690 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002691 }
2692 // Otherwise, we are not in spring loaded mode, so don't do anything.
2693 }
2694
Michael Jurkab737ee62011-11-15 15:57:22 -08002695 void hideDockDivider() {
2696 if (mQsbDivider != null && mDockDivider != null) {
2697 mQsbDivider.setVisibility(View.INVISIBLE);
2698 mDockDivider.setVisibility(View.INVISIBLE);
2699 }
2700 }
2701
2702 void showDockDivider(boolean animated) {
2703 if (mQsbDivider != null && mDockDivider != null) {
2704 mQsbDivider.setVisibility(View.VISIBLE);
2705 mDockDivider.setVisibility(View.VISIBLE);
2706 if (mDividerAnimator != null) {
2707 mDividerAnimator.cancel();
2708 mQsbDivider.setAlpha(1f);
2709 mDockDivider.setAlpha(1f);
2710 mDividerAnimator = null;
2711 }
2712 if (animated) {
2713 mDividerAnimator = new AnimatorSet();
2714 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2715 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2716 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2717 mDividerAnimator.start();
2718 }
2719 }
2720 }
2721
Michael Jurkab3e22d92011-10-31 15:58:33 -07002722 void lockAllApps() {
2723 // TODO
2724 }
2725
2726 void unlockAllApps() {
2727 // TODO
2728 }
2729
Adam Cohenfc53cd22011-07-20 15:45:11 -07002730 public boolean isAllAppsCustomizeOpen() {
2731 return mState == State.APPS_CUSTOMIZE;
2732 }
2733
Winson Chungf0ea4d32011-06-06 14:27:16 -07002734 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002735 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002736 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002737 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002738 if (!LauncherApplication.isScreenLarge()) {
2739 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002740 if (mHotseat.getAlpha() != 1f) {
2741 int duration = mSearchDropTargetBar.getTransitionInDuration();
2742 mHotseat.animate().alpha(1f).setDuration(duration);
2743 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002744 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002745 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002746 }
2747 }
2748 }
2749
2750 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002751 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002752 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002753 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002754 if (!LauncherApplication.isScreenLarge()) {
2755 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002756 if (mHotseat.getAlpha() != 0f) {
2757 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2758 mHotseat.animate().alpha(0f).setDuration(duration);
2759 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002760 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002761 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002762 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002763 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002764 }
2765
Patrick Dubroy5f445422011-02-18 14:35:21 -08002766 /**
2767 * Add an item from all apps or customize onto the given workspace screen.
2768 * If layout is null, add to the current screen.
2769 */
2770 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002771 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002772 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002773 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002774 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002775
Winson Chungdff8ebb2011-09-08 17:25:31 -07002776 /** Maps the current orientation to an index for referencing orientation correct global icons */
2777 private int getCurrentOrientationIndexForGlobalIcons() {
2778 // default - 0, landscape - 1
2779 switch (getResources().getConfiguration().orientation) {
2780 case Configuration.ORIENTATION_LANDSCAPE:
2781 return 1;
2782 default:
2783 return 0;
2784 }
2785 }
2786
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002787 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002788 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002789 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002790 // Look for the toolbar icon specified in the activity meta-data
2791 Bundle metaData = packageManager.getActivityInfo(
2792 activityName, PackageManager.GET_META_DATA).metaData;
2793 if (metaData != null) {
2794 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2795 if (iconResId != 0) {
2796 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002797 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002798 }
2799 }
2800 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002801 // This can happen if the activity defines an invalid drawable
2802 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2803 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002804 } catch (Resources.NotFoundException nfe) {
2805 // This can happen if the activity defines an invalid drawable
2806 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2807 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002808 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002809 return null;
2810 }
2811
2812 // if successful in getting icon, return it; otherwise, set button to use default drawable
2813 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2814 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002815 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002816 Resources r = getResources();
2817 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2818 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002819
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002820 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002821 // If we were unable to find the icon via the meta-data, use a generic one
2822 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002823 toolbarIcon = r.getDrawable(fallbackDrawableId);
2824 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002825 if (button != null) {
2826 button.setCompoundDrawables(toolbarIcon, null, null, null);
2827 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002828 return null;
2829 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002830 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002831 if (button != null) {
2832 button.setCompoundDrawables(toolbarIcon, null, null, null);
2833 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002834 return toolbarIcon.getConstantState();
2835 }
2836 }
2837
2838 // if successful in getting icon, return it; otherwise, set button to use default drawable
2839 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2840 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2841 ImageView button = (ImageView) findViewById(buttonId);
2842 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2843
Michael Jurka19e0fc52011-07-22 18:00:21 -07002844 if (button != null) {
2845 // If we were unable to find the icon via the meta-data, use a
2846 // generic one
2847 if (toolbarIcon == null) {
2848 button.setImageResource(fallbackDrawableId);
2849 } else {
2850 button.setImageDrawable(toolbarIcon);
2851 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002852 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002853
2854 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2855
Michael Jurka0423dcf2010-10-05 14:56:18 -07002856 }
2857
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002858 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2859 TextView button = (TextView) findViewById(buttonId);
2860 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2861 }
2862
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002863 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002864 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002865 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002866 }
2867
Winson Chungbb185bd2011-11-21 12:31:42 -08002868 private void invalidatePressedFocusedStates(View container, View button) {
2869 if (container instanceof HolographicLinearLayout) {
2870 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2871 layout.invalidatePressedFocusedStates();
2872 } else if (button instanceof HolographicImageView) {
2873 HolographicImageView view = (HolographicImageView) button;
2874 view.invalidatePressedFocusedStates();
2875 }
2876 }
2877
Winson Chungc51db6a2011-10-05 11:44:49 -07002878 private boolean updateGlobalSearchIcon() {
2879 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002880 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2881 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002882 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002883 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002884
Winson Chung1cad91e2011-05-25 17:41:01 -07002885 final SearchManager searchManager =
2886 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2887 ComponentName activityName = searchManager.getGlobalSearchActivity();
2888 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002889 int coi = getCurrentOrientationIndexForGlobalIcons();
2890 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002891 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002892 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002893 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2894 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002895 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002896 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002897 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002898 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002899 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2901 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2902 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002903 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002904 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002905 }
2906 }
2907
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002908 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002909 final View searchButtonContainer = findViewById(R.id.search_button_container);
2910 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002911 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002912 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002913 }
2914
Winson Chungc51db6a2011-10-05 11:44:49 -07002915 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002916 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002917 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002918 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002919
Winson Chungc51db6a2011-10-05 11:44:49 -07002920 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002921 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2922 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002923 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002924 int coi = getCurrentOrientationIndexForGlobalIcons();
2925 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002926 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002927 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002928 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002929 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002930 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002931 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002932 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002933 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002934 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002935 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002936 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002937 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002938 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002939
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002940 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002941 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2942 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002943 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002944 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002945 }
2946
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002947 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002948 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002949 */
2950 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002951 final View marketButton = findViewById(R.id.market_button);
2952 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2953 // Find the app market activity by resolving an intent.
2954 // (If multiple app markets are installed, it will return the ResolverActivity.)
2955 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002956 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002957 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002958 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002959 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002960 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002961 marketButton.setVisibility(View.VISIBLE);
2962 } else {
2963 // We should hide and disable the view so that we don't try and restore the visibility
2964 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2965 marketButton.setVisibility(View.GONE);
2966 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002967 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002968 }
2969
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002970 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002971 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002972 }
2973
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002974 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 * Displays the shortcut creation dialog and launches, if necessary, the
2976 * appropriate activity.
2977 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002978 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002979 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2980 DialogInterface.OnShowListener {
2981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002986
Winson Chung55b65502011-05-26 12:03:43 -07002987 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2988 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002989 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002991 AlertDialog dialog = builder.create();
2992 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002993 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002994 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002996 return dialog;
2997 }
2998
2999 public void onCancel(DialogInterface dialog) {
3000 mWaitingForResult = false;
3001 cleanup();
3002 }
3003
Romain Guycbb89e42009-06-08 15:52:54 -07003004 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003005 mWaitingForResult = false;
3006 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003007 }
3008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003010 try {
3011 dismissDialog(DIALOG_CREATE_SHORTCUT);
3012 } catch (Exception e) {
3013 // An exception is thrown if the dialog is not visible, which is fine
3014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
3016
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003017 /**
3018 * Handle the action clicked in the "Add to home" dialog.
3019 */
3020 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003021 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003022
Winson Chung55b65502011-05-26 12:03:43 -07003023 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3024 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003025 case AddAdapter.ITEM_APPLICATION: {
3026 if (mAppsCustomizeTabHost != null) {
3027 mAppsCustomizeTabHost.selectAppsTab();
3028 }
3029 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003030 break;
3031 }
Winson Chung55b65502011-05-26 12:03:43 -07003032 case AddAdapter.ITEM_APPWIDGET: {
3033 if (mAppsCustomizeTabHost != null) {
3034 mAppsCustomizeTabHost.selectWidgetsTab();
3035 }
3036 showAllApps(true);
3037 break;
3038 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003039 case AddAdapter.ITEM_WALLPAPER: {
3040 startWallpaper();
3041 break;
3042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 }
3044 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003045
3046 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003047 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
3050
3051 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003052 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003053 */
3054 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3055 @Override
3056 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003057 closeSystemDialogs();
3058 }
3059 }
3060
3061 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003062 * Receives notifications whenever the appwidgets are reset.
3063 */
3064 private class AppWidgetResetObserver extends ContentObserver {
3065 public AppWidgetResetObserver() {
3066 super(new Handler());
3067 }
3068
3069 @Override
3070 public void onChange(boolean selfChange) {
3071 onAppWidgetReset();
3072 }
3073 }
3074
3075 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003076 * If the activity is currently paused, signal that we need to re-run the loader
3077 * in onResume.
3078 *
3079 * This needs to be called from incoming places where resources might have been loaded
3080 * while we are paused. That is becaues the Configuration might be wrong
3081 * when we're not running, and if it comes back to what it was when we
3082 * were paused, we are not restarted.
3083 *
3084 * Implementation of the method from LauncherModel.Callbacks.
3085 *
3086 * @return true if we are currently paused. The caller might be able to
3087 * skip some work in that case since we will come back again.
3088 */
3089 public boolean setLoadOnResume() {
3090 if (mPaused) {
3091 Log.i(TAG, "setLoadOnResume");
3092 mOnResumeNeedsLoad = true;
3093 return true;
3094 } else {
3095 return false;
3096 }
3097 }
3098
3099 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003103 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003104 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003105 } else {
3106 return SCREEN_COUNT / 2;
3107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003109
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 /**
3111 * Refreshes the shortcuts shown on the workspace.
3112 *
3113 * Implementation of the method from LauncherModel.Callbacks.
3114 */
3115 public void startBinding() {
3116 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003117
Winson Chungf0c6ae02012-03-21 16:10:31 -07003118 mNewShortcutAnimatePage = -1;
3119 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003120 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 int count = workspace.getChildCount();
3122 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003123 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003124 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3125 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003127 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003128 if (mHotseat != null) {
3129 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 }
3131 }
3132
3133 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 * Bind the items start-end from the list.
3135 *
3136 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003139 setLoadOnResume();
3140
Winson Chungf0c6ae02012-03-21 16:10:31 -07003141 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3142 Set<String> newApps = new HashSet<String>();
3143 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3144
3145 Workspace workspace = mWorkspace;
3146 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003148
3149 // Short circuit if we are loading dock items for a configuration which has no dock
3150 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3151 mHotseat == null) {
3152 continue;
3153 }
3154
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 switch (item.itemType) {
3156 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3157 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003158 ShortcutInfo info = (ShortcutInfo) item;
3159 String uri = info.intent.toUri(0).toString();
3160 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003161 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3162 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003163 if (newApps.contains(uri)) {
3164 newApps.remove(uri);
3165
3166 // Prepare the view to be animated up
3167 shortcut.setAlpha(0f);
3168 shortcut.setScaleX(0f);
3169 shortcut.setScaleY(0f);
3170 mNewShortcutAnimatePage = item.screen;
3171 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3172 mNewShortcutAnimateViews.add(shortcut);
3173 }
3174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003176 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003177 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003178 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003179 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3181 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003185
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 }
3188
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189 /**
3190 * Implementation of the method from LauncherModel.Callbacks.
3191 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003192 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003193 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003194 sFolders.clear();
3195 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197
3198 /**
3199 * Add the views for a widget to the workspace.
3200 *
3201 * Implementation of the method from LauncherModel.Callbacks.
3202 */
3203 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003204 setLoadOnResume();
3205
Daniel Sandler843e8602010-06-07 14:59:01 -04003206 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3207 if (DEBUG_WIDGETS) {
3208 Log.d(TAG, "bindAppWidget: " + item);
3209 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 final Workspace workspace = mWorkspace;
3211
3212 final int appWidgetId = item.appWidgetId;
3213 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003214 if (DEBUG_WIDGETS) {
3215 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3216 }
3217
Joe Onorato9c1289c2009-08-17 11:03:03 -04003218 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3219
Joe Onorato9c1289c2009-08-17 11:03:03 -04003220 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3221 item.hostView.setTag(item);
3222
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 item.cellY, item.spanX, item.spanY, false);
3225
Adam Cohended9f8d2010-11-03 13:25:16 -07003226 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3227
Joe Onorato9c1289c2009-08-17 11:03:03 -04003228 workspace.requestLayout();
3229
Daniel Sandler843e8602010-06-07 14:59:01 -04003230 if (DEBUG_WIDGETS) {
3231 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3232 + (SystemClock.uptimeMillis()-start) + "ms");
3233 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003234 }
3235
3236 /**
3237 * Callback saying that there aren't any more items to bind.
3238 *
3239 * Implementation of the method from LauncherModel.Callbacks.
3240 */
3241 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003242 setLoadOnResume();
3243
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 if (mSavedState != null) {
3245 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003246 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 mSavedState = null;
3249 }
3250
3251 if (mSavedInstanceState != null) {
3252 super.onRestoreInstanceState(mSavedInstanceState);
3253 mSavedInstanceState = null;
3254 }
3255
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003256 // If we received the result of any pending adds while the loader was running (e.g. the
3257 // widget configuration forced an orientation change), process them now.
3258 for (int i = 0; i < sPendingAddList.size(); i++) {
3259 completeAdd(sPendingAddList.get(i));
3260 }
3261 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262
Winson Chungc51db6a2011-10-05 11:44:49 -07003263 // Update the market app icon as necessary (the other icons will be managed in response to
3264 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003265 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003266
Winson Chungf0c6ae02012-03-21 16:10:31 -07003267 // Animate up any icons as necessary
3268 if (mVisible || mWorkspaceLoading) {
3269 Runnable newAppsRunnable = new Runnable() {
3270 @Override
3271 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003272 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003273 }
3274 };
Winson Chunga2413752012-04-03 14:22:34 -07003275
3276 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3277 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3278 if (canRunNewAppsAnimation()) {
3279 // If the user has not interacted recently, then either snap to the new page to show
3280 // the new-apps animation or just run them if they are to appear on the current page
3281 if (willSnapPage) {
3282 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3283 } else {
3284 runNewAppsAnimation(false);
3285 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003286 } else {
Winson Chunga2413752012-04-03 14:22:34 -07003287 // If the user has interacted recently, then run the animations immediately if they
3288 // are on another page (or just normally if they are added to the current page)
3289 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003290 }
3291 }
3292
3293 mWorkspaceLoading = false;
3294 }
3295
Winson Chunga2413752012-04-03 14:22:34 -07003296 private boolean canRunNewAppsAnimation() {
3297 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3298 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3299 }
3300
Winson Chungf0c6ae02012-03-21 16:10:31 -07003301 /**
3302 * Runs a new animation that scales up icons that were added while Launcher was in the
3303 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003304 *
3305 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003306 */
Winson Chunga2413752012-04-03 14:22:34 -07003307 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003308 AnimatorSet anim = new AnimatorSet();
3309 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003310
3311 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003312 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3313 @Override
3314 public int compare(View a, View b) {
3315 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3316 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3317 int cellCountX = LauncherModel.getCellCountX();
3318 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3319 }
3320 });
Winson Chunga2413752012-04-03 14:22:34 -07003321
3322 // Animate each of the views in place (or show them immediately if requested)
3323 if (immediate) {
3324 for (View v : mNewShortcutAnimateViews) {
3325 v.setAlpha(1f);
3326 v.setScaleX(1f);
3327 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003328 }
Winson Chunga2413752012-04-03 14:22:34 -07003329 } else {
3330 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3331 View v = mNewShortcutAnimateViews.get(i);
3332 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3333 PropertyValuesHolder.ofFloat("alpha", 1f),
3334 PropertyValuesHolder.ofFloat("scaleX", 1f),
3335 PropertyValuesHolder.ofFloat("scaleY", 1f));
3336 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3337 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3338 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3339 bounceAnims.add(bounceAnim);
3340 }
3341 anim.playTogether(bounceAnims);
3342 anim.addListener(new AnimatorListenerAdapter() {
3343 @Override
3344 public void onAnimationEnd(Animator animation) {
3345 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3346 }
3347 });
3348 anim.start();
3349 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003350
3351 // Clean up
3352 mNewShortcutAnimatePage = -1;
3353 mNewShortcutAnimateViews.clear();
3354 new Thread("clearNewAppsThread") {
3355 public void run() {
3356 mSharedPrefs.edit()
3357 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3358 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3359 .commit();
3360 }
3361 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003362 }
3363
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003364 @Override
3365 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003366 boolean searchVisible = updateGlobalSearchIcon();
3367 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3368 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003369 }
3370
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003371 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 * Add the icons for all apps.
3373 *
3374 * Implementation of the method from LauncherModel.Callbacks.
3375 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003376 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3377 // Remove the progress bar entirely; we could also make it GONE
3378 // but better to remove it since we know it's not going to be used
3379 View progressBar = mAppsCustomizeTabHost.
3380 findViewById(R.id.apps_customize_progress_bar);
3381 if (progressBar != null) {
3382 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003384 // We just post the call to setApps so the user sees the progress bar
3385 // disappear-- otherwise, it just looks like the progress bar froze
3386 // which doesn't look great
3387 mAppsCustomizeTabHost.post(new Runnable() {
3388 public void run() {
3389 if (mAppsCustomizeContent != null) {
3390 mAppsCustomizeContent.setApps(apps);
3391 }
3392 }
3393 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 }
3395
3396 /**
3397 * A package was installed.
3398 *
3399 * Implementation of the method from LauncherModel.Callbacks.
3400 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003401 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003404
Winson Chung785d2eb2011-04-14 16:08:02 -07003405 if (mAppsCustomizeContent != null) {
3406 mAppsCustomizeContent.addApps(apps);
3407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 }
3409
3410 /**
3411 * A package was updated.
3412 *
3413 * Implementation of the method from LauncherModel.Callbacks.
3414 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003415 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003416 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003418 if (mWorkspace != null) {
3419 mWorkspace.updateShortcuts(apps);
3420 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421
Winson Chung785d2eb2011-04-14 16:08:02 -07003422 if (mAppsCustomizeContent != null) {
3423 mAppsCustomizeContent.updateApps(apps);
3424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 }
3426
3427 /**
3428 * A package was uninstalled.
3429 *
3430 * Implementation of the method from LauncherModel.Callbacks.
3431 */
Joe Onorato36115782010-06-17 13:28:48 -04003432 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003434 if (permanent) {
3435 mWorkspace.removeItems(apps);
3436 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437
Winson Chung785d2eb2011-04-14 16:08:02 -07003438 if (mAppsCustomizeContent != null) {
3439 mAppsCustomizeContent.removeApps(apps);
3440 }
Winson Chunga1820962011-10-03 16:31:06 -07003441
3442 // Notify the drag controller
3443 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 }
Joe Onoratobe386092009-11-17 17:32:16 -08003445
3446 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003447 * A number of packages were updated.
3448 */
3449 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003450 if (mAppsCustomizeContent != null) {
3451 mAppsCustomizeContent.onPackagesUpdated();
3452 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003453 }
3454
Winson Chung400438b2011-01-16 17:53:48 -08003455 private int mapConfigurationOriActivityInfoOri(int configOri) {
3456 final Display d = getWindowManager().getDefaultDisplay();
3457 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3458 switch (d.getRotation()) {
3459 case Surface.ROTATION_0:
3460 case Surface.ROTATION_180:
3461 // We are currently in the same basic orientation as the natural orientation
3462 naturalOri = configOri;
3463 break;
3464 case Surface.ROTATION_90:
3465 case Surface.ROTATION_270:
3466 // We are currently in the other basic orientation to the natural orientation
3467 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3468 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3469 break;
3470 }
3471
3472 int[] oriMap = {
3473 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3474 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3475 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3476 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3477 };
3478 // Since the map starts at portrait, we need to offset if this device's natural orientation
3479 // is landscape.
3480 int indexOffset = 0;
3481 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3482 indexOffset = 1;
3483 }
3484 return oriMap[(d.getRotation() + indexOffset) % 4];
3485 }
Adam Cohen446e9402011-09-15 18:21:21 -07003486
3487 public void lockScreenOrientationOnLargeUI() {
3488 if (LauncherApplication.isScreenLarge()) {
3489 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3490 .getConfiguration().orientation));
3491 }
Winson Chung400438b2011-01-16 17:53:48 -08003492 }
Adam Cohen446e9402011-09-15 18:21:21 -07003493 public void unlockScreenOrientationOnLargeUI() {
3494 if (LauncherApplication.isScreenLarge()) {
3495 mHandler.postDelayed(new Runnable() {
3496 public void run() {
3497 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3498 }
3499 }, mRestoreScreenOrientationDelay);
3500 }
Winson Chung400438b2011-01-16 17:53:48 -08003501 }
3502
Winson Chung82f55532011-08-09 14:14:23 -07003503 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003504 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003505 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003506 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003507
Winson Chung7d7541e2011-09-16 20:14:36 -07003508 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003509 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003510 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3511 Cling cling = (Cling) findViewById(clingId);
3512 if (cling != null) {
3513 cling.init(this, positionData);
3514 cling.setVisibility(View.VISIBLE);
3515 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3516 if (animate) {
3517 cling.buildLayer();
3518 cling.setAlpha(0f);
3519 cling.animate()
3520 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003521 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003522 .setDuration(SHOW_CLING_DURATION)
3523 .setStartDelay(delay)
3524 .start();
3525 } else {
3526 cling.setAlpha(1f);
3527 }
3528 }
3529 return cling;
3530 }
3531 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003532 if (cling != null) {
3533 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003534 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003535 anim.addListener(new AnimatorListenerAdapter() {
3536 public void onAnimationEnd(Animator animation) {
3537 cling.setVisibility(View.GONE);
3538 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003539 // We should update the shared preferences on a background thread
3540 new Thread("dismissClingThread") {
3541 public void run() {
3542 SharedPreferences.Editor editor = mSharedPrefs.edit();
3543 editor.putBoolean(flag, true);
3544 editor.commit();
3545 }
3546 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003547 };
3548 });
3549 anim.start();
3550 }
3551 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003552 private void removeCling(int id) {
3553 final View cling = findViewById(id);
3554 if (cling != null) {
3555 final ViewGroup parent = (ViewGroup) cling.getParent();
3556 parent.post(new Runnable() {
3557 @Override
3558 public void run() {
3559 parent.removeView(cling);
3560 }
3561 });
3562 }
3563 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003564 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003565 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003566 if (isClingsEnabled() &&
3567 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003568 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003569 } else {
3570 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003571 }
3572 }
3573 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003574 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003575 if (isClingsEnabled() &&
3576 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003577 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003578 } else {
3579 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003580 }
3581 }
3582 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003583 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003584 if (isClingsEnabled() &&
3585 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3586 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003587 } else {
3588 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003589 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003590 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003591 }
3592 public boolean isFolderClingVisible() {
3593 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003594 if (cling != null) {
3595 return cling.getVisibility() == View.VISIBLE;
3596 }
3597 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003598 }
Winson Chung82f55532011-08-09 14:14:23 -07003599 public void dismissWorkspaceCling(View v) {
3600 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003601 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003602 }
3603 public void dismissAllAppsCling(View v) {
3604 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003605 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3606 }
3607 public void dismissFolderCling(View v) {
3608 Cling cling = (Cling) findViewById(R.id.folder_cling);
3609 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003610 }
3611
Winson Chung80baf5a2010-08-09 16:03:15 -07003612 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003613 * Prints out out state for debugging.
3614 */
3615 public void dumpState() {
3616 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003617 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003618 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3619 Log.d(TAG, "mRestoring=" + mRestoring);
3620 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3621 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003622 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003623 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003624
Winson Chung785d2eb2011-04-14 16:08:02 -07003625 if (mAppsCustomizeContent != null) {
3626 mAppsCustomizeContent.dumpState();
3627 }
Joe Onoratobe386092009-11-17 17:32:16 -08003628 Log.d(TAG, "END launcher2 dump state");
3629 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003630
3631 @Override
3632 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3633 super.dump(prefix, fd, writer, args);
3634 writer.println(" ");
3635 writer.println("Debug logs: ");
3636 for (int i = 0; i < sDumpLogs.size(); i++) {
3637 writer.println(" " + sDumpLogs.get(i));
3638 }
3639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003640}
Michael Jurka2763be32011-02-24 11:19:57 -08003641
Michael Jurkaabded662011-03-04 12:06:57 -08003642interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003643 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003644 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003645 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003646 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003647}