ManageApplications.java revision 5203bdffe6ce2b829d0fab8c7691fa92aca6e972
1afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/*
2afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Copyright (C) 2006 The Android Open Source Project
3afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
4afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * you may not use this file except in compliance with the License.
6afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * You may obtain a copy of the License at
7afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
8afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project *
10afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * See the License for the specific language governing permissions and
14afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * limitations under the License.
15afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
16afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
17728ac35373028b79205a788c526ef4b495518ec1Dianne Hackbornpackage com.android.settings.applications;
18afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
19afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport com.android.settings.R;
205203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasani
215203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasaniimport com.android.internal.content.PackageHelper;
22e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackbornimport com.android.settings.applications.ApplicationsState.AppEntry;
235203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasaniimport com.android.settings.Settings.RunningServicesActivity;
245203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasaniimport com.android.settings.Settings.StorageUseActivity;
2538b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu
26f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackbornimport android.app.Fragment;
27afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Context;
28afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.content.Intent;
2919df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackbornimport android.content.pm.ApplicationInfo;
306dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornimport android.content.pm.IPackageManager;
316dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornimport android.content.pm.PackageInfo;
32afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.os.Bundle;
331337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackbornimport android.os.Environment;
346dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornimport android.os.RemoteException;
356dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornimport android.os.ServiceManager;
361337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackbornimport android.os.StatFs;
37f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackbornimport android.preference.PreferenceActivity;
38728ac35373028b79205a788c526ef4b495518ec1Dianne Hackbornimport android.provider.Settings;
391337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackbornimport android.text.format.Formatter;
40afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.util.Log;
41afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.LayoutInflater;
42afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.Menu;
43f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackbornimport android.view.MenuInflater;
44afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.MenuItem;
45afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.View;
46afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.view.ViewGroup;
47ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackbornimport android.view.animation.AnimationUtils;
48d2be882d8f2e5acd8a5806c649ba4640249cf4baDianne Hackbornimport android.view.inputmethod.InputMethodManager;
49e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackbornimport android.widget.AbsListView;
50afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.AdapterView;
51afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.BaseAdapter;
526dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornimport android.widget.CheckBox;
53c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapuimport android.widget.Filter;
54c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapuimport android.widget.Filterable;
55afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.ImageView;
56afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.ListView;
5738b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapuimport android.widget.TabHost;
58afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.TextView;
59afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport android.widget.AdapterView.OnItemClickListener;
60b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
61afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport java.util.ArrayList;
62afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Projectimport java.util.Comparator;
63afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
646dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackbornfinal class CanBeOnSdCardChecker {
656dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    final IPackageManager mPm;
666dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    int mInstallLocation;
676dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn
686dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    CanBeOnSdCardChecker() {
696dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        mPm = IPackageManager.Stub.asInterface(
706dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                ServiceManager.getService("package"));
716dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    }
726dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn
736dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    void init() {
746dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        try {
756dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            mInstallLocation = mPm.getInstallLocation();
766dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        } catch (RemoteException e) {
776dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            Log.e("CanBeOnSdCardChecker", "Is Package Manager running?");
786dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            return;
796dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        }
806dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    }
816dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn
826dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    boolean check(ApplicationInfo info) {
836dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        boolean canBe = false;
846dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        if ((info.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
856dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            canBe = true;
866dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        } else {
876dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            if ((info.flags & ApplicationInfo.FLAG_FORWARD_LOCK) == 0 &&
886dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    (info.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
896dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                if (info.installLocation == PackageInfo.INSTALL_LOCATION_PREFER_EXTERNAL ||
906dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                        info.installLocation == PackageInfo.INSTALL_LOCATION_AUTO) {
916dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    canBe = true;
926dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                } else if (info.installLocation
936dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                        == PackageInfo.INSTALL_LOCATION_UNSPECIFIED) {
946dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    if (mInstallLocation == PackageHelper.APP_INSTALL_EXTERNAL) {
956dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                        // For apps with no preference and the default value set
966dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                        // to install on sdcard.
976dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                        canBe = true;
986dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    }
996dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                }
1006dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn            }
1016dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        }
1026dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        return canBe;
1036dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn    }
1046dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn}
1056dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn
106afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project/**
107afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * Activity to pick an application that will be used to display installation information and
108afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * options to uninstall/delete user data for system applications. This activity
109afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * can be launched through Settings or via the ACTION_MANAGE_PACKAGE_STORAGE
110afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project * intent.
111afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project */
112f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackbornpublic class ManageApplications extends Fragment implements
113f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        OnItemClickListener,
114e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        TabHost.TabContentFactory, TabHost.OnTabChangeListener {
115e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static final String TAG = "ManageApplications";
116e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static final boolean DEBUG = false;
117afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
118afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // attributes used as keys when passing values to InstalledAppDetails activity
11938b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu    public static final String APP_CHG = "chg";
120afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
121afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // constant value that can be used to check return code from sub activity.
122afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int INSTALLED_APP_DETAILS = 1;
123d2be882d8f2e5acd8a5806c649ba4640249cf4baDianne Hackborn
124afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // sort order that can be changed through the menu can be sorted alphabetically
125afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // or size(descending)
126afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private static final int MENU_OPTIONS_BASE = 0;
127afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // Filter options used for displayed list of applications
1286cf44dbe90e3215715c713874c357d0bd26b8102Suchi Amalapurapu    public static final int FILTER_APPS_ALL = MENU_OPTIONS_BASE + 0;
129728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    public static final int FILTER_APPS_THIRD_PARTY = MENU_OPTIONS_BASE + 1;
130728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    public static final int FILTER_APPS_SDCARD = MENU_OPTIONS_BASE + 2;
131747ea55fce48c23b51fe6f6df8c4236effd91e37Suchi Amalapurapu
1326cf44dbe90e3215715c713874c357d0bd26b8102Suchi Amalapurapu    public static final int SORT_ORDER_ALPHA = MENU_OPTIONS_BASE + 4;
1336cf44dbe90e3215715c713874c357d0bd26b8102Suchi Amalapurapu    public static final int SORT_ORDER_SIZE = MENU_OPTIONS_BASE + 5;
1341337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    public static final int SHOW_RUNNING_SERVICES = MENU_OPTIONS_BASE + 6;
1351337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    public static final int SHOW_BACKGROUND_PROCESSES = MENU_OPTIONS_BASE + 7;
136afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // sort order
137afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private int mSortOrder = SORT_ORDER_ALPHA;
138afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // Filter value
139fe4a2bc43f45b14db62c67d7c2531a3d7f1b8590Suchi Amalapurapu    private int mFilterApps = FILTER_APPS_THIRD_PARTY;
140afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
141e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    private ApplicationsState mApplicationsState;
142e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    private ApplicationsAdapter mApplicationsAdapter;
143afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
144afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // Size resource used for packages whose size computation failed for some reason
145afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private CharSequence mInvalidSizeStr;
146afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private CharSequence mComputingSizeStr;
147afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
148afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // layout inflater object used to inflate views
149afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private LayoutInflater mInflater;
150afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
151de03ac27b74d1d69c1fea36e7fb00f068ab16b21Suchi Amalapurapu    private String mCurrentPkgName;
152afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
153ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn    private View mLoadingContainer;
154ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn
155508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn    private View mListContainer;
156508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn
157afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // ListView used to display list
158afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private ListView mListView;
159728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    // Custom view used to display running processes
160728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    private RunningProcessesView mRunningProcessesView;
161afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
1621337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    LinearColorBar mColorBar;
1631337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    TextView mStorageChartLabel;
1641337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    TextView mUsedStorageText;
1651337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    TextView mFreeStorageText;
1661337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
167ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    private Menu mOptionsMenu;
168ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn
169728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    // These are for keeping track of activity and tab switch state.
170728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    private int mCurView;
171728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    private boolean mCreatedRunning;
172728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
173728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    private boolean mResumedRunning;
174728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    private boolean mActivityResumed;
175728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
1761337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    private StatFs mDataFileStats;
1771337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    private StatFs mSDCardFileStats;
1781337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    private boolean mLastShowedInternalStorage = true;
1791337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    private long mLastUsedStorage, mLastAppStorage, mLastFreeStorage;
1801337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
181f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    static final String TAB_DOWNLOADED = "Downloaded";
182f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    static final String TAB_RUNNING = "Running";
183f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    static final String TAB_ALL = "All";
184f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    static final String TAB_SDCARD = "OnSdCard";
185f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    private View mRootView;
186f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
187f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    // -------------- Copied from TabActivity --------------
188f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
189f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    private TabHost mTabHost;
190f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    private String mDefaultTab = null;
191f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
192f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    // -------------- Copied from TabActivity --------------
193f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
194ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn    final Runnable mRunningProcessesAvail = new Runnable() {
195ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn        public void run() {
196ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            handleRunningProcessesAvail();
197ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn        }
198ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn    };
199ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn
200e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    // View Holder used when displaying views
201e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static class AppViewHolder {
202e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        ApplicationsState.AppEntry entry;
203e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        TextView appName;
204e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        ImageView appIcon;
205e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        TextView appSize;
206e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        TextView disabled;
2076dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn        CheckBox checkBox;
208ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu
209e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        void updateSizeText(ManageApplications ma) {
210e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (DEBUG) Log.i(TAG, "updateSizeText of " + entry.label + " " + entry
211e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    + ": " + entry.sizeStr);
212e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (entry.sizeStr != null) {
213e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                appSize.setText(entry.sizeStr);
214e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            } else if (entry.size == ApplicationsState.SIZE_INVALID) {
215e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                appSize.setText(ma.mInvalidSizeStr);
216ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu            }
217ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu        }
218ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu    }
219ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu
220e6dd1fa1851302710ac7845d25d8ad8a5b6ee438The Android Open Source Project    /*
221e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * Custom adapter implementation for the ListView
222e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * This adapter maintains a map for each displayed application and its properties
223e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * An index value on each AppInfo object indicates the correct position or index
224e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * in the list. If the list gets updated dynamically when the user is viewing the list of
225e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * applications, we need to return the correct index of position. This is done by mapping
226e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * the getId methods via the package name into the internal maps and indices.
227e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn     * The order of applications in the list is mirrored in mAppLocalList
228e6dd1fa1851302710ac7845d25d8ad8a5b6ee438The Android Open Source Project     */
229e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    class ApplicationsAdapter extends BaseAdapter implements Filterable,
230e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            ApplicationsState.Callbacks, AbsListView.RecyclerListener {
231e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private final ApplicationsState mState;
232e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private final ArrayList<View> mActive = new ArrayList<View>();
233e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private ArrayList<ApplicationsState.AppEntry> mBaseEntries;
234e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private ArrayList<ApplicationsState.AppEntry> mEntries;
235e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private boolean mResumed;
236e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private int mLastFilterMode=-1, mLastSortMode=-1;
23719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn        private boolean mWaitingForData;
238e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        CharSequence mCurFilterPrefix;
239e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn
240e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        private Filter mFilter = new Filter() {
241e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            @Override
242e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            protected FilterResults performFiltering(CharSequence constraint) {
243e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                ArrayList<ApplicationsState.AppEntry> entries
244e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        = applyPrefixFilter(constraint, mBaseEntries);
245e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                FilterResults fr = new FilterResults();
246e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                fr.values = entries;
247e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                fr.count = entries.size();
248e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                return fr;
249e6dd1fa1851302710ac7845d25d8ad8a5b6ee438The Android Open Source Project            }
250355b32faa924a0b39067dd759ba03e5af97065caSuchi Amalapurapu
251e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            @Override
252e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            protected void publishResults(CharSequence constraint, FilterResults results) {
253e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mCurFilterPrefix = constraint;
254e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mEntries = (ArrayList<ApplicationsState.AppEntry>)results.values;
255e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                notifyDataSetChanged();
2561337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                updateStorageUsage();
257afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
258e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        };
259c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu
260e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public ApplicationsAdapter(ApplicationsState state) {
261e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mState = state;
262c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu        }
263e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn
264e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void resume(int filter, int sort) {
265e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (DEBUG) Log.i(TAG, "Resume!  mResumed=" + mResumed);
266e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (!mResumed) {
267e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mResumed = true;
268e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mState.resume(this);
269e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mLastFilterMode = filter;
270e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mLastSortMode = sort;
27119df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                rebuild(true);
272e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            } else {
273e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                rebuild(filter, sort);
274c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu            }
275e6dd1fa1851302710ac7845d25d8ad8a5b6ee438The Android Open Source Project        }
276c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu
277e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void pause() {
278e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mResumed) {
279e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mResumed = false;
280e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mState.pause();
281e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
282afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
283b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
284e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void rebuild(int filter, int sort) {
285e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (filter == mLastFilterMode && sort == mLastSortMode) {
286e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                return;
287e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
288e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mLastFilterMode = filter;
289e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mLastSortMode = sort;
29019df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            rebuild(true);
291afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
292afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
29319df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn        public void rebuild(boolean eraseold) {
294e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (DEBUG) Log.i(TAG, "Rebuilding app list...");
295e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            ApplicationsState.AppFilter filterObj;
296e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            Comparator<AppEntry> comparatorObj;
297e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            switch (mLastFilterMode) {
298e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                case FILTER_APPS_THIRD_PARTY:
299e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    filterObj = ApplicationsState.THIRD_PARTY_FILTER;
300e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    break;
301e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                case FILTER_APPS_SDCARD:
302e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    filterObj = ApplicationsState.ON_SD_CARD_FILTER;
303e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    break;
304e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                default:
305e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    filterObj = null;
306e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    break;
307e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
308e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            switch (mLastSortMode) {
309e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                case SORT_ORDER_SIZE:
310e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    comparatorObj = ApplicationsState.SIZE_COMPARATOR;
311e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    break;
312e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                default:
313e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    comparatorObj = ApplicationsState.ALPHA_COMPARATOR;
314e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    break;
315e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
31619df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            ArrayList<ApplicationsState.AppEntry> entries
31719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                    = mState.rebuild(filterObj, comparatorObj);
31819df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            if (entries == null && !eraseold) {
31919df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                // Don't have new list yet, but can continue using the old one.
32019df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                return;
32119df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            }
32219df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mBaseEntries = entries;
32319df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            if (mBaseEntries != null) {
32419df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mEntries = applyPrefixFilter(mCurFilterPrefix, mBaseEntries);
32519df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            } else {
32619df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mEntries = null;
32719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            }
328e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            notifyDataSetChanged();
3291337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            updateStorageUsage();
33019df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn
33119df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            if (entries == null) {
33219df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mWaitingForData = true;
33319df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mListContainer.setVisibility(View.INVISIBLE);
33419df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mLoadingContainer.setVisibility(View.VISIBLE);
33519df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            } else {
33619df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mListContainer.setVisibility(View.VISIBLE);
33719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mLoadingContainer.setVisibility(View.GONE);
33819df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            }
339afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
340afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
341e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        ArrayList<ApplicationsState.AppEntry> applyPrefixFilter(CharSequence prefix,
342e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                ArrayList<ApplicationsState.AppEntry> origEntries) {
343e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (prefix == null || prefix.length() == 0) {
344e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                return origEntries;
345afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            } else {
346e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                String prefixStr = ApplicationsState.normalize(prefix.toString());
347e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                final String spacePrefixStr = " " + prefixStr;
348e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                ArrayList<ApplicationsState.AppEntry> newEntries
349e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        = new ArrayList<ApplicationsState.AppEntry>();
350e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                for (int i=0; i<origEntries.size(); i++) {
351e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    ApplicationsState.AppEntry entry = origEntries.get(i);
352e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    String nlabel = entry.getNormalizedLabel();
353e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    if (nlabel.startsWith(prefixStr) || nlabel.indexOf(spacePrefixStr) != -1) {
354e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        newEntries.add(entry);
355ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu                    }
356ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu                }
357e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                return newEntries;
358afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
359afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
360ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu
361e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
362e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onRunningStateChanged(boolean running) {
363f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn            getActivity().setProgressBarIndeterminateVisibility(running);
364afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
365b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
366e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
36719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn        public void onRebuildComplete(ArrayList<AppEntry> apps) {
3684e4ed14ee2b82cb90a25b3027eb36f190ce2e1e7Dianne Hackborn            if (mLoadingContainer.getVisibility() == View.VISIBLE) {
3694e4ed14ee2b82cb90a25b3027eb36f190ce2e1e7Dianne Hackborn                mLoadingContainer.startAnimation(AnimationUtils.loadAnimation(
370f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                        getActivity(), android.R.anim.fade_out));
3714e4ed14ee2b82cb90a25b3027eb36f190ce2e1e7Dianne Hackborn                mListContainer.startAnimation(AnimationUtils.loadAnimation(
372f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                        getActivity(), android.R.anim.fade_in));
3734e4ed14ee2b82cb90a25b3027eb36f190ce2e1e7Dianne Hackborn            }
37419df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mListContainer.setVisibility(View.VISIBLE);
37519df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mLoadingContainer.setVisibility(View.GONE);
37619df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mWaitingForData = false;
37719df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mBaseEntries = apps;
37819df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            mEntries = applyPrefixFilter(mCurFilterPrefix, mBaseEntries);
37919df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            notifyDataSetChanged();
3801337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            updateStorageUsage();
38119df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn        }
38219df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn
38319df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn        @Override
384e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onPackageListChanged() {
38519df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn            rebuild(false);
386afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
387b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
388e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
389e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onPackageIconChanged() {
390e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            // We ensure icons are loaded when their item is displayed, so
391e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            // don't care about icons loaded in the background.
392afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
393b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
394e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
395e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onPackageSizeChanged(String packageName) {
396e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            for (int i=0; i<mActive.size(); i++) {
397e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                AppViewHolder holder = (AppViewHolder)mActive.get(i).getTag();
398e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                if (holder.entry.info.packageName.equals(packageName)) {
399e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    synchronized (holder.entry) {
400e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        holder.updateSizeText(ManageApplications.this);
401c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu                    }
402e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    if (holder.entry.info.packageName.equals(mCurrentPkgName)
403e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                            && mLastSortMode == SORT_ORDER_SIZE) {
404e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        // We got the size information for the last app the
405e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        // user viewed, and are sorting by size...  they may
406e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        // have cleared data, so we immediately want to resort
407e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                        // the list with the new size to reflect it to the user.
40819df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                        rebuild(false);
409c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu                    }
4101337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                    updateStorageUsage();
411e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    return;
412c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu                }
413c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu            }
414c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu        }
415c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu
416e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
417e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onAllSizesComputed() {
418e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mLastSortMode == SORT_ORDER_SIZE) {
41919df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                rebuild(false);
420afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
421afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
422afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
423afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public int getCount() {
424e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            return mEntries != null ? mEntries.size() : 0;
425afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
426afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
427afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public Object getItem(int position) {
428e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            return mEntries.get(position);
429afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
430afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
431e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public ApplicationsState.AppEntry getAppEntry(int position) {
432e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            return mEntries.get(position);
433afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
434afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
435afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public long getItemId(int position) {
436e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            return mEntries.get(position).id;
437afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
438afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
439afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        public View getView(int position, View convertView, ViewGroup parent) {
440347c9a0544328e65120582834ce330fb0d473f28Jeff Brown            // A ViewHolder keeps references to children views to avoid unnecessary calls
441afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // to findViewById() on each row.
442afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            AppViewHolder holder;
443afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
444afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // When convertView is not null, we can reuse it directly, there is no need
445afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // to reinflate it. We only inflate a new View when the convertView supplied
446afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // by ListView is null.
447afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            if (convertView == null) {
448afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                convertView = mInflater.inflate(R.layout.manage_applications_item, null);
449afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
450afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                // Creates a ViewHolder and store references to the two children views
451afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                // we want to bind data to.
452afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                holder = new AppViewHolder();
453afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                holder.appName = (TextView) convertView.findViewById(R.id.app_name);
454afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
455afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
4569db5ee35d5345e13e6e6b857dbad5a83e9f037d1Dianne Hackborn                holder.disabled = (TextView) convertView.findViewById(R.id.app_disabled);
4576dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                holder.checkBox = (CheckBox) convertView.findViewById(R.id.app_on_sdcard);
458afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                convertView.setTag(holder);
459afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            } else {
460afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                // Get the ViewHolder back to get fast access to the TextView
461afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                // and the ImageView.
462afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                holder = (AppViewHolder) convertView.getTag();
463afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
464afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
465afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            // Bind the data efficiently with the holder
466e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            ApplicationsState.AppEntry entry = mEntries.get(position);
467e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            synchronized (entry) {
468e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                holder.entry = entry;
469e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                if (entry.label != null) {
470e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    holder.appName.setText(entry.label);
471f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                    holder.appName.setTextColor(getActivity().getResources().getColorStateList(
472e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                            entry.info.enabled ? android.R.color.primary_text_dark
4739db5ee35d5345e13e6e6b857dbad5a83e9f037d1Dianne Hackborn                                    : android.R.color.secondary_text_dark));
474afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                }
475e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mState.ensureIcon(entry);
476e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                if (entry.icon != null) {
477e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                    holder.appIcon.setImageDrawable(entry.icon);
478afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project                }
479e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                holder.updateSizeText(ManageApplications.this);
48044178e2801c013e60defb4b5f390d893e7344a71Dianne Hackborn                if (InstalledAppDetails.SUPPORT_DISABLE_APPS) {
48144178e2801c013e60defb4b5f390d893e7344a71Dianne Hackborn                    holder.disabled.setVisibility(entry.info.enabled ? View.GONE : View.VISIBLE);
48244178e2801c013e60defb4b5f390d893e7344a71Dianne Hackborn                } else {
48344178e2801c013e60defb4b5f390d893e7344a71Dianne Hackborn                    holder.disabled.setVisibility(View.GONE);
48444178e2801c013e60defb4b5f390d893e7344a71Dianne Hackborn                }
4856dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                if (mLastFilterMode == FILTER_APPS_SDCARD) {
4866dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    holder.checkBox.setVisibility(View.VISIBLE);
4876dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    holder.checkBox.setChecked((entry.info.flags
4886dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                            & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0);
4896dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                } else {
4906dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                    holder.checkBox.setVisibility(View.GONE);
4916dc1bf84cf72dd72a2517878f6b280e8afdcf4c2Dianne Hackborn                }
492afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            }
493e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mActive.remove(convertView);
494e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mActive.add(convertView);
495afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return convertView;
496afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
497c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu
498e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
499c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu        public Filter getFilter() {
500e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            return mFilter;
501c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu        }
502c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu
503e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        @Override
504e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        public void onMovedToScrapHeap(View view) {
505e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mActive.remove(view);
506728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        }
507728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    }
508728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
509afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
510f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onCreate(Bundle savedInstanceState) {
511afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        super.onCreate(savedInstanceState);
512f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
513f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        setHasOptionsMenu(true);
514f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
515f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mApplicationsState = ApplicationsState.getInstance(getActivity().getApplication());
516e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mApplicationsAdapter = new ApplicationsAdapter(mApplicationsState);
517f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        Intent intent = getActivity().getIntent();
518ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu        String action = intent.getAction();
51938b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        String defaultTabTag = TAB_DOWNLOADED;
520ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        String className = getArguments() != null
521ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                ? getArguments().getString("classname") : null;
522ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        if (className == null) {
523ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            className = intent.getComponent().getClassName();
524ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        }
5255203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasani        if (className.equals(RunningServicesActivity.class.getName())) {
526728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            defaultTabTag = TAB_RUNNING;
5275203bdffe6ce2b829d0fab8c7691fa92aca6e972Amith Yamasani        } else if (className.equals(StorageUseActivity.class.getName())
528508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn                || action.equals(Intent.ACTION_MANAGE_PACKAGE_STORAGE)) {
529afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            mSortOrder = SORT_ORDER_SIZE;
530fe4a2bc43f45b14db62c67d7c2531a3d7f1b8590Suchi Amalapurapu            mFilterApps = FILTER_APPS_ALL;
53138b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu            defaultTabTag = TAB_ALL;
5329a294912ada6b90e6dac882e79ee449964c2263fWinson Chung        } else if (action.equals(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS)) {
5339a294912ada6b90e6dac882e79ee449964c2263fWinson Chung            // Select the all-apps tab, with the default sorting
5349a294912ada6b90e6dac882e79ee449964c2263fWinson Chung            defaultTabTag = TAB_ALL;
535afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
536728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
537728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        if (savedInstanceState != null) {
538728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            mSortOrder = savedInstanceState.getInt("sortOrder", mSortOrder);
539728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            mFilterApps = savedInstanceState.getInt("filterApps", mFilterApps);
540728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            String tmp = savedInstanceState.getString("defaultTabTag");
541728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            if (tmp != null) defaultTabTag = tmp;
542728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        }
543728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
544f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mDefaultTab = defaultTabTag;
545728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
5461337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mDataFileStats = new StatFs("/data");
5471337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mSDCardFileStats = new StatFs(Environment.getExternalStorageDirectory().toString());
5481337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
549f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mInvalidSizeStr = getActivity().getText(R.string.invalid_size_value);
550f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mComputingSizeStr = getActivity().getText(R.string.computing_size);
551f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    }
552f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
553f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
554f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    @Override
555f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
556afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // initialize the inflater
557f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mInflater = inflater;
558f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mRootView = inflater.inflate(R.layout.manage_applications, null);
559ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn        mLoadingContainer = mRootView.findViewById(R.id.loading_container);
560508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn        mListContainer = mRootView.findViewById(R.id.list_container);
561afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        // Create adapter and list view here
562508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn        ListView lv = (ListView) mListContainer.findViewById(android.R.id.list);
563508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn        View emptyView = mListContainer.findViewById(com.android.internal.R.id.empty);
564508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn        if (emptyView != null) {
565508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn            lv.setEmptyView(emptyView);
566508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn        }
567afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        lv.setOnItemClickListener(this);
568afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        lv.setSaveEnabled(true);
569afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        lv.setItemsCanFocus(true);
570afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        lv.setOnItemClickListener(this);
571c0b23d3f0bb42b26950512590d5ebd6d3373032dSuchi Amalapurapu        lv.setTextFilterEnabled(true);
572afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        mListView = lv;
573e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        lv.setRecyclerListener(mApplicationsAdapter);
574e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mListView.setAdapter(mApplicationsAdapter);
5751337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mColorBar = (LinearColorBar)mListContainer.findViewById(R.id.storage_color_bar);
5761337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mStorageChartLabel = (TextView)mListContainer.findViewById(R.id.storageChartLabel);
5771337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mUsedStorageText = (TextView)mListContainer.findViewById(R.id.usedStorageText);
5781337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        mFreeStorageText = (TextView)mListContainer.findViewById(R.id.freeStorageText);
579728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        mRunningProcessesView = (RunningProcessesView)mRootView.findViewById(
580728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn                R.id.running_processes);
581f4c4b59f13a0cadd55832004e56ff7b6d827f7f0Suchi Amalapurapu
582ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        mCreatedRunning = mResumedRunning = false;
583ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn
584f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        View tabRoot = mInflater.inflate(com.android.internal.R.layout.tab_content, null);
585f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mTabHost = (TabHost)tabRoot.findViewById(com.android.internal.R.id.tabhost);
586f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        mTabHost.setup();
587f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        final TabHost tabHost = mTabHost;
58838b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        tabHost.addTab(tabHost.newTabSpec(TAB_DOWNLOADED)
589f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                .setIndicator(getActivity().getString(R.string.filter_apps_third_party),
590f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                        getActivity().getResources().getDrawable(R.drawable.ic_tab_download))
59138b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu                .setContent(this));
59238b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        tabHost.addTab(tabHost.newTabSpec(TAB_ALL)
593f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                .setIndicator(getActivity().getString(R.string.filter_apps_all),
594f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                        getActivity().getResources().getDrawable(R.drawable.ic_tab_all))
59538b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu                .setContent(this));
596ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        if (!Environment.isExternalStorageEmulated()) {
597ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            tabHost.addTab(tabHost.newTabSpec(TAB_SDCARD)
598ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                    .setIndicator(getActivity().getString(R.string.filter_apps_onsdcard),
599ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                            getActivity().getResources().getDrawable(R.drawable.ic_tab_sdcard))
600ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                    .setContent(this));
601ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        }
602728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        tabHost.addTab(tabHost.newTabSpec(TAB_RUNNING)
603f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                .setIndicator(getActivity().getString(R.string.filter_apps_running),
604f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                        getActivity().getResources().getDrawable(R.drawable.ic_tab_running))
605728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn                .setContent(this));
606f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        tabHost.setCurrentTabByTag(mDefaultTab);
60738b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        tabHost.setOnTabChangedListener(this);
608f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
609f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        return tabRoot;
610afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
611f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
612f4c4b59f13a0cadd55832004e56ff7b6d827f7f0Suchi Amalapurapu    @Override
613728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    public void onStart() {
614728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        super.onStart();
615b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu    }
616b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
617afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
618f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onResume() {
619728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        super.onResume();
620728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        mActivityResumed = true;
621e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        showCurrentTab();
622ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        updateOptionsMenu();
623afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
624b285593cafacc58ecfdf97be395dc8f5a7493051Suchi Amalapurapu
625728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    @Override
626f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onSaveInstanceState(Bundle outState) {
627728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        super.onSaveInstanceState(outState);
628728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        outState.putInt("sortOrder", mSortOrder);
629728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        outState.putInt("filterApps", mFilterApps);
630ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        if (mDefaultTab != null) {
631ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            outState.putString("defautTabTag", mDefaultTab);
632f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        }
633728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    }
634728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
635728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    @Override
636f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onPause() {
637728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        super.onPause();
638728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        mActivityResumed = false;
639e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mApplicationsAdapter.pause();
640728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        if (mResumedRunning) {
641728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            mRunningProcessesView.doPause();
642728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            mResumedRunning = false;
643728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        }
644728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    }
645728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
646728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn    @Override
647f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onActivityResult(int requestCode, int resultCode, Intent data) {
648728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        if (requestCode == INSTALLED_APP_DETAILS && mCurrentPkgName != null) {
649e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mApplicationsState.requestSize(mCurrentPkgName);
650ff271ce1b70d6a8c59f3ea8904ef2025061e73f1Suchi Amalapurapu        }
651afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
652afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
653afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    // utility method used to start sub activity
654afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    private void startApplicationDetailsActivity() {
655f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        // start new fragment to display extended information
656f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        Bundle args = new Bundle();
657f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        args.putString(InstalledAppDetails.ARG_PACKAGE_NAME, mCurrentPkgName);
658f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn
659f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn        PreferenceActivity pa = (PreferenceActivity)getActivity();
660ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        pa.startPreferencePanel(InstalledAppDetails.class.getName(), args,
661ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                R.string.application_info_label, null, this, INSTALLED_APP_DETAILS);
662afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
663afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
664afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
665f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
666ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        Log.i(TAG, "onCreateOptionsMenu in " + this + ": " + menu);
667ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        mOptionsMenu = menu;
668afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        menu.add(0, SORT_ORDER_ALPHA, 1, R.string.sort_order_alpha)
669ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setIcon(android.R.drawable.ic_menu_sort_alphabetically)
670ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
671afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        menu.add(0, SORT_ORDER_SIZE, 2, R.string.sort_order_size)
672ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setIcon(android.R.drawable.ic_menu_sort_by_size)
673ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
674ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        menu.add(0, SHOW_RUNNING_SERVICES, 3, R.string.show_running_services)
675ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
676ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        menu.add(0, SHOW_BACKGROUND_PROCESSES, 3, R.string.show_background_processes)
677ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
678ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        updateOptionsMenu();
679afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
680afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
681afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
682f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn    public void onPrepareOptionsMenu(Menu menu) {
683ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        updateOptionsMenu();
684ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    }
685ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn
686ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    @Override
687ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    public void onDestroyOptionsMenu() {
688ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        mOptionsMenu = null;
689ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    }
690ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn
691ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn    void updateOptionsMenu() {
692ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        if (mOptionsMenu == null) {
693ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            return;
694ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        }
695ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn
696673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root        /*
697673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root         * The running processes screen doesn't use the mApplicationsAdapter
698673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root         * so bringing up this menu in that case doesn't make any sense.
699673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root         */
700673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root        if (mCurView == VIEW_RUNNING) {
701ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            boolean showingBackground = mRunningProcessesView != null
702ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                    ? mRunningProcessesView.mAdapter.getShowBackground() : false;
703ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SORT_ORDER_ALPHA).setVisible(false);
704ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SORT_ORDER_SIZE).setVisible(false);
705ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SHOW_RUNNING_SERVICES).setVisible(showingBackground);
706ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SHOW_BACKGROUND_PROCESSES).setVisible(!showingBackground);
7071337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        } else {
708ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SORT_ORDER_ALPHA).setVisible(mSortOrder != SORT_ORDER_ALPHA);
709ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SORT_ORDER_SIZE).setVisible(mSortOrder != SORT_ORDER_SIZE);
710ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SHOW_RUNNING_SERVICES).setVisible(false);
711ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn            mOptionsMenu.findItem(SHOW_BACKGROUND_PROCESSES).setVisible(false);
712673085af9e5e040eb0109138e9cb6916fa4b88f0Kenny Root        }
713afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
714afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
715afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    @Override
716afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public boolean onOptionsItemSelected(MenuItem item) {
717afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        int menuId = item.getItemId();
718afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        if ((menuId == SORT_ORDER_ALPHA) || (menuId == SORT_ORDER_SIZE)) {
719e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mSortOrder = menuId;
7201337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mCurView != VIEW_RUNNING) {
7211337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mApplicationsAdapter.rebuild(mFilterApps, mSortOrder);
7221337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
7231337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        } else if (menuId == SHOW_RUNNING_SERVICES) {
7241337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mRunningProcessesView.mAdapter.setShowBackground(false);
7251337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        } else if (menuId == SHOW_BACKGROUND_PROCESSES) {
7261337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mRunningProcessesView.mAdapter.setShowBackground(true);
727afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
728ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        updateOptionsMenu();
729afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        return true;
730afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
731d2be882d8f2e5acd8a5806c649ba4640249cf4baDianne Hackborn
732afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    public void onItemClick(AdapterView<?> parent, View view, int position,
733afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            long id) {
734e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        ApplicationsState.AppEntry entry = mApplicationsAdapter.getAppEntry(position);
735e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mCurrentPkgName = entry.info.packageName;
736afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        startApplicationDetailsActivity();
737afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
738afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project
73938b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu    public View createTabContent(String tag) {
74038b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        return mRootView;
74138b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu    }
74238b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu
743e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static final int VIEW_NOTHING = 0;
744e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static final int VIEW_LIST = 1;
745e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    static final int VIEW_RUNNING = 2;
746e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn
7471337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    void updateStorageUsage() {
7481337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        if (mCurView == VIEW_RUNNING) {
7491337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            return;
7501337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        }
7511337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
7521337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        long freeStorage = 0;
7531337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        long appStorage = 0;
7541337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        long totalStorage = 0;
7551337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        CharSequence newLabel = null;
7561337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
7571337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        if (mFilterApps == FILTER_APPS_SDCARD) {
7581337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mLastShowedInternalStorage) {
7591337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastShowedInternalStorage = false;
7601337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
761f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn            newLabel = getActivity().getText(R.string.sd_card_storage);
7621337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mSDCardFileStats.restat(Environment.getExternalStorageDirectory().toString());
7631337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            try {
7641337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                totalStorage = (long)mSDCardFileStats.getBlockCount() *
7651337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                        mSDCardFileStats.getBlockSize();
7661337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                freeStorage = (long) mSDCardFileStats.getAvailableBlocks() *
7671337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mSDCardFileStats.getBlockSize();
7681337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            } catch (IllegalArgumentException e) {
7691337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                // use the old value of mFreeMem
7701337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
7711337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        } else {
7721337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (!mLastShowedInternalStorage) {
7731337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastShowedInternalStorage = true;
7741337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
775f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn            newLabel = getActivity().getText(R.string.internal_storage);
7761337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mDataFileStats.restat("/data");
7771337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            try {
7781337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                totalStorage = (long)mDataFileStats.getBlockCount() *
7791337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                        mDataFileStats.getBlockSize();
7801337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                freeStorage = (long) mDataFileStats.getAvailableBlocks() *
7811337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                    mDataFileStats.getBlockSize();
7821337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            } catch (IllegalArgumentException e) {
7831337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
7841337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            final int N = mApplicationsAdapter.getCount();
7851337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            for (int i=0; i<N; i++) {
7861337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                ApplicationsState.AppEntry ae = mApplicationsAdapter.getAppEntry(i);
7871337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                appStorage += ae.codeSize + ae.dataSize;
7881337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
789d2be882d8f2e5acd8a5806c649ba4640249cf4baDianne Hackborn            freeStorage += mApplicationsState.sumCacheSizes();
7901337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        }
7911337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        if (newLabel != null) {
7921337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mStorageChartLabel.setText(newLabel);
7931337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        }
7941337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        if (totalStorage > 0) {
7951337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mColorBar.setRatios((totalStorage-freeStorage-appStorage)/(float)totalStorage,
7961337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                    appStorage/(float)totalStorage, freeStorage/(float)totalStorage);
7971337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            long usedStorage = totalStorage - freeStorage;
7981337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mLastUsedStorage != usedStorage) {
7991337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastUsedStorage = usedStorage;
800f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                String sizeStr = Formatter.formatShortFileSize(getActivity(), usedStorage);
801f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                mUsedStorageText.setText(getActivity().getResources().getString(
8021337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                        R.string.service_foreground_processes, sizeStr));
8031337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
8041337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mLastFreeStorage != freeStorage) {
8051337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastFreeStorage = freeStorage;
806f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                String sizeStr = Formatter.formatShortFileSize(getActivity(), freeStorage);
807f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                mFreeStorageText.setText(getActivity().getResources().getString(
8081337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                        R.string.service_background_processes, sizeStr));
8091337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
8101337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        } else {
8111337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            mColorBar.setRatios(0, 0, 0);
8121337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mLastUsedStorage != -1) {
8131337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastUsedStorage = -1;
8141337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mUsedStorageText.setText("");
8151337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
8161337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            if (mLastFreeStorage != -1) {
8171337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mLastFreeStorage = -1;
8181337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn                mFreeStorageText.setText("");
8191337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn            }
8201337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        }
8211337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn    }
8221337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn
823e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    private void selectView(int which) {
824e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        if (which == VIEW_LIST) {
825e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mResumedRunning) {
826e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mRunningProcessesView.doPause();
827e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mResumedRunning = false;
828e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
829e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mCurView != which) {
830e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mRunningProcessesView.setVisibility(View.GONE);
831508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn                mListContainer.setVisibility(View.VISIBLE);
83219df79af269c4a2354b0989cebfcfe949aea42c4Dianne Hackborn                mLoadingContainer.setVisibility(View.GONE);
833e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
834e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mActivityResumed) {
835e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mApplicationsAdapter.resume(mFilterApps, mSortOrder);
836e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
837e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        } else if (which == VIEW_RUNNING) {
838e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (!mCreatedRunning) {
839f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                mRunningProcessesView.doCreate(null);
840e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mCreatedRunning = true;
841e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
842ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            boolean haveData = true;
843e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mActivityResumed && !mResumedRunning) {
844ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn                haveData = mRunningProcessesView.doResume(this, mRunningProcessesAvail);
845e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn                mResumedRunning = true;
846e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
847e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            mApplicationsAdapter.pause();
848e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            if (mCurView != which) {
849ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn                if (haveData) {
850ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn                    mRunningProcessesView.setVisibility(View.VISIBLE);
851ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn                } else {
852ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn                    mLoadingContainer.setVisibility(View.VISIBLE);
853ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn                }
854508fedc4102790dcd7004e4962e2b540967b714bDianne Hackborn                mListContainer.setVisibility(View.GONE);
855e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn            }
856e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        }
857e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mCurView = which;
858e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    }
859e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn
860ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn    void handleRunningProcessesAvail() {
861ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn        if (mCurView == VIEW_RUNNING) {
862ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            mLoadingContainer.startAnimation(AnimationUtils.loadAnimation(
863f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                    getActivity(), android.R.anim.fade_out));
864ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            mRunningProcessesView.startAnimation(AnimationUtils.loadAnimation(
865f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                    getActivity(), android.R.anim.fade_in));
866ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            mRunningProcessesView.setVisibility(View.VISIBLE);
867ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn            mLoadingContainer.setVisibility(View.GONE);
868ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn        }
869ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn    }
870ff3673c7f477b1d78e64a8bc28754552dcdc8826Dianne Hackborn
871e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    public void showCurrentTab() {
872ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        String tabId = mDefaultTab = mTabHost.getCurrentTabTag();
873afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        int newOption;
87438b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        if (TAB_DOWNLOADED.equalsIgnoreCase(tabId)) {
87538b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu            newOption = FILTER_APPS_THIRD_PARTY;
87638b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        } else if (TAB_ALL.equalsIgnoreCase(tabId)) {
87738b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu            newOption = FILTER_APPS_ALL;
878747ea55fce48c23b51fe6f6df8c4236effd91e37Suchi Amalapurapu        } else if (TAB_SDCARD.equalsIgnoreCase(tabId)) {
879747ea55fce48c23b51fe6f6df8c4236effd91e37Suchi Amalapurapu            newOption = FILTER_APPS_SDCARD;
880728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        } else if (TAB_RUNNING.equalsIgnoreCase(tabId)) {
881f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn            ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
882f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                    .hideSoftInputFromWindow(
883f4eb85bc3c57ca70b577cea1524431fead4a4cccDianne Hackborn                            getActivity().getWindow().getDecorView().getWindowToken(), 0);
884728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            selectView(VIEW_RUNNING);
885728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn            return;
88638b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu        } else {
88738b89707068a4ef344f0a734c6c38e175de349e8Suchi Amalapurapu            // Invalid option. Do nothing
888afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project            return;
889afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project        }
890728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn
891e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        mFilterApps = newOption;
892728ac35373028b79205a788c526ef4b495518ec1Dianne Hackborn        selectView(VIEW_LIST);
8931337d0f70b18dd732c6a617be83e5be0bb8f2f87Dianne Hackborn        updateStorageUsage();
894ee2937941d4e9a4d2cd94d504ddaa9685bcca090Dianne Hackborn        updateOptionsMenu();
895e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    }
896e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn
897e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn    public void onTabChanged(String tabId) {
898e7623f775cb37a8de049dae8c03b5b396526fdd1Dianne Hackborn        showCurrentTab();
899afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project    }
900afc4ab2ffbb8327ddce9907961295a32cbf49d0fThe Android Open Source Project}
901