1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.deviceinfo;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.storage.StorageVolume;
import android.text.format.Formatter;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import com.android.settings.R;
import com.android.settings.deviceinfo.StorageMeasurement.FileInfo;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* This class handles the selection and removal of Misc files.
*/
public class MiscFilesHandler extends ListActivity {
private static final String TAG = "MemorySettings";
private String mNumSelectedFormat;
private String mNumBytesSelectedFormat;
private MemoryMearurementAdapter mAdapter;
private LayoutInflater mInflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setFinishOnTouchOutside(true);
setTitle(R.string.misc_files);
mNumSelectedFormat = getString(R.string.misc_files_selected_count);
mNumBytesSelectedFormat = getString(R.string.misc_files_selected_count_bytes);
mAdapter = new MemoryMearurementAdapter(this);
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(R.layout.settings_storage_miscfiles_list);
ListView lv = getListView();
lv.setItemsCanFocus(true);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setMultiChoiceModeListener(new ModeCallback(this));
setListAdapter(mAdapter);
}
private class ModeCallback implements ListView.MultiChoiceModeListener {
private int mDataCount;
private final Context mContext;
public ModeCallback(Context context) {
mContext = context;
mDataCount = mAdapter.getCount();
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
final MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.misc_files_menu, menu);
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
ListView lv = getListView();
switch (item.getItemId()) {
case R.id.action_delete:
// delete the files selected
SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
int checkedCount = getListView().getCheckedItemCount();
if (checkedCount > mDataCount) {
throw new IllegalStateException("checked item counts do not match. " +
"checkedCount: " + checkedCount + ", dataSize: " + mDataCount);
}
if (mDataCount > 0) {
ArrayList<Object> toRemove = new ArrayList<Object>();
for (int i = 0; i < mDataCount; i++) {
if (!checkedItems.get(i)) {
//item not selected
continue;
}
if (StorageMeasurement.LOGV) {
Log.i(TAG, "deleting: " + mAdapter.getItem(i));
}
// delete the file
File file = new File(mAdapter.getItem(i).mFileName);
if (file.isDirectory()) {
deleteDir(file);
} else {
file.delete();
}
toRemove.add(mAdapter.getItem(i));
}
mAdapter.removeAll(toRemove);
mAdapter.notifyDataSetChanged();
mDataCount = mAdapter.getCount();
}
mode.finish();
break;
case R.id.action_select_all:
// check ALL items
for (int i = 0; i < mDataCount; i++) {
lv.setItemChecked(i, true);
}
// update the title and subtitle with number selected and numberBytes selected
onItemCheckedStateChanged(mode, 1, 0, true);
break;
}
return true;
}
// Deletes all files and subdirectories under given dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
private boolean deleteDir(File dir) {
String[] children = dir.list();
if (children != null) {
for (int i=0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
public void onDestroyActionMode(ActionMode mode) {
// This block intentionally left blank
}
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
boolean checked) {
ListView lv = getListView();
int numChecked = lv.getCheckedItemCount();
mode.setTitle(String.format(mNumSelectedFormat, numChecked, mAdapter.getCount()));
// total the sizes of all items selected so far
SparseBooleanArray checkedItems = lv.getCheckedItemPositions();
long selectedDataSize = 0;
if (numChecked > 0) {
for (int i = 0; i < mDataCount; i++) {
if (checkedItems.get(i)) {
// item is checked
selectedDataSize += mAdapter.getItem(i).mSize;
}
}
}
mode.setSubtitle(String.format(mNumBytesSelectedFormat,
Formatter.formatFileSize(mContext, selectedDataSize),
Formatter.formatFileSize(mContext, mAdapter.getDataSize())));
}
}
class MemoryMearurementAdapter extends BaseAdapter {
private ArrayList<StorageMeasurement.FileInfo> mData = null;
private long mDataSize = 0;
private Context mContext;
public MemoryMearurementAdapter(Activity activity) {
mContext = activity;
final StorageVolume storageVolume = activity.getIntent().getParcelableExtra(
StorageVolume.EXTRA_STORAGE_VOLUME);
StorageMeasurement mMeasurement = StorageMeasurement.getInstance(
activity, storageVolume);
if (mMeasurement == null) return;
mData = (ArrayList<StorageMeasurement.FileInfo>) mMeasurement.mFileInfoForMisc;
if (mData != null) {
for (StorageMeasurement.FileInfo info : mData) {
mDataSize += info.mSize;
}
}
}
@Override
public int getCount() {
return (mData == null) ? 0 : mData.size();
}
@Override
public StorageMeasurement.FileInfo getItem(int position) {
if (mData == null || mData.size() <= position) {
return null;
}
return mData.get(position);
}
@Override
public long getItemId(int position) {
if (mData == null || mData.size() <= position) {
return 0;
}
return mData.get(position).mId;
}
public void removeAll(List<Object> objs) {
if (mData == null) {
return;
}
for (Object o : objs) {
mData.remove(o);
mDataSize -= ((StorageMeasurement.FileInfo) o).mSize;
}
}
public long getDataSize() {
return mDataSize;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final FileItemInfoLayout view = (convertView == null) ?
(FileItemInfoLayout) mInflater.inflate(R.layout.settings_storage_miscfiles,
parent, false) : (FileItemInfoLayout) convertView;
FileInfo item = getItem(position);
view.setFileName(item.mFileName);
view.setFileSize(Formatter.formatFileSize(mContext, item.mSize));
final ListView listView = (ListView) parent;
final int listPosition = position;
view.getCheckBox().setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listView.setItemChecked(listPosition, isChecked);
}
});
view.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (listView.getCheckedItemCount() > 0) {
return false;
}
listView.setItemChecked(listPosition, !view.isChecked());
return true;
}
});
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (listView.getCheckedItemCount() > 0) {
listView.setItemChecked(listPosition, !view.isChecked());
}
}
});
return view;
}
}
}
|