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