Launcher.java revision 39a06042556a90c98f55a92bda5811e7d9492d13
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
18325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerpackage com.android.launcher3;
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;
5396d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.graphics.Color;
54268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohenimport android.graphics.PorterDuff;
55af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.graphics.Rect;
56af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurkaimport android.graphics.drawable.Drawable;
57c9b1877f9acf604897c719d70dc99685d10849b5Daniel Sandlerimport android.net.Uri;
58319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrickimport android.os.AsyncTask;
5931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.os.Bundle;
600fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauerimport android.os.Environment;
611e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkeyimport android.os.Handler;
62ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohenimport android.os.Message;
63a2413751e3a698aef9c87411a639637883856939Winson Chungimport android.os.StrictMode;
64843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandlerimport android.os.SystemClock;
654ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroyimport android.provider.Settings;
666d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasaniimport android.speech.RecognizerIntent;
6731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.Selection;
6831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.SpannableStringBuilder;
69a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurkaimport android.text.TextUtils;
7031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.text.method.TextKeyListener;
717c312c15e33b6cffbda707429e88ade498fc9f5eJoe Onoratoimport android.util.Log;
7296d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.Display;
7396d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.Gravity;
7496d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.HapticFeedbackConstants;
7596d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.KeyEvent;
7696d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.LayoutInflater;
7796d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.Menu;
7896d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.MenuItem;
7996d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.MotionEvent;
8096d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.Surface;
8196d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.View;
820cf2a7c3eeb75cbd532b011524140a4b963f2c93Adam Cohenimport android.view.View.OnLongClickListener;
8396d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.ViewGroup;
8496d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.ViewTreeObserver;
852a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurkaimport android.view.ViewTreeObserver.OnGlobalLayoutListener;
8696d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.view.WindowManager;
87815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganovimport android.view.accessibility.AccessibilityEvent;
88cff6af81d721415785bddc2efd74e179d2def9daAdam Cohenimport android.view.animation.AccelerateDecelerateInterpolator;
897a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chungimport android.view.animation.AccelerateInterpolator;
90f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohenimport android.view.animation.DecelerateInterpolator;
9131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Projectimport android.view.inputmethod.InputMethodManager;
9296d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.widget.Advanceable;
9396d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.widget.FrameLayout;
9496d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.widget.ImageView;
9596d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.widget.TextView;
9696d30a165185dc59617e181314f4d22634e22952Adam Cohenimport android.widget.Toast;
974ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy
98325dc23624160689e59fbac708cf6f222b20d025Daniel Sandlerimport com.android.launcher3.DropTarget.DragObject;
99edcce099c98a6c40d10109ac092ab50f9d2668f3Romain Guy
100c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.DataInputStream;
101c0dcf597084d00e4c23a7fea5fd0738f6c095a6bAdam Cohenimport java.io.DataOutputStream;
10216d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohenimport java.io.FileDescriptor;
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 */
118c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringertpublic class Launcher extends Activity
1190e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120cd81073eac537e9f85eeee14588d513ea8a56e17Winson Chung                   View.OnTouchListener {
121a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato    static final String TAG = "Launcher";
122cc67f476c01ee6a7d593fa67f80392c6793432d7Joe Onorato    static final boolean LOGD = false;
12331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
124f061f82461b8afe0c62eecb776e807c14bc0a4aaDaniel Sandler    static final boolean PROFILE_STARTUP = false;
125843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler    static final boolean DEBUG_WIDGETS = false;
126a2413751e3a698aef9c87411a639637883856939Winson Chung    static final boolean DEBUG_STRICT_MODE = false;
127f061f82461b8afe0c62eecb776e807c14bc0a4aaDaniel Sandler    static final boolean DEBUG_RESUME_TIME = false;
1286fefcf1f83ba05e898ea7164a68dcced657bb43eRomain Guy
12970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_GROUP_WALLPAPER = 1;
13070d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
13170d721061536e0634f6cfa8cd219759a83befceeWinson Chung    private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
132236d431cc582df9b2841718195ae977c3948f751Winson Chung    private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133236d431cc582df9b2841718195ae977c3948f751Winson Chung    private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
13431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
13531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_CREATE_SHORTCUT = 1;
1367376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private static final int REQUEST_CREATE_APPWIDGET = 5;
13731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_PICK_APPLICATION = 6;
13831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final int REQUEST_PICK_SHORTCUT = 7;
1397376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private static final int REQUEST_PICK_APPWIDGET = 9;
140a0618e4102d0543d953d81e162d93c8ea641d0fdMike Cleron    private static final int REQUEST_PICK_WALLPAPER = 10;
14131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1428b805b17158886035b38261eb611d8641701ae43Michael Jurka    private static final int REQUEST_BIND_APPWIDGET = 11;
143876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood
144876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood    /**
145876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood     * IntentStarter uses request codes starting with this. This must be greater than all activity
146876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood     * request codes used internally.
147876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood     */
148876a846edc6a9d9c6957ee0f63b01951763e3606Mathew Inwood    protected static final int REQUEST_LAST = 100;
1498b805b17158886035b38261eb611d8641701ae43Michael Jurka
15031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
15131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1523a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    static final int SCREEN_COUNT = 5;
1533a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    static final int DEFAULT_SCREEN = 2;
15431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
15598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static final String PREFERENCES = "launcher.preferences";
1560a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    // To turn on these properties, type
1570a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
1580a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
1590a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
16031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1612672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung    // The Intent extra that defines whether to ignore the launch animation
1622672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung    static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
163325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler            "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
1642672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung
16531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
16631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
167883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    // Type: int
168883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    private static final String RUNTIME_STATE = "launcher.state";
16931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
1703d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
1713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    // Type: int
17231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
17331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
174fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
17531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: int
176fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
17731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: boolean
17831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
17931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    // Type: long
18031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
1819d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: int
1829d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
1839d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: int
1849d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
1859d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    // Type: parcelable
1869d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
18731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
188e441bbc7d2baee03e96897b80f55db5ce2aa7e1fBjorn Bringert    private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
189ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
190e441bbc7d2baee03e96897b80f55db5ce2aa7e1fBjorn Bringert            "com.android.launcher.toolbar_search_icon";
191ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
192e441bbc7d2baee03e96897b80f55db5ce2aa7e1fBjorn Bringert            "com.android.launcher.toolbar_voice_search_icon";
193ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
19439a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
19539a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
1966b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    /** The different states that Launcher can be in. */
1974a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
198c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    private State mState = State.WORKSPACE;
199c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    private AnimatorSet mStateAnimation;
200b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private AnimatorSet mDividerAnimator;
2016b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
2029c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    static final int APPWIDGET_HOST_ID = 1024;
203557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
2057a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chung    private static final int SHOW_CLING_DURATION = 550;
2067d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private static final int DISMISS_CLING_DURATION = 250;
20731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
20831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private static final Object sLock = new Object();
2093a2b3f2be58843d26549fb0ec6c6533627c7cd19Mike Cleron    private static int sScreen = DEFAULT_SCREEN;
21031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
211a2413751e3a698aef9c87411a639637883856939Winson Chung    // How long to wait before the new-shortcut animation automatically pans the workspace
212a2413751e3a698aef9c87411a639637883856939Winson Chung    private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
21364359a53dc827797917a8d9a0697a91996789801Winson Chung    private static int NEW_APPS_ANIMATION_DELAY = 500;
214a2413751e3a698aef9c87411a639637883856939Winson Chung
2152ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    private final BroadcastReceiver mCloseSystemDialogsReceiver
2162ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            = new CloseSystemDialogsIntentReceiver();
2171e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
2181e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
21931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private LayoutInflater mInflater;
22031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
22131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Workspace mWorkspace;
222b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private View mQsbDivider;
223360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner    private View mLauncherView;
224b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private DragLayer mDragLayer;
225b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    private DragController mDragController;
22639a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    private View mWeightWatcher;
227cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
2287376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private AppWidgetManager mAppWidgetManager;
2297376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    private LauncherAppWidgetHost mAppWidgetHost;
230cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
231c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka    private ItemInfo mPendingAddInfo = new ItemInfo();
2329d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    private AppWidgetProviderInfo mPendingAddWidgetInfo;
2339d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen
2340280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private int[] mTmpAddItemCellCoordinates = new int[2];
2350280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
23631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private FolderInfo mFolderInfo;
23731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    private Hotseat mHotseat;
239f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    private View mAllAppsButton;
2403d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
241c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private SearchDropTargetBar mSearchDropTargetBar;
242785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    private AppsCustomizeTabHost mAppsCustomizeTabHost;
243785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung    private AppsCustomizePagedView mAppsCustomizeContent;
244ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private boolean mAutoAdvanceRunning = false;
2452b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
24631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Bundle mSavedState;
2474a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    // We set the state in both onCreate and then onNewIntent in some cases, which causes both
2484a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    // scroll issues (because the workspace may not have been measured yet) and extra work.
2494a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
2504a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    private State mOnResumeState = State.NONE;
25131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
25231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private SpannableStringBuilder mDefaultKeySsb = null;
25331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private boolean mWorkspaceLoading = true;
2559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
25634a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato    private boolean mPaused = true;
25731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean mRestoring;
25831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean mWaitingForResult;
259ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    private boolean mOnResumeNeedsLoad;
26031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2617607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
2627607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka
2634a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    // Keep track of whether the user has left launcher
2644a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    private static boolean sPausedFromUserAction = false;
2654a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung
26631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private Bundle mSavedInstanceState;
26731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    private LauncherModel mModel;
2690589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    private IconCache mIconCache;
270d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mUserPresent = true;
271d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mVisible = false;
272d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen    private boolean mAttached = false;
2737564d98164c9fcba178e0ab0faf5066e33ad9a31Adam Cohen    private static final boolean DISABLE_CLINGS = true;
2749c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
275319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick    private static LocaleConfiguration sLocaleConfiguration = null;
276319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
277319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick    private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
278cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
279ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    private Intent mAppMarketIntent = null;
280ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
281ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    // Related to the auto-advancing of widgets
282ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int ADVANCE_MSG = 1;
283ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int mAdvanceInterval = 20000;
284ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final int mAdvanceStagger = 250;
285ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private long mAutoAdvanceSentTime;
286ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private long mAutoAdvanceTimeLeft = -1;
287ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
288ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        new HashMap<View, AppWidgetProviderInfo>();
289ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
290400438b79fe412cb625c96297edeea9c6155349eWinson Chung    // Determines how long to wait after a rotation before restoring the screen orientation to
291400438b79fe412cb625c96297edeea9c6155349eWinson Chung    // match the sensor state.
292400438b79fe412cb625c96297edeea9c6155349eWinson Chung    private final int mRestoreScreenOrientationDelay = 500;
293400438b79fe412cb625c96297edeea9c6155349eWinson Chung
2944ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    // External icons saved in case of resource changes, orientation, etc.
295dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
296dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
297dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
2984ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
299360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner    private Drawable mWorkspaceBackgroundDrawable;
300360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner
3011462de39f01cec0ba785386532719cb0207dd827Adam Cohen    private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
3021462de39f01cec0ba785386532719cb0207dd827Adam Cohen
30316d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    static final ArrayList<String> sDumpLogs = new ArrayList<String>();
30416d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen
30546353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    // We only want to get the SharedPreferences once since it does an FS stat each time we get
30646353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    // it from the context.
30746353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung    private SharedPreferences mSharedPrefs;
30846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung
309e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen    private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
310e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen
311f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    // Holds the page that we need to animate to, and the icon views that we need to animate up
312f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    // when we scroll to that page on resume.
313dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    private long mNewShortcutAnimateScreenId = -1;
314f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
315268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private ImageView mFolderIconImageView;
316268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Bitmap mFolderIconBitmap;
317268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Canvas mFolderIconCanvas;
318268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private Rect mRectForFolderAnimation = new Rect();
3192801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
320ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka    private BubbleTextView mWaitingForResume;
321ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka
32222143131770f7e90be4181e88ced0cb1d7158306Michael Jurka    private HideFromAccessibilityHelper mHideFromAccessibilityHelper
32322143131770f7e90be4181e88ced0cb1d7158306Michael Jurka        = new HideFromAccessibilityHelper();
32422143131770f7e90be4181e88ced0cb1d7158306Michael Jurka
325c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    private Runnable mBuildLayersRunnable = new Runnable() {
326c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        public void run() {
3279d906c7742af73160df257a40a702e1cc4f87899Michael Jurka            if (mWorkspace != null) {
3289d906c7742af73160df257a40a702e1cc4f87899Michael Jurka                mWorkspace.buildPageHardwareLayers();
3299d906c7742af73160df257a40a702e1cc4f87899Michael Jurka            }
330c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka        }
331c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka    };
332c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka
333002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    private static ArrayList<PendingAddArguments> sPendingAddList
334002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            = new ArrayList<PendingAddArguments>();
335002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
3360a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
3370a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka
338002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    private static class PendingAddArguments {
339002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int requestCode;
340002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        Intent intent;
3413d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        long container;
342dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        long screenId;
343002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int cellX;
344002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        int cellY;
345002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    }
346002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
3470a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka    private static boolean isPropertyEnabled(String propertyName) {
3480a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka        return Log.isLoggable(propertyName, Log.VERBOSE);
3499bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka    }
3509bc8eba3338455c5d915055e8ba044981c512dfaMichael Jurka
35131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
35231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onCreate(Bundle savedInstanceState) {
353a2413751e3a698aef9c87411a639637883856939Winson Chung        if (DEBUG_STRICT_MODE) {
354a2413751e3a698aef9c87411a639637883856939Winson Chung            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
355a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectDiskReads()
356a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectDiskWrites()
357a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectNetwork()   // or .detectAll() for all detectable problems
358a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyLog()
359a2413751e3a698aef9c87411a639637883856939Winson Chung                    .build());
360a2413751e3a698aef9c87411a639637883856939Winson Chung            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
361a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectLeakedSqlLiteObjects()
362a2413751e3a698aef9c87411a639637883856939Winson Chung                    .detectLeakedClosableObjects()
363a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyLog()
364a2413751e3a698aef9c87411a639637883856939Winson Chung                    .penaltyDeath()
365a2413751e3a698aef9c87411a639637883856939Winson Chung                    .build());
366a2413751e3a698aef9c87411a639637883856939Winson Chung        }
367a2413751e3a698aef9c87411a639637883856939Winson Chung
36831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onCreate(savedInstanceState);
369e060b0b3c72b93b47031f0f33bf18b121fb99333Daniel Sandler
370e060b0b3c72b93b47031f0f33bf18b121fb99333Daniel Sandler        // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
371e060b0b3c72b93b47031f0f33bf18b121fb99333Daniel Sandler        LauncherAppState.setApplicationContext(getApplicationContext());
372cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        LauncherAppState app = LauncherAppState.getInstance();
373cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
374f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                Context.MODE_PRIVATE);
3750589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mModel = app.setLauncher(this);
3760589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        mIconCache = app.getIconCache();
37741a12d225a546b0a193628a4e5dd6b62cc60b7d0Joe Onorato        mDragController = new DragController(this);
37831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mInflater = getLayoutInflater();
379cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3807376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetManager = AppWidgetManager.getInstance(this);
3817376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
3827376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        mAppWidgetHost.startListening();
383cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
384b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
385b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // this also ensures that any synchronous binding below doesn't re-trigger another
386b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        // LauncherModel load.
387b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung        mPaused = false;
388b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung
38931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (PROFILE_STARTUP) {
3900fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauer            android.os.Debug.startMethodTracing(
3910fbe7bc14edda0e42d700956f5a3fbd72ed44c49Christian Mehlmauer                    Environment.getExternalStorageDirectory() + "/launcher");
39231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
39331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
39431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        checkForLocaleChange();
39531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        setContentView(R.layout.launcher);
39631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        setupViews();
3977d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        showFirstRunWorkspaceCling();
39831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3991e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        registerContentObservers();
4001e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
4017c312c15e33b6cffbda707429e88ade498fc9f5eJoe Onorato        lockAllApps();
4027404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato
40331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mSavedState = savedInstanceState;
40431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        restoreState(mSavedState);
40531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
406a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung        // Update customization drawer _after_ restoring the states
407785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
408c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            mAppsCustomizeContent.onPackagesUpdated(
409c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                LauncherModel.getSortedWidgetsAndShortcuts(this));
410785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
411a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung
41231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (PROFILE_STARTUP) {
41331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            android.os.Debug.stopMethodTracing();
41431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
41531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
41631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
4174a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            if (sPausedFromUserAction) {
4184a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                // If the user leaves launcher, then we should just load items asynchronously when
4194a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                // they return.
4204a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                mModel.startLoader(true, -1);
4214a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            } else {
4224a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                // We only load the page synchronously if the user rotates (or triggers a
4234a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                // configuration change) while launcher is in the foreground
4244a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung                mModel.startLoader(true, mWorkspace.getCurrentPage());
4254a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            }
42631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
42731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
42831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // For handling default keys
42931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        mDefaultKeySsb = new SpannableStringBuilder();
43031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        Selection.setSelection(mDefaultKeySsb, 0);
43134a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato
43234a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
43334a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        registerReceiver(mCloseSystemDialogsReceiver, filter);
4344ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
435f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        updateGlobalIcons();
436f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
437f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // On large interfaces, we want the screen to auto-rotate based on the current orientation
438f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        unlockScreenOrientation(true);
439f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen    }
440f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
4414a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    protected void onUserLeaveHint() {
4424a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        super.onUserLeaveHint();
4434a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        sPausedFromUserAction = true;
4444a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung    }
4454a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung
446f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen    private void updateGlobalIcons() {
447c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean searchVisible = false;
448c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean voiceVisible = false;
4494ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        // If we have a saved version of these external icons, we load them up immediately
450dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        int coi = getCurrentOrientationIndexForGlobalIcons();
451dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
452dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                sAppMarketIcon[coi] == null) {
453c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            updateAppMarketIcon();
454c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchVisible = updateGlobalSearchIcon();
455c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            voiceVisible = updateVoiceSearchIcon(searchVisible);
456f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
457dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sGlobalSearchIcon[coi] != null) {
458dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung             updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
459c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung             searchVisible = true;
460f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
461dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sVoiceSearchIcon[coi] != null) {
462dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
463c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            voiceVisible = true;
464f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
465dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        if (sAppMarketIcon[coi] != null) {
466dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            updateAppMarketIcon(sAppMarketIcon[coi]);
4674ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        }
468adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        if (mSearchDropTargetBar != null) {
469adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung            mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
470adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        }
47131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
472cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
47331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void checkForLocaleChange() {
474319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        if (sLocaleConfiguration == null) {
475319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            new AsyncTask<Void, Void, LocaleConfiguration>() {
476319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                @Override
477319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                protected LocaleConfiguration doInBackground(Void... unused) {
478319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    LocaleConfiguration localeConfiguration = new LocaleConfiguration();
479319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    readConfiguration(Launcher.this, localeConfiguration);
480319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    return localeConfiguration;
481319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
482319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
483319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                @Override
484319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                protected void onPostExecute(LocaleConfiguration result) {
485319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    sLocaleConfiguration = result;
486319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    checkForLocaleChange();  // recursive, but now with a locale configuration
487319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
488319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            }.execute();
489319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            return;
490319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        }
491fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
49231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final Configuration configuration = getResources().getConfiguration();
49331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
494319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final String previousLocale = sLocaleConfiguration.locale;
49531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final String locale = configuration.locale.toString();
49631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
497319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final int previousMcc = sLocaleConfiguration.mcc;
49831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final int mcc = configuration.mcc;
49931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
500319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        final int previousMnc = sLocaleConfiguration.mnc;
50131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final int mnc = configuration.mnc;
50231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
50384f296c106cb1c7b6d3ae6c6d5508a17f1324e29Romain Guy        boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
50431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
50584f296c106cb1c7b6d3ae6c6d5508a17f1324e29Romain Guy        if (localeChanged) {
506319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.locale = locale;
507319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.mcc = mcc;
508319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            sLocaleConfiguration.mnc = mnc;
50998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
5100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            mIconCache.flush();
511319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick
512319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
513319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            new Thread("WriteLocaleConfiguration") {
514dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
515319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                public void run() {
516319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                    writeConfiguration(Launcher.this, localeConfiguration);
517319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick                }
518319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            }.start();
51998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        }
52098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
52198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
52298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static class LocaleConfiguration {
52398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public String locale;
52498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public int mcc = -1;
52598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        public int mnc = -1;
52698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
527fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
52898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static void readConfiguration(Context context, LocaleConfiguration configuration) {
52998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        DataInputStream in = null;
53098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        try {
53198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            in = new DataInputStream(context.openFileInput(PREFERENCES));
53298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.locale = in.readUTF();
53398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.mcc = in.readInt();
53498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            configuration.mnc = in.readInt();
53598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (FileNotFoundException e) {
53698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
53798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (IOException e) {
53898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
53998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } finally {
54098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            if (in != null) {
54198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                try {
54298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    in.close();
54398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                } catch (IOException e) {
54498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    // Ignore
54598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                }
54698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            }
54798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        }
54898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    }
54998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy
55098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy    private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
55198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        DataOutputStream out = null;
55298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        try {
55398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
55498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeUTF(configuration.locale);
55598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeInt(configuration.mcc);
55698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.writeInt(configuration.mnc);
55798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            out.flush();
55898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (FileNotFoundException e) {
55998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            // Ignore
56098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } catch (IOException e) {
56198d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            //noinspection ResultOfMethodCallIgnored
56298d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            context.getFileStreamPath(PREFERENCES).delete();
56398d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy        } finally {
56498d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            if (out != null) {
56598d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                try {
56698d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    out.close();
56798d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                } catch (IOException e) {
56898d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                    // Ignore
56998d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy                }
57098d0165c88b643d6ead4ebf35bb60be220f5811aRomain Guy            }
57131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
57231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
57331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
574c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    public LayoutInflater getInflater() {
575c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert        return mInflater;
576c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    }
577c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert
578716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen    public DragLayer getDragLayer() {
579716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen        return mDragLayer;
580716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen    }
581716b51e030f9c6ed34af2b947760e46a280c65a6Adam Cohen
58236a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    boolean isDraggingEnabled() {
58336a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        // We prevent dragging when we are loading the workspace as it is possible to pick up a view
58436a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        // that is subsequently removed from the workspace in startBinding().
58536a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        return !mModel.isLoadingWorkspace();
58636a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung    }
58736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung
58831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static int getScreen() {
58931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        synchronized (sLock) {
59031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return sScreen;
59131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
59231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
59331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
59431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    static void setScreen(int screen) {
59531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        synchronized (sLock) {
59631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            sScreen = screen;
59731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
59831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
59931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
600557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    /**
601557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
602557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     * a configuration step, this allows the proper animations to run after other transitions.
603557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung     */
604557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    private boolean completeAdd(PendingAddArguments args) {
605b8472bb83cd96456a543137e1e56d589171934c3Winson Chung        boolean result = false;
606002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        switch (args.requestCode) {
607002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_APPLICATION:
608dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
6093d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        args.cellY);
610002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
611002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_SHORTCUT:
612002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                processShortcut(args.intent);
613002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
614002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_CREATE_SHORTCUT:
615dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
6163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                        args.cellY);
617b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                result = true;
618b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                break;
619002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_CREATE_APPWIDGET:
620002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
621dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
622b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                result = true;
623b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                break;
624002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            case REQUEST_PICK_WALLPAPER:
625002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                // We just wanted the activity result here so we can clear mWaitingForResult
626002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                break;
627002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        }
62827614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
62927614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // if you turned the screen off and then back while in All Apps, Launcher would not
63027614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        // return to the workspace. Clearing mAddInfo.container here fixes this issue
63127614d22a159cf376605ebf68b51a5c45964abd9Michael Jurka        resetAddInfo();
632b8472bb83cd96456a543137e1e56d589171934c3Winson Chung        return result;
633002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy    }
634002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy
63531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
6368b805b17158886035b38261eb611d8641701ae43Michael Jurka    protected void onActivityResult(
6378b805b17158886035b38261eb611d8641701ae43Michael Jurka            final int requestCode, final int resultCode, final Intent data) {
6388b805b17158886035b38261eb611d8641701ae43Michael Jurka        if (requestCode == REQUEST_BIND_APPWIDGET) {
6398b805b17158886035b38261eb611d8641701ae43Michael Jurka            int appWidgetId = data != null ?
6408b805b17158886035b38261eb611d8641701ae43Michael Jurka                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
6418b805b17158886035b38261eb611d8641701ae43Michael Jurka            if (resultCode == RESULT_CANCELED) {
6428b805b17158886035b38261eb611d8641701ae43Michael Jurka                completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
6438b805b17158886035b38261eb611d8641701ae43Michael Jurka            } else if (resultCode == RESULT_OK) {
6449d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
6458b805b17158886035b38261eb611d8641701ae43Michael Jurka            }
6468b805b17158886035b38261eb611d8641701ae43Michael Jurka            return;
6478b805b17158886035b38261eb611d8641701ae43Michael Jurka        }
648557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        boolean delayExitSpringLoadedMode = false;
649ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
650ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                requestCode == REQUEST_CREATE_APPWIDGET);
651aad5ef44c665fc4ce143a61c009dc04be3ab0437Romain Guy        mWaitingForResult = false;
652aad5ef44c665fc4ce143a61c009dc04be3ab0437Romain Guy
653ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        // We have special handling for widgets
654ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (isWidgetDrop) {
655ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            int appWidgetId = data != null ?
656ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
6575aaab774cfb83833197dac553d4a6eb769032146Winson Chung            if (appWidgetId < 0) {
6585aaab774cfb83833197dac553d4a6eb769032146Winson Chung                Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
6595aaab774cfb83833197dac553d4a6eb769032146Winson Chung                        "widget configuration activity.");
6605aaab774cfb83833197dac553d4a6eb769032146Winson Chung                completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
6615aaab774cfb83833197dac553d4a6eb769032146Winson Chung            } else {
6625aaab774cfb83833197dac553d4a6eb769032146Winson Chung                completeTwoStageWidgetDrop(resultCode, appWidgetId);
6635aaab774cfb83833197dac553d4a6eb769032146Winson Chung            }
664ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            return;
665ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
666ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
66731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // The pattern used here is that a user PICKs a specific application,
66831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // which, depending on the target, might need to CREATE the actual target.
669cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
67031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // For example, the user would PICK_SHORTCUT for "Music playlist", and we
67131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // launch over to the Music app to actually CREATE_SHORTCUT.
672c7da555dc5b252042dff0a6920efb1bfeb4a0c9aWinson Chung        if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
673002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            final PendingAddArguments args = new PendingAddArguments();
674002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            args.requestCode = requestCode;
675002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            args.intent = data;
6763d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.container = mPendingAddInfo.container;
677dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            args.screenId = mPendingAddInfo.screenId;
6783d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.cellX = mPendingAddInfo.cellX;
6793d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            args.cellY = mPendingAddInfo.cellY;
680002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            if (isWorkspaceLocked()) {
681002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy                sPendingAddList.add(args);
682002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            } else {
683557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                delayExitSpringLoadedMode = completeAdd(args);
68431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
68531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
686ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        mDragLayer.clearAnimatedView();
687557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        // Exit spring loaded mode if necessary after cancelling the configuration of a widget
688ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
689ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                null);
690ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
691ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
692ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
6938b805b17158886035b38261eb611d8641701ae43Michael Jurka        CellLayout cellLayout =
694dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
695ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        Runnable onCompleteRunnable = null;
696ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int animationType = 0;
697ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
6989d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        AppWidgetHostView boundWidget = null;
699ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (resultCode == RESULT_OK) {
700ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
701ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
7029d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    mPendingAddWidgetInfo);
7039d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            boundWidget = layout;
704ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            onCompleteRunnable = new Runnable() {
705ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                @Override
706ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                public void run() {
707ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
708dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            mPendingAddInfo.screenId, layout, null);
709ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
710ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                            null);
711ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                }
712ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            };
713ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else if (resultCode == RESULT_CANCELED) {
714ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
715ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            onCompleteRunnable = new Runnable() {
716ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                @Override
717ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                public void run() {
718ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
719ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                            null);
720ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                }
721ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            };
722ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
7239d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        if (mDragLayer.getAnimatedView() != null) {
7249d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
7259d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
7269d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    animationType, boundWidget, true);
7279d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        } else {
7289d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // The animated view may be null in the case of a rotation during widget configuration
7299d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            onCompleteRunnable.run();
7309d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        }
73131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
73231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
73331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
734cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    protected void onStop() {
735cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka        super.onStop();
736cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka        FirstFrameAnimatorHelper.setIsVisible(false);
737cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    }
738cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka
739cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    @Override
740cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    protected void onStart() {
741cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka        super.onStart();
742cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka        FirstFrameAnimatorHelper.setIsVisible(true);
743cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    }
744cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka
745cd496d723ca6b4b100771ae66f09007e7acf0d17Michael Jurka    @Override
74631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onResume() {
747447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        long startTime = 0;
748447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        if (DEBUG_RESUME_TIME) {
749447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka            startTime = System.currentTimeMillis();
750924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler            Log.v(TAG, "Launcher.onResume()");
751447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        }
75231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onResume();
753f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
7544a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        // Restore the previous launcher state
7554a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        if (mOnResumeState == State.WORKSPACE) {
7564a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            showWorkspace(false);
7574a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
7584a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            showAllApps(false);
7594a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        }
7604a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        mOnResumeState = State.NONE;
7614a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung
762360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        // Background was set to gradient in onPause(), restore to black if in all apps.
763360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        setWorkspaceBackground(mState == State.WORKSPACE);
764360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner
765de0fb8fbf11caac382589ab464b50d528942273aWinson Chung        // Process any items that were added while Launcher was away
766de0fb8fbf11caac382589ab464b50d528942273aWinson Chung        InstallShortcutReceiver.flushInstallQueue(this);
767de0fb8fbf11caac382589ab464b50d528942273aWinson Chung
76834a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        mPaused = false;
7694a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung        sPausedFromUserAction = false;
770ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        if (mRestoring || mOnResumeNeedsLoad) {
7719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mWorkspaceLoading = true;
772b8b2a5aa45d82ce81301250707bc373e1da4aa14Winson Chung            mModel.startLoader(true, -1);
7739c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mRestoring = false;
774ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            mOnResumeNeedsLoad = false;
77531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
776c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        if (mOnResumeCallbacks.size() > 0) {
777c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            // We might have postponed some bind calls until onResume (see waitUntilResume) --
778c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            // execute them here
779c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            long startTimeCallbacks = 0;
780c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            if (DEBUG_RESUME_TIME) {
781c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                startTimeCallbacks = System.currentTimeMillis();
782c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
783c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
784c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            if (mAppsCustomizeContent != null) {
785c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                mAppsCustomizeContent.setBulkBind(true);
786c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
787c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
788c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                mOnResumeCallbacks.get(i).run();
789c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
790c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            if (mAppsCustomizeContent != null) {
791c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                mAppsCustomizeContent.setBulkBind(false);
792c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
793c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            mOnResumeCallbacks.clear();
794c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            if (DEBUG_RESUME_TIME) {
795c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                Log.d(TAG, "Time spent processing callbacks in onResume: " +
796c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                    (System.currentTimeMillis() - startTimeCallbacks));
797c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
798447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        }
799e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung
800e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        // Reset the pressed state of icons that were locked in the press state while activities
801e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        // were launching
802ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        if (mWaitingForResume != null) {
803e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            // Resets the previous workspace icon press state
804ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            mWaitingForResume.setStayPressed(false);
805ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        }
806e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        if (mAppsCustomizeContent != null) {
807e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            // Resets the previous all apps icon press state
808e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung            mAppsCustomizeContent.resetDrawableState();
809e4e506660ba93ae2c4f1cb0b41378cf148c85e2bWinson Chung        }
81006dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // It is possible that widgets can receive updates while launcher is not in the foreground.
81106dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // Consequently, the widgets will be inflated in the orientation of the foreground activity
81206dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // (framework issue). On resuming, we ensure that any widgets are inflated for the current
81306dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen        // orientation.
8143d50932a93dd367537db3548bde29d1380b371c0Adam Cohen        getWorkspace().reinflateWidgetsIfNecessary();
815f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen
816f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // Again, as with the above scenario, it's possible that one or more of the global icons
817f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        // were updated in the wrong orientation.
818f9426d5df0592a67c8a998d3ab445a3bb5eafc18Adam Cohen        updateGlobalIcons();
819447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        if (DEBUG_RESUME_TIME) {
820447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka            Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
821447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        }
82206dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen    }
82306dff35763bc46884b6464a9bf4406d32cdcaea4Adam Cohen
82431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
8257376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    protected void onPause() {
826ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
827ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // to be consistent.  So re-enable the flag here, and we will re-disable it as necessary
828ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        // when Launcher resumes and we are still in AllApps.
829ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung        updateWallpaperVisibility(true);
830ca889b34821583ea0e70abb403ebbdfe1a290d9eWinson Chung
8317376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        super.onPause();
832ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        mPaused = true;
83324b6fd854f75f21700a330c2f0d11938e5dfeab6Joe Onorato        mDragController.cancelDrag();
834a2413751e3a698aef9c87411a639637883856939Winson Chung        mDragController.resetLastGestureUpTime();
8357376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    }
836cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
83766a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen    protected void onFinishBindingItems() {
83866a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen    }
83966a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen
84066a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen    // Add a fullscreen unpadded view to the workspace to the left all other screens.
84166a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen    public void addCustomContentToLeft(View customContent) {
842dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mWorkspace.addCustomContentToLeft(customContent);
84366a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen    }
84466a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen
845edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    // The custom content needs to offset its content to account for the QSB
846edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    public int getTopOffsetForCustomContent() {
847edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen        return mWorkspace.getPaddingTop();
848edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen    }
849edb4076e59861d0c343e7245f247e2bee6d48159Adam Cohen
85099c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    @Override
85199c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    public Object onRetainNonConfigurationInstance() {
8529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        // Flag the loader to stop early before switching
8539c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        mModel.stopLoader();
854785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
855785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.surrender();
856785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
85713c2e7b788203f397085bef563bc5597e4c98028Romain Guy        return Boolean.TRUE;
85899c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey    }
8597376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project
8602d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    // We can't hide the IME if it was forced open.  So don't bother
8612d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    /*
8622d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    @Override
8632d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    public void onWindowFocusChanged(boolean hasFocus) {
8642d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        super.onWindowFocusChanged(hasFocus);
8652d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato
8662d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        if (hasFocus) {
8672d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            final InputMethodManager inputManager = (InputMethodManager)
8682d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                    getSystemService(Context.INPUT_METHOD_SERVICE);
8692d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            WindowManager.LayoutParams lp = getWindow().getAttributes();
8702d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
8712d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        android.os.Handler()) {
8722d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        protected void onReceiveResult(int resultCode, Bundle resultData) {
8732d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                            Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
8742d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                        }
8752d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato                    });
8762d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato            Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
8772d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato        }
8782d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    }
8792d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato    */
8802d7e7d0d4826850c8983f001ce706d802282b0f3Joe Onorato
88131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private boolean acceptFilter() {
88231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final InputMethodManager inputManager = (InputMethodManager)
88331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                getSystemService(Context.INPUT_METHOD_SERVICE);
88431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return !inputManager.isFullscreenMode();
88531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
88631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
88731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
88831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onKeyDown(int keyCode, KeyEvent event) {
88997d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final int uniChar = event.getUnicodeChar();
89097d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final boolean handled = super.onKeyDown(keyCode, event);
89197d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
89297d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung        if (!handled && acceptFilter() && isKeyNotWhitespace) {
89331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
89431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    keyCode, event);
89531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
896138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // something usable has been typed - start a search
897cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy                // the typed text will be retrieved and cleared by
898138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // showSearchDialog()
899138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // If there are multiple keystrokes before the search dialog takes focus,
900138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // onSearchRequested() will be called for every keystroke,
901138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                // but it is idempotent, so it's fine.
902138a04170d964da9cdec228e95b976875ae52481Karl Rosaen                return onSearchRequested();
90331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
90431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
90531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9068a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        // Eat the long press event so the keyboard doesn't come up.
9078a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
9088a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato            return true;
9098a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato        }
9108a9625e218744d4aed1a97ca637aa0ef0b06f5efJoe Onorato
91131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return handled;
91231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
91331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
914138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    private String getTypedText() {
915138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        return mDefaultKeySsb.toString();
916138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    }
917138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
918138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    private void clearTypedText() {
919138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        mDefaultKeySsb.clear();
920138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        mDefaultKeySsb.clearSpans();
921138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        Selection.setSelection(mDefaultKeySsb, 0);
922138a04170d964da9cdec228e95b976875ae52481Karl Rosaen    }
923138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
92431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
925883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
926883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     * State
927883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka     */
928883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    private static State intToState(int stateOrdinal) {
929883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        State state = State.WORKSPACE;
930883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        final State[] stateValues = State.values();
931883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        for (int i = 0; i < stateValues.length; i++) {
932883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka            if (stateValues[i].ordinal() == stateOrdinal) {
933883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka                state = stateValues[i];
934883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka                break;
935883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka            }
936883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        }
937883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        return state;
938883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    }
939883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka
940883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka    /**
94131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Restores the previous state, if it exists.
94231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
94331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param savedState The previous state.
94431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
94531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void restoreState(Bundle savedState) {
94631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (savedState == null) {
94731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return;
94831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
94931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
950883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
951f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        if (state == State.APPS_CUSTOMIZE) {
9524a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung            mOnResumeState = State.APPS_CUSTOMIZE;
9533a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato        }
9543a8820bdbad90642cf5cda4b00a8c92ecb699159Joe Onorato
955f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
95631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (currentScreen > -1) {
9570142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            mWorkspace.setCurrentPage(currentScreen);
95831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
95931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
9603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
961dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
9623d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
9633d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
9643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.container = pendingAddContainer;
965dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mPendingAddInfo.screenId = pendingAddScreen;
9663d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
9673d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
9689d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
9699d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
9709d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
97187a9f5bf6a62a2b877bd2285f91e0300b288e02aAdam Cohen            mWaitingForResult = true;
97231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            mRestoring = true;
97331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
97431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
97531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
97631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (renameFolder) {
97731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
978319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick            mFolderInfo = mModel.getFolderById(this, sFolders, id);
97931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            mRestoring = true;
98031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
981a12a2502e6a448d36ab7b8de46de0c1afe40b34fWinson Chung
982785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Restore the AppsCustomize tab
983785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeTabHost != null) {
984785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            String curTab = savedState.getString("apps_customize_currentTab");
985785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            if (curTab != null) {
986c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung                mAppsCustomizeTabHost.setContentTypeImmediate(
987785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                        mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
988f314b0eab03bb354c14b6c89f694a6685bd246d9Winson Chung                mAppsCustomizeContent.loadAssociatedPages(
989f314b0eab03bb354c14b6c89f694a6685bd246d9Winson Chung                        mAppsCustomizeContent.getCurrentPage());
990785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
991785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung
9925afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            int currentIndex = savedState.getInt("apps_customize_currentIndex");
9935afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            mAppsCustomizeContent.restorePageForIndex(currentIndex);
994785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
99531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
99631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
99731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
99831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Finds all the views we need and configure them properly.
99931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
100031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void setupViews() {
1001a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        final DragController dragController = mDragController;
100200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
1003360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mLauncherView = findViewById(R.id.launcher);
10044c98d9235d164680186180974719f551cf935d08Winson Chung        mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
10054c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
1006360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mQsbDivider = findViewById(R.id.qsb_divider);
1007360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner
1008360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1009360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
101031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
10114c98d9235d164680186180974719f551cf935d08Winson Chung        // Setup the drag layer
10124c98d9235d164680186180974719f551cf935d08Winson Chung        mDragLayer.setup(this, dragController);
10134c98d9235d164680186180974719f551cf935d08Winson Chung
10143d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // Setup the hotseat
10153d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mHotseat = (Hotseat) findViewById(R.id.hotseat);
10163d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mHotseat != null) {
10173d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mHotseat.setup(this);
10183d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        }
10193d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung
10204c98d9235d164680186180974719f551cf935d08Winson Chung        // Setup the workspace
10214c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace.setHapticFeedbackEnabled(false);
10224c98d9235d164680186180974719f551cf935d08Winson Chung        mWorkspace.setOnLongClickListener(this);
1023cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        mWorkspace.setup(dragController);
1024d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        dragController.addDragListener(mWorkspace);
10254c98d9235d164680186180974719f551cf935d08Winson Chung
1026f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Get the search/delete bar
1027c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
1028f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
1029f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Setup AppsCustomize
1030360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
1031f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        mAppsCustomizeContent = (AppsCustomizePagedView)
1032f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1033f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        mAppsCustomizeContent.setup(this, dragController);
1034360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner
10353d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // Setup the drag controller (drop targets have to be added in reverse order in priority)
10364c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setDragScoller(mWorkspace);
10374c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setScrollView(mDragLayer);
10384c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.setMoveTarget(mWorkspace);
10394c98d9235d164680186180974719f551cf935d08Winson Chung        dragController.addDropTarget(mWorkspace);
1040c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        if (mSearchDropTargetBar != null) {
1041c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            mSearchDropTargetBar.setup(this, dragController);
1042e0f5a61a1f105216b0067124bacac69194c0ef70Michael Jurka        }
1043924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler
1044a127b7ad151dd1ae66d665a073e98984930c3d9dDaniel Sandler        if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
1045924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler            Log.v(TAG, "adding WeightWatcher");
104639a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            mWeightWatcher = new WeightWatcher(this);
104739a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            mWeightWatcher.setAlpha(0.5f);
104839a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            ((FrameLayout) mLauncherView).addView(mWeightWatcher,
1049924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler                    new FrameLayout.LayoutParams(
1050924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler                            FrameLayout.LayoutParams.MATCH_PARENT,
1051b9eb2865af05d9ade572e71934a9e6a50421aa7dDaniel Sandler                            FrameLayout.LayoutParams.WRAP_CONTENT,
1052924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler                            Gravity.BOTTOM)
1053924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler            );
105439a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
105539a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            boolean show = shouldShowWeightWatcher();
105639a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
1057924b993c750e6de8537afb750c2d2b9932efbdacDaniel Sandler        }
105831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
105931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
106031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
106131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Creates a view representing a shortcut.
106231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
106331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param info The data structure describing the shortcut.
106431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
106531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return A View inflated from R.layout.application.
106631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
10670589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    View createShortcut(ShortcutInfo info) {
106831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return createShortcut(R.layout.application,
10690142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
107031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
107131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
107231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
107331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Creates a view representing a shortcut inflated from the specified resource.
107431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
107531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param layoutResId The id of the XML layout used to create the shortcut.
107631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param parent The group the shortcut belongs to.
107731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param info The data structure describing the shortcut.
107831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
107931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @return A View inflated from layoutResId.
108031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
10810589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato    View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
108267b2f6cba5aa7b5d0fac5e66ba8f3d5f887baee6Michael Jurka        BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
108367b2f6cba5aa7b5d0fac5e66ba8f3d5f887baee6Michael Jurka        favorite.applyFromShortcutInfo(info, mIconCache);
108431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        favorite.setOnClickListener(this);
108531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return favorite;
108631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
108731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
108831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
108931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Add an application shortcut to the workspace.
109031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
109131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param data The intent describing the application.
109231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param cellInfo The position on screen where to create the shortcut.
109331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1094dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
10950280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final int[] cellXY = mTmpAddItemCellCoordinates;
1096dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        final CellLayout layout = getCellLayout(container, screenId);
10970280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1098fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        // First we check if we already know the exact location where we want to add this item.
1099fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cellX >= 0 && cellY >= 0) {
1100fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[0] = cellX;
1101fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[1] = cellY;
1102fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
110393eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
11040280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return;
11050280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
110631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1107002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
1108cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
11090589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (info != null) {
11100589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
111131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
11120589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            info.container = ItemInfo.NO_ID;
1113dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
1114fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen                    isWorkspaceLocked(), cellX, cellY);
11150589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        } else {
11160589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato            Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
111731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
111831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1119cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
112031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
112131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Add a shortcut to the workspace.
112231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
112331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param data The intent describing the shortcut.
112431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param cellInfo The position on screen where to create the shortcut.
112531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1126dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
11273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellY) {
11283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] cellXY = mTmpAddItemCellCoordinates;
11293d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] touchXY = mPendingAddInfo.dropPos;
1130dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        CellLayout layout = getCellLayout(container, screenId);
11310280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1132d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        boolean foundCellSpan = false;
1133fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1134558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen        ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
1135d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        if (info == null) {
1136d919882006a457d16dea46bd546472e6534b5c19Adam Cohen            return;
1137d919882006a457d16dea46bd546472e6534b5c19Adam Cohen        }
1138558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen        final View view = createShortcut(info);
1139558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
1140fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        // First we check if we already know the exact location where we want to add this item.
1141fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cellX >= 0 && cellY >= 0) {
1142fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[0] = cellX;
1143fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            cellXY[1] = cellY;
1144fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = true;
1145558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen
1146558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            // If appropriate, either create a folder or add to an existing folder
1147482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
1148558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                    true, null,null)) {
1149558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                return;
1150558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
1151558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            DragObject dragObject = new DragObject();
1152558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            dragObject.dragInfo = info;
1153482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen            if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1154482ed823afb4c7452e037ce8add7ea425fc83da2Adam Cohen                    true)) {
1155558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen                return;
1156558baaf0a0b1c6a9062a6771088098533a35222bAdam Cohen            }
1157fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (touchXY != null) {
1158d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            // when dragging and dropping, just find the closest free spot
11593d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
1160d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            foundCellSpan = (result != null);
1161d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        } else {
1162fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
1163d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        }
1164d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
1165d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka        if (!foundCellSpan) {
116693eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
11670280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            return;
11680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        }
1169cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1170dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
117131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
117231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
1173dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
11743d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    isWorkspaceLocked());
117531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
117631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
117731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
11782f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
11792f093b6ad185967c129781526060288e983bc58aAdam Cohen            int minHeight) {
11802f093b6ad185967c129781526060288e983bc58aAdam Cohen        Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
1181f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        // We want to account for the extra amount of padding that we are adding to the widget
1182f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        // to ensure that it gets the full amount of space that it has requested
1183f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        int requiredWidth = minWidth + padding.left + padding.right;
1184f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen        int requiredHeight = minHeight + padding.top + padding.bottom;
11852f093b6ad185967c129781526060288e983bc58aAdam Cohen        return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
1186f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1187f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
11882f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
11892f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
1190f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1191f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
11922f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
11932f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
1194cbf47e38e24cc0d63b4bd3f345c050216f06f404Adam Cohen    }
1195cbf47e38e24cc0d63b4bd3f345c050216f06f404Adam Cohen
11962f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
11972f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
1198f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen    }
1199f814aa0f33c020a60de8a921176345f71a4b07c8Adam Cohen
12002f093b6ad185967c129781526060288e983bc58aAdam Cohen    static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
12012f093b6ad185967c129781526060288e983bc58aAdam Cohen        return getSpanForWidget(context, info.componentName, info.minResizeWidth,
1202a5c9636f563a2fa5bd3c97266894e002453b13dcWinson Chung                info.minResizeHeight);
1203d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen    }
1204d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
120531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
12067376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project     * Add a widget to the workspace.
120731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
12085bbc91bbbcba6cbcfff66b8d8dd510abb06bc076Romain Guy     * @param appWidgetId The app widget id
12097376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project     * @param cellInfo The position on screen where to create the widget.
121031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
1211dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
1212ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1213ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (appWidgetInfo == null) {
1214ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1215ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
1216cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
12177376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        // Calculate the grid spans needed to fit this widget
1218dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        CellLayout layout = getCellLayout(container, screenId);
1219093538671bdbce2629dd0bf788e5d3489514631eAdam Cohen
12202f093b6ad185967c129781526060288e983bc58aAdam Cohen        int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
12212f093b6ad185967c129781526060288e983bc58aAdam Cohen        int[] spanXY = getSpanForWidget(this, appWidgetInfo);
1222cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
122331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Try finding open space on Launcher screen
1224a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // We have saved the position to which the widget was dragged-- this really only matters
1225a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        // if we are placing widgets on a "spring-loaded" screen
12263d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] cellXY = mTmpAddItemCellCoordinates;
12273d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        int[] touchXY = mPendingAddInfo.dropPos;
1228d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        int[] finalSpan = new int[2];
12290280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        boolean foundCellSpan = false;
12303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
12313d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            cellXY[0] = mPendingAddInfo.cellX;
12323d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            cellXY[1] = mPendingAddInfo.cellY;
1233d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[0] = mPendingAddInfo.spanX;
1234d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[1] = mPendingAddInfo.spanY;
1235fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen            foundCellSpan = true;
1236fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        } else if (touchXY != null) {
12370280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            // when dragging and dropping, just find the closest free spot
12383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] result = layout.findNearestVacantArea(
1239d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1240d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen                    spanXY[1], cellXY, finalSpan);
1241d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[0] = finalSpan[0];
1242d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            spanXY[1] = finalSpan[1];
1243d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka            foundCellSpan = (result != null);
12440280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        } else {
1245d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
1246a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka        }
1247a63c452f5bd491ba9b28c332ccedc6c6c7e2f3ccMichael Jurka
12480280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        if (!foundCellSpan) {
1249f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            if (appWidgetId != -1) {
1250f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                // Deleting an app widget ID is a void call but writes to disk before returning
1251f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                // to the caller...
1252f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                new Thread("deleteAppWidgetId") {
1253f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                    public void run() {
1254f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                        mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1255f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                    }
1256f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung                }.start();
1257f7640c8bba304ba99c99afcd7393893eccc9a0d9Winson Chung            }
125893eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
125918042c82c5847d3caf9af65583c5590c17927c3dRomain Guy            return;
126018042c82c5847d3caf9af65583c5590c17927c3dRomain Guy        }
126131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
12627376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        // Build Launcher-specific widget info and save to database
126311a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung        LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
126411a4937fbff0dbc50fb022513dc3b6c643154445Winson Chung                appWidgetInfo.provider);
12650280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        launcherInfo.spanX = spanXY[0];
12660280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        launcherInfo.spanY = spanXY[1];
1267d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1268d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
1269cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
127031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        LauncherModel.addItemToDatabase(this, launcherInfo,
1271dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                container, screenId, cellXY[0], cellXY[1], false);
127231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
127331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!mRestoring) {
1274ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            if (hostView == null) {
1275ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                // Perform actual inflation because we're live
1276ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1277ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1278ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            } else {
1279ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                // The AppWidgetHostView has already been inflated and instantiated
1280ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                launcherInfo.hostView = hostView;
1281ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            }
1282cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
128331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            launcherInfo.hostView.setTag(launcherInfo);
1284d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            launcherInfo.hostView.setVisibility(View.VISIBLE);
1285aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen            launcherInfo.notifyWidgetSizeChanged(this);
1286aaa5c21ee2e03def644fb6ba8eadbb59873ddd45Adam Cohen
1287dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
12889c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
1289ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1290ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
129131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
12926af9af057f2e40c54a4ed447c4628eef7dc15683Adam Cohen        resetAddInfo();
129331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1294cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1295ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1296ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        @Override
1297ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        public void onReceive(Context context, Intent intent) {
1298ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            final String action = intent.getAction();
1299ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1300ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mUserPresent = false;
1301bec6ac5bb3a9d6a677081eb50147abdc58a2cabbAdam Cohen                mDragLayer.clearAllResizeFrames();
1302ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                updateRunning();
1303337cd9d3f6b2abfe7e70a8e20f0017d67e9535ffWinson Chung
1304c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                // Reset AllApps to its initial state only if we are not in the middle of
1305b8472bb83cd96456a543137e1e56d589171934c3Winson Chung                // processing a multi-step drop
1306c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1307c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                    mAppsCustomizeTabHost.reset();
1308c100e8eae7d67ad5bd78767494b62b8fae5d1779Winson Chung                    showWorkspace(false);
1309785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                }
1310ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1311ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mUserPresent = true;
1312ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                updateRunning();
1313ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1314ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1315ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    };
1316ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1317ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    @Override
1318ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onAttachedToWindow() {
1319ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        super.onAttachedToWindow();
1320ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1321ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        // Listen for broadcasts related to user-presence
1322ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        final IntentFilter filter = new IntentFilter();
1323ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        filter.addAction(Intent.ACTION_SCREEN_OFF);
1324ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        filter.addAction(Intent.ACTION_USER_PRESENT);
1325ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        registerReceiver(mReceiver, filter);
1326f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka        FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
1327d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        mAttached = true;
1328ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = true;
1329ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1330ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1331ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    @Override
1332ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onDetachedFromWindow() {
1333ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        super.onDetachedFromWindow();
1334ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = false;
1335ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1336d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        if (mAttached) {
1337d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen            unregisterReceiver(mReceiver);
1338d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen            mAttached = false;
1339d113e0c4abc8b3818dd683dcf86f53fc32e3b354Adam Cohen        }
1340ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        updateRunning();
1341ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1342ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1343ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    public void onWindowVisibilityChanged(int visibility) {
1344ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mVisible = visibility == View.VISIBLE;
1345ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        updateRunning();
13462a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // The following code used to be in onResume, but it turns out onResume is called when
13472a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
13482a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        // is a more appropriate event to handle
13492a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        if (mVisible) {
13502a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mAppsCustomizeTabHost.onWindowVisible();
13512a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (!mWorkspaceLoading) {
13522a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
13532a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // We want to let Launcher draw itself at least once before we force it to build
13542a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                // layers on all the workspace pages, so that transitioning to Launcher from other
1355f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                // apps is nice and speedy.
1356f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
1357df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                    private boolean mStarted = false;
1358f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                    public void onDraw() {
1359df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                        if (mStarted) return;
1360df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                        mStarted = true;
13616ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // We delay the layer building a bit in order to give
13626ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // other message processing a time to run.  In particular
13636ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // this avoids a delay in hiding the IME if it was
13646ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // currently shown, because doing that may involve
13656ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka                        // some communication back with the app.
1366aeae56bf1282761a7dd017458fe27d83f9683efcWinson Chung                        mWorkspace.postDelayed(mBuildLayersRunnable, 500);
1367df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                        final ViewTreeObserver.OnDrawListener listener = this;
1368df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                        mWorkspace.post(new Runnable() {
1369df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                                public void run() {
1370b68e03a83cfc8e59c0c93d070c5e3cc13cc2996bMichael Jurka                                    if (mWorkspace != null &&
1371b68e03a83cfc8e59c0c93d070c5e3cc13cc2996bMichael Jurka                                            mWorkspace.getViewTreeObserver() != null) {
1372b68e03a83cfc8e59c0c93d070c5e3cc13cc2996bMichael Jurka                                        mWorkspace.getViewTreeObserver().
1373b68e03a83cfc8e59c0c93d070c5e3cc13cc2996bMichael Jurka                                                removeOnDrawListener(listener);
1374b68e03a83cfc8e59c0c93d070c5e3cc13cc2996bMichael Jurka                                    }
1375df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                                }
1376df96add095c82f9083f75305d8fa155c8192a4e6Michael Jurka                            });
1377f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                        return;
13782a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    }
13792a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                });
13802a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
13816ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            // When Launcher comes back to foreground, a different Activity might be responsible for
13826ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            // the app market intent, so refresh the icon
13836ee21d28fadef3b1bd594a0ac8708f40a24bdcd7Michael Jurka            updateAppMarketIcon();
13842a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            clearTypedText();
13852a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        }
1386ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1387ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1388ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private void sendAdvanceMessage(long delay) {
1389ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mHandler.removeMessages(ADVANCE_MSG);
1390ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1391ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mHandler.sendMessageDelayed(msg, delay);
1392ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        mAutoAdvanceSentTime = System.currentTimeMillis();
1393ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1394ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1395ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private void updateRunning() {
1396ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1397ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (autoAdvanceRunning != mAutoAdvanceRunning) {
1398ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mAutoAdvanceRunning = autoAdvanceRunning;
1399ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (autoAdvanceRunning) {
1400ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1401ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                sendAdvanceMessage(delay);
1402ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            } else {
1403ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                if (!mWidgetsToAdvance.isEmpty()) {
1404ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1405ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                            (System.currentTimeMillis() - mAutoAdvanceSentTime));
1406ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                }
1407ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                mHandler.removeMessages(ADVANCE_MSG);
14082313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy                mHandler.removeMessages(0); // Remove messages sent using postDelayed()
1409ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1410ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1411ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1412ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1413ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    private final Handler mHandler = new Handler() {
1414ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        @Override
1415ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        public void handleMessage(Message msg) {
1416ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            if (msg.what == ADVANCE_MSG) {
1417ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                int i = 0;
1418ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                for (View key: mWidgetsToAdvance.keySet()) {
1419ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1420ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    final int delay = mAdvanceStagger * i;
1421ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    if (v instanceof Advanceable) {
1422ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                       postDelayed(new Runnable() {
1423ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                           public void run() {
1424ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                               ((Advanceable) v).advance();
1425ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                           }
1426ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                       }, delay);
1427ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    }
1428ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                    i++;
1429ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                }
1430ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen                sendAdvanceMessage(mAdvanceInterval);
1431ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            }
1432ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1433ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    };
1434ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1435ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1436292c025d06b41c42546e9771b0da0333e08f9db7Adam Cohen        if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
1437ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1438ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (v instanceof Advanceable) {
1439ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mWidgetsToAdvance.put(hostView, appWidgetInfo);
14402ddf13e175910f784cb4e54493ef202671e499f8Adam Cohen            ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
1441ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            updateRunning();
1442ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
1443ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1444ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
1445ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    void removeWidgetToAutoAdvance(View hostView) {
1446ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        if (mWidgetsToAdvance.containsKey(hostView)) {
1447ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            mWidgetsToAdvance.remove(hostView);
1448ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen            updateRunning();
1449ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        }
14500280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
14510280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
14529c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1453ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        removeWidgetToAutoAdvance(launcherInfo.hostView);
14549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        launcherInfo.hostView = null;
14559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
14569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
145793eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung    void showOutOfSpaceMessage(boolean isHotseatLayout) {
145893eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung        int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
145993eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung        Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
1460ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen    }
1461ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
14627376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project    public LauncherAppWidgetHost getAppWidgetHost() {
14637376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project        return mAppWidgetHost;
146431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1465cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1466a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    public LauncherModel getModel() {
1467a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung        return mModel;
1468a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung    }
1469a9abd0e0bdedb5cbbd12b84cb83037a735e79a20Winson Chung
1470c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    public void closeSystemDialogs() {
14712ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        getWindow().closeAllPanels();
14722ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
1473a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato        // Whatever we were doing is hereby canceled.
1474a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato        mWaitingForResult = false;
14752ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    }
14762ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
147731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
147831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onNewIntent(Intent intent) {
1479447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        long startTime = 0;
1480447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        if (DEBUG_RESUME_TIME) {
1481447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka            startTime = System.currentTimeMillis();
1482447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        }
148331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onNewIntent(intent);
148431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
148531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Close the menu
148631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (Intent.ACTION_MAIN.equals(intent.getAction())) {
1487a5c32d6ecc9da6d47cf59554bebf5d0dbe4f99d2Joe Onorato            // also will cancel mWaitingForResult.
14882ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            closeSystemDialogs();
1489fd22dac33e535779d1006fcfe22ef2b95152d916Jason Sams
1490cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis            final boolean alreadyOnHome =
1491cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
149214f122bf847e50a3e7730ccbe57abc25d086a01bJoe Onorato                        != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
149301f0ed4126be412c8c4352026dad3b2a49832267Michael Jurka
1494cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis            Runnable processIntent = new Runnable() {
1495cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                public void run() {
1496ffc2682a9784e03d7cc9943f4f408255f8468b96Michael Jurka                    if (mWorkspace == null) {
1497ffc2682a9784e03d7cc9943f4f408255f8468b96Michael Jurka                        // Can be cases where mWorkspace is null, this prevents a NPE
1498ffc2682a9784e03d7cc9943f4f408255f8468b96Michael Jurka                        return;
1499ffc2682a9784e03d7cc9943f4f408255f8468b96Michael Jurka                    }
1500cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    Folder openFolder = mWorkspace.getOpenFolder();
1501cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    // In all these cases, only animate if we're already on home
1502cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    mWorkspace.exitWidgetResizeMode();
1503cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1504cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                            openFolder == null) {
1505cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        mWorkspace.moveToDefaultScreen(true);
1506cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    }
1507ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen
1508cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    closeFolder();
1509cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    exitSpringLoadedDragMode();
15104a2afa36553079e9f75fe7c8fcce863e84578e14Winson Chung
1511cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    // If we are already on home, then just animate back to the workspace,
1512cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    // otherwise, just wait until onResume to set the state back to Workspace
1513cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    if (alreadyOnHome) {
1514cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        showWorkspace(true);
1515cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    } else {
1516cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        mOnResumeState = State.WORKSPACE;
1517cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    }
151873b979d8c141c7ceac82dad7c5b271a6a42afa67Romain Guy
1519cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    final View v = getWindow().peekDecorView();
1520cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    if (v != null && v.getWindowToken() != null) {
1521cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        InputMethodManager imm = (InputMethodManager)getSystemService(
1522cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                                INPUT_METHOD_SERVICE);
1523cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1524cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    }
1525337cd9d3f6b2abfe7e70a8e20f0017d67e9535ffWinson Chung
1526cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    // Reset AllApps to its initial state
1527cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1528cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                        mAppsCustomizeTabHost.reset();
1529cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                    }
1530cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                }
1531cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis            };
1532cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis
1533cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis            if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1534cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                // Delay processing of the intent to allow the status bar animation to finish
1535cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                // first in order to avoid janky animations.
1536cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                mWorkspace.postDelayed(processIntent, 350);
1537cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis            } else {
1538cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                // Process the intent immediately.
1539cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis                processIntent.run();
1540785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
1541cb222e85d256e37da41f0ffa3744656c9b53083cJamie Gennis
154231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1543447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        if (DEBUG_RESUME_TIME) {
1544447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka            Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1545447bf84d2454ac28ac26d70feea8139ed986c321Michael Jurka        }
154631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
154731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
154831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
15491462de39f01cec0ba785386532719cb0207dd827Adam Cohen    public void onRestoreInstanceState(Bundle state) {
15501462de39f01cec0ba785386532719cb0207dd827Adam Cohen        super.onRestoreInstanceState(state);
15511462de39f01cec0ba785386532719cb0207dd827Adam Cohen        for (int page: mSynchronouslyBoundPages) {
15521462de39f01cec0ba785386532719cb0207dd827Adam Cohen            mWorkspace.restoreInstanceStateForChild(page);
15531462de39f01cec0ba785386532719cb0207dd827Adam Cohen        }
155431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
155531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
155631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
155731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    protected void onSaveInstanceState(Bundle outState) {
1558c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung        outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
155995bb800de9b0142bea0110b074eddbcf0e475736Adam Cohen        super.onSaveInstanceState(outState);
156031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1561883f55b1d261862b0de1b43af0b17d351761a9c6Michael Jurka        outState.putInt(RUNTIME_STATE, mState.ordinal());
156251e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        // We close any open folder since it will not be re-opened, and we need to make sure
156351e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        // this state is reflected.
156451e95039ac40f94de024413a6e7fb1e18d41ef19Adam Cohen        closeFolder();
156531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1566dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
15673d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mWaitingForResult) {
15683d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1569dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
15703d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
15713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
15729d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
15739d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
15749d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
157531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
157631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
157731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (mFolderInfo != null && mWaitingForResult) {
157831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
157931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
158031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1581946ad470c72a3caa7568d11836c182b7f84d840dMichael Jurka
1582785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Save the current AppsCustomize tab
1583785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeTabHost != null) {
1584785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1585785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            if (currentTabTag != null) {
1586785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                outState.putString("apps_customize_currentTab", currentTabTag);
1587785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            }
15885afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
15895afbf7bdd6f6879124c9b8283e005fe57f310d02Winson Chung            outState.putInt("apps_customize_currentIndex", currentIndex);
1590785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
159131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
159231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
159331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
159431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onDestroy() {
159531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.onDestroy();
1596cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1597e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        // Remove all pending runnables
1598e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.removeMessages(ADVANCE_MSG);
1599e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.removeMessages(0);
16009d906c7742af73160df257a40a702e1cc4f87899Michael Jurka        mWorkspace.removeCallbacks(mBuildLayersRunnable);
1601e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung
1602cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        // Stop callbacks from LauncherModel
1603cc8befac3550c81d04ea206c170adae36c9a6c93Daniel Sandler        LauncherAppState app = (LauncherAppState.getInstance());
1604cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        mModel.stopLoader();
1605cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung        app.setLauncher(null);
1606cd2b01416febb5bf50ed5ede1544dea33fee3b0bWinson Chung
160731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        try {
16087376faefbbcbe30cc4e3f706ab95c254a4707d98The Android Open Source Project            mAppWidgetHost.stopListening();
160931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } catch (NullPointerException ex) {
1610a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato            Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
161131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
16122313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mAppWidgetHost = null;
16132313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy
16142313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWidgetsToAdvance.clear();
161531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
161631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        TextKeyListener.getInstance().release();
161731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
161881b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
161981b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        // to prevent leaking Launcher activities on orientation change.
162081b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        if (mModel != null) {
162181b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung            mModel.unbindItemInfosAndClearQueuedBindRunnables();
162281b52252796625dcbadc9f8b908f8d8a284565c0Winson Chung        }
16231e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
16241e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        getContentResolver().unregisterContentObserver(mWidgetObserver);
162534a0e1b00b9baeff19e94f1ee35dd890063489d2Joe Onorato        unregisterReceiver(mCloseSystemDialogsReceiver);
16262313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy
1627accf3bffedc736c04034bfee8e1a8e4fe775f568Adam Cohen        mDragLayer.clearAllResizeFrames();
16282313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        ((ViewGroup) mWorkspace.getParent()).removeAllViews();
16292313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWorkspace.removeAllViews();
16302313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mWorkspace = null;
16312313eff05b8f52e72ebe06c589c419941fa019d6Patrick Dubroy        mDragController = null;
163260b7c53ccc6384b65aaca77d97794e8f09e8cfc8Patrick Dubroy
16332ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka        LauncherAnimUtils.onDestroyActivity();
163431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
163531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1636a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public DragController getDragController() {
1637a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        return mDragController;
1638a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    }
1639a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen
164031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
164131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void startActivityForResult(Intent intent, int requestCode) {
164208f97496b72aaff822aba89271e9bd458aafae2dRomain Guy        if (requestCode >= 0) mWaitingForResult = true;
164331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        super.startActivityForResult(intent, requestCode);
164431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
164531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
164670d721061536e0634f6cfa8cd219759a83befceeWinson Chung    /**
164770d721061536e0634f6cfa8cd219759a83befceeWinson Chung     * Indicates that we want global search for this activity by setting the globalSearch
164870d721061536e0634f6cfa8cd219759a83befceeWinson Chung     * argument for {@link #startSearch} to true.
164970d721061536e0634f6cfa8cd219759a83befceeWinson Chung     */
165031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
1651cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy    public void startSearch(String initialQuery, boolean selectInitialQuery,
165231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            Bundle appSearchData, boolean globalSearch) {
1653138a04170d964da9cdec228e95b976875ae52481Karl Rosaen
1654c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        showWorkspace(true);
1655cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1656138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        if (initialQuery == null) {
1657138a04170d964da9cdec228e95b976875ae52481Karl Rosaen            // Use any text typed in the launcher as the initial query
1658138a04170d964da9cdec228e95b976875ae52481Karl Rosaen            initialQuery = getTypedText();
1659138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        }
166031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (appSearchData == null) {
166131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            appSearchData = new Bundle();
166232b12d2c792b74f193547e4b8375c97240079be1Bjorn Bringert            appSearchData.putString("source", "launcher-search");
166331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
1664adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        Rect sourceBounds = new Rect();
1665adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        if (mSearchDropTargetBar != null) {
1666adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung            sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1667adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        }
1668cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1669c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert        startSearch(initialQuery, selectInitialQuery,
1670c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert                appSearchData, sourceBounds);
1671c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    }
1672c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert
1673c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    public void startSearch(String initialQuery,
1674c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
1675a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        startGlobalSearch(initialQuery, selectInitialQuery,
1676c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert                appSearchData, sourceBounds);
1677a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka    }
1678a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka
1679a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka    /**
1680a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka     * Starts the global search activity. This code is a copied from SearchManager
1681a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka     */
1682c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    private void startGlobalSearch(String initialQuery,
1683a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
1684138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        final SearchManager searchManager =
1685a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1686a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1687a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (globalSearchActivity == null) {
1688a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            Log.w(TAG, "No global search activity found.");
1689a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            return;
1690a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1691a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1692a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1693a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.setComponent(globalSearchActivity);
1694a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        // Make sure that we have a Bundle to put source in
1695a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (appSearchData == null) {
1696a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData = new Bundle();
1697a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        } else {
1698a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData = new Bundle(appSearchData);
1699a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1700a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        // Set source to package name of app that starts global search, if not set already.
1701a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (!appSearchData.containsKey("source")) {
1702a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            appSearchData.putString("source", getPackageName());
1703a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1704a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.putExtra(SearchManager.APP_DATA, appSearchData);
1705a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (!TextUtils.isEmpty(initialQuery)) {
1706a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            intent.putExtra(SearchManager.QUERY, initialQuery);
1707a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1708a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        if (selectInitialQuery) {
1709a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1710a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
1711a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        intent.setSourceBounds(sourceBounds);
1712a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        try {
1713a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            startActivity(intent);
1714a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        } catch (ActivityNotFoundException ex) {
1715a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka            Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1716a33411cb83e2c7f822d6486183a7a4ea7d1b09e6Michael Jurka        }
171731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
171831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
171970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
172070d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onCreateOptionsMenu(Menu menu) {
172170d721061536e0634f6cfa8cd219759a83befceeWinson Chung        if (isWorkspaceLocked()) {
172270d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return false;
172370d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
172470d721061536e0634f6cfa8cd219759a83befceeWinson Chung
172570d721061536e0634f6cfa8cd219759a83befceeWinson Chung        super.onCreateOptionsMenu(menu);
1726dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
1727dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1728dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1729dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1730236d431cc582df9b2841718195ae977c3948f751Winson Chung        Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1731236d431cc582df9b2841718195ae977c3948f751Winson Chung        settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1732236d431cc582df9b2841718195ae977c3948f751Winson Chung                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1733b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        String helpUrl = getString(R.string.help_url);
1734b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
1735dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1736dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1737dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
173870d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
173970d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setIcon(android.R.drawable.ic_menu_gallery)
174070d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setAlphabeticShortcut('W');
174170d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
174270d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setIcon(android.R.drawable.ic_menu_manage)
1743dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            .setIntent(manageApps)
174470d721061536e0634f6cfa8cd219759a83befceeWinson Chung            .setAlphabeticShortcut('M');
1745236d431cc582df9b2841718195ae977c3948f751Winson Chung        menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1746236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setIcon(android.R.drawable.ic_menu_preferences)
1747236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setIntent(settings)
1748236d431cc582df9b2841718195ae977c3948f751Winson Chung            .setAlphabeticShortcut('P');
1749b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        if (!helpUrl.isEmpty()) {
1750b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka            menu.add(0, MENU_HELP, 0, R.string.menu_help)
1751b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setIcon(android.R.drawable.ic_menu_help)
1752b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setIntent(help)
1753b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka                .setAlphabeticShortcut('H');
1754b964f9c5c27b3fc79f1321d307c97b2d48e1e461Michael Jurka        }
175570d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return true;
175670d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
175770d721061536e0634f6cfa8cd219759a83befceeWinson Chung
175870d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
175970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onPrepareOptionsMenu(Menu menu) {
176070d721061536e0634f6cfa8cd219759a83befceeWinson Chung        super.onPrepareOptionsMenu(menu);
176170d721061536e0634f6cfa8cd219759a83befceeWinson Chung
176270d721061536e0634f6cfa8cd219759a83befceeWinson Chung        if (mAppsCustomizeTabHost.isTransitioning()) {
176370d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return false;
176470d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
176570d721061536e0634f6cfa8cd219759a83befceeWinson Chung        boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
176670d721061536e0634f6cfa8cd219759a83befceeWinson Chung        menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
176770d721061536e0634f6cfa8cd219759a83befceeWinson Chung
176870d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return true;
176970d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
177070d721061536e0634f6cfa8cd219759a83befceeWinson Chung
177170d721061536e0634f6cfa8cd219759a83befceeWinson Chung    @Override
177270d721061536e0634f6cfa8cd219759a83befceeWinson Chung    public boolean onOptionsItemSelected(MenuItem item) {
177370d721061536e0634f6cfa8cd219759a83befceeWinson Chung        switch (item.getItemId()) {
177470d721061536e0634f6cfa8cd219759a83befceeWinson Chung        case MENU_WALLPAPER_SETTINGS:
177570d721061536e0634f6cfa8cd219759a83befceeWinson Chung            startWallpaper();
177670d721061536e0634f6cfa8cd219759a83befceeWinson Chung            return true;
177770d721061536e0634f6cfa8cd219759a83befceeWinson Chung        }
177870d721061536e0634f6cfa8cd219759a83befceeWinson Chung
177970d721061536e0634f6cfa8cd219759a83befceeWinson Chung        return super.onOptionsItemSelected(item);
178070d721061536e0634f6cfa8cd219759a83befceeWinson Chung    }
178131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
1782ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    @Override
1783ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    public boolean onSearchRequested() {
1784cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy        startSearch(null, false, null, true);
1785a135ba896e4e12a99deb054f671c63535c3e11e3Amith Yamasani        // Use a custom animation for launching search
1786138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        return true;
1787ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project    }
1788ca9475f0403d9c0e843d5c189575068a386b2eb6The Android Open Source Project
17899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public boolean isWorkspaceLocked() {
17909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        return mWorkspaceLoading || mWaitingForResult;
179131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
179231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
17930280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    private void resetAddInfo() {
17943d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = ItemInfo.NO_ID;
1795dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mPendingAddInfo.screenId = -1;
17963d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
17973d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1798d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
17993d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = null;
18000280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
18010280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
18029d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen    void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
18039d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            AppWidgetProviderInfo appWidgetInfo) {
18049d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen        if (appWidgetInfo.configure != null) {
18059d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            mPendingAddWidgetInfo = appWidgetInfo;
1806af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka
18077984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            // Launch over to configure widget, if needed
18087984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
18099d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            intent.setComponent(appWidgetInfo.configure);
18107984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
18118e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
18127984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert        } else {
18137984c9496bcaeca23ee7fdecc4d4f1bb2467e0f2Bjorn Bringert            // Otherwise just add it
1814dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
18159d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                    appWidgetInfo);
1816557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            // Exit spring loaded mode if necessary after adding the widget
1817ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            exitSpringLoadedDragModeDelayed(true, false, null);
181831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
181931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1820cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1821fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    /**
1822fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * Process a shortcut drop.
1823fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     *
1824fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param componentName The name of the component
1825dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * @param screenId The ID of the screen where it should be added
1826fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param cell The cell it should be added to, optional
1827fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param position The location on the screen where it was dropped, optional
1828fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     */
1829dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
18303d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int[] cell, int[] loc) {
18310280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        resetAddInfo();
18323d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = container;
1833dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mPendingAddInfo.screenId = screenId;
18343d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = loc;
1835fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1836fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cell != null) {
18373d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = cell[0];
18383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = cell[1];
1839fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        }
18400280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
18410280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
18420280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        createShortcutIntent.setComponent(componentName);
18430280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        processShortcut(createShortcutIntent);
18440280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka    }
18450280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka
1846fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    /**
1847fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * Process a widget drop.
1848fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     *
1849fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param info The PendingAppWidgetInfo of the widget being added.
1850dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen     * @param screenId The ID of the screen where it should be added
1851fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param cell The cell it should be added to, optional
1852fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     * @param position The location on the screen where it was dropped, optional
1853fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen     */
1854dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
1855d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            int[] cell, int[] span, int[] loc) {
1856fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        resetAddInfo();
18573d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.container = info.container = container;
1858dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mPendingAddInfo.screenId = info.screenId = screenId;
18593d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        mPendingAddInfo.dropPos = loc;
1860d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanX = info.minSpanX;
1861d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        mPendingAddInfo.minSpanY = info.minSpanY;
1862d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen
1863fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        if (cell != null) {
18643d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellX = cell[0];
18653d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mPendingAddInfo.cellY = cell[1];
1866fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen        }
1867d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        if (span != null) {
1868d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            mPendingAddInfo.spanX = span[0];
1869d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen            mPendingAddInfo.spanY = span[1];
1870d41fbf5680750e34335bba6b38298186c144a4b7Adam Cohen        }
1871fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1872ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        AppWidgetHostView hostView = info.boundWidget;
1873ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        int appWidgetId;
1874ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        if (hostView != null) {
1875ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetId = hostView.getAppWidgetId();
18769d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            addAppWidgetImpl(appWidgetId, info, hostView, info.info);
1877ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        } else {
18789d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // In this case, we either need to start an activity to get permission to bind
18799d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen            // the widget, or we need to start an activity to configure the widget, or both.
1880ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1881dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            Bundle options = info.bindOptions;
1882dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen
1883dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            boolean success = false;
1884dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            if (options != null) {
1885dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1886dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                        info.componentName, options);
1887dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            } else {
1888dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1889dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                        info.componentName);
1890dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            }
1891dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen            if (success) {
18929d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                addAppWidgetImpl(appWidgetId, info, null, info.info);
18938b805b17158886035b38261eb611d8641701ae43Michael Jurka            } else {
18949d5b7d81ae498da060deb387a14b6a8475415592Adam Cohen                mPendingAddWidgetInfo = info.info;
18958b805b17158886035b38261eb611d8641701ae43Michael Jurka                Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
18968b805b17158886035b38261eb611d8641701ae43Michael Jurka                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
18978b805b17158886035b38261eb611d8641701ae43Michael Jurka                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1898dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                // TODO: we need to make sure that this accounts for the options bundle.
1899dd70d66852bb8bd16f55796e546ba290360d1198Adam Cohen                // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
19008b805b17158886035b38261eb611d8641701ae43Michael Jurka                startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
19018b805b17158886035b38261eb611d8641701ae43Michael Jurka            }
1902ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        }
1903fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen    }
1904fbba09bb1a67f44ba9537f0fc9988f696e899d42Adam Cohen
1905deb98afe40cd1b6f1c042492e56b48b29320a134Joe Onorato    void processShortcut(Intent intent) {
1906c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        // Handle case where user selected "Applications"
1907c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        String applicationName = getResources().getString(R.string.group_applications);
1908c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
1909cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1910c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        if (applicationName != null && applicationName.equals(shortcutName)) {
1911c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1912c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
1913cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
1914c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1915c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey            pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
191658f208892274c91b83dbbad6c50ea8638f106b33Winson Chung            pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
19174a79a04bcd5f8c4d4fab7bcc46eea9e19a3dc63eJoe Onorato            startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
1918c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        } else {
19194a79a04bcd5f8c4d4fab7bcc46eea9e19a3dc63eJoe Onorato            startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
1920c7fdae17639470c8803c3ff00ff331cbb1535999Jeffrey Sharkey        }
192131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
192231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
192324ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung    void processWallpaper(Intent intent) {
192424ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung        startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
192524ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung    }
192624ab2f1bb59a1b15f80bd398c7e3e3b3c98599fbWinson Chung
1927dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
19283d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            int cellY) {
1929c9d95c5897fc5ebbf53903d4ab18ad13d196f643Michael Jurka        final FolderInfo folderInfo = new FolderInfo();
193031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        folderInfo.title = getText(R.string.folder_name);
193131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
193231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Update the model
1933dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
19343d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                false);
1935319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.put(folderInfo.id, folderInfo);
193631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
193731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // Create the view
19383d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        FolderIcon newFolder =
19393d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1940dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
19413d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                isWorkspaceLocked());
1942df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        return newFolder;
19439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
19449c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
19459c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    void removeFolder(FolderInfo folder) {
1946319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.remove(folder.id);
194731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
1948cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
194931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    private void startWallpaper() {
1950c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        showWorkspace(true);
195131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
19528355ae39ad77b8e2e9ff3b4cbff8077a002ce151Dianne Hackborn        Intent chooser = Intent.createChooser(pickWallpaper,
19538355ae39ad77b8e2e9ff3b4cbff8077a002ce151Dianne Hackborn                getText(R.string.chooser_wallpaper));
1954f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy        // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1955f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy        //       Removed in Eclair MR1
1956f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        WallpaperManager wm = (WallpaperManager)
1957f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//                getSystemService(Context.WALLPAPER_SERVICE);
1958f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        WallpaperInfo wi = wm.getWallpaperInfo();
1959f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        if (wi != null && wi.getSettingsActivity() != null) {
1960f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            LabeledIntent li = new LabeledIntent(getPackageName(),
1961f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//                    R.string.configure_wallpaper, 0);
1962f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1963f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//            chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1964f2826c7e2131ad9d81810b4bd7e3f0b120bbc97cRomain Guy//        }
1965a0618e4102d0543d953d81e162d93c8ea641d0fdMike Cleron        startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
196631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
196731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
19682ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    /**
19692ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     * Registers various content observers. The current implementation registers
19702ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     * only a favorites observer to keep track of the favorites applications.
19712ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     */
19721e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private void registerContentObservers() {
19731e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        ContentResolver resolver = getContentResolver();
19741e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
19751e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey                true, mWidgetObserver);
19761e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
19771e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
197831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    @Override
197931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean dispatchKeyEvent(KeyEvent event) {
198031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (event.getAction() == KeyEvent.ACTION_DOWN) {
198131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            switch (event.getKeyCode()) {
1982678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn                case KeyEvent.KEYCODE_HOME:
1983678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn                    return true;
1984be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                case KeyEvent.KEYCODE_VOLUME_DOWN:
19850a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka                    if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
1986be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                        dumpState();
1987be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                        return true;
1988be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                    }
1989be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato                    break;
1990678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn            }
1991678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn        } else if (event.getAction() == KeyEvent.ACTION_UP) {
1992678008667e05161442e550edc4dc6bbd77078c0cDianne Hackborn            switch (event.getKeyCode()) {
199331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                case KeyEvent.KEYCODE_HOME:
199431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    return true;
199531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
199631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
199731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
199831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return super.dispatchKeyEvent(event);
199931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
200031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
200188ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    @Override
200288ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    public void onBackPressed() {
2003c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung        if (isAllAppsVisible()) {
2004c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka            showWorkspace(true);
200594f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        } else if (mWorkspace.getOpenFolder() != null) {
200676fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            Folder openFolder = mWorkspace.getOpenFolder();
200776fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            if (openFolder.isEditingName()) {
200876fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen                openFolder.dismissEditingName();
200976fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            } else {
201076fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen                closeFolder();
201176fc085d28178a5d4fb3787ede956281a2cc3179Adam Cohen            }
201294f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy        } else {
2013758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy            mWorkspace.exitWidgetResizeMode();
2014758a9239f045ff4eb85c421a2f67a720b5b3c621Patrick Dubroy
201594f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            // Back button is a no-op here, but give at least some feedback for the button press
201694f78a5c44c08b258f59b2c78ff6694e5eaca827Patrick Dubroy            mWorkspace.showOutlinesTemporarily();
2017af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka        }
201888ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato    }
201988ec0990c66702158d3c7f2f6ef5c03531978247Joe Onorato
202031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
20211e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     * Re-listen when widgets are reset.
20221e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     */
20231e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private void onAppWidgetReset() {
2024bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka        if (mAppWidgetHost != null) {
2025bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka            mAppWidgetHost.startListening();
2026bbbad6bcc278f25b89aab71c6003593b984f0f03Michael Jurka        }
20271e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
20281e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
20291e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    /**
203031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * Launches the intent referred by the clicked shortcut.
203131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
203231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param v The view representing the clicked shortcut.
203331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
203431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public void onClick(View v) {
20359932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        // Make sure that rogue clicks don't get through while allapps is launching, or after the
20369932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        // view has detached (it's possible for this to happen if the view is removed mid touch).
20379932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (v.getWindowToken() == null) {
20389932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen            return;
20399932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        }
20409932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen
20419b0b2fefb2de9aca457ab1c38ffd08d7667f4065Winson Chung        if (!mWorkspace.isFinishedSwitchingState()) {
20429932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen            return;
20439932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        }
20442f84ef259314b0bef20f6e7e503377212fadc88cAdam Cohen
204531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        Object tag = v.getTag();
20460589f0f66ce498512c6ee47482c649d88294c9d0Joe Onorato        if (tag instanceof ShortcutInfo) {
204731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Open shortcut
2048fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy            final Intent intent = ((ShortcutInfo) tag).intent;
2049b5fe60c8a502fb7e265e3183550478b2b2a49264Adam Cohen
2050f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler            // Check for special shortcuts
2051f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler            if (intent.getComponent() != null) {
2052f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                final String shortcutClass = intent.getComponent().getClassName();
2053f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler
2054f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                if (shortcutClass.equals(WidgetAdder.class.getName())) {
2055f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                    showAllApps(true);
2056f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                    return;
2057f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2058f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                    MemoryDumpActivity.startDump(this);
2059f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                    return;
206039a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen                } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
206139a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen                    toggleShowWeightWatcher();
206239a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen                    return;
2063f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler                }
2064b5fe60c8a502fb7e265e3183550478b2b2a49264Adam Cohen            }
2065f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler
2066f8577a39058fcc07a390c650cf7b8c68949450d9Daniel Sandler            // Start activities
206713724ea6078748195bcf747641bf6ee8fbbe0f4bJoe Onorato            int[] pos = new int[2];
206813724ea6078748195bcf747641bf6ee8fbbe0f4bJoe Onorato            v.getLocationOnScreen(pos);
2069fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy            intent.setSourceBounds(new Rect(pos[0], pos[1],
2070fb5411e98278d461b78aea0663d3e85f7a9e8a15Romain Guy                    pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2071c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
2072c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            boolean success = startActivitySafely(v, intent, tag);
2073ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka
2074ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            if (success && v instanceof BubbleTextView) {
2075ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka                mWaitingForResume = (BubbleTextView) v;
2076ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka                mWaitingForResume.setStayPressed(true);
2077ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            }
207831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else if (tag instanceof FolderInfo) {
2079a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            if (v instanceof FolderIcon) {
2080a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                FolderIcon fi = (FolderIcon) v;
2081a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                handleFolderClick(fi);
2082a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            }
2083f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        } else if (v == mAllAppsButton) {
2084c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung            if (isAllAppsVisible()) {
2085c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka                showWorkspace(true);
20867404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato            } else {
20872a55232840acc214d95474a3734b3b897669efd9Michael Jurka                onClickAllAppsButton(v);
20887404ee4a16e03dba9f026ac8c78d7cea3886260fJoe Onorato            }
208931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
209031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
209131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
20920e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka    public boolean onTouch(View v, MotionEvent event) {
2093dee0589388ba0f6373912e18bf86243282fb3b9bMichael Jurka        // this is an intercepted event being forwarded from mWorkspace;
2094c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        // clicking anywhere on the workspace causes the customization drawer to slide down
209569688aa359cdbffce4decf334f5d6f0143e3a694Bjorn Bringert        if (event.getAction() == MotionEvent.ACTION_DOWN) {
209669688aa359cdbffce4decf334f5d6f0143e3a694Bjorn Bringert            showWorkspace(true);
209769688aa359cdbffce4decf334f5d6f0143e3a694Bjorn Bringert        }
20980e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka        return false;
20990e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka    }
21000e26059548e429e5d1c973bebe4c561bead2926fMichael Jurka
2101af44209bfa60da3c7ab49b7f508f9effd316ee41Michael Jurka    /**
21022c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * Event handler for the search button
21032c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     *
21042c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * @param v The view that was clicked.
21052c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     */
21062c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    public void onClickSearchButton(View v) {
2107bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2108bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung
2109a135ba896e4e12a99deb054f671c63535c3e11e3Amith Yamasani        onSearchRequested();
21102c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    }
21112c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka
21122c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    /**
21136d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     * Event handler for the voice button
21146d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     *
21156d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     * @param v The view that was clicked.
21166d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani     */
21176d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    public void onClickVoiceButton(View v) {
2118bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
21196d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
2120c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert        startVoice();
2121c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    }
2122c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert
2123c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    public void startVoice() {
2124ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        try {
2125ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            final SearchManager searchManager =
2126ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2127ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            ComponentName activityName = searchManager.getGlobalSearchActivity();
2128ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2129f615f710bb1a1c6fdccb538c1add4db5925d7799Andy Huang            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2130ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (activityName != null) {
2131ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                intent.setPackage(activityName.getPackageName());
2132ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
213386a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            startActivity(null, intent, "onClickVoiceButton");
2134ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        } catch (ActivityNotFoundException e) {
2135ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2136f615f710bb1a1c6fdccb538c1add4db5925d7799Andy Huang            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2137ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            startActivitySafely(null, intent, "onClickVoiceButton");
2138ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
21396d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
21406d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
21416d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    /**
21422c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * Event handler for the "grid" button that appears on the home screen, which
21432c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * enters all apps mode.
21442c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     *
21452c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     * @param v The view that was clicked.
21462c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka     */
21472c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    public void onClickAllAppsButton(View v) {
21485130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka        showAllApps(true);
21495130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka    }
21505130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka
21515130e401a005fcf6afba6e8e72a628fc45f9a287Michael Jurka    public void onTouchDownAllAppsButton(View v) {
21522a55232840acc214d95474a3734b3b897669efd9Michael Jurka        // Provide the same haptic feedback that the system offers for virtual keys.
21532a55232840acc214d95474a3734b3b897669efd9Michael Jurka        v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
21542c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka    }
21552c3af5ffb21039a73107ef70723d2353146ab94aMichael Jurka
2156ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    public void onClickAppMarketButton(View v) {
2157ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        if (mAppMarketIntent != null) {
2158c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            startActivitySafely(v, mAppMarketIntent, "app market");
21594f916f4b35aef7ef834f7888bbdf3a41b9cb3cdbWinson Chung        } else {
21604f916f4b35aef7ef834f7888bbdf3a41b9cb3cdbWinson Chung            Log.e(TAG, "Invalid app market intent.");
2161ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        }
2162ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    }
2163ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
2164bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy    void startApplicationDetailsActivity(ComponentName componentName) {
2165bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy        String packageName = componentName.getPackageName();
21664ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
21674ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy                Uri.fromParts("package", packageName, null));
2168dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
21693b1b36bf2ebed328ab60f1f78fe4d5bdd5322f2eWinson Chung        startActivitySafely(null, intent, "startApplicationDetailsActivity");
21704ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy    }
21714ed6278e518cc6894cb150b606382e8e6a012599Patrick Dubroy
21725539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy    void startApplicationUninstallActivity(ApplicationInfo appInfo) {
21735539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
21745539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            // System applications cannot be installed. For now, show a toast explaining that.
21755539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            // We may give them the option of disabling apps this way.
21765539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            int messageId = R.string.uninstall_system_app_text;
21775539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
21785539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        } else {
21795539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            String packageName = appInfo.componentName.getPackageName();
21805539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            String className = appInfo.componentName.getClassName();
21815539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            Intent intent = new Intent(
21825539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy                    Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2183dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2184dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung                    Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
21855539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy            startActivity(intent);
21865539af7826ad6dd16202c56549d26dfdcbf977b1Patrick Dubroy        }
2187bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy    }
2188bc6840b316992c6fdd681be419fc767b5de2f226Patrick Dubroy
218986a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    boolean startActivity(View v, Intent intent, Object tag) {
219031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2191c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
219231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        try {
21932672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            // Only launch using the new animation if the shortcut has not opted out (this is a
21942672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            // private contract between launcher and may be ignored in the future).
21952672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            boolean useLaunchAnimation = (v != null) &&
21962672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung                    !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
21972672ff937ea7885d184679dfee9f355b7bf2fc7dWinson Chung            if (useLaunchAnimation) {
2198c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2199c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                        v.getMeasuredWidth(), v.getMeasuredHeight());
2200c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung
2201c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                startActivity(intent, opts.toBundle());
2202c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            } else {
2203c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung                startActivity(intent);
2204c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung            }
2205ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka            return true;
220631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } catch (SecurityException e) {
220731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2208a30ce8e6b25e41f392a41fd4d0d3e0a424a84dadJoe Onorato            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
220931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2210f984e853f253c76a7a636a094680bb01c4b8ffb8Joe Onorato                    "or use the exported attribute for this activity. "
2211f984e853f253c76a7a636a094680bb01c4b8ffb8Joe Onorato                    + "tag="+ tag + " intent=" + intent, e);
22128e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        }
2213ddd62e9f3dd21c52dffceef9b168629ebb7f4c03Michael Jurka        return false;
22148e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy    }
2215aafa03cbb925c74be1c13f8bb99d928be429e62fWinson Chung
221686a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    boolean startActivitySafely(View v, Intent intent, Object tag) {
221786a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        boolean success = false;
221886a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        try {
221986a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            success = startActivity(v, intent, tag);
222086a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        } catch (ActivityNotFoundException e) {
222186a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
222286a720e4927a997e9fd98276d2dab67244138f79Michael Jurka            Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
222386a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        }
222486a720e4927a997e9fd98276d2dab67244138f79Michael Jurka        return success;
222586a720e4927a997e9fd98276d2dab67244138f79Michael Jurka    }
222686a720e4927a997e9fd98276d2dab67244138f79Michael Jurka
22278e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy    void startActivityForResultSafely(Intent intent, int requestCode) {
22288e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        try {
22298e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            startActivityForResult(intent, requestCode);
22308e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        } catch (ActivityNotFoundException e) {
22318e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
22328e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy        } catch (SecurityException e) {
22338e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
22348e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
22358e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
22368e633c5a3e4a387ecf02fb74eae8ff926a1b33cdRomain Guy                    "or use the exported attribute for this activity.", e);
223731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
223831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
223931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2240a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    private void handleFolderClick(FolderIcon folderIcon) {
2241fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        final FolderInfo info = folderIcon.getFolderInfo();
22423c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        Folder openFolder = mWorkspace.getFolderForTag(info);
22433c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen
22443c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        // If the folder info reports that the associated folder is open, then verify that
22453c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        // it is actually opened. There have been a few instances where this gets out of sync.
22463c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        if (info.opened && openFolder == null) {
22473c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen            Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2248dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
22493c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen            info.opened = false;
22503c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen        }
22513c81a3835b27bbacf8c87eedfc244528a22973adAdam Cohen
2252fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
225331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Close any open folder
225431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            closeFolder();
225531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Open the requested folder
2256a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen            openFolder(folderIcon);
225731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        } else {
225831dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            // Find the open folder...
225931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            int folderScreen;
226031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            if (openFolder != null) {
22610142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                folderScreen = mWorkspace.getPageForView(openFolder);
226231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                // .. and close it
226331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                closeFolder(openFolder);
22640142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                if (folderScreen != mWorkspace.getCurrentPage()) {
226531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // Close any folder open on the current screen
226631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    closeFolder();
226731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // Pull the folder onto this screen
2268a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen                    openFolder(folderIcon);
226931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
227031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
227131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
227231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
227331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2274268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    /**
2275268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2276268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     * in the DragLayer in the exact absolute location of the original FolderIcon.
2277268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen     */
2278268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private void copyFolderIconToImage(FolderIcon fi) {
2279268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final int width = fi.getMeasuredWidth();
2280268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final int height = fi.getMeasuredHeight();
2281268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2282268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Lazy load ImageView, Bitmap and Canvas
2283268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconImageView == null) {
2284268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconImageView = new ImageView(this);
2285268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2286268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2287268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                mFolderIconBitmap.getHeight() != height) {
2288268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2289268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2290268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2291268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2292268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        DragLayer.LayoutParams lp;
2293268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2294268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2295268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        } else {
2296268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            lp = new DragLayer.LayoutParams(width, height);
2297268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2298268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2299307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        // The layout from which the folder is being opened may be scaled, adjust the starting
2300307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        // view size by this scale factor.
2301307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
2302268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.customPosition = true;
2303268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.x = mRectForFolderAnimation.left;
2304268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        lp.y = mRectForFolderAnimation.top;
2305307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        lp.width = (int) (scale * width);
2306307fe23f125cbbd5512ad8d4660025f2ab68f30bAdam Cohen        lp.height = (int) (scale * height);
2307268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2308268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2309268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        fi.draw(mFolderIconCanvas);
2310268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
2311fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (fi.getFolder() != null) {
2312fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2313fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
23148ec2303379eb467e063113385c9514063abed787Adam Cohen        }
2315268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Just in case this image view is still in the drag layer from a previous animation,
2316268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // we remove it and re-add it.
2317268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2318268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen            mDragLayer.removeView(mFolderIconImageView);
2319268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        }
2320268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mDragLayer.addView(mFolderIconImageView, lp);
2321fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        if (fi.getFolder() != null) {
2322fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen            fi.getFolder().bringToFront();
23238ec2303379eb467e063113385c9514063abed787Adam Cohen        }
2324268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    }
2325268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
23262801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    private void growAndFadeOutFolderIcon(FolderIcon fi) {
23279932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (fi == null) return;
23282801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
23292801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
23302801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
23312801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2332c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        FolderInfo info = (FolderInfo) fi.getTag();
2333c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2334c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            CellLayout cl = (CellLayout) fi.getParent().getParent();
2335e50adee3d54978840036bb3a814771ce498aee18Winson Chung            CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2336e50adee3d54978840036bb3a814771ce498aee18Winson Chung            cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
2337c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        }
2338c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen
2339268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2340268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        copyFolderIconToImage(fi);
2341268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        fi.setVisibility(View.INVISIBLE);
2342268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
23432ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka        ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2344268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                scaleX, scaleY);
23452801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
23462801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.start();
23472801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
23482801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2349268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen    private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
23509932a9b1e66fed4b5f38ce50f13d56331bbdcd7eAdam Cohen        if (fi == null) return;
23512801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
23522801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
23532801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
23542801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
2355268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        final CellLayout cl = (CellLayout) fi.getParent().getParent();
2356268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen
2357268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        // We remove and re-draw the FolderIcon in-case it has changed
2358268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        mDragLayer.removeView(mFolderIconImageView);
2359268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        copyFolderIconToImage(fi);
23602ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka        ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2361268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                scaleX, scaleY);
23622801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2363c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        oa.addListener(new AnimatorListenerAdapter() {
2364c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            @Override
2365c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            public void onAnimationEnd(Animator animation) {
2366268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                if (cl != null) {
2367268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    cl.clearFolderLeaveBehind();
2368268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    // Remove the ImageView copy of the FolderIcon and make the original visible.
2369268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    mDragLayer.removeView(mFolderIconImageView);
2370268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen                    fi.setVisibility(View.VISIBLE);
2371c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen                }
2372c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen            }
2373c51934bfdfed6a5011c6d6c5b7b70f2d75613d41Adam Cohen        });
23742801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen        oa.start();
23752801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen    }
23762801cafe62653131fdc9da402e5c44e5ffd0bf47Adam Cohen
237731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
2378774bd377df3053ef43e34ab8ca98160eb6811be1Michael Jurka     * Opens the user folder described by the specified tag. The opening of the folder
237931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * is animated relative to the specified View. If the View is null, no animation
238031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * is played.
238131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     *
238231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     * @param folderInfo The FolderInfo describing the folder to open.
238331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
2384a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen    public void openFolder(FolderIcon folderIcon) {
2385fb91f303f5e672f9b4c4f9f8a03486b224d60cd6Adam Cohen        Folder folder = folderIcon.getFolder();
2386a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        FolderInfo info = folder.mInfo;
238731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2388a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        info.opened = true;
238931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
23904554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        // Just verify that the folder hasn't already been added to the DragLayer.
23914554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        // There was a one-off crash where the folder had a parent already.
23924554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (folder.getParent() == null) {
23934554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            mDragLayer.addView(folder);
23944554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            mDragController.addDropTarget((DropTarget) folder);
23954554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        } else {
23964554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
23974554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen                    folder.getParent() + ").");
23984554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
2399a9cf38f533d1e86269868f1e6a806ccffd4a78fcAdam Cohen        folder.animateOpen();
2400268c4751b547e1470a94030af73a4e7bb8ea221eAdam Cohen        growAndFadeOutFolderIcon(folderIcon);
240183ca480941b3dd635b62347008fc8d7859d82002Winson Chung
240283ca480941b3dd635b62347008fc8d7859d82002Winson Chung        // Notify the accessibility manager that this folder "window" has appeared and occluded
240383ca480941b3dd635b62347008fc8d7859d82002Winson Chung        // the workspace items
240483ca480941b3dd635b62347008fc8d7859d82002Winson Chung        folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
240583ca480941b3dd635b62347008fc8d7859d82002Winson Chung        getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
24064554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    }
24074554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
24084554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    public void closeFolder() {
24094554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        Folder folder = mWorkspace.getOpenFolder();
24104554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (folder != null) {
2411ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            if (folder.isEditingName()) {
2412ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen                folder.dismissEditingName();
2413ac56cff1860b71d3f164aedd268703936e08fdc0Adam Cohen            }
24144554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            closeFolder(folder);
24157d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung
24167d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            // Dismiss the folder cling
24177d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            dismissFolderCling(null);
24184554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
24194554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    }
24204554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
24214554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen    void closeFolder(Folder folder) {
24224554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        folder.getInfo().opened = false;
24234554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen
24244554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        ViewGroup parent = (ViewGroup) folder.getParent().getParent();
24254554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        if (parent != null) {
24264554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
24274554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen            shrinkAndFadeInFolderIcon(fi);
24284554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        }
24294554ee16f122a3dbd9a8d3b828f1c8c7e7b7fe99Adam Cohen        folder.animateClosed();
243083ca480941b3dd635b62347008fc8d7859d82002Winson Chung
243183ca480941b3dd635b62347008fc8d7859d82002Winson Chung        // Notify the accessibility manager that this folder "window" has disappeard and no
243283ca480941b3dd635b62347008fc8d7859d82002Winson Chung        // longer occludeds the workspace items
243383ca480941b3dd635b62347008fc8d7859d82002Winson Chung        getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
243431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
243531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
243631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    public boolean onLongClick(View v) {
243736a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (!isDraggingEnabled()) return false;
243836a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (isWorkspaceLocked()) return false;
243936a62fe917be0a2520c457f985075fb5d3d09d1cWinson Chung        if (mState != State.WORKSPACE) return false;
244031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
244131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        if (!(v instanceof CellLayout)) {
24428c920dd3683d752aa4c43e964831ce53f9b72887Michael Jurka            v = (View) v.getParent().getParent();
244331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
244431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24450280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        resetAddInfo();
24460280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
244731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        // This happens when long clicking an item with the dpad/trackball
2448faea1f816ead0033e52b730ab4dd598d2c186168Adam Cohen        if (longClickCellInfo == null) {
244931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            return true;
245031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
245131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // The hotseat touch handling does not go through Workspace, and we always allow long press
24533d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        // on hotseat items.
24540280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka        final View itemUnderLongClick = longClickCellInfo.cell;
24553d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
24563d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (allowLongPress && !mDragController.isDragging()) {
24570280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka            if (itemUnderLongClick == null) {
24580280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                // User long pressed on empty space
24590280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
24600280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                        HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
24617d30a37007bac318db1c9af47a9af12d348042a5Adam Cohen                // Disabling reordering until we sort out some issues.
2462dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen                if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2463dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen                    mWorkspace.startReordering();
2464dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen                } else {
2465dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen                    startWallpaper();
2466dedbd962dcfaf055a6835abf5b0000ce1564e51eAdam Cohen                }
246731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            } else {
24680280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                if (!(itemUnderLongClick instanceof Folder)) {
246931dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                    // User long pressed on an item
24700280c3be4d9f8fc6fdf015b7ecd276eb26f76f2dMichael Jurka                    mWorkspace.startDrag(longClickCellInfo);
247131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project                }
247231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project            }
247331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
247431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return true;
247531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
247631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
24773d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    boolean isHotseatLayout(View layout) {
24783d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return mHotseat != null && layout != null &&
24793d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
24801fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy    }
24813d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    Hotseat getHotseat() {
24823d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        return mHotseat;
24831fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy    }
2484ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen    SearchDropTargetBar getSearchBar() {
2485ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen        return mSearchDropTargetBar;
2486ebea84d1c95f4c38ba8cee46cd586fd757b4fce2Adam Cohen    }
24871fbc1c843170fe0c9326bb3ffd5bd9c5c599ca2eRomain Guy
24883d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    /**
24893d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Returns the CellLayout of the specified container at the specified screen.
24903d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     */
2491dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    CellLayout getCellLayout(long container, long screenId) {
24923d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
24933d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            if (mHotseat != null) {
24943d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return mHotseat.getLayout();
24953d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            } else {
24963d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                return null;
24979d31e9fa3dedf7c51b407c1c5fafe5294fc3c01bRomain Guy            }
24983d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        } else {
2499dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            return (CellLayout) mWorkspace.getScreenWithId(screenId);
2500a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy        }
2501a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy    }
2502a6abce8464b57ce91e8f083951ad263370fc2da8Romain Guy
250331dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    Workspace getWorkspace() {
250431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        return mWorkspace;
250531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
250631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
2507843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler    public boolean isAllAppsVisible() {
2508c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung        return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
2509fb0ca67aea25f4282096d9247ddaebee6fbf7e00Joe Onorato    }
2510fb0ca67aea25f4282096d9247ddaebee6fbf7e00Joe Onorato
25116b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    /**
25126b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * Helper method for the cameraZoomIn/cameraZoomOut animations
25136b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param view The view being animated
25146b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param scaleFactor The scale factor used for the zoom
25156b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     */
2516b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    private void setPivotsForZoom(View view, float scaleFactor) {
25176b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        view.setPivotX(view.getWidth() / 2.0f);
25187777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen        view.setPivotY(view.getHeight() / 2.0f);
25196b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy    }
2520c351eb8b6a057366f2878f5201f2106ffce439edDaniel Sandler
252118f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    void disableWallpaperIfInAllApps() {
252218f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        // Only disable it if we are in all apps
2523c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung        if (isAllAppsVisible()) {
252418f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            if (mAppsCustomizeTabHost != null &&
252518f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung                    !mAppsCustomizeTabHost.isTransitioning()) {
252618f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung                updateWallpaperVisibility(false);
252718f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            }
252818f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        }
252918f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    }
253018f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung
2531360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner    private void setWorkspaceBackground(boolean workspace) {
2532360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        mLauncherView.setBackground(workspace ?
2533496fefb40d403101d0bf1d35ab77a4ae2e85843cMichael Jurka                mWorkspaceBackgroundDrawable : null);
2534360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner    }
2535360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner
2536bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn    void updateWallpaperVisibility(boolean visible) {
2537bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2538bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        int curflags = getWindow().getAttributes().flags
2539bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2540bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        if (wpflags != curflags) {
2541bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn            getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2542bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        }
2543360310b74de10d7bd2cc0a2ab874b1d246a6a7baCraig Mautner        setWorkspaceBackground(visible);
2544bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn    }
2545bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn
2546a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2547a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        if (v instanceof LauncherTransitionable) {
2548a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2549a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        }
2550a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    }
2551a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka
2552bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2553bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        if (v instanceof LauncherTransitionable) {
2554bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2555bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        }
25567044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
25577044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        // Update the workspace transition step as well
25587044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        dispatchOnLauncherTransitionStep(v, 0f);
25597044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    }
25607044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
25617044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    private void dispatchOnLauncherTransitionStep(View v, float t) {
25627044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        if (v instanceof LauncherTransitionable) {
25637044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
25647044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        }
2565bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    }
2566bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2567bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2568bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        if (v instanceof LauncherTransitionable) {
2569bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2570bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        }
25717044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung
25727044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        // Update the workspace transition step as well
25737044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung        dispatchOnLauncherTransitionStep(v, 1f);
2574bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    }
2575bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
25763ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    /**
2577b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     * Things to test when changing the following seven functions.
2578b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Home from workspace
2579b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2580b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2581b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Home from all apps
2582b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2583b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2584b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Back from all apps
2585b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from center screen
2586b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - from other screens
2587b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch app from workspace and quit
2588b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2589b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2590b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch app from all apps and quit
2591b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2592b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2593b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Go to a screen that's not the default, then all
2594b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *     apps, and launch and app, and go back
2595b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2596b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          -with home
2597b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On workspace, long press power and go back
2598b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2599b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2600b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On all apps, long press power and go back
2601b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with back
2602b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - with home
2603b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On workspace, power off
2604b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - On all apps, power off
2605b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Launch an app and turn off the screen while in that app
2606b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - Go back with home key
2607b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - Go back with back key  TODO: make this not go to workspace
2608b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From all apps
2609b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From workspace
2610b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *   - Enter and exit car mode (becuase it causes an extra configuration changed)
2611b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From all apps
2612b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From the center workspace
2613b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     *          - From another workspace
2614b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     */
2615b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2616b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    /**
26173ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Zoom the camera out from the workspace to reveal 'toView'.
26183ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Assumes that the view to show is anchored at either the very top or very bottom
26193ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * of the screen.
26203ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     */
2621bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
2622b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mStateAnimation != null) {
2623f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka            mStateAnimation.setDuration(0);
2624b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation.cancel();
2625b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation = null;
2626b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        }
26273ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy        final Resources res = getResources();
2628f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
2629f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2630f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2631f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2632bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        final View fromView = mWorkspace;
2633a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka        final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
2634cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen        final int startDelay =
2635cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
26363ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy
2637b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        setPivotsForZoom(toView, scale);
26387247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2639d74c984fcc61f86160476a2543faff715e3ee2fdMichael Jurka        // Shrink workspaces away if going to AppsCustomize from workspace
26402a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        Animator workspaceAnim =
26412a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
26427247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2643df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        if (animated) {
26447407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            toView.setScaleX(scale);
26457407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            toView.setScaleY(scale);
26467407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
26477407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            scaleAnim.
26487407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                scaleX(1f).scaleY(1f).
26497407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                setDuration(duration).
26507407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                setInterpolator(new Workspace.ZoomOutInterpolator());
265161033d3d86fcae1f654f3fbfc9979d131e265d76Adam Cohen
2652f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            toView.setVisibility(View.VISIBLE);
2653c00f0b996712243e0745ea1af870b0229ca11a73Adam Cohen            toView.setAlpha(0f);
2654f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka            final ObjectAnimator alphaAnim = LauncherAnimUtils
2655159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .ofFloat(toView, "alpha", 0f, 1f)
2656159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .setDuration(fadeDuration);
2657f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
26587044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
26597044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                @Override
26607044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                public void onAnimationUpdate(ValueAnimator animation) {
2661059798af0927ade335bd9ed7a552798b17690bd8Michael Jurka                    if (animation == null) {
2662059798af0927ade335bd9ed7a552798b17690bd8Michael Jurka                        throw new RuntimeException("animation is null");
2663059798af0927ade335bd9ed7a552798b17690bd8Michael Jurka                    }
26647044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    float t = (Float) animation.getAnimatedValue();
26657044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(fromView, t);
26667044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(toView, t);
26677044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                }
26687044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            });
2669f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohen
26702a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // toView should appear right at the end of the workspace shrink
26712a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // animation
26722ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka            mStateAnimation = LauncherAnimUtils.createAnimatorSet();
26732a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mStateAnimation.play(scaleAnim).after(startDelay);
26747407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka            mStateAnimation.play(alphaAnim).after(startDelay);
26752a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
26762a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2677f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                boolean animationCancelled = false;
2678f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen
2679dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
2680b1254a6bdb11024042fb6c27e179210c1bbd6e1cChet Haase                public void onAnimationStart(Animator animation) {
2681bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                    updateWallpaperVisibility(true);
2682df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    // Prepare the position
2683df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setTranslationX(0.0f);
2684df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setTranslationY(0.0f);
2685df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    toView.setVisibility(View.VISIBLE);
2686785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung                    toView.bringToFront();
2687df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                }
26883c4c20fbe682cb4b3ef94f09afe0af09171583f3Michael Jurka                @Override
26898edd75c8bb0729a10cb39f614183e3e9ae4288e8Michael Jurka                public void onAnimationEnd(Animator animation) {
2690bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(fromView, animated, false);
2691bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(toView, animated, false);
269232174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung
2693e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                    if (mWorkspace != null
2694e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                            && !springLoaded
2695e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler                            && !LauncherAppState.getInstance().isScreenLarge()) {
2696b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                        hideDockDivider();
269732174c878c77b4e0f13449f4c26ff6b487bc7ca6Winson Chung                    }
2698f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    if (!animationCancelled) {
2699f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                        updateWallpaperVisibility(false);
2700f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    }
2701c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2702c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                    // Hide the search bar
2703adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    if (mSearchDropTargetBar != null) {
2704adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                        mSearchDropTargetBar.hideSearchBar(false);
2705adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    }
2706f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                }
2707f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen
2708cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen                @Override
2709f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                public void onAnimationCancel(Animator animation) {
2710f4ddea3df72fad2e0fb022cc7212d87d8cb69143Adam Cohen                    animationCancelled = true;
27113c4c20fbe682cb4b3ef94f09afe0af09171583f3Michael Jurka                }
2712df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            });
27136b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
27142a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (workspaceAnim != null) {
27152a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mStateAnimation.play(workspaceAnim);
27162a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
27171899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka
27181899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            boolean delayAnim = false;
27192a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
2720a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2721a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, false);
27222a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
27232a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // If any of the objects being animated haven't been measured/laid out
27242a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            // yet, delay the animation until we get a layout pass
2725bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
27262a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    (mWorkspace.getMeasuredWidth() == 0) ||
27272a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    (toView.getMeasuredWidth() == 0)) {
27282a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                delayAnim = true;
27291899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            }
27302a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
27313dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            final AnimatorSet stateAnimation = mStateAnimation;
27323dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            final Runnable startAnimRunnable = new Runnable() {
27333dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                public void run() {
27343dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    // Check that mStateAnimation hasn't changed while
27353dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    // we waited for a layout/draw pass
27363dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    if (mStateAnimation != stateAnimation)
27373dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                        return;
27383dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    setPivotsForZoom(toView, scale);
27393dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    dispatchOnLauncherTransitionStart(fromView, animated, false);
27403dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                    dispatchOnLauncherTransitionStart(toView, animated, false);
2741f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                    LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
27423dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                }
27433dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka            };
27442a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (delayAnim) {
2745f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                final ViewTreeObserver observer = toView.getViewTreeObserver();
2746f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2747f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                        public void onGlobalLayout() {
2748f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                            startAnimRunnable.run();
2749f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                            toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2750f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                        }
2751f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka                    });
27522a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            } else {
27533dcd79ecb39af92b94ac7f41558c2743a51f0596Michael Jurka                startAnimRunnable.run();
27541899a36ab3aab7671420511f47eb8b273b9b1078Michael Jurka            }
27556b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        } else {
2756df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setTranslationX(0.0f);
2757df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setTranslationY(0.0f);
2758df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setScaleX(1.0f);
2759df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setScaleY(1.0f);
2760df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            toView.setVisibility(View.VISIBLE);
2761785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            toView.bringToFront();
2762bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2763e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler            if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
2764bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                hideDockDivider();
2765c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2766c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung                // Hide the search bar
2767adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                if (mSearchDropTargetBar != null) {
2768adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    mSearchDropTargetBar.hideSearchBar(false);
2769adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                }
2770abded66084680bb31cc7ea403c88f44f79a3c884Michael Jurka            }
2771a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2772bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, false);
2773bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(fromView, animated, false);
2774a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, false);
2775bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(toView, animated, false);
2776bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(toView, animated, false);
2777bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn            updateWallpaperVisibility(false);
27786b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy        }
27793ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    }
27807247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
27813ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    /**
27823ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     * Zoom the camera back into the workspace, hiding 'fromView'.
2783b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka     * This is the opposite of showAppsCustomizeHelper.
27846b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy     * @param animated If true, the transition will be animated.
27853ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy     */
2786ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    private void hideAppsCustomizeHelper(State toState, final boolean animated,
2787ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final boolean springLoaded, final Runnable onCompleteRunnable) {
2788bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2789b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mStateAnimation != null) {
2790f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka            mStateAnimation.setDuration(0);
2791b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation.cancel();
2792b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mStateAnimation = null;
2793b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        }
27943ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy        Resources res = getResources();
2795f16e571330f030e445c4f7f63f55d093337ffd87Adam Cohen
2796f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2797159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka        final int fadeOutDuration =
2798159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
2799f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final float scaleFactor = (float)
2800f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung                res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2801f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        final View fromView = mAppsCustomizeTabHost;
2802bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka        final View toView = mWorkspace;
28032a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        Animator workspaceAnim = null;
28042a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka
28052a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        if (toState == State.WORKSPACE) {
28062a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
28072a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            workspaceAnim = mWorkspace.getChangeStateAnimation(
28082a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    Workspace.State.NORMAL, animated, stagger);
28092a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
28102a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            workspaceAnim = mWorkspace.getChangeStateAnimation(
28112a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                    Workspace.State.SPRING_LOADED, animated);
28122a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka        }
28132b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
2814b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        setPivotsForZoom(fromView, scaleFactor);
2815bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn        updateWallpaperVisibility(true);
28164afe9b31f51022538b30646fe83780e83bf22490Winson Chung        showHotseat(animated);
2817df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        if (animated) {
2818159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            final LauncherViewPropertyAnimator scaleAnim =
2819159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                    new LauncherViewPropertyAnimator(fromView);
2820159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka            scaleAnim.
2821159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                scaleX(scaleFactor).scaleY(scaleFactor).
2822159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                setDuration(duration).
2823159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                setInterpolator(new Workspace.ZoomInInterpolator());
2824159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka
2825f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka            final ObjectAnimator alphaAnim = LauncherAnimUtils
2826159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .ofFloat(fromView, "alpha", 1f, 0f)
2827159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka                .setDuration(fadeOutDuration);
2828cff6af81d721415785bddc2efd74e179d2def9daAdam Cohen            alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
28297044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
28307044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                @Override
28317044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                public void onAnimationUpdate(ValueAnimator animation) {
28327044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    float t = 1f - (Float) animation.getAnimatedValue();
28337044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(fromView, t);
28347044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                    dispatchOnLauncherTransitionStep(toView, t);
28357044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung                }
28367044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung            });
2837159b4cc1e463ba730dbdff7c4ada2af01f35a1f1Michael Jurka
28382ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka            mStateAnimation = LauncherAnimUtils.createAnimatorSet();
2839bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2840a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2841a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, true);
2842bc2f082dac475eff7fc0e40f90eb6c0552511170Chet Haase            mAppsCustomizeContent.pauseScrolling();
2843bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka
2844bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            mStateAnimation.addListener(new AnimatorListenerAdapter() {
2845dd6c992943a139ce277768b5715e276cfa974f91Gilles Debunne                @Override
28468edd75c8bb0729a10cb39f614183e3e9ae4288e8Michael Jurka                public void onAnimationEnd(Animator animation) {
2847bb003a540bb4599e020b36c2680e0f0f520b32d6Dianne Hackborn                    updateWallpaperVisibility(true);
2848df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                    fromView.setVisibility(View.GONE);
2849bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(fromView, animated, true);
2850bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka                    dispatchOnLauncherTransitionEnd(toView, animated, true);
2851ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    if (onCompleteRunnable != null) {
2852ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                        onCompleteRunnable.run();
2853ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    }
2854bc2f082dac475eff7fc0e40f90eb6c0552511170Chet Haase                    mAppsCustomizeContent.updateCurrentPageScroll();
2855bc2f082dac475eff7fc0e40f90eb6c0552511170Chet Haase                    mAppsCustomizeContent.resumeScrolling();
2856df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy                }
2857df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            });
28586b509c1fd62a39916bb957eac07917161f549206Patrick Dubroy
2859f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            mStateAnimation.playTogether(scaleAnim, alphaAnim);
28602a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            if (workspaceAnim != null) {
28612a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka                mStateAnimation.play(workspaceAnim);
28622a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka            }
2863a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, true);
2864a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionStart(toView, animated, true);
2865f1ad608c28c79c8e9b83d83ce9154f1b7284f412Michael Jurka            LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
2866df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        } else {
2867df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy            fromView.setVisibility(View.GONE);
2868a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2869bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(fromView, animated, true);
2870bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(fromView, animated, true);
2871a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka            dispatchOnLauncherTransitionPrepare(toView, animated, true);
2872bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionStart(toView, animated, true);
2873bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka            dispatchOnLauncherTransitionEnd(toView, animated, true);
2874df7c3a7853abd4dec460247370fe0b17b50866acPatrick Dubroy        }
28753ec8bdd576e23f6aa783d5377abecac6fda07374Patrick Dubroy    }
28767247f6315baf16eacb3286f21bd80321385c1defPatrick Dubroy
2877e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    @Override
2878e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    public void onTrimMemory(int level) {
2879e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka        super.onTrimMemory(level);
2880c7450e3ebd7672fcd7b9d51a8c1e68f63c5ce86bWinson Chung        if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
2881e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka            mAppsCustomizeTabHost.onTrimMemory();
2882e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka        }
2883e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka    }
2884e326f186af6b00e4ea32849f1527254c669d0600Michael Jurka
288518f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    @Override
288618f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    public void onWindowFocusChanged(boolean hasFocus) {
288718f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        if (!hasFocus) {
288818f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // When another window occludes launcher (like the notification shade, or recents),
288918f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // ensure that we enable the wallpaper flag so that transitions are done correctly.
289018f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            updateWallpaperVisibility(true);
289118f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        } else {
289218f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung            // When launcher has focus again, disable the wallpaper if we are in AllApps
28936cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung            mWorkspace.postDelayed(new Runnable() {
28946cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                @Override
28956cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                public void run() {
28966cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                    disableWallpaperIfInAllApps();
28976cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung                }
28986cf79096ce2dcd0ba4cacc82b9f1d83ea69b5686Winson Chung            }, 500);
289918f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung        }
290018f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung    }
290118f41f8327860d7e257d063888d08b6b5c528fe9Winson Chung
2902c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka    void showWorkspace(boolean animated) {
2903ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen        showWorkspace(animated, null);
2904ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    }
2905ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen
2906ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
2907b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mState != State.WORKSPACE) {
2908c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
2909b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            mWorkspace.setVisibility(View.VISIBLE);
2910ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
2911b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2912c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            // Show the search bar (only animate if we were showing the drop target bar in spring
2913c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            // loaded mode)
2914adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung            if (mSearchDropTargetBar != null) {
2915adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2916adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung            }
2917c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung
2918b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            // We only need to animate in the dock divider if we're going from spring loaded mode
2919c7d2b60ab1db9a478f173d045b9f501d994eb8b1Winson Chung            showDockDivider(animated && wasInSpringLoadedMode);
2920b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2921b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            // Set focus to the AppsCustomize button
2922b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            if (mAllAppsButton != null) {
2923b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka                mAllAppsButton.requestFocus();
2924b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            }
2925c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        }
29266b879f0a5885274a85333531e091283405d490ccAdam Lesinski
2927c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        // Change the state *after* we've called all the transition code
2928c0e8fcae492730210b2b8b6d5bb3d65636bad245Michael Jurka        mState = State.WORKSPACE;
2929815ba2d2eabded5ffee53fba668d63ca9173e6fdSvetoslav Ganov
29305fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        // Resume the auto-advance of widgets
29315fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        mUserPresent = true;
29325fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung        updateRunning();
29335fb63473cbc7f1b77e45c765d56b01361772429bWinson Chung
29341d4fde686433ddfa7f940e298d935433ecfed8ebalanv        // Send an accessibility event to announce the context change
29351d4fde686433ddfa7f940e298d935433ecfed8ebalanv        getWindow().getDecorView()
29361d4fde686433ddfa7f940e298d935433ecfed8ebalanv                .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2937c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert
293869688aa359cdbffce4decf334f5d6f0143e3a694Bjorn Bringert        onWorkspaceShown(animated);
2939c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert    }
2940c459e5241ea0c53dd53c16e73fd7a187701f537bBjorn Bringert
294169688aa359cdbffce4decf334f5d6f0143e3a694Bjorn Bringert    public void onWorkspaceShown(boolean animated) {
294200acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato    }
294300acb123c5100f06b8e89e8ec8978ebafc6f6d26Joe Onorato
2944b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void showAllApps(boolean animated) {
2945b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        if (mState != State.WORKSPACE) return;
2946b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2947b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        showAppsCustomizeHelper(animated, false);
2948b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mAppsCustomizeTabHost.requestFocus();
2949b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2950b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Change the state *after* we've called all the transition code
2951b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mState = State.APPS_CUSTOMIZE;
2952b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2953b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Pause the auto-advance of widgets until we are out of AllApps
2954b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        mUserPresent = false;
2955b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        updateRunning();
2956b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        closeFolder();
2957b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2958b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // Send an accessibility event to announce the context change
29591d4fde686433ddfa7f940e298d935433ecfed8ebalanv        getWindow().getDecorView()
29601d4fde686433ddfa7f940e298d935433ecfed8ebalanv                .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2961b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
2962b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
29637777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen    void enterSpringLoadedDragMode() {
2964c93e5ae12018bb214099ff88a48cc21580aec4c3Winson Chung        if (isAllAppsVisible()) {
2965ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
2966b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            hideDockDivider();
2967c07918d0053fc7d2a19d7b013565a5d2e7d4af51Winson Chung            mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2968b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        }
2969d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
29707777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen
2971ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen    void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2972ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen            final Runnable onCompleteRunnable) {
297309bfc45ae8aee2db579698c15007b1d138b4141fWinson Chung        if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
297409bfc45ae8aee2db579698c15007b1d138b4141fWinson Chung
2975e7a0394ad468748fb4651c74b44b313e1e07cab6Winson Chung        mHandler.postDelayed(new Runnable() {
2976557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            @Override
2977557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            public void run() {
29786a3fd3f042c8bfa7d83e29c1eeadff21323ab089Winson Chung                if (successfulDrop) {
29797bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // Before we show workspace, hide all apps again because
29807bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
29817bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    // clean up our state transition functions
29827bdb25a9894970da74248c9cdc606d0e1ba05c99Michael Jurka                    mAppsCustomizeTabHost.setVisibility(View.GONE);
2983ed66b2bac7447febe2e405b4ce725cae4f6b5988Adam Cohen                    showWorkspace(true, onCompleteRunnable);
29847777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                } else {
29857777d967a60ecfd34ad2d94141d598fca4021058Adam Cohen                    exitSpringLoadedDragMode();
29866a3fd3f042c8bfa7d83e29c1eeadff21323ab089Winson Chung                }
2987557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung            }
2988557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung        }, (extendedDelay ?
2989557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2990557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung                EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2991557d6ed50cc6560163440aa134870ef0bb11415bWinson Chung    }
2992b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
2993d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    void exitSpringLoadedDragMode() {
2994b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2995b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            final boolean animated = true;
2996b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            final boolean springLoaded = true;
2997b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka            showAppsCustomizeHelper(animated, springLoaded);
2998b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung            mState = State.APPS_CUSTOMIZE;
2999f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
3000f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        // Otherwise, we are not in spring loaded mode, so don't do anything.
3001f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    }
3002f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3003b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    void hideDockDivider() {
3004d2be381d36485059d0108ae89cfe57a30a7a8499Winson Chung        if (mQsbDivider != null) {
3005b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mQsbDivider.setVisibility(View.INVISIBLE);
3006b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        }
3007b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    }
3008b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka
3009b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    void showDockDivider(boolean animated) {
3010d2be381d36485059d0108ae89cfe57a30a7a8499Winson Chung        if (mQsbDivider != null) {
3011b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            mQsbDivider.setVisibility(View.VISIBLE);
3012b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            if (mDividerAnimator != null) {
3013b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.cancel();
3014b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mQsbDivider.setAlpha(1f);
3015b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator = null;
3016b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            }
3017b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            if (animated) {
30182ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka                mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3019d2be381d36485059d0108ae89cfe57a30a7a8499Winson Chung                mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
3020adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                int duration = 0;
3021adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                if (mSearchDropTargetBar != null) {
3022adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    duration = mSearchDropTargetBar.getTransitionInDuration();
3023adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                }
3024adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                mDividerAnimator.setDuration(duration);
3025b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka                mDividerAnimator.start();
3026b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka            }
3027b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka        }
3028b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka    }
3029b737ee6ab2dc4938a1b2a574c336af0eab826712Michael Jurka
3030b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void lockAllApps() {
3031b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // TODO
3032b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
3033b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
3034b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    void unlockAllApps() {
3035b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka        // TODO
3036b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka    }
3037b3e22d99c1682642352cec71aace0adcef7296a6Michael Jurka
3038f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    /**
30393d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Shows the hotseat area.
3040f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung     */
30413d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void showHotseat(boolean animated) {
3042e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        if (!LauncherAppState.getInstance().isScreenLarge()) {
3043f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            if (animated) {
30447407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                if (mHotseat.getAlpha() != 1f) {
3045adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    int duration = 0;
3046adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    if (mSearchDropTargetBar != null) {
3047adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                        duration = mSearchDropTargetBar.getTransitionInDuration();
3048adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    }
30497407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    mHotseat.animate().alpha(1f).setDuration(duration);
30507407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                }
3051f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            } else {
30523d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mHotseat.setAlpha(1f);
3053f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            }
3054f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung        }
3055f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    }
3056f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3057f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung    /**
30583d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung     * Hides the hotseat area.
3059f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung     */
30603d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung    void hideHotseat(boolean animated) {
3061e4f9891f01bdc10d8f96e4e2429e2f4d0558238bDaniel Sandler        if (!LauncherAppState.getInstance().isScreenLarge()) {
3062f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            if (animated) {
30637407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                if (mHotseat.getAlpha() != 0f) {
3064adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    int duration = 0;
3065adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    if (mSearchDropTargetBar != null) {
3066adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                        duration = mSearchDropTargetBar.getTransitionOutDuration();
3067adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung                    }
30687407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                    mHotseat.animate().alpha(0f).setDuration(duration);
30697407d2a16ed6cf22494122cf683bf13de6fa3695Michael Jurka                }
3070f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            } else {
30713d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                mHotseat.setAlpha(0f);
3072f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung            }
3073b26f3d6a8c62e7c1a603b6c7979375d8dd4f20d4Winson Chung        }
3074d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka    }
3075d3ef3065ab0941567c45e9aec98783138b623c68Michael Jurka
30765f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    /**
30775f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     * Add an item from all apps or customize onto the given workspace screen.
30785f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     * If layout is null, add to the current screen.
30795f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy     */
30805f445425606b2c27276a79e9e871cf7f67b1b719Patrick Dubroy    void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
30816b4b25d093b0a3008d6e275438c79db9c21daab9Michael Jurka        if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
308293eef082ec2da802ee4507fb2ca729e550f833f1Winson Chung            showOutOfSpaceMessage(isHotseatLayout(layout));
3083213d96354e7407dba5c9715eb10d04bebd82c395Michael Jurka        }
30846b4b25d093b0a3008d6e275438c79db9c21daab9Michael Jurka    }
30852b9ff37edb3f5965559b3ff7d37e418b4a2917a1Patrick Dubroy
3086dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    /** Maps the current orientation to an index for referencing orientation correct global icons */
3087dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    private int getCurrentOrientationIndexForGlobalIcons() {
3088dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        // default - 0, landscape - 1
3089dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        switch (getResources().getConfiguration().orientation) {
3090dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        case Configuration.ORIENTATION_LANDSCAPE:
3091dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            return 1;
3092dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        default:
3093dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            return 0;
3094dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung        }
3095dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung    }
3096dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung
3097ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka    private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
30980423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        try {
3099ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy            PackageManager packageManager = getPackageManager();
31000423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            // Look for the toolbar icon specified in the activity meta-data
31010423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            Bundle metaData = packageManager.getActivityInfo(
31020423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                    activityName, PackageManager.GET_META_DATA).metaData;
31030423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            if (metaData != null) {
3104ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                int iconResId = metaData.getInt(resourceName);
31050423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                if (iconResId != 0) {
31060423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka                    Resources res = packageManager.getResourcesForActivity(activityName);
3107fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung                    return res.getDrawable(iconResId);
3108ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy                }
31090423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka            }
31100423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        } catch (NameNotFoundException e) {
3111b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka            // This can happen if the activity defines an invalid drawable
3112b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka            Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3113b6052a9a0c7b57f0f4bd6c2b0c9b3280f0b1ed5bMichael Jurka                    " not found", e);
311470d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood        } catch (Resources.NotFoundException nfe) {
311570d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood            // This can happen if the activity defines an invalid drawable
311670d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood            Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
311770d510293964eba0fca9377baef29d27cba3abf6Mathew Inwood                    nfe);
31180423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
3119fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        return null;
3120fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
3121fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
3122fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    // if successful in getting icon, return it; otherwise, set button to use default drawable
3123fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3124ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            int buttonId, ComponentName activityName, int fallbackDrawableId,
3125ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            String toolbarResourceName) {
3126ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
3127128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        Resources r = getResources();
3128128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3129128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3130fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
31314da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka        TextView button = (TextView) findViewById(buttonId);
3132fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        // If we were unable to find the icon via the meta-data, use a generic one
3133fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        if (toolbarIcon == null) {
3134128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon = r.getDrawable(fallbackDrawableId);
3135128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon.setBounds(0, 0, w, h);
31364da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            if (button != null) {
31374da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka                button.setCompoundDrawables(toolbarIcon, null, null, null);
31384da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            }
3139fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung            return null;
3140fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        } else {
3141128bbcd1e6606b3c1933c8e85a32b30b3e58ead3Winson Chung            toolbarIcon.setBounds(0, 0, w, h);
31424da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            if (button != null) {
31434da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka                button.setCompoundDrawables(toolbarIcon, null, null, null);
31444da7a3e55618a6ac2eb528a792902bbbfe775c79Michael Jurka            }
3145fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung            return toolbarIcon.getConstantState();
3146fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        }
3147fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
3148fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
3149fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    // if successful in getting icon, return it; otherwise, set button to use default drawable
3150fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3151ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            int buttonId, ComponentName activityName, int fallbackDrawableId,
3152ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            String toolbarResourceName) {
3153fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        ImageView button = (ImageView) findViewById(buttonId);
3154ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
3155fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
315619e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka        if (button != null) {
315719e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            // If we were unable to find the icon via the meta-data, use a
315819e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            // generic one
315919e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            if (toolbarIcon == null) {
316019e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka                button.setImageResource(fallbackDrawableId);
316119e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            } else {
316219e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka                button.setImageDrawable(toolbarIcon);
316319e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka            }
31640423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
316519e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka
316619e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka        return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
316719e0fc53c43a7746d7ae3d7c6027166bded33f0fMichael Jurka
31680423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka    }
31690423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka
31701b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung    private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
3171fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung        TextView button = (TextView) findViewById(buttonId);
31721b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        button.setCompoundDrawables(d, null, null, null);
3173fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung    }
3174fbb3d9b55e50e8661c45c4284026f3bb05fa95fcWinson Chung
3175e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
31764ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        ImageView button = (ImageView) findViewById(buttonId);
3177e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka        button.setImageDrawable(d.newDrawable(getResources()));
31784ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
31794ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3180bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    private void invalidatePressedFocusedStates(View container, View button) {
3181bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        if (container instanceof HolographicLinearLayout) {
3182bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            HolographicLinearLayout layout = (HolographicLinearLayout) container;
3183bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            layout.invalidatePressedFocusedStates();
3184bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        } else if (button instanceof HolographicImageView) {
3185bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            HolographicImageView view = (HolographicImageView) button;
3186bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            view.invalidatePressedFocusedStates();
3187bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        }
3188bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung    }
3189bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung
3190c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private boolean updateGlobalSearchIcon() {
3191c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View searchButtonContainer = findViewById(R.id.search_button_container);
31921cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
3193c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3194cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung        final View voiceButton = findViewById(R.id.voice_button);
319597d85d23b013347bead4e2f5fa430a79ce69431eWinson Chung
31961cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final SearchManager searchManager =
31971cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
31981cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        ComponentName activityName = searchManager.getGlobalSearchActivity();
31991cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        if (activityName != null) {
3200dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3201dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3202ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3203ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_SEARCH_ICON_METADATA_NAME);
3204ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (sGlobalSearchIcon[coi] == null) {
3205ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3206ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3207ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        TOOLBAR_ICON_METADATA_NAME);
3208ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
3209ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3210c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3211c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchButton.setVisibility(View.VISIBLE);
3212bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            invalidatePressedFocusedStates(searchButtonContainer, searchButton);
3213c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return true;
32141cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        } else {
3215cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung            // We disable both search and voice search when there is no global search provider
3216c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3217c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3218c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            searchButton.setVisibility(View.GONE);
3219cbf7c4d50ce2609518640c3f590f6df435edabaeWinson Chung            voiceButton.setVisibility(View.GONE);
3220ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert            setVoiceButtonProxyVisible(false);
3221c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return false;
32226d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani        }
32236d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
32246d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
3225e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateGlobalSearchIcon(Drawable.ConstantState d) {
3226bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View searchButtonContainer = findViewById(R.id.search_button_container);
3227bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View searchButton = (ImageView) findViewById(R.id.search_button);
32284ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        updateButtonWithDrawable(R.id.search_button, d);
3229bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        invalidatePressedFocusedStates(searchButtonContainer, searchButton);
32304ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
32314ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3232c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung    private boolean updateVoiceSearchIcon(boolean searchVisible) {
3233c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
32341cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        final View voiceButton = findViewById(R.id.voice_button);
32351cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung
3236c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // We only show/update the voice search icon if the search icon is enabled as well
3237ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        final SearchManager searchManager =
3238ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3239ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3240ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3241ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        ComponentName activityName = null;
3242ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        if (globalSearchActivity != null) {
3243ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // Check if the global search activity handles voice search
3244ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3245ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            intent.setPackage(globalSearchActivity.getPackageName());
3246ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            activityName = intent.resolveActivity(getPackageManager());
3247ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
3248ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka
3249ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        if (activityName == null) {
3250ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // Fallback: check if an activity other than the global search activity
3251ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            // resolves this
3252ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3253ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            activityName = intent.resolveActivity(getPackageManager());
3254ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka        }
3255c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        if (searchVisible && activityName != null) {
3256dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3257dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3258ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3259ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3260ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            if (sVoiceSearchIcon[coi] == null) {
3261ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3262ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3263ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                        TOOLBAR_ICON_METADATA_NAME);
3264ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka            }
3265c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
32661cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung            voiceButton.setVisibility(View.VISIBLE);
3267ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert            setVoiceButtonProxyVisible(true);
3268bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung            invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
3269c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return true;
32701cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung        } else {
3271c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
32721cad91e625386885a4b5f82a7fa3f05c5e575750Winson Chung            voiceButton.setVisibility(View.GONE);
3273ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert            setVoiceButtonProxyVisible(false);
3274c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung            return false;
3275ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy        }
32766ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    }
32770423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka
3278e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateVoiceSearchIcon(Drawable.ConstantState d) {
3279bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3280bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        final View voiceButton = findViewById(R.id.voice_button);
32814ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka        updateButtonWithDrawable(R.id.voice_button, d);
3282bb185bdd57fcdb2c8ff9eee00347b116bab7db23Winson Chung        invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
32834ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
32844ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3285ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert    public void setVoiceButtonProxyVisible(boolean visible) {
3286ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert        final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3287ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert        if (voiceButtonProxy != null) {
3288ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert            voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3289ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert        }
3290ecf33e4819d0c4b019706edd456ed14a6a0f200cBjorn Bringert    }
32916ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    /**
3292eb66b14000712b92fa6a5ba28f5bc47ce8fab6bdWinson Chung     * Sets the app market icon
32936ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka     */
32946ae0fcfc80b55322824f87b2cc3c1f51ed83e5abMichael Jurka    private void updateAppMarketIcon() {
3295785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        final View marketButton = findViewById(R.id.market_button);
3296785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3297785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // Find the app market activity by resolving an intent.
3298785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        // (If multiple app markets are installed, it will return the ResolverActivity.)
3299785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        ComponentName activityName = intent.resolveActivity(getPackageManager());
33006a26e5bed48bf64996aef3ea1db4767ee147640eWinson Chung        if (activityName != null) {
3301dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            int coi = getCurrentOrientationIndexForGlobalIcons();
3302785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppMarketIntent = intent;
3303dff8ebbafd239e9912a51811edfb865bb16eea81Winson Chung            sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3304ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3305ae65ee30331bc2e9cc069687540692d7c62d0fefMichael Jurka                    TOOLBAR_ICON_METADATA_NAME);
3306785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setVisibility(View.VISIBLE);
3307785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        } else {
3308785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            // We should hide and disable the view so that we don't try and restore the visibility
3309785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            // of it when we swap between drag & normal states from IconDropTarget subclasses.
3310785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setVisibility(View.GONE);
3311785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            marketButton.setEnabled(false);
33120423dcf2863362407d9c1281b9b1d7e449082713Michael Jurka        }
3313ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    }
3314ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy
3315e7bf83b0a3250d73ef2c4c2dfa9d7fafb6847985Michael Jurka    private void updateAppMarketIcon(Drawable.ConstantState d) {
33161b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
33171b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        Resources r = getResources();
33181b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        Drawable marketIconDrawable = d.newDrawable(r);
33191b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
33201b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
33211b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        marketIconDrawable.setBounds(0, 0, w, h);
33221b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung
33231b8840936f90ed48d6c8edcd87fbc4466dc4e5baWinson Chung        updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
33244ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka    }
33254ef207ba1c200099d64a232c164b7ebfae48635eMichael Jurka
3326d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    @Override
3327d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
33281d4fde686433ddfa7f940e298d935433ecfed8ebalanv        final boolean result = super.dispatchPopulateAccessibilityEvent(event);
3329d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        final List<CharSequence> text = event.getText();
3330d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        text.clear();
33311d4fde686433ddfa7f940e298d935433ecfed8ebalanv        // Populate event with a fake title based on the current state.
33321d4fde686433ddfa7f940e298d935433ecfed8ebalanv        if (mState == State.APPS_CUSTOMIZE) {
33331d4fde686433ddfa7f940e298d935433ecfed8ebalanv            text.add(getString(R.string.all_apps_button_label));
33341d4fde686433ddfa7f940e298d935433ecfed8ebalanv        } else {
33351d4fde686433ddfa7f940e298d935433ecfed8ebalanv            text.add(getString(R.string.all_apps_home_button_label));
33361d4fde686433ddfa7f940e298d935433ecfed8ebalanv        }
3337d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka        return result;
3338d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka    }
3339d7c28056d3fc462f42b508026154d6d8fee5606bMichael Jurka
3340ceae05d573255a0143981888e5d257b5891e3925Patrick Dubroy    /**
3341b8472bb83cd96456a543137e1e56d589171934c3Winson Chung     * Receives notifications when system dialogs are to be closed.
33422ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato     */
33432ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
33442ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        @Override
33452ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        public void onReceive(Context context, Intent intent) {
33462ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato            closeSystemDialogs();
33472ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato        }
33482ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    }
33492ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato
33502ca0ae7d7c4d5f6b24a1b3d987813cad9ee4197fJoe Onorato    /**
33511e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     * Receives notifications whenever the appwidgets are reset.
33521e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey     */
33531e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    private class AppWidgetResetObserver extends ContentObserver {
33541e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        public AppWidgetResetObserver() {
33551e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey            super(new Handler());
33561e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        }
33571e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
33581e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        @Override
33591e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        public void onChange(boolean selfChange) {
33601e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey            onAppWidgetReset();
33611e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey        }
33621e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    }
33631e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey
33641e2efc8a6d355bb6c0fc75261eb819e250ddb8fbJeff Sharkey    /**
33657607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * If the activity is currently paused, signal that we need to run the passed Runnable
33667607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * in onResume.
33677607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     *
33687607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * This needs to be called from incoming places where resources might have been loaded
33697607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * while we are paused.  That is becaues the Configuration might be wrong
33707607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * when we're not running, and if it comes back to what it was when we
33717607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * were paused, we are not restarted.
33727607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     *
33737607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * Implementation of the method from LauncherModel.Callbacks.
33747607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     *
33757607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * @return true if we are currently paused.  The caller might be able to
33767607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     * skip some work in that case since we will come back again.
33777607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka     */
3378c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
33797607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (mPaused) {
33807607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            Log.i(TAG, "Deferring update until onResume");
3381c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            if (deletePreviousRunnables) {
3382c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                while (mOnResumeCallbacks.remove(run)) {
3383c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                }
3384c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            }
33857607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            mOnResumeCallbacks.add(run);
33867607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return true;
33877607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        } else {
33887607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return false;
33897607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
33907607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    }
33917607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka
3392c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    private boolean waitUntilResume(Runnable run) {
3393c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        return waitUntilResume(run, false);
3394c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    }
3395c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
33967607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    /**
3397ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * If the activity is currently paused, signal that we need to re-run the loader
3398ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * in onResume.
3399ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3400ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * This needs to be called from incoming places where resources might have been loaded
3401ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * while we are paused.  That is becaues the Configuration might be wrong
3402ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * when we're not running, and if it comes back to what it was when we
3403ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * were paused, we are not restarted.
3404ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3405ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
3406ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     *
3407ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * @return true if we are currently paused.  The caller might be able to
3408ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     * skip some work in that case since we will come back again.
3409ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato     */
3410ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    public boolean setLoadOnResume() {
3411ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        if (mPaused) {
3412ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            Log.i(TAG, "setLoadOnResume");
3413ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            mOnResumeNeedsLoad = true;
3414ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            return true;
3415ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        } else {
3416ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato            return false;
3417ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato        }
3418ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    }
3419ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3420ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato    /**
34219c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
342231dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
34239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public int getCurrentWorkspaceScreen() {
3424d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        if (mWorkspace != null) {
34250142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka            return mWorkspace.getCurrentPage();
3426d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        } else {
3427d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato            return SCREEN_COUNT / 2;
3428d0afc87d9639a228b247cfbc2cf19d16132137b3Joe Onorato        }
34299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
3430f96811cdf564469a7a654a0c876288c9fd14f35eThe Android Open Source Project
34319c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
34329c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Refreshes the shortcuts shown on the workspace.
34339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
34349c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
34359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
34369c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    public void startBinding() {
34377607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        // If we're starting binding all over again, clear any bind calls we'd postponed in
34387607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        // the past (see waitUntilResume) -- we don't need them since we're starting binding
34397607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        // from scratch again
34407607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        mOnResumeCallbacks.clear();
3441df0353815c629fc678824b07a234b89a1ff94208Adam Cohen
34427607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        final Workspace workspace = mWorkspace;
3443dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mNewShortcutAnimateScreenId = -1;
3444f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimateViews.clear();
3445df0353815c629fc678824b07a234b89a1ff94208Adam Cohen        mWorkspace.clearDropTargets();
34469c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        int count = workspace.getChildCount();
34479c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        for (int i = 0; i < count; i++) {
34483c2f7e105d8daab439bff2c362a727ad348d8a44Joe Onorato            // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
34497a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung            final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
34507a25a9e39a13df706daeb0a4a82273f3c5151997Winson Chung            layoutParent.removeAllViewsInLayout();
34519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        }
345205bf644e371c9d965ba864cf7dda6515c7cdce91Michael Jurka        mWidgetsToAdvance.clear();
34533d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung        if (mHotseat != null) {
34543d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung            mHotseat.resetLayout();
345531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
345631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
345731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3458dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    @Override
3459dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    public void bindScreens(ArrayList<Long> orderedScreenIds) {
346064359a53dc827797917a8d9a0697a91996789801Winson Chung        bindAddScreens(orderedScreenIds);
346164359a53dc827797917a8d9a0697a91996789801Winson Chung        mWorkspace.addExtraEmptyScreen();
346264359a53dc827797917a8d9a0697a91996789801Winson Chung    }
346364359a53dc827797917a8d9a0697a91996789801Winson Chung
346464359a53dc827797917a8d9a0697a91996789801Winson Chung    @Override
346564359a53dc827797917a8d9a0697a91996789801Winson Chung    public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
3466dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        int count = orderedScreenIds.size();
3467dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        for (int i = 0; i < count; i++) {
346864359a53dc827797917a8d9a0697a91996789801Winson Chung            mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
3469dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        }
3470dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen    }
3471dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
347239a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    private boolean shouldShowWeightWatcher() {
347339a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        String spKey = LauncherAppState.getSharedPreferencesKey();
347439a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
347539a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
347639a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
347739a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        return show;
347839a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    }
347939a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
348039a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    private void toggleShowWeightWatcher() {
348139a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        String spKey = LauncherAppState.getSharedPreferencesKey();
348239a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
348339a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
348439a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
348539a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        show = !show;
348639a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
348739a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        SharedPreferences.Editor editor = sp.edit();
348839a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
348939a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        editor.commit();
349039a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
349139a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        if (mWeightWatcher != null) {
349239a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen            mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
349339a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen        }
349439a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen    }
349539a06042556a90c98f55a92bda5811e7d9492d13Adam Cohen
349631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    /**
34979c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Bind the items start-end from the list.
34989c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
34999c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
350031dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project     */
350164359a53dc827797917a8d9a0697a91996789801Winson Chung    public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
350264359a53dc827797917a8d9a0697a91996789801Winson Chung                          final boolean forceAnimateIcons) {
35037607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (waitUntilResume(new Runnable() {
35047607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                public void run() {
350564359a53dc827797917a8d9a0697a91996789801Winson Chung                    bindItems(shortcuts, start, end, forceAnimateIcons);
35067607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                }
35077607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            })) {
35087607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return;
35097607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
3510ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3511f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Get the list of added shortcuts and intersect them with the set of shortcuts here
3512f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Set<String> newApps = new HashSet<String>();
3513f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3514f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
351564359a53dc827797917a8d9a0697a91996789801Winson Chung        final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
351664359a53dc827797917a8d9a0697a91996789801Winson Chung        final Collection<Animator> bounceAnims = new ArrayList<Animator>();
3517f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Workspace workspace = mWorkspace;
3518f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        for (int i = start; i < end; i++) {
35199c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            final ItemInfo item = shortcuts.get(i);
35204d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung
35214d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            // Short circuit if we are loading dock items for a configuration which has no dock
35224d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
35234d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung                    mHotseat == null) {
35244d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung                continue;
35254d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung            }
35264d279d94ade1c0d455404312b3c9cfde0078c547Winson Chung
35279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            switch (item.itemType) {
35289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
35299c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
3530f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    ShortcutInfo info = (ShortcutInfo) item;
3531f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    String uri = info.intent.toUri(0).toString();
3532f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    View shortcut = createShortcut(info);
3533dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen
353464359a53dc827797917a8d9a0697a91996789801Winson Chung                    /*
353564359a53dc827797917a8d9a0697a91996789801Winson Chung                     * TODO: FIX collision case
353664359a53dc827797917a8d9a0697a91996789801Winson Chung                     */
3537ce376639c8287747e462fd67a7d2d511ac80c344Winson Chung                    if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3538ce376639c8287747e462fd67a7d2d511ac80c344Winson Chung                        CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3539ce376639c8287747e462fd67a7d2d511ac80c344Winson Chung                        if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3540ce376639c8287747e462fd67a7d2d511ac80c344Winson Chung                            throw new RuntimeException("OCCUPIED");
3541ce376639c8287747e462fd67a7d2d511ac80c344Winson Chung                        }
354264359a53dc827797917a8d9a0697a91996789801Winson Chung                    }
354364359a53dc827797917a8d9a0697a91996789801Winson Chung
3544dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3545dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            item.cellY, 1, 1);
3546bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    boolean animateIconUp = false;
3547bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    synchronized (newApps) {
3548bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                        if (newApps.contains(uri)) {
3549bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                            animateIconUp = newApps.remove(uri);
3550bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                        }
3551bfeac0681b63091731c853fdafa3206642449a9dWinson Chung                    }
355264359a53dc827797917a8d9a0697a91996789801Winson Chung                    if (forceAnimateIcons) {
355364359a53dc827797917a8d9a0697a91996789801Winson Chung                        // Animate all the applications up now
355464359a53dc827797917a8d9a0697a91996789801Winson Chung                        shortcut.setAlpha(0f);
355564359a53dc827797917a8d9a0697a91996789801Winson Chung                        shortcut.setScaleX(0f);
355664359a53dc827797917a8d9a0697a91996789801Winson Chung                        shortcut.setScaleY(0f);
355764359a53dc827797917a8d9a0697a91996789801Winson Chung                        bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
355864359a53dc827797917a8d9a0697a91996789801Winson Chung                    } else if (animateIconUp) {
3559f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        // Prepare the view to be animated up
3560f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setAlpha(0f);
3561f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setScaleX(0f);
3562f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        shortcut.setScaleY(0f);
3563dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                        mNewShortcutAnimateScreenId = item.screenId;
3564f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        if (!mNewShortcutAnimateViews.contains(shortcut)) {
3565f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            mNewShortcutAnimateViews.add(shortcut);
3566f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                        }
3567f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                    }
35689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    break;
3569df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
35703d503fbd9468fb2b9fa645f4f7b91e11229edbfaWinson Chung                    FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
35710142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                            (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
3572df2cc41acbfacd576f99483a4af1cda32ebd3d09Adam Cohen                            (FolderInfo) item, mIconCache);
3573dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3574dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                            item.cellY, 1, 1);
35759c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                    break;
35769c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
357731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project        }
3578f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
357964359a53dc827797917a8d9a0697a91996789801Winson Chung        if (forceAnimateIcons) {
358064359a53dc827797917a8d9a0697a91996789801Winson Chung            // We post the animation slightly delayed to prevent slowdowns when we are loading
358164359a53dc827797917a8d9a0697a91996789801Winson Chung            // right after we return to launcher.
358264359a53dc827797917a8d9a0697a91996789801Winson Chung            mWorkspace.postDelayed(new Runnable() {
358364359a53dc827797917a8d9a0697a91996789801Winson Chung                public void run() {
358464359a53dc827797917a8d9a0697a91996789801Winson Chung                    anim.playTogether(bounceAnims);
358564359a53dc827797917a8d9a0697a91996789801Winson Chung                    anim.start();
358664359a53dc827797917a8d9a0697a91996789801Winson Chung                }
358764359a53dc827797917a8d9a0697a91996789801Winson Chung            }, NEW_APPS_ANIMATION_DELAY);
358864359a53dc827797917a8d9a0697a91996789801Winson Chung        }
35899c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        workspace.requestLayout();
35909c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
35919c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
35929c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
35939c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
35949c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
35957607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    public void bindFolders(final HashMap<Long, FolderInfo> folders) {
35967607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (waitUntilResume(new Runnable() {
35977607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                public void run() {
35987607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                    bindFolders(folders);
35997607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                }
36007607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            })) {
36017607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return;
36027607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
3603319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.clear();
3604319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        sFolders.putAll(folders);
360531dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
360631dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
36079c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
36089c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add the views for a widget to the workspace.
36099c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
36109c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
36119c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
36127607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    public void bindAppWidget(final LauncherAppWidgetInfo item) {
36137607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (waitUntilResume(new Runnable() {
36147607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                public void run() {
36157607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                    bindAppWidget(item);
36167607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                }
36177607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            })) {
36187607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return;
36197607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
3620ef2efcff5e70e5900f4f10f9e46c3fa17c03b0ecJoe Onorato
3621843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3622843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3623843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bindAppWidget: " + item);
3624843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
36259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final Workspace workspace = mWorkspace;
3626cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
36279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final int appWidgetId = item.appWidgetId;
36289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
3629843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3630843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3631843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
3632843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler
36339c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
363431dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
36359c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        item.hostView.setTag(item);
3636211bac3cd89b26bda4ff52df984731d11156dc38Winson Chung        item.onBindAppWidget(this);
363731dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project
3638dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
36399c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato                item.cellY, item.spanX, item.spanY, false);
3640ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen        addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3641ded9f8d8658d0b6601006c0a954cd3bf530e55c1Adam Cohen
36429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        workspace.requestLayout();
3643cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3644843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        if (DEBUG_WIDGETS) {
3645843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler            Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3646843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler                    + (SystemClock.uptimeMillis()-start) + "ms");
3647843e860e8ebab96ff70988f2829fac38afd9d937Daniel Sandler        }
36489c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
3649cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
36501462de39f01cec0ba785386532719cb0207dd827Adam Cohen    public void onPageBoundSynchronously(int page) {
36511462de39f01cec0ba785386532719cb0207dd827Adam Cohen        mSynchronouslyBoundPages.add(page);
36521462de39f01cec0ba785386532719cb0207dd827Adam Cohen    }
36531462de39f01cec0ba785386532719cb0207dd827Adam Cohen
36549c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
36559c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Callback saying that there aren't any more items to bind.
36569c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
36579c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
36589c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3659e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen    public void finishBindingItems(final boolean upgradePath) {
36607607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (waitUntilResume(new Runnable() {
36617607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                public void run() {
3662e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen                    finishBindingItems(upgradePath);
36637607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                }
36647607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            })) {
36657607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return;
36667607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
36679c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato        if (mSavedState != null) {
36689c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            if (!mWorkspace.hasFocus()) {
36690142d49e1378a7155bcca1fb59965d9e73016dbcMichael Jurka                mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
36709c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            }
36719c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato            mSavedState = null;
3672138a04170d964da9cdec228e95b976875ae52481Karl Rosaen        }
3673cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
36741462de39f01cec0ba785386532719cb0207dd827Adam Cohen        mWorkspace.restoreInstanceStateForRemainingPages();
3675cbb89e4fc253a5fc3f24162dfb4e29fc6a815c2bRomain Guy
3676002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        // If we received the result of any pending adds while the loader was running (e.g. the
3677002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        // widget configuration forced an orientation change), process them now.
3678002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        for (int i = 0; i < sPendingAddList.size(); i++) {
3679002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy            completeAdd(sPendingAddList.get(i));
3680002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        }
3681002cbf4cc0a0da6d8084dd5b3dee6e03ff2d496cPatrick Dubroy        sPendingAddList.clear();
368299c8758563f5e9e5970ec565bb80ee6441aa0e87Jeffrey Sharkey
3683c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // Update the market app icon as necessary (the other icons will be managed in response to
3684c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        // package changes in bindSearchablesChanged()
36856d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani        updateAppMarketIcon();
3686c1f5d26675e9777a55076d41404208e6160ca483Michael Jurka
3687f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Animate up any icons as necessary
3688f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        if (mVisible || mWorkspaceLoading) {
3689f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            Runnable newAppsRunnable = new Runnable() {
3690f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                @Override
3691f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                public void run() {
3692a2413751e3a698aef9c87411a639637883856939Winson Chung                    runNewAppsAnimation(false);
3693f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                }
3694f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            };
3695a2413751e3a698aef9c87411a639637883856939Winson Chung
3696dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen            boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3697dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
3698a2413751e3a698aef9c87411a639637883856939Winson Chung            if (canRunNewAppsAnimation()) {
3699a2413751e3a698aef9c87411a639637883856939Winson Chung                // If the user has not interacted recently, then either snap to the new page to show
3700a2413751e3a698aef9c87411a639637883856939Winson Chung                // the new-apps animation or just run them if they are to appear on the current page
3701a2413751e3a698aef9c87411a639637883856939Winson Chung                if (willSnapPage) {
3702dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen                    mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
3703a2413751e3a698aef9c87411a639637883856939Winson Chung                } else {
3704a2413751e3a698aef9c87411a639637883856939Winson Chung                    runNewAppsAnimation(false);
3705a2413751e3a698aef9c87411a639637883856939Winson Chung                }
3706f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            } else {
3707318eee06ae69ac5ef8fddde984eb2f7601d2101dWinson Chung                // If the user has interacted recently, then just add the items in place if they
3708a2413751e3a698aef9c87411a639637883856939Winson Chung                // are on another page (or just normally if they are added to the current page)
3709a2413751e3a698aef9c87411a639637883856939Winson Chung                runNewAppsAnimation(willSnapPage);
3710f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3711f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }
3712f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3713f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mWorkspaceLoading = false;
3714e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        if (upgradePath) {
3715e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen            mWorkspace.saveWorkspaceToDb();
3716a0b5749433c66b6fed5c6026414ef438a938089bAdam Cohen            mWorkspace.stripDuplicateApps();
3717a0b5749433c66b6fed5c6026414ef438a938089bAdam Cohen            mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
3718e25af795647a19f1a0d60bc4baea5a996d215fdfAdam Cohen        }
371999894d9bb73fdc4bf9bba8423b5c5d5715a4f5d5Adam Cohen
372066a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen        mWorkspace.post(new Runnable() {
372166a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen            @Override
372266a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen            public void run() {
372366a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen                onFinishBindingItems();
372466a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen            }
372566a01fd24bde7c94ea892b007df7f79fbaa43d80Adam Cohen        });
3726f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    }
3727f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3728a2413751e3a698aef9c87411a639637883856939Winson Chung    private boolean canRunNewAppsAnimation() {
3729a2413751e3a698aef9c87411a639637883856939Winson Chung        long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3730a2413751e3a698aef9c87411a639637883856939Winson Chung        return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3731a2413751e3a698aef9c87411a639637883856939Winson Chung    }
3732a2413751e3a698aef9c87411a639637883856939Winson Chung
3733c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3734c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3735c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                PropertyValuesHolder.ofFloat("alpha", 1f),
3736c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                PropertyValuesHolder.ofFloat("scaleX", 1f),
3737c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                PropertyValuesHolder.ofFloat("scaleY", 1f));
3738c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3739c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3740c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3741c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung        return bounceAnim;
3742c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung    }
3743c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung
3744f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung    /**
3745f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     * Runs a new animation that scales up icons that were added while Launcher was in the
3746f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     * background.
3747a2413751e3a698aef9c87411a639637883856939Winson Chung     *
3748a2413751e3a698aef9c87411a639637883856939Winson Chung     * @param immediate whether to run the animation or show the results immediately
3749f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung     */
3750a2413751e3a698aef9c87411a639637883856939Winson Chung    private void runNewAppsAnimation(boolean immediate) {
37512ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka        AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3752f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Collection<Animator> bounceAnims = new ArrayList<Animator>();
3753a2413751e3a698aef9c87411a639637883856939Winson Chung
3754a2413751e3a698aef9c87411a639637883856939Winson Chung        // Order these new views spatially so that they animate in order
3755f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3756f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            @Override
3757f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            public int compare(View a, View b) {
3758f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3759f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3760f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                int cellCountX = LauncherModel.getCellCountX();
3761f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3762f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3763f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        });
3764a2413751e3a698aef9c87411a639637883856939Winson Chung
3765a2413751e3a698aef9c87411a639637883856939Winson Chung        // Animate each of the views in place (or show them immediately if requested)
3766a2413751e3a698aef9c87411a639637883856939Winson Chung        if (immediate) {
3767a2413751e3a698aef9c87411a639637883856939Winson Chung            for (View v : mNewShortcutAnimateViews) {
3768a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setAlpha(1f);
3769a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setScaleX(1f);
3770a2413751e3a698aef9c87411a639637883856939Winson Chung                v.setScaleY(1f);
3771f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3772a2413751e3a698aef9c87411a639637883856939Winson Chung        } else {
3773a2413751e3a698aef9c87411a639637883856939Winson Chung            for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3774a2413751e3a698aef9c87411a639637883856939Winson Chung                View v = mNewShortcutAnimateViews.get(i);
3775c916834ad0da535f08325bcd902a60070f2f7e53Winson Chung                bounceAnims.add(createNewAppBounceAnimation(v, i));
3776a2413751e3a698aef9c87411a639637883856939Winson Chung            }
3777a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.playTogether(bounceAnims);
3778a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.addListener(new AnimatorListenerAdapter() {
3779a2413751e3a698aef9c87411a639637883856939Winson Chung                @Override
3780a2413751e3a698aef9c87411a639637883856939Winson Chung                public void onAnimationEnd(Animator animation) {
3781d6dd7c8a21e416d7c118517569a242ac7de6c696Michael Jurka                    if (mWorkspace != null) {
3782d6dd7c8a21e416d7c118517569a242ac7de6c696Michael Jurka                        mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3783d6dd7c8a21e416d7c118517569a242ac7de6c696Michael Jurka                    }
3784a2413751e3a698aef9c87411a639637883856939Winson Chung                }
3785a2413751e3a698aef9c87411a639637883856939Winson Chung            });
3786a2413751e3a698aef9c87411a639637883856939Winson Chung            anim.start();
3787a2413751e3a698aef9c87411a639637883856939Winson Chung        }
3788f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung
3789f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        // Clean up
3790dcd297f05a866e07090d6e2af8fb4b15f28cb555Adam Cohen        mNewShortcutAnimateScreenId = -1;
3791f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        mNewShortcutAnimateViews.clear();
3792f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        new Thread("clearNewAppsThread") {
3793f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            public void run() {
3794f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                mSharedPrefs.edit()
3795f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3796f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3797f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung                            .commit();
3798f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung            }
3799f0c6ae0e35d3e020db55b5b826955da66b14b7f6Winson Chung        }.start();
38006d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    }
38016d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani
3802cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    @Override
3803cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    public void bindSearchablesChanged() {
3804c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean searchVisible = updateGlobalSearchIcon();
3805c51db6accf2fdcb15d6361a38b176534e3d2cca5Winson Chung        boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3806adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        if (mSearchDropTargetBar != null) {
3807adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung            mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3808adf0c180c1c0899f0a91ec91bcdc2002f90a56a5Winson Chung        }
3809cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath    }
3810cb1a4778686a46c46d8dc88b6c83674f6fac6592Narayan Kamath
38116d7fe506fcfbc7bd6810ec8dd48c214e856aa87aAmith Yamasani    /**
38129c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Add the icons for all apps.
38139c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
38149c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
38159c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
3816c57b7a8233d32c6dd8de5057c570afe3f50e3ef2Michael Jurka    public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
381764359a53dc827797917a8d9a0697a91996789801Winson Chung        if (mIntentsOnWorkspaceFromUpgradePath != null) {
381864359a53dc827797917a8d9a0697a91996789801Winson Chung            getHotseat().addAllAppsFolder(mIconCache, apps,
3819156ab5b22e45b36a1c5edbe5accccf6aefcb4907Winson Chung                    mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
382064359a53dc827797917a8d9a0697a91996789801Winson Chung            mIntentsOnWorkspaceFromUpgradePath = null;
3821785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
38229c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
38239c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
38249c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
38259c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * A package was updated.
38269c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
38279c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
38289c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
38297607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka    public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
38307607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        if (waitUntilResume(new Runnable() {
38317607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                public void run() {
38327607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                    bindAppsUpdated(apps);
38337607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka                }
38347607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            })) {
38357607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka            return;
38367607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka        }
38377607c2fb2729c7743022ff3fde9cfc75ae6f504fMichael Jurka
3838f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy        if (mWorkspace != null) {
3839f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy            mWorkspace.updateShortcuts(apps);
3840f5afda70648505aadde6fbd64b06c9724a33460cPatrick Dubroy        }
38419c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    }
38429c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato
38439c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato    /**
384483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung     * A package was uninstalled.  We take both the super set of packageNames
384583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung     * in addition to specific applications to remove, the reason being that
384683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung     * this can be called when a package is updated as well.  In that scenario,
384783892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung     * we only remove specific components from the workspace, where as
384883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung     * package-removal should clear all items by package name.
38499c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     *
38509c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     * Implementation of the method from LauncherModel.Callbacks.
38519c1289cb3bfb74f86e53ec7ac6dd76bb39666b2dJoe Onorato     */
385283892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung    public void bindComponentsRemoved(final ArrayList<String> packageNames,
385383892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung                                      final ArrayList<ApplicationInfo> appInfos,
385464359a53dc827797917a8d9a0697a91996789801Winson Chung                                      final boolean packageRemoved) {
385583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        if (waitUntilResume(new Runnable() {
385683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            public void run() {
385764359a53dc827797917a8d9a0697a91996789801Winson Chung                bindComponentsRemoved(packageNames, appInfos, packageRemoved);
385883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            }
385983892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        })) {
386083892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            return;
386183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        }
386283892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
386364359a53dc827797917a8d9a0697a91996789801Winson Chung        if (packageRemoved) {
386483892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            mWorkspace.removeItemsByPackageName(packageNames);
386583892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        } else {
386683892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            mWorkspace.removeItemsByApplicationInfo(appInfos);
38673611578579b4bfb25616085dafdb1a45207394f9Joe Onorato        }
3868f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
3869a182096cb20e0580dfca6661b2e5871c8886c834Winson Chung        // Notify the drag controller
387083892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        mDragController.onAppsRemoved(appInfos, this);
387131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project    }
3872be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato
3873be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    /**
387480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung     * A number of packages were updated.
387580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung     */
3876c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    private ArrayList<Object> mWidgetsAndShortcuts;
3877c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
387883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            public void run() {
3879c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                bindPackagesUpdated(mWidgetsAndShortcuts);
3880c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka                mWidgetsAndShortcuts = null;
388183892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            }
3882c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        };
3883c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka
3884c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka    public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3885c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka        if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3886c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            mWidgetsAndShortcuts = widgetsAndShortcuts;
388783892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung            return;
388883892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung        }
388983892cc0768bd6d7f0827886e9e7a2eb3eed2b01Winson Chung
389064359a53dc827797917a8d9a0697a91996789801Winson Chung        // Update the widgets pane
3891785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
3892c402cd9992d431b6beacdf3c75e31cf103e230cbMichael Jurka            mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
3893785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
389480baf5a6b3c62a62265f626d43d1167783c94131Winson Chung    }
389580baf5a6b3c62a62265f626d43d1167783c94131Winson Chung
3896400438b79fe412cb625c96297edeea9c6155349eWinson Chung    private int mapConfigurationOriActivityInfoOri(int configOri) {
3897400438b79fe412cb625c96297edeea9c6155349eWinson Chung        final Display d = getWindowManager().getDefaultDisplay();
3898400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3899400438b79fe412cb625c96297edeea9c6155349eWinson Chung        switch (d.getRotation()) {
3900400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_0:
3901400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_180:
3902400438b79fe412cb625c96297edeea9c6155349eWinson Chung            // We are currently in the same basic orientation as the natural orientation
3903400438b79fe412cb625c96297edeea9c6155349eWinson Chung            naturalOri = configOri;
3904400438b79fe412cb625c96297edeea9c6155349eWinson Chung            break;
3905400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_90:
3906400438b79fe412cb625c96297edeea9c6155349eWinson Chung        case Surface.ROTATION_270:
3907400438b79fe412cb625c96297edeea9c6155349eWinson Chung            // We are currently in the other basic orientation to the natural orientation
3908400438b79fe412cb625c96297edeea9c6155349eWinson Chung            naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3909400438b79fe412cb625c96297edeea9c6155349eWinson Chung                    Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3910400438b79fe412cb625c96297edeea9c6155349eWinson Chung            break;
3911400438b79fe412cb625c96297edeea9c6155349eWinson Chung        }
3912400438b79fe412cb625c96297edeea9c6155349eWinson Chung
3913400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int[] oriMap = {
3914400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3915400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3916400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3917400438b79fe412cb625c96297edeea9c6155349eWinson Chung                ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3918400438b79fe412cb625c96297edeea9c6155349eWinson Chung        };
3919400438b79fe412cb625c96297edeea9c6155349eWinson Chung        // Since the map starts at portrait, we need to offset if this device's natural orientation
3920400438b79fe412cb625c96297edeea9c6155349eWinson Chung        // is landscape.
3921400438b79fe412cb625c96297edeea9c6155349eWinson Chung        int indexOffset = 0;
3922400438b79fe412cb625c96297edeea9c6155349eWinson Chung        if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3923400438b79fe412cb625c96297edeea9c6155349eWinson Chung            indexOffset = 1;
3924400438b79fe412cb625c96297edeea9c6155349eWinson Chung        }
3925400438b79fe412cb625c96297edeea9c6155349eWinson Chung        return oriMap[(d.getRotation() + indexOffset) % 4];
3926400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
3927446e940fc8839173de12f1299b8c24f318d53a3fAdam Cohen
39284b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    public boolean isRotationEnabled() {
39290a457bfbd9ca6562e8fcd604bcdfeb22f1fb4dedMichael Jurka        boolean enableRotation = sForceEnableRotation ||
39304b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                getResources().getBoolean(R.bool.allow_rotation);
39314b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        return enableRotation;
39324b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    }
3933641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung    public void lockScreenOrientation() {
39344b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        if (isRotationEnabled()) {
39354b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
39364b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    .getConfiguration().orientation));
39374b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        }
3938400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
39394b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung    public void unlockScreenOrientation(boolean immediate) {
39404b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        if (isRotationEnabled()) {
39414b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            if (immediate) {
3942641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
39434b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung            } else {
39444b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                mHandler.postDelayed(new Runnable() {
39454b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    public void run() {
39464b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
39474b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                    }
39484b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung                }, mRestoreScreenOrientationDelay);
3949641d71d6da6c77cc32e5d0f65ccae89caed1ac34Winson Chung            }
39504b919f813f4e5e0323a4c1747392551fce4a8758Winson Chung        }
3951400438b79fe412cb625c96297edeea9c6155349eWinson Chung    }
3952400438b79fe412cb625c96297edeea9c6155349eWinson Chung
395382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    /* Cling related */
39547d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private boolean isClingsEnabled() {
39557564d98164c9fcba178e0ab0faf5066e33ad9a31Adam Cohen        if (DISABLE_CLINGS) {
39567564d98164c9fcba178e0ab0faf5066e33ad9a31Adam Cohen            return false;
39577564d98164c9fcba178e0ab0faf5066e33ad9a31Adam Cohen        }
39587564d98164c9fcba178e0ab0faf5066e33ad9a31Adam Cohen
39592a9e228e6fd719732639beb880d6df64d48158a1Brett Chabot        // disable clings when running in a test harness
39607d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        if(ActivityManager.isRunningInTestHarness()) return false;
39612a9e228e6fd719732639beb880d6df64d48158a1Brett Chabot
3962e233a8bf79d624c5419805fde4bb0e025c66b020Michael Jurka        // Restricted secondary users (child mode) will potentially have very few apps
3963e233a8bf79d624c5419805fde4bb0e025c66b020Michael Jurka        // seeded when they start up for the first time. Clings won't work well with that
3964325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//        boolean supportsLimitedUsers =
3965325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//                android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3966325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//        Account[] accounts = AccountManager.get(this).getAccounts();
3967325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//        if (supportsLimitedUsers && accounts.length == 0) {
3968325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//            UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3969325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//            Bundle restrictions = um.getUserRestrictions();
3970325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//            if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3971325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//               return false;
3972325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//            }
3973325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler//        }
39747d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        return true;
39757d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
397622143131770f7e90be4181e88ced0cb1d7158306Michael Jurka
39777d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
397822143131770f7e90be4181e88ced0cb1d7158306Michael Jurka        final Cling cling = (Cling) findViewById(clingId);
39797d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        if (cling != null) {
39807d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            cling.init(this, positionData);
398182f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            cling.setVisibility(View.VISIBLE);
39827d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
39837d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            if (animate) {
39847d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.buildLayer();
39857d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.setAlpha(0f);
39867d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.animate()
39877d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .alpha(1f)
39887a74ac9cd68db5e8bba87bbda43f73cea9c427c8Winson Chung                    .setInterpolator(new AccelerateInterpolator())
39897d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .setDuration(SHOW_CLING_DURATION)
39907d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .setStartDelay(delay)
39917d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                    .start();
39927d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            } else {
39937d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung                cling.setAlpha(1f);
39947d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            }
399522143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            cling.setFocusableInTouchMode(true);
399622143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            cling.post(new Runnable() {
399722143131770f7e90be4181e88ced0cb1d7158306Michael Jurka                public void run() {
399822143131770f7e90be4181e88ced0cb1d7158306Michael Jurka                    cling.setFocusable(true);
399922143131770f7e90be4181e88ced0cb1d7158306Michael Jurka                    cling.requestFocus();
400022143131770f7e90be4181e88ced0cb1d7158306Michael Jurka                }
400122143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            });
400222143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
400322143131770f7e90be4181e88ced0cb1d7158306Michael Jurka                    mDragLayer, clingId == R.id.all_apps_cling);
400482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        }
40057d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        return cling;
400682f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
400722143131770f7e90be4181e88ced0cb1d7158306Michael Jurka
40087d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    private void dismissCling(final Cling cling, final String flag, int duration) {
40097819abd2d191a170cbc9d4bd190001c465187a9dWinson Chung        // To catch cases where siblings of top-level views are made invisible, just check whether
40107819abd2d191a170cbc9d4bd190001c465187a9dWinson Chung        // the cling is directly set to GONE before dismissing it.
40117819abd2d191a170cbc9d4bd190001c465187a9dWinson Chung        if (cling != null && cling.getVisibility() != View.GONE) {
40122ecf995e0d2d55eb71d03f7230ca87270872d1a3Michael Jurka            ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
40137d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            anim.setDuration(duration);
401482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            anim.addListener(new AnimatorListenerAdapter() {
401582f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                public void onAnimationEnd(Animator animation) {
401682f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                    cling.setVisibility(View.GONE);
401782f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                    cling.cleanup();
401846353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    // We should update the shared preferences on a background thread
401946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    new Thread("dismissClingThread") {
402046353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                        public void run() {
402146353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            SharedPreferences.Editor editor = mSharedPrefs.edit();
402246353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            editor.putBoolean(flag, true);
402346353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                            editor.commit();
402446353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                        }
402546353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                    }.start();
402682f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung                };
402782f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            });
402882f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung            anim.start();
402922143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
403082f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        }
403182f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
403222143131770f7e90be4181e88ced0cb1d7158306Michael Jurka
40339d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung    private void removeCling(int id) {
40349d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        final View cling = findViewById(id);
40359d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        if (cling != null) {
40369d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            final ViewGroup parent = (ViewGroup) cling.getParent();
40379d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            parent.post(new Runnable() {
40389d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                @Override
40399d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                public void run() {
40409d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                    parent.removeView(cling);
40419d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung                }
40429d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            });
404322143131770f7e90be4181e88ced0cb1d7158306Michael Jurka            mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
40449d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        }
40459d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung    }
4046974c3864bb438fc924913e0017385f873332d052Michael Jurka
4047974c3864bb438fc924913e0017385f873332d052Michael Jurka    private boolean skipCustomClingIfNoAccounts() {
4048974c3864bb438fc924913e0017385f873332d052Michael Jurka        Cling cling = (Cling) findViewById(R.id.workspace_cling);
4049974c3864bb438fc924913e0017385f873332d052Michael Jurka        boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4050974c3864bb438fc924913e0017385f873332d052Michael Jurka        if (customCling) {
4051974c3864bb438fc924913e0017385f873332d052Michael Jurka            AccountManager am = AccountManager.get(this);
4052325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler            if (am == null) return false;
4053974c3864bb438fc924913e0017385f873332d052Michael Jurka            Account[] accounts = am.getAccountsByType("com.google");
4054974c3864bb438fc924913e0017385f873332d052Michael Jurka            return accounts.length == 0;
4055974c3864bb438fc924913e0017385f873332d052Michael Jurka        }
4056974c3864bb438fc924913e0017385f873332d052Michael Jurka        return false;
4057974c3864bb438fc924913e0017385f873332d052Michael Jurka    }
4058974c3864bb438fc924913e0017385f873332d052Michael Jurka
40597d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void showFirstRunWorkspaceCling() {
40607d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
406146353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
4062974c3864bb438fc924913e0017385f873332d052Michael Jurka                !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4063974c3864bb438fc924913e0017385f873332d052Michael Jurka                !skipCustomClingIfNoAccounts() ) {
406445355c4596f396d7e7247e91de850646356bd104Michael Jurka            // If we're not using the default workspace layout, replace workspace cling
406545355c4596f396d7e7247e91de850646356bd104Michael Jurka            // with a custom workspace cling (usually specified in an overlay)
406645355c4596f396d7e7247e91de850646356bd104Michael Jurka            // For now, only do this on tablets
406745355c4596f396d7e7247e91de850646356bd104Michael Jurka            if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
4068a113121fd3c43f1e1d23064e17c540c48fe330ccMichael Jurka                    getResources().getBoolean(R.bool.config_useCustomClings)) {
406945355c4596f396d7e7247e91de850646356bd104Michael Jurka                // Use a custom cling
407045355c4596f396d7e7247e91de850646356bd104Michael Jurka                View cling = findViewById(R.id.workspace_cling);
407145355c4596f396d7e7247e91de850646356bd104Michael Jurka                ViewGroup clingParent = (ViewGroup) cling.getParent();
407245355c4596f396d7e7247e91de850646356bd104Michael Jurka                int clingIndex = clingParent.indexOfChild(cling);
407345355c4596f396d7e7247e91de850646356bd104Michael Jurka                clingParent.removeViewAt(clingIndex);
407445355c4596f396d7e7247e91de850646356bd104Michael Jurka                View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
407545355c4596f396d7e7247e91de850646356bd104Michael Jurka                clingParent.addView(customCling, clingIndex);
407645355c4596f396d7e7247e91de850646356bd104Michael Jurka                customCling.setId(R.id.workspace_cling);
407745355c4596f396d7e7247e91de850646356bd104Michael Jurka            }
40787d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            initCling(R.id.workspace_cling, null, false, 0);
40799d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
40809d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.workspace_cling);
40817d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
40827d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
40837d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void showFirstRunAllAppsCling(int[] position) {
40847d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
408546353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
408646353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
40877d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung            initCling(R.id.all_apps_cling, position, true, 0);
40889d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
40899d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.all_apps_cling);
40907d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
40917d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
40927d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public Cling showFirstRunFoldersCling() {
40937d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        // Enable the clings only if they have not been dismissed before
409446353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung        if (isClingsEnabled() &&
409546353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung                !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
409646353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung            return initCling(R.id.folder_cling, null, true, 0);
40979d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        } else {
40989d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            removeCling(R.id.folder_cling);
409946353de2ab5aa9db30af1b055ebb6faaf47bd2e9Winson Chung            return null;
41007d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        }
41017d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
41027d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public boolean isFolderClingVisible() {
41037d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        Cling cling = (Cling) findViewById(R.id.folder_cling);
41049d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        if (cling != null) {
41059d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung            return cling.getVisibility() == View.VISIBLE;
41069d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        }
41079d9d74f2b0927b9b28ac70d453c046d48788b57bWinson Chung        return false;
41087d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
410982f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    public void dismissWorkspaceCling(View v) {
411082f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        Cling cling = (Cling) findViewById(R.id.workspace_cling);
41117d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
411282f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
411382f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    public void dismissAllAppsCling(View v) {
411482f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung        Cling cling = (Cling) findViewById(R.id.all_apps_cling);
41157d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
41167d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    }
41177d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung    public void dismissFolderCling(View v) {
41187d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        Cling cling = (Cling) findViewById(R.id.folder_cling);
41197d7541e7b48fdc114c24b3b0aa75e70d7228041eWinson Chung        dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
412082f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung    }
412182f5553a137f0a25f7333a6fc9507c847b7a42edWinson Chung
412280baf5a6b3c62a62265f626d43d1167783c94131Winson Chung    /**
4123be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato     * Prints out out state for debugging.
4124be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato     */
4125be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    public void dumpState() {
4126325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler        Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
412739bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mSavedState=" + mSavedState);
412839bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
412939bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mRestoring=" + mRestoring);
413039bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
413139bfc13a77b25aa9e1dc322b223030545c9af2c1Joe Onorato        Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
4132319226a894f5f0b94ca94e64f976a043e3873a40Brad Fitzpatrick        Log.d(TAG, "sFolders.size=" + sFolders.size());
4133be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato        mModel.dumpState();
4134f0ea4d3378be7b962c8e0bce2392df5e82491fb8Winson Chung
4135785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        if (mAppsCustomizeContent != null) {
4136785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung            mAppsCustomizeContent.dumpState();
4137785d2eb2b8d7072c8124300dd9168ff51a91cf38Winson Chung        }
4138325dc23624160689e59fbac708cf6f222b20d025Daniel Sandler        Log.d(TAG, "END launcher3 dump state");
4139be38609f875f9b9374ceaf723135c0a624637fcbJoe Onorato    }
414016d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen
414116d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    @Override
414216d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
414316d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        super.dump(prefix, fd, writer, args);
414416d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        writer.println(" ");
414516d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        writer.println("Debug logs: ");
414616d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        for (int i = 0; i < sDumpLogs.size(); i++) {
414716d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen            writer.println("  " + sDumpLogs.get(i));
414816d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen        }
414916d7ffc5d55cc69ddd5a7854f138b2d8cc0a7635Adam Cohen    }
4150487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen
4151487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    public static void dumpDebugLogsToConsole() {
4152487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, "");
4153487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, "*********************");
4154487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, "Launcher debug logs: ");
4155487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        for (int i = 0; i < sDumpLogs.size(); i++) {
4156487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen            Log.d(TAG, "  " + sDumpLogs.get(i));
4157487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        }
4158487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, "*********************");
4159487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen        Log.d(TAG, "");
4160487f7dd3059621527eb439d7d51d34e00293f9b1Adam Cohen    }
416131dd503c6aa69018e694d91724d46db49ea09327The Android Open Source Project}
41622763be3150d429d338b9848286e9365b4052c100Michael Jurka
4163abded66084680bb31cc7ea403c88f44f79a3c884Michael Jurkainterface LauncherTransitionable {
41642a4b1a809c6bc0af9ae3ad974fe50e2a91569543Michael Jurka    View getContent();
4165a35e35ae0ac2391b8928eb15bf91274aea96b115Michael Jurka    void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
4166bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
41677044272f180d34ab29866f6c59ee75376e8a1df5Winson Chung    void onLauncherTransitionStep(Launcher l, float t);
4168bed61d22bdbfcdbe04f92675c7751b83c7a8947cMichael Jurka    void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
41692763be3150d429d338b9848286e9365b4052c100Michael Jurka}
4170