/[projects]/android/RescanSdcard/src/dk/thoerup/rescansdcard/RescanMain.java
ViewVC logotype

Contents of /android/RescanSdcard/src/dk/thoerup/rescansdcard/RescanMain.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 787 - (show annotations) (download)
Wed Jun 2 16:13:01 2010 UTC (13 years, 11 months ago) by torben
File size: 5166 byte(s)
override back button while scanning
1 package dk.thoerup.rescansdcard;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import android.app.Activity;
8 import android.app.AlertDialog;
9 import android.content.DialogInterface;
10 import android.media.MediaScannerConnection;
11 import android.media.MediaScannerConnection.MediaScannerConnectionClient;
12 import android.net.Uri;
13 import android.os.AsyncTask;
14 import android.os.Bundle;
15 import android.os.Environment;
16 import android.os.Handler;
17 import android.os.Message;
18 import android.util.Log;
19 import android.view.KeyEvent;
20 import android.view.View;
21 import android.view.View.OnClickListener;
22 import android.widget.Button;
23 import android.widget.ProgressBar;
24 import android.widget.Spinner;
25 import android.widget.TextView;
26
27 public class RescanMain extends Activity {
28 final static String TAG = "RescanSdcard";
29
30
31 Button scanBtn;
32 TextView totalTv;
33 TextView processedTv;
34 Spinner targetSpinner;
35
36 ProgressBar progress;
37
38 MediaScannerConnection scan;
39
40 int total = 0;
41
42 int processed = 0;
43
44 boolean isScanning = false;
45
46
47 /** Called when the activity is first created. */
48 @Override
49 public void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 Log.i(TAG, "onCreate");
52
53 setContentView(R.layout.main);
54 scanBtn = (Button) findViewById(R.id.scan);
55 scanBtn.setOnClickListener( new ScanListener() );
56
57 totalTv = (TextView) findViewById(R.id.total);
58 processedTv = (TextView) findViewById(R.id.processed);
59
60 targetSpinner = (Spinner) findViewById(R.id.target);
61 progress = (ProgressBar) findViewById(R.id.progress);
62
63
64 checkSdCard();
65
66
67 }
68
69
70
71 @Override
72 public boolean onKeyDown(int keyCode, KeyEvent event) {
73 if (keyCode == KeyEvent.KEYCODE_BACK) {
74 if (isScanning == true) { //scanning scanning
75 return true; //do nothing and claim event to be handled
76 }
77 }
78
79 return super.onKeyDown(keyCode, event);
80 }
81
82
83
84 void populateSpinner() {
85
86 }
87
88 boolean checkSdCard() {
89 //check whether sdcard is mounted
90 if ( ! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
91 AlertDialog.Builder b = new AlertDialog.Builder(this);
92
93 b.setMessage("SD-card is not currently mounted");
94 b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
95 @Override
96 public void onClick(DialogInterface dialog, int which) {
97 RescanMain.this.finish();
98 }
99 });
100 b.show();
101 return false;
102 } else {
103 return true;
104 }
105 }
106
107
108 Handler updateHandler = new Handler() {
109 @Override
110 public void handleMessage(Message msg) {
111 super.handleMessage(msg);
112
113 totalTv.setText("" + total);
114 processedTv.setText("" + processed);
115
116 progress.setMax(total);
117 progress.setProgress(processed);
118
119 if (processed == total) {
120 isScanning = false;
121 scanBtn.setEnabled(true);
122 targetSpinner.setEnabled(true);
123 } else {
124 updateHandler.sendEmptyMessageDelayed(0, 500);
125 }
126 }
127 };
128
129
130 void findFilesRecursive(String path, List<String> files) {
131 Log.i(TAG, "scanRecursive " + path);
132
133 File dir = new File(path);
134
135 String entries[] = dir.list();
136
137 File nomedia = new File(dir.getPath() + "/.nomedia" );
138 if (nomedia.exists()) {
139 Log.e(TAG, nomedia.getPath() + " exists, ignoring folder");
140 return;
141 }
142
143
144 for (String entry : entries) {
145
146 if (entry.charAt(0) == '.') {
147 Log.e(TAG, "ignore " + entry);
148 continue;
149 }
150
151 File current = new File(dir.getPath() + "/" + entry);
152 //Log.v(TAG, current.getPath());
153
154
155 if (current.isDirectory()) {
156 findFilesRecursive(current.getPath(), files);
157 } else {
158 files.add(current.getPath());
159 }
160 }
161 }
162
163 class FileEnqueuer extends AsyncTask<Void, Void, Void> {
164
165 @Override
166 protected Void doInBackground(Void... arg0) {
167
168 List<String> files = new ArrayList<String>();
169
170 findFilesRecursive(Environment.getExternalStorageDirectory().getPath(), files);
171
172 total = files.size();
173 updateHandler.sendEmptyMessage(0);
174
175 for (String f : files) {
176 scan.scanFile(f, null);
177 }
178
179 scan.disconnect();
180 scan = null;
181
182 files.clear();
183
184 return null;
185 }
186
187 }
188
189
190 class ScanListener implements OnClickListener {
191 @Override
192 public void onClick(View arg0) {
193 if (checkSdCard() == true) {
194 total = 0;
195 processed = 0;
196 isScanning = true;
197
198 scan = new MediaScannerConnection(RescanMain.this, new ScannerClient() );
199 scan.connect();
200
201 scanBtn.setEnabled(false);
202 targetSpinner.setEnabled(false);
203 }
204 }
205 }
206
207 class ScannerClient implements MediaScannerConnectionClient {
208
209 @Override
210 public void onMediaScannerConnected() {
211 Log.i(TAG, "scannerConnected");
212
213 new FileEnqueuer().execute();
214 }
215
216 @Override
217 public void onScanCompleted(String path, Uri uri) {
218 //Log.v(TAG, "scanCompleted " + path + " " + uri);
219 processed++;
220 }
221 }
222 }

  ViewVC Help
Powered by ViewVC 1.1.20