blob: ee540f82cf26474033728eddab6fa0c0af325f7f [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040055import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070056import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Winson Chungf0c6ae02012-03-21 16:10:31 -070058import android.os.Debug;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Winson Chungf0c6ae02012-03-21 16:10:31 -070088import android.view.animation.BounceInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070093import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070094import android.widget.TextView;
95import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070096
Adam Cohendf2cc412011-04-27 16:56:57 -070097import com.android.common.Search;
98import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070099import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
141
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800142 static final int SCREEN_COUNT = 5;
143 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Joe Onorato7c312c12009-08-13 21:36:53 -0700145 static final int DIALOG_CREATE_SHORTCUT = 1;
146 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800149 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
157 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
167
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700168 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
169
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700171 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700172 private State mState = State.WORKSPACE;
173 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800174 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700175
Joe Onorato9c1289c2009-08-17 11:03:03 -0400176 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700177 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
178 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700179 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700180 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800183 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800185 private final BroadcastReceiver mCloseSystemDialogsReceiver
186 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800187 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private LayoutInflater mInflater;
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800192 private View mQsbDivider;
193 private View mDockDivider;
194 private DragLayer mDragLayer;
195 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Michael Jurkac9d95c52011-08-29 14:03:34 -0700200 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 private int[] mTmpAddItemCellCoordinates = new int[2];
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private FolderInfo mFolderInfo;
204
Winson Chung3d503fb2011-07-13 17:25:49 -0700205 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800206 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700207
Winson Chungc51db6a2011-10-05 11:44:49 -0700208 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700209 private AppsCustomizeTabHost mAppsCustomizeTabHost;
210 private AppsCustomizePagedView mAppsCustomizeContent;
211 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private Bundle mSavedState;
214
215 private SpannableStringBuilder mDefaultKeySsb = null;
216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 private boolean mWorkspaceLoading = true;
218
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800219 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private boolean mRestoring;
221 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700222 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223
224 private Bundle mSavedInstanceState;
225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800227 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800228 private boolean mUserPresent = true;
229 private boolean mVisible = false;
230 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700232 private static LocaleConfiguration sLocaleConfiguration = null;
233
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700234 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700235
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700236 private Intent mAppMarketIntent = null;
237
Adam Cohended9f8d2010-11-03 13:25:16 -0700238 // Related to the auto-advancing of widgets
239 private final int ADVANCE_MSG = 1;
240 private final int mAdvanceInterval = 20000;
241 private final int mAdvanceStagger = 250;
242 private long mAutoAdvanceSentTime;
243 private long mAutoAdvanceTimeLeft = -1;
244 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
245 new HashMap<View, AppWidgetProviderInfo>();
246
Winson Chung400438b2011-01-16 17:53:48 -0800247 // Determines how long to wait after a rotation before restoring the screen orientation to
248 // match the sensor state.
249 private final int mRestoreScreenOrientationDelay = 500;
250
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700252 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
253 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
254 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800255
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700256 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800257 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700258
Winson Chung46353de2012-02-16 14:05:10 -0800259 // We only want to get the SharedPreferences once since it does an FS stat each time we get
260 // it from the context.
261 private SharedPreferences mSharedPrefs;
262
Winson Chungf0c6ae02012-03-21 16:10:31 -0700263 // Holds the page that we need to animate to, and the icon views that we need to animate up
264 // when we scroll to that page on resume.
265 private int mNewShortcutAnimatePage = -1;
266 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700267
Michael Jurkaddd62e92011-02-16 17:49:14 -0800268 private BubbleTextView mWaitingForResume;
269
Michael Jurkac1f5d262011-09-30 19:32:27 -0700270 private Runnable mBuildLayersRunnable = new Runnable() {
271 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700272 if (mWorkspace != null) {
273 mWorkspace.buildPageHardwareLayers();
274 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700275 }
276 };
277
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800278 private static ArrayList<PendingAddArguments> sPendingAddList
279 = new ArrayList<PendingAddArguments>();
280
281 private static class PendingAddArguments {
282 int requestCode;
283 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700284 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800285 int screen;
286 int cellX;
287 int cellY;
288 }
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 @Override
291 protected void onCreate(Bundle savedInstanceState) {
292 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700294 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
295 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 mModel = app.setLauncher(this);
297 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400298 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700300
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700302 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
303 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200306 android.os.Debug.startMethodTracing(
307 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
310 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 setContentView(R.layout.launcher);
312 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700313 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800315 registerContentObservers();
316
Joe Onorato7c312c12009-08-13 21:36:53 -0700317 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700318
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 mSavedState = savedInstanceState;
320 restoreState(mSavedState);
321
Winson Chunga12a2502010-12-20 14:41:35 -0800322 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700323 if (mAppsCustomizeContent != null) {
324 mAppsCustomizeContent.onPackagesUpdated();
325 }
Winson Chunga12a2502010-12-20 14:41:35 -0800326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 if (PROFILE_STARTUP) {
328 android.os.Debug.stopMethodTracing();
329 }
330
331 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700332 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 }
334
Michael Jurkac57b7a82011-08-09 22:02:20 -0700335 if (!mModel.isAllAppsLoaded()) {
336 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
337 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 // For handling default keys
341 mDefaultKeySsb = new SpannableStringBuilder();
342 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800343
344 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
345 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800346
Winson Chungc51db6a2011-10-05 11:44:49 -0700347 boolean searchVisible = false;
348 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800349 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700350 int coi = getCurrentOrientationIndexForGlobalIcons();
351 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
352 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700353 updateAppMarketIcon();
354 searchVisible = updateGlobalSearchIcon();
355 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700356 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700357 if (sGlobalSearchIcon[coi] != null) {
358 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700359 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700360 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700361 if (sVoiceSearchIcon[coi] != null) {
362 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700363 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700364 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700365 if (sAppMarketIcon[coi] != null) {
366 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800367 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700368 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700369
Adam Cohen23a4d302011-12-01 17:26:44 -0800370 final String forceEnableRotation =
371 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
372
Adam Cohen446e9402011-09-15 18:21:21 -0700373 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800374 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700375 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
Romain Guycbb89e42009-06-08 15:52:54 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417
418 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
419 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700420 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 public void run() {
422 writeConfiguration(Launcher.this, localeConfiguration);
423 }
424 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700425 }
426 }
427
428 private static class LocaleConfiguration {
429 public String locale;
430 public int mcc = -1;
431 public int mnc = -1;
432 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700433
Romain Guy98d01652009-06-30 16:21:04 -0700434 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
435 DataInputStream in = null;
436 try {
437 in = new DataInputStream(context.openFileInput(PREFERENCES));
438 configuration.locale = in.readUTF();
439 configuration.mcc = in.readInt();
440 configuration.mnc = in.readInt();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 // Ignore
445 } finally {
446 if (in != null) {
447 try {
448 in.close();
449 } catch (IOException e) {
450 // Ignore
451 }
452 }
453 }
454 }
455
456 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
457 DataOutputStream out = null;
458 try {
459 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
460 out.writeUTF(configuration.locale);
461 out.writeInt(configuration.mcc);
462 out.writeInt(configuration.mnc);
463 out.flush();
464 } catch (FileNotFoundException e) {
465 // Ignore
466 } catch (IOException e) {
467 //noinspection ResultOfMethodCallIgnored
468 context.getFileStreamPath(PREFERENCES).delete();
469 } finally {
470 if (out != null) {
471 try {
472 out.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478 }
479
Adam Cohen716b51e2011-06-30 12:09:54 -0700480 public DragLayer getDragLayer() {
481 return mDragLayer;
482 }
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 static int getScreen() {
485 synchronized (sLock) {
486 return sScreen;
487 }
488 }
489
490 static void setScreen(int screen) {
491 synchronized (sLock) {
492 sScreen = screen;
493 }
494 }
495
Winson Chung557d6ed2011-07-08 15:34:52 -0700496 /**
497 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
498 * a configuration step, this allows the proper animations to run after other transitions.
499 */
500 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700501 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 switch (args.requestCode) {
503 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700504 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
505 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 break;
507 case REQUEST_PICK_SHORTCUT:
508 processShortcut(args.intent);
509 break;
510 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700511 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
512 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700513 result = true;
514 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515 case REQUEST_PICK_APPWIDGET:
516 addAppWidgetFromPick(args.intent);
517 break;
518 case REQUEST_CREATE_APPWIDGET:
519 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800520 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700521 result = true;
522 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523 case REQUEST_PICK_WALLPAPER:
524 // We just wanted the activity result here so we can clear mWaitingForResult
525 break;
526 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700527 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528 }
529
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800531 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700532 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800533 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
534 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700535 mWaitingForResult = false;
536
Adam Cohened66b2b2012-01-23 17:28:51 -0800537 // We have special handling for widgets
538 if (isWidgetDrop) {
539 int appWidgetId = data != null ?
540 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
541 completeTwoStageWidgetDrop(resultCode, appWidgetId);
542 return;
543 }
544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 // The pattern used here is that a user PICKs a specific application,
546 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700547
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
549 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700550 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800551 final PendingAddArguments args = new PendingAddArguments();
552 args.requestCode = requestCode;
553 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700554 args.container = mPendingAddInfo.container;
555 args.screen = mPendingAddInfo.screen;
556 args.cellX = mPendingAddInfo.cellX;
557 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800558 if (isWorkspaceLocked()) {
559 sPendingAddList.add(args);
560 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700561 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800564 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700565 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800566 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
567 null);
568 }
569
570 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
571 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
572 Runnable onCompleteRunnable = null;
573 int animationType = 0;
574
575 if (resultCode == RESULT_OK) {
576 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
577 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
578 mWidgetBeingConfigured.info);
579 mWidgetBeingConfigured.boundWidget = layout;
580 onCompleteRunnable = new Runnable() {
581 @Override
582 public void run() {
583 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
584 mPendingAddInfo.screen, layout, null);
585 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
586 null);
587 }
588 };
589 } else if (resultCode == RESULT_CANCELED) {
590 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
591 onCompleteRunnable = new Runnable() {
592 @Override
593 public void run() {
594 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
595 null);
596 }
597 };
598 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800599 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800600 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800601 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800602 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604
605 @Override
606 protected void onResume() {
607 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700608
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800609 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700610 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400611 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700612 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400613 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700614 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
Winson Chunge4e50662012-01-23 14:45:13 -0800616
617 // Reset the pressed state of icons that were locked in the press state while activities
618 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800619 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800620 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800621 mWaitingForResume.setStayPressed(false);
622 }
Winson Chunge4e50662012-01-23 14:45:13 -0800623 if (mAppsCustomizeContent != null) {
624 // Resets the previous all apps icon press state
625 mAppsCustomizeContent.resetDrawableState();
626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628
629 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700630 protected void onPause() {
631 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700632 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800633 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700634 }
Romain Guycbb89e42009-06-08 15:52:54 -0700635
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700636 @Override
637 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400638 // Flag the loader to stop early before switching
639 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700640 if (mAppsCustomizeContent != null) {
641 mAppsCustomizeContent.surrender();
642 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800643 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700644 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700645
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800646 // We can't hide the IME if it was forced open. So don't bother
647 /*
648 @Override
649 public void onWindowFocusChanged(boolean hasFocus) {
650 super.onWindowFocusChanged(hasFocus);
651
652 if (hasFocus) {
653 final InputMethodManager inputManager = (InputMethodManager)
654 getSystemService(Context.INPUT_METHOD_SERVICE);
655 WindowManager.LayoutParams lp = getWindow().getAttributes();
656 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
657 android.os.Handler()) {
658 protected void onReceiveResult(int resultCode, Bundle resultData) {
659 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
660 }
661 });
662 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
663 }
664 }
665 */
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 private boolean acceptFilter() {
668 final InputMethodManager inputManager = (InputMethodManager)
669 getSystemService(Context.INPUT_METHOD_SERVICE);
670 return !inputManager.isFullscreenMode();
671 }
672
673 @Override
674 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700675 final int uniChar = event.getUnicodeChar();
676 final boolean handled = super.onKeyDown(keyCode, event);
677 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
678 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
680 keyCode, event);
681 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700682 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700683 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700684 // showSearchDialog()
685 // If there are multiple keystrokes before the search dialog takes focus,
686 // onSearchRequested() will be called for every keystroke,
687 // but it is idempotent, so it's fine.
688 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690 }
691
Joe Onorato8a9625e2010-01-28 15:55:35 -0800692 // Eat the long press event so the keyboard doesn't come up.
693 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
694 return true;
695 }
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 return handled;
698 }
699
Karl Rosaen138a0412009-04-23 19:00:21 -0700700 private String getTypedText() {
701 return mDefaultKeySsb.toString();
702 }
703
704 private void clearTypedText() {
705 mDefaultKeySsb.clear();
706 mDefaultKeySsb.clearSpans();
707 Selection.setSelection(mDefaultKeySsb, 0);
708 }
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700711 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
712 * State
713 */
714 private static State intToState(int stateOrdinal) {
715 State state = State.WORKSPACE;
716 final State[] stateValues = State.values();
717 for (int i = 0; i < stateValues.length; i++) {
718 if (stateValues[i].ordinal() == stateOrdinal) {
719 state = stateValues[i];
720 break;
721 }
722 }
723 return state;
724 }
725
726 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 * Restores the previous state, if it exists.
728 *
729 * @param savedState The previous state.
730 */
731 private void restoreState(Bundle savedState) {
732 if (savedState == null) {
733 return;
734 }
735
Michael Jurka883f55b2010-10-21 15:47:14 -0700736 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700737 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800738 showAllApps(false);
739 }
740
Winson Chungf0c6ae02012-03-21 16:10:31 -0700741 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700743 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
747 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700748
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
750 mPendingAddInfo.container = pendingAddContainer;
751 mPendingAddInfo.screen = pendingAddScreen;
752 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
753 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 mRestoring = true;
755 }
756
757 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
758 if (renameFolder) {
759 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700760 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 mRestoring = true;
762 }
Winson Chunga12a2502010-12-20 14:41:35 -0800763
Winson Chung785d2eb2011-04-14 16:08:02 -0700764
765 // Restore the AppsCustomize tab
766 if (mAppsCustomizeTabHost != null) {
767 String curTab = savedState.getString("apps_customize_currentTab");
768 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700769 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700770 mAppsCustomizeContent.setContentType(
771 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
772 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700773 mAppsCustomizeContent.loadAssociatedPages(
774 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700775 }
776
Winson Chung5afbf7b2011-07-25 11:53:08 -0700777 int currentIndex = savedState.getInt("apps_customize_currentIndex");
778 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 }
781
782 /**
783 * Finds all the views we need and configure them properly.
784 */
785 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700786 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400787
Winson Chung4c98d922011-05-31 16:50:48 -0700788 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
789 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800790 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
791 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792
Winson Chung4c98d922011-05-31 16:50:48 -0700793 // Setup the drag layer
794 mDragLayer.setup(this, dragController);
795
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 // Setup the hotseat
797 mHotseat = (Hotseat) findViewById(R.id.hotseat);
798 if (mHotseat != null) {
799 mHotseat.setup(this);
800 }
801
Winson Chung4c98d922011-05-31 16:50:48 -0700802 // Setup the workspace
803 mWorkspace.setHapticFeedbackEnabled(false);
804 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700805 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700806 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700807
Winson Chungf0ea4d32011-06-06 14:27:16 -0700808 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700809 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700810
Winson Chungf0ea4d32011-06-06 14:27:16 -0700811 // Setup AppsCustomize
812 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
813 findViewById(R.id.apps_customize_pane);
814 mAppsCustomizeContent = (AppsCustomizePagedView)
815 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
816 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400817
Michael Jurka5130e402011-10-13 04:55:35 -0700818 // Get the all apps button
819 mAllAppsButton = findViewById(R.id.all_apps_button);
820 if (mAllAppsButton != null) {
821 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
822 @Override
823 public boolean onTouch(View v, MotionEvent event) {
824 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
825 onTouchDownAllAppsButton(v);
826 }
827 return false;
828 }
829 });
830 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700832 dragController.setDragScoller(mWorkspace);
833 dragController.setScrollView(mDragLayer);
834 dragController.setMoveTarget(mWorkspace);
835 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700836 if (mSearchDropTargetBar != null) {
837 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840
841 /**
842 * Creates a view representing a shortcut.
843 *
844 * @param info The data structure describing the shortcut.
845 *
846 * @return A View inflated from R.layout.application.
847 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800848 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700850 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 }
852
853 /**
854 * Creates a view representing a shortcut inflated from the specified resource.
855 *
856 * @param layoutResId The id of the XML layout used to create the shortcut.
857 * @param parent The group the shortcut belongs to.
858 * @param info The data structure describing the shortcut.
859 *
860 * @return A View inflated from layoutResId.
861 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800862 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800863 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
864 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 return favorite;
867 }
868
869 /**
870 * Add an application shortcut to the workspace.
871 *
872 * @param data The intent describing the application.
873 * @param cellInfo The position on screen where to create the shortcut.
874 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878
Adam Cohenfbba09b2011-07-18 21:43:05 -0700879 // First we check if we already know the exact location where we want to add this item.
880 if (cellX >= 0 && cellY >= 0) {
881 cellXY[0] = cellX;
882 cellXY[1] = cellY;
883 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700884 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700885 return;
886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800888 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800889
Romain Guy73b979d2009-06-09 12:57:21 -0700890 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800891 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800893 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700895 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800896 } else {
897 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
899 }
Romain Guycbb89e42009-06-08 15:52:54 -0700900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 /**
902 * Add a shortcut to the workspace.
903 *
904 * @param data The intent describing the shortcut.
905 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700907 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
908 int cellY) {
909 int[] cellXY = mTmpAddItemCellCoordinates;
910 int[] touchXY = mPendingAddInfo.dropPos;
911 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Michael Jurkad3ef3062010-11-23 16:23:58 -0800913 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700914
Adam Cohen558baaf2011-08-15 15:22:57 -0700915 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800916 if (info == null) {
917 return;
918 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700919 final View view = createShortcut(info);
920
Adam Cohenfbba09b2011-07-18 21:43:05 -0700921 // First we check if we already know the exact location where we want to add this item.
922 if (cellX >= 0 && cellY >= 0) {
923 cellXY[0] = cellX;
924 cellXY[1] = cellY;
925 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700926
927 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800928 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700929 true, null,null)) {
930 return;
931 }
932 DragObject dragObject = new DragObject();
933 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800934 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
935 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700936 return;
937 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700938 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800939 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800941 foundCellSpan = (result != null);
942 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700943 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800944 }
945
946 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700947 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700948 return;
949 }
950
Adam Cohen558baaf2011-08-15 15:22:57 -0700951 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952
953 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
955 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957 }
958
Adam Cohenf814aa02011-09-01 13:48:05 -0700959 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
960 if (spanXY == null) {
961 spanXY = new int[2];
962 }
963
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800964 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700965 // We want to account for the extra amount of padding that we are adding to the widget
966 // to ensure that it gets the full amount of space that it has requested
967 int requiredWidth = minWidth + padding.left + padding.right;
968 int requiredHeight = minHeight + padding.top + padding.bottom;
969 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
970 }
971
972 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
973 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
974 }
975
Adam Cohend41fbf52012-02-16 23:53:59 -0800976 int[] getMinSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
Adam Cohencbf47e32011-09-16 17:32:37 -0700977 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
978 }
979
Adam Cohenf814aa02011-09-01 13:48:05 -0700980 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
981 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
982 }
983
Adam Cohend41fbf52012-02-16 23:53:59 -0800984 int[] getMinSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
985 return getSpanForWidget(info.componentName, info.minResizeWidth,
986 info.minResizeHeight, spanXY);
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700992 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800995 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
996 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
997 if (appWidgetInfo == null) {
998 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
999 }
Romain Guycbb89e42009-06-08 15:52:54 -07001000
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001001 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001003
Adam Cohend41fbf52012-02-16 23:53:59 -08001004 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001005 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001008 // We have saved the position to which the widget was dragged-- this really only matters
1009 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001010 int[] cellXY = mTmpAddItemCellCoordinates;
1011 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001012 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001013 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1015 cellXY[0] = mPendingAddInfo.cellX;
1016 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001017 spanXY[0] = mPendingAddInfo.spanX;
1018 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001019 foundCellSpan = true;
1020 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001021 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001023 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1024 spanXY[1], cellXY, finalSpan);
1025 spanXY[0] = finalSpan[0];
1026 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001027 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001029 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001030 }
1031
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001033 if (appWidgetId != -1) {
1034 // Deleting an app widget ID is a void call but writes to disk before returning
1035 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001036 new Thread("deleteAppWidgetId") {
1037 public void run() {
1038 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1039 }
1040 }.start();
1041 }
Winson Chung93eef082012-03-23 15:59:27 -07001042 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001043 return;
1044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001047 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 launcherInfo.spanX = spanXY[0];
1049 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001050 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1051 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001054 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055
1056 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001057 if (hostView == null) {
1058 // Perform actual inflation because we're live
1059 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1060 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1061 } else {
1062 // The AppWidgetHostView has already been inflated and instantiated
1063 launcherInfo.hostView = hostView;
1064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001067 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001069 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001070
1071 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001073 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
Romain Guycbb89e42009-06-08 15:52:54 -07001075
Adam Cohended9f8d2010-11-03 13:25:16 -07001076 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1077 @Override
1078 public void onReceive(Context context, Intent intent) {
1079 final String action = intent.getAction();
1080 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1081 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001082 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001083 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001084
Winson Chungc100e8e2011-08-09 16:02:43 -07001085 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001086 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001087 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1088 mAppsCustomizeTabHost.reset();
1089 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001090 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001091 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1092 mUserPresent = true;
1093 updateRunning();
1094 }
1095 }
1096 };
1097
1098 @Override
1099 public void onAttachedToWindow() {
1100 super.onAttachedToWindow();
1101
1102 // Listen for broadcasts related to user-presence
1103 final IntentFilter filter = new IntentFilter();
1104 filter.addAction(Intent.ACTION_SCREEN_OFF);
1105 filter.addAction(Intent.ACTION_USER_PRESENT);
1106 registerReceiver(mReceiver, filter);
1107
Adam Cohend113e0c2010-11-11 10:48:05 -08001108 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001109 mVisible = true;
1110 }
1111
1112 @Override
1113 public void onDetachedFromWindow() {
1114 super.onDetachedFromWindow();
1115 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001116 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001117
Adam Cohend113e0c2010-11-11 10:48:05 -08001118 if (mAttached) {
1119 unregisterReceiver(mReceiver);
1120 mAttached = false;
1121 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 updateRunning();
1123 }
1124
1125 public void onWindowVisibilityChanged(int visibility) {
1126 mVisible = visibility == View.VISIBLE;
1127 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001128 // The following code used to be in onResume, but it turns out onResume is called when
1129 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1130 // is a more appropriate event to handle
1131 if (mVisible) {
1132 mAppsCustomizeTabHost.onWindowVisible();
1133 if (!mWorkspaceLoading) {
1134 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001135 // We want to let Launcher draw itself at least once before we force it to build
1136 // layers on all the workspace pages, so that transitioning to Launcher from other
1137 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1138 // a true draw so we wait until the second preDraw call to be safe
1139 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001140 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001141 // We delay the layer building a bit in order to give
1142 // other message processing a time to run. In particular
1143 // this avoids a delay in hiding the IME if it was
1144 // currently shown, because doing that may involve
1145 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001146 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001147 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001148 return true;
1149 }
1150 });
1151 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001152 // When Launcher comes back to foreground, a different Activity might be responsible for
1153 // the app market intent, so refresh the icon
1154 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001155 clearTypedText();
1156 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 }
1158
1159 private void sendAdvanceMessage(long delay) {
1160 mHandler.removeMessages(ADVANCE_MSG);
1161 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1162 mHandler.sendMessageDelayed(msg, delay);
1163 mAutoAdvanceSentTime = System.currentTimeMillis();
1164 }
1165
1166 private void updateRunning() {
1167 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1168 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1169 mAutoAdvanceRunning = autoAdvanceRunning;
1170 if (autoAdvanceRunning) {
1171 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1172 sendAdvanceMessage(delay);
1173 } else {
1174 if (!mWidgetsToAdvance.isEmpty()) {
1175 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1176 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1177 }
1178 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001179 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001180 }
1181 }
1182 }
1183
1184 private final Handler mHandler = new Handler() {
1185 @Override
1186 public void handleMessage(Message msg) {
1187 if (msg.what == ADVANCE_MSG) {
1188 int i = 0;
1189 for (View key: mWidgetsToAdvance.keySet()) {
1190 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1191 final int delay = mAdvanceStagger * i;
1192 if (v instanceof Advanceable) {
1193 postDelayed(new Runnable() {
1194 public void run() {
1195 ((Advanceable) v).advance();
1196 }
1197 }, delay);
1198 }
1199 i++;
1200 }
1201 sendAdvanceMessage(mAdvanceInterval);
1202 }
1203 }
1204 };
1205
1206 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001207 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001208 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1209 if (v instanceof Advanceable) {
1210 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001211 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 updateRunning();
1213 }
1214 }
1215
1216 void removeWidgetToAutoAdvance(View hostView) {
1217 if (mWidgetsToAdvance.containsKey(hostView)) {
1218 mWidgetsToAdvance.remove(hostView);
1219 updateRunning();
1220 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 }
1222
Joe Onorato9c1289c2009-08-17 11:03:03 -04001223 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001224 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001225 launcherInfo.hostView = null;
1226 }
1227
Winson Chung93eef082012-03-23 15:59:27 -07001228 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1229 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1230 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 }
1232
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 public LauncherAppWidgetHost getAppWidgetHost() {
1234 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
Romain Guycbb89e42009-06-08 15:52:54 -07001236
Winson Chunga9abd0e2010-10-27 17:18:37 -07001237 public LauncherModel getModel() {
1238 return mModel;
1239 }
1240
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001241 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001242 getWindow().closeAllPanels();
1243
Winson Chung87acb482011-08-23 17:28:05 -07001244 /**
1245 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001246 try {
1247 dismissDialog(DIALOG_CREATE_SHORTCUT);
1248 // Unlock the workspace if the dialog was showing
1249 } catch (Exception e) {
1250 // An exception is thrown if the dialog is not visible, which is fine
1251 }
1252
1253 try {
1254 dismissDialog(DIALOG_RENAME_FOLDER);
1255 // Unlock the workspace if the dialog was showing
1256 } catch (Exception e) {
1257 // An exception is thrown if the dialog is not visible, which is fine
1258 }
Winson Chung87acb482011-08-23 17:28:05 -07001259 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001260
1261 // Whatever we were doing is hereby canceled.
1262 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
1266 protected void onNewIntent(Intent intent) {
1267 super.onNewIntent(intent);
1268
1269 // Close the menu
1270 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001271 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001272 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001273
Joe Onorato14f122b2009-11-19 14:06:36 -08001274 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1275 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001276
Adam Cohenac56cff2011-09-28 20:45:37 -07001277 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001278 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001279 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001280 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1281 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001282 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001283 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001284
1285 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001286 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001287 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001288
Joe Onorato3a8820b2009-11-10 15:06:42 -08001289 final View v = getWindow().peekDecorView();
1290 if (v != null && v.getWindowToken() != null) {
1291 InputMethodManager imm = (InputMethodManager)getSystemService(
1292 INPUT_METHOD_SERVICE);
1293 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001295
Michael Jurka35aa14d2011-07-07 17:01:08 -07001296 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001297 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001298 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301 }
1302
1303 @Override
1304 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1305 // Do not call super here
1306 mSavedInstanceState = savedInstanceState;
1307 }
1308
1309 @Override
1310 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001311 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001312 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313
Michael Jurka883f55b2010-10-21 15:47:14 -07001314 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001315 // We close any open folder since it will not be re-opened, and we need to make sure
1316 // this state is reflected.
1317 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1320 mWaitingForResult) {
1321 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1322 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1323 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1324 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 }
1326
1327 if (mFolderInfo != null && mWaitingForResult) {
1328 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1329 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1330 }
Michael Jurka946ad472010-07-09 18:05:18 -07001331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 // Save the current AppsCustomize tab
1333 if (mAppsCustomizeTabHost != null) {
1334 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1335 if (currentTabTag != null) {
1336 outState.putString("apps_customize_currentTab", currentTabTag);
1337 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001338 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1339 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 @Override
1344 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001346
Winson Chunge7a03942011-08-05 15:05:12 -07001347 // Remove all pending runnables
1348 mHandler.removeMessages(ADVANCE_MSG);
1349 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001350 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001351
Winson Chungcd2b0142011-06-08 16:02:26 -07001352 // Stop callbacks from LauncherModel
1353 LauncherApplication app = ((LauncherApplication) getApplication());
1354 mModel.stopLoader();
1355 app.setLauncher(null);
1356
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001358 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001360 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001362 mAppWidgetHost = null;
1363
1364 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
1366 TextKeyListener.getInstance().release();
1367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001370
1371 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001372 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001373
1374 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1375 mWorkspace.removeAllViews();
1376 mWorkspace = null;
1377 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001378
1379 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
1381
Adam Cohena9cf38f2011-05-02 15:36:58 -07001382 public DragController getDragController() {
1383 return mDragController;
1384 }
1385
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 @Override
1387 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001388 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 super.startActivityForResult(intent, requestCode);
1390 }
1391
Winson Chung70d72102011-08-12 11:24:35 -07001392 /**
1393 * Indicates that we want global search for this activity by setting the globalSearch
1394 * argument for {@link #startSearch} to true.
1395 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001397 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001399
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001400 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Karl Rosaen138a0412009-04-23 19:00:21 -07001402 if (initialQuery == null) {
1403 // Use any text typed in the launcher as the initial query
1404 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 if (appSearchData == null) {
1407 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001408 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001410 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Karl Rosaen138a0412009-04-23 19:00:21 -07001412 final SearchManager searchManager =
1413 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001415 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
Winson Chung70d72102011-08-12 11:24:35 -07001418 @Override
1419 public boolean onCreateOptionsMenu(Menu menu) {
1420 if (isWorkspaceLocked()) {
1421 return false;
1422 }
1423
1424 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001425
1426 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1427 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1428 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001429 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1430 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1431 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001432 String helpUrl = getString(R.string.help_url);
1433 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001434 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1435 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1436
Winson Chung70d72102011-08-12 11:24:35 -07001437 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1438 .setIcon(android.R.drawable.ic_menu_gallery)
1439 .setAlphabeticShortcut('W');
1440 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1441 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001442 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001443 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001444 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1445 .setIcon(android.R.drawable.ic_menu_preferences)
1446 .setIntent(settings)
1447 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001448 if (!helpUrl.isEmpty()) {
1449 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1450 .setIcon(android.R.drawable.ic_menu_help)
1451 .setIntent(help)
1452 .setAlphabeticShortcut('H');
1453 }
Winson Chung70d72102011-08-12 11:24:35 -07001454 return true;
1455 }
1456
1457 @Override
1458 public boolean onPrepareOptionsMenu(Menu menu) {
1459 super.onPrepareOptionsMenu(menu);
1460
1461 if (mAppsCustomizeTabHost.isTransitioning()) {
1462 return false;
1463 }
1464 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1465 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1466
1467 return true;
1468 }
1469
1470 @Override
1471 public boolean onOptionsItemSelected(MenuItem item) {
1472 switch (item.getItemId()) {
1473 case MENU_WALLPAPER_SETTINGS:
1474 startWallpaper();
1475 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001476 }
1477
1478 return super.onOptionsItemSelected(item);
1479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001481 @Override
1482 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001483 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001484 // Use a custom animation for launching search
1485 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001486 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001487 }
1488
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 public boolean isWorkspaceLocked() {
1490 return mWorkspaceLoading || mWaitingForResult;
1491 }
1492
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001494 mPendingAddInfo.container = ItemInfo.NO_ID;
1495 mPendingAddInfo.screen = -1;
1496 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1497 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001498 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001500 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001501
Michael Jurkaaf442092010-06-10 17:01:57 -07001502 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 // TODO: catch bad widget exception when sent
1504 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001505 // TODO: Is this log message meaningful?
1506 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001507 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001508 }
1509
Adam Cohened66b2b2012-01-23 17:28:51 -08001510 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1511 final AppWidgetProviderInfo appWidget = info.info;
1512 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001513 if (appWidget.configure != null) {
1514 // Launch over to configure widget, if needed
1515 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1516 intent.setComponent(appWidget.configure);
1517 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001518 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001519 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001520 intent.putExtra(InstallWidgetReceiver.
1521 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001522
1523 final String mimeType = info.mimeType;
1524 final ClipData clipData = (ClipData) info.configurationData;
1525 final ClipDescription clipDesc = clipData.getDescription();
1526 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1527 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001528 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001529 final CharSequence stringData = item.getText();
1530 final Uri uriData = item.getUri();
1531 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001532 final String key = InstallWidgetReceiver.
1533 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001534 if (uriData != null) {
1535 intent.putExtra(key, uriData);
1536 } else if (intentData != null) {
1537 intent.putExtra(key, intentData);
1538 } else if (stringData != null) {
1539 intent.putExtra(key, stringData);
1540 }
1541 break;
1542 }
1543 }
1544 }
Winson Chung55cef262010-10-28 14:14:18 -07001545 }
Romain Guy8e633c52010-03-04 12:51:36 -08001546 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001549 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001550 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001551 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
Romain Guycbb89e42009-06-08 15:52:54 -07001555
Adam Cohenfbba09b2011-07-18 21:43:05 -07001556 /**
1557 * Process a shortcut drop.
1558 *
1559 * @param componentName The name of the component
1560 * @param screen The screen where it should be added
1561 * @param cell The cell it should be added to, optional
1562 * @param position The location on the screen where it was dropped, optional
1563 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1565 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001567 mPendingAddInfo.container = container;
1568 mPendingAddInfo.screen = screen;
1569 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001570
1571 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 mPendingAddInfo.cellX = cell[0];
1573 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001575
1576 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1577 createShortcutIntent.setComponent(componentName);
1578 processShortcut(createShortcutIntent);
1579 }
1580
Adam Cohenfbba09b2011-07-18 21:43:05 -07001581 /**
1582 * Process a widget drop.
1583 *
1584 * @param info The PendingAppWidgetInfo of the widget being added.
1585 * @param screen The screen where it should be added
1586 * @param cell The cell it should be added to, optional
1587 * @param position The location on the screen where it was dropped, optional
1588 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 mPendingAddInfo.container = info.container = container;
1593 mPendingAddInfo.screen = info.screen = screen;
1594 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001595 mPendingAddInfo.minSpanX = info.minSpanX;
1596 mPendingAddInfo.minSpanY = info.minSpanY;
1597
Adam Cohenfbba09b2011-07-18 21:43:05 -07001598 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001599 mPendingAddInfo.cellX = cell[0];
1600 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001601 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001602 if (span != null) {
1603 mPendingAddInfo.spanX = span[0];
1604 mPendingAddInfo.spanY = span[1];
1605 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001606
Adam Cohened66b2b2012-01-23 17:28:51 -08001607 AppWidgetHostView hostView = info.boundWidget;
1608 int appWidgetId;
1609 if (hostView != null) {
1610 appWidgetId = hostView.getAppWidgetId();
1611 } else {
1612 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1613 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1614 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001615 addAppWidgetImpl(appWidgetId, info);
1616 }
1617
Joe Onoratodeb98af2010-02-19 14:59:39 -08001618 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001619 // Handle case where user selected "Applications"
1620 String applicationName = getResources().getString(R.string.group_applications);
1621 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001622
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001623 if (applicationName != null && applicationName.equals(shortcutName)) {
1624 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1625 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001627 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1628 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001629 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001630 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001631 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001632 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
Winson Chung24ab2f12010-09-16 14:10:47 -07001636 void processWallpaper(Intent intent) {
1637 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1638 }
1639
Winson Chung3d503fb2011-07-13 17:25:49 -07001640 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1641 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001642 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 folderInfo.title = getText(R.string.folder_name);
1644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1647 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001648 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
1650 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001651 FolderIcon newFolder =
1652 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1653 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1654 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001655 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
Romain Guycbb89e42009-06-08 15:52:54 -07001657
Joe Onorato9c1289c2009-08-17 11:03:03 -04001658 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001659 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 }
1661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001663 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001665 Intent chooser = Intent.createChooser(pickWallpaper,
1666 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001667 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1668 // Removed in Eclair MR1
1669// WallpaperManager wm = (WallpaperManager)
1670// getSystemService(Context.WALLPAPER_SERVICE);
1671// WallpaperInfo wi = wm.getWallpaperInfo();
1672// if (wi != null && wi.getSettingsActivity() != null) {
1673// LabeledIntent li = new LabeledIntent(getPackageName(),
1674// R.string.configure_wallpaper, 0);
1675// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1676// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1677// }
Mike Clerona0618e42009-10-22 13:55:21 -07001678 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001681 /**
1682 * Registers various content observers. The current implementation registers
1683 * only a favorites observer to keep track of the favorites applications.
1684 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001685 private void registerContentObservers() {
1686 ContentResolver resolver = getContentResolver();
1687 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1688 true, mWidgetObserver);
1689 }
1690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 @Override
1692 public boolean dispatchKeyEvent(KeyEvent event) {
1693 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1694 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001696 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001697 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001698 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001699 dumpState();
1700 return true;
1701 }
1702 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001703 }
1704 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1705 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001706 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 return true;
1708 }
1709 }
1710
1711 return super.dispatchKeyEvent(event);
1712 }
1713
Joe Onorato88ec0992009-11-19 13:16:06 -08001714 @Override
1715 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001716 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001717 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001718 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001719 Folder openFolder = mWorkspace.getOpenFolder();
1720 if (openFolder.isEditingName()) {
1721 openFolder.dismissEditingName();
1722 } else {
1723 closeFolder();
1724 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001725 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001726 mWorkspace.exitWidgetResizeMode();
1727
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001728 // Back button is a no-op here, but give at least some feedback for the button press
1729 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001730 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001731 }
1732
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001734 * Re-listen when widgets are reset.
1735 */
1736 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001737 if (mAppWidgetHost != null) {
1738 mAppWidgetHost.startListening();
1739 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001740 }
1741
1742 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1744 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001747 if (mModel != null) {
1748 mModel.unbindWorkspaceItems();
1749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 /**
1753 * Launches the intent referred by the clicked shortcut.
1754 *
1755 * @param v The view representing the clicked shortcut.
1756 */
1757 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001758 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1759 // view has detached (it's possible for this to happen if the view is removed mid touch).
1760 if (v.getWindowToken() == null) {
1761 return;
1762 }
1763
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001764 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001765 return;
1766 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001767
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001769 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001771 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001772 int[] pos = new int[2];
1773 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001774 intent.setSourceBounds(new Rect(pos[0], pos[1],
1775 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001776 boolean success = startActivitySafely(intent, tag);
1777
1778 if (success && v instanceof BubbleTextView) {
1779 mWaitingForResume = (BubbleTextView) v;
1780 mWaitingForResume.setStayPressed(true);
1781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001783 if (v instanceof FolderIcon) {
1784 FolderIcon fi = (FolderIcon) v;
1785 handleFolderClick(fi);
1786 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001787 } else if (v == mAllAppsButton) {
1788 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001789 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001790 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001791 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001792 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794 }
1795
Michael Jurka0e260592010-06-30 17:07:39 -07001796 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001797 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001798 // clicking anywhere on the workspace causes the customization drawer to slide down
1799 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001800 return false;
1801 }
1802
Michael Jurkaaf442092010-06-10 17:01:57 -07001803 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001804 * Event handler for the search button
1805 *
1806 * @param v The view that was clicked.
1807 */
1808 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1810
Amith Yamasania135ba82011-08-09 17:42:01 -07001811 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001812 }
1813
1814 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001815 * Event handler for the voice button
1816 *
1817 * @param v The view that was clicked.
1818 */
1819 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001820 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001821
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001822 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001823 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001824 startActivity(intent);
1825 }
1826
1827 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001828 * Event handler for the "grid" button that appears on the home screen, which
1829 * enters all apps mode.
1830 *
1831 * @param v The view that was clicked.
1832 */
1833 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001834 showAllApps(true);
1835 }
1836
1837 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001838 // Provide the same haptic feedback that the system offers for virtual keys.
1839 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001840 }
1841
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001842 public void onClickAppMarketButton(View v) {
1843 if (mAppMarketIntent != null) {
1844 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001845 } else {
1846 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001847 }
1848 }
1849
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001850 void startApplicationDetailsActivity(ComponentName componentName) {
1851 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001852 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1853 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001854 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001855 startActivity(intent);
1856 }
1857
Patrick Dubroy5539af72010-09-07 15:22:01 -07001858 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1859 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1860 // System applications cannot be installed. For now, show a toast explaining that.
1861 // We may give them the option of disabling apps this way.
1862 int messageId = R.string.uninstall_system_app_text;
1863 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1864 } else {
1865 String packageName = appInfo.componentName.getPackageName();
1866 String className = appInfo.componentName.getClassName();
1867 Intent intent = new Intent(
1868 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001869 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1870 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001871 startActivity(intent);
1872 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001873 }
1874
Michael Jurkaddd62e92011-02-16 17:49:14 -08001875 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1877 try {
1878 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001879 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 } catch (ActivityNotFoundException e) {
1881 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001882 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 } catch (SecurityException e) {
1884 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001885 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001887 "or use the exported attribute for this activity. "
1888 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001890 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001892
Romain Guy8e633c52010-03-04 12:51:36 -08001893 void startActivityForResultSafely(Intent intent, int requestCode) {
1894 try {
1895 startActivityForResult(intent, requestCode);
1896 } catch (ActivityNotFoundException e) {
1897 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1898 } catch (SecurityException e) {
1899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1900 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1901 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1902 "or use the exported attribute for this activity.", e);
1903 }
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905
Adam Cohena9cf38f2011-05-02 15:36:58 -07001906 private void handleFolderClick(FolderIcon folderIcon) {
1907 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001908 Folder openFolder = mWorkspace.getFolderForTag(info);
1909
1910 // If the folder info reports that the associated folder is open, then verify that
1911 // it is actually opened. There have been a few instances where this gets out of sync.
1912 if (info.opened && openFolder == null) {
1913 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1914 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1915 info.opened = false;
1916 }
1917
Adam Cohena9cf38f2011-05-02 15:36:58 -07001918 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 // Close any open folder
1920 closeFolder();
1921 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001922 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 } else {
1924 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 int folderScreen;
1926 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001927 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 // .. and close it
1929 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001930 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 // Close any folder open on the current screen
1932 closeFolder();
1933 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001934 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936 }
1937 }
1938 }
1939
Adam Cohen2801caf2011-05-13 20:57:39 -07001940 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001941 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001942 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1943 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1944 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1945
Adam Cohenc51934b2011-07-26 21:07:43 -07001946 FolderInfo info = (FolderInfo) fi.getTag();
1947 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1948 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001949 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1950 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001951 }
1952
Adam Cohen2801caf2011-05-13 20:57:39 -07001953 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1954 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1955 oa.start();
1956 }
1957
1958 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001959 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001960 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1961 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1962 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1963
Adam Cohenc51934b2011-07-26 21:07:43 -07001964 FolderInfo info = (FolderInfo) fi.getTag();
1965 CellLayout cl = null;
1966 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1967 cl = (CellLayout) fi.getParent().getParent();
1968 }
1969
1970 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001971 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1972 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001973 oa.addListener(new AnimatorListenerAdapter() {
1974 @Override
1975 public void onAnimationEnd(Animator animation) {
1976 if (layout != null) {
1977 layout.clearFolderLeaveBehind();
1978 }
1979 }
1980 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001981 oa.start();
1982 }
1983
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001985 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 * is animated relative to the specified View. If the View is null, no animation
1987 * is played.
1988 *
1989 * @param folderInfo The FolderInfo describing the folder to open.
1990 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 public void openFolder(FolderIcon folderIcon) {
1992 Folder folder = folderIcon.mFolder;
1993 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994
Adam Cohen2801caf2011-05-13 20:57:39 -07001995 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001996 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohen4554ee12011-08-03 16:13:21 -07001998 // Just verify that the folder hasn't already been added to the DragLayer.
1999 // There was a one-off crash where the folder had a parent already.
2000 if (folder.getParent() == null) {
2001 mDragLayer.addView(folder);
2002 mDragController.addDropTarget((DropTarget) folder);
2003 } else {
2004 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2005 folder.getParent() + ").");
2006 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002007 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002008 }
2009
2010 public void closeFolder() {
2011 Folder folder = mWorkspace.getOpenFolder();
2012 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002013 if (folder.isEditingName()) {
2014 folder.dismissEditingName();
2015 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002016 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002017
2018 // Dismiss the folder cling
2019 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002020 }
2021 }
2022
2023 void closeFolder(Folder folder) {
2024 folder.getInfo().opened = false;
2025
2026 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2027 if (parent != null) {
2028 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2029 shrinkAndFadeInFolderIcon(fi);
2030 }
2031 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002035
Patrick Dubroye708c522011-03-01 16:03:43 -08002036 if (mState != State.WORKSPACE) {
2037 return false;
2038 }
2039
Joe Onorato9c1289c2009-08-17 11:03:03 -04002040 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 return false;
2042 }
2043
2044 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002045 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Michael Jurka0280c3b2010-09-17 15:00:07 -07002048 resetAddInfo();
2049 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002051 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 return true;
2053 }
2054
Winson Chung3d503fb2011-07-13 17:25:49 -07002055 // The hotseat touch handling does not go through Workspace, and we always allow long press
2056 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002057 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002058 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2059 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002060 if (itemUnderLongClick == null) {
2061 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002062 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2063 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002064 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002068 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070 }
2071 }
2072 return true;
2073 }
2074
Winson Chung3d503fb2011-07-13 17:25:49 -07002075 boolean isHotseatLayout(View layout) {
2076 return mHotseat != null && layout != null &&
2077 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2078 }
2079 Hotseat getHotseat() {
2080 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002081 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002082 SearchDropTargetBar getSearchBar() {
2083 return mSearchDropTargetBar;
2084 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002085
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 /**
2087 * Returns the CellLayout of the specified container at the specified screen.
2088 */
2089 CellLayout getCellLayout(long container, int screen) {
2090 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2091 if (mHotseat != null) {
2092 return mHotseat.getLayout();
2093 } else {
2094 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002095 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002096 } else {
2097 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002098 }
2099 }
2100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 Workspace getWorkspace() {
2102 return mWorkspace;
2103 }
2104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 @Override
2106 protected Dialog onCreateDialog(int id) {
2107 switch (id) {
2108 case DIALOG_CREATE_SHORTCUT:
2109 return new CreateShortcut().createDialog();
2110 case DIALOG_RENAME_FOLDER:
2111 return new RenameFolder().createDialog();
2112 }
2113
2114 return super.onCreateDialog(id);
2115 }
2116
2117 @Override
2118 protected void onPrepareDialog(int id, Dialog dialog) {
2119 switch (id) {
2120 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 break;
2122 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002123 if (mFolderInfo != null) {
2124 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2125 final CharSequence text = mFolderInfo.title;
2126 input.setText(text);
2127 input.setSelection(0, text.length());
2128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 break;
2130 }
2131 }
2132
2133 void showRenameDialog(FolderInfo info) {
2134 mFolderInfo = info;
2135 mWaitingForResult = true;
2136 showDialog(DIALOG_RENAME_FOLDER);
2137 }
2138
Adam Cohenfbba09b2011-07-18 21:43:05 -07002139 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002140 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002141 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2142 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 mWaitingForResult = true;
2144 showDialog(DIALOG_CREATE_SHORTCUT);
2145 }
2146
2147 private class RenameFolder {
2148 private EditText mInput;
2149
2150 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2152 mInput = (EditText) layout.findViewById(R.id.folder_name);
2153
2154 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2155 builder.setIcon(0);
2156 builder.setTitle(getString(R.string.rename_folder_title));
2157 builder.setCancelable(true);
2158 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2159 public void onCancel(DialogInterface dialog) {
2160 cleanup();
2161 }
2162 });
2163 builder.setNegativeButton(getString(R.string.cancel_action),
2164 new Dialog.OnClickListener() {
2165 public void onClick(DialogInterface dialog, int which) {
2166 cleanup();
2167 }
2168 }
2169 );
2170 builder.setPositiveButton(getString(R.string.rename_action),
2171 new Dialog.OnClickListener() {
2172 public void onClick(DialogInterface dialog, int which) {
2173 changeFolderName();
2174 }
2175 }
2176 );
2177 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002178
2179 final AlertDialog dialog = builder.create();
2180 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2181 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002182 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002183 mInput.requestFocus();
2184 InputMethodManager inputManager = (InputMethodManager)
2185 getSystemService(Context.INPUT_METHOD_SERVICE);
2186 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002187 }
2188 });
2189
2190 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192
2193 private void changeFolderName() {
2194 final String name = mInput.getText().toString();
2195 if (!TextUtils.isEmpty(name)) {
2196 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002197 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 mFolderInfo.title = name;
2199 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2200
Joe Onorato9c1289c2009-08-17 11:03:03 -04002201 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002202 lockAllApps();
Winson Chungf0c6ae02012-03-21 16:10:31 -07002203 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 } else {
2205 final FolderIcon folderIcon = (FolderIcon)
2206 mWorkspace.getViewForTag(mFolderInfo);
2207 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002208 // TODO: At some point we'll probably want some version of setting
2209 // the text for a folder icon.
2210 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 getWorkspace().requestLayout();
2212 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002213 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002214 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -07002215 mModel.startLoader(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 }
2218 }
2219 cleanup();
2220 }
2221
2222 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 dismissDialog(DIALOG_RENAME_FOLDER);
2224 mWaitingForResult = false;
2225 mFolderInfo = null;
2226 }
2227 }
2228
Daniel Sandler843e8602010-06-07 14:59:01 -04002229 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2230 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002231 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002232 }
2233
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002234 public boolean isAllAppsButtonRank(int rank) {
2235 return mHotseat.isAllAppsButtonRank(rank);
2236 }
2237
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002238 // AllAppsView.Watcher
2239 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002240 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002241 mWorkspace.setVisibility(View.GONE);
2242 }
2243 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002244
2245 /**
2246 * Helper method for the cameraZoomIn/cameraZoomOut animations
2247 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002248 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002249 * @param scaleFactor The scale factor used for the zoom
2250 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002251 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002252 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002253 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002255
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002256 void updateWallpaperVisibility(boolean visible) {
2257 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2258 int curflags = getWindow().getAttributes().flags
2259 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2260 if (wpflags != curflags) {
2261 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2262 }
2263 }
2264
Michael Jurkabed61d22012-02-14 22:51:29 -08002265 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2266 if (v instanceof LauncherTransitionable) {
2267 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2268 }
Winson Chung70442722012-02-10 15:43:22 -08002269
2270 // Update the workspace transition step as well
2271 dispatchOnLauncherTransitionStep(v, 0f);
2272 }
2273
2274 private void dispatchOnLauncherTransitionStep(View v, float t) {
2275 if (v instanceof LauncherTransitionable) {
2276 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2277 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002278 }
2279
2280 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2281 if (v instanceof LauncherTransitionable) {
2282 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2283 }
Winson Chung70442722012-02-10 15:43:22 -08002284
2285 // Update the workspace transition step as well
2286 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002287 }
2288
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002289 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002290 * Things to test when changing the following seven functions.
2291 * - Home from workspace
2292 * - from center screen
2293 * - from other screens
2294 * - Home from all apps
2295 * - from center screen
2296 * - from other screens
2297 * - Back from all apps
2298 * - from center screen
2299 * - from other screens
2300 * - Launch app from workspace and quit
2301 * - with back
2302 * - with home
2303 * - Launch app from all apps and quit
2304 * - with back
2305 * - with home
2306 * - Go to a screen that's not the default, then all
2307 * apps, and launch and app, and go back
2308 * - with back
2309 * -with home
2310 * - On workspace, long press power and go back
2311 * - with back
2312 * - with home
2313 * - On all apps, long press power and go back
2314 * - with back
2315 * - with home
2316 * - On workspace, power off
2317 * - On all apps, power off
2318 * - Launch an app and turn off the screen while in that app
2319 * - Go back with home key
2320 * - Go back with back key TODO: make this not go to workspace
2321 * - From all apps
2322 * - From workspace
2323 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2324 * - From all apps
2325 * - From the center workspace
2326 * - From another workspace
2327 */
2328
2329 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002330 * Zoom the camera out from the workspace to reveal 'toView'.
2331 * Assumes that the view to show is anchored at either the very top or very bottom
2332 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002333 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002334 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002335 if (mStateAnimation != null) {
2336 mStateAnimation.cancel();
2337 mStateAnimation = null;
2338 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002340 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002341
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2343 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2344 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002345 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002346 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002347 final int startDelay =
2348 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002349
Michael Jurkab3e22d92011-10-31 15:58:33 -07002350 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351
Michael Jurkad74c9842011-07-10 12:44:21 -07002352 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002353 Animator workspaceAnim =
2354 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355
2356 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002357 toView.setScaleX(scale);
2358 toView.setScaleY(scale);
2359 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2360 scaleAnim.
2361 scaleX(1f).scaleY(1f).
2362 setDuration(duration).
2363 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002364
Winson Chungf0ea4d32011-06-06 14:27:16 -07002365 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002366 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002367 final ObjectAnimator alphaAnim = ObjectAnimator
2368 .ofFloat(toView, "alpha", 0f, 1f)
2369 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002370 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002371 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2372 @Override
2373 public void onAnimationUpdate(ValueAnimator animation) {
2374 float t = (Float) animation.getAnimatedValue();
2375 dispatchOnLauncherTransitionStep(fromView, t);
2376 dispatchOnLauncherTransitionStep(toView, t);
2377 }
2378 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002379
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002380 // toView should appear right at the end of the workspace shrink
2381 // animation
2382 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002383 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002384 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002385
2386 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002387 boolean animationCancelled = false;
2388
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002389 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002390 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002391 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002392 // Prepare the position
2393 toView.setTranslationX(0.0f);
2394 toView.setTranslationY(0.0f);
2395 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002396 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002397 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002398 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002399 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002400 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2401 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002402
2403 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2404 // Hide the workspace scrollbar
2405 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002406 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002407 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002408 if (!animationCancelled) {
2409 updateWallpaperVisibility(false);
2410 }
2411 }
2412
Adam Cohencff6af82011-09-13 14:51:53 -07002413 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002414 public void onAnimationCancel(Animator animation) {
2415 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002416 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002417 });
2418
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002419 if (workspaceAnim != null) {
2420 mStateAnimation.play(workspaceAnim);
2421 }
Michael Jurka1899a362011-11-03 13:50:45 -07002422
2423 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002424 final ViewTreeObserver observer;
2425
Michael Jurkabed61d22012-02-14 22:51:29 -08002426 dispatchOnLauncherTransitionStart(fromView, animated, false);
2427 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002428
2429 // If any of the objects being animated haven't been measured/laid out
2430 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002431 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002432 (mWorkspace.getMeasuredWidth() == 0) ||
2433 (toView.getMeasuredWidth() == 0)) {
2434 observer = mWorkspace.getViewTreeObserver();
2435 delayAnim = true;
2436 } else {
2437 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002438 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002439
2440 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002441 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002442 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2443 public void onGlobalLayout() {
2444 mWorkspace.post(new Runnable() {
2445 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002446 // Check that mStateAnimation hasn't changed while
2447 // we waited for a layout pass
2448 if (mStateAnimation == stateAnimation) {
2449 // Need to update pivots for zoom if layout changed
2450 setPivotsForZoom(toView, scale);
2451 mStateAnimation.start();
2452 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002453 }
2454 });
2455 observer.removeGlobalOnLayoutListener(this);
2456 }
2457 };
2458 observer.addOnGlobalLayoutListener(delayedStart);
2459 } else {
2460 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002461 mStateAnimation.start();
2462 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463 } else {
2464 toView.setTranslationX(0.0f);
2465 toView.setTranslationY(0.0f);
2466 toView.setScaleX(1.0f);
2467 toView.setScaleY(1.0f);
2468 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002469 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002470
Michael Jurkabed61d22012-02-14 22:51:29 -08002471 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2472 // Hide the workspace scrollbar
2473 mWorkspace.hideScrollingIndicator(true);
2474 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002475 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002476 dispatchOnLauncherTransitionStart(fromView, animated, false);
2477 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2478 dispatchOnLauncherTransitionStart(toView, animated, false);
2479 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002480 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002481 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002482 }
2483
2484 /**
2485 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002486 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002488 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002489 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2490 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002491
Michael Jurkab3e22d92011-10-31 15:58:33 -07002492 if (mStateAnimation != null) {
2493 mStateAnimation.cancel();
2494 mStateAnimation = null;
2495 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002496 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002497
Winson Chungf0ea4d32011-06-06 14:27:16 -07002498 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002499 final int fadeOutDuration =
2500 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002501 final float scaleFactor = (float)
2502 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2503 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002504 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 Animator workspaceAnim = null;
2506
2507 if (toState == State.WORKSPACE) {
2508 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2509 workspaceAnim = mWorkspace.getChangeStateAnimation(
2510 Workspace.State.NORMAL, animated, stagger);
2511 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2512 workspaceAnim = mWorkspace.getChangeStateAnimation(
2513 Workspace.State.SPRING_LOADED, animated);
2514 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002515
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002517 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002518 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002519 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002520 final float oldScaleX = fromView.getScaleX();
2521 final float oldScaleY = fromView.getScaleY();
2522
Michael Jurka159b4cc2012-01-17 03:00:35 -08002523 final LauncherViewPropertyAnimator scaleAnim =
2524 new LauncherViewPropertyAnimator(fromView);
2525 scaleAnim.
2526 scaleX(scaleFactor).scaleY(scaleFactor).
2527 setDuration(duration).
2528 setInterpolator(new Workspace.ZoomInInterpolator());
2529
2530 final ObjectAnimator alphaAnim = ObjectAnimator
2531 .ofFloat(fromView, "alpha", 1f, 0f)
2532 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002533 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002534 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2535 @Override
2536 public void onAnimationUpdate(ValueAnimator animation) {
2537 float t = 1f - (Float) animation.getAnimatedValue();
2538 dispatchOnLauncherTransitionStep(fromView, t);
2539 dispatchOnLauncherTransitionStep(toView, t);
2540 }
2541 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002542
Michael Jurkabed61d22012-02-14 22:51:29 -08002543 mStateAnimation = new AnimatorSet();
2544
2545 dispatchOnLauncherTransitionStart(fromView, animated, true);
2546 dispatchOnLauncherTransitionStart(toView, animated, true);
2547
2548 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002549 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002550 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002551 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002552 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002553 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2554 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002555 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002556 if (onCompleteRunnable != null) {
2557 onCompleteRunnable.run();
2558 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002559 }
2560 });
2561
Winson Chungf0ea4d32011-06-06 14:27:16 -07002562 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002563 if (workspaceAnim != null) {
2564 mStateAnimation.play(workspaceAnim);
2565 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 } else {
2568 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002569 dispatchOnLauncherTransitionStart(fromView, animated, true);
2570 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2571 dispatchOnLauncherTransitionStart(toView, animated, true);
2572 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002573 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002574 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002575 }
2576
Michael Jurkae326f182011-11-21 14:05:46 -08002577 @Override
2578 public void onTrimMemory(int level) {
2579 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002580 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002581 mAppsCustomizeTabHost.onTrimMemory();
2582 }
2583 }
2584
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002585 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002586 showWorkspace(animated, null);
2587 }
2588
2589 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002590 if (mState != State.WORKSPACE) {
2591 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002592 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002593
2594 // Show the search bar and hotseat
2595 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002596 // We only need to animate in the dock divider if we're going from spring loaded mode
2597 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002598
2599 // Set focus to the AppsCustomize button
2600 if (mAllAppsButton != null) {
2601 mAllAppsButton.requestFocus();
2602 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002603 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002604
Michael Jurkab737ee62011-11-15 15:57:22 -08002605 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002606
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002607 // Change the state *after* we've called all the transition code
2608 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002609
Winson Chung5fb63472011-02-02 17:03:37 -08002610 // Resume the auto-advance of widgets
2611 mUserPresent = true;
2612 updateRunning();
2613
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002614 // send an accessibility event to announce the context change
2615 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002616 }
2617
Michael Jurkab3e22d92011-10-31 15:58:33 -07002618 void showAllApps(boolean animated) {
2619 if (mState != State.WORKSPACE) return;
2620
2621 showAppsCustomizeHelper(animated, false);
2622 mAppsCustomizeTabHost.requestFocus();
2623
2624 // Hide the search bar and hotseat
2625 mSearchDropTargetBar.hideSearchBar(animated);
2626
2627 // Change the state *after* we've called all the transition code
2628 mState = State.APPS_CUSTOMIZE;
2629
2630 // Pause the auto-advance of widgets until we are out of AllApps
2631 mUserPresent = false;
2632 updateRunning();
2633 closeFolder();
2634
2635 // Send an accessibility event to announce the context change
2636 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2637 }
2638
Adam Cohen7777d962011-08-18 18:58:38 -07002639 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002640 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002641 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002642 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002643 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002644 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002645 }
Adam Cohen7777d962011-08-18 18:58:38 -07002646
Adam Cohened66b2b2012-01-23 17:28:51 -08002647 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2648 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002649 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2650
Winson Chunge7a03942011-08-05 15:05:12 -07002651 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002652 @Override
2653 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002654 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002655 // Before we show workspace, hide all apps again because
2656 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2657 // clean up our state transition functions
2658 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002659 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002660 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002661 } else {
2662 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002663 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002664 }
2665 }, (extendedDelay ?
2666 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2667 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2668 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002669
Michael Jurkad3ef3062010-11-23 16:23:58 -08002670 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002671 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002672 final boolean animated = true;
2673 final boolean springLoaded = true;
2674 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002675 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002676 }
2677 // Otherwise, we are not in spring loaded mode, so don't do anything.
2678 }
2679
Michael Jurkab737ee62011-11-15 15:57:22 -08002680 void hideDockDivider() {
2681 if (mQsbDivider != null && mDockDivider != null) {
2682 mQsbDivider.setVisibility(View.INVISIBLE);
2683 mDockDivider.setVisibility(View.INVISIBLE);
2684 }
2685 }
2686
2687 void showDockDivider(boolean animated) {
2688 if (mQsbDivider != null && mDockDivider != null) {
2689 mQsbDivider.setVisibility(View.VISIBLE);
2690 mDockDivider.setVisibility(View.VISIBLE);
2691 if (mDividerAnimator != null) {
2692 mDividerAnimator.cancel();
2693 mQsbDivider.setAlpha(1f);
2694 mDockDivider.setAlpha(1f);
2695 mDividerAnimator = null;
2696 }
2697 if (animated) {
2698 mDividerAnimator = new AnimatorSet();
2699 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2700 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2701 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2702 mDividerAnimator.start();
2703 }
2704 }
2705 }
2706
Michael Jurkab3e22d92011-10-31 15:58:33 -07002707 void lockAllApps() {
2708 // TODO
2709 }
2710
2711 void unlockAllApps() {
2712 // TODO
2713 }
2714
Adam Cohenfc53cd22011-07-20 15:45:11 -07002715 public boolean isAllAppsCustomizeOpen() {
2716 return mState == State.APPS_CUSTOMIZE;
2717 }
2718
Winson Chungf0ea4d32011-06-06 14:27:16 -07002719 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002720 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002721 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002722 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002723 if (!LauncherApplication.isScreenLarge()) {
2724 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002725 if (mHotseat.getAlpha() != 1f) {
2726 int duration = mSearchDropTargetBar.getTransitionInDuration();
2727 mHotseat.animate().alpha(1f).setDuration(duration);
2728 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002729 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002730 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002731 }
2732 }
2733 }
2734
2735 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002736 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002737 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002738 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002739 if (!LauncherApplication.isScreenLarge()) {
2740 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002741 if (mHotseat.getAlpha() != 0f) {
2742 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2743 mHotseat.animate().alpha(0f).setDuration(duration);
2744 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002745 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002746 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002747 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002748 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002749 }
2750
Patrick Dubroy5f445422011-02-18 14:35:21 -08002751 /**
2752 * Add an item from all apps or customize onto the given workspace screen.
2753 * If layout is null, add to the current screen.
2754 */
2755 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002756 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002757 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002758 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002759 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002760
Winson Chungdff8ebb2011-09-08 17:25:31 -07002761 /** Maps the current orientation to an index for referencing orientation correct global icons */
2762 private int getCurrentOrientationIndexForGlobalIcons() {
2763 // default - 0, landscape - 1
2764 switch (getResources().getConfiguration().orientation) {
2765 case Configuration.ORIENTATION_LANDSCAPE:
2766 return 1;
2767 default:
2768 return 0;
2769 }
2770 }
2771
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002772 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002773 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002774 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002775 // Look for the toolbar icon specified in the activity meta-data
2776 Bundle metaData = packageManager.getActivityInfo(
2777 activityName, PackageManager.GET_META_DATA).metaData;
2778 if (metaData != null) {
2779 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2780 if (iconResId != 0) {
2781 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002782 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002783 }
2784 }
2785 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002786 // This can happen if the activity defines an invalid drawable
2787 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2788 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002789 } catch (Resources.NotFoundException nfe) {
2790 // This can happen if the activity defines an invalid drawable
2791 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2792 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002793 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002794 return null;
2795 }
2796
2797 // if successful in getting icon, return it; otherwise, set button to use default drawable
2798 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2799 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002800 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002801 Resources r = getResources();
2802 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2803 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002804
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002805 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002806 // If we were unable to find the icon via the meta-data, use a generic one
2807 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002808 toolbarIcon = r.getDrawable(fallbackDrawableId);
2809 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002810 if (button != null) {
2811 button.setCompoundDrawables(toolbarIcon, null, null, null);
2812 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002813 return null;
2814 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002815 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002816 if (button != null) {
2817 button.setCompoundDrawables(toolbarIcon, null, null, null);
2818 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002819 return toolbarIcon.getConstantState();
2820 }
2821 }
2822
2823 // if successful in getting icon, return it; otherwise, set button to use default drawable
2824 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2825 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2826 ImageView button = (ImageView) findViewById(buttonId);
2827 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2828
Michael Jurka19e0fc52011-07-22 18:00:21 -07002829 if (button != null) {
2830 // If we were unable to find the icon via the meta-data, use a
2831 // generic one
2832 if (toolbarIcon == null) {
2833 button.setImageResource(fallbackDrawableId);
2834 } else {
2835 button.setImageDrawable(toolbarIcon);
2836 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002837 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002838
2839 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2840
Michael Jurka0423dcf2010-10-05 14:56:18 -07002841 }
2842
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002843 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2844 TextView button = (TextView) findViewById(buttonId);
2845 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2846 }
2847
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002848 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002849 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002850 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002851 }
2852
Winson Chungbb185bd2011-11-21 12:31:42 -08002853 private void invalidatePressedFocusedStates(View container, View button) {
2854 if (container instanceof HolographicLinearLayout) {
2855 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2856 layout.invalidatePressedFocusedStates();
2857 } else if (button instanceof HolographicImageView) {
2858 HolographicImageView view = (HolographicImageView) button;
2859 view.invalidatePressedFocusedStates();
2860 }
2861 }
2862
Winson Chungc51db6a2011-10-05 11:44:49 -07002863 private boolean updateGlobalSearchIcon() {
2864 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002865 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2866 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002867 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002868 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002869
Winson Chung1cad91e2011-05-25 17:41:01 -07002870 final SearchManager searchManager =
2871 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2872 ComponentName activityName = searchManager.getGlobalSearchActivity();
2873 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002874 int coi = getCurrentOrientationIndexForGlobalIcons();
2875 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002876 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002877 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002878 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2879 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002880 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002881 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002882 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002883 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002884 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002885 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2886 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2887 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002888 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002889 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002890 }
2891 }
2892
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002893 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002894 final View searchButtonContainer = findViewById(R.id.search_button_container);
2895 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002896 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002897 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002898 }
2899
Winson Chungc51db6a2011-10-05 11:44:49 -07002900 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002901 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002902 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002903 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002904
Winson Chungc51db6a2011-10-05 11:44:49 -07002905 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002906 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2907 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002908 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002909 int coi = getCurrentOrientationIndexForGlobalIcons();
2910 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002911 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002912 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002913 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002914 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002915 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002916 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002917 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002918 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002919 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002920 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002921 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002922 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002923 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002924
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002925 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002926 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2927 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002928 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002929 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002930 }
2931
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002932 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002933 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002934 */
2935 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002936 final View marketButton = findViewById(R.id.market_button);
2937 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2938 // Find the app market activity by resolving an intent.
2939 // (If multiple app markets are installed, it will return the ResolverActivity.)
2940 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002941 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002942 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002943 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002944 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002945 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002946 marketButton.setVisibility(View.VISIBLE);
2947 } else {
2948 // We should hide and disable the view so that we don't try and restore the visibility
2949 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2950 marketButton.setVisibility(View.GONE);
2951 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002952 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002953 }
2954
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002955 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002956 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002957 }
2958
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002959 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 * Displays the shortcut creation dialog and launches, if necessary, the
2961 * appropriate activity.
2962 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002963 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002964 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2965 DialogInterface.OnShowListener {
2966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002971
Winson Chung55b65502011-05-26 12:03:43 -07002972 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2973 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002974 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 AlertDialog dialog = builder.create();
2977 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002978 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002979 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 return dialog;
2982 }
2983
2984 public void onCancel(DialogInterface dialog) {
2985 mWaitingForResult = false;
2986 cleanup();
2987 }
2988
Romain Guycbb89e42009-06-08 15:52:54 -07002989 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002990 mWaitingForResult = false;
2991 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002992 }
2993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002995 try {
2996 dismissDialog(DIALOG_CREATE_SHORTCUT);
2997 } catch (Exception e) {
2998 // An exception is thrown if the dialog is not visible, which is fine
2999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 }
3001
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003002 /**
3003 * Handle the action clicked in the "Add to home" dialog.
3004 */
3005 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003006 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003007
Winson Chung55b65502011-05-26 12:03:43 -07003008 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
3009 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07003010 case AddAdapter.ITEM_APPLICATION: {
3011 if (mAppsCustomizeTabHost != null) {
3012 mAppsCustomizeTabHost.selectAppsTab();
3013 }
3014 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003015 break;
3016 }
Winson Chung55b65502011-05-26 12:03:43 -07003017 case AddAdapter.ITEM_APPWIDGET: {
3018 if (mAppsCustomizeTabHost != null) {
3019 mAppsCustomizeTabHost.selectWidgetsTab();
3020 }
3021 showAllApps(true);
3022 break;
3023 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003024 case AddAdapter.ITEM_WALLPAPER: {
3025 startWallpaper();
3026 break;
3027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
3029 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003030
3031 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003032 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 }
3035
3036 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003037 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003038 */
3039 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3040 @Override
3041 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003042 closeSystemDialogs();
3043 }
3044 }
3045
3046 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003047 * Receives notifications whenever the appwidgets are reset.
3048 */
3049 private class AppWidgetResetObserver extends ContentObserver {
3050 public AppWidgetResetObserver() {
3051 super(new Handler());
3052 }
3053
3054 @Override
3055 public void onChange(boolean selfChange) {
3056 onAppWidgetReset();
3057 }
3058 }
3059
3060 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003061 * If the activity is currently paused, signal that we need to re-run the loader
3062 * in onResume.
3063 *
3064 * This needs to be called from incoming places where resources might have been loaded
3065 * while we are paused. That is becaues the Configuration might be wrong
3066 * when we're not running, and if it comes back to what it was when we
3067 * were paused, we are not restarted.
3068 *
3069 * Implementation of the method from LauncherModel.Callbacks.
3070 *
3071 * @return true if we are currently paused. The caller might be able to
3072 * skip some work in that case since we will come back again.
3073 */
3074 public boolean setLoadOnResume() {
3075 if (mPaused) {
3076 Log.i(TAG, "setLoadOnResume");
3077 mOnResumeNeedsLoad = true;
3078 return true;
3079 } else {
3080 return false;
3081 }
3082 }
3083
3084 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003088 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003089 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003090 } else {
3091 return SCREEN_COUNT / 2;
3092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003094
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 /**
3096 * Refreshes the shortcuts shown on the workspace.
3097 *
3098 * Implementation of the method from LauncherModel.Callbacks.
3099 */
3100 public void startBinding() {
3101 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003102
Winson Chungf0c6ae02012-03-21 16:10:31 -07003103 mNewShortcutAnimatePage = -1;
3104 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003105 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 int count = workspace.getChildCount();
3107 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003108 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003109 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3110 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003112 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003113 if (mHotseat != null) {
3114 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 }
3116 }
3117
3118 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 * Bind the items start-end from the list.
3120 *
3121 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003124 setLoadOnResume();
3125
Winson Chungf0c6ae02012-03-21 16:10:31 -07003126 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3127 Set<String> newApps = new HashSet<String>();
3128 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3129
3130 Workspace workspace = mWorkspace;
3131 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003133
3134 // Short circuit if we are loading dock items for a configuration which has no dock
3135 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3136 mHotseat == null) {
3137 continue;
3138 }
3139
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 switch (item.itemType) {
3141 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3142 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003143 ShortcutInfo info = (ShortcutInfo) item;
3144 String uri = info.intent.toUri(0).toString();
3145 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003146 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3147 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003148 if (newApps.contains(uri)) {
3149 newApps.remove(uri);
3150
3151 // Prepare the view to be animated up
3152 shortcut.setAlpha(0f);
3153 shortcut.setScaleX(0f);
3154 shortcut.setScaleY(0f);
3155 mNewShortcutAnimatePage = item.screen;
3156 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3157 mNewShortcutAnimateViews.add(shortcut);
3158 }
3159 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003160 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003161 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003162 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003163 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003164 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003165 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3166 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003170
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 /**
3175 * Implementation of the method from LauncherModel.Callbacks.
3176 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003177 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003178 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003179 sFolders.clear();
3180 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182
3183 /**
3184 * Add the views for a widget to the workspace.
3185 *
3186 * Implementation of the method from LauncherModel.Callbacks.
3187 */
3188 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003189 setLoadOnResume();
3190
Daniel Sandler843e8602010-06-07 14:59:01 -04003191 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3192 if (DEBUG_WIDGETS) {
3193 Log.d(TAG, "bindAppWidget: " + item);
3194 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195 final Workspace workspace = mWorkspace;
3196
3197 final int appWidgetId = item.appWidgetId;
3198 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003199 if (DEBUG_WIDGETS) {
3200 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3201 }
3202
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3204
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3206 item.hostView.setTag(item);
3207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 item.cellY, item.spanX, item.spanY, false);
3210
Adam Cohended9f8d2010-11-03 13:25:16 -07003211 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3212
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 workspace.requestLayout();
3214
Daniel Sandler843e8602010-06-07 14:59:01 -04003215 if (DEBUG_WIDGETS) {
3216 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3217 + (SystemClock.uptimeMillis()-start) + "ms");
3218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 }
3220
3221 /**
3222 * Callback saying that there aren't any more items to bind.
3223 *
3224 * Implementation of the method from LauncherModel.Callbacks.
3225 */
3226 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003227 setLoadOnResume();
3228
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 if (mSavedState != null) {
3230 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003231 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003232 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003233 mSavedState = null;
3234 }
3235
3236 if (mSavedInstanceState != null) {
3237 super.onRestoreInstanceState(mSavedInstanceState);
3238 mSavedInstanceState = null;
3239 }
3240
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003241 // If we received the result of any pending adds while the loader was running (e.g. the
3242 // widget configuration forced an orientation change), process them now.
3243 for (int i = 0; i < sPendingAddList.size(); i++) {
3244 completeAdd(sPendingAddList.get(i));
3245 }
3246 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247
Winson Chungc51db6a2011-10-05 11:44:49 -07003248 // Update the market app icon as necessary (the other icons will be managed in response to
3249 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003250 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003251
Winson Chungf0c6ae02012-03-21 16:10:31 -07003252 // Animate up any icons as necessary
3253 if (mVisible || mWorkspaceLoading) {
3254 Runnable newAppsRunnable = new Runnable() {
3255 @Override
3256 public void run() {
3257 runNewAppsAnimation();
3258 }
3259 };
3260 if (mNewShortcutAnimatePage > -1 &&
3261 mNewShortcutAnimatePage != mWorkspace.getCurrentPage()) {
3262 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3263 } else {
3264 newAppsRunnable.run();
3265 }
3266 }
3267
3268 mWorkspaceLoading = false;
3269 }
3270
3271 /**
3272 * Runs a new animation that scales up icons that were added while Launcher was in the
3273 * background.
3274 */
3275 private void runNewAppsAnimation() {
3276 AnimatorSet anim = new AnimatorSet();
3277 Collection<Animator> bounceAnims = new ArrayList<Animator>();
3278 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3279 @Override
3280 public int compare(View a, View b) {
3281 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3282 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3283 int cellCountX = LauncherModel.getCellCountX();
3284 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3285 }
3286 });
3287 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3288 View v = mNewShortcutAnimateViews.get(i);
3289 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3290 PropertyValuesHolder.ofFloat("alpha", 1f),
3291 PropertyValuesHolder.ofFloat("scaleX", 1f),
3292 PropertyValuesHolder.ofFloat("scaleY", 1f));
3293 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3294 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3295 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3296 bounceAnims.add(bounceAnim);
3297 }
3298 anim.playTogether(bounceAnims);
3299 anim.addListener(new AnimatorListenerAdapter() {
3300 @Override
3301 public void onAnimationEnd(Animator animation) {
3302 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3303 }
3304 });
3305 anim.start();
3306
3307 // Clean up
3308 mNewShortcutAnimatePage = -1;
3309 mNewShortcutAnimateViews.clear();
3310 new Thread("clearNewAppsThread") {
3311 public void run() {
3312 mSharedPrefs.edit()
3313 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3314 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3315 .commit();
3316 }
3317 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003318 }
3319
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003320 @Override
3321 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003322 boolean searchVisible = updateGlobalSearchIcon();
3323 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3324 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003325 }
3326
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003327 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 * Add the icons for all apps.
3329 *
3330 * Implementation of the method from LauncherModel.Callbacks.
3331 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003332 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3333 // Remove the progress bar entirely; we could also make it GONE
3334 // but better to remove it since we know it's not going to be used
3335 View progressBar = mAppsCustomizeTabHost.
3336 findViewById(R.id.apps_customize_progress_bar);
3337 if (progressBar != null) {
3338 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003339 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003340 // We just post the call to setApps so the user sees the progress bar
3341 // disappear-- otherwise, it just looks like the progress bar froze
3342 // which doesn't look great
3343 mAppsCustomizeTabHost.post(new Runnable() {
3344 public void run() {
3345 if (mAppsCustomizeContent != null) {
3346 mAppsCustomizeContent.setApps(apps);
3347 }
3348 }
3349 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 }
3351
3352 /**
3353 * A package was installed.
3354 *
3355 * Implementation of the method from LauncherModel.Callbacks.
3356 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003357 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003358 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003360
Winson Chung785d2eb2011-04-14 16:08:02 -07003361 if (mAppsCustomizeContent != null) {
3362 mAppsCustomizeContent.addApps(apps);
3363 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 }
3365
3366 /**
3367 * A package was updated.
3368 *
3369 * Implementation of the method from LauncherModel.Callbacks.
3370 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003371 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003372 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003373 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003374 if (mWorkspace != null) {
3375 mWorkspace.updateShortcuts(apps);
3376 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003377
Winson Chung785d2eb2011-04-14 16:08:02 -07003378 if (mAppsCustomizeContent != null) {
3379 mAppsCustomizeContent.updateApps(apps);
3380 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 }
3382
3383 /**
3384 * A package was uninstalled.
3385 *
3386 * Implementation of the method from LauncherModel.Callbacks.
3387 */
Joe Onorato36115782010-06-17 13:28:48 -04003388 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003390 if (permanent) {
3391 mWorkspace.removeItems(apps);
3392 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003393
Winson Chung785d2eb2011-04-14 16:08:02 -07003394 if (mAppsCustomizeContent != null) {
3395 mAppsCustomizeContent.removeApps(apps);
3396 }
Winson Chunga1820962011-10-03 16:31:06 -07003397
3398 // Notify the drag controller
3399 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003400 }
Joe Onoratobe386092009-11-17 17:32:16 -08003401
3402 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003403 * A number of packages were updated.
3404 */
3405 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003406 if (mAppsCustomizeContent != null) {
3407 mAppsCustomizeContent.onPackagesUpdated();
3408 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003409 }
3410
Winson Chung400438b2011-01-16 17:53:48 -08003411 private int mapConfigurationOriActivityInfoOri(int configOri) {
3412 final Display d = getWindowManager().getDefaultDisplay();
3413 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3414 switch (d.getRotation()) {
3415 case Surface.ROTATION_0:
3416 case Surface.ROTATION_180:
3417 // We are currently in the same basic orientation as the natural orientation
3418 naturalOri = configOri;
3419 break;
3420 case Surface.ROTATION_90:
3421 case Surface.ROTATION_270:
3422 // We are currently in the other basic orientation to the natural orientation
3423 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3424 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3425 break;
3426 }
3427
3428 int[] oriMap = {
3429 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3430 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3431 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3432 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3433 };
3434 // Since the map starts at portrait, we need to offset if this device's natural orientation
3435 // is landscape.
3436 int indexOffset = 0;
3437 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3438 indexOffset = 1;
3439 }
3440 return oriMap[(d.getRotation() + indexOffset) % 4];
3441 }
Adam Cohen446e9402011-09-15 18:21:21 -07003442
3443 public void lockScreenOrientationOnLargeUI() {
3444 if (LauncherApplication.isScreenLarge()) {
3445 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3446 .getConfiguration().orientation));
3447 }
Winson Chung400438b2011-01-16 17:53:48 -08003448 }
Adam Cohen446e9402011-09-15 18:21:21 -07003449 public void unlockScreenOrientationOnLargeUI() {
3450 if (LauncherApplication.isScreenLarge()) {
3451 mHandler.postDelayed(new Runnable() {
3452 public void run() {
3453 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3454 }
3455 }, mRestoreScreenOrientationDelay);
3456 }
Winson Chung400438b2011-01-16 17:53:48 -08003457 }
3458
Winson Chung82f55532011-08-09 14:14:23 -07003459 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003460 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003461 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003462 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003463
Winson Chung7d7541e2011-09-16 20:14:36 -07003464 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003465 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003466 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3467 Cling cling = (Cling) findViewById(clingId);
3468 if (cling != null) {
3469 cling.init(this, positionData);
3470 cling.setVisibility(View.VISIBLE);
3471 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3472 if (animate) {
3473 cling.buildLayer();
3474 cling.setAlpha(0f);
3475 cling.animate()
3476 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003477 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003478 .setDuration(SHOW_CLING_DURATION)
3479 .setStartDelay(delay)
3480 .start();
3481 } else {
3482 cling.setAlpha(1f);
3483 }
3484 }
3485 return cling;
3486 }
3487 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003488 if (cling != null) {
3489 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003490 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003491 anim.addListener(new AnimatorListenerAdapter() {
3492 public void onAnimationEnd(Animator animation) {
3493 cling.setVisibility(View.GONE);
3494 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003495 // We should update the shared preferences on a background thread
3496 new Thread("dismissClingThread") {
3497 public void run() {
3498 SharedPreferences.Editor editor = mSharedPrefs.edit();
3499 editor.putBoolean(flag, true);
3500 editor.commit();
3501 }
3502 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003503 };
3504 });
3505 anim.start();
3506 }
3507 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003508 private void removeCling(int id) {
3509 final View cling = findViewById(id);
3510 if (cling != null) {
3511 final ViewGroup parent = (ViewGroup) cling.getParent();
3512 parent.post(new Runnable() {
3513 @Override
3514 public void run() {
3515 parent.removeView(cling);
3516 }
3517 });
3518 }
3519 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003520 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003521 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003522 if (isClingsEnabled() &&
3523 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003524 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003525 } else {
3526 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003527 }
3528 }
3529 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003530 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003531 if (isClingsEnabled() &&
3532 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003533 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003534 } else {
3535 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003536 }
3537 }
3538 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003539 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003540 if (isClingsEnabled() &&
3541 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3542 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003543 } else {
3544 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003545 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003546 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003547 }
3548 public boolean isFolderClingVisible() {
3549 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003550 if (cling != null) {
3551 return cling.getVisibility() == View.VISIBLE;
3552 }
3553 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003554 }
Winson Chung82f55532011-08-09 14:14:23 -07003555 public void dismissWorkspaceCling(View v) {
3556 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003557 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003558 }
3559 public void dismissAllAppsCling(View v) {
3560 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003561 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3562 }
3563 public void dismissFolderCling(View v) {
3564 Cling cling = (Cling) findViewById(R.id.folder_cling);
3565 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003566 }
3567
Winson Chung80baf5a2010-08-09 16:03:15 -07003568 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003569 * Prints out out state for debugging.
3570 */
3571 public void dumpState() {
3572 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003573 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003574 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3575 Log.d(TAG, "mRestoring=" + mRestoring);
3576 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3577 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003578 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003579 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003580
Winson Chung785d2eb2011-04-14 16:08:02 -07003581 if (mAppsCustomizeContent != null) {
3582 mAppsCustomizeContent.dumpState();
3583 }
Joe Onoratobe386092009-11-17 17:32:16 -08003584 Log.d(TAG, "END launcher2 dump state");
3585 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003586
3587 @Override
3588 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3589 super.dump(prefix, fd, writer, args);
3590 writer.println(" ");
3591 writer.println("Debug logs: ");
3592 for (int i = 0; i < sDumpLogs.size(); i++) {
3593 writer.println(" " + sDumpLogs.get(i));
3594 }
3595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596}
Michael Jurka2763be32011-02-24 11:19:57 -08003597
Michael Jurkaabded662011-03-04 12:06:57 -08003598interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003599 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003600 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003601 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003602 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003603}