blob: a3b98f8a891729517fa197096787a24253acd211 [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;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001531 if (appWidget.configure != null) {
1532 // Launch over to configure widget, if needed
1533 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1534 intent.setComponent(appWidget.configure);
1535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001536 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001537 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001538 intent.putExtra(InstallWidgetReceiver.
1539 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001540
1541 final String mimeType = info.mimeType;
1542 final ClipData clipData = (ClipData) info.configurationData;
1543 final ClipDescription clipDesc = clipData.getDescription();
1544 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1545 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001546 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001547 final CharSequence stringData = item.getText();
1548 final Uri uriData = item.getUri();
1549 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 final String key = InstallWidgetReceiver.
1551 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001552 if (uriData != null) {
1553 intent.putExtra(key, uriData);
1554 } else if (intentData != null) {
1555 intent.putExtra(key, intentData);
1556 } else if (stringData != null) {
1557 intent.putExtra(key, stringData);
1558 }
1559 break;
1560 }
1561 }
1562 }
Winson Chung55cef262010-10-28 14:14:18 -07001563 }
Romain Guy8e633c52010-03-04 12:51:36 -08001564 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001567 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001568 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001569 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001570 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 /**
1575 * Process a shortcut drop.
1576 *
1577 * @param componentName The name of the component
1578 * @param screen The screen where it should be added
1579 * @param cell The cell it should be added to, optional
1580 * @param position The location on the screen where it was dropped, optional
1581 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001582 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1583 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001585 mPendingAddInfo.container = container;
1586 mPendingAddInfo.screen = screen;
1587 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001588
1589 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001590 mPendingAddInfo.cellX = cell[0];
1591 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001592 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001593
1594 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1595 createShortcutIntent.setComponent(componentName);
1596 processShortcut(createShortcutIntent);
1597 }
1598
Adam Cohenfbba09b2011-07-18 21:43:05 -07001599 /**
1600 * Process a widget drop.
1601 *
1602 * @param info The PendingAppWidgetInfo of the widget being added.
1603 * @param screen The screen where it should be added
1604 * @param cell The cell it should be added to, optional
1605 * @param position The location on the screen where it was dropped, optional
1606 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001608 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001609 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001610 mPendingAddInfo.container = info.container = container;
1611 mPendingAddInfo.screen = info.screen = screen;
1612 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001613 mPendingAddInfo.minSpanX = info.minSpanX;
1614 mPendingAddInfo.minSpanY = info.minSpanY;
1615
Adam Cohenfbba09b2011-07-18 21:43:05 -07001616 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001617 mPendingAddInfo.cellX = cell[0];
1618 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001619 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001620 if (span != null) {
1621 mPendingAddInfo.spanX = span[0];
1622 mPendingAddInfo.spanY = span[1];
1623 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001624
Adam Cohened66b2b2012-01-23 17:28:51 -08001625 AppWidgetHostView hostView = info.boundWidget;
1626 int appWidgetId;
1627 if (hostView != null) {
1628 appWidgetId = hostView.getAppWidgetId();
1629 } else {
1630 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1631 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1632 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001633 addAppWidgetImpl(appWidgetId, info);
1634 }
1635
Joe Onoratodeb98af2010-02-19 14:59:39 -08001636 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001637 // Handle case where user selected "Applications"
1638 String applicationName = getResources().getString(R.string.group_applications);
1639 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001641 if (applicationName != null && applicationName.equals(shortcutName)) {
1642 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1643 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001645 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1646 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001647 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001648 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001649 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001650 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 }
1653
Winson Chung24ab2f12010-09-16 14:10:47 -07001654 void processWallpaper(Intent intent) {
1655 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1656 }
1657
Winson Chung3d503fb2011-07-13 17:25:49 -07001658 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1659 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001660 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 folderInfo.title = getText(R.string.folder_name);
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001664 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1665 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001666 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667
1668 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001669 FolderIcon newFolder =
1670 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1671 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1672 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001673 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Joe Onorato9c1289c2009-08-17 11:03:03 -04001676 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001677 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678 }
1679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001681 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001683 Intent chooser = Intent.createChooser(pickWallpaper,
1684 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001685 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1686 // Removed in Eclair MR1
1687// WallpaperManager wm = (WallpaperManager)
1688// getSystemService(Context.WALLPAPER_SERVICE);
1689// WallpaperInfo wi = wm.getWallpaperInfo();
1690// if (wi != null && wi.getSettingsActivity() != null) {
1691// LabeledIntent li = new LabeledIntent(getPackageName(),
1692// R.string.configure_wallpaper, 0);
1693// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1694// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1695// }
Mike Clerona0618e42009-10-22 13:55:21 -07001696 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 }
1698
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001699 /**
1700 * Registers various content observers. The current implementation registers
1701 * only a favorites observer to keep track of the favorites applications.
1702 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001703 private void registerContentObservers() {
1704 ContentResolver resolver = getContentResolver();
1705 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1706 true, mWidgetObserver);
1707 }
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 @Override
1710 public boolean dispatchKeyEvent(KeyEvent event) {
1711 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1712 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001714 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001715 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001716 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001717 dumpState();
1718 return true;
1719 }
1720 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001721 }
1722 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1723 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001724 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 return true;
1726 }
1727 }
1728
1729 return super.dispatchKeyEvent(event);
1730 }
1731
Joe Onorato88ec0992009-11-19 13:16:06 -08001732 @Override
1733 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001734 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001735 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001736 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001737 Folder openFolder = mWorkspace.getOpenFolder();
1738 if (openFolder.isEditingName()) {
1739 openFolder.dismissEditingName();
1740 } else {
1741 closeFolder();
1742 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001743 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001744 mWorkspace.exitWidgetResizeMode();
1745
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001746 // Back button is a no-op here, but give at least some feedback for the button press
1747 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001748 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001749 }
1750
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001752 * Re-listen when widgets are reset.
1753 */
1754 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001755 if (mAppWidgetHost != null) {
1756 mAppWidgetHost.startListening();
1757 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001758 }
1759
1760 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1762 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001764 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001765 if (mModel != null) {
1766 mModel.unbindWorkspaceItems();
1767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 /**
1771 * Launches the intent referred by the clicked shortcut.
1772 *
1773 * @param v The view representing the clicked shortcut.
1774 */
1775 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001776 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1777 // view has detached (it's possible for this to happen if the view is removed mid touch).
1778 if (v.getWindowToken() == null) {
1779 return;
1780 }
1781
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001782 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001783 return;
1784 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001787 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001789 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001790 int[] pos = new int[2];
1791 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001792 intent.setSourceBounds(new Rect(pos[0], pos[1],
1793 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001794 boolean success = startActivitySafely(intent, tag);
1795
1796 if (success && v instanceof BubbleTextView) {
1797 mWaitingForResume = (BubbleTextView) v;
1798 mWaitingForResume.setStayPressed(true);
1799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001801 if (v instanceof FolderIcon) {
1802 FolderIcon fi = (FolderIcon) v;
1803 handleFolderClick(fi);
1804 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001805 } else if (v == mAllAppsButton) {
1806 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001807 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001808 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001809 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812 }
1813
Michael Jurka0e260592010-06-30 17:07:39 -07001814 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001815 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001816 // clicking anywhere on the workspace causes the customization drawer to slide down
1817 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001818 return false;
1819 }
1820
Michael Jurkaaf442092010-06-10 17:01:57 -07001821 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001822 * Event handler for the search button
1823 *
1824 * @param v The view that was clicked.
1825 */
1826 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001827 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1828
Amith Yamasania135ba82011-08-09 17:42:01 -07001829 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001830 }
1831
1832 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001833 * Event handler for the voice button
1834 *
1835 * @param v The view that was clicked.
1836 */
1837 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001838 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001839
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001840 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001841 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001842 startActivity(intent);
1843 }
1844
1845 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001846 * Event handler for the "grid" button that appears on the home screen, which
1847 * enters all apps mode.
1848 *
1849 * @param v The view that was clicked.
1850 */
1851 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001852 showAllApps(true);
1853 }
1854
1855 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001856 // Provide the same haptic feedback that the system offers for virtual keys.
1857 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001858 }
1859
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001860 public void onClickAppMarketButton(View v) {
1861 if (mAppMarketIntent != null) {
1862 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001863 } else {
1864 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001865 }
1866 }
1867
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001868 void startApplicationDetailsActivity(ComponentName componentName) {
1869 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001870 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1871 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001872 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001873 startActivity(intent);
1874 }
1875
Patrick Dubroy5539af72010-09-07 15:22:01 -07001876 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1877 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1878 // System applications cannot be installed. For now, show a toast explaining that.
1879 // We may give them the option of disabling apps this way.
1880 int messageId = R.string.uninstall_system_app_text;
1881 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1882 } else {
1883 String packageName = appInfo.componentName.getPackageName();
1884 String className = appInfo.componentName.getClassName();
1885 Intent intent = new Intent(
1886 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001887 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1888 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001889 startActivity(intent);
1890 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001891 }
1892
Michael Jurkaddd62e92011-02-16 17:49:14 -08001893 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1895 try {
1896 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001897 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 } catch (ActivityNotFoundException e) {
1899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001900 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 } catch (SecurityException e) {
1902 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001903 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001905 "or use the exported attribute for this activity. "
1906 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001908 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001910
Romain Guy8e633c52010-03-04 12:51:36 -08001911 void startActivityForResultSafely(Intent intent, int requestCode) {
1912 try {
1913 startActivityForResult(intent, requestCode);
1914 } catch (ActivityNotFoundException e) {
1915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1916 } catch (SecurityException e) {
1917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1918 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1919 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1920 "or use the exported attribute for this activity.", e);
1921 }
1922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
Adam Cohena9cf38f2011-05-02 15:36:58 -07001924 private void handleFolderClick(FolderIcon folderIcon) {
1925 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001926 Folder openFolder = mWorkspace.getFolderForTag(info);
1927
1928 // If the folder info reports that the associated folder is open, then verify that
1929 // it is actually opened. There have been a few instances where this gets out of sync.
1930 if (info.opened && openFolder == null) {
1931 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1932 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1933 info.opened = false;
1934 }
1935
Adam Cohena9cf38f2011-05-02 15:36:58 -07001936 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 // Close any open folder
1938 closeFolder();
1939 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001940 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 } else {
1942 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 int folderScreen;
1944 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001945 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 // .. and close it
1947 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001948 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 // Close any folder open on the current screen
1950 closeFolder();
1951 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001952 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954 }
1955 }
1956 }
1957
Adam Cohen2801caf2011-05-13 20:57:39 -07001958 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001959 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001960 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1961 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1962 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1963
Adam Cohenc51934b2011-07-26 21:07:43 -07001964 FolderInfo info = (FolderInfo) fi.getTag();
1965 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1966 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001967 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1968 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001969 }
1970
Adam Cohen2801caf2011-05-13 20:57:39 -07001971 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1972 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1973 oa.start();
1974 }
1975
1976 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001977 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001978 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1979 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1980 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1981
Adam Cohenc51934b2011-07-26 21:07:43 -07001982 FolderInfo info = (FolderInfo) fi.getTag();
1983 CellLayout cl = null;
1984 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1985 cl = (CellLayout) fi.getParent().getParent();
1986 }
1987
1988 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001989 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1990 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001991 oa.addListener(new AnimatorListenerAdapter() {
1992 @Override
1993 public void onAnimationEnd(Animator animation) {
1994 if (layout != null) {
1995 layout.clearFolderLeaveBehind();
1996 }
1997 }
1998 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 oa.start();
2000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002003 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 * is animated relative to the specified View. If the View is null, no animation
2005 * is played.
2006 *
2007 * @param folderInfo The FolderInfo describing the folder to open.
2008 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002009 public void openFolder(FolderIcon folderIcon) {
2010 Folder folder = folderIcon.mFolder;
2011 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012
Adam Cohen2801caf2011-05-13 20:57:39 -07002013 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002014 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015
Adam Cohen4554ee12011-08-03 16:13:21 -07002016 // Just verify that the folder hasn't already been added to the DragLayer.
2017 // There was a one-off crash where the folder had a parent already.
2018 if (folder.getParent() == null) {
2019 mDragLayer.addView(folder);
2020 mDragController.addDropTarget((DropTarget) folder);
2021 } else {
2022 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2023 folder.getParent() + ").");
2024 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002025 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002026 }
2027
2028 public void closeFolder() {
2029 Folder folder = mWorkspace.getOpenFolder();
2030 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002031 if (folder.isEditingName()) {
2032 folder.dismissEditingName();
2033 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002034 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002035
2036 // Dismiss the folder cling
2037 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002038 }
2039 }
2040
2041 void closeFolder(Folder folder) {
2042 folder.getInfo().opened = false;
2043
2044 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2045 if (parent != null) {
2046 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2047 shrinkAndFadeInFolderIcon(fi);
2048 }
2049 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
2051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002053
Patrick Dubroye708c522011-03-01 16:03:43 -08002054 if (mState != State.WORKSPACE) {
2055 return false;
2056 }
2057
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 return false;
2060 }
2061
2062 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002063 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 resetAddInfo();
2067 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002069 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 return true;
2071 }
2072
Winson Chung3d503fb2011-07-13 17:25:49 -07002073 // The hotseat touch handling does not go through Workspace, and we always allow long press
2074 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002075 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002076 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2077 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002078 if (itemUnderLongClick == null) {
2079 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002080 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2081 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002082 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002084 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002086 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 }
2088 }
2089 }
2090 return true;
2091 }
2092
Winson Chung3d503fb2011-07-13 17:25:49 -07002093 boolean isHotseatLayout(View layout) {
2094 return mHotseat != null && layout != null &&
2095 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2096 }
2097 Hotseat getHotseat() {
2098 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002099 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002100 SearchDropTargetBar getSearchBar() {
2101 return mSearchDropTargetBar;
2102 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002103
Winson Chung3d503fb2011-07-13 17:25:49 -07002104 /**
2105 * Returns the CellLayout of the specified container at the specified screen.
2106 */
2107 CellLayout getCellLayout(long container, int screen) {
2108 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2109 if (mHotseat != null) {
2110 return mHotseat.getLayout();
2111 } else {
2112 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002113 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002114 } else {
2115 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002116 }
2117 }
2118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 Workspace getWorkspace() {
2120 return mWorkspace;
2121 }
2122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 @Override
2124 protected Dialog onCreateDialog(int id) {
2125 switch (id) {
2126 case DIALOG_CREATE_SHORTCUT:
2127 return new CreateShortcut().createDialog();
2128 case DIALOG_RENAME_FOLDER:
2129 return new RenameFolder().createDialog();
2130 }
2131
2132 return super.onCreateDialog(id);
2133 }
2134
2135 @Override
2136 protected void onPrepareDialog(int id, Dialog dialog) {
2137 switch (id) {
2138 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 break;
2140 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002141 if (mFolderInfo != null) {
2142 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2143 final CharSequence text = mFolderInfo.title;
2144 input.setText(text);
2145 input.setSelection(0, text.length());
2146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 break;
2148 }
2149 }
2150
2151 void showRenameDialog(FolderInfo info) {
2152 mFolderInfo = info;
2153 mWaitingForResult = true;
2154 showDialog(DIALOG_RENAME_FOLDER);
2155 }
2156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002157 private class RenameFolder {
2158 private EditText mInput;
2159
2160 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2162 mInput = (EditText) layout.findViewById(R.id.folder_name);
2163
2164 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2165 builder.setIcon(0);
2166 builder.setTitle(getString(R.string.rename_folder_title));
2167 builder.setCancelable(true);
2168 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2169 public void onCancel(DialogInterface dialog) {
2170 cleanup();
2171 }
2172 });
2173 builder.setNegativeButton(getString(R.string.cancel_action),
2174 new Dialog.OnClickListener() {
2175 public void onClick(DialogInterface dialog, int which) {
2176 cleanup();
2177 }
2178 }
2179 );
2180 builder.setPositiveButton(getString(R.string.rename_action),
2181 new Dialog.OnClickListener() {
2182 public void onClick(DialogInterface dialog, int which) {
2183 changeFolderName();
2184 }
2185 }
2186 );
2187 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002188
2189 final AlertDialog dialog = builder.create();
2190 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2191 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002192 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002193 mInput.requestFocus();
2194 InputMethodManager inputManager = (InputMethodManager)
2195 getSystemService(Context.INPUT_METHOD_SERVICE);
2196 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002197 }
2198 });
2199
2200 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 }
2202
2203 private void changeFolderName() {
2204 final String name = mInput.getText().toString();
2205 if (!TextUtils.isEmpty(name)) {
2206 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002207 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 mFolderInfo.title = name;
2209 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2210
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002212 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002213 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 } else {
2215 final FolderIcon folderIcon = (FolderIcon)
2216 mWorkspace.getViewForTag(mFolderInfo);
2217 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002218 // TODO: At some point we'll probably want some version of setting
2219 // the text for a folder icon.
2220 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 getWorkspace().requestLayout();
2222 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002223 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002225 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 }
2227 }
2228 }
2229 cleanup();
2230 }
2231
2232 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 dismissDialog(DIALOG_RENAME_FOLDER);
2234 mWaitingForResult = false;
2235 mFolderInfo = null;
2236 }
2237 }
2238
Daniel Sandler843e8602010-06-07 14:59:01 -04002239 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2240 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002241 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002242 }
2243
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002244 public boolean isAllAppsButtonRank(int rank) {
2245 return mHotseat.isAllAppsButtonRank(rank);
2246 }
2247
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002248 // AllAppsView.Watcher
2249 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002250 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002251 mWorkspace.setVisibility(View.GONE);
2252 }
2253 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254
2255 /**
2256 * Helper method for the cameraZoomIn/cameraZoomOut animations
2257 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002258 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002259 * @param scaleFactor The scale factor used for the zoom
2260 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002261 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002262 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002263 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002264 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002265
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002266 void updateWallpaperVisibility(boolean visible) {
2267 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2268 int curflags = getWindow().getAttributes().flags
2269 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2270 if (wpflags != curflags) {
2271 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2272 }
2273 }
2274
Michael Jurkabed61d22012-02-14 22:51:29 -08002275 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2276 if (v instanceof LauncherTransitionable) {
2277 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2278 }
Winson Chung70442722012-02-10 15:43:22 -08002279
2280 // Update the workspace transition step as well
2281 dispatchOnLauncherTransitionStep(v, 0f);
2282 }
2283
2284 private void dispatchOnLauncherTransitionStep(View v, float t) {
2285 if (v instanceof LauncherTransitionable) {
2286 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2287 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002288 }
2289
2290 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2291 if (v instanceof LauncherTransitionable) {
2292 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2293 }
Winson Chung70442722012-02-10 15:43:22 -08002294
2295 // Update the workspace transition step as well
2296 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002297 }
2298
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002299 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002300 * Things to test when changing the following seven functions.
2301 * - Home from workspace
2302 * - from center screen
2303 * - from other screens
2304 * - Home from all apps
2305 * - from center screen
2306 * - from other screens
2307 * - Back from all apps
2308 * - from center screen
2309 * - from other screens
2310 * - Launch app from workspace and quit
2311 * - with back
2312 * - with home
2313 * - Launch app from all apps and quit
2314 * - with back
2315 * - with home
2316 * - Go to a screen that's not the default, then all
2317 * apps, and launch and app, and go back
2318 * - with back
2319 * -with home
2320 * - On workspace, long press power and go back
2321 * - with back
2322 * - with home
2323 * - On all apps, long press power and go back
2324 * - with back
2325 * - with home
2326 * - On workspace, power off
2327 * - On all apps, power off
2328 * - Launch an app and turn off the screen while in that app
2329 * - Go back with home key
2330 * - Go back with back key TODO: make this not go to workspace
2331 * - From all apps
2332 * - From workspace
2333 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2334 * - From all apps
2335 * - From the center workspace
2336 * - From another workspace
2337 */
2338
2339 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002340 * Zoom the camera out from the workspace to reveal 'toView'.
2341 * Assumes that the view to show is anchored at either the very top or very bottom
2342 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002343 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002344 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002345 if (mStateAnimation != null) {
2346 mStateAnimation.cancel();
2347 mStateAnimation = null;
2348 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002349 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002350
Winson Chungf0ea4d32011-06-06 14:27:16 -07002351 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2352 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2353 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002354 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002356 final int startDelay =
2357 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002358
Michael Jurkab3e22d92011-10-31 15:58:33 -07002359 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002360
Michael Jurkad74c9842011-07-10 12:44:21 -07002361 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002362 Animator workspaceAnim =
2363 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002364
2365 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002366 toView.setScaleX(scale);
2367 toView.setScaleY(scale);
2368 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2369 scaleAnim.
2370 scaleX(1f).scaleY(1f).
2371 setDuration(duration).
2372 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002373
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002375 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002376 final ObjectAnimator alphaAnim = ObjectAnimator
2377 .ofFloat(toView, "alpha", 0f, 1f)
2378 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002379 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002380 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2381 @Override
2382 public void onAnimationUpdate(ValueAnimator animation) {
2383 float t = (Float) animation.getAnimatedValue();
2384 dispatchOnLauncherTransitionStep(fromView, t);
2385 dispatchOnLauncherTransitionStep(toView, t);
2386 }
2387 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002388
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002389 // toView should appear right at the end of the workspace shrink
2390 // animation
2391 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002392 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002393 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002394
2395 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002396 boolean animationCancelled = false;
2397
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002398 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002399 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002400 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002401 // Prepare the position
2402 toView.setTranslationX(0.0f);
2403 toView.setTranslationY(0.0f);
2404 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002405 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002407 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002408 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002409 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2410 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002411
2412 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2413 // Hide the workspace scrollbar
2414 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002415 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002416 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002417 if (!animationCancelled) {
2418 updateWallpaperVisibility(false);
2419 }
2420 }
2421
Adam Cohencff6af82011-09-13 14:51:53 -07002422 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002423 public void onAnimationCancel(Animator animation) {
2424 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002425 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 });
2427
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002428 if (workspaceAnim != null) {
2429 mStateAnimation.play(workspaceAnim);
2430 }
Michael Jurka1899a362011-11-03 13:50:45 -07002431
2432 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002433 final ViewTreeObserver observer;
2434
Michael Jurkabed61d22012-02-14 22:51:29 -08002435 dispatchOnLauncherTransitionStart(fromView, animated, false);
2436 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002437
2438 // If any of the objects being animated haven't been measured/laid out
2439 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002440 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002441 (mWorkspace.getMeasuredWidth() == 0) ||
2442 (toView.getMeasuredWidth() == 0)) {
2443 observer = mWorkspace.getViewTreeObserver();
2444 delayAnim = true;
2445 } else {
2446 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002447 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002448
2449 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002450 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002451 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2452 public void onGlobalLayout() {
2453 mWorkspace.post(new Runnable() {
2454 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002455 // Check that mStateAnimation hasn't changed while
2456 // we waited for a layout pass
2457 if (mStateAnimation == stateAnimation) {
2458 // Need to update pivots for zoom if layout changed
2459 setPivotsForZoom(toView, scale);
2460 mStateAnimation.start();
2461 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002462 }
2463 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002464 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002465 }
2466 };
2467 observer.addOnGlobalLayoutListener(delayedStart);
2468 } else {
2469 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002470 mStateAnimation.start();
2471 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 } else {
2473 toView.setTranslationX(0.0f);
2474 toView.setTranslationY(0.0f);
2475 toView.setScaleX(1.0f);
2476 toView.setScaleY(1.0f);
2477 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002478 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002479
Michael Jurkabed61d22012-02-14 22:51:29 -08002480 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2481 // Hide the workspace scrollbar
2482 mWorkspace.hideScrollingIndicator(true);
2483 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002484 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002485 dispatchOnLauncherTransitionStart(fromView, animated, false);
2486 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2487 dispatchOnLauncherTransitionStart(toView, animated, false);
2488 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002489 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002491 }
2492
2493 /**
2494 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002495 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002497 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002498 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2499 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002500
Michael Jurkab3e22d92011-10-31 15:58:33 -07002501 if (mStateAnimation != null) {
2502 mStateAnimation.cancel();
2503 mStateAnimation = null;
2504 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002505 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002506
Winson Chungf0ea4d32011-06-06 14:27:16 -07002507 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002508 final int fadeOutDuration =
2509 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002510 final float scaleFactor = (float)
2511 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2512 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002513 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002514 Animator workspaceAnim = null;
2515
2516 if (toState == State.WORKSPACE) {
2517 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2518 workspaceAnim = mWorkspace.getChangeStateAnimation(
2519 Workspace.State.NORMAL, animated, stagger);
2520 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2521 workspaceAnim = mWorkspace.getChangeStateAnimation(
2522 Workspace.State.SPRING_LOADED, animated);
2523 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002524
Michael Jurkab3e22d92011-10-31 15:58:33 -07002525 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002526 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002527 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002528 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002529 final LauncherViewPropertyAnimator scaleAnim =
2530 new LauncherViewPropertyAnimator(fromView);
2531 scaleAnim.
2532 scaleX(scaleFactor).scaleY(scaleFactor).
2533 setDuration(duration).
2534 setInterpolator(new Workspace.ZoomInInterpolator());
2535
2536 final ObjectAnimator alphaAnim = ObjectAnimator
2537 .ofFloat(fromView, "alpha", 1f, 0f)
2538 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002539 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002540 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2541 @Override
2542 public void onAnimationUpdate(ValueAnimator animation) {
2543 float t = 1f - (Float) animation.getAnimatedValue();
2544 dispatchOnLauncherTransitionStep(fromView, t);
2545 dispatchOnLauncherTransitionStep(toView, t);
2546 }
2547 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002548
Michael Jurkabed61d22012-02-14 22:51:29 -08002549 mStateAnimation = new AnimatorSet();
2550
2551 dispatchOnLauncherTransitionStart(fromView, animated, true);
2552 dispatchOnLauncherTransitionStart(toView, animated, true);
2553
2554 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002555 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002556 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002557 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002558 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002559 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2560 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002561 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002562 if (onCompleteRunnable != null) {
2563 onCompleteRunnable.run();
2564 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002565 }
2566 });
2567
Winson Chungf0ea4d32011-06-06 14:27:16 -07002568 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002569 if (workspaceAnim != null) {
2570 mStateAnimation.play(workspaceAnim);
2571 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002572 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002573 } else {
2574 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002575 dispatchOnLauncherTransitionStart(fromView, animated, true);
2576 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2577 dispatchOnLauncherTransitionStart(toView, animated, true);
2578 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002579 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002581 }
2582
Michael Jurkae326f182011-11-21 14:05:46 -08002583 @Override
2584 public void onTrimMemory(int level) {
2585 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002586 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002587 mAppsCustomizeTabHost.onTrimMemory();
2588 }
2589 }
2590
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002591 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002592 showWorkspace(animated, null);
2593 }
2594
2595 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002596 if (mState != State.WORKSPACE) {
2597 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002598 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002599
2600 // Show the search bar and hotseat
2601 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002602 // We only need to animate in the dock divider if we're going from spring loaded mode
2603 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002604
2605 // Set focus to the AppsCustomize button
2606 if (mAllAppsButton != null) {
2607 mAllAppsButton.requestFocus();
2608 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002609 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002610
Michael Jurkab737ee62011-11-15 15:57:22 -08002611 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002612
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002613 // Change the state *after* we've called all the transition code
2614 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002615
Winson Chung5fb63472011-02-02 17:03:37 -08002616 // Resume the auto-advance of widgets
2617 mUserPresent = true;
2618 updateRunning();
2619
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002620 // send an accessibility event to announce the context change
2621 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002622 }
2623
Michael Jurkab3e22d92011-10-31 15:58:33 -07002624 void showAllApps(boolean animated) {
2625 if (mState != State.WORKSPACE) return;
2626
2627 showAppsCustomizeHelper(animated, false);
2628 mAppsCustomizeTabHost.requestFocus();
2629
2630 // Hide the search bar and hotseat
2631 mSearchDropTargetBar.hideSearchBar(animated);
2632
2633 // Change the state *after* we've called all the transition code
2634 mState = State.APPS_CUSTOMIZE;
2635
2636 // Pause the auto-advance of widgets until we are out of AllApps
2637 mUserPresent = false;
2638 updateRunning();
2639 closeFolder();
2640
2641 // Send an accessibility event to announce the context change
2642 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2643 }
2644
Adam Cohen7777d962011-08-18 18:58:38 -07002645 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002646 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002647 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002648 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002649 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002650 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002651 }
Adam Cohen7777d962011-08-18 18:58:38 -07002652
Adam Cohened66b2b2012-01-23 17:28:51 -08002653 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2654 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002655 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2656
Winson Chunge7a03942011-08-05 15:05:12 -07002657 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002658 @Override
2659 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002660 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002661 // Before we show workspace, hide all apps again because
2662 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2663 // clean up our state transition functions
2664 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002665 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002666 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002667 } else {
2668 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002669 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002670 }
2671 }, (extendedDelay ?
2672 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2673 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2674 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002675
Michael Jurkad3ef3062010-11-23 16:23:58 -08002676 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002677 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002678 final boolean animated = true;
2679 final boolean springLoaded = true;
2680 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002681 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002682 }
2683 // Otherwise, we are not in spring loaded mode, so don't do anything.
2684 }
2685
Michael Jurkab737ee62011-11-15 15:57:22 -08002686 void hideDockDivider() {
2687 if (mQsbDivider != null && mDockDivider != null) {
2688 mQsbDivider.setVisibility(View.INVISIBLE);
2689 mDockDivider.setVisibility(View.INVISIBLE);
2690 }
2691 }
2692
2693 void showDockDivider(boolean animated) {
2694 if (mQsbDivider != null && mDockDivider != null) {
2695 mQsbDivider.setVisibility(View.VISIBLE);
2696 mDockDivider.setVisibility(View.VISIBLE);
2697 if (mDividerAnimator != null) {
2698 mDividerAnimator.cancel();
2699 mQsbDivider.setAlpha(1f);
2700 mDockDivider.setAlpha(1f);
2701 mDividerAnimator = null;
2702 }
2703 if (animated) {
2704 mDividerAnimator = new AnimatorSet();
2705 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2706 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2707 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2708 mDividerAnimator.start();
2709 }
2710 }
2711 }
2712
Michael Jurkab3e22d92011-10-31 15:58:33 -07002713 void lockAllApps() {
2714 // TODO
2715 }
2716
2717 void unlockAllApps() {
2718 // TODO
2719 }
2720
Adam Cohenfc53cd22011-07-20 15:45:11 -07002721 public boolean isAllAppsCustomizeOpen() {
2722 return mState == State.APPS_CUSTOMIZE;
2723 }
2724
Winson Chungf0ea4d32011-06-06 14:27:16 -07002725 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002726 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002727 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002728 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002729 if (!LauncherApplication.isScreenLarge()) {
2730 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002731 if (mHotseat.getAlpha() != 1f) {
2732 int duration = mSearchDropTargetBar.getTransitionInDuration();
2733 mHotseat.animate().alpha(1f).setDuration(duration);
2734 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002735 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002736 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002737 }
2738 }
2739 }
2740
2741 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002742 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002743 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002744 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002745 if (!LauncherApplication.isScreenLarge()) {
2746 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002747 if (mHotseat.getAlpha() != 0f) {
2748 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2749 mHotseat.animate().alpha(0f).setDuration(duration);
2750 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002752 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002753 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002754 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002755 }
2756
Patrick Dubroy5f445422011-02-18 14:35:21 -08002757 /**
2758 * Add an item from all apps or customize onto the given workspace screen.
2759 * If layout is null, add to the current screen.
2760 */
2761 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002762 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002763 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002764 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002765 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002766
Winson Chungdff8ebb2011-09-08 17:25:31 -07002767 /** Maps the current orientation to an index for referencing orientation correct global icons */
2768 private int getCurrentOrientationIndexForGlobalIcons() {
2769 // default - 0, landscape - 1
2770 switch (getResources().getConfiguration().orientation) {
2771 case Configuration.ORIENTATION_LANDSCAPE:
2772 return 1;
2773 default:
2774 return 0;
2775 }
2776 }
2777
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002778 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002780 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002781 // Look for the toolbar icon specified in the activity meta-data
2782 Bundle metaData = packageManager.getActivityInfo(
2783 activityName, PackageManager.GET_META_DATA).metaData;
2784 if (metaData != null) {
2785 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2786 if (iconResId != 0) {
2787 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002788 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002789 }
2790 }
2791 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002792 // This can happen if the activity defines an invalid drawable
2793 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2794 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002795 } catch (Resources.NotFoundException nfe) {
2796 // This can happen if the activity defines an invalid drawable
2797 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2798 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002799 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002800 return null;
2801 }
2802
2803 // if successful in getting icon, return it; otherwise, set button to use default drawable
2804 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2805 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002807 Resources r = getResources();
2808 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2809 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002810
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002811 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002812 // If we were unable to find the icon via the meta-data, use a generic one
2813 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002814 toolbarIcon = r.getDrawable(fallbackDrawableId);
2815 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002816 if (button != null) {
2817 button.setCompoundDrawables(toolbarIcon, null, null, null);
2818 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002819 return null;
2820 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002821 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002822 if (button != null) {
2823 button.setCompoundDrawables(toolbarIcon, null, null, null);
2824 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002825 return toolbarIcon.getConstantState();
2826 }
2827 }
2828
2829 // if successful in getting icon, return it; otherwise, set button to use default drawable
2830 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2831 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2832 ImageView button = (ImageView) findViewById(buttonId);
2833 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2834
Michael Jurka19e0fc52011-07-22 18:00:21 -07002835 if (button != null) {
2836 // If we were unable to find the icon via the meta-data, use a
2837 // generic one
2838 if (toolbarIcon == null) {
2839 button.setImageResource(fallbackDrawableId);
2840 } else {
2841 button.setImageDrawable(toolbarIcon);
2842 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002843 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002844
2845 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2846
Michael Jurka0423dcf2010-10-05 14:56:18 -07002847 }
2848
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002849 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2850 TextView button = (TextView) findViewById(buttonId);
2851 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2852 }
2853
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002854 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002855 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002856 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002857 }
2858
Winson Chungbb185bd2011-11-21 12:31:42 -08002859 private void invalidatePressedFocusedStates(View container, View button) {
2860 if (container instanceof HolographicLinearLayout) {
2861 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2862 layout.invalidatePressedFocusedStates();
2863 } else if (button instanceof HolographicImageView) {
2864 HolographicImageView view = (HolographicImageView) button;
2865 view.invalidatePressedFocusedStates();
2866 }
2867 }
2868
Winson Chungc51db6a2011-10-05 11:44:49 -07002869 private boolean updateGlobalSearchIcon() {
2870 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002871 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2872 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002873 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002874 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002875
Winson Chung1cad91e2011-05-25 17:41:01 -07002876 final SearchManager searchManager =
2877 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2878 ComponentName activityName = searchManager.getGlobalSearchActivity();
2879 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002880 int coi = getCurrentOrientationIndexForGlobalIcons();
2881 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002882 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002883 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002884 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2885 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002886 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002887 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002888 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002889 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002890 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002891 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2892 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2893 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002894 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002895 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002896 }
2897 }
2898
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002899 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002900 final View searchButtonContainer = findViewById(R.id.search_button_container);
2901 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002902 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002903 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002904 }
2905
Winson Chungc51db6a2011-10-05 11:44:49 -07002906 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002907 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002908 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002909 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002910
Winson Chungc51db6a2011-10-05 11:44:49 -07002911 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002912 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2913 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002914 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002915 int coi = getCurrentOrientationIndexForGlobalIcons();
2916 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002917 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002918 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002919 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002920 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002921 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002922 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002923 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002924 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002925 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002926 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002927 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002928 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002929 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002930
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002931 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002932 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2933 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002934 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002935 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002936 }
2937
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002938 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002939 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002940 */
2941 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002942 final View marketButton = findViewById(R.id.market_button);
2943 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2944 // Find the app market activity by resolving an intent.
2945 // (If multiple app markets are installed, it will return the ResolverActivity.)
2946 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002947 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002948 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002949 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002950 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002951 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002952 marketButton.setVisibility(View.VISIBLE);
2953 } else {
2954 // We should hide and disable the view so that we don't try and restore the visibility
2955 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2956 marketButton.setVisibility(View.GONE);
2957 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002958 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002959 }
2960
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002961 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002962 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002963 }
2964
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002965 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 * Displays the shortcut creation dialog and launches, if necessary, the
2967 * appropriate activity.
2968 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002969 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002970 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2971 DialogInterface.OnShowListener {
2972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002977
Winson Chung55b65502011-05-26 12:03:43 -07002978 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2979 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002980 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002982 AlertDialog dialog = builder.create();
2983 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002984 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002985 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 return dialog;
2988 }
2989
2990 public void onCancel(DialogInterface dialog) {
2991 mWaitingForResult = false;
2992 cleanup();
2993 }
2994
Romain Guycbb89e42009-06-08 15:52:54 -07002995 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002996 mWaitingForResult = false;
2997 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002998 }
2999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003001 try {
3002 dismissDialog(DIALOG_CREATE_SHORTCUT);
3003 } catch (Exception e) {
3004 // An exception is thrown if the dialog is not visible, which is fine
3005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003008 /**
3009 * Handle the action clicked in the "Add to home" dialog.
3010 */
3011 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003012 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003013
Winson Chung55b65502011-05-26 12:03:43 -07003014 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3015 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003016 case AddAdapter.ITEM_APPLICATION: {
3017 if (mAppsCustomizeTabHost != null) {
3018 mAppsCustomizeTabHost.selectAppsTab();
3019 }
3020 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003021 break;
3022 }
Winson Chung55b65502011-05-26 12:03:43 -07003023 case AddAdapter.ITEM_APPWIDGET: {
3024 if (mAppsCustomizeTabHost != null) {
3025 mAppsCustomizeTabHost.selectWidgetsTab();
3026 }
3027 showAllApps(true);
3028 break;
3029 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003030 case AddAdapter.ITEM_WALLPAPER: {
3031 startWallpaper();
3032 break;
3033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 }
3035 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003036
3037 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003038 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 }
3041
3042 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003043 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003044 */
3045 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3046 @Override
3047 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003048 closeSystemDialogs();
3049 }
3050 }
3051
3052 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003053 * Receives notifications whenever the appwidgets are reset.
3054 */
3055 private class AppWidgetResetObserver extends ContentObserver {
3056 public AppWidgetResetObserver() {
3057 super(new Handler());
3058 }
3059
3060 @Override
3061 public void onChange(boolean selfChange) {
3062 onAppWidgetReset();
3063 }
3064 }
3065
3066 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003067 * If the activity is currently paused, signal that we need to re-run the loader
3068 * in onResume.
3069 *
3070 * This needs to be called from incoming places where resources might have been loaded
3071 * while we are paused. That is becaues the Configuration might be wrong
3072 * when we're not running, and if it comes back to what it was when we
3073 * were paused, we are not restarted.
3074 *
3075 * Implementation of the method from LauncherModel.Callbacks.
3076 *
3077 * @return true if we are currently paused. The caller might be able to
3078 * skip some work in that case since we will come back again.
3079 */
3080 public boolean setLoadOnResume() {
3081 if (mPaused) {
3082 Log.i(TAG, "setLoadOnResume");
3083 mOnResumeNeedsLoad = true;
3084 return true;
3085 } else {
3086 return false;
3087 }
3088 }
3089
3090 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003094 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003095 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003096 } else {
3097 return SCREEN_COUNT / 2;
3098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003100
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 /**
3102 * Refreshes the shortcuts shown on the workspace.
3103 *
3104 * Implementation of the method from LauncherModel.Callbacks.
3105 */
3106 public void startBinding() {
3107 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003108
Winson Chungf0c6ae02012-03-21 16:10:31 -07003109 mNewShortcutAnimatePage = -1;
3110 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003111 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 int count = workspace.getChildCount();
3113 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003114 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003115 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3116 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003118 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 if (mHotseat != null) {
3120 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122 }
3123
3124 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003125 * Bind the items start-end from the list.
3126 *
3127 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003130 setLoadOnResume();
3131
Winson Chungf0c6ae02012-03-21 16:10:31 -07003132 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3133 Set<String> newApps = new HashSet<String>();
3134 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3135
3136 Workspace workspace = mWorkspace;
3137 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003139
3140 // Short circuit if we are loading dock items for a configuration which has no dock
3141 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3142 mHotseat == null) {
3143 continue;
3144 }
3145
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 switch (item.itemType) {
3147 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3148 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003149 ShortcutInfo info = (ShortcutInfo) item;
3150 String uri = info.intent.toUri(0).toString();
3151 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003152 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3153 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003154 if (newApps.contains(uri)) {
3155 newApps.remove(uri);
3156
3157 // Prepare the view to be animated up
3158 shortcut.setAlpha(0f);
3159 shortcut.setScaleX(0f);
3160 shortcut.setScaleY(0f);
3161 mNewShortcutAnimatePage = item.screen;
3162 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3163 mNewShortcutAnimateViews.add(shortcut);
3164 }
3165 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003166 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003167 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003169 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003170 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3172 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003173 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003176
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 }
3179
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 /**
3181 * Implementation of the method from LauncherModel.Callbacks.
3182 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003183 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003184 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003185 sFolders.clear();
3186 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188
3189 /**
3190 * Add the views for a widget to the workspace.
3191 *
3192 * Implementation of the method from LauncherModel.Callbacks.
3193 */
3194 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003195 setLoadOnResume();
3196
Daniel Sandler843e8602010-06-07 14:59:01 -04003197 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3198 if (DEBUG_WIDGETS) {
3199 Log.d(TAG, "bindAppWidget: " + item);
3200 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 final Workspace workspace = mWorkspace;
3202
3203 final int appWidgetId = item.appWidgetId;
3204 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003205 if (DEBUG_WIDGETS) {
3206 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3207 }
3208
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3210
Joe Onorato9c1289c2009-08-17 11:03:03 -04003211 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3212 item.hostView.setTag(item);
3213
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003215 item.cellY, item.spanX, item.spanY, false);
3216
Adam Cohended9f8d2010-11-03 13:25:16 -07003217 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3218
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 workspace.requestLayout();
3220
Daniel Sandler843e8602010-06-07 14:59:01 -04003221 if (DEBUG_WIDGETS) {
3222 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3223 + (SystemClock.uptimeMillis()-start) + "ms");
3224 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003225 }
3226
3227 /**
3228 * Callback saying that there aren't any more items to bind.
3229 *
3230 * Implementation of the method from LauncherModel.Callbacks.
3231 */
3232 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003233 setLoadOnResume();
3234
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 if (mSavedState != null) {
3236 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003237 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003239 mSavedState = null;
3240 }
3241
3242 if (mSavedInstanceState != null) {
3243 super.onRestoreInstanceState(mSavedInstanceState);
3244 mSavedInstanceState = null;
3245 }
3246
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003247 // If we received the result of any pending adds while the loader was running (e.g. the
3248 // widget configuration forced an orientation change), process them now.
3249 for (int i = 0; i < sPendingAddList.size(); i++) {
3250 completeAdd(sPendingAddList.get(i));
3251 }
3252 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253
Winson Chungc51db6a2011-10-05 11:44:49 -07003254 // Update the market app icon as necessary (the other icons will be managed in response to
3255 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003256 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003257
Winson Chungf0c6ae02012-03-21 16:10:31 -07003258 // Animate up any icons as necessary
3259 if (mVisible || mWorkspaceLoading) {
3260 Runnable newAppsRunnable = new Runnable() {
3261 @Override
3262 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003263 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003264 }
3265 };
Winson Chunga2413752012-04-03 14:22:34 -07003266
3267 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3268 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3269 if (canRunNewAppsAnimation()) {
3270 // If the user has not interacted recently, then either snap to the new page to show
3271 // the new-apps animation or just run them if they are to appear on the current page
3272 if (willSnapPage) {
3273 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3274 } else {
3275 runNewAppsAnimation(false);
3276 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003277 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003278 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003279 // are on another page (or just normally if they are added to the current page)
3280 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003281 }
3282 }
3283
3284 mWorkspaceLoading = false;
3285 }
3286
Winson Chunga2413752012-04-03 14:22:34 -07003287 private boolean canRunNewAppsAnimation() {
3288 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3289 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3290 }
3291
Winson Chungf0c6ae02012-03-21 16:10:31 -07003292 /**
3293 * Runs a new animation that scales up icons that were added while Launcher was in the
3294 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003295 *
3296 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003297 */
Winson Chunga2413752012-04-03 14:22:34 -07003298 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003299 AnimatorSet anim = new AnimatorSet();
3300 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003301
3302 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003303 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3304 @Override
3305 public int compare(View a, View b) {
3306 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3307 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3308 int cellCountX = LauncherModel.getCellCountX();
3309 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3310 }
3311 });
Winson Chunga2413752012-04-03 14:22:34 -07003312
3313 // Animate each of the views in place (or show them immediately if requested)
3314 if (immediate) {
3315 for (View v : mNewShortcutAnimateViews) {
3316 v.setAlpha(1f);
3317 v.setScaleX(1f);
3318 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003319 }
Winson Chunga2413752012-04-03 14:22:34 -07003320 } else {
3321 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3322 View v = mNewShortcutAnimateViews.get(i);
3323 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3324 PropertyValuesHolder.ofFloat("alpha", 1f),
3325 PropertyValuesHolder.ofFloat("scaleX", 1f),
3326 PropertyValuesHolder.ofFloat("scaleY", 1f));
3327 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3328 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3329 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3330 bounceAnims.add(bounceAnim);
3331 }
3332 anim.playTogether(bounceAnims);
3333 anim.addListener(new AnimatorListenerAdapter() {
3334 @Override
3335 public void onAnimationEnd(Animator animation) {
3336 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3337 }
3338 });
3339 anim.start();
3340 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003341
3342 // Clean up
3343 mNewShortcutAnimatePage = -1;
3344 mNewShortcutAnimateViews.clear();
3345 new Thread("clearNewAppsThread") {
3346 public void run() {
3347 mSharedPrefs.edit()
3348 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3349 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3350 .commit();
3351 }
3352 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003353 }
3354
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003355 @Override
3356 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003357 boolean searchVisible = updateGlobalSearchIcon();
3358 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3359 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003360 }
3361
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003362 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 * Add the icons for all apps.
3364 *
3365 * Implementation of the method from LauncherModel.Callbacks.
3366 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003367 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3368 // Remove the progress bar entirely; we could also make it GONE
3369 // but better to remove it since we know it's not going to be used
3370 View progressBar = mAppsCustomizeTabHost.
3371 findViewById(R.id.apps_customize_progress_bar);
3372 if (progressBar != null) {
3373 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003374 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003375 // We just post the call to setApps so the user sees the progress bar
3376 // disappear-- otherwise, it just looks like the progress bar froze
3377 // which doesn't look great
3378 mAppsCustomizeTabHost.post(new Runnable() {
3379 public void run() {
3380 if (mAppsCustomizeContent != null) {
3381 mAppsCustomizeContent.setApps(apps);
3382 }
3383 }
3384 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 }
3386
3387 /**
3388 * A package was installed.
3389 *
3390 * Implementation of the method from LauncherModel.Callbacks.
3391 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003392 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003393 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003395
Winson Chung785d2eb2011-04-14 16:08:02 -07003396 if (mAppsCustomizeContent != null) {
3397 mAppsCustomizeContent.addApps(apps);
3398 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 }
3400
3401 /**
3402 * A package was updated.
3403 *
3404 * Implementation of the method from LauncherModel.Callbacks.
3405 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003406 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003407 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003409 if (mWorkspace != null) {
3410 mWorkspace.updateShortcuts(apps);
3411 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412
Winson Chung785d2eb2011-04-14 16:08:02 -07003413 if (mAppsCustomizeContent != null) {
3414 mAppsCustomizeContent.updateApps(apps);
3415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 }
3417
3418 /**
3419 * A package was uninstalled.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
Joe Onorato36115782010-06-17 13:28:48 -04003423 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003425 if (permanent) {
3426 mWorkspace.removeItems(apps);
3427 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003428
Winson Chung785d2eb2011-04-14 16:08:02 -07003429 if (mAppsCustomizeContent != null) {
3430 mAppsCustomizeContent.removeApps(apps);
3431 }
Winson Chunga1820962011-10-03 16:31:06 -07003432
3433 // Notify the drag controller
3434 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 }
Joe Onoratobe386092009-11-17 17:32:16 -08003436
3437 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003438 * A number of packages were updated.
3439 */
3440 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003441 if (mAppsCustomizeContent != null) {
3442 mAppsCustomizeContent.onPackagesUpdated();
3443 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003444 }
3445
Winson Chung400438b2011-01-16 17:53:48 -08003446 private int mapConfigurationOriActivityInfoOri(int configOri) {
3447 final Display d = getWindowManager().getDefaultDisplay();
3448 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3449 switch (d.getRotation()) {
3450 case Surface.ROTATION_0:
3451 case Surface.ROTATION_180:
3452 // We are currently in the same basic orientation as the natural orientation
3453 naturalOri = configOri;
3454 break;
3455 case Surface.ROTATION_90:
3456 case Surface.ROTATION_270:
3457 // We are currently in the other basic orientation to the natural orientation
3458 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3459 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3460 break;
3461 }
3462
3463 int[] oriMap = {
3464 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3465 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3466 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3467 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3468 };
3469 // Since the map starts at portrait, we need to offset if this device's natural orientation
3470 // is landscape.
3471 int indexOffset = 0;
3472 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3473 indexOffset = 1;
3474 }
3475 return oriMap[(d.getRotation() + indexOffset) % 4];
3476 }
Adam Cohen446e9402011-09-15 18:21:21 -07003477
3478 public void lockScreenOrientationOnLargeUI() {
3479 if (LauncherApplication.isScreenLarge()) {
3480 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3481 .getConfiguration().orientation));
3482 }
Winson Chung400438b2011-01-16 17:53:48 -08003483 }
Adam Cohen446e9402011-09-15 18:21:21 -07003484 public void unlockScreenOrientationOnLargeUI() {
3485 if (LauncherApplication.isScreenLarge()) {
3486 mHandler.postDelayed(new Runnable() {
3487 public void run() {
3488 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3489 }
3490 }, mRestoreScreenOrientationDelay);
3491 }
Winson Chung400438b2011-01-16 17:53:48 -08003492 }
3493
Winson Chung82f55532011-08-09 14:14:23 -07003494 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003495 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003496 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003497 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003498
Winson Chung7d7541e2011-09-16 20:14:36 -07003499 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003500 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003501 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3502 Cling cling = (Cling) findViewById(clingId);
3503 if (cling != null) {
3504 cling.init(this, positionData);
3505 cling.setVisibility(View.VISIBLE);
3506 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3507 if (animate) {
3508 cling.buildLayer();
3509 cling.setAlpha(0f);
3510 cling.animate()
3511 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003512 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003513 .setDuration(SHOW_CLING_DURATION)
3514 .setStartDelay(delay)
3515 .start();
3516 } else {
3517 cling.setAlpha(1f);
3518 }
3519 }
3520 return cling;
3521 }
3522 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003523 if (cling != null) {
3524 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003525 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003526 anim.addListener(new AnimatorListenerAdapter() {
3527 public void onAnimationEnd(Animator animation) {
3528 cling.setVisibility(View.GONE);
3529 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003530 // We should update the shared preferences on a background thread
3531 new Thread("dismissClingThread") {
3532 public void run() {
3533 SharedPreferences.Editor editor = mSharedPrefs.edit();
3534 editor.putBoolean(flag, true);
3535 editor.commit();
3536 }
3537 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003538 };
3539 });
3540 anim.start();
3541 }
3542 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003543 private void removeCling(int id) {
3544 final View cling = findViewById(id);
3545 if (cling != null) {
3546 final ViewGroup parent = (ViewGroup) cling.getParent();
3547 parent.post(new Runnable() {
3548 @Override
3549 public void run() {
3550 parent.removeView(cling);
3551 }
3552 });
3553 }
3554 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003555 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003556 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003557 if (isClingsEnabled() &&
3558 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003559 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003560 } else {
3561 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003562 }
3563 }
3564 public void showFirstRunAllAppsCling(int[] position) {
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.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003568 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003569 } else {
3570 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003571 }
3572 }
3573 public Cling showFirstRunFoldersCling() {
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.FOLDER_CLING_DISMISSED_KEY, false)) {
3577 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003578 } else {
3579 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003580 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003581 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003582 }
3583 public boolean isFolderClingVisible() {
3584 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003585 if (cling != null) {
3586 return cling.getVisibility() == View.VISIBLE;
3587 }
3588 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003589 }
Winson Chung82f55532011-08-09 14:14:23 -07003590 public void dismissWorkspaceCling(View v) {
3591 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003592 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003593 }
3594 public void dismissAllAppsCling(View v) {
3595 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003596 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3597 }
3598 public void dismissFolderCling(View v) {
3599 Cling cling = (Cling) findViewById(R.id.folder_cling);
3600 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003601 }
3602
Winson Chung80baf5a2010-08-09 16:03:15 -07003603 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003604 * Prints out out state for debugging.
3605 */
3606 public void dumpState() {
3607 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003608 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003609 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3610 Log.d(TAG, "mRestoring=" + mRestoring);
3611 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3612 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003613 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003614 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003615
Winson Chung785d2eb2011-04-14 16:08:02 -07003616 if (mAppsCustomizeContent != null) {
3617 mAppsCustomizeContent.dumpState();
3618 }
Joe Onoratobe386092009-11-17 17:32:16 -08003619 Log.d(TAG, "END launcher2 dump state");
3620 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003621
3622 @Override
3623 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3624 super.dump(prefix, fd, writer, args);
3625 writer.println(" ");
3626 writer.println("Debug logs: ");
3627 for (int i = 0; i < sDumpLogs.size(); i++) {
3628 writer.println(" " + sDumpLogs.get(i));
3629 }
3630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003631}
Michael Jurka2763be32011-02-24 11:19:57 -08003632
Michael Jurkaabded662011-03-04 12:06:57 -08003633interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003634 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003635 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003636 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003637 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003638}