Launcher.java revision a33411cb83e2c7f822d6486183a7a4ea7d1b09e6
101f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka
231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/*
331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * you may not use this file except in compliance with the License.
731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * You may obtain a copy of the License at
831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
1031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project *
1131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * See the License for the specific language governing permissions and
1531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * limitations under the License.
1631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
1731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
18a5902524d4403885eb4c50360bf3465c6be796efJoe Onoratopackage com.android.launcher2;
1931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
20974c3864bb438fc924913e0017385f873332d052Michael Jurkaimport android.accounts.Account;
21974c3864bb438fc924913e0017385f873332d052Michael Jurkaimport android.accounts.AccountManager;
22dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunneimport android.animation.Animator;
238edd75c8bb0729a10cb39f614183e3e9ae4288e8Michael Jurkaimport android.animation.AnimatorListenerAdapter;
24dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunneimport android.animation.AnimatorSet;
252801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohenimport android.animation.ObjectAnimator;
262801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohenimport android.animation.PropertyValuesHolder;
27dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunneimport android.animation.ValueAnimator;
287044272f180d34ab29866f6c59ee75376e8a1df5Winson Chungimport android.animation.ValueAnimator.AnimatorUpdateListener;
29946ad470c72a3caa7568d11836c182b7f84d840dMichael Jurkaimport android.app.Activity;
302a9e228e6fd719732639beb880d6df64d48158a1Brett Chabotimport android.app.ActivityManager;
31c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chungimport android.app.ActivityOptions;
3231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.app.SearchManager;
330cf2a7c3eeb75cbd532b011524140a4b963f2c93Adam Cohenimport android.appwidget.AppWidgetHostView;
34af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.appwidget.AppWidgetManager;
35af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.appwidget.AppWidgetProviderInfo;
3631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.ActivityNotFoundException;
372ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onoratoimport android.content.BroadcastReceiver;
38e326f186af6b00e4ea32849f1527254c669d0600Michael Jurkaimport android.content.ComponentCallbacks2;
39c9b1877f9acf604897c719d70dc99685d10849b5Daniel Sandlerimport android.content.ComponentName;
401e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkeyimport android.content.ContentResolver;
4131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Context;
4231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.Intent;
43f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chungimport android.content.IntentFilter;
4482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chungimport android.content.SharedPreferences;
45aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chungimport android.content.pm.ActivityInfo;
4631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.pm.PackageManager;
47716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohenimport android.content.pm.PackageManager.NameNotFoundException;
4831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.content.res.Configuration;
49138a04170d964da9cdec228e95b976875ae52481Karl Rosaenimport android.content.res.Resources;
501e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkeyimport android.database.ContentObserver;
51268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohenimport android.graphics.Bitmap;
52268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohenimport android.graphics.Canvas;
53268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohenimport android.graphics.PorterDuff;
54af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.graphics.Rect;
55af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.graphics.drawable.Drawable;
56c9b1877f9acf604897c719d70dc99685d10849b5Daniel Sandlerimport android.net.Uri;
57319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrickimport android.os.AsyncTask;
5831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Bundle;
590fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauerimport android.os.Environment;
601e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkeyimport android.os.Handler;
61ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohenimport android.os.Message;
62a2413751e3a698aef9c87411a639637883856939Winson Chungimport android.os.StrictMode;
63843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandlerimport android.os.SystemClock;
644ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroyimport android.provider.Settings;
656d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasaniimport android.speech.RecognizerIntent;
6631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.Selection;
6731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.SpannableStringBuilder;
68a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurkaimport android.text.TextUtils;
6931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.method.TextKeyListener;
707c312c15e33b6cffbda707429e88ade498fc9f5eJoe Onoratoimport android.util.Log;
71400438b79fe412cb625c96297edeea9c6155349eWinson Chungimport android.view.Display;
720d44e9482b95d8f163b28bf20131c4349185b589Joe Onoratoimport android.view.HapticFeedbackConstants;
7331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.KeyEvent;
7431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.LayoutInflater;
7531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.Menu;
7631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.MenuItem;
770e26059548e429e5d1c973bebe4c561bead2926fMichael Jurkaimport android.view.MotionEvent;
78400438b79fe412cb625c96297edeea9c6155349eWinson Chungimport android.view.Surface;
7931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.View;
800cf2a7c3eeb75cbd532b011524140a4b963f2c93Adam Cohenimport android.view.View.OnLongClickListener;
8182f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chungimport android.view.ViewGroup;
82b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurkaimport android.view.ViewTreeObserver;
832a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurkaimport android.view.ViewTreeObserver.OnGlobalLayoutListener;
84bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackbornimport android.view.WindowManager;
85815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
86cff6af81d721415785bddc2efd74e179d2def9daAdam Cohenimport android.view.animation.AccelerateDecelerateInterpolator;
877a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chungimport android.view.animation.AccelerateInterpolator;
88f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohenimport android.view.animation.DecelerateInterpolator;
8931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.inputmethod.InputMethodManager;
90ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohenimport android.widget.Advanceable;
918a73c51ee87b6d9b12daba188034889caf7a905bRomain Guyimport android.widget.ImageView;
9268846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport android.widget.TextView;
9368846fdce6c01bbe474bd0c8307e1ccaac161cbcWinson Chungimport android.widget.Toast;
944ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy
95df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport com.android.common.Search;
96df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohenimport com.android.launcher.R;
97558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohenimport com.android.launcher2.DropTarget.DragObject;
98edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
99c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.DataInputStream;
100c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.DataOutputStream;
10116d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohenimport java.io.FileDescriptor;
1029bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurkaimport java.io.FileInputStream;
103c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.FileNotFoundException;
104c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.IOException;
10516d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohenimport java.io.PrintWriter;
106c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.util.ArrayList;
107f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chungimport java.util.Collection;
108f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chungimport java.util.Collections;
109f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chungimport java.util.Comparator;
110c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.util.HashMap;
111f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chungimport java.util.HashSet;
112d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurkaimport java.util.List;
113f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chungimport java.util.Set;
114c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohen
11531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project/**
11631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project * Default launcher application.
11731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project */
118946ad470c72a3caa7568d11836c182b7f84d840dMichael Jurkapublic final class Launcher extends Activity
1190e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
1200e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka                   AllAppsView.Watcher, View.OnTouchListener {
121a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final String TAG = "Launcher";
122cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    static final boolean LOGD = false;
12331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final boolean PROFILE_STARTUP = false;
125843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler    static final boolean DEBUG_WIDGETS = false;
126a2413751e3a698aef9c87411a639637883856939Winson Chung    static final boolean DEBUG_STRICT_MODE = false;
1276fefcf1f83ba05e898ea7164a68dcced657bb43eRomain Guy
12870d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_GROUP_WALLPAPER = 1;
12970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
13070d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
131236d431cc582df9b2841718195ae977c3948f751Winson Chung    private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
132236d431cc582df9b2841718195ae977c3948f751Winson Chung    private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
13331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_CREATE_SHORTCUT = 1;
1357376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private static final int REQUEST_CREATE_APPWIDGET = 5;
13631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_PICK_APPLICATION = 6;
13731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_PICK_SHORTCUT = 7;
1387376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private static final int REQUEST_PICK_APPWIDGET = 9;
139a0618e4102d0543d953d81e162d93c8ea641d0fdMike Cleron    private static final int REQUEST_PICK_WALLPAPER = 10;
14031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1418b805b17158886035b38261eb611d8641701ae43Michael Jurka    private static final int REQUEST_BIND_APPWIDGET = 11;
1428b805b17158886035b38261eb611d8641701ae43Michael Jurka
14331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
14431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1453a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    static final int SCREEN_COUNT = 5;
1463a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    static final int DEFAULT_SCREEN = 2;
14731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static final String PREFERENCES = "launcher.preferences";
149f3bade67262f958b892b29aea770151132886ec9Michael Jurka    static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
150f3bade67262f958b892b29aea770151132886ec9Michael Jurka    static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
15131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1522672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung    // The Intent extra that defines whether to ignore the launch animation
1532672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung    static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
1542672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
1552672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung
15631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
15731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
158883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    // Type: int
159883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    private static final String RUNTIME_STATE = "launcher.state";
16031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
1613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
1623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    // Type: int
16331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
16431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
165fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
16631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
167fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
16831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: boolean
16931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
17031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: long
17131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
1729d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: int
1739d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
1749d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: int
1759d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
1769d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: parcelable
1779d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
17831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
179ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
180ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
181ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            "com.android.launcher.toolbar_search_icon";
182ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
183ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            "com.android.launcher.toolbar_voice_search_icon";
184ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
1856b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    /** The different states that Launcher can be in. */
186f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
187c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    private State mState = State.WORKSPACE;
188c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    private AnimatorSet mStateAnimation;
189b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private AnimatorSet mDividerAnimator;
1906b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
1919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final int APPWIDGET_HOST_ID = 1024;
192557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
193557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
1947a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chung    private static final int SHOW_CLING_DURATION = 550;
1957d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private static final int DISMISS_CLING_DURATION = 250;
19631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final Object sLock = new Object();
1983a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    private static int sScreen = DEFAULT_SCREEN;
19931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
200a2413751e3a698aef9c87411a639637883856939Winson Chung    // How long to wait before the new-shortcut animation automatically pans the workspace
201a2413751e3a698aef9c87411a639637883856939Winson Chung    private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
202a2413751e3a698aef9c87411a639637883856939Winson Chung
2032ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    private final BroadcastReceiver mCloseSystemDialogsReceiver
2042ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            = new CloseSystemDialogsIntentReceiver();
2051e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
2061e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
20731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private LayoutInflater mInflater;
20831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
20931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Workspace mWorkspace;
210b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private View mQsbDivider;
211b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private View mDockDivider;
212b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private DragLayer mDragLayer;
213b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private DragController mDragController;
214cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
2157376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private AppWidgetManager mAppWidgetManager;
2167376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private LauncherAppWidgetHost mAppWidgetHost;
217cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
218c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    private ItemInfo mPendingAddInfo = new ItemInfo();
2199d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private AppWidgetProviderInfo mPendingAddWidgetInfo;
2209d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen
2210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private int[] mTmpAddItemCellCoordinates = new int[2];
2220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
22331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private FolderInfo mFolderInfo;
22431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private Hotseat mHotseat;
226f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    private View mAllAppsButton;
2273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
228c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private SearchDropTargetBar mSearchDropTargetBar;
229785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    private AppsCustomizeTabHost mAppsCustomizeTabHost;
230785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    private AppsCustomizePagedView mAppsCustomizeContent;
231ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private boolean mAutoAdvanceRunning = false;
2322b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
23331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Bundle mSavedState;
23431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
23531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private SpannableStringBuilder mDefaultKeySsb = null;
23631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private boolean mWorkspaceLoading = true;
2389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
23934a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato    private boolean mPaused = true;
24031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean mRestoring;
24131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean mWaitingForResult;
242ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    private boolean mOnResumeNeedsLoad;
24331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Bundle mSavedInstanceState;
24531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private LauncherModel mModel;
2470589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
248d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mUserPresent = true;
249d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mVisible = false;
250d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mAttached = false;
2519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
252319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick    private static LocaleConfiguration sLocaleConfiguration = null;
253319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
254319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick    private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
255cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
256ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    private Intent mAppMarketIntent = null;
257ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
258ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    // Related to the auto-advancing of widgets
259ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int ADVANCE_MSG = 1;
260ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int mAdvanceInterval = 20000;
261ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int mAdvanceStagger = 250;
262ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private long mAutoAdvanceSentTime;
263ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private long mAutoAdvanceTimeLeft = -1;
264ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
265ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        new HashMap<View, AppWidgetProviderInfo>();
266ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
267400438b79fe412cb625c96297edeea9c6155349eWinson Chung    // Determines how long to wait after a rotation before restoring the screen orientation to
268400438b79fe412cb625c96297edeea9c6155349eWinson Chung    // match the sensor state.
269400438b79fe412cb625c96297edeea9c6155349eWinson Chung    private final int mRestoreScreenOrientationDelay = 500;
270400438b79fe412cb625c96297edeea9c6155349eWinson Chung
2714ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    // External icons saved in case of resource changes, orientation, etc.
272dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
273dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
274dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
2754ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
27616d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    static final ArrayList<String> sDumpLogs = new ArrayList<String>();
27716d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen
27846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    // We only want to get the SharedPreferences once since it does an FS stat each time we get
27946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    // it from the context.
28046353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    private SharedPreferences mSharedPrefs;
28146353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung
282f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    // Holds the page that we need to animate to, and the icon views that we need to animate up
283f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    // when we scroll to that page on resume.
284f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    private int mNewShortcutAnimatePage = -1;
285f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
286268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private ImageView mFolderIconImageView;
287268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Bitmap mFolderIconBitmap;
288268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Canvas mFolderIconCanvas;
289268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Rect mRectForFolderAnimation = new Rect();
2902801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
291ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka    private BubbleTextView mWaitingForResume;
292ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka
293c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    private Runnable mBuildLayersRunnable = new Runnable() {
294c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        public void run() {
2959d906c7742af73160df257a40a702e1cc4f87899Michael Jurka            if (mWorkspace != null) {
2969d906c7742af73160df257a40a702e1cc4f87899Michael Jurka                mWorkspace.buildPageHardwareLayers();
2979d906c7742af73160df257a40a702e1cc4f87899Michael Jurka            }
298c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        }
299c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    };
300c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka
301002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    private static ArrayList<PendingAddArguments> sPendingAddList
302002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            = new ArrayList<PendingAddArguments>();
303002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
304002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    private static class PendingAddArguments {
305002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int requestCode;
306002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        Intent intent;
3073d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        long container;
308002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int screen;
309002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int cellX;
310002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int cellY;
311002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    }
312002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
3139bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka
3149bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka    private boolean doesFileExist(String filename) {
3159bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        FileInputStream fis = null;
3169bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        try {
3179bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka            fis = openFileInput(filename);
3189bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka            fis.close();
3199bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka            return true;
3209bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        } catch (java.io.FileNotFoundException e) {
3219bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka            return false;
3229bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        } catch (java.io.IOException e) {
3239bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka            return true;
3249bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        }
3259bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka    }
3269bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka
32731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
32831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onCreate(Bundle savedInstanceState) {
329a2413751e3a698aef9c87411a639637883856939Winson Chung        if (DEBUG_STRICT_MODE) {
330a2413751e3a698aef9c87411a639637883856939Winson Chung            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
331a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectDiskReads()
332a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectDiskWrites()
333a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectNetwork()   // or .detectAll() for all detectable problems
334a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyLog()
335a2413751e3a698aef9c87411a639637883856939Winson Chung                    .build());
336a2413751e3a698aef9c87411a639637883856939Winson Chung            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
337a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectLeakedSqlLiteObjects()
338a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectLeakedClosableObjects()
339a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyLog()
340a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyDeath()
341a2413751e3a698aef9c87411a639637883856939Winson Chung                    .build());
342a2413751e3a698aef9c87411a639637883856939Winson Chung        }
343a2413751e3a698aef9c87411a639637883856939Winson Chung
34431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onCreate(savedInstanceState);
3450589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        LauncherApplication app = ((LauncherApplication)getApplication());
346f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
347f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                Context.MODE_PRIVATE);
3480589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mModel = app.setLauncher(this);
3490589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = app.getIconCache();
35041a12d225a546b0a193628a4e5dd6b62cc60b7d0Joe Onorato        mDragController = new DragController(this);
35131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mInflater = getLayoutInflater();
352cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3537376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetManager = AppWidgetManager.getInstance(this);
3547376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
3557376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetHost.startListening();
356cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
35731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (PROFILE_STARTUP) {
3580fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauer            android.os.Debug.startMethodTracing(
3590fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauer                    Environment.getExternalStorageDirectory() + "/launcher");
36031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
36131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
36231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        checkForLocaleChange();
36331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        setContentView(R.layout.launcher);
36431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        setupViews();
3657d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        showFirstRunWorkspaceCling();
36631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3671e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        registerContentObservers();
3681e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
3697c312c15e33b6cffbda707429e88ade498fc9f5eJoe Onorato        lockAllApps();
3707404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato
37131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mSavedState = savedInstanceState;
37231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        restoreState(mSavedState);
37331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
374a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung        // Update customization drawer _after_ restoring the states
375785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
376785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.onPackagesUpdated();
377785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
378a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung
37931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (PROFILE_STARTUP) {
38031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            android.os.Debug.stopMethodTracing();
38131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
38231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
38331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
384f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mModel.startLoader(true);
38531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
38631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
387c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        if (!mModel.isAllAppsLoaded()) {
388c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
389c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
390c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        }
391c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka
39231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // For handling default keys
39331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDefaultKeySsb = new SpannableStringBuilder();
39431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        Selection.setSelection(mDefaultKeySsb, 0);
39534a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato
39634a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
39734a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        registerReceiver(mCloseSystemDialogsReceiver, filter);
3984ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
399f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        updateGlobalIcons();
400f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
401f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // On large interfaces, we want the screen to auto-rotate based on the current orientation
402f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        unlockScreenOrientation(true);
403f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen    }
404f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
405f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen    private void updateGlobalIcons() {
406c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean searchVisible = false;
407c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean voiceVisible = false;
4084ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        // If we have a saved version of these external icons, we load them up immediately
409dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        int coi = getCurrentOrientationIndexForGlobalIcons();
410dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
411dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                sAppMarketIcon[coi] == null) {
412c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            updateAppMarketIcon();
413c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchVisible = updateGlobalSearchIcon();
414c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            voiceVisible = updateVoiceSearchIcon(searchVisible);
415f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
416dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sGlobalSearchIcon[coi] != null) {
417dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung             updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
418c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung             searchVisible = true;
419f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
420dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sVoiceSearchIcon[coi] != null) {
421dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
422c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            voiceVisible = true;
423f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
424dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sAppMarketIcon[coi] != null) {
425dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            updateAppMarketIcon(sAppMarketIcon[coi]);
4264ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        }
427c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
42831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
429cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
43031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void checkForLocaleChange() {
431319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        if (sLocaleConfiguration == null) {
432319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            new AsyncTask<Void, Void, LocaleConfiguration>() {
433319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                @Override
434319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                protected LocaleConfiguration doInBackground(Void... unused) {
435319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
436319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    readConfiguration(Launcher.this, localeConfiguration);
437319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    return localeConfiguration;
438319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
439319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
440319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                @Override
441319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                protected void onPostExecute(LocaleConfiguration result) {
442319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    sLocaleConfiguration = result;
443319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    checkForLocaleChange();  // recursive, but now with a locale configuration
444319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
445319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            }.execute();
446319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            return;
447319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        }
448fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
44931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final Configuration configuration = getResources().getConfiguration();
45031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
451319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final String previousLocale = sLocaleConfiguration.locale;
45231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final String locale = configuration.locale.toString();
45331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
454319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final int previousMcc = sLocaleConfiguration.mcc;
45531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final int mcc = configuration.mcc;
45631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
457319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final int previousMnc = sLocaleConfiguration.mnc;
45831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final int mnc = configuration.mnc;
45931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
46084f296c106cb1c7b6d3ae6c6d5508a17f1324e29Romain Guy        boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
46131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
46284f296c106cb1c7b6d3ae6c6d5508a17f1324e29Romain Guy        if (localeChanged) {
463319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.locale = locale;
464319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.mcc = mcc;
465319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.mnc = mnc;
46698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
4670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            mIconCache.flush();
468319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
469319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
470319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            new Thread("WriteLocaleConfiguration") {
471dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
472319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                public void run() {
473319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    writeConfiguration(Launcher.this, localeConfiguration);
474319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
475319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            }.start();
47698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        }
47798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
47898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
47998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static class LocaleConfiguration {
48098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public String locale;
48198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public int mcc = -1;
48298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public int mnc = -1;
48398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
484fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
48598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
48698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        DataInputStream in = null;
48798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        try {
48898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            in = new DataInputStream(context.openFileInput(PREFERENCES));
48998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.locale = in.readUTF();
49098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.mcc = in.readInt();
49198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.mnc = in.readInt();
49298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (FileNotFoundException e) {
49398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
49498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (IOException e) {
49598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
49698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } finally {
49798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            if (in != null) {
49898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                try {
49998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    in.close();
50098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                } catch (IOException e) {
50198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    // Ignore
50298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                }
50398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            }
50498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        }
50598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
50698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
50798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
50898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        DataOutputStream out = null;
50998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        try {
51098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
51198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeUTF(configuration.locale);
51298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeInt(configuration.mcc);
51398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeInt(configuration.mnc);
51498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.flush();
51598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (FileNotFoundException e) {
51698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
51798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (IOException e) {
51898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            //noinspection ResultOfMethodCallIgnored
51998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            context.getFileStreamPath(PREFERENCES).delete();
52098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } finally {
52198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            if (out != null) {
52298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                try {
52398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    out.close();
52498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                } catch (IOException e) {
52598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    // Ignore
52698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                }
52798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            }
52831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
52931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
53031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
531716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen    public DragLayer getDragLayer() {
532716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        return mDragLayer;
533716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen    }
534716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen
53536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isDraggingEnabled() {
53636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        // We prevent dragging when we are loading the workspace as it is possible to pick up a view
53736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        // that is subsequently removed from the workspace in startBinding().
53836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return !mModel.isLoadingWorkspace();
53936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
54036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
54131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static int getScreen() {
54231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        synchronized (sLock) {
54331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return sScreen;
54431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
54531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
54631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
54731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static void setScreen(int screen) {
54831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        synchronized (sLock) {
54931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            sScreen = screen;
55031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
55131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
55231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
553557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    /**
554557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
555557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     * a configuration step, this allows the proper animations to run after other transitions.
556557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     */
557557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private boolean completeAdd(PendingAddArguments args) {
558b8472bb83cd96456a543137e1e56d589171934c3Winson Chung        boolean result = false;
559002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        switch (args.requestCode) {
560002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_APPLICATION:
5613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                completeAddApplication(args.intent, args.container, args.screen, args.cellX,
5623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        args.cellY);
563002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
564002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_SHORTCUT:
565002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                processShortcut(args.intent);
566002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
567002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_CREATE_SHORTCUT:
5683d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
5693d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        args.cellY);
570b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                result = true;
571b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                break;
572002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_CREATE_APPWIDGET:
573002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
574ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
575b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                result = true;
576b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                break;
577002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_WALLPAPER:
578002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                // We just wanted the activity result here so we can clear mWaitingForResult
579002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
580002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        }
58127614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
58227614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // if you turned the screen off and then back while in All Apps, Launcher would not
58327614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // return to the workspace. Clearing mAddInfo.container here fixes this issue
58427614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        resetAddInfo();
585b8472bb83cd96456a543137e1e56d589171934c3Winson Chung        return result;
586002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    }
587002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
58831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
5898b805b17158886035b38261eb611d8641701ae43Michael Jurka    protected void onActivityResult(
5908b805b17158886035b38261eb611d8641701ae43Michael Jurka            final int requestCode, final int resultCode, final Intent data) {
5918b805b17158886035b38261eb611d8641701ae43Michael Jurka        if (requestCode == REQUEST_BIND_APPWIDGET) {
5928b805b17158886035b38261eb611d8641701ae43Michael Jurka            int appWidgetId = data != null ?
5938b805b17158886035b38261eb611d8641701ae43Michael Jurka                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
5948b805b17158886035b38261eb611d8641701ae43Michael Jurka            if (resultCode == RESULT_CANCELED) {
5958b805b17158886035b38261eb611d8641701ae43Michael Jurka                completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
5968b805b17158886035b38261eb611d8641701ae43Michael Jurka            } else if (resultCode == RESULT_OK) {
5979d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
5988b805b17158886035b38261eb611d8641701ae43Michael Jurka            }
5998b805b17158886035b38261eb611d8641701ae43Michael Jurka            return;
6008b805b17158886035b38261eb611d8641701ae43Michael Jurka        }
601557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        boolean delayExitSpringLoadedMode = false;
602ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
603ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                requestCode == REQUEST_CREATE_APPWIDGET);
604aad5ef44c665fc4ce143a61c009dc04be3ab0437Romain Guy        mWaitingForResult = false;
605aad5ef44c665fc4ce143a61c009dc04be3ab0437Romain Guy
606ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // We have special handling for widgets
607ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (isWidgetDrop) {
608ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            int appWidgetId = data != null ?
609ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
6105aaab774cfb83833197dac553d4a6eb769032146Winson Chung            if (appWidgetId < 0) {
6115aaab774cfb83833197dac553d4a6eb769032146Winson Chung                Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
6125aaab774cfb83833197dac553d4a6eb769032146Winson Chung                        "widget configuration activity.");
6135aaab774cfb83833197dac553d4a6eb769032146Winson Chung                completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
6145aaab774cfb83833197dac553d4a6eb769032146Winson Chung            } else {
6155aaab774cfb83833197dac553d4a6eb769032146Winson Chung                completeTwoStageWidgetDrop(resultCode, appWidgetId);
6165aaab774cfb83833197dac553d4a6eb769032146Winson Chung            }
617ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            return;
618ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
619ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
62031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // The pattern used here is that a user PICKs a specific application,
62131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // which, depending on the target, might need to CREATE the actual target.
622cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
62331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
62431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // launch over to the Music app to actually CREATE_SHORTCUT.
625c7da555dc5b252042dff0a6920efb1bfeb4a0c9aWinson Chung        if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
626002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            final PendingAddArguments args = new PendingAddArguments();
627002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            args.requestCode = requestCode;
628002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            args.intent = data;
6293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.container = mPendingAddInfo.container;
6303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.screen = mPendingAddInfo.screen;
6313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.cellX = mPendingAddInfo.cellX;
6323d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.cellY = mPendingAddInfo.cellY;
633002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            if (isWorkspaceLocked()) {
634002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                sPendingAddList.add(args);
635002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            } else {
636557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                delayExitSpringLoadedMode = completeAdd(args);
63731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
63831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
639ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        mDragLayer.clearAnimatedView();
640557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        // Exit spring loaded mode if necessary after cancelling the configuration of a widget
641ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
642ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                null);
643ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
644ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
645ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
6468b805b17158886035b38261eb611d8641701ae43Michael Jurka        CellLayout cellLayout =
6479d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
648ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Runnable onCompleteRunnable = null;
649ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int animationType = 0;
650ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
6519d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        AppWidgetHostView boundWidget = null;
652ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (resultCode == RESULT_OK) {
653ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
654ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
6559d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    mPendingAddWidgetInfo);
6569d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            boundWidget = layout;
657ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            onCompleteRunnable = new Runnable() {
658ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                @Override
659ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                public void run() {
660ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
661ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                            mPendingAddInfo.screen, layout, null);
662ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
663ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                            null);
664ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                }
665ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            };
666ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else if (resultCode == RESULT_CANCELED) {
667ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
668ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            onCompleteRunnable = new Runnable() {
669ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                @Override
670ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                public void run() {
671ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
672ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                            null);
673ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                }
674ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            };
675ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
6769d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        if (mDragLayer.getAnimatedView() != null) {
6779d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
6789d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
6799d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    animationType, boundWidget, true);
6809d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        } else {
6819d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // The animated view may be null in the case of a rotation during widget configuration
6829d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            onCompleteRunnable.run();
6839d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        }
68431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
68531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
68631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
68731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onResume() {
68831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onResume();
689f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
690de0fb8fbf11caac382589ab464b50d528942273aWinson Chung        // Process any items that were added while Launcher was away
691de0fb8fbf11caac382589ab464b50d528942273aWinson Chung        InstallShortcutReceiver.flushInstallQueue(this);
692de0fb8fbf11caac382589ab464b50d528942273aWinson Chung
69334a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        mPaused = false;
694ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        if (mRestoring || mOnResumeNeedsLoad) {
6959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mWorkspaceLoading = true;
696f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            mModel.startLoader(true);
6979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mRestoring = false;
698ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            mOnResumeNeedsLoad = false;
69931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
700e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung
701e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        // Reset the pressed state of icons that were locked in the press state while activities
702e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        // were launching
703ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        if (mWaitingForResume != null) {
704e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            // Resets the previous workspace icon press state
705ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            mWaitingForResume.setStayPressed(false);
706ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        }
707e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        if (mAppsCustomizeContent != null) {
708e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            // Resets the previous all apps icon press state
709e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            mAppsCustomizeContent.resetDrawableState();
710e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        }
71106dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // It is possible that widgets can receive updates while launcher is not in the foreground.
71206dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // Consequently, the widgets will be inflated in the orientation of the foreground activity
71306dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // (framework issue). On resuming, we ensure that any widgets are inflated for the current
71406dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // orientation.
7153d50932a93dd367537db3548bde29d1380b371c0Adam Cohen        getWorkspace().reinflateWidgetsIfNecessary();
716f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
717f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // Again, as with the above scenario, it's possible that one or more of the global icons
718f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // were updated in the wrong orientation.
719f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        updateGlobalIcons();
72006dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen    }
72106dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen
72231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
7237376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    protected void onPause() {
724ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
725ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // to be consistent.  So re-enable the flag here, and we will re-disable it as necessary
726ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // when Launcher resumes and we are still in AllApps.
727ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        updateWallpaperVisibility(true);
728ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung
7297376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        super.onPause();
730ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        mPaused = true;
73124b6fd854f75f21700a330c2f0d11938e5dfeab6Joe Onorato        mDragController.cancelDrag();
732a2413751e3a698aef9c87411a639637883856939Winson Chung        mDragController.resetLastGestureUpTime();
7337376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    }
734cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
73599c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    @Override
73699c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    public Object onRetainNonConfigurationInstance() {
7379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // Flag the loader to stop early before switching
7389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        mModel.stopLoader();
739785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
740785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.surrender();
741785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
74213c2e7b788203f397085bef563bc5597e4c98028Romain Guy        return Boolean.TRUE;
74399c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    }
7447376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
7452d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    // We can't hide the IME if it was forced open.  So don't bother
7462d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    /*
7472d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    @Override
7482d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    public void onWindowFocusChanged(boolean hasFocus) {
7492d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        super.onWindowFocusChanged(hasFocus);
7502d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato
7512d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        if (hasFocus) {
7522d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            final InputMethodManager inputManager = (InputMethodManager)
7532d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                    getSystemService(Context.INPUT_METHOD_SERVICE);
7542d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            WindowManager.LayoutParams lp = getWindow().getAttributes();
7552d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
7562d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        android.os.Handler()) {
7572d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        protected void onReceiveResult(int resultCode, Bundle resultData) {
7582d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                            Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
7592d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        }
7602d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                    });
7612d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
7622d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        }
7632d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    }
7642d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    */
7652d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato
76631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean acceptFilter() {
76731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final InputMethodManager inputManager = (InputMethodManager)
76831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                getSystemService(Context.INPUT_METHOD_SERVICE);
76931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return !inputManager.isFullscreenMode();
77031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
77131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
77231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
77331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onKeyDown(int keyCode, KeyEvent event) {
77497d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final int uniChar = event.getUnicodeChar();
77597d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final boolean handled = super.onKeyDown(keyCode, event);
77697d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
77797d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        if (!handled && acceptFilter() && isKeyNotWhitespace) {
77831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
77931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    keyCode, event);
78031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
781138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // something usable has been typed - start a search
782cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy                // the typed text will be retrieved and cleared by
783138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // showSearchDialog()
784138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // If there are multiple keystrokes before the search dialog takes focus,
785138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // onSearchRequested() will be called for every keystroke,
786138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // but it is idempotent, so it's fine.
787138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                return onSearchRequested();
78831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
78931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
79031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
7918a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        // Eat the long press event so the keyboard doesn't come up.
7928a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
7938a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato            return true;
7948a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        }
7958a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato
79631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return handled;
79731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
79831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
799138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    private String getTypedText() {
800138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        return mDefaultKeySsb.toString();
801138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    }
802138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
803138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    private void clearTypedText() {
804138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        mDefaultKeySsb.clear();
805138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        mDefaultKeySsb.clearSpans();
806138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        Selection.setSelection(mDefaultKeySsb, 0);
807138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    }
808138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
80931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
810883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
811883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     * State
812883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     */
813883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    private static State intToState(int stateOrdinal) {
814883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        State state = State.WORKSPACE;
815883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        final State[] stateValues = State.values();
816883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        for (int i = 0; i < stateValues.length; i++) {
817883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka            if (stateValues[i].ordinal() == stateOrdinal) {
818883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka                state = stateValues[i];
819883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka                break;
820883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka            }
821883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        }
822883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        return state;
823883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    }
824883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka
825883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    /**
82631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Restores the previous state, if it exists.
82731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
82831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param savedState The previous state.
82931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
83031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void restoreState(Bundle savedState) {
83131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (savedState == null) {
83231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return;
83331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
83431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
835883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
836f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (state == State.APPS_CUSTOMIZE) {
8373a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato            showAllApps(false);
8383a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato        }
8393a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato
840f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
84131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (currentScreen > -1) {
8420142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            mWorkspace.setCurrentPage(currentScreen);
84331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
84431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
8463d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
8473d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
8483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
8493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.container = pendingAddContainer;
8503d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.screen = pendingAddScreen;
8513d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
8523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
8539d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
8549d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
8559d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
85687a9f5bf6a62a2b877bd2285f91e0300b288e02aAdam Cohen            mWaitingForResult = true;
85731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            mRestoring = true;
85831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
85931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8609d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen
86131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
86231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (renameFolder) {
86331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
864319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            mFolderInfo = mModel.getFolderById(this, sFolders, id);
86531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            mRestoring = true;
86631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
867a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung
868785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung
869785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Restore the AppsCustomize tab
870785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeTabHost != null) {
871785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            String curTab = savedState.getString("apps_customize_currentTab");
872785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            if (curTab != null) {
873f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                // We set this directly so that there is no delay before the tab is set
874785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                mAppsCustomizeContent.setContentType(
875785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                        mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
876785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
877f314b0eab03bb354c14b6c89f694a6685bd246d9Winson Chung                mAppsCustomizeContent.loadAssociatedPages(
878f314b0eab03bb354c14b6c89f694a6685bd246d9Winson Chung                        mAppsCustomizeContent.getCurrentPage());
879785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
880785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung
8815afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            int currentIndex = savedState.getInt("apps_customize_currentIndex");
8825afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            mAppsCustomizeContent.restorePageForIndex(currentIndex);
883785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
88431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
88531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
88631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
88731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Finds all the views we need and configure them properly.
88831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
88931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void setupViews() {
890a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        final DragController dragController = mDragController;
89100acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
8924c98d9235d164680186180974719f551cf935d08Winson Chung        mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
8934c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
894b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
895b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        mDockDivider = (ImageView) findViewById(R.id.dock_divider);
89631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
8974c98d9235d164680186180974719f551cf935d08Winson Chung        // Setup the drag layer
8984c98d9235d164680186180974719f551cf935d08Winson Chung        mDragLayer.setup(this, dragController);
8994c98d9235d164680186180974719f551cf935d08Winson Chung
9003d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // Setup the hotseat
9013d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mHotseat = (Hotseat) findViewById(R.id.hotseat);
9023d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mHotseat != null) {
9033d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mHotseat.setup(this);
9043d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
9053d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
9064c98d9235d164680186180974719f551cf935d08Winson Chung        // Setup the workspace
9074c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace.setHapticFeedbackEnabled(false);
9084c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace.setOnLongClickListener(this);
909cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mWorkspace.setup(dragController);
910d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        dragController.addDragListener(mWorkspace);
9114c98d9235d164680186180974719f551cf935d08Winson Chung
912f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Get the search/delete bar
913c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
914f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
915f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Setup AppsCustomize
916f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        mAppsCustomizeTabHost = (AppsCustomizeTabHost)
917f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                findViewById(R.id.apps_customize_pane);
918f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        mAppsCustomizeContent = (AppsCustomizePagedView)
919f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
920f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        mAppsCustomizeContent.setup(this, dragController);
921f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
9225130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        // Get the all apps button
9235130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        mAllAppsButton = findViewById(R.id.all_apps_button);
9245130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        if (mAllAppsButton != null) {
9255130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka            mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
9265130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                @Override
9275130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                public boolean onTouch(View v, MotionEvent event) {
9285130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                    if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
9295130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                        onTouchDownAllAppsButton(v);
9305130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                    }
9315130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                    return false;
9325130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka                }
9335130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka            });
9345130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        }
9353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // Setup the drag controller (drop targets have to be added in reverse order in priority)
9364c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setDragScoller(mWorkspace);
9374c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setScrollView(mDragLayer);
9384c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setMoveTarget(mWorkspace);
9394c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.addDropTarget(mWorkspace);
940c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        if (mSearchDropTargetBar != null) {
941c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            mSearchDropTargetBar.setup(this, dragController);
942e0f5a61a1f105216b0067124bacac69194c0ef70Michael Jurka        }
94331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
94431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
94531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
94631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Creates a view representing a shortcut.
94731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
94831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param info The data structure describing the shortcut.
94931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
95031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return A View inflated from R.layout.application.
95131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
9520589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    View createShortcut(ShortcutInfo info) {
95331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return createShortcut(R.layout.application,
9540142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
95531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
95631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
95731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
95831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Creates a view representing a shortcut inflated from the specified resource.
95931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
96031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param layoutResId The id of the XML layout used to create the shortcut.
96131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param parent The group the shortcut belongs to.
96231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param info The data structure describing the shortcut.
96331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
96431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return A View inflated from layoutResId.
96531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
9660589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
96767b2f6cba5aa7b5d0fac5e66ba8f3d5f887baee6Michael Jurka        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
96867b2f6cba5aa7b5d0fac5e66ba8f3d5f887baee6Michael Jurka        favorite.applyFromShortcutInfo(info, mIconCache);
96931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        favorite.setOnClickListener(this);
97031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return favorite;
97131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
97231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
97331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
97431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Add an application shortcut to the workspace.
97531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
97631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param data The intent describing the application.
97731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param cellInfo The position on screen where to create the shortcut.
97831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
9793d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
9800280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final int[] cellXY = mTmpAddItemCellCoordinates;
9813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final CellLayout layout = getCellLayout(container, screen);
9820280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
983fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        // First we check if we already know the exact location where we want to add this item.
984fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cellX >= 0 && cellY >= 0) {
985fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[0] = cellX;
986fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[1] = cellY;
987fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
98893eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
9890280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return;
9900280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
99131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
992002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
993cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
9940589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (info != null) {
9950589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
99631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
9970589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            info.container = ItemInfo.NO_ID;
9983d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
999fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen                    isWorkspaceLocked(), cellX, cellY);
10000589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
10010589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
100231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
100331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1004cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
100531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
100631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Add a shortcut to the workspace.
100731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
100831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param data The intent describing the shortcut.
100931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param cellInfo The position on screen where to create the shortcut.
101031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
10113d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private void completeAddShortcut(Intent data, long container, int screen, int cellX,
10123d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellY) {
10133d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] cellXY = mTmpAddItemCellCoordinates;
10143d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] touchXY = mPendingAddInfo.dropPos;
10153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        CellLayout layout = getCellLayout(container, screen);
10160280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1017d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        boolean foundCellSpan = false;
1018fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1019558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen        ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
1020d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
1021d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return;
1022d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
1023558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen        final View view = createShortcut(info);
1024558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
1025fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        // First we check if we already know the exact location where we want to add this item.
1026fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cellX >= 0 && cellY >= 0) {
1027fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[0] = cellX;
1028fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[1] = cellY;
1029fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = true;
1030558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
1031558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            // If appropriate, either create a folder or add to an existing folder
1032482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1033558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                    true, null,null)) {
1034558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                return;
1035558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
1036558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            DragObject dragObject = new DragObject();
1037558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            dragObject.dragInfo = info;
1038482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1039482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    true)) {
1040558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                return;
1041558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
1042fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (touchXY != null) {
1043d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            // when dragging and dropping, just find the closest free spot
10443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
1045d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            foundCellSpan = (result != null);
1046d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        } else {
1047fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1048d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        }
1049d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1050d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        if (!foundCellSpan) {
105193eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
10520280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return;
10530280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
1054cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1055558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen        LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
105631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
105731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
10583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
10593d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    isWorkspaceLocked());
106031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
106131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
106231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10632f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
10642f093b6ad185967c129781526060288e983bc58aAdam Cohen            int minHeight) {
10652f093b6ad185967c129781526060288e983bc58aAdam Cohen        Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
1066f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        // We want to account for the extra amount of padding that we are adding to the widget
1067f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        // to ensure that it gets the full amount of space that it has requested
1068f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        int requiredWidth = minWidth + padding.left + padding.right;
1069f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        int requiredHeight = minHeight + padding.top + padding.bottom;
10702f093b6ad185967c129781526060288e983bc58aAdam Cohen        return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
1071f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1072f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
10732f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
10742f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
1075f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1076f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
10772f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
10782f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
1079cbf47e38e24cc0d63b4bd3f345c050216f06f404Adam Cohen    }
1080cbf47e38e24cc0d63b4bd3f345c050216f06f404Adam Cohen
10812f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
10822f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
1083f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1084f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
10852f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
10862f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.componentName, info.minResizeWidth,
1087a5c9636f563a2fa5bd3c97266894e002453b13dcWinson Chung                info.minResizeHeight);
1088d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    }
1089d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
109031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
10917376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project     * Add a widget to the workspace.
109231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
10935bbc91bbbcba6cbcfff66b8d8dd510abb06bc076Romain Guy     * @param appWidgetId The app widget id
10947376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project     * @param cellInfo The position on screen where to create the widget.
109531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1096ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1097ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1098ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (appWidgetInfo == null) {
1099ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1100ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
1101cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
11027376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        // Calculate the grid spans needed to fit this widget
11033d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        CellLayout layout = getCellLayout(container, screen);
1104093538671bdbce2629dd0bf788e5d3489514631eAdam Cohen
11052f093b6ad185967c129781526060288e983bc58aAdam Cohen        int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
11062f093b6ad185967c129781526060288e983bc58aAdam Cohen        int[] spanXY = getSpanForWidget(this, appWidgetInfo);
1107cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
110831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Try finding open space on Launcher screen
1109a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // We have saved the position to which the widget was dragged-- this really only matters
1110a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // if we are placing widgets on a "spring-loaded" screen
11113d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] cellXY = mTmpAddItemCellCoordinates;
11123d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] touchXY = mPendingAddInfo.dropPos;
1113d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int[] finalSpan = new int[2];
11140280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        boolean foundCellSpan = false;
11153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
11163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            cellXY[0] = mPendingAddInfo.cellX;
11173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            cellXY[1] = mPendingAddInfo.cellY;
1118d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[0] = mPendingAddInfo.spanX;
1119d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[1] = mPendingAddInfo.spanY;
1120fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = true;
1121fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (touchXY != null) {
11220280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // when dragging and dropping, just find the closest free spot
11233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] result = layout.findNearestVacantArea(
1124d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1125d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    spanXY[1], cellXY, finalSpan);
1126d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[0] = finalSpan[0];
1127d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[1] = finalSpan[1];
1128d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            foundCellSpan = (result != null);
11290280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        } else {
1130d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
1131a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
1132a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
11330280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (!foundCellSpan) {
1134f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            if (appWidgetId != -1) {
1135f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                // Deleting an app widget ID is a void call but writes to disk before returning
1136f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                // to the caller...
1137f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                new Thread("deleteAppWidgetId") {
1138f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                    public void run() {
1139f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1140f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                    }
1141f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                }.start();
1142f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            }
114393eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
114418042c82c5847d3caf9af65583c5590c17927c3dRomain Guy            return;
114518042c82c5847d3caf9af65583c5590c17927c3dRomain Guy        }
114631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11477376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        // Build Launcher-specific widget info and save to database
114811a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
114911a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung                appWidgetInfo.provider);
11500280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        launcherInfo.spanX = spanXY[0];
11510280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        launcherInfo.spanY = spanXY[1];
1152d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1153d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
1154cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
115531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        LauncherModel.addItemToDatabase(this, launcherInfo,
11563d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                container, screen, cellXY[0], cellXY[1], false);
115731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
115831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
1159ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            if (hostView == null) {
1160ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                // Perform actual inflation because we're live
1161ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1162ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1163ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            } else {
1164ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                // The AppWidgetHostView has already been inflated and instantiated
1165ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView = hostView;
1166ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            }
1167cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
116831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            launcherInfo.hostView.setTag(launcherInfo);
1169d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            launcherInfo.hostView.setVisibility(View.VISIBLE);
1170211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung            launcherInfo.notifyWidgetSizeChanged(this);
11713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
11729c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
1173ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1174ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
117531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
11766af9af057f2e40c54a4ed447c4628eef7dc15683Adam Cohen        resetAddInfo();
117731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1178cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1179ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1180ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        @Override
1181ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        public void onReceive(Context context, Intent intent) {
1182ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            final String action = intent.getAction();
1183ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1184ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mUserPresent = false;
1185bec6ac5bb3a9d6a677081eb50147abdc58a2cabbAdam Cohen                mDragLayer.clearAllResizeFrames();
1186ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                updateRunning();
1187337cd9d3f6b2abfe7e70a8e20f0017d67e9535ffWinson Chung
1188c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                // Reset AllApps to its initial state only if we are not in the middle of
1189b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                // processing a multi-step drop
1190c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1191c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                    mAppsCustomizeTabHost.reset();
1192c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                    showWorkspace(false);
1193785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                }
1194ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1195ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mUserPresent = true;
1196ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                updateRunning();
1197ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1198ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1199ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    };
1200ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1201ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    @Override
1202ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onAttachedToWindow() {
1203ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        super.onAttachedToWindow();
1204ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1205ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        // Listen for broadcasts related to user-presence
1206ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        final IntentFilter filter = new IntentFilter();
1207ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        filter.addAction(Intent.ACTION_SCREEN_OFF);
1208ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        filter.addAction(Intent.ACTION_USER_PRESENT);
1209ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        registerReceiver(mReceiver, filter);
1210ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1211d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        mAttached = true;
1212ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = true;
1213ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1214ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1215ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    @Override
1216ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onDetachedFromWindow() {
1217ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        super.onDetachedFromWindow();
1218ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = false;
1219ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1220d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        if (mAttached) {
1221d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen            unregisterReceiver(mReceiver);
1222d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen            mAttached = false;
1223d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        }
1224ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        updateRunning();
1225ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1226ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1227ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onWindowVisibilityChanged(int visibility) {
1228ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = visibility == View.VISIBLE;
1229ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        updateRunning();
12302a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // The following code used to be in onResume, but it turns out onResume is called when
12312a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
12322a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // is a more appropriate event to handle
12332a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        if (mVisible) {
12342a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mAppsCustomizeTabHost.onWindowVisible();
12352a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (!mWorkspaceLoading) {
12362a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
12372a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // We want to let Launcher draw itself at least once before we force it to build
12382a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // layers on all the workspace pages, so that transitioning to Launcher from other
12392a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
12402a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // a true draw so we wait until the second preDraw call to be safe
12412a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
12422a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    public boolean onPreDraw() {
12436ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // We delay the layer building a bit in order to give
12446ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // other message processing a time to run.  In particular
12456ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // this avoids a delay in hiding the IME if it was
12466ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // currently shown, because doing that may involve
12476ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // some communication back with the app.
1248aeae56bf1282761a7dd017458fe27d83f9683efcWinson Chung                        mWorkspace.postDelayed(mBuildLayersRunnable, 500);
124931ce073f6a4a6e3e71d533ea4a2a423c2509ad89Winson Chung
12506ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        observer.removeOnPreDrawListener(this);
12512a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                        return true;
12522a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    }
12532a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                });
12542a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
12556ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            // When Launcher comes back to foreground, a different Activity might be responsible for
12566ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            // the app market intent, so refresh the icon
12576ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            updateAppMarketIcon();
12582a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            clearTypedText();
12592a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        }
1260ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1261ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1262ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private void sendAdvanceMessage(long delay) {
1263ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mHandler.removeMessages(ADVANCE_MSG);
1264ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1265ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mHandler.sendMessageDelayed(msg, delay);
1266ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mAutoAdvanceSentTime = System.currentTimeMillis();
1267ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1268ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1269ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private void updateRunning() {
1270ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1271ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (autoAdvanceRunning != mAutoAdvanceRunning) {
1272ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mAutoAdvanceRunning = autoAdvanceRunning;
1273ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (autoAdvanceRunning) {
1274ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1275ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                sendAdvanceMessage(delay);
1276ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            } else {
1277ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                if (!mWidgetsToAdvance.isEmpty()) {
1278ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1279ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                            (System.currentTimeMillis() - mAutoAdvanceSentTime));
1280ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                }
1281ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mHandler.removeMessages(ADVANCE_MSG);
12822313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy                mHandler.removeMessages(0); // Remove messages sent using postDelayed()
1283ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1284ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1285ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1286ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1287ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final Handler mHandler = new Handler() {
1288ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        @Override
1289ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        public void handleMessage(Message msg) {
1290ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (msg.what == ADVANCE_MSG) {
1291ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                int i = 0;
1292ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                for (View key: mWidgetsToAdvance.keySet()) {
1293ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1294ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    final int delay = mAdvanceStagger * i;
1295ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    if (v instanceof Advanceable) {
1296ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                       postDelayed(new Runnable() {
1297ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                           public void run() {
1298ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                               ((Advanceable) v).advance();
1299ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                           }
1300ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                       }, delay);
1301ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    }
1302ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    i++;
1303ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                }
1304ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                sendAdvanceMessage(mAdvanceInterval);
1305ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1306ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1307ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    };
1308ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1309ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1310292c025d06b41c42546e9771b0da0333e08f9db7Adam Cohen        if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
1311ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1312ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (v instanceof Advanceable) {
1313ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mWidgetsToAdvance.put(hostView, appWidgetInfo);
13142ddf13e175910f784cb4e54493ef202671e499f8Adam Cohen            ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
1315ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            updateRunning();
1316ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1317ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1318ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1319ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    void removeWidgetToAutoAdvance(View hostView) {
1320ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (mWidgetsToAdvance.containsKey(hostView)) {
1321ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mWidgetsToAdvance.remove(hostView);
1322ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            updateRunning();
1323ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
13240280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
13250280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
13269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1327ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        removeWidgetToAutoAdvance(launcherInfo.hostView);
13289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        launcherInfo.hostView = null;
13299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
13309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
133193eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung    void showOutOfSpaceMessage(boolean isHotseatLayout) {
133293eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung        int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
133393eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung        Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
1334ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1335ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
13367376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    public LauncherAppWidgetHost getAppWidgetHost() {
13377376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        return mAppWidgetHost;
133831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1339cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1340a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    public LauncherModel getModel() {
1341a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        return mModel;
1342a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
1343a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
13442ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    void closeSystemDialogs() {
13452ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        getWindow().closeAllPanels();
13462ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
1347a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato        // Whatever we were doing is hereby canceled.
1348a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato        mWaitingForResult = false;
13492ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    }
13502ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
135131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
135231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onNewIntent(Intent intent) {
135331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onNewIntent(intent);
135431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
135531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Close the menu
135631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
1357a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato            // also will cancel mWaitingForResult.
13582ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            closeSystemDialogs();
1359fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
136014f122bf847e50a3e7730ccbe57abc25d086a01bJoe Onorato            boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
136114f122bf847e50a3e7730ccbe57abc25d086a01bJoe Onorato                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
136201f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka
1363ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            Folder openFolder = mWorkspace.getOpenFolder();
13646ec2e187cd8f0b2d1c98e46b794efb99fd55556dPatrick Dubroy            // In all these cases, only animate if we're already on home
1365758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy            mWorkspace.exitWidgetResizeMode();
1366ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1367ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen                    openFolder == null) {
13686ec2e187cd8f0b2d1c98e46b794efb99fd55556dPatrick Dubroy                mWorkspace.moveToDefaultScreen(true);
13693a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato            }
1370ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
1371ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            closeFolder();
1372de1af7661548692d370518528ff91c7422b9c8aeWinson Chung            exitSpringLoadedDragMode();
1373c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka            showWorkspace(alreadyOnHome);
137473b979d8c141c7ceac82dad7c5b271a6a42afa67Romain Guy
13753a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato            final View v = getWindow().peekDecorView();
13763a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato            if (v != null && v.getWindowToken() != null) {
13773a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato                InputMethodManager imm = (InputMethodManager)getSystemService(
13783a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato                        INPUT_METHOD_SERVICE);
13793a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
138031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
1381337cd9d3f6b2abfe7e70a8e20f0017d67e9535ffWinson Chung
138235aa14d5e33e4f5cccb9fd23be1047f26d67b409Michael Jurka            // Reset AllApps to its initial state
1383b64d36e6f8006b3ea5a6c2951af4cf8dea72d08eAdam Cohen            if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1384c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                mAppsCustomizeTabHost.reset();
1385785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
138631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
138731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
138831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
138931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
139031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onRestoreInstanceState(Bundle savedInstanceState) {
139131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Do not call super here
139231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mSavedInstanceState = savedInstanceState;
139331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
139431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
139531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
139631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onSaveInstanceState(Bundle outState) {
13970142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
139895bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        super.onSaveInstanceState(outState);
139931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1400883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        outState.putInt(RUNTIME_STATE, mState.ordinal());
140151e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        // We close any open folder since it will not be re-opened, and we need to make sure
140251e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        // this state is reflected.
140351e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        closeFolder();
140431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14053d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
14063d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mWaitingForResult) {
14073d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
14083d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
14093d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
14103d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
14119d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
14129d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
14139d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
141431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
141531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
141631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (mFolderInfo != null && mWaitingForResult) {
141731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
141831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
141931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1420946ad470c72a3caa7568d11836c182b7f84d840dMichael Jurka
1421785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Save the current AppsCustomize tab
1422785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeTabHost != null) {
1423785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1424785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            if (currentTabTag != null) {
1425785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                outState.putString("apps_customize_currentTab", currentTabTag);
1426785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
14275afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
14285afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            outState.putInt("apps_customize_currentIndex", currentIndex);
1429785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
143031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
143131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
143231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
143331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onDestroy() {
143431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onDestroy();
1435cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1436e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        // Remove all pending runnables
1437e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.removeMessages(ADVANCE_MSG);
1438e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.removeMessages(0);
14399d906c7742af73160df257a40a702e1cc4f87899Michael Jurka        mWorkspace.removeCallbacks(mBuildLayersRunnable);
1440e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung
1441cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        // Stop callbacks from LauncherModel
1442cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        LauncherApplication app = ((LauncherApplication) getApplication());
1443cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        mModel.stopLoader();
1444cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        app.setLauncher(null);
1445cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung
144631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        try {
14477376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            mAppWidgetHost.stopListening();
144831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } catch (NullPointerException ex) {
1449a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato            Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
145031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
14512313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mAppWidgetHost = null;
14522313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy
14532313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWidgetsToAdvance.clear();
145431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
145531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        TextKeyListener.getInstance().release();
145631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
145731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
14583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        unbindWorkspaceAndHotseatItems();
14591e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
14601e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        getContentResolver().unregisterContentObserver(mWidgetObserver);
146134a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        unregisterReceiver(mCloseSystemDialogsReceiver);
14622313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy
1463accf3bffedc736c04034bfee8e1a8e4fe775f568Adam Cohen        mDragLayer.clearAllResizeFrames();
14642313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        ((ViewGroup) mWorkspace.getParent()).removeAllViews();
14652313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWorkspace.removeAllViews();
14662313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWorkspace = null;
14672313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mDragController = null;
146860b7c53ccc6384b65aaca77d97794e8f09e8cfc8Patrick Dubroy
146960b7c53ccc6384b65aaca77d97794e8f09e8cfc8Patrick Dubroy        ValueAnimator.clearAllAnimations();
147031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
147131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1472a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public DragController getDragController() {
1473a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        return mDragController;
1474a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    }
1475a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen
147631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
147731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void startActivityForResult(Intent intent, int requestCode) {
147808f97496b72aaff822aba89271e9bd458aafae2dRomain Guy        if (requestCode >= 0) mWaitingForResult = true;
147931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.startActivityForResult(intent, requestCode);
148031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
148131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
148270d721061536e0634f6cfa8cd219759a83befceeWinson Chung    /**
148370d721061536e0634f6cfa8cd219759a83befceeWinson Chung     * Indicates that we want global search for this activity by setting the globalSearch
148470d721061536e0634f6cfa8cd219759a83befceeWinson Chung     * argument for {@link #startSearch} to true.
148570d721061536e0634f6cfa8cd219759a83befceeWinson Chung     */
148631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
1487cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy    public void startSearch(String initialQuery, boolean selectInitialQuery,
148831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            Bundle appSearchData, boolean globalSearch) {
1489138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
1490c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        showWorkspace(true);
1491cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1492138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        if (initialQuery == null) {
1493138a04170d964da9cdec228e95b976875ae52481Karl Rosaen            // Use any text typed in the launcher as the initial query
1494138a04170d964da9cdec228e95b976875ae52481Karl Rosaen            initialQuery = getTypedText();
1495138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        }
149631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (appSearchData == null) {
149731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            appSearchData = new Bundle();
14983e244cf9d2da4fb04ef095f8b752a2a2c6e2f287Bjorn Bringert            appSearchData.putString(Search.SOURCE, "launcher-search");
149931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1500cf7f63bb4dcc152ba7977fc7499081d651a4313aMathew Inwood        Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1501cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1502a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        startGlobalSearch(initialQuery, selectInitialQuery,
1503a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData, sourceBounds);
1504a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka    }
1505a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka
1506a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka    /**
1507a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka     * Starts the global search activity. This code is a copied from SearchManager
1508a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka     */
1509a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka    public void startGlobalSearch(String initialQuery,
1510a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
1511138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        final SearchManager searchManager =
1512a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1513a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1514a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (globalSearchActivity == null) {
1515a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            Log.w(TAG, "No global search activity found.");
1516a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            return;
1517a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1518a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1519a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1520a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.setComponent(globalSearchActivity);
1521a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        // Make sure that we have a Bundle to put source in
1522a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (appSearchData == null) {
1523a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData = new Bundle();
1524a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        } else {
1525a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData = new Bundle(appSearchData);
1526a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1527a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        // Set source to package name of app that starts global search, if not set already.
1528a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (!appSearchData.containsKey("source")) {
1529a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData.putString("source", getPackageName());
1530a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1531a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.putExtra(SearchManager.APP_DATA, appSearchData);
1532a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (!TextUtils.isEmpty(initialQuery)) {
1533a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            intent.putExtra(SearchManager.QUERY, initialQuery);
1534a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1535a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (selectInitialQuery) {
1536a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1537a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1538a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.setSourceBounds(sourceBounds);
1539a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        try {
1540a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            startActivity(intent);
1541a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        } catch (ActivityNotFoundException ex) {
1542a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1543a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
154431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
154531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
154670d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
154770d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onCreateOptionsMenu(Menu menu) {
154870d721061536e0634f6cfa8cd219759a83befceeWinson Chung        if (isWorkspaceLocked()) {
154970d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return false;
155070d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
155170d721061536e0634f6cfa8cd219759a83befceeWinson Chung
155270d721061536e0634f6cfa8cd219759a83befceeWinson Chung        super.onCreateOptionsMenu(menu);
1553dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
1554dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1555dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1556dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1557236d431cc582df9b2841718195ae977c3948f751Winson Chung        Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1558236d431cc582df9b2841718195ae977c3948f751Winson Chung        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1559236d431cc582df9b2841718195ae977c3948f751Winson Chung                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1560b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        String helpUrl = getString(R.string.help_url);
1561b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
1562dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1563dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1564dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
156570d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
156670d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setIcon(android.R.drawable.ic_menu_gallery)
156770d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setAlphabeticShortcut('W');
156870d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
156970d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setIcon(android.R.drawable.ic_menu_manage)
1570dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            .setIntent(manageApps)
157170d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setAlphabeticShortcut('M');
1572236d431cc582df9b2841718195ae977c3948f751Winson Chung        menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1573236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setIcon(android.R.drawable.ic_menu_preferences)
1574236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setIntent(settings)
1575236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setAlphabeticShortcut('P');
1576b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        if (!helpUrl.isEmpty()) {
1577b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka            menu.add(0, MENU_HELP, 0, R.string.menu_help)
1578b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setIcon(android.R.drawable.ic_menu_help)
1579b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setIntent(help)
1580b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setAlphabeticShortcut('H');
1581b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        }
158270d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return true;
158370d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
158470d721061536e0634f6cfa8cd219759a83befceeWinson Chung
158570d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
158670d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onPrepareOptionsMenu(Menu menu) {
158770d721061536e0634f6cfa8cd219759a83befceeWinson Chung        super.onPrepareOptionsMenu(menu);
158870d721061536e0634f6cfa8cd219759a83befceeWinson Chung
158970d721061536e0634f6cfa8cd219759a83befceeWinson Chung        if (mAppsCustomizeTabHost.isTransitioning()) {
159070d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return false;
159170d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
159270d721061536e0634f6cfa8cd219759a83befceeWinson Chung        boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
159370d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
159470d721061536e0634f6cfa8cd219759a83befceeWinson Chung
159570d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return true;
159670d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
159770d721061536e0634f6cfa8cd219759a83befceeWinson Chung
159870d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
159970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onOptionsItemSelected(MenuItem item) {
160070d721061536e0634f6cfa8cd219759a83befceeWinson Chung        switch (item.getItemId()) {
160170d721061536e0634f6cfa8cd219759a83befceeWinson Chung        case MENU_WALLPAPER_SETTINGS:
160270d721061536e0634f6cfa8cd219759a83befceeWinson Chung            startWallpaper();
160370d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return true;
160470d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
160570d721061536e0634f6cfa8cd219759a83befceeWinson Chung
160670d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return super.onOptionsItemSelected(item);
160770d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
160831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1609ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    @Override
1610ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    public boolean onSearchRequested() {
1611cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy        startSearch(null, false, null, true);
1612a135ba896e4e12a99deb054f671c63535c3e11e3Amith Yamasani        // Use a custom animation for launching search
1613a135ba896e4e12a99deb054f671c63535c3e11e3Amith Yamasani        overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
1614138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        return true;
1615ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    }
1616ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
16179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public boolean isWorkspaceLocked() {
16189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return mWorkspaceLoading || mWaitingForResult;
161931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
162031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
16210280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private void resetAddInfo() {
16223d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = ItemInfo.NO_ID;
16233d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.screen = -1;
16243d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
16253d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1626d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
16273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = null;
16280280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
16290280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
16309d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
16319d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            AppWidgetProviderInfo appWidgetInfo) {
16329d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        if (appWidgetInfo.configure != null) {
16339d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddWidgetInfo = appWidgetInfo;
1634af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka
16357984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            // Launch over to configure widget, if needed
16367984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
16379d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            intent.setComponent(appWidgetInfo.configure);
16387984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
16398e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
16407984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert        } else {
16417984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            // Otherwise just add it
16429d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
16439d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    appWidgetInfo);
1644557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            // Exit spring loaded mode if necessary after adding the widget
1645ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            exitSpringLoadedDragModeDelayed(true, false, null);
164631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
164731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1648cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1649fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    /**
1650fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * Process a shortcut drop.
1651fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     *
1652fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param componentName The name of the component
1653fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param screen The screen where it should be added
1654fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param cell The cell it should be added to, optional
1655fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param position The location on the screen where it was dropped, optional
1656fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     */
16573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void processShortcutFromDrop(ComponentName componentName, long container, int screen,
16583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] cell, int[] loc) {
16590280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        resetAddInfo();
16603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = container;
16613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.screen = screen;
16623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = loc;
1663fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1664fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cell != null) {
16653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = cell[0];
16663d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = cell[1];
1667fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        }
16680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
16690280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
16700280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        createShortcutIntent.setComponent(componentName);
16710280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        processShortcut(createShortcutIntent);
16720280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
16730280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1674fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    /**
1675fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * Process a widget drop.
1676fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     *
1677fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param info The PendingAppWidgetInfo of the widget being added.
1678fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param screen The screen where it should be added
1679fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param cell The cell it should be added to, optional
1680fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param position The location on the screen where it was dropped, optional
1681fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     */
16823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1683d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int[] cell, int[] span, int[] loc) {
1684fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        resetAddInfo();
16853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = info.container = container;
16863d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.screen = info.screen = screen;
16873d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = loc;
1688d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanX = info.minSpanX;
1689d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanY = info.minSpanY;
1690d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
1691fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cell != null) {
16923d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = cell[0];
16933d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = cell[1];
1694fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        }
1695d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        if (span != null) {
1696d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            mPendingAddInfo.spanX = span[0];
1697d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            mPendingAddInfo.spanY = span[1];
1698d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        }
1699fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1700ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        AppWidgetHostView hostView = info.boundWidget;
1701ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int appWidgetId;
1702ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (hostView != null) {
1703ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetId = hostView.getAppWidgetId();
17049d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            addAppWidgetImpl(appWidgetId, info, hostView, info.info);
1705ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else {
17069d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // In this case, we either need to start an activity to get permission to bind
17079d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // the widget, or we need to start an activity to configure the widget, or both.
1708ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetId = getAppWidgetHost().allocateAppWidgetId();
17098b805b17158886035b38261eb611d8641701ae43Michael Jurka            if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
17109d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                addAppWidgetImpl(appWidgetId, info, null, info.info);
17118b805b17158886035b38261eb611d8641701ae43Michael Jurka            } else {
17129d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                mPendingAddWidgetInfo = info.info;
17138b805b17158886035b38261eb611d8641701ae43Michael Jurka                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
17148b805b17158886035b38261eb611d8641701ae43Michael Jurka                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
17158b805b17158886035b38261eb611d8641701ae43Michael Jurka                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
17168b805b17158886035b38261eb611d8641701ae43Michael Jurka                startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
17178b805b17158886035b38261eb611d8641701ae43Michael Jurka            }
1718ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
1719fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    }
1720fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1721deb98afe40cd1b6f1c042492e56b48b29320a134Joe Onorato    void processShortcut(Intent intent) {
1722c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        // Handle case where user selected "Applications"
1723c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        String applicationName = getResources().getString(R.string.group_applications);
1724c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1725cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1726c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        if (applicationName != null && applicationName.equals(shortcutName)) {
1727c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1728c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1729cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1730c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1731c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
173258f208892274c91b83dbbad6c50ea8638f106b33Winson Chung            pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
17334a79a04bcd5f8c4d4fab7bcc46eea9e19a3dc63eJoe Onorato            startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
1734c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        } else {
17354a79a04bcd5f8c4d4fab7bcc46eea9e19a3dc63eJoe Onorato            startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
1736c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        }
173731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
173831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
173924ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung    void processWallpaper(Intent intent) {
174024ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung        startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
174124ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung    }
174224ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung
17433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
17443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellY) {
1745c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final FolderInfo folderInfo = new FolderInfo();
174631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        folderInfo.title = getText(R.string.folder_name);
174731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
174831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Update the model
17493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
17503d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                false);
1751319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.put(folderInfo.id, folderInfo);
175231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
175331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Create the view
17543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        FolderIcon newFolder =
17553d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
17563d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
17573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                isWorkspaceLocked());
1758df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        return newFolder;
17599c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
17609c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
17619c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    void removeFolder(FolderInfo folder) {
1762319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.remove(folder.id);
176331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1764cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
176531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void startWallpaper() {
1766c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        showWorkspace(true);
176731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
17688355ae39ad77b8e2e9ff3b4cbff8077a002ce151Dianne Hackborn        Intent chooser = Intent.createChooser(pickWallpaper,
17698355ae39ad77b8e2e9ff3b4cbff8077a002ce151Dianne Hackborn                getText(R.string.chooser_wallpaper));
1770f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1771f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy        //       Removed in Eclair MR1
1772f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        WallpaperManager wm = (WallpaperManager)
1773f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//                getSystemService(Context.WALLPAPER_SERVICE);
1774f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        WallpaperInfo wi = wm.getWallpaperInfo();
1775f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        if (wi != null && wi.getSettingsActivity() != null) {
1776f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            LabeledIntent li = new LabeledIntent(getPackageName(),
1777f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//                    R.string.configure_wallpaper, 0);
1778f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1779f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1780f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        }
1781a0618e4102d0543d953d81e162d93c8ea641d0fdMike Cleron        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
178231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
178331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17842ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    /**
17852ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     * Registers various content observers. The current implementation registers
17862ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     * only a favorites observer to keep track of the favorites applications.
17872ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     */
17881e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private void registerContentObservers() {
17891e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        ContentResolver resolver = getContentResolver();
17901e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
17911e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey                true, mWidgetObserver);
17921e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
17931e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
179431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
179531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean dispatchKeyEvent(KeyEvent event) {
179631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (event.getAction() == KeyEvent.ACTION_DOWN) {
179731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            switch (event.getKeyCode()) {
1798678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn                case KeyEvent.KEYCODE_HOME:
1799678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn                    return true;
1800be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                case KeyEvent.KEYCODE_VOLUME_DOWN:
18019bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka                    if (doesFileExist(DUMP_STATE_PROPERTY)) {
1802be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                        dumpState();
1803be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                        return true;
1804be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                    }
1805be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                    break;
1806678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn            }
1807678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn        } else if (event.getAction() == KeyEvent.ACTION_UP) {
1808678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn            switch (event.getKeyCode()) {
180931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                case KeyEvent.KEYCODE_HOME:
181031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    return true;
181131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
181231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
181331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
181431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return super.dispatchKeyEvent(event);
181531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
181631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
181788ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    @Override
181888ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    public void onBackPressed() {
1819f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (mState == State.APPS_CUSTOMIZE) {
1820c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka            showWorkspace(true);
182194f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        } else if (mWorkspace.getOpenFolder() != null) {
182276fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            Folder openFolder = mWorkspace.getOpenFolder();
182376fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            if (openFolder.isEditingName()) {
182476fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen                openFolder.dismissEditingName();
182576fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            } else {
182676fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen                closeFolder();
182776fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            }
182894f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        } else {
1829758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy            mWorkspace.exitWidgetResizeMode();
1830758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy
183194f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            // Back button is a no-op here, but give at least some feedback for the button press
183294f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            mWorkspace.showOutlinesTemporarily();
1833af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        }
183488ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    }
183588ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato
183631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
18371e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     * Re-listen when widgets are reset.
18381e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     */
18391e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private void onAppWidgetReset() {
1840bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka        if (mAppWidgetHost != null) {
1841bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka            mAppWidgetHost.startListening();
1842bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka        }
18431e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
18441e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
18451e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    /**
18469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Go through the and disconnect any of the callbacks in the drawables and the views or we
18479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * leak the previous Home screen on orientation change.
184831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
18493d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private void unbindWorkspaceAndHotseatItems() {
1850603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        if (mModel != null) {
1851603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung            mModel.unbindWorkspaceItems();
1852603bcb91a091d0f4512fdfb92d6df3c6f9fa8059Winson Chung        }
185331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
185499c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey
185531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
185631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Launches the intent referred by the clicked shortcut.
185731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
185831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param v The view representing the clicked shortcut.
185931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
186031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onClick(View v) {
18619932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        // Make sure that rogue clicks don't get through while allapps is launching, or after the
18629932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        // view has detached (it's possible for this to happen if the view is removed mid touch).
18639932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (v.getWindowToken() == null) {
18649932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen            return;
18659932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        }
18669932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen
18679b0b2fefb2de9aca457ab1c38ffd08d7667f4065Winson Chung        if (!mWorkspace.isFinishedSwitchingState()) {
18689932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen            return;
18699932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        }
18702f84ef259314b0bef20f6e7e503377212fadc88cAdam Cohen
187131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        Object tag = v.getTag();
18720589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (tag instanceof ShortcutInfo) {
187331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Open shortcut
1874fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy            final Intent intent = ((ShortcutInfo) tag).intent;
187513724ea6078748195bcf747641bf6ee8fbbe0f4bJoe Onorato            int[] pos = new int[2];
187613724ea6078748195bcf747641bf6ee8fbbe0f4bJoe Onorato            v.getLocationOnScreen(pos);
1877fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy            intent.setSourceBounds(new Rect(pos[0], pos[1],
1878fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
1879c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
1880c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            boolean success = startActivitySafely(v, intent, tag);
1881ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka
1882ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            if (success && v instanceof BubbleTextView) {
1883ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka                mWaitingForResume = (BubbleTextView) v;
1884ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka                mWaitingForResume.setStayPressed(true);
1885ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            }
188631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else if (tag instanceof FolderInfo) {
1887a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            if (v instanceof FolderIcon) {
1888a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                FolderIcon fi = (FolderIcon) v;
1889a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                handleFolderClick(fi);
1890a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            }
1891f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        } else if (v == mAllAppsButton) {
1892f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            if (mState == State.APPS_CUSTOMIZE) {
1893c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka                showWorkspace(true);
18947404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato            } else {
18952a55232840acc214d95474a3734b3b897669efd9Michael Jurka                onClickAllAppsButton(v);
18967404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato            }
189731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
189831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
189931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19000e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka    public boolean onTouch(View v, MotionEvent event) {
1901dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // this is an intercepted event being forwarded from mWorkspace;
1902c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        // clicking anywhere on the workspace causes the customization drawer to slide down
1903c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        showWorkspace(true);
19040e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        return false;
19050e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka    }
19060e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka
1907af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka    /**
19082c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * Event handler for the search button
19092c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     *
19102c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * @param v The view that was clicked.
19112c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     */
19122c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    public void onClickSearchButton(View v) {
1913bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1914bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung
1915a135ba896e4e12a99deb054f671c63535c3e11e3Amith Yamasani        onSearchRequested();
19162c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    }
19172c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka
19182c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    /**
19196d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     * Event handler for the voice button
19206d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     *
19216d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     * @param v The view that was clicked.
19226d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     */
19236d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    public void onClickVoiceButton(View v) {
1924bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
19256d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
1926ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        try {
1927ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            final SearchManager searchManager =
1928ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1929ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            ComponentName activityName = searchManager.getGlobalSearchActivity();
1930ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1931f615f710bb1a1c6fdccb538c1add4db5925d7799Andy Huang            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1932ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (activityName != null) {
1933ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                intent.setPackage(activityName.getPackageName());
1934ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
193586a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            startActivity(null, intent, "onClickVoiceButton");
193601b0b6318634b1bb3c99cdc043512ebb829ec6daWinson Chung            overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
1937ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        } catch (ActivityNotFoundException e) {
1938ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1939f615f710bb1a1c6fdccb538c1add4db5925d7799Andy Huang            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1940ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            startActivitySafely(null, intent, "onClickVoiceButton");
1941ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
19426d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
19436d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
19446d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    /**
19452c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * Event handler for the "grid" button that appears on the home screen, which
19462c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * enters all apps mode.
19472c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     *
19482c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * @param v The view that was clicked.
19492c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     */
19502c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    public void onClickAllAppsButton(View v) {
19515130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        showAllApps(true);
19525130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka    }
19535130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka
19545130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka    public void onTouchDownAllAppsButton(View v) {
19552a55232840acc214d95474a3734b3b897669efd9Michael Jurka        // Provide the same haptic feedback that the system offers for virtual keys.
19562a55232840acc214d95474a3734b3b897669efd9Michael Jurka        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
19572c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    }
19582c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka
1959ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    public void onClickAppMarketButton(View v) {
1960ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        if (mAppMarketIntent != null) {
1961c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            startActivitySafely(v, mAppMarketIntent, "app market");
19624f916f4b35aef7ef834f7888bbdf3a41b9cb3cdbWinson Chung        } else {
19634f916f4b35aef7ef834f7888bbdf3a41b9cb3cdbWinson Chung            Log.e(TAG, "Invalid app market intent.");
1964ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        }
1965ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    }
1966ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
1967bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy    void startApplicationDetailsActivity(ComponentName componentName) {
1968bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy        String packageName = componentName.getPackageName();
19694ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
19704ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy                Uri.fromParts("package", packageName, null));
1971dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
19723b1b36bf2ebed328ab60f1f78fe4d5bdd5322f2eWinson Chung        startActivitySafely(null, intent, "startApplicationDetailsActivity");
19734ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy    }
19744ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy
19755539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy    void startApplicationUninstallActivity(ApplicationInfo appInfo) {
19765539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
19775539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            // System applications cannot be installed. For now, show a toast explaining that.
19785539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            // We may give them the option of disabling apps this way.
19795539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            int messageId = R.string.uninstall_system_app_text;
19805539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
19815539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        } else {
19825539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            String packageName = appInfo.componentName.getPackageName();
19835539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            String className = appInfo.componentName.getClassName();
19845539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            Intent intent = new Intent(
19855539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy                    Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1986dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1987dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
19885539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            startActivity(intent);
19895539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        }
1990bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy    }
1991bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy
199286a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    boolean startActivity(View v, Intent intent, Object tag) {
199331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1994c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
199531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        try {
19962672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            // Only launch using the new animation if the shortcut has not opted out (this is a
19972672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            // private contract between launcher and may be ignored in the future).
19982672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            boolean useLaunchAnimation = (v != null) &&
19992672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung                    !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
20002672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            if (useLaunchAnimation) {
2001c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2002c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                        v.getMeasuredWidth(), v.getMeasuredHeight());
2003c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
2004c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                startActivity(intent, opts.toBundle());
2005c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            } else {
2006c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                startActivity(intent);
2007c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            }
2008ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            return true;
200931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } catch (SecurityException e) {
201031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2011a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
201231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2013f984e853f253c76a7a636a094680bb01c4b8ffb8Joe Onorato                    "or use the exported attribute for this activity. "
2014f984e853f253c76a7a636a094680bb01c4b8ffb8Joe Onorato                    + "tag="+ tag + " intent=" + intent, e);
20158e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        }
2016ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        return false;
20178e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy    }
2018aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
201986a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    boolean startActivitySafely(View v, Intent intent, Object tag) {
202086a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        boolean success = false;
202186a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        try {
202286a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            success = startActivity(v, intent, tag);
202386a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        } catch (ActivityNotFoundException e) {
202486a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
202586a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
202686a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        }
202786a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        return success;
202886a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    }
202986a720e4927a997e9fd98276d2dab67244138f79Michael Jurka
20308e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy    void startActivityForResultSafely(Intent intent, int requestCode) {
20318e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        try {
20328e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            startActivityForResult(intent, requestCode);
20338e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        } catch (ActivityNotFoundException e) {
20348e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
20358e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        } catch (SecurityException e) {
20368e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
20378e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
20388e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
20398e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy                    "or use the exported attribute for this activity.", e);
204031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
204131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
204231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2043a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    private void handleFolderClick(FolderIcon folderIcon) {
2044fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        final FolderInfo info = folderIcon.getFolderInfo();
20453c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        Folder openFolder = mWorkspace.getFolderForTag(info);
20463c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen
20473c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        // If the folder info reports that the associated folder is open, then verify that
20483c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        // it is actually opened. There have been a few instances where this gets out of sync.
20493c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        if (info.opened && openFolder == null) {
20503c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen            Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
20513c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen                    + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
20523c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen            info.opened = false;
20533c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        }
20543c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen
2055fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
205631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Close any open folder
205731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            closeFolder();
205831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Open the requested folder
2059a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            openFolder(folderIcon);
206031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
206131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Find the open folder...
206231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            int folderScreen;
206331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (openFolder != null) {
20640142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                folderScreen = mWorkspace.getPageForView(openFolder);
206531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                // .. and close it
206631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                closeFolder(openFolder);
20670142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                if (folderScreen != mWorkspace.getCurrentPage()) {
206831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // Close any folder open on the current screen
206931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    closeFolder();
207031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // Pull the folder onto this screen
2071a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                    openFolder(folderIcon);
207231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
207331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
207431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
207531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
207631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2077268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    /**
2078268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2079268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     * in the DragLayer in the exact absolute location of the original FolderIcon.
2080268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     */
2081268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private void copyFolderIconToImage(FolderIcon fi) {
2082268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final int width = fi.getMeasuredWidth();
2083268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final int height = fi.getMeasuredHeight();
2084268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2085268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Lazy load ImageView, Bitmap and Canvas
2086268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconImageView == null) {
2087268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconImageView = new ImageView(this);
2088268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2089268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2090268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                mFolderIconBitmap.getHeight() != height) {
2091268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2092268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2093268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2094268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2095268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        DragLayer.LayoutParams lp;
2096268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2097268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2098268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        } else {
2099268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            lp = new DragLayer.LayoutParams(width, height);
2100268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2101268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2102268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2103268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.customPosition = true;
2104268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.x = mRectForFolderAnimation.left;
2105268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.y = mRectForFolderAnimation.top;
21068ec2303379eb467e063113385c9514063abed787Adam Cohen        lp.width = width;
21078ec2303379eb467e063113385c9514063abed787Adam Cohen        lp.height = height;
2108268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2109268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2110268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        fi.draw(mFolderIconCanvas);
2111268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
2112fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (fi.getFolder() != null) {
2113fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2114fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
21158ec2303379eb467e063113385c9514063abed787Adam Cohen        }
2116268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Just in case this image view is still in the drag layer from a previous animation,
2117268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // we remove it and re-add it.
2118268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2119268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mDragLayer.removeView(mFolderIconImageView);
2120268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2121268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mDragLayer.addView(mFolderIconImageView, lp);
2122fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (fi.getFolder() != null) {
2123fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            fi.getFolder().bringToFront();
21248ec2303379eb467e063113385c9514063abed787Adam Cohen        }
2125268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    }
2126268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
21272801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    private void growAndFadeOutFolderIcon(FolderIcon fi) {
21289932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (fi == null) return;
21292801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
21302801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
21312801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
21322801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2133c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        FolderInfo info = (FolderInfo) fi.getTag();
2134c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2135c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            CellLayout cl = (CellLayout) fi.getParent().getParent();
2136e50adee3d54978840036bb3a814771ce498aee18Winson Chung            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2137e50adee3d54978840036bb3a814771ce498aee18Winson Chung            cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
2138c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        }
2139c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen
2140268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2141268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        copyFolderIconToImage(fi);
2142268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        fi.setVisibility(View.INVISIBLE);
2143268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2144268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2145268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                scaleX, scaleY);
21462801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
21472801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.start();
21482801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
21492801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2150268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
21519932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (fi == null) return;
21522801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
21532801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
21542801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
21552801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2156268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final CellLayout cl = (CellLayout) fi.getParent().getParent();
2157268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2158268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // We remove and re-draw the FolderIcon in-case it has changed
2159268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mDragLayer.removeView(mFolderIconImageView);
2160268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        copyFolderIconToImage(fi);
2161268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2162268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                scaleX, scaleY);
21632801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2164c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        oa.addListener(new AnimatorListenerAdapter() {
2165c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            @Override
2166c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            public void onAnimationEnd(Animator animation) {
2167268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                if (cl != null) {
2168268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    cl.clearFolderLeaveBehind();
2169268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    // Remove the ImageView copy of the FolderIcon and make the original visible.
2170268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    mDragLayer.removeView(mFolderIconImageView);
2171268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    fi.setVisibility(View.VISIBLE);
2172c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen                }
2173c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            }
2174c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        });
21752801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.start();
21762801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
21772801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
217831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
2179774bd377df3053ef43e34ab8ca98160eb6811be1Michael Jurka     * Opens the user folder described by the specified tag. The opening of the folder
218031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * is animated relative to the specified View. If the View is null, no animation
218131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * is played.
218231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
218331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param folderInfo The FolderInfo describing the folder to open.
218431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2185a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public void openFolder(FolderIcon folderIcon) {
2186fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        Folder folder = folderIcon.getFolder();
2187a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        FolderInfo info = folder.mInfo;
218831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2189a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        info.opened = true;
219031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
21914554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        // Just verify that the folder hasn't already been added to the DragLayer.
21924554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        // There was a one-off crash where the folder had a parent already.
21934554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (folder.getParent() == null) {
21944554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            mDragLayer.addView(folder);
21954554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            mDragController.addDropTarget((DropTarget) folder);
21964554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        } else {
21974554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
21984554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen                    folder.getParent() + ").");
21994554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
2200a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        folder.animateOpen();
2201268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        growAndFadeOutFolderIcon(folderIcon);
22024554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    }
22034554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
22044554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    public void closeFolder() {
22054554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        Folder folder = mWorkspace.getOpenFolder();
22064554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (folder != null) {
2207ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            if (folder.isEditingName()) {
2208ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen                folder.dismissEditingName();
2209ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            }
22104554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            closeFolder(folder);
22117d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung
22127d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            // Dismiss the folder cling
22137d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            dismissFolderCling(null);
22144554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
22154554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    }
22164554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
22174554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    void closeFolder(Folder folder) {
22184554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        folder.getInfo().opened = false;
22194554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
22204554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        ViewGroup parent = (ViewGroup) folder.getParent().getParent();
22214554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (parent != null) {
22224554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
22234554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            shrinkAndFadeInFolderIcon(fi);
22244554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
22254554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        folder.animateClosed();
222631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
222731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
222831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onLongClick(View v) {
222936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (!isDraggingEnabled()) return false;
223036a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (isWorkspaceLocked()) return false;
223136a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (mState != State.WORKSPACE) return false;
223231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
223331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(v instanceof CellLayout)) {
22348c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            v = (View) v.getParent().getParent();
223531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
223631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22370280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        resetAddInfo();
22380280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
223931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // This happens when long clicking an item with the dpad/trackball
2240faea1f816ead0033e52b730ab4dd598d2c186168Adam Cohen        if (longClickCellInfo == null) {
224131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return true;
224231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
224331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22443d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // The hotseat touch handling does not go through Workspace, and we always allow long press
22453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // on hotseat items.
22460280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final View itemUnderLongClick = longClickCellInfo.cell;
22473d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
22483d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (allowLongPress && !mDragController.isDragging()) {
22490280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            if (itemUnderLongClick == null) {
22500280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                // User long pressed on empty space
22510280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
22520280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                        HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
22536a3fd3f042c8bfa7d83e29c1eeadff21323ab089Winson Chung                startWallpaper();
225431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            } else {
22550280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                if (!(itemUnderLongClick instanceof Folder)) {
225631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // User long pressed on an item
22570280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    mWorkspace.startDrag(longClickCellInfo);
225831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
225931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
226031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
226131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return true;
226231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
226331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    boolean isHotseatLayout(View layout) {
22653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return mHotseat != null && layout != null &&
22663d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
22671fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy    }
22683d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    Hotseat getHotseat() {
22693d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return mHotseat;
22701fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy    }
2271ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen    SearchDropTargetBar getSearchBar() {
2272ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        return mSearchDropTargetBar;
2273ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen    }
22741fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy
22753d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
22763d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns the CellLayout of the specified container at the specified screen.
22773d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
22783d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    CellLayout getCellLayout(long container, int screen) {
22793d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
22803d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mHotseat != null) {
22813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return mHotseat.getLayout();
22823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
22833d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return null;
22849d31e9fa3dedf7c51b407c1c5fafe5294fc3c01bRomain Guy            }
22853d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
22863d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            return (CellLayout) mWorkspace.getChildAt(screen);
2287a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy        }
2288a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy    }
2289a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy
229031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    Workspace getWorkspace() {
229131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return mWorkspace;
229231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
229331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2294843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler    // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2295843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler    public boolean isAllAppsVisible() {
2296f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        return (mState == State.APPS_CUSTOMIZE);
2297fb0ca67aea25f4282096d9247ddaebee6fbf7e00Joe Onorato    }
2298fb0ca67aea25f4282096d9247ddaebee6fbf7e00Joe Onorato
22990dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn    public boolean isAllAppsButtonRank(int rank) {
23000dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn        return mHotseat.isAllAppsButtonRank(rank);
23010dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn    }
23020dca1ec41479a74f8da080224fa0c7eacab674d6Andrew Flynn
2303c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler    // AllAppsView.Watcher
2304c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler    public void zoomed(float zoom) {
2305f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (zoom == 1.0f) {
2306c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler            mWorkspace.setVisibility(View.GONE);
2307c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler        }
2308c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler    }
23096b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
23106b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    /**
23116b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * Helper method for the cameraZoomIn/cameraZoomOut animations
23126b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param view The view being animated
2313f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung     * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
23146b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param scaleFactor The scale factor used for the zoom
23156b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     */
2316b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    private void setPivotsForZoom(View view, float scaleFactor) {
23176b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        view.setPivotX(view.getWidth() / 2.0f);
23187777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        view.setPivotY(view.getHeight() / 2.0f);
23196b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    }
2320c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler
232118f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    void disableWallpaperIfInAllApps() {
232218f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        // Only disable it if we are in all apps
232318f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        if (mState == State.APPS_CUSTOMIZE) {
232418f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            if (mAppsCustomizeTabHost != null &&
232518f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung                    !mAppsCustomizeTabHost.isTransitioning()) {
232618f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung                updateWallpaperVisibility(false);
232718f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            }
232818f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        }
232918f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    }
233018f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung
2331bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn    void updateWallpaperVisibility(boolean visible) {
2332bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2333bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        int curflags = getWindow().getAttributes().flags
2334bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2335bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        if (wpflags != curflags) {
2336bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn            getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2337bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        }
2338bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn    }
2339bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn
2340a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2341a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        if (v instanceof LauncherTransitionable) {
2342a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2343a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        }
2344a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    }
2345a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka
2346bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2347bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        if (v instanceof LauncherTransitionable) {
2348bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2349bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        }
23507044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
23517044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        // Update the workspace transition step as well
23527044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        dispatchOnLauncherTransitionStep(v, 0f);
23537044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    }
23547044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
23557044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    private void dispatchOnLauncherTransitionStep(View v, float t) {
23567044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        if (v instanceof LauncherTransitionable) {
23577044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
23587044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        }
2359bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    }
2360bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2361bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2362bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        if (v instanceof LauncherTransitionable) {
2363bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2364bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        }
23657044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
23667044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        // Update the workspace transition step as well
23677044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        dispatchOnLauncherTransitionStep(v, 1f);
2368bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    }
2369bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
23703ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    /**
2371b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     * Things to test when changing the following seven functions.
2372b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Home from workspace
2373b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2374b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2375b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Home from all apps
2376b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2377b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2378b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Back from all apps
2379b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2380b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2381b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch app from workspace and quit
2382b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2383b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2384b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch app from all apps and quit
2385b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2386b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2387b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Go to a screen that's not the default, then all
2388b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *     apps, and launch and app, and go back
2389b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2390b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          -with home
2391b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On workspace, long press power and go back
2392b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2393b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2394b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On all apps, long press power and go back
2395b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2396b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2397b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On workspace, power off
2398b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On all apps, power off
2399b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch an app and turn off the screen while in that app
2400b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - Go back with home key
2401b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - Go back with back key  TODO: make this not go to workspace
2402b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From all apps
2403b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From workspace
2404b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Enter and exit car mode (becuase it causes an extra configuration changed)
2405b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From all apps
2406b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From the center workspace
2407b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From another workspace
2408b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     */
2409b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2410b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    /**
24113ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Zoom the camera out from the workspace to reveal 'toView'.
24123ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Assumes that the view to show is anchored at either the very top or very bottom
24133ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * of the screen.
24143ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     */
2415bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
2416b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mStateAnimation != null) {
2417b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation.cancel();
2418b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation = null;
2419b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        }
24203ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy        final Resources res = getResources();
2421f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
2422f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2423f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2424f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2425bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        final View fromView = mWorkspace;
2426a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
2427cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        final int startDelay =
2428cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
24293ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy
2430b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        setPivotsForZoom(toView, scale);
24317247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2432d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // Shrink workspaces away if going to AppsCustomize from workspace
24332a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        Animator workspaceAnim =
24342a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
24357247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2436df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        if (animated) {
24377407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            toView.setScaleX(scale);
24387407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            toView.setScaleY(scale);
24397407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
24407407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            scaleAnim.
24417407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                scaleX(1f).scaleY(1f).
24427407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                setDuration(duration).
24437407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                setInterpolator(new Workspace.ZoomOutInterpolator());
244461033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
2445f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            toView.setVisibility(View.VISIBLE);
2446c00f0b996712243e0745ea1af870b0229ca11a73Adam Cohen            toView.setAlpha(0f);
2447159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            final ObjectAnimator alphaAnim = ObjectAnimator
2448159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .ofFloat(toView, "alpha", 0f, 1f)
2449159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .setDuration(fadeDuration);
2450f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
24517044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
24527044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                @Override
24537044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                public void onAnimationUpdate(ValueAnimator animation) {
24547044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    float t = (Float) animation.getAnimatedValue();
24557044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(fromView, t);
24567044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(toView, t);
24577044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                }
24587044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            });
2459f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohen
24602a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // toView should appear right at the end of the workspace shrink
24612a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // animation
24622a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mStateAnimation = new AnimatorSet();
24632a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mStateAnimation.play(scaleAnim).after(startDelay);
24647407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            mStateAnimation.play(alphaAnim).after(startDelay);
24652a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
24662a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2467f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                boolean animationCancelled = false;
2468f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen
2469dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
2470b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                public void onAnimationStart(Animator animation) {
2471bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                    updateWallpaperVisibility(true);
2472df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    // Prepare the position
2473df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setTranslationX(0.0f);
2474df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setTranslationY(0.0f);
2475df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setVisibility(View.VISIBLE);
2476785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                    toView.bringToFront();
2477df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                }
24783c4c20fbe682cb4b3ef94f09afe0af09171583f3Michael Jurka                @Override
24798edd75c8bb0729a10cb39f614183e3e9ae4288e8Michael Jurka                public void onAnimationEnd(Animator animation) {
2480bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(fromView, animated, false);
2481bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(toView, animated, false);
248232174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung
248332174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung                    if (!springLoaded && !LauncherApplication.isScreenLarge()) {
248432174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung                        // Hide the workspace scrollbar
248532174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung                        mWorkspace.hideScrollingIndicator(true);
2486b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                        hideDockDivider();
248732174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung                    }
2488f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    if (!animationCancelled) {
2489f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                        updateWallpaperVisibility(false);
2490f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    }
2491c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2492c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                    // Hide the search bar
2493c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                    mSearchDropTargetBar.hideSearchBar(false);
2494f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                }
2495f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen
2496cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                @Override
2497f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                public void onAnimationCancel(Animator animation) {
2498f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    animationCancelled = true;
24993c4c20fbe682cb4b3ef94f09afe0af09171583f3Michael Jurka                }
2500df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            });
25016b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
25022a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (workspaceAnim != null) {
25032a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mStateAnimation.play(workspaceAnim);
25042a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
25051899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka
25061899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            boolean delayAnim = false;
25072a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            final ViewTreeObserver observer;
25082a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
2509a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2510a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, false);
25112a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
25122a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // If any of the objects being animated haven't been measured/laid out
25132a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // yet, delay the animation until we get a layout pass
2514bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
25152a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    (mWorkspace.getMeasuredWidth() == 0) ||
25162a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    (toView.getMeasuredWidth() == 0)) {
25172a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                observer = mWorkspace.getViewTreeObserver();
25182a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                delayAnim = true;
25192a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            } else {
25202a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                observer = null;
25211899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            }
25222a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
25233dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            final AnimatorSet stateAnimation = mStateAnimation;
25243dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            final Runnable startAnimRunnable = new Runnable() {
25253dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                public void run() {
25263dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    // Check that mStateAnimation hasn't changed while
25273dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    // we waited for a layout/draw pass
25283dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    if (mStateAnimation != stateAnimation)
25293dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                        return;
25303dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    setPivotsForZoom(toView, scale);
25313dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    dispatchOnLauncherTransitionStart(fromView, animated, false);
25323dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    dispatchOnLauncherTransitionStart(toView, animated, false);
2533382aa1835c3e0e88d5ca33263cee39cc3ac5711eMichael Jurka                    toView.post(new Runnable() {
25343dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                        public void run() {
25353dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                            // Check that mStateAnimation hasn't changed while
25363dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                            // we waited for a layout/draw pass
25373dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                            if (mStateAnimation != stateAnimation)
25383dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                                return;
25393dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                            mStateAnimation.start();
25403dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                        }
25413dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    });
25423dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                }
25433dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            };
25442a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (delayAnim) {
25452a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
25462a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    public void onGlobalLayout() {
2547382aa1835c3e0e88d5ca33263cee39cc3ac5711eMichael Jurka                        toView.post(startAnimRunnable);
25483a9fcedbcd235372cd2ab64f825a0b5b3937f59eMichael Jurka                        observer.removeOnGlobalLayoutListener(this);
25492a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    }
25502a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                };
25512a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                observer.addOnGlobalLayoutListener(delayedStart);
25522a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            } else {
25533dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                startAnimRunnable.run();
25541899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            }
25556b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        } else {
2556df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setTranslationX(0.0f);
2557df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setTranslationY(0.0f);
2558df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setScaleX(1.0f);
2559df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setScaleY(1.0f);
2560df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setVisibility(View.VISIBLE);
2561785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            toView.bringToFront();
2562bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2563bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2564bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                // Hide the workspace scrollbar
2565bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                mWorkspace.hideScrollingIndicator(true);
2566bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                hideDockDivider();
2567c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2568c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                // Hide the search bar
2569c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                mSearchDropTargetBar.hideSearchBar(false);
2570abded66084680bb31cc7ea403c88f44f79a3c884Michael Jurka            }
2571a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2572bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, false);
2573bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(fromView, animated, false);
2574a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, false);
2575bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(toView, animated, false);
2576bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(toView, animated, false);
2577bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn            updateWallpaperVisibility(false);
25786b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        }
25793ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    }
25807247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
25813ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    /**
25823ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Zoom the camera back into the workspace, hiding 'fromView'.
2583b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     * This is the opposite of showAppsCustomizeHelper.
25846b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param animated If true, the transition will be animated.
25853ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     */
2586ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    private void hideAppsCustomizeHelper(State toState, final boolean animated,
2587ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final boolean springLoaded, final Runnable onCompleteRunnable) {
2588bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2589b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mStateAnimation != null) {
2590b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation.cancel();
2591b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation = null;
2592b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        }
25933ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy        Resources res = getResources();
2594f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohen
2595f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2596159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka        final int fadeOutDuration =
2597159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
2598f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final float scaleFactor = (float)
2599f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2600f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final View fromView = mAppsCustomizeTabHost;
2601bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        final View toView = mWorkspace;
26022a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        Animator workspaceAnim = null;
26032a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
26042a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        if (toState == State.WORKSPACE) {
26052a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
26062a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            workspaceAnim = mWorkspace.getChangeStateAnimation(
26072a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    Workspace.State.NORMAL, animated, stagger);
26082a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
26092a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            workspaceAnim = mWorkspace.getChangeStateAnimation(
26102a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    Workspace.State.SPRING_LOADED, animated);
26112a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        }
26122b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
2613b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        setPivotsForZoom(fromView, scaleFactor);
2614bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        updateWallpaperVisibility(true);
26154afe9b31f51022538b30646fe83780e83bf22490Winson Chung        showHotseat(animated);
2616df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        if (animated) {
2617159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            final LauncherViewPropertyAnimator scaleAnim =
2618159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                    new LauncherViewPropertyAnimator(fromView);
2619159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            scaleAnim.
2620159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                scaleX(scaleFactor).scaleY(scaleFactor).
2621159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                setDuration(duration).
2622159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                setInterpolator(new Workspace.ZoomInInterpolator());
2623159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka
2624159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            final ObjectAnimator alphaAnim = ObjectAnimator
2625159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .ofFloat(fromView, "alpha", 1f, 0f)
2626159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .setDuration(fadeOutDuration);
2627cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen            alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
26287044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
26297044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                @Override
26307044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                public void onAnimationUpdate(ValueAnimator animation) {
26317044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    float t = 1f - (Float) animation.getAnimatedValue();
26327044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(fromView, t);
26337044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(toView, t);
26347044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                }
26357044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            });
2636159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka
2637bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            mStateAnimation = new AnimatorSet();
2638bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2639a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2640a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, true);
2641bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2642bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2643dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
26448edd75c8bb0729a10cb39f614183e3e9ae4288e8Michael Jurka                public void onAnimationEnd(Animator animation) {
2645bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                    updateWallpaperVisibility(true);
2646df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    fromView.setVisibility(View.GONE);
2647bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(fromView, animated, true);
2648bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(toView, animated, true);
2649bafdaafb993ab210761d912cdbb92cd58acc80f1Michael Jurka                    if (mWorkspace != null) {
2650bafdaafb993ab210761d912cdbb92cd58acc80f1Michael Jurka                        mWorkspace.hideScrollingIndicator(false);
2651bafdaafb993ab210761d912cdbb92cd58acc80f1Michael Jurka                    }
2652ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    if (onCompleteRunnable != null) {
2653ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                        onCompleteRunnable.run();
2654ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    }
2655df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                }
2656df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            });
26576b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
2658f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            mStateAnimation.playTogether(scaleAnim, alphaAnim);
26592a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (workspaceAnim != null) {
26602a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mStateAnimation.play(workspaceAnim);
26612a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
2662a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, true);
2663a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionStart(toView, animated, true);
26643dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            final Animator stateAnimation = mStateAnimation;
26653dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            mWorkspace.post(new Runnable() {
26663dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                public void run() {
26673dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    if (stateAnimation != mStateAnimation)
26683dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                        return;
26693dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    mStateAnimation.start();
26703dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                }
26713dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            });
2672df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        } else {
2673df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            fromView.setVisibility(View.GONE);
2674a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2675bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, true);
2676bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(fromView, animated, true);
2677a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, true);
2678bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(toView, animated, true);
2679bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(toView, animated, true);
2680b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mWorkspace.hideScrollingIndicator(false);
2681df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        }
26823ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    }
26837247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2684e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    @Override
2685e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    public void onTrimMemory(int level) {
2686e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka        super.onTrimMemory(level);
2687c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung        if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
2688e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka            mAppsCustomizeTabHost.onTrimMemory();
2689e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka        }
2690e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    }
2691e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka
269218f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    @Override
269318f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    public void onWindowFocusChanged(boolean hasFocus) {
269418f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        if (!hasFocus) {
269518f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // When another window occludes launcher (like the notification shade, or recents),
269618f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // ensure that we enable the wallpaper flag so that transitions are done correctly.
269718f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            updateWallpaperVisibility(true);
269818f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        } else {
269918f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // When launcher has focus again, disable the wallpaper if we are in AllApps
27006cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung            mWorkspace.postDelayed(new Runnable() {
27016cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                @Override
27026cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                public void run() {
27036cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                    disableWallpaperIfInAllApps();
27046cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                }
27056cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung            }, 500);
270618f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        }
270718f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    }
270818f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung
2709c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    void showWorkspace(boolean animated) {
2710ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        showWorkspace(animated, null);
2711ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
2712ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
2713ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
2714b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mState != State.WORKSPACE) {
2715c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
2716b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mWorkspace.setVisibility(View.VISIBLE);
2717ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
2718b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2719c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            // Show the search bar (only animate if we were showing the drop target bar in spring
2720c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            // loaded mode)
2721c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2722c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2723b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            // We only need to animate in the dock divider if we're going from spring loaded mode
2724c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            showDockDivider(animated && wasInSpringLoadedMode);
2725b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2726b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            // Set focus to the AppsCustomize button
2727b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            if (mAllAppsButton != null) {
2728b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka                mAllAppsButton.requestFocus();
2729b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            }
2730c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        }
27316b879f0a5885274a85333531e091283405d490ccAdam Lesinski
2732b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        mWorkspace.flashScrollingIndicator(animated);
27337777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
2734c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        // Change the state *after* we've called all the transition code
2735c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        mState = State.WORKSPACE;
2736815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganov
27375fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        // Resume the auto-advance of widgets
27385fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        mUserPresent = true;
27395fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        updateRunning();
27405fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung
2741815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganov        // send an accessibility event to announce the context change
2742815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganov        getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
274300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
274400acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
2745b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void showAllApps(boolean animated) {
2746b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mState != State.WORKSPACE) return;
2747b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2748b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        showAppsCustomizeHelper(animated, false);
2749b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mAppsCustomizeTabHost.requestFocus();
2750b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2751b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Change the state *after* we've called all the transition code
2752b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mState = State.APPS_CUSTOMIZE;
2753b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2754b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Pause the auto-advance of widgets until we are out of AllApps
2755b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mUserPresent = false;
2756b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        updateRunning();
2757b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        closeFolder();
2758b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2759b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Send an accessibility event to announce the context change
2760b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2761b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
2762b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
27637777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen    void enterSpringLoadedDragMode() {
2764b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        if (mState == State.APPS_CUSTOMIZE) {
2765ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
2766b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            hideDockDivider();
2767c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2768b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        }
2769d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
27707777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
2771ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2772ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final Runnable onCompleteRunnable) {
277309bfc45ae8aee2db579698c15007b1d138b4141fWinson Chung        if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
277409bfc45ae8aee2db579698c15007b1d138b4141fWinson Chung
2775e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.postDelayed(new Runnable() {
2776557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            @Override
2777557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            public void run() {
27786a3fd3f042c8bfa7d83e29c1eeadff21323ab089Winson Chung                if (successfulDrop) {
27797bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // Before we show workspace, hide all apps again because
27807bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
27817bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // clean up our state transition functions
27827bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    mAppsCustomizeTabHost.setVisibility(View.GONE);
2783ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    showWorkspace(true, onCompleteRunnable);
27847777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                } else {
27857777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    exitSpringLoadedDragMode();
27866a3fd3f042c8bfa7d83e29c1eeadff21323ab089Winson Chung                }
2787557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
2788557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        }, (extendedDelay ?
2789557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2790557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2791557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    }
2792b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2793d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    void exitSpringLoadedDragMode() {
2794b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2795b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            final boolean animated = true;
2796b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            final boolean springLoaded = true;
2797b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            showAppsCustomizeHelper(animated, springLoaded);
2798b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung            mState = State.APPS_CUSTOMIZE;
2799f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
2800f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Otherwise, we are not in spring loaded mode, so don't do anything.
2801f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    }
2802f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
2803b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    void hideDockDivider() {
2804b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        if (mQsbDivider != null && mDockDivider != null) {
2805b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mQsbDivider.setVisibility(View.INVISIBLE);
2806b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mDockDivider.setVisibility(View.INVISIBLE);
2807b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        }
2808b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    }
2809b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka
2810b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    void showDockDivider(boolean animated) {
2811b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        if (mQsbDivider != null && mDockDivider != null) {
2812b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mQsbDivider.setVisibility(View.VISIBLE);
2813b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mDockDivider.setVisibility(View.VISIBLE);
2814b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            if (mDividerAnimator != null) {
2815b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.cancel();
2816b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mQsbDivider.setAlpha(1f);
2817b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDockDivider.setAlpha(1f);
2818b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator = null;
2819b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            }
2820b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            if (animated) {
2821b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator = new AnimatorSet();
2822b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2823b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                        ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2824b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2825b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.start();
2826b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            }
2827b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        }
2828b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    }
2829b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka
2830b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void lockAllApps() {
2831b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // TODO
2832b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
2833b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2834b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void unlockAllApps() {
2835b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // TODO
2836b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
2837b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2838fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    public boolean isAllAppsCustomizeOpen() {
2839fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen        return mState == State.APPS_CUSTOMIZE;
2840fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen    }
2841fc53cd22c9a78708c78c85946443f23ec8c59502Adam Cohen
2842f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    /**
28433d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Shows the hotseat area.
2844f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung     */
28453d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void showHotseat(boolean animated) {
2846f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (!LauncherApplication.isScreenLarge()) {
2847f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            if (animated) {
28487407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                if (mHotseat.getAlpha() != 1f) {
28497407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    int duration = mSearchDropTargetBar.getTransitionInDuration();
28507407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    mHotseat.animate().alpha(1f).setDuration(duration);
28517407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                }
2852f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            } else {
28533d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mHotseat.setAlpha(1f);
2854f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            }
2855f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
2856f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    }
2857f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
2858f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    /**
28593d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Hides the hotseat area.
2860f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung     */
28613d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void hideHotseat(boolean animated) {
2862f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (!LauncherApplication.isScreenLarge()) {
2863f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            if (animated) {
28647407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                if (mHotseat.getAlpha() != 0f) {
28657407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    int duration = mSearchDropTargetBar.getTransitionOutDuration();
28667407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    mHotseat.animate().alpha(0f).setDuration(duration);
28677407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                }
2868f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            } else {
28693d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mHotseat.setAlpha(0f);
2870f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            }
2871b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        }
2872d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
2873d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
28745f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    /**
28755f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     * Add an item from all apps or customize onto the given workspace screen.
28765f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     * If layout is null, add to the current screen.
28775f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     */
28785f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
28796b4b25d093b0a3008d6e275438c79db9c21daab9Michael Jurka        if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
288093eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
2881213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka        }
28826b4b25d093b0a3008d6e275438c79db9c21daab9Michael Jurka    }
28832b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
2884dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    /** Maps the current orientation to an index for referencing orientation correct global icons */
2885dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private int getCurrentOrientationIndexForGlobalIcons() {
2886dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        // default - 0, landscape - 1
2887dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        switch (getResources().getConfiguration().orientation) {
2888dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        case Configuration.ORIENTATION_LANDSCAPE:
2889dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            return 1;
2890dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        default:
2891dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            return 0;
2892dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        }
2893dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    }
2894dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
2895ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
28960423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        try {
2897ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy            PackageManager packageManager = getPackageManager();
28980423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            // Look for the toolbar icon specified in the activity meta-data
28990423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            Bundle metaData = packageManager.getActivityInfo(
29000423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                    activityName, PackageManager.GET_META_DATA).metaData;
29010423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            if (metaData != null) {
2902ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                int iconResId = metaData.getInt(resourceName);
29030423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                if (iconResId != 0) {
29040423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                    Resources res = packageManager.getResourcesForActivity(activityName);
2905fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung                    return res.getDrawable(iconResId);
2906ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy                }
29070423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            }
29080423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        } catch (NameNotFoundException e) {
2909b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka            // This can happen if the activity defines an invalid drawable
2910b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka            Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2911b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka                    " not found", e);
291270d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood        } catch (Resources.NotFoundException nfe) {
291370d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood            // This can happen if the activity defines an invalid drawable
291470d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood            Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
291570d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood                    nfe);
29160423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
2917fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        return null;
2918fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
2919fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
2920fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    // if successful in getting icon, return it; otherwise, set button to use default drawable
2921fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2922ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            int buttonId, ComponentName activityName, int fallbackDrawableId,
2923ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            String toolbarResourceName) {
2924ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
2925128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        Resources r = getResources();
2926128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2927128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
2928fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
29294da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka        TextView button = (TextView) findViewById(buttonId);
2930fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        // If we were unable to find the icon via the meta-data, use a generic one
2931fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        if (toolbarIcon == null) {
2932128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon = r.getDrawable(fallbackDrawableId);
2933128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon.setBounds(0, 0, w, h);
29344da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            if (button != null) {
29354da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka                button.setCompoundDrawables(toolbarIcon, null, null, null);
29364da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            }
2937fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung            return null;
2938fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        } else {
2939128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon.setBounds(0, 0, w, h);
29404da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            if (button != null) {
29414da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka                button.setCompoundDrawables(toolbarIcon, null, null, null);
29424da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            }
2943fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung            return toolbarIcon.getConstantState();
2944fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        }
2945fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
2946fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
2947fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    // if successful in getting icon, return it; otherwise, set button to use default drawable
2948fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2949ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            int buttonId, ComponentName activityName, int fallbackDrawableId,
2950ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            String toolbarResourceName) {
2951fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        ImageView button = (ImageView) findViewById(buttonId);
2952ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
2953fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
295419e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka        if (button != null) {
295519e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            // If we were unable to find the icon via the meta-data, use a
295619e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            // generic one
295719e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            if (toolbarIcon == null) {
295819e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka                button.setImageResource(fallbackDrawableId);
295919e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            } else {
296019e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka                button.setImageDrawable(toolbarIcon);
296119e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            }
29620423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
296319e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka
296419e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka        return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
296519e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka
29660423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka    }
29670423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka
29681b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung    private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
2969fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        TextView button = (TextView) findViewById(buttonId);
29701b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        button.setCompoundDrawables(d, null, null, null);
2971fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
2972fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
2973e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
29744ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        ImageView button = (ImageView) findViewById(buttonId);
2975e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka        button.setImageDrawable(d.newDrawable(getResources()));
29764ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
29774ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
2978bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    private void invalidatePressedFocusedStates(View container, View button) {
2979bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        if (container instanceof HolographicLinearLayout) {
2980bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            HolographicLinearLayout layout = (HolographicLinearLayout) container;
2981bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            layout.invalidatePressedFocusedStates();
2982bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        } else if (button instanceof HolographicImageView) {
2983bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            HolographicImageView view = (HolographicImageView) button;
2984bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            view.invalidatePressedFocusedStates();
2985bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        }
2986bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    }
2987bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung
2988c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private boolean updateGlobalSearchIcon() {
2989c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View searchButtonContainer = findViewById(R.id.search_button_container);
29901cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
29911cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final View searchDivider = findViewById(R.id.search_divider);
2992c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2993cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        final View voiceButton = findViewById(R.id.voice_button);
2994e3fbfa933e89e43b66b69deeaa2cfa5f322b78e6Winson Chung        final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
299597d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
29961cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final SearchManager searchManager =
29971cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
29981cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        ComponentName activityName = searchManager.getGlobalSearchActivity();
29991cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        if (activityName != null) {
3000dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3001dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3002ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3003ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_SEARCH_ICON_METADATA_NAME);
3004ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (sGlobalSearchIcon[coi] == null) {
3005ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3006ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3007ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        TOOLBAR_ICON_METADATA_NAME);
3008ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
3009ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3010649723cfb3d73af16dd02462725700897ca60e38Winson Chung            if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
3011c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3012c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchButton.setVisibility(View.VISIBLE);
3013bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            invalidatePressedFocusedStates(searchButtonContainer, searchButton);
3014c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return true;
30151cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        } else {
3016cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung            // We disable both search and voice search when there is no global search provider
3017649723cfb3d73af16dd02462725700897ca60e38Winson Chung            if (searchDivider != null) searchDivider.setVisibility(View.GONE);
3018c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3019c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3020c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchButton.setVisibility(View.GONE);
3021cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung            voiceButton.setVisibility(View.GONE);
3022c60498a91d82ca7d73402d5a5dd8467cd7e0391cMichael Jurka            if (voiceButtonProxy != null) {
3023c60498a91d82ca7d73402d5a5dd8467cd7e0391cMichael Jurka                voiceButtonProxy.setVisibility(View.GONE);
3024c60498a91d82ca7d73402d5a5dd8467cd7e0391cMichael Jurka            }
3025c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return false;
30266d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani        }
30276d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
30286d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
3029e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateGlobalSearchIcon(Drawable.ConstantState d) {
3030bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View searchButtonContainer = findViewById(R.id.search_button_container);
3031bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View searchButton = (ImageView) findViewById(R.id.search_button);
30324ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        updateButtonWithDrawable(R.id.search_button, d);
3033bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        invalidatePressedFocusedStates(searchButtonContainer, searchButton);
30344ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
30354ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3036c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private boolean updateVoiceSearchIcon(boolean searchVisible) {
30371cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final View searchDivider = findViewById(R.id.search_divider);
3038c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
30391cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final View voiceButton = findViewById(R.id.voice_button);
3040e3fbfa933e89e43b66b69deeaa2cfa5f322b78e6Winson Chung        final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
30411cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung
3042c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // We only show/update the voice search icon if the search icon is enabled as well
3043ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        final SearchManager searchManager =
3044ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3045ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3046ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3047ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        ComponentName activityName = null;
3048ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        if (globalSearchActivity != null) {
3049ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // Check if the global search activity handles voice search
3050ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3051ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            intent.setPackage(globalSearchActivity.getPackageName());
3052ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            activityName = intent.resolveActivity(getPackageManager());
3053ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
3054ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3055ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        if (activityName == null) {
3056ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // Fallback: check if an activity other than the global search activity
3057ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // resolves this
3058ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3059ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            activityName = intent.resolveActivity(getPackageManager());
3060ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
3061c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        if (searchVisible && activityName != null) {
3062dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3063dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3064ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3065ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3066ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (sVoiceSearchIcon[coi] == null) {
3067ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3068ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3069ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        TOOLBAR_ICON_METADATA_NAME);
3070ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
3071649723cfb3d73af16dd02462725700897ca60e38Winson Chung            if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
3072c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
30731cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung            voiceButton.setVisibility(View.VISIBLE);
307414091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller            if (voiceButtonProxy != null) {
307514091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller                voiceButtonProxy.setVisibility(View.VISIBLE);
307614091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller            }
3077bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
3078c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return true;
30791cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        } else {
3080649723cfb3d73af16dd02462725700897ca60e38Winson Chung            if (searchDivider != null) searchDivider.setVisibility(View.GONE);
3081c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
30821cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung            voiceButton.setVisibility(View.GONE);
308314091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller            if (voiceButtonProxy != null) {
308414091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller                voiceButtonProxy.setVisibility(View.GONE);
308514091b1df4dded743ecdbfe7caabfbaaaaaf0713Jim Miller            }
3086c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return false;
3087ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        }
30886ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    }
30890423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka
3090e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateVoiceSearchIcon(Drawable.ConstantState d) {
3091bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3092bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View voiceButton = findViewById(R.id.voice_button);
30934ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        updateButtonWithDrawable(R.id.voice_button, d);
3094bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
30954ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
30964ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
30976ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    /**
3098eb66b14000712b92fa6a5ba28f5bc47ce8fab6bdWinson Chung     * Sets the app market icon
30996ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka     */
31006ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    private void updateAppMarketIcon() {
3101785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        final View marketButton = findViewById(R.id.market_button);
3102785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3103785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Find the app market activity by resolving an intent.
3104785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // (If multiple app markets are installed, it will return the ResolverActivity.)
3105785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ComponentName activityName = intent.resolveActivity(getPackageManager());
31066a26e5bed48bf64996aef3ea1db4767ee147640eWinson Chung        if (activityName != null) {
3107dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3108785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppMarketIntent = intent;
3109dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3110ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3111ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_ICON_METADATA_NAME);
3112785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setVisibility(View.VISIBLE);
3113785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        } else {
3114785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            // We should hide and disable the view so that we don't try and restore the visibility
3115785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            // of it when we swap between drag & normal states from IconDropTarget subclasses.
3116785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setVisibility(View.GONE);
3117785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setEnabled(false);
31180423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
3119ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    }
3120ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
3121e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateAppMarketIcon(Drawable.ConstantState d) {
31221b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
31231b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        Resources r = getResources();
31241b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        Drawable marketIconDrawable = d.newDrawable(r);
31251b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
31261b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
31271b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        marketIconDrawable.setBounds(0, 0, w, h);
31281b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung
31291b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
31304ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
31314ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3132d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    @Override
3133d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3134d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        boolean result = super.dispatchPopulateAccessibilityEvent(event);
3135d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        final List<CharSequence> text = event.getText();
3136d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        text.clear();
3137d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        text.add(getString(R.string.home));
3138d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        return result;
3139d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    }
3140d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka
3141ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    /**
3142b8472bb83cd96456a543137e1e56d589171934c3Winson Chung     * Receives notifications when system dialogs are to be closed.
31432ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     */
31442ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
31452ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        @Override
31462ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        public void onReceive(Context context, Intent intent) {
31472ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            closeSystemDialogs();
31482ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        }
31492ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    }
31502ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
31512ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    /**
31521e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     * Receives notifications whenever the appwidgets are reset.
31531e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     */
31541e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private class AppWidgetResetObserver extends ContentObserver {
31551e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        public AppWidgetResetObserver() {
31561e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey            super(new Handler());
31571e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        }
31581e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
31591e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        @Override
31601e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        public void onChange(boolean selfChange) {
31611e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey            onAppWidgetReset();
31621e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        }
31631e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
31641e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
31651e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    /**
3166ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * If the activity is currently paused, signal that we need to re-run the loader
3167ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * in onResume.
3168ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3169ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * This needs to be called from incoming places where resources might have been loaded
3170ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * while we are paused.  That is becaues the Configuration might be wrong
3171ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * when we're not running, and if it comes back to what it was when we
3172ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * were paused, we are not restarted.
3173ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3174ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
3175ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3176ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * @return true if we are currently paused.  The caller might be able to
3177ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * skip some work in that case since we will come back again.
3178ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     */
3179ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    public boolean setLoadOnResume() {
3180ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        if (mPaused) {
3181ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            Log.i(TAG, "setLoadOnResume");
3182ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            mOnResumeNeedsLoad = true;
3183ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            return true;
3184ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        } else {
3185ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            return false;
3186ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        }
3187ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    }
3188ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3189ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    /**
31909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
319131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
31929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public int getCurrentWorkspaceScreen() {
3193d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        if (mWorkspace != null) {
31940142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            return mWorkspace.getCurrentPage();
3195d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        } else {
3196d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato            return SCREEN_COUNT / 2;
3197d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        }
31989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
3199f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
32009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
32019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Refreshes the shortcuts shown on the workspace.
32029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
32039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
32049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
32059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void startBinding() {
32069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final Workspace workspace = mWorkspace;
3207df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
3208f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimatePage = -1;
3209f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimateViews.clear();
3210df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        mWorkspace.clearDropTargets();
32119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int count = workspace.getChildCount();
32129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        for (int i = 0; i < count; i++) {
32133c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato            // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
32147a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung            final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
32157a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung            layoutParent.removeAllViewsInLayout();
32169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
321705bf644e371c9d965ba864cf7dda6515c7cdce91Michael Jurka        mWidgetsToAdvance.clear();
32183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mHotseat != null) {
32193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mHotseat.resetLayout();
322031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
322131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
322231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
322331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
32249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Bind the items start-end from the list.
32259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
32269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
322731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
32289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3229ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3230ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3231f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Get the list of added shortcuts and intersect them with the set of shortcuts here
3232f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Set<String> newApps = new HashSet<String>();
3233f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3234f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3235f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Workspace workspace = mWorkspace;
3236f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        for (int i = start; i < end; i++) {
32379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            final ItemInfo item = shortcuts.get(i);
32384d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung
32394d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            // Short circuit if we are loading dock items for a configuration which has no dock
32404d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
32414d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung                    mHotseat == null) {
32424d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung                continue;
32434d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            }
32444d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung
32459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            switch (item.itemType) {
32469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
32479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3248f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    ShortcutInfo info = (ShortcutInfo) item;
3249f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    String uri = info.intent.toUri(0).toString();
3250f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    View shortcut = createShortcut(info);
32513d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
32523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            item.cellY, 1, 1, false);
3253bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    boolean animateIconUp = false;
3254bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    synchronized (newApps) {
3255bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                        if (newApps.contains(uri)) {
3256bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                            animateIconUp = newApps.remove(uri);
3257bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                        }
3258bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    }
3259bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    if (animateIconUp) {
3260f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        // Prepare the view to be animated up
3261f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setAlpha(0f);
3262f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setScaleX(0f);
3263f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setScaleY(0f);
3264f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        mNewShortcutAnimatePage = item.screen;
3265f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        if (!mNewShortcutAnimateViews.contains(shortcut)) {
3266f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            mNewShortcutAnimateViews.add(shortcut);
3267f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        }
3268f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    }
32699c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    break;
3270df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
32713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
32720142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                            (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
3273df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                            (FolderInfo) item, mIconCache);
32743d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
32753d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                            item.cellY, 1, 1, false);
32769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    break;
32779c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
327831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3279f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
32809c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        workspace.requestLayout();
32819c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
32829c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
32839c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
32849c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
32859c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3286ad72e1705c32da463a552f2283ae467dc15a4a34Joe Onorato    public void bindFolders(HashMap<Long, FolderInfo> folders) {
3287ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3288319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.clear();
3289319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.putAll(folders);
329031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
329131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
32929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
32939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add the views for a widget to the workspace.
32949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
32959c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
32969c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
32979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void bindAppWidget(LauncherAppWidgetInfo item) {
3298ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3299ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3300843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3301843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3302843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bindAppWidget: " + item);
3303843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
33049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final Workspace workspace = mWorkspace;
3305cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
33069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final int appWidgetId = item.appWidgetId;
33079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
3308843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3309843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3310843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
3311843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
33129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
331331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
33149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.hostView.setTag(item);
3315211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung        item.onBindAppWidget(this);
331631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
33173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
33189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                item.cellY, item.spanX, item.spanY, false);
3319ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3320ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
33219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        workspace.requestLayout();
3322cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3323843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3324843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3325843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    + (SystemClock.uptimeMillis()-start) + "ms");
3326843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
33279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
3328cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
33299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
33309c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Callback saying that there aren't any more items to bind.
33319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
33329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
33339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
33349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void finishBindingItems() {
3335ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3336ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
33379c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (mSavedState != null) {
33389c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (!mWorkspace.hasFocus()) {
33390142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
33409c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
33419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mSavedState = null;
3342138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        }
3343cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
33449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (mSavedInstanceState != null) {
33459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            super.onRestoreInstanceState(mSavedInstanceState);
33469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mSavedInstanceState = null;
334799c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey        }
3348cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3349002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        // If we received the result of any pending adds while the loader was running (e.g. the
3350002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        // widget configuration forced an orientation change), process them now.
3351002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        for (int i = 0; i < sPendingAddList.size(); i++) {
3352002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            completeAdd(sPendingAddList.get(i));
3353002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        }
3354002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        sPendingAddList.clear();
335599c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey
3356c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // Update the market app icon as necessary (the other icons will be managed in response to
3357c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // package changes in bindSearchablesChanged()
33586d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani        updateAppMarketIcon();
3359c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka
3360f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Animate up any icons as necessary
3361f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        if (mVisible || mWorkspaceLoading) {
3362f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            Runnable newAppsRunnable = new Runnable() {
3363f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                @Override
3364f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                public void run() {
3365a2413751e3a698aef9c87411a639637883856939Winson Chung                    runNewAppsAnimation(false);
3366f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                }
3367f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            };
3368a2413751e3a698aef9c87411a639637883856939Winson Chung
3369a2413751e3a698aef9c87411a639637883856939Winson Chung            boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3370a2413751e3a698aef9c87411a639637883856939Winson Chung                    mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3371a2413751e3a698aef9c87411a639637883856939Winson Chung            if (canRunNewAppsAnimation()) {
3372a2413751e3a698aef9c87411a639637883856939Winson Chung                // If the user has not interacted recently, then either snap to the new page to show
3373a2413751e3a698aef9c87411a639637883856939Winson Chung                // the new-apps animation or just run them if they are to appear on the current page
3374a2413751e3a698aef9c87411a639637883856939Winson Chung                if (willSnapPage) {
3375a2413751e3a698aef9c87411a639637883856939Winson Chung                    mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3376a2413751e3a698aef9c87411a639637883856939Winson Chung                } else {
3377a2413751e3a698aef9c87411a639637883856939Winson Chung                    runNewAppsAnimation(false);
3378a2413751e3a698aef9c87411a639637883856939Winson Chung                }
3379f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            } else {
3380318eee06ae69ac5ef8fddde984eb2f7601d2101dWinson Chung                // If the user has interacted recently, then just add the items in place if they
3381a2413751e3a698aef9c87411a639637883856939Winson Chung                // are on another page (or just normally if they are added to the current page)
3382a2413751e3a698aef9c87411a639637883856939Winson Chung                runNewAppsAnimation(willSnapPage);
3383f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3384f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }
3385f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3386f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mWorkspaceLoading = false;
3387f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
3388f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3389a2413751e3a698aef9c87411a639637883856939Winson Chung    private boolean canRunNewAppsAnimation() {
3390a2413751e3a698aef9c87411a639637883856939Winson Chung        long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3391a2413751e3a698aef9c87411a639637883856939Winson Chung        return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3392a2413751e3a698aef9c87411a639637883856939Winson Chung    }
3393a2413751e3a698aef9c87411a639637883856939Winson Chung
3394f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    /**
3395f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     * Runs a new animation that scales up icons that were added while Launcher was in the
3396f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     * background.
3397a2413751e3a698aef9c87411a639637883856939Winson Chung     *
3398a2413751e3a698aef9c87411a639637883856939Winson Chung     * @param immediate whether to run the animation or show the results immediately
3399f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     */
3400a2413751e3a698aef9c87411a639637883856939Winson Chung    private void runNewAppsAnimation(boolean immediate) {
3401f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        AnimatorSet anim = new AnimatorSet();
3402f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Collection<Animator> bounceAnims = new ArrayList<Animator>();
3403a2413751e3a698aef9c87411a639637883856939Winson Chung
3404a2413751e3a698aef9c87411a639637883856939Winson Chung        // Order these new views spatially so that they animate in order
3405f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3406f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            @Override
3407f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            public int compare(View a, View b) {
3408f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3409f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3410f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                int cellCountX = LauncherModel.getCellCountX();
3411f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3412f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3413f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        });
3414a2413751e3a698aef9c87411a639637883856939Winson Chung
3415a2413751e3a698aef9c87411a639637883856939Winson Chung        // Animate each of the views in place (or show them immediately if requested)
3416a2413751e3a698aef9c87411a639637883856939Winson Chung        if (immediate) {
3417a2413751e3a698aef9c87411a639637883856939Winson Chung            for (View v : mNewShortcutAnimateViews) {
3418a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setAlpha(1f);
3419a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setScaleX(1f);
3420a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setScaleY(1f);
3421f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3422a2413751e3a698aef9c87411a639637883856939Winson Chung        } else {
3423a2413751e3a698aef9c87411a639637883856939Winson Chung            for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3424a2413751e3a698aef9c87411a639637883856939Winson Chung                View v = mNewShortcutAnimateViews.get(i);
3425a2413751e3a698aef9c87411a639637883856939Winson Chung                ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3426a2413751e3a698aef9c87411a639637883856939Winson Chung                        PropertyValuesHolder.ofFloat("alpha", 1f),
3427a2413751e3a698aef9c87411a639637883856939Winson Chung                        PropertyValuesHolder.ofFloat("scaleX", 1f),
3428a2413751e3a698aef9c87411a639637883856939Winson Chung                        PropertyValuesHolder.ofFloat("scaleY", 1f));
3429a2413751e3a698aef9c87411a639637883856939Winson Chung                bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3430a2413751e3a698aef9c87411a639637883856939Winson Chung                bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3431a2413751e3a698aef9c87411a639637883856939Winson Chung                bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3432a2413751e3a698aef9c87411a639637883856939Winson Chung                bounceAnims.add(bounceAnim);
3433a2413751e3a698aef9c87411a639637883856939Winson Chung            }
3434a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.playTogether(bounceAnims);
3435a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.addListener(new AnimatorListenerAdapter() {
3436a2413751e3a698aef9c87411a639637883856939Winson Chung                @Override
3437a2413751e3a698aef9c87411a639637883856939Winson Chung                public void onAnimationEnd(Animator animation) {
3438a2413751e3a698aef9c87411a639637883856939Winson Chung                    mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3439a2413751e3a698aef9c87411a639637883856939Winson Chung                }
3440a2413751e3a698aef9c87411a639637883856939Winson Chung            });
3441a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.start();
3442a2413751e3a698aef9c87411a639637883856939Winson Chung        }
3443f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3444f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Clean up
3445f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimatePage = -1;
3446f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimateViews.clear();
3447f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        new Thread("clearNewAppsThread") {
3448f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            public void run() {
3449f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                mSharedPrefs.edit()
3450f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3451f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3452f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .commit();
3453f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3454f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }.start();
34556d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
34566d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
3457cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
3458cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    public void bindSearchablesChanged() {
3459c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean searchVisible = updateGlobalSearchIcon();
3460c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3461c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3462cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    }
3463cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath
34646d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    /**
34659c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add the icons for all apps.
34669c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
34679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
34689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3469c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3470c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        // Remove the progress bar entirely; we could also make it GONE
3471c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        // but better to remove it since we know it's not going to be used
3472c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        View progressBar = mAppsCustomizeTabHost.
3473c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            findViewById(R.id.apps_customize_progress_bar);
3474c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        if (progressBar != null) {
3475c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            ((ViewGroup)progressBar.getParent()).removeView(progressBar);
3476c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        }
3477c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        // We just post the call to setApps so the user sees the progress bar
3478c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        // disappear-- otherwise, it just looks like the progress bar froze
3479c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        // which doesn't look great
3480c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        mAppsCustomizeTabHost.post(new Runnable() {
3481c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            public void run() {
3482c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka                if (mAppsCustomizeContent != null) {
3483c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka                    mAppsCustomizeContent.setApps(apps);
3484c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka                }
3485c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka            }
3486c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka        });
34879c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
3488cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
34899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
34909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * A package was installed.
34919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
34929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
34939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
349464e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
3495ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3496f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3497785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3498785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.addApps(apps);
3499785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
35009c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
35019c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
35029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
35039c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * A package was updated.
35049c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
35059c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
35069c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
350764e6be78dc72e1a89fe8fb95c502586f9260df28Joe Onorato    public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
3508ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        setLoadOnResume();
3509f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy        if (mWorkspace != null) {
3510f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy            mWorkspace.updateShortcuts(apps);
3511f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy        }
3512f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3513785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3514785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.updateApps(apps);
3515785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
35169c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
35179c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
35189c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
35199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * A package was uninstalled.
35209c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
35219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
35229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
35233611578579b4bfb25616085dafdb1a45207394f9Joe Onorato    public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
35243611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        if (permanent) {
35253611578579b4bfb25616085dafdb1a45207394f9Joe Onorato            mWorkspace.removeItems(apps);
35263611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3527f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3528785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3529785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.removeApps(apps);
3530785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3531a182096cb20e0580dfca6661b2e5871c8886c834Winson Chung
3532a182096cb20e0580dfca6661b2e5871c8886c834Winson Chung        // Notify the drag controller
3533a182096cb20e0580dfca6661b2e5871c8886c834Winson Chung        mDragController.onAppsRemoved(apps, this);
353431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3535be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3536be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    /**
353780baf5a6b3c62a62265f626d43d1167783c94131Winson Chung     * A number of packages were updated.
353880baf5a6b3c62a62265f626d43d1167783c94131Winson Chung     */
353980baf5a6b3c62a62265f626d43d1167783c94131Winson Chung    public void bindPackagesUpdated() {
3540785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3541785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.onPackagesUpdated();
3542785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
354380baf5a6b3c62a62265f626d43d1167783c94131Winson Chung    }
354480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
3545400438b79fe412cb625c96297edeea9c6155349eWinson Chung    private int mapConfigurationOriActivityInfoOri(int configOri) {
3546400438b79fe412cb625c96297edeea9c6155349eWinson Chung        final Display d = getWindowManager().getDefaultDisplay();
3547400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3548400438b79fe412cb625c96297edeea9c6155349eWinson Chung        switch (d.getRotation()) {
3549400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_0:
3550400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_180:
3551400438b79fe412cb625c96297edeea9c6155349eWinson Chung            // We are currently in the same basic orientation as the natural orientation
3552400438b79fe412cb625c96297edeea9c6155349eWinson Chung            naturalOri = configOri;
3553400438b79fe412cb625c96297edeea9c6155349eWinson Chung            break;
3554400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_90:
3555400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_270:
3556400438b79fe412cb625c96297edeea9c6155349eWinson Chung            // We are currently in the other basic orientation to the natural orientation
3557400438b79fe412cb625c96297edeea9c6155349eWinson Chung            naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3558400438b79fe412cb625c96297edeea9c6155349eWinson Chung                    Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3559400438b79fe412cb625c96297edeea9c6155349eWinson Chung            break;
3560400438b79fe412cb625c96297edeea9c6155349eWinson Chung        }
3561400438b79fe412cb625c96297edeea9c6155349eWinson Chung
3562400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int[] oriMap = {
3563400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3564400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3565400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3566400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3567400438b79fe412cb625c96297edeea9c6155349eWinson Chung        };
3568400438b79fe412cb625c96297edeea9c6155349eWinson Chung        // Since the map starts at portrait, we need to offset if this device's natural orientation
3569400438b79fe412cb625c96297edeea9c6155349eWinson Chung        // is landscape.
3570400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int indexOffset = 0;
3571400438b79fe412cb625c96297edeea9c6155349eWinson Chung        if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3572400438b79fe412cb625c96297edeea9c6155349eWinson Chung            indexOffset = 1;
3573400438b79fe412cb625c96297edeea9c6155349eWinson Chung        }
3574400438b79fe412cb625c96297edeea9c6155349eWinson Chung        return oriMap[(d.getRotation() + indexOffset) % 4];
3575400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
3576446e940fc8839173de12f1299b8c24f318d53a3fAdam Cohen
35774b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    public boolean isRotationEnabled() {
35789bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka        boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
35794b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        boolean enableRotation = forceEnableRotation ||
35804b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                getResources().getBoolean(R.bool.allow_rotation);
35814b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        return enableRotation;
35824b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    }
3583641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung    public void lockScreenOrientation() {
35844b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        if (isRotationEnabled()) {
35854b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
35864b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    .getConfiguration().orientation));
35874b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        }
3588400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
35894b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    public void unlockScreenOrientation(boolean immediate) {
35904b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        if (isRotationEnabled()) {
35914b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            if (immediate) {
3592641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
35934b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            } else {
35944b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                mHandler.postDelayed(new Runnable() {
35954b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    public void run() {
35964b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
35974b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    }
35984b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                }, mRestoreScreenOrientationDelay);
3599641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung            }
36004b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        }
3601400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
3602400438b79fe412cb625c96297edeea9c6155349eWinson Chung
360382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    /* Cling related */
36047d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private boolean isClingsEnabled() {
36052a9e228e6fd719732639beb880d6df64d48158a1Brett Chabot        // disable clings when running in a test harness
36067d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        if(ActivityManager.isRunningInTestHarness()) return false;
36072a9e228e6fd719732639beb880d6df64d48158a1Brett Chabot
36087d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        return true;
36097d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
36107d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
36117d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        Cling cling = (Cling) findViewById(clingId);
36127d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        if (cling != null) {
36137d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            cling.init(this, positionData);
361482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            cling.setVisibility(View.VISIBLE);
36157d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
36167d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            if (animate) {
36177d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.buildLayer();
36187d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.setAlpha(0f);
36197d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.animate()
36207d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .alpha(1f)
36217a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chung                    .setInterpolator(new AccelerateInterpolator())
36227d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .setDuration(SHOW_CLING_DURATION)
36237d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .setStartDelay(delay)
36247d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .start();
36257d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            } else {
36267d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.setAlpha(1f);
36277d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            }
362882f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        }
36297d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        return cling;
363082f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
36317d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private void dismissCling(final Cling cling, final String flag, int duration) {
363282f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        if (cling != null) {
363382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
36347d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            anim.setDuration(duration);
363582f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            anim.addListener(new AnimatorListenerAdapter() {
363682f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                public void onAnimationEnd(Animator animation) {
363782f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                    cling.setVisibility(View.GONE);
363882f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                    cling.cleanup();
363946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    // We should update the shared preferences on a background thread
364046353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    new Thread("dismissClingThread") {
364146353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                        public void run() {
364246353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            SharedPreferences.Editor editor = mSharedPrefs.edit();
364346353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            editor.putBoolean(flag, true);
364446353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            editor.commit();
364546353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                        }
364646353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    }.start();
364782f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                };
364882f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            });
364982f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            anim.start();
365082f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        }
365182f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
36529d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung    private void removeCling(int id) {
36539d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        final View cling = findViewById(id);
36549d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        if (cling != null) {
36559d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            final ViewGroup parent = (ViewGroup) cling.getParent();
36569d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            parent.post(new Runnable() {
36579d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                @Override
36589d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                public void run() {
36599d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                    parent.removeView(cling);
36609d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                }
36619d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            });
36629d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        }
36639d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung    }
3664974c3864bb438fc924913e0017385f873332d052Michael Jurka
3665974c3864bb438fc924913e0017385f873332d052Michael Jurka    private boolean skipCustomClingIfNoAccounts() {
3666974c3864bb438fc924913e0017385f873332d052Michael Jurka        Cling cling = (Cling) findViewById(R.id.workspace_cling);
3667974c3864bb438fc924913e0017385f873332d052Michael Jurka        boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3668974c3864bb438fc924913e0017385f873332d052Michael Jurka        if (customCling) {
3669974c3864bb438fc924913e0017385f873332d052Michael Jurka            AccountManager am = AccountManager.get(this);
3670974c3864bb438fc924913e0017385f873332d052Michael Jurka            Account[] accounts = am.getAccountsByType("com.google");
3671974c3864bb438fc924913e0017385f873332d052Michael Jurka            return accounts.length == 0;
3672974c3864bb438fc924913e0017385f873332d052Michael Jurka        }
3673974c3864bb438fc924913e0017385f873332d052Michael Jurka        return false;
3674974c3864bb438fc924913e0017385f873332d052Michael Jurka    }
3675974c3864bb438fc924913e0017385f873332d052Michael Jurka
36767d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void showFirstRunWorkspaceCling() {
36777d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
367846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
3679974c3864bb438fc924913e0017385f873332d052Michael Jurka                !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3680974c3864bb438fc924913e0017385f873332d052Michael Jurka                !skipCustomClingIfNoAccounts() ) {
36817d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            initCling(R.id.workspace_cling, null, false, 0);
36829d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
36839d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.workspace_cling);
36847d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
36857d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
36867d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void showFirstRunAllAppsCling(int[] position) {
36877d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
368846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
368946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
36907d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            initCling(R.id.all_apps_cling, position, true, 0);
36919d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
36929d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.all_apps_cling);
36937d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
36947d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
36957d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public Cling showFirstRunFoldersCling() {
36967d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
369746353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
369846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
369946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung            return initCling(R.id.folder_cling, null, true, 0);
37009d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
37019d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.folder_cling);
370246353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung            return null;
37037d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
37047d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
37057d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public boolean isFolderClingVisible() {
37067d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        Cling cling = (Cling) findViewById(R.id.folder_cling);
37079d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        if (cling != null) {
37089d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            return cling.getVisibility() == View.VISIBLE;
37099d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        }
37109d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        return false;
37117d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
371282f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    public void dismissWorkspaceCling(View v) {
371382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        Cling cling = (Cling) findViewById(R.id.workspace_cling);
37147d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
371582f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
371682f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    public void dismissAllAppsCling(View v) {
371782f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        Cling cling = (Cling) findViewById(R.id.all_apps_cling);
37187d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
37197d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
37207d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void dismissFolderCling(View v) {
37217d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        Cling cling = (Cling) findViewById(R.id.folder_cling);
37227d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
372382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
372482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung
372580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung    /**
3726be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato     * Prints out out state for debugging.
3727be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato     */
3728be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
3729be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
373039bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mSavedState=" + mSavedState);
373139bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
373239bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mRestoring=" + mRestoring);
373339bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
373439bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3735319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        Log.d(TAG, "sFolders.size=" + sFolders.size());
3736be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        mModel.dumpState();
3737f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3738785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3739785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.dumpState();
3740785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
3741be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        Log.d(TAG, "END launcher2 dump state");
3742be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
374316d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen
374416d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    @Override
374516d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
374616d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        super.dump(prefix, fd, writer, args);
374716d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        writer.println(" ");
374816d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        writer.println("Debug logs: ");
374916d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        for (int i = 0; i < sDumpLogs.size(); i++) {
375016d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen            writer.println("  " + sDumpLogs.get(i));
375116d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        }
375216d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    }
375331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
37542763be3150d429d338b9848286e9365b4052c100Michael Jurka
3755abded66084680bb31cc7ea403c88f44f79a3c884Michael Jurkainterface LauncherTransitionable {
37562a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka    View getContent();
3757a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
3758bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
37597044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    void onLauncherTransitionStep(Launcher l, float t);
3760bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
37612763be3150d429d338b9848286e9365b4052c100Michael Jurka}
3762