MiscFilesHandler.java revision 8128ff8cbe239c1d50f8ba12cf375c754862df0a
121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori/*
221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * Copyright (C) 2010 The Android Open Source Project
321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori *
421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * Licensed under the Apache License, Version 2.0 (the "License");
521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * you may not use this file except in compliance with the License.
621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * You may obtain a copy of the License at
721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori *
821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori *      http://www.apache.org/licenses/LICENSE-2.0
921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori *
1021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * Unless required by applicable law or agreed to in writing, software
1121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * distributed under the License is distributed on an "AS IS" BASIS,
1221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * See the License for the specific language governing permissions and
1421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * limitations under the License.
1521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori */
1621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
1721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noripackage com.android.settings.deviceinfo;
1821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
1921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport com.android.settings.R;
2021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport com.android.settings.deviceinfo.MemoryMeasurement.FileInfo;
2121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
2221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.app.ListActivity;
2321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.content.Context;
2421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.os.Bundle;
2521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.text.format.Formatter;
2621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.util.Log;
2721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.util.SparseBooleanArray;
2821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.ActionMode;
2921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.LayoutInflater;
3021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.Menu;
3121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.MenuInflater;
3221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.MenuItem;
3321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.View;
3421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.View.OnClickListener;
3521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.View.OnLongClickListener;
3621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.view.ViewGroup;
3721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.widget.BaseAdapter;
3821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.widget.CompoundButton;
3921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.widget.CompoundButton.OnCheckedChangeListener;
4021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport android.widget.ListView;
4121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
4221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport java.io.File;
4321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport java.util.ArrayList;
4421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noriimport java.util.List;
4521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
4621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori/**
4721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori * This class handles the selection and removal of Misc files.
4821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori */
4921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Noripublic class MiscFilesHandler extends ListActivity {
5021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    private static final String TAG = "MemorySettings";
518128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori    private String mNumSelectedFormat;
528128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori    private String mNumBytesSelectedFormat;
5321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    private MemoryMearurementAdapter mAdapter;
5421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    private LayoutInflater mInflater;
5521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
5621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    @Override
5721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    public void onCreate(Bundle savedInstanceState) {
5821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        super.onCreate(savedInstanceState);
5921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        setFinishOnTouchOutside(true);
6021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        setTitle(R.string.misc_files);
618128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori        mNumSelectedFormat = getString(R.string.misc_files_selected_count);
628128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori        mNumBytesSelectedFormat = getString(R.string.misc_files_selected_count_bytes);
6321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        mAdapter = new MemoryMearurementAdapter(this);
6421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
6521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        setContentView(R.layout.settings_storage_miscfiles_list);
6621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        ListView lv = getListView();
6721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        lv.setItemsCanFocus(true);
6821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
6921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        lv.setMultiChoiceModeListener(new ModeCallback(this));
7021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        setListAdapter(mAdapter);
7121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    }
7221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
7321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    private class ModeCallback implements ListView.MultiChoiceModeListener {
7421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private int mDataCount;
7521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private final Context mContext;
7621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
7721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public ModeCallback(Context context) {
7821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            mContext = context;
7921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            mDataCount = mAdapter.getCount();
8021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
8121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
8221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
8321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            final MenuInflater inflater = getMenuInflater();
8421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            inflater.inflate(R.menu.misc_files_menu, menu);
8521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return true;
8621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
8721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
8821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
8921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return true;
9021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
9121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
9221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
9321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            ListView lv = getListView();
9421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            switch (item.getItemId()) {
9521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            case R.id.action_delete:
9621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                // delete the files selected
9721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
9821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                int checkedCount = getListView().getCheckedItemCount();
9921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                if (checkedCount > mDataCount) {
10021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    throw new IllegalStateException("checked item counts do not match. " +
10121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            "checkedCount: " + checkedCount + ", dataSize: " + mDataCount);
10221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
10321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                if (mDataCount > 0) {
10421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    ArrayList<Object> toRemove = new ArrayList<Object>();
10521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    for (int i = 0; i < mDataCount; i++) {
10621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        if (!checkedItems.get(i)) {
10721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            //item not selected
10821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            continue;
10921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        }
11021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        if (MemoryMeasurement.LOGV) {
11121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            Log.i(TAG, "deleting: " + mAdapter.getItem(i));
11221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        }
11321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        // delete the file
11421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        File file = new File(mAdapter.getItem(i).mFileName);
11521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        if (file.isDirectory()) {
11621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            deleteDir(file);
11721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        } else {
11821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            file.delete();
11921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        }
12021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        toRemove.add(mAdapter.getItem(i));
12121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    }
12221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    mAdapter.removeAll(toRemove);
12321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    mAdapter.notifyDataSetChanged();
12421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    mDataCount = mAdapter.getCount();
12521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
12621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                mode.finish();
12721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                break;
12821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
12921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            case R.id.action_select_all:
13021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                // check ALL items
13121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                for (int i = 0; i < mDataCount; i++) {
13221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    lv.setItemChecked(i, true);
13321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
13421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                // update the title and subtitle with number selected and numberBytes selected
13521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                onItemCheckedStateChanged(mode, 1, 0, true);
13621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                break;
13721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
13821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return true;
13921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
14021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
14121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        // Deletes all files and subdirectories under given dir.
14221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        // Returns true if all deletions were successful.
14321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        // If a deletion fails, the method stops attempting to delete and returns false.
14421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private boolean deleteDir(File dir) {
14521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (dir.isDirectory()) {
14621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                String[] children = dir.list();
14721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                for (int i=0; i < children.length; i++) {
14821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    boolean success = deleteDir(new File(dir, children[i]));
14921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    if (!success) {
15021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        return false;
15121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    }
15221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
15321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
15421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            // The directory is now empty so delete it
15521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return dir.delete();
15621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
15721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
15821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public void onDestroyActionMode(ActionMode mode) {
15921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
16021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
16121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
16221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                boolean checked) {
16321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            ListView lv = getListView();
16421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            int numChecked = lv.getCheckedItemCount();
1658128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori            mode.setTitle(String.format(mNumSelectedFormat, numChecked, mAdapter.getCount()));
16621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
16721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            // total the sizes of all items selected so far
16821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
16921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            long selectedDataSize = 0;
17021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (numChecked > 0) {
17121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                for (int i = 0; i < mDataCount; i++) {
17221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    if (checkedItems.get(i)) {
17321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        // item is checked
17421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        selectedDataSize += mAdapter.getItem(i).mSize;
17521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    }
17621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
17721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
1788128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori            mode.setSubtitle(String.format(mNumBytesSelectedFormat,
1798128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori                    Formatter.formatFileSize(mContext, selectedDataSize),
1808128ff8cbe239c1d50f8ba12cf375c754862df0aVasu Nori                    Formatter.formatFileSize(mContext, mAdapter.getDataSize())));
18121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
18221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    }
18321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
18421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    public class MemoryMearurementAdapter extends BaseAdapter {
18521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private ArrayList<MemoryMeasurement.FileInfo> mData = null;
18621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private long mDataSize = 0;
18721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        private Context mContext;
18821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
18921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public MemoryMearurementAdapter(Context context) {
19021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            mContext = context;
19121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            MemoryMeasurement mMeasurement = MemoryMeasurement.getInstance(context);
19221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            mData = (ArrayList<MemoryMeasurement.FileInfo>)mMeasurement.mFileInfoForMisc;
19321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (mData != null) {
19421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                for (MemoryMeasurement.FileInfo info : mData) {
19521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    mDataSize += info.mSize;
19621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
19721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
19821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
19921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
20021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        @Override
20121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public int getCount() {
20221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return (mData == null) ? 0 : mData.size();
20321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
20421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
20521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        @Override
20621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public MemoryMeasurement.FileInfo getItem(int position) {
20721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (mData == null || mData.size() <= position) {
20821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                return null;
20921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
21021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return mData.get(position);
21121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
21221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
21321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        @Override
21421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public long getItemId(int position) {
21521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (mData == null || mData.size() <= position) {
21621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                return 0;
21721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
21821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return mData.get(position).mId;
21921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
22021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public void removeAll(List<Object> objs) {
22121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            if (mData == null) {
22221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                return;
22321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
22421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            for (Object o : objs) {
22521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                mData.remove(o);
22621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                mDataSize -= ((MemoryMeasurement.FileInfo) o).mSize;
22721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            }
22821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
22921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
23021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public long getDataSize() {
23121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return mDataSize;
23221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
23321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
23421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        @Override
23521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public void notifyDataSetChanged() {
23621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            super.notifyDataSetChanged();
23721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
23821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
23921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        @Override
24021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        public View getView(int position, View convertView, ViewGroup parent) {
24121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            final FileItemInfoLayout view = (convertView == null) ?
24221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    (FileItemInfoLayout) mInflater.inflate(R.layout.settings_storage_miscfiles,
24321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                            parent, false) : (FileItemInfoLayout) convertView;
24421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            FileInfo item = getItem(position);
24521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            view.setFileName(item.mFileName);
24621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            view.setFileSize(Formatter.formatFileSize(mContext, item.mSize));
24721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            final ListView listView = (ListView) parent;
24821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            final int listPosition = position;
24921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            view.getCheckBox().setOnCheckedChangeListener(new OnCheckedChangeListener() {
25021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
25121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                @Override
25221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
25321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    listView.setItemChecked(listPosition, isChecked);
25421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
25521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori
25621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            });
25721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            view.setOnLongClickListener(new OnLongClickListener() {
25821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                @Override
25921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                public boolean onLongClick(View v) {
26021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    if (listView.getCheckedItemCount() > 0) {
26121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        return false;
26221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    }
26321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    listView.setItemChecked(listPosition, !view.isChecked());
26421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    return true;
26521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
26621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            });
26721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            view.setOnClickListener(new OnClickListener() {
26821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                @Override
26921809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                public void onClick(View v) {
27021809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    if (listView.getCheckedItemCount() > 0) {
27121809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                        listView.setItemChecked(listPosition, !view.isChecked());
27221809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                    }
27321809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori                }
27421809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            });
27521809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori            return view;
27621809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori        }
27721809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori    }
27821809740f2fd8fe8df4e300e5d1d15f0ccedbb5fVasu Nori}