blob: cef7137635fb1ef9ad186ad0503f56949fc04a49 [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
Adam Cohenf814aa02011-09-01 13:48:05 -0700982 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
983 if (spanXY == null) {
984 spanXY = new int[2];
985 }
986
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800987 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700988 // We want to account for the extra amount of padding that we are adding to the widget
989 // to ensure that it gets the full amount of space that it has requested
990 int requiredWidth = minWidth + padding.left + padding.right;
991 int requiredHeight = minHeight + padding.top + padding.bottom;
992 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
993 }
994
995 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
996 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
997 }
998
Adam Cohend41fbf52012-02-16 23:53:59 -0800999 int[] getMinSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
Adam Cohencbf47e32011-09-16 17:32:37 -07001000 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
1001 }
1002
Adam Cohenf814aa02011-09-01 13:48:05 -07001003 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
1004 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
1005 }
1006
Adam Cohend41fbf52012-02-16 23:53:59 -08001007 int[] getMinSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
1008 return getSpanForWidget(info.componentName, info.minResizeWidth,
1009 info.minResizeHeight, spanXY);
1010 }
1011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001013 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001015 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001016 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001018 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1019 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1020 if (appWidgetInfo == null) {
1021 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1022 }
Romain Guycbb89e42009-06-08 15:52:54 -07001023
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001024 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001025 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001026
Adam Cohend41fbf52012-02-16 23:53:59 -08001027 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001028 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001031 // We have saved the position to which the widget was dragged-- this really only matters
1032 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001033 int[] cellXY = mTmpAddItemCellCoordinates;
1034 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001035 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001036 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001037 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1038 cellXY[0] = mPendingAddInfo.cellX;
1039 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001040 spanXY[0] = mPendingAddInfo.spanX;
1041 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001042 foundCellSpan = true;
1043 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001046 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1047 spanXY[1], cellXY, finalSpan);
1048 spanXY[0] = finalSpan[0];
1049 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001050 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001052 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001053 }
1054
Michael Jurka0280c3b2010-09-17 15:00:07 -07001055 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001056 if (appWidgetId != -1) {
1057 // Deleting an app widget ID is a void call but writes to disk before returning
1058 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001059 new Thread("deleteAppWidgetId") {
1060 public void run() {
1061 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1062 }
1063 }.start();
1064 }
Winson Chung93eef082012-03-23 15:59:27 -07001065 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001066 return;
1067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001070 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 launcherInfo.spanX = spanXY[0];
1072 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001073 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1074 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078
1079 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001080 if (hostView == null) {
1081 // Perform actual inflation because we're live
1082 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1083 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1084 } else {
1085 // The AppWidgetHostView has already been inflated and instantiated
1086 launcherInfo.hostView = hostView;
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001090 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001091 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001093
1094 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001096 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Adam Cohended9f8d2010-11-03 13:25:16 -07001099 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1100 @Override
1101 public void onReceive(Context context, Intent intent) {
1102 final String action = intent.getAction();
1103 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1104 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001105 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001106 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001107
Winson Chungc100e8e2011-08-09 16:02:43 -07001108 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001109 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001110 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1111 mAppsCustomizeTabHost.reset();
1112 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001113 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001114 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1115 mUserPresent = true;
1116 updateRunning();
1117 }
1118 }
1119 };
1120
1121 @Override
1122 public void onAttachedToWindow() {
1123 super.onAttachedToWindow();
1124
1125 // Listen for broadcasts related to user-presence
1126 final IntentFilter filter = new IntentFilter();
1127 filter.addAction(Intent.ACTION_SCREEN_OFF);
1128 filter.addAction(Intent.ACTION_USER_PRESENT);
1129 registerReceiver(mReceiver, filter);
1130
Adam Cohend113e0c2010-11-11 10:48:05 -08001131 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 mVisible = true;
1133 }
1134
1135 @Override
1136 public void onDetachedFromWindow() {
1137 super.onDetachedFromWindow();
1138 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001139 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001140
Adam Cohend113e0c2010-11-11 10:48:05 -08001141 if (mAttached) {
1142 unregisterReceiver(mReceiver);
1143 mAttached = false;
1144 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001145 updateRunning();
1146 }
1147
1148 public void onWindowVisibilityChanged(int visibility) {
1149 mVisible = visibility == View.VISIBLE;
1150 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001151 // The following code used to be in onResume, but it turns out onResume is called when
1152 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1153 // is a more appropriate event to handle
1154 if (mVisible) {
1155 mAppsCustomizeTabHost.onWindowVisible();
1156 if (!mWorkspaceLoading) {
1157 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001158 // We want to let Launcher draw itself at least once before we force it to build
1159 // layers on all the workspace pages, so that transitioning to Launcher from other
1160 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1161 // a true draw so we wait until the second preDraw call to be safe
1162 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001163 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001164 // We delay the layer building a bit in order to give
1165 // other message processing a time to run. In particular
1166 // this avoids a delay in hiding the IME if it was
1167 // currently shown, because doing that may involve
1168 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001169 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001170 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001171 return true;
1172 }
1173 });
1174 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001175 // When Launcher comes back to foreground, a different Activity might be responsible for
1176 // the app market intent, so refresh the icon
1177 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001178 clearTypedText();
1179 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001180 }
1181
1182 private void sendAdvanceMessage(long delay) {
1183 mHandler.removeMessages(ADVANCE_MSG);
1184 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1185 mHandler.sendMessageDelayed(msg, delay);
1186 mAutoAdvanceSentTime = System.currentTimeMillis();
1187 }
1188
1189 private void updateRunning() {
1190 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1191 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1192 mAutoAdvanceRunning = autoAdvanceRunning;
1193 if (autoAdvanceRunning) {
1194 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1195 sendAdvanceMessage(delay);
1196 } else {
1197 if (!mWidgetsToAdvance.isEmpty()) {
1198 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1199 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1200 }
1201 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001202 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001203 }
1204 }
1205 }
1206
1207 private final Handler mHandler = new Handler() {
1208 @Override
1209 public void handleMessage(Message msg) {
1210 if (msg.what == ADVANCE_MSG) {
1211 int i = 0;
1212 for (View key: mWidgetsToAdvance.keySet()) {
1213 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1214 final int delay = mAdvanceStagger * i;
1215 if (v instanceof Advanceable) {
1216 postDelayed(new Runnable() {
1217 public void run() {
1218 ((Advanceable) v).advance();
1219 }
1220 }, delay);
1221 }
1222 i++;
1223 }
1224 sendAdvanceMessage(mAdvanceInterval);
1225 }
1226 }
1227 };
1228
1229 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001230 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1232 if (v instanceof Advanceable) {
1233 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001234 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001235 updateRunning();
1236 }
1237 }
1238
1239 void removeWidgetToAutoAdvance(View hostView) {
1240 if (mWidgetsToAdvance.containsKey(hostView)) {
1241 mWidgetsToAdvance.remove(hostView);
1242 updateRunning();
1243 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 }
1245
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248 launcherInfo.hostView = null;
1249 }
1250
Winson Chung93eef082012-03-23 15:59:27 -07001251 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1252 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1253 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001254 }
1255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 public LauncherAppWidgetHost getAppWidgetHost() {
1257 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
Romain Guycbb89e42009-06-08 15:52:54 -07001259
Winson Chunga9abd0e2010-10-27 17:18:37 -07001260 public LauncherModel getModel() {
1261 return mModel;
1262 }
1263
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001264 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001265 getWindow().closeAllPanels();
1266
Winson Chung87acb482011-08-23 17:28:05 -07001267 /**
1268 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001269 try {
1270 dismissDialog(DIALOG_CREATE_SHORTCUT);
1271 // Unlock the workspace if the dialog was showing
1272 } catch (Exception e) {
1273 // An exception is thrown if the dialog is not visible, which is fine
1274 }
1275
1276 try {
1277 dismissDialog(DIALOG_RENAME_FOLDER);
1278 // Unlock the workspace if the dialog was showing
1279 } catch (Exception e) {
1280 // An exception is thrown if the dialog is not visible, which is fine
1281 }
Winson Chung87acb482011-08-23 17:28:05 -07001282 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001283
1284 // Whatever we were doing is hereby canceled.
1285 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 @Override
1289 protected void onNewIntent(Intent intent) {
1290 super.onNewIntent(intent);
1291
1292 // Close the menu
1293 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001294 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001295 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001296
Joe Onorato14f122b2009-11-19 14:06:36 -08001297 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1298 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001299
Adam Cohenac56cff2011-09-28 20:45:37 -07001300 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001301 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001302 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001303 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1304 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001305 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001306 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001307
1308 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001309 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001310 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001311
Joe Onorato3a8820b2009-11-10 15:06:42 -08001312 final View v = getWindow().peekDecorView();
1313 if (v != null && v.getWindowToken() != null) {
1314 InputMethodManager imm = (InputMethodManager)getSystemService(
1315 INPUT_METHOD_SERVICE);
1316 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001318
Michael Jurka35aa14d2011-07-07 17:01:08 -07001319 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001320 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001321 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 }
1324 }
1325
1326 @Override
1327 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1328 // Do not call super here
1329 mSavedInstanceState = savedInstanceState;
1330 }
1331
1332 @Override
1333 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001334 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001335 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336
Michael Jurka883f55b2010-10-21 15:47:14 -07001337 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001338 // We close any open folder since it will not be re-opened, and we need to make sure
1339 // this state is reflected.
1340 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1343 mWaitingForResult) {
1344 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1345 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1346 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1347 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349
1350 if (mFolderInfo != null && mWaitingForResult) {
1351 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1352 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1353 }
Michael Jurka946ad472010-07-09 18:05:18 -07001354
Winson Chung785d2eb2011-04-14 16:08:02 -07001355 // Save the current AppsCustomize tab
1356 if (mAppsCustomizeTabHost != null) {
1357 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1358 if (currentTabTag != null) {
1359 outState.putString("apps_customize_currentTab", currentTabTag);
1360 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001361 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1362 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001363 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
1365
1366 @Override
1367 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001369
Winson Chunge7a03942011-08-05 15:05:12 -07001370 // Remove all pending runnables
1371 mHandler.removeMessages(ADVANCE_MSG);
1372 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001373 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001374
Winson Chungcd2b0142011-06-08 16:02:26 -07001375 // Stop callbacks from LauncherModel
1376 LauncherApplication app = ((LauncherApplication) getApplication());
1377 mModel.stopLoader();
1378 app.setLauncher(null);
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001381 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001383 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001385 mAppWidgetHost = null;
1386
1387 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388
1389 TextKeyListener.getInstance().release();
1390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001393
1394 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001395 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001396
1397 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1398 mWorkspace.removeAllViews();
1399 mWorkspace = null;
1400 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001401
1402 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404
Adam Cohena9cf38f2011-05-02 15:36:58 -07001405 public DragController getDragController() {
1406 return mDragController;
1407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 @Override
1410 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001411 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 super.startActivityForResult(intent, requestCode);
1413 }
1414
Winson Chung70d72102011-08-12 11:24:35 -07001415 /**
1416 * Indicates that we want global search for this activity by setting the globalSearch
1417 * argument for {@link #startSearch} to true.
1418 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001420 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001422
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001423 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001424
Karl Rosaen138a0412009-04-23 19:00:21 -07001425 if (initialQuery == null) {
1426 // Use any text typed in the launcher as the initial query
1427 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 if (appSearchData == null) {
1430 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001431 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001433 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Karl Rosaen138a0412009-04-23 19:00:21 -07001435 final SearchManager searchManager =
1436 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001437 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001438 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
Winson Chung70d72102011-08-12 11:24:35 -07001441 @Override
1442 public boolean onCreateOptionsMenu(Menu menu) {
1443 if (isWorkspaceLocked()) {
1444 return false;
1445 }
1446
1447 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001448
1449 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1450 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1451 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001452 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1453 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1454 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001455 String helpUrl = getString(R.string.help_url);
1456 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001457 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1458 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1459
Winson Chung70d72102011-08-12 11:24:35 -07001460 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1461 .setIcon(android.R.drawable.ic_menu_gallery)
1462 .setAlphabeticShortcut('W');
1463 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1464 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001465 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001466 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001467 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1468 .setIcon(android.R.drawable.ic_menu_preferences)
1469 .setIntent(settings)
1470 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001471 if (!helpUrl.isEmpty()) {
1472 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1473 .setIcon(android.R.drawable.ic_menu_help)
1474 .setIntent(help)
1475 .setAlphabeticShortcut('H');
1476 }
Winson Chung70d72102011-08-12 11:24:35 -07001477 return true;
1478 }
1479
1480 @Override
1481 public boolean onPrepareOptionsMenu(Menu menu) {
1482 super.onPrepareOptionsMenu(menu);
1483
1484 if (mAppsCustomizeTabHost.isTransitioning()) {
1485 return false;
1486 }
1487 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1488 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1489
1490 return true;
1491 }
1492
1493 @Override
1494 public boolean onOptionsItemSelected(MenuItem item) {
1495 switch (item.getItemId()) {
1496 case MENU_WALLPAPER_SETTINGS:
1497 startWallpaper();
1498 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001499 }
1500
1501 return super.onOptionsItemSelected(item);
1502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001504 @Override
1505 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001506 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001507 // Use a custom animation for launching search
1508 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001509 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001510 }
1511
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 public boolean isWorkspaceLocked() {
1513 return mWorkspaceLoading || mWaitingForResult;
1514 }
1515
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 mPendingAddInfo.container = ItemInfo.NO_ID;
1518 mPendingAddInfo.screen = -1;
1519 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1520 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001521 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001522 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001523 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001524
Michael Jurkaaf442092010-06-10 17:01:57 -07001525 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001526 // TODO: catch bad widget exception when sent
1527 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001528 // TODO: Is this log message meaningful?
1529 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001530 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001531 }
1532
Adam Cohened66b2b2012-01-23 17:28:51 -08001533 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1534 final AppWidgetProviderInfo appWidget = info.info;
1535 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001536 if (appWidget.configure != null) {
1537 // Launch over to configure widget, if needed
1538 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1539 intent.setComponent(appWidget.configure);
1540 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001541 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001542 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001543 intent.putExtra(InstallWidgetReceiver.
1544 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001545
1546 final String mimeType = info.mimeType;
1547 final ClipData clipData = (ClipData) info.configurationData;
1548 final ClipDescription clipDesc = clipData.getDescription();
1549 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1550 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001551 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001552 final CharSequence stringData = item.getText();
1553 final Uri uriData = item.getUri();
1554 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001555 final String key = InstallWidgetReceiver.
1556 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001557 if (uriData != null) {
1558 intent.putExtra(key, uriData);
1559 } else if (intentData != null) {
1560 intent.putExtra(key, intentData);
1561 } else if (stringData != null) {
1562 intent.putExtra(key, stringData);
1563 }
1564 break;
1565 }
1566 }
1567 }
Winson Chung55cef262010-10-28 14:14:18 -07001568 }
Romain Guy8e633c52010-03-04 12:51:36 -08001569 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001570 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001572 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001573 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001574 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001575 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
1577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Adam Cohenfbba09b2011-07-18 21:43:05 -07001579 /**
1580 * Process a shortcut drop.
1581 *
1582 * @param componentName The name of the component
1583 * @param screen The screen where it should be added
1584 * @param cell The cell it should be added to, optional
1585 * @param position The location on the screen where it was dropped, optional
1586 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001587 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1588 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001589 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001590 mPendingAddInfo.container = container;
1591 mPendingAddInfo.screen = screen;
1592 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593
1594 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 mPendingAddInfo.cellX = cell[0];
1596 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001597 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001598
1599 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1600 createShortcutIntent.setComponent(componentName);
1601 processShortcut(createShortcutIntent);
1602 }
1603
Adam Cohenfbba09b2011-07-18 21:43:05 -07001604 /**
1605 * Process a widget drop.
1606 *
1607 * @param info The PendingAppWidgetInfo of the widget being added.
1608 * @param screen The screen where it should be added
1609 * @param cell The cell it should be added to, optional
1610 * @param position The location on the screen where it was dropped, optional
1611 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001612 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001613 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001614 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001615 mPendingAddInfo.container = info.container = container;
1616 mPendingAddInfo.screen = info.screen = screen;
1617 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001618 mPendingAddInfo.minSpanX = info.minSpanX;
1619 mPendingAddInfo.minSpanY = info.minSpanY;
1620
Adam Cohenfbba09b2011-07-18 21:43:05 -07001621 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001622 mPendingAddInfo.cellX = cell[0];
1623 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001624 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001625 if (span != null) {
1626 mPendingAddInfo.spanX = span[0];
1627 mPendingAddInfo.spanY = span[1];
1628 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001629
Adam Cohened66b2b2012-01-23 17:28:51 -08001630 AppWidgetHostView hostView = info.boundWidget;
1631 int appWidgetId;
1632 if (hostView != null) {
1633 appWidgetId = hostView.getAppWidgetId();
1634 } else {
1635 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1636 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1637 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001638 addAppWidgetImpl(appWidgetId, info);
1639 }
1640
Joe Onoratodeb98af2010-02-19 14:59:39 -08001641 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 // Handle case where user selected "Applications"
1643 String applicationName = getResources().getString(R.string.group_applications);
1644 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 if (applicationName != null && applicationName.equals(shortcutName)) {
1647 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1648 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1651 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001652 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001653 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001655 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
1658
Winson Chung24ab2f12010-09-16 14:10:47 -07001659 void processWallpaper(Intent intent) {
1660 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1661 }
1662
Winson Chung3d503fb2011-07-13 17:25:49 -07001663 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1664 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001665 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 folderInfo.title = getText(R.string.folder_name);
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001669 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1670 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001671 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672
1673 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001674 FolderIcon newFolder =
1675 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1676 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1677 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001678 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
Romain Guycbb89e42009-06-08 15:52:54 -07001680
Joe Onorato9c1289c2009-08-17 11:03:03 -04001681 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001682 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001683 }
1684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001686 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001688 Intent chooser = Intent.createChooser(pickWallpaper,
1689 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001690 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1691 // Removed in Eclair MR1
1692// WallpaperManager wm = (WallpaperManager)
1693// getSystemService(Context.WALLPAPER_SERVICE);
1694// WallpaperInfo wi = wm.getWallpaperInfo();
1695// if (wi != null && wi.getSettingsActivity() != null) {
1696// LabeledIntent li = new LabeledIntent(getPackageName(),
1697// R.string.configure_wallpaper, 0);
1698// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1699// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1700// }
Mike Clerona0618e42009-10-22 13:55:21 -07001701 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001704 /**
1705 * Registers various content observers. The current implementation registers
1706 * only a favorites observer to keep track of the favorites applications.
1707 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001708 private void registerContentObservers() {
1709 ContentResolver resolver = getContentResolver();
1710 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1711 true, mWidgetObserver);
1712 }
1713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 @Override
1715 public boolean dispatchKeyEvent(KeyEvent event) {
1716 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1717 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001719 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001720 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001721 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001722 dumpState();
1723 return true;
1724 }
1725 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001726 }
1727 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1728 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001729 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 return true;
1731 }
1732 }
1733
1734 return super.dispatchKeyEvent(event);
1735 }
1736
Joe Onorato88ec0992009-11-19 13:16:06 -08001737 @Override
1738 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001739 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001740 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001741 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001742 Folder openFolder = mWorkspace.getOpenFolder();
1743 if (openFolder.isEditingName()) {
1744 openFolder.dismissEditingName();
1745 } else {
1746 closeFolder();
1747 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001748 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001749 mWorkspace.exitWidgetResizeMode();
1750
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001751 // Back button is a no-op here, but give at least some feedback for the button press
1752 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001753 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001754 }
1755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001757 * Re-listen when widgets are reset.
1758 */
1759 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001760 if (mAppWidgetHost != null) {
1761 mAppWidgetHost.startListening();
1762 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001763 }
1764
1765 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1767 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001769 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001770 if (mModel != null) {
1771 mModel.unbindWorkspaceItems();
1772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 /**
1776 * Launches the intent referred by the clicked shortcut.
1777 *
1778 * @param v The view representing the clicked shortcut.
1779 */
1780 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001781 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1782 // view has detached (it's possible for this to happen if the view is removed mid touch).
1783 if (v.getWindowToken() == null) {
1784 return;
1785 }
1786
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001787 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001788 return;
1789 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001792 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001794 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001795 int[] pos = new int[2];
1796 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001797 intent.setSourceBounds(new Rect(pos[0], pos[1],
1798 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001799 boolean success = startActivitySafely(intent, tag);
1800
1801 if (success && v instanceof BubbleTextView) {
1802 mWaitingForResume = (BubbleTextView) v;
1803 mWaitingForResume.setStayPressed(true);
1804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001806 if (v instanceof FolderIcon) {
1807 FolderIcon fi = (FolderIcon) v;
1808 handleFolderClick(fi);
1809 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001810 } else if (v == mAllAppsButton) {
1811 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001812 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001813 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001814 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 }
1817 }
1818
Michael Jurka0e260592010-06-30 17:07:39 -07001819 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001820 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001821 // clicking anywhere on the workspace causes the customization drawer to slide down
1822 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001823 return false;
1824 }
1825
Michael Jurkaaf442092010-06-10 17:01:57 -07001826 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001827 * Event handler for the search button
1828 *
1829 * @param v The view that was clicked.
1830 */
1831 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001832 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1833
Amith Yamasania135ba82011-08-09 17:42:01 -07001834 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001835 }
1836
1837 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001838 * Event handler for the voice button
1839 *
1840 * @param v The view that was clicked.
1841 */
1842 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001843 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001844
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001845 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001846 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001847 startActivity(intent);
1848 }
1849
1850 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001851 * Event handler for the "grid" button that appears on the home screen, which
1852 * enters all apps mode.
1853 *
1854 * @param v The view that was clicked.
1855 */
1856 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001857 showAllApps(true);
1858 }
1859
1860 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001861 // Provide the same haptic feedback that the system offers for virtual keys.
1862 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001863 }
1864
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001865 public void onClickAppMarketButton(View v) {
1866 if (mAppMarketIntent != null) {
1867 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001868 } else {
1869 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001870 }
1871 }
1872
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001873 void startApplicationDetailsActivity(ComponentName componentName) {
1874 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001875 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1876 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001877 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001878 startActivity(intent);
1879 }
1880
Patrick Dubroy5539af72010-09-07 15:22:01 -07001881 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1882 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1883 // System applications cannot be installed. For now, show a toast explaining that.
1884 // We may give them the option of disabling apps this way.
1885 int messageId = R.string.uninstall_system_app_text;
1886 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1887 } else {
1888 String packageName = appInfo.componentName.getPackageName();
1889 String className = appInfo.componentName.getClassName();
1890 Intent intent = new Intent(
1891 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1893 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001894 startActivity(intent);
1895 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001896 }
1897
Michael Jurkaddd62e92011-02-16 17:49:14 -08001898 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1900 try {
1901 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001902 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 } catch (ActivityNotFoundException e) {
1904 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001905 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } catch (SecurityException e) {
1907 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001908 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001910 "or use the exported attribute for this activity. "
1911 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001913 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001915
Romain Guy8e633c52010-03-04 12:51:36 -08001916 void startActivityForResultSafely(Intent intent, int requestCode) {
1917 try {
1918 startActivityForResult(intent, requestCode);
1919 } catch (ActivityNotFoundException e) {
1920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1921 } catch (SecurityException e) {
1922 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1923 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1924 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1925 "or use the exported attribute for this activity.", e);
1926 }
1927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928
Adam Cohena9cf38f2011-05-02 15:36:58 -07001929 private void handleFolderClick(FolderIcon folderIcon) {
1930 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001931 Folder openFolder = mWorkspace.getFolderForTag(info);
1932
1933 // If the folder info reports that the associated folder is open, then verify that
1934 // it is actually opened. There have been a few instances where this gets out of sync.
1935 if (info.opened && openFolder == null) {
1936 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1937 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1938 info.opened = false;
1939 }
1940
Adam Cohena9cf38f2011-05-02 15:36:58 -07001941 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 // Close any open folder
1943 closeFolder();
1944 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001945 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 } else {
1947 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 int folderScreen;
1949 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001950 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 // .. and close it
1952 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001953 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 // Close any folder open on the current screen
1955 closeFolder();
1956 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001957 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959 }
1960 }
1961 }
1962
Adam Cohen2801caf2011-05-13 20:57:39 -07001963 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001964 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001965 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1966 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1967 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1968
Adam Cohenc51934b2011-07-26 21:07:43 -07001969 FolderInfo info = (FolderInfo) fi.getTag();
1970 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1971 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001972 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1973 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001974 }
1975
Adam Cohen2801caf2011-05-13 20:57:39 -07001976 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1977 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1978 oa.start();
1979 }
1980
1981 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001982 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001983 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1984 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1985 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1986
Adam Cohenc51934b2011-07-26 21:07:43 -07001987 FolderInfo info = (FolderInfo) fi.getTag();
1988 CellLayout cl = null;
1989 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1990 cl = (CellLayout) fi.getParent().getParent();
1991 }
1992
1993 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001994 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1995 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001996 oa.addListener(new AnimatorListenerAdapter() {
1997 @Override
1998 public void onAnimationEnd(Animator animation) {
1999 if (layout != null) {
2000 layout.clearFolderLeaveBehind();
2001 }
2002 }
2003 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002004 oa.start();
2005 }
2006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002008 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 * is animated relative to the specified View. If the View is null, no animation
2010 * is played.
2011 *
2012 * @param folderInfo The FolderInfo describing the folder to open.
2013 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002014 public void openFolder(FolderIcon folderIcon) {
2015 Folder folder = folderIcon.mFolder;
2016 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017
Adam Cohen2801caf2011-05-13 20:57:39 -07002018 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020
Adam Cohen4554ee12011-08-03 16:13:21 -07002021 // Just verify that the folder hasn't already been added to the DragLayer.
2022 // There was a one-off crash where the folder had a parent already.
2023 if (folder.getParent() == null) {
2024 mDragLayer.addView(folder);
2025 mDragController.addDropTarget((DropTarget) folder);
2026 } else {
2027 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2028 folder.getParent() + ").");
2029 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002030 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002031 }
2032
2033 public void closeFolder() {
2034 Folder folder = mWorkspace.getOpenFolder();
2035 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002036 if (folder.isEditingName()) {
2037 folder.dismissEditingName();
2038 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002039 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002040
2041 // Dismiss the folder cling
2042 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002043 }
2044 }
2045
2046 void closeFolder(Folder folder) {
2047 folder.getInfo().opened = false;
2048
2049 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2050 if (parent != null) {
2051 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2052 shrinkAndFadeInFolderIcon(fi);
2053 }
2054 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002058
Patrick Dubroye708c522011-03-01 16:03:43 -08002059 if (mState != State.WORKSPACE) {
2060 return false;
2061 }
2062
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 return false;
2065 }
2066
2067 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002068 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070
Michael Jurka0280c3b2010-09-17 15:00:07 -07002071 resetAddInfo();
2072 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002074 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 return true;
2076 }
2077
Winson Chung3d503fb2011-07-13 17:25:49 -07002078 // The hotseat touch handling does not go through Workspace, and we always allow long press
2079 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002080 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2082 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002083 if (itemUnderLongClick == null) {
2084 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002085 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2086 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002087 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002089 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
2093 }
2094 }
2095 return true;
2096 }
2097
Winson Chung3d503fb2011-07-13 17:25:49 -07002098 boolean isHotseatLayout(View layout) {
2099 return mHotseat != null && layout != null &&
2100 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2101 }
2102 Hotseat getHotseat() {
2103 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002104 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002105 SearchDropTargetBar getSearchBar() {
2106 return mSearchDropTargetBar;
2107 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002108
Winson Chung3d503fb2011-07-13 17:25:49 -07002109 /**
2110 * Returns the CellLayout of the specified container at the specified screen.
2111 */
2112 CellLayout getCellLayout(long container, int screen) {
2113 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2114 if (mHotseat != null) {
2115 return mHotseat.getLayout();
2116 } else {
2117 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002118 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002119 } else {
2120 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002121 }
2122 }
2123
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 Workspace getWorkspace() {
2125 return mWorkspace;
2126 }
2127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 @Override
2129 protected Dialog onCreateDialog(int id) {
2130 switch (id) {
2131 case DIALOG_CREATE_SHORTCUT:
2132 return new CreateShortcut().createDialog();
2133 case DIALOG_RENAME_FOLDER:
2134 return new RenameFolder().createDialog();
2135 }
2136
2137 return super.onCreateDialog(id);
2138 }
2139
2140 @Override
2141 protected void onPrepareDialog(int id, Dialog dialog) {
2142 switch (id) {
2143 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 break;
2145 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002146 if (mFolderInfo != null) {
2147 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2148 final CharSequence text = mFolderInfo.title;
2149 input.setText(text);
2150 input.setSelection(0, text.length());
2151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 break;
2153 }
2154 }
2155
2156 void showRenameDialog(FolderInfo info) {
2157 mFolderInfo = info;
2158 mWaitingForResult = true;
2159 showDialog(DIALOG_RENAME_FOLDER);
2160 }
2161
Adam Cohenfbba09b2011-07-18 21:43:05 -07002162 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002164 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2165 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 mWaitingForResult = true;
2167 showDialog(DIALOG_CREATE_SHORTCUT);
2168 }
2169
2170 private class RenameFolder {
2171 private EditText mInput;
2172
2173 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2175 mInput = (EditText) layout.findViewById(R.id.folder_name);
2176
2177 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2178 builder.setIcon(0);
2179 builder.setTitle(getString(R.string.rename_folder_title));
2180 builder.setCancelable(true);
2181 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2182 public void onCancel(DialogInterface dialog) {
2183 cleanup();
2184 }
2185 });
2186 builder.setNegativeButton(getString(R.string.cancel_action),
2187 new Dialog.OnClickListener() {
2188 public void onClick(DialogInterface dialog, int which) {
2189 cleanup();
2190 }
2191 }
2192 );
2193 builder.setPositiveButton(getString(R.string.rename_action),
2194 new Dialog.OnClickListener() {
2195 public void onClick(DialogInterface dialog, int which) {
2196 changeFolderName();
2197 }
2198 }
2199 );
2200 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002201
2202 final AlertDialog dialog = builder.create();
2203 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2204 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002205 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002206 mInput.requestFocus();
2207 InputMethodManager inputManager = (InputMethodManager)
2208 getSystemService(Context.INPUT_METHOD_SERVICE);
2209 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002210 }
2211 });
2212
2213 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215
2216 private void changeFolderName() {
2217 final String name = mInput.getText().toString();
2218 if (!TextUtils.isEmpty(name)) {
2219 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002220 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 mFolderInfo.title = name;
2222 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2223
Joe Onorato9c1289c2009-08-17 11:03:03 -04002224 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002225 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002226 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 } else {
2228 final FolderIcon folderIcon = (FolderIcon)
2229 mWorkspace.getViewForTag(mFolderInfo);
2230 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002231 // TODO: At some point we'll probably want some version of setting
2232 // the text for a folder icon.
2233 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 getWorkspace().requestLayout();
2235 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002236 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002238 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 }
2240 }
2241 }
2242 cleanup();
2243 }
2244
2245 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 dismissDialog(DIALOG_RENAME_FOLDER);
2247 mWaitingForResult = false;
2248 mFolderInfo = null;
2249 }
2250 }
2251
Daniel Sandler843e8602010-06-07 14:59:01 -04002252 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2253 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002254 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002255 }
2256
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002257 public boolean isAllAppsButtonRank(int rank) {
2258 return mHotseat.isAllAppsButtonRank(rank);
2259 }
2260
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002261 // AllAppsView.Watcher
2262 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002263 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002264 mWorkspace.setVisibility(View.GONE);
2265 }
2266 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002267
2268 /**
2269 * Helper method for the cameraZoomIn/cameraZoomOut animations
2270 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002271 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 * @param scaleFactor The scale factor used for the zoom
2273 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002274 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002275 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002276 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002277 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002278
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002279 void updateWallpaperVisibility(boolean visible) {
2280 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2281 int curflags = getWindow().getAttributes().flags
2282 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2283 if (wpflags != curflags) {
2284 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2285 }
2286 }
2287
Michael Jurkabed61d22012-02-14 22:51:29 -08002288 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2289 if (v instanceof LauncherTransitionable) {
2290 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2291 }
Winson Chung70442722012-02-10 15:43:22 -08002292
2293 // Update the workspace transition step as well
2294 dispatchOnLauncherTransitionStep(v, 0f);
2295 }
2296
2297 private void dispatchOnLauncherTransitionStep(View v, float t) {
2298 if (v instanceof LauncherTransitionable) {
2299 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2300 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002301 }
2302
2303 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2304 if (v instanceof LauncherTransitionable) {
2305 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2306 }
Winson Chung70442722012-02-10 15:43:22 -08002307
2308 // Update the workspace transition step as well
2309 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002310 }
2311
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002312 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002313 * Things to test when changing the following seven functions.
2314 * - Home from workspace
2315 * - from center screen
2316 * - from other screens
2317 * - Home from all apps
2318 * - from center screen
2319 * - from other screens
2320 * - Back from all apps
2321 * - from center screen
2322 * - from other screens
2323 * - Launch app from workspace and quit
2324 * - with back
2325 * - with home
2326 * - Launch app from all apps and quit
2327 * - with back
2328 * - with home
2329 * - Go to a screen that's not the default, then all
2330 * apps, and launch and app, and go back
2331 * - with back
2332 * -with home
2333 * - On workspace, long press power and go back
2334 * - with back
2335 * - with home
2336 * - On all apps, long press power and go back
2337 * - with back
2338 * - with home
2339 * - On workspace, power off
2340 * - On all apps, power off
2341 * - Launch an app and turn off the screen while in that app
2342 * - Go back with home key
2343 * - Go back with back key TODO: make this not go to workspace
2344 * - From all apps
2345 * - From workspace
2346 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2347 * - From all apps
2348 * - From the center workspace
2349 * - From another workspace
2350 */
2351
2352 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002353 * Zoom the camera out from the workspace to reveal 'toView'.
2354 * Assumes that the view to show is anchored at either the very top or very bottom
2355 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002356 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002357 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002358 if (mStateAnimation != null) {
2359 mStateAnimation.cancel();
2360 mStateAnimation = null;
2361 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002362 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002363 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002364
Winson Chungf0ea4d32011-06-06 14:27:16 -07002365 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2366 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2367 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002368 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002370 final int startDelay =
2371 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002372
Michael Jurkab3e22d92011-10-31 15:58:33 -07002373 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002374
Michael Jurkad74c9842011-07-10 12:44:21 -07002375 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002376 Animator workspaceAnim =
2377 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002378
2379 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002380 toView.setScaleX(scale);
2381 toView.setScaleY(scale);
2382 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2383 scaleAnim.
2384 scaleX(1f).scaleY(1f).
2385 setDuration(duration).
2386 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002387
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002389 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002390 final ObjectAnimator alphaAnim = ObjectAnimator
2391 .ofFloat(toView, "alpha", 0f, 1f)
2392 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002393 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002394 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2395 @Override
2396 public void onAnimationUpdate(ValueAnimator animation) {
2397 float t = (Float) animation.getAnimatedValue();
2398 dispatchOnLauncherTransitionStep(fromView, t);
2399 dispatchOnLauncherTransitionStep(toView, t);
2400 }
2401 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002402
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002403 // toView should appear right at the end of the workspace shrink
2404 // animation
2405 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002406 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002407 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002408
2409 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002410 boolean animationCancelled = false;
2411
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002412 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002413 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002414 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 // Prepare the position
2416 toView.setTranslationX(0.0f);
2417 toView.setTranslationY(0.0f);
2418 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002419 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002420 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002421 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002422 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002423 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2424 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002425
2426 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2427 // Hide the workspace scrollbar
2428 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002429 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002430 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002431 if (!animationCancelled) {
2432 updateWallpaperVisibility(false);
2433 }
2434 }
2435
Adam Cohencff6af82011-09-13 14:51:53 -07002436 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002437 public void onAnimationCancel(Animator animation) {
2438 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002439 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002440 });
2441
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002442 if (workspaceAnim != null) {
2443 mStateAnimation.play(workspaceAnim);
2444 }
Michael Jurka1899a362011-11-03 13:50:45 -07002445
2446 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002447 final ViewTreeObserver observer;
2448
Michael Jurkabed61d22012-02-14 22:51:29 -08002449 dispatchOnLauncherTransitionStart(fromView, animated, false);
2450 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002451
2452 // If any of the objects being animated haven't been measured/laid out
2453 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002454 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002455 (mWorkspace.getMeasuredWidth() == 0) ||
2456 (toView.getMeasuredWidth() == 0)) {
2457 observer = mWorkspace.getViewTreeObserver();
2458 delayAnim = true;
2459 } else {
2460 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002461 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002462
2463 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002464 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002465 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2466 public void onGlobalLayout() {
2467 mWorkspace.post(new Runnable() {
2468 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002469 // Check that mStateAnimation hasn't changed while
2470 // we waited for a layout pass
2471 if (mStateAnimation == stateAnimation) {
2472 // Need to update pivots for zoom if layout changed
2473 setPivotsForZoom(toView, scale);
2474 mStateAnimation.start();
2475 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002476 }
2477 });
2478 observer.removeGlobalOnLayoutListener(this);
2479 }
2480 };
2481 observer.addOnGlobalLayoutListener(delayedStart);
2482 } else {
2483 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002484 mStateAnimation.start();
2485 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 } else {
2487 toView.setTranslationX(0.0f);
2488 toView.setTranslationY(0.0f);
2489 toView.setScaleX(1.0f);
2490 toView.setScaleY(1.0f);
2491 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002492 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002493
Michael Jurkabed61d22012-02-14 22:51:29 -08002494 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2495 // Hide the workspace scrollbar
2496 mWorkspace.hideScrollingIndicator(true);
2497 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002498 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002499 dispatchOnLauncherTransitionStart(fromView, animated, false);
2500 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2501 dispatchOnLauncherTransitionStart(toView, animated, false);
2502 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002503 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002505 }
2506
2507 /**
2508 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002509 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002511 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002512 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2513 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002514
Michael Jurkab3e22d92011-10-31 15:58:33 -07002515 if (mStateAnimation != null) {
2516 mStateAnimation.cancel();
2517 mStateAnimation = null;
2518 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002519 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002520
Winson Chungf0ea4d32011-06-06 14:27:16 -07002521 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002522 final int fadeOutDuration =
2523 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002524 final float scaleFactor = (float)
2525 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2526 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002527 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002528 Animator workspaceAnim = null;
2529
2530 if (toState == State.WORKSPACE) {
2531 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2532 workspaceAnim = mWorkspace.getChangeStateAnimation(
2533 Workspace.State.NORMAL, animated, stagger);
2534 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2535 workspaceAnim = mWorkspace.getChangeStateAnimation(
2536 Workspace.State.SPRING_LOADED, animated);
2537 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002538
Michael Jurkab3e22d92011-10-31 15:58:33 -07002539 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002540 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002541 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002542 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002543 final float oldScaleX = fromView.getScaleX();
2544 final float oldScaleY = fromView.getScaleY();
2545
Michael Jurka159b4cc2012-01-17 03:00:35 -08002546 final LauncherViewPropertyAnimator scaleAnim =
2547 new LauncherViewPropertyAnimator(fromView);
2548 scaleAnim.
2549 scaleX(scaleFactor).scaleY(scaleFactor).
2550 setDuration(duration).
2551 setInterpolator(new Workspace.ZoomInInterpolator());
2552
2553 final ObjectAnimator alphaAnim = ObjectAnimator
2554 .ofFloat(fromView, "alpha", 1f, 0f)
2555 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002556 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002557 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2558 @Override
2559 public void onAnimationUpdate(ValueAnimator animation) {
2560 float t = 1f - (Float) animation.getAnimatedValue();
2561 dispatchOnLauncherTransitionStep(fromView, t);
2562 dispatchOnLauncherTransitionStep(toView, t);
2563 }
2564 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002565
Michael Jurkabed61d22012-02-14 22:51:29 -08002566 mStateAnimation = new AnimatorSet();
2567
2568 dispatchOnLauncherTransitionStart(fromView, animated, true);
2569 dispatchOnLauncherTransitionStart(toView, animated, true);
2570
2571 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002572 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002573 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002574 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002576 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2577 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002578 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002579 if (onCompleteRunnable != null) {
2580 onCompleteRunnable.run();
2581 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 }
2583 });
2584
Winson Chungf0ea4d32011-06-06 14:27:16 -07002585 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002586 if (workspaceAnim != null) {
2587 mStateAnimation.play(workspaceAnim);
2588 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002589 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 } else {
2591 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002592 dispatchOnLauncherTransitionStart(fromView, animated, true);
2593 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2594 dispatchOnLauncherTransitionStart(toView, animated, true);
2595 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002596 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002597 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002598 }
2599
Michael Jurkae326f182011-11-21 14:05:46 -08002600 @Override
2601 public void onTrimMemory(int level) {
2602 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002603 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002604 mAppsCustomizeTabHost.onTrimMemory();
2605 }
2606 }
2607
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002608 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002609 showWorkspace(animated, null);
2610 }
2611
2612 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002613 if (mState != State.WORKSPACE) {
2614 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002615 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616
2617 // Show the search bar and hotseat
2618 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002619 // We only need to animate in the dock divider if we're going from spring loaded mode
2620 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002621
2622 // Set focus to the AppsCustomize button
2623 if (mAllAppsButton != null) {
2624 mAllAppsButton.requestFocus();
2625 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002626 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002627
Michael Jurkab737ee62011-11-15 15:57:22 -08002628 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002629
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002630 // Change the state *after* we've called all the transition code
2631 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002632
Winson Chung5fb63472011-02-02 17:03:37 -08002633 // Resume the auto-advance of widgets
2634 mUserPresent = true;
2635 updateRunning();
2636
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002637 // send an accessibility event to announce the context change
2638 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002639 }
2640
Michael Jurkab3e22d92011-10-31 15:58:33 -07002641 void showAllApps(boolean animated) {
2642 if (mState != State.WORKSPACE) return;
2643
2644 showAppsCustomizeHelper(animated, false);
2645 mAppsCustomizeTabHost.requestFocus();
2646
2647 // Hide the search bar and hotseat
2648 mSearchDropTargetBar.hideSearchBar(animated);
2649
2650 // Change the state *after* we've called all the transition code
2651 mState = State.APPS_CUSTOMIZE;
2652
2653 // Pause the auto-advance of widgets until we are out of AllApps
2654 mUserPresent = false;
2655 updateRunning();
2656 closeFolder();
2657
2658 // Send an accessibility event to announce the context change
2659 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2660 }
2661
Adam Cohen7777d962011-08-18 18:58:38 -07002662 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002663 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002664 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002665 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002666 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002667 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002668 }
Adam Cohen7777d962011-08-18 18:58:38 -07002669
Adam Cohened66b2b2012-01-23 17:28:51 -08002670 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2671 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002672 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2673
Winson Chunge7a03942011-08-05 15:05:12 -07002674 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002675 @Override
2676 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002677 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002678 // Before we show workspace, hide all apps again because
2679 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2680 // clean up our state transition functions
2681 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002682 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002683 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002684 } else {
2685 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002686 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002687 }
2688 }, (extendedDelay ?
2689 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2690 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2691 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002692
Michael Jurkad3ef3062010-11-23 16:23:58 -08002693 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002694 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002695 final boolean animated = true;
2696 final boolean springLoaded = true;
2697 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002698 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002699 }
2700 // Otherwise, we are not in spring loaded mode, so don't do anything.
2701 }
2702
Michael Jurkab737ee62011-11-15 15:57:22 -08002703 void hideDockDivider() {
2704 if (mQsbDivider != null && mDockDivider != null) {
2705 mQsbDivider.setVisibility(View.INVISIBLE);
2706 mDockDivider.setVisibility(View.INVISIBLE);
2707 }
2708 }
2709
2710 void showDockDivider(boolean animated) {
2711 if (mQsbDivider != null && mDockDivider != null) {
2712 mQsbDivider.setVisibility(View.VISIBLE);
2713 mDockDivider.setVisibility(View.VISIBLE);
2714 if (mDividerAnimator != null) {
2715 mDividerAnimator.cancel();
2716 mQsbDivider.setAlpha(1f);
2717 mDockDivider.setAlpha(1f);
2718 mDividerAnimator = null;
2719 }
2720 if (animated) {
2721 mDividerAnimator = new AnimatorSet();
2722 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2723 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2724 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2725 mDividerAnimator.start();
2726 }
2727 }
2728 }
2729
Michael Jurkab3e22d92011-10-31 15:58:33 -07002730 void lockAllApps() {
2731 // TODO
2732 }
2733
2734 void unlockAllApps() {
2735 // TODO
2736 }
2737
Adam Cohenfc53cd22011-07-20 15:45:11 -07002738 public boolean isAllAppsCustomizeOpen() {
2739 return mState == State.APPS_CUSTOMIZE;
2740 }
2741
Winson Chungf0ea4d32011-06-06 14:27:16 -07002742 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002743 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002744 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002745 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002746 if (!LauncherApplication.isScreenLarge()) {
2747 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002748 if (mHotseat.getAlpha() != 1f) {
2749 int duration = mSearchDropTargetBar.getTransitionInDuration();
2750 mHotseat.animate().alpha(1f).setDuration(duration);
2751 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002752 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002753 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002754 }
2755 }
2756 }
2757
2758 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002759 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002760 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002761 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002762 if (!LauncherApplication.isScreenLarge()) {
2763 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002764 if (mHotseat.getAlpha() != 0f) {
2765 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2766 mHotseat.animate().alpha(0f).setDuration(duration);
2767 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002768 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002769 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002770 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002771 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002772 }
2773
Patrick Dubroy5f445422011-02-18 14:35:21 -08002774 /**
2775 * Add an item from all apps or customize onto the given workspace screen.
2776 * If layout is null, add to the current screen.
2777 */
2778 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002779 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002780 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002781 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002782 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002783
Winson Chungdff8ebb2011-09-08 17:25:31 -07002784 /** Maps the current orientation to an index for referencing orientation correct global icons */
2785 private int getCurrentOrientationIndexForGlobalIcons() {
2786 // default - 0, landscape - 1
2787 switch (getResources().getConfiguration().orientation) {
2788 case Configuration.ORIENTATION_LANDSCAPE:
2789 return 1;
2790 default:
2791 return 0;
2792 }
2793 }
2794
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002795 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002796 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002797 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002798 // Look for the toolbar icon specified in the activity meta-data
2799 Bundle metaData = packageManager.getActivityInfo(
2800 activityName, PackageManager.GET_META_DATA).metaData;
2801 if (metaData != null) {
2802 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2803 if (iconResId != 0) {
2804 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002805 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002806 }
2807 }
2808 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002809 // This can happen if the activity defines an invalid drawable
2810 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2811 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002812 } catch (Resources.NotFoundException nfe) {
2813 // This can happen if the activity defines an invalid drawable
2814 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2815 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002816 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002817 return null;
2818 }
2819
2820 // if successful in getting icon, return it; otherwise, set button to use default drawable
2821 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2822 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002823 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002824 Resources r = getResources();
2825 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2826 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002827
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002828 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002829 // If we were unable to find the icon via the meta-data, use a generic one
2830 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002831 toolbarIcon = r.getDrawable(fallbackDrawableId);
2832 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002833 if (button != null) {
2834 button.setCompoundDrawables(toolbarIcon, null, null, null);
2835 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002836 return null;
2837 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002838 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002839 if (button != null) {
2840 button.setCompoundDrawables(toolbarIcon, null, null, null);
2841 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002842 return toolbarIcon.getConstantState();
2843 }
2844 }
2845
2846 // if successful in getting icon, return it; otherwise, set button to use default drawable
2847 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2848 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2849 ImageView button = (ImageView) findViewById(buttonId);
2850 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2851
Michael Jurka19e0fc52011-07-22 18:00:21 -07002852 if (button != null) {
2853 // If we were unable to find the icon via the meta-data, use a
2854 // generic one
2855 if (toolbarIcon == null) {
2856 button.setImageResource(fallbackDrawableId);
2857 } else {
2858 button.setImageDrawable(toolbarIcon);
2859 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002860 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002861
2862 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2863
Michael Jurka0423dcf2010-10-05 14:56:18 -07002864 }
2865
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002866 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2867 TextView button = (TextView) findViewById(buttonId);
2868 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2869 }
2870
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002871 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002872 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002873 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002874 }
2875
Winson Chungbb185bd2011-11-21 12:31:42 -08002876 private void invalidatePressedFocusedStates(View container, View button) {
2877 if (container instanceof HolographicLinearLayout) {
2878 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2879 layout.invalidatePressedFocusedStates();
2880 } else if (button instanceof HolographicImageView) {
2881 HolographicImageView view = (HolographicImageView) button;
2882 view.invalidatePressedFocusedStates();
2883 }
2884 }
2885
Winson Chungc51db6a2011-10-05 11:44:49 -07002886 private boolean updateGlobalSearchIcon() {
2887 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002888 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2889 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002890 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002891 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002892
Winson Chung1cad91e2011-05-25 17:41:01 -07002893 final SearchManager searchManager =
2894 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2895 ComponentName activityName = searchManager.getGlobalSearchActivity();
2896 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002897 int coi = getCurrentOrientationIndexForGlobalIcons();
2898 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002899 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002900 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002901 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2902 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002903 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002904 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002905 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002906 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002907 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002908 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2909 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2910 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002911 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002912 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002913 }
2914 }
2915
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002916 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002917 final View searchButtonContainer = findViewById(R.id.search_button_container);
2918 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002919 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002920 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002921 }
2922
Winson Chungc51db6a2011-10-05 11:44:49 -07002923 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002924 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002925 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002926 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002927
Winson Chungc51db6a2011-10-05 11:44:49 -07002928 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002929 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2930 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002931 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002932 int coi = getCurrentOrientationIndexForGlobalIcons();
2933 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002934 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002935 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002936 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002937 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002938 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002939 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002940 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002941 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002942 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002943 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002944 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002945 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002946 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002947
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002948 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002949 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2950 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002951 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002952 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002953 }
2954
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002955 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002956 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002957 */
2958 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002959 final View marketButton = findViewById(R.id.market_button);
2960 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2961 // Find the app market activity by resolving an intent.
2962 // (If multiple app markets are installed, it will return the ResolverActivity.)
2963 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002964 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002965 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002966 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002967 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002968 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002969 marketButton.setVisibility(View.VISIBLE);
2970 } else {
2971 // We should hide and disable the view so that we don't try and restore the visibility
2972 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2973 marketButton.setVisibility(View.GONE);
2974 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002975 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002976 }
2977
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002978 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002979 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002980 }
2981
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002982 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 * Displays the shortcut creation dialog and launches, if necessary, the
2984 * appropriate activity.
2985 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002986 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002987 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2988 DialogInterface.OnShowListener {
2989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002994
Winson Chung55b65502011-05-26 12:03:43 -07002995 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2996 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002997 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002998
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 AlertDialog dialog = builder.create();
3000 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003001 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003002 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 return dialog;
3005 }
3006
3007 public void onCancel(DialogInterface dialog) {
3008 mWaitingForResult = false;
3009 cleanup();
3010 }
3011
Romain Guycbb89e42009-06-08 15:52:54 -07003012 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07003013 mWaitingForResult = false;
3014 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003015 }
3016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003018 try {
3019 dismissDialog(DIALOG_CREATE_SHORTCUT);
3020 } catch (Exception e) {
3021 // An exception is thrown if the dialog is not visible, which is fine
3022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 }
3024
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003025 /**
3026 * Handle the action clicked in the "Add to home" dialog.
3027 */
3028 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003029 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003030
Winson Chung55b65502011-05-26 12:03:43 -07003031 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3032 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003033 case AddAdapter.ITEM_APPLICATION: {
3034 if (mAppsCustomizeTabHost != null) {
3035 mAppsCustomizeTabHost.selectAppsTab();
3036 }
3037 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003038 break;
3039 }
Winson Chung55b65502011-05-26 12:03:43 -07003040 case AddAdapter.ITEM_APPWIDGET: {
3041 if (mAppsCustomizeTabHost != null) {
3042 mAppsCustomizeTabHost.selectWidgetsTab();
3043 }
3044 showAllApps(true);
3045 break;
3046 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003047 case AddAdapter.ITEM_WALLPAPER: {
3048 startWallpaper();
3049 break;
3050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 }
3052 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003053
3054 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003055 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 }
3058
3059 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003060 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003061 */
3062 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3063 @Override
3064 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003065 closeSystemDialogs();
3066 }
3067 }
3068
3069 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003070 * Receives notifications whenever the appwidgets are reset.
3071 */
3072 private class AppWidgetResetObserver extends ContentObserver {
3073 public AppWidgetResetObserver() {
3074 super(new Handler());
3075 }
3076
3077 @Override
3078 public void onChange(boolean selfChange) {
3079 onAppWidgetReset();
3080 }
3081 }
3082
3083 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003084 * If the activity is currently paused, signal that we need to re-run the loader
3085 * in onResume.
3086 *
3087 * This needs to be called from incoming places where resources might have been loaded
3088 * while we are paused. That is becaues the Configuration might be wrong
3089 * when we're not running, and if it comes back to what it was when we
3090 * were paused, we are not restarted.
3091 *
3092 * Implementation of the method from LauncherModel.Callbacks.
3093 *
3094 * @return true if we are currently paused. The caller might be able to
3095 * skip some work in that case since we will come back again.
3096 */
3097 public boolean setLoadOnResume() {
3098 if (mPaused) {
3099 Log.i(TAG, "setLoadOnResume");
3100 mOnResumeNeedsLoad = true;
3101 return true;
3102 } else {
3103 return false;
3104 }
3105 }
3106
3107 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003111 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003112 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003113 } else {
3114 return SCREEN_COUNT / 2;
3115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003117
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 /**
3119 * Refreshes the shortcuts shown on the workspace.
3120 *
3121 * Implementation of the method from LauncherModel.Callbacks.
3122 */
3123 public void startBinding() {
3124 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003125
Winson Chungf0c6ae02012-03-21 16:10:31 -07003126 mNewShortcutAnimatePage = -1;
3127 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003128 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 int count = workspace.getChildCount();
3130 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003131 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003132 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3133 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003135 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003136 if (mHotseat != null) {
3137 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 }
3139 }
3140
3141 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 * Bind the items start-end from the list.
3143 *
3144 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003147 setLoadOnResume();
3148
Winson Chungf0c6ae02012-03-21 16:10:31 -07003149 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3150 Set<String> newApps = new HashSet<String>();
3151 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3152
3153 Workspace workspace = mWorkspace;
3154 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003156
3157 // Short circuit if we are loading dock items for a configuration which has no dock
3158 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3159 mHotseat == null) {
3160 continue;
3161 }
3162
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 switch (item.itemType) {
3164 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3165 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003166 ShortcutInfo info = (ShortcutInfo) item;
3167 String uri = info.intent.toUri(0).toString();
3168 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3170 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003171 if (newApps.contains(uri)) {
3172 newApps.remove(uri);
3173
3174 // Prepare the view to be animated up
3175 shortcut.setAlpha(0f);
3176 shortcut.setScaleX(0f);
3177 shortcut.setScaleY(0f);
3178 mNewShortcutAnimatePage = item.screen;
3179 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3180 mNewShortcutAnimateViews.add(shortcut);
3181 }
3182 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003184 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003186 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003187 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003188 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3189 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003193
Joe Onorato9c1289c2009-08-17 11:03:03 -04003194 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 }
3196
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 /**
3198 * Implementation of the method from LauncherModel.Callbacks.
3199 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003200 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003201 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003202 sFolders.clear();
3203 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205
3206 /**
3207 * Add the views for a widget to the workspace.
3208 *
3209 * Implementation of the method from LauncherModel.Callbacks.
3210 */
3211 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003212 setLoadOnResume();
3213
Daniel Sandler843e8602010-06-07 14:59:01 -04003214 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3215 if (DEBUG_WIDGETS) {
3216 Log.d(TAG, "bindAppWidget: " + item);
3217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003218 final Workspace workspace = mWorkspace;
3219
3220 final int appWidgetId = item.appWidgetId;
3221 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003222 if (DEBUG_WIDGETS) {
3223 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3224 }
3225
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3227
Joe Onorato9c1289c2009-08-17 11:03:03 -04003228 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3229 item.hostView.setTag(item);
3230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 item.cellY, item.spanX, item.spanY, false);
3233
Adam Cohended9f8d2010-11-03 13:25:16 -07003234 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3235
Joe Onorato9c1289c2009-08-17 11:03:03 -04003236 workspace.requestLayout();
3237
Daniel Sandler843e8602010-06-07 14:59:01 -04003238 if (DEBUG_WIDGETS) {
3239 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3240 + (SystemClock.uptimeMillis()-start) + "ms");
3241 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003242 }
3243
3244 /**
3245 * Callback saying that there aren't any more items to bind.
3246 *
3247 * Implementation of the method from LauncherModel.Callbacks.
3248 */
3249 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003250 setLoadOnResume();
3251
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252 if (mSavedState != null) {
3253 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003254 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003255 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 mSavedState = null;
3257 }
3258
3259 if (mSavedInstanceState != null) {
3260 super.onRestoreInstanceState(mSavedInstanceState);
3261 mSavedInstanceState = null;
3262 }
3263
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003264 // If we received the result of any pending adds while the loader was running (e.g. the
3265 // widget configuration forced an orientation change), process them now.
3266 for (int i = 0; i < sPendingAddList.size(); i++) {
3267 completeAdd(sPendingAddList.get(i));
3268 }
3269 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003270
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 // Update the market app icon as necessary (the other icons will be managed in response to
3272 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003273 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003274
Winson Chungf0c6ae02012-03-21 16:10:31 -07003275 // Animate up any icons as necessary
3276 if (mVisible || mWorkspaceLoading) {
3277 Runnable newAppsRunnable = new Runnable() {
3278 @Override
3279 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003280 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003281 }
3282 };
Winson Chunga2413752012-04-03 14:22:34 -07003283
3284 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3285 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3286 if (canRunNewAppsAnimation()) {
3287 // If the user has not interacted recently, then either snap to the new page to show
3288 // the new-apps animation or just run them if they are to appear on the current page
3289 if (willSnapPage) {
3290 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3291 } else {
3292 runNewAppsAnimation(false);
3293 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003294 } else {
Winson Chunga2413752012-04-03 14:22:34 -07003295 // If the user has interacted recently, then run the animations immediately if they
3296 // are on another page (or just normally if they are added to the current page)
3297 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003298 }
3299 }
3300
3301 mWorkspaceLoading = false;
3302 }
3303
Winson Chunga2413752012-04-03 14:22:34 -07003304 private boolean canRunNewAppsAnimation() {
3305 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3306 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3307 }
3308
Winson Chungf0c6ae02012-03-21 16:10:31 -07003309 /**
3310 * Runs a new animation that scales up icons that were added while Launcher was in the
3311 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003312 *
3313 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003314 */
Winson Chunga2413752012-04-03 14:22:34 -07003315 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003316 AnimatorSet anim = new AnimatorSet();
3317 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003318
3319 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003320 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3321 @Override
3322 public int compare(View a, View b) {
3323 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3324 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3325 int cellCountX = LauncherModel.getCellCountX();
3326 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3327 }
3328 });
Winson Chunga2413752012-04-03 14:22:34 -07003329
3330 // Animate each of the views in place (or show them immediately if requested)
3331 if (immediate) {
3332 for (View v : mNewShortcutAnimateViews) {
3333 v.setAlpha(1f);
3334 v.setScaleX(1f);
3335 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003336 }
Winson Chunga2413752012-04-03 14:22:34 -07003337 } else {
3338 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3339 View v = mNewShortcutAnimateViews.get(i);
3340 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3341 PropertyValuesHolder.ofFloat("alpha", 1f),
3342 PropertyValuesHolder.ofFloat("scaleX", 1f),
3343 PropertyValuesHolder.ofFloat("scaleY", 1f));
3344 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3345 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3346 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3347 bounceAnims.add(bounceAnim);
3348 }
3349 anim.playTogether(bounceAnims);
3350 anim.addListener(new AnimatorListenerAdapter() {
3351 @Override
3352 public void onAnimationEnd(Animator animation) {
3353 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3354 }
3355 });
3356 anim.start();
3357 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003358
3359 // Clean up
3360 mNewShortcutAnimatePage = -1;
3361 mNewShortcutAnimateViews.clear();
3362 new Thread("clearNewAppsThread") {
3363 public void run() {
3364 mSharedPrefs.edit()
3365 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3366 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3367 .commit();
3368 }
3369 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003370 }
3371
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003372 @Override
3373 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003374 boolean searchVisible = updateGlobalSearchIcon();
3375 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3376 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003377 }
3378
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003379 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 * Add the icons for all apps.
3381 *
3382 * Implementation of the method from LauncherModel.Callbacks.
3383 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003384 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3385 // Remove the progress bar entirely; we could also make it GONE
3386 // but better to remove it since we know it's not going to be used
3387 View progressBar = mAppsCustomizeTabHost.
3388 findViewById(R.id.apps_customize_progress_bar);
3389 if (progressBar != null) {
3390 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003391 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003392 // We just post the call to setApps so the user sees the progress bar
3393 // disappear-- otherwise, it just looks like the progress bar froze
3394 // which doesn't look great
3395 mAppsCustomizeTabHost.post(new Runnable() {
3396 public void run() {
3397 if (mAppsCustomizeContent != null) {
3398 mAppsCustomizeContent.setApps(apps);
3399 }
3400 }
3401 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 }
3403
3404 /**
3405 * A package was installed.
3406 *
3407 * Implementation of the method from LauncherModel.Callbacks.
3408 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003409 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003410 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003412
Winson Chung785d2eb2011-04-14 16:08:02 -07003413 if (mAppsCustomizeContent != null) {
3414 mAppsCustomizeContent.addApps(apps);
3415 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 }
3417
3418 /**
3419 * A package was updated.
3420 *
3421 * Implementation of the method from LauncherModel.Callbacks.
3422 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003423 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003424 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003426 if (mWorkspace != null) {
3427 mWorkspace.updateShortcuts(apps);
3428 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003429
Winson Chung785d2eb2011-04-14 16:08:02 -07003430 if (mAppsCustomizeContent != null) {
3431 mAppsCustomizeContent.updateApps(apps);
3432 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 }
3434
3435 /**
3436 * A package was uninstalled.
3437 *
3438 * Implementation of the method from LauncherModel.Callbacks.
3439 */
Joe Onorato36115782010-06-17 13:28:48 -04003440 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003442 if (permanent) {
3443 mWorkspace.removeItems(apps);
3444 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003445
Winson Chung785d2eb2011-04-14 16:08:02 -07003446 if (mAppsCustomizeContent != null) {
3447 mAppsCustomizeContent.removeApps(apps);
3448 }
Winson Chunga1820962011-10-03 16:31:06 -07003449
3450 // Notify the drag controller
3451 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 }
Joe Onoratobe386092009-11-17 17:32:16 -08003453
3454 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003455 * A number of packages were updated.
3456 */
3457 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003458 if (mAppsCustomizeContent != null) {
3459 mAppsCustomizeContent.onPackagesUpdated();
3460 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003461 }
3462
Winson Chung400438b2011-01-16 17:53:48 -08003463 private int mapConfigurationOriActivityInfoOri(int configOri) {
3464 final Display d = getWindowManager().getDefaultDisplay();
3465 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3466 switch (d.getRotation()) {
3467 case Surface.ROTATION_0:
3468 case Surface.ROTATION_180:
3469 // We are currently in the same basic orientation as the natural orientation
3470 naturalOri = configOri;
3471 break;
3472 case Surface.ROTATION_90:
3473 case Surface.ROTATION_270:
3474 // We are currently in the other basic orientation to the natural orientation
3475 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3476 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3477 break;
3478 }
3479
3480 int[] oriMap = {
3481 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3482 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3483 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3484 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3485 };
3486 // Since the map starts at portrait, we need to offset if this device's natural orientation
3487 // is landscape.
3488 int indexOffset = 0;
3489 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3490 indexOffset = 1;
3491 }
3492 return oriMap[(d.getRotation() + indexOffset) % 4];
3493 }
Adam Cohen446e9402011-09-15 18:21:21 -07003494
3495 public void lockScreenOrientationOnLargeUI() {
3496 if (LauncherApplication.isScreenLarge()) {
3497 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3498 .getConfiguration().orientation));
3499 }
Winson Chung400438b2011-01-16 17:53:48 -08003500 }
Adam Cohen446e9402011-09-15 18:21:21 -07003501 public void unlockScreenOrientationOnLargeUI() {
3502 if (LauncherApplication.isScreenLarge()) {
3503 mHandler.postDelayed(new Runnable() {
3504 public void run() {
3505 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3506 }
3507 }, mRestoreScreenOrientationDelay);
3508 }
Winson Chung400438b2011-01-16 17:53:48 -08003509 }
3510
Winson Chung82f55532011-08-09 14:14:23 -07003511 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003512 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003513 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003514 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003515
Winson Chung7d7541e2011-09-16 20:14:36 -07003516 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003517 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003518 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3519 Cling cling = (Cling) findViewById(clingId);
3520 if (cling != null) {
3521 cling.init(this, positionData);
3522 cling.setVisibility(View.VISIBLE);
3523 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3524 if (animate) {
3525 cling.buildLayer();
3526 cling.setAlpha(0f);
3527 cling.animate()
3528 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003529 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003530 .setDuration(SHOW_CLING_DURATION)
3531 .setStartDelay(delay)
3532 .start();
3533 } else {
3534 cling.setAlpha(1f);
3535 }
3536 }
3537 return cling;
3538 }
3539 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003540 if (cling != null) {
3541 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003542 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003543 anim.addListener(new AnimatorListenerAdapter() {
3544 public void onAnimationEnd(Animator animation) {
3545 cling.setVisibility(View.GONE);
3546 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003547 // We should update the shared preferences on a background thread
3548 new Thread("dismissClingThread") {
3549 public void run() {
3550 SharedPreferences.Editor editor = mSharedPrefs.edit();
3551 editor.putBoolean(flag, true);
3552 editor.commit();
3553 }
3554 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003555 };
3556 });
3557 anim.start();
3558 }
3559 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003560 private void removeCling(int id) {
3561 final View cling = findViewById(id);
3562 if (cling != null) {
3563 final ViewGroup parent = (ViewGroup) cling.getParent();
3564 parent.post(new Runnable() {
3565 @Override
3566 public void run() {
3567 parent.removeView(cling);
3568 }
3569 });
3570 }
3571 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003572 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003573 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003574 if (isClingsEnabled() &&
3575 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003576 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003577 } else {
3578 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003579 }
3580 }
3581 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003582 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003583 if (isClingsEnabled() &&
3584 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003585 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003586 } else {
3587 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003588 }
3589 }
3590 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003591 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003592 if (isClingsEnabled() &&
3593 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3594 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003595 } else {
3596 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003597 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003598 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003599 }
3600 public boolean isFolderClingVisible() {
3601 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003602 if (cling != null) {
3603 return cling.getVisibility() == View.VISIBLE;
3604 }
3605 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003606 }
Winson Chung82f55532011-08-09 14:14:23 -07003607 public void dismissWorkspaceCling(View v) {
3608 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003609 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003610 }
3611 public void dismissAllAppsCling(View v) {
3612 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003613 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3614 }
3615 public void dismissFolderCling(View v) {
3616 Cling cling = (Cling) findViewById(R.id.folder_cling);
3617 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003618 }
3619
Winson Chung80baf5a2010-08-09 16:03:15 -07003620 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003621 * Prints out out state for debugging.
3622 */
3623 public void dumpState() {
3624 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003625 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003626 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3627 Log.d(TAG, "mRestoring=" + mRestoring);
3628 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3629 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003630 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003631 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003632
Winson Chung785d2eb2011-04-14 16:08:02 -07003633 if (mAppsCustomizeContent != null) {
3634 mAppsCustomizeContent.dumpState();
3635 }
Joe Onoratobe386092009-11-17 17:32:16 -08003636 Log.d(TAG, "END launcher2 dump state");
3637 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003638
3639 @Override
3640 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3641 super.dump(prefix, fd, writer, args);
3642 writer.println(" ");
3643 writer.println("Debug logs: ");
3644 for (int i = 0; i < sDumpLogs.size(); i++) {
3645 writer.println(" " + sDumpLogs.get(i));
3646 }
3647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003648}
Michael Jurka2763be32011-02-24 11:19:57 -08003649
Michael Jurkaabded662011-03-04 12:06:57 -08003650interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003651 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003652 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003653 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003654 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003655}