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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 789 - (hide annotations) (download)
Thu Jun 3 07:21:14 2010 UTC (14 years ago) by torben
File size: 5290 byte(s)
Comment on this methods inefficiency
1 torben 789 /*
2     * this is a inefficient and ugly hack - the correct way is to send a ACTION_MEDIA_MOUNTED broadcast
3     */
4    
5 torben 784 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 torben 787 import android.view.KeyEvent;
24 torben 784 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 torben 789 @Deprecated
32 torben 784 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 torben 787 boolean isScanning = false;
50 torben 784
51 torben 787
52 torben 784 /** 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 torben 786
71    
72 torben 784 }
73    
74 torben 787
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 torben 784
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 torben 787 isScanning = false;
126 torben 784 scanBtn.setEnabled(true);
127     targetSpinner.setEnabled(true);
128 torben 786 } else {
129     updateHandler.sendEmptyMessageDelayed(0, 500);
130 torben 784 }
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 torben 786 updateHandler.sendEmptyMessage(0);
179    
180 torben 784 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 torben 787 isScanning = true;
202 torben 784
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     }

  ViewVC Help
Powered by ViewVC 1.1.20