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.View; |
20 |
import android.view.View.OnClickListener; |
21 |
import android.widget.Button; |
22 |
import android.widget.ProgressBar; |
23 |
import android.widget.Spinner; |
24 |
import android.widget.TextView; |
25 |
|
26 |
public class RescanMain extends Activity { |
27 |
final static String TAG = "RescanSdcard"; |
28 |
|
29 |
|
30 |
Button scanBtn; |
31 |
TextView totalTv; |
32 |
TextView processedTv; |
33 |
Spinner targetSpinner; |
34 |
|
35 |
ProgressBar progress; |
36 |
|
37 |
MediaScannerConnection scan; |
38 |
|
39 |
int total = 0; |
40 |
|
41 |
int processed = 0; |
42 |
|
43 |
|
44 |
/** Called when the activity is first created. */ |
45 |
@Override |
46 |
public void onCreate(Bundle savedInstanceState) { |
47 |
super.onCreate(savedInstanceState); |
48 |
Log.i(TAG, "onCreate"); |
49 |
|
50 |
setContentView(R.layout.main); |
51 |
scanBtn = (Button) findViewById(R.id.scan); |
52 |
scanBtn.setOnClickListener( new ScanListener() ); |
53 |
|
54 |
totalTv = (TextView) findViewById(R.id.total); |
55 |
processedTv = (TextView) findViewById(R.id.processed); |
56 |
|
57 |
targetSpinner = (Spinner) findViewById(R.id.target); |
58 |
progress = (ProgressBar) findViewById(R.id.progress); |
59 |
|
60 |
|
61 |
checkSdCard(); |
62 |
} |
63 |
|
64 |
void populateSpinner() { |
65 |
|
66 |
} |
67 |
|
68 |
boolean checkSdCard() { |
69 |
//check whether sdcard is mounted |
70 |
if ( ! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) { |
71 |
AlertDialog.Builder b = new AlertDialog.Builder(this); |
72 |
|
73 |
b.setMessage("SD-card is not currently mounted"); |
74 |
b.setPositiveButton("ok", new DialogInterface.OnClickListener() { |
75 |
@Override |
76 |
public void onClick(DialogInterface dialog, int which) { |
77 |
RescanMain.this.finish(); |
78 |
} |
79 |
}); |
80 |
b.show(); |
81 |
return false; |
82 |
} else { |
83 |
return true; |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
Handler updateHandler = new Handler() { |
89 |
@Override |
90 |
public void handleMessage(Message msg) { |
91 |
super.handleMessage(msg); |
92 |
|
93 |
totalTv.setText("" + total); |
94 |
processedTv.setText("" + processed); |
95 |
|
96 |
progress.setMax(total); |
97 |
progress.setProgress(processed); |
98 |
|
99 |
if (processed == total) { |
100 |
scanBtn.setEnabled(true); |
101 |
targetSpinner.setEnabled(true); |
102 |
} |
103 |
} |
104 |
}; |
105 |
|
106 |
|
107 |
void findFilesRecursive(String path, List<String> files) { |
108 |
Log.i(TAG, "scanRecursive " + path); |
109 |
|
110 |
File dir = new File(path); |
111 |
|
112 |
String entries[] = dir.list(); |
113 |
|
114 |
File nomedia = new File(dir.getPath() + "/.nomedia" ); |
115 |
if (nomedia.exists()) { |
116 |
Log.e(TAG, nomedia.getPath() + " exists, ignoring folder"); |
117 |
return; |
118 |
} |
119 |
|
120 |
|
121 |
for (String entry : entries) { |
122 |
|
123 |
if (entry.charAt(0) == '.') { |
124 |
Log.e(TAG, "ignore " + entry); |
125 |
continue; |
126 |
} |
127 |
|
128 |
File current = new File(dir.getPath() + "/" + entry); |
129 |
//Log.v(TAG, current.getPath()); |
130 |
|
131 |
|
132 |
if (current.isDirectory()) { |
133 |
findFilesRecursive(current.getPath(), files); |
134 |
} else { |
135 |
files.add(current.getPath()); |
136 |
} |
137 |
} |
138 |
} |
139 |
|
140 |
class FileEnqueuer extends AsyncTask<Void, Void, Void> { |
141 |
|
142 |
@Override |
143 |
protected Void doInBackground(Void... arg0) { |
144 |
|
145 |
List<String> files = new ArrayList<String>(); |
146 |
|
147 |
findFilesRecursive(Environment.getExternalStorageDirectory().getPath(), files); |
148 |
|
149 |
|
150 |
total = files.size(); |
151 |
for (String f : files) { |
152 |
scan.scanFile(f, null); |
153 |
} |
154 |
|
155 |
scan.disconnect(); |
156 |
scan = null; |
157 |
|
158 |
files.clear(); |
159 |
|
160 |
return null; |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
|
166 |
class ScanListener implements OnClickListener { |
167 |
@Override |
168 |
public void onClick(View arg0) { |
169 |
if (checkSdCard() == true) { |
170 |
total = 0; |
171 |
processed = 0; |
172 |
|
173 |
scan = new MediaScannerConnection(RescanMain.this, new ScannerClient() ); |
174 |
scan.connect(); |
175 |
|
176 |
scanBtn.setEnabled(false); |
177 |
targetSpinner.setEnabled(false); |
178 |
} |
179 |
} |
180 |
} |
181 |
|
182 |
class ScannerClient implements MediaScannerConnectionClient { |
183 |
|
184 |
@Override |
185 |
public void onMediaScannerConnected() { |
186 |
Log.i(TAG, "scannerConnected"); |
187 |
|
188 |
new FileEnqueuer().execute(); |
189 |
} |
190 |
|
191 |
@Override |
192 |
public void onScanCompleted(String path, Uri uri) { |
193 |
//Log.v(TAG, "scanCompleted " + path + " " + uri); |
194 |
processed++; |
195 |
updateHandler.sendEmptyMessage(0); |
196 |
} |
197 |
} |
198 |
} |