/[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 786 - (hide annotations) (download)
Wed Jun 2 15:59:06 2010 UTC (14 years ago) by torben
File size: 4769 byte(s)
update the gui on a time based schedule
1 torben 784 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 torben 786
63    
64 torben 784 }
65    
66     void populateSpinner() {
67    
68     }
69    
70     boolean checkSdCard() {
71     //check whether sdcard is mounted
72     if ( ! Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ) {
73     AlertDialog.Builder b = new AlertDialog.Builder(this);
74    
75     b.setMessage("SD-card is not currently mounted");
76     b.setPositiveButton("ok", new DialogInterface.OnClickListener() {
77     @Override
78     public void onClick(DialogInterface dialog, int which) {
79     RescanMain.this.finish();
80     }
81     });
82     b.show();
83     return false;
84     } else {
85     return true;
86     }
87     }
88    
89    
90     Handler updateHandler = new Handler() {
91     @Override
92     public void handleMessage(Message msg) {
93     super.handleMessage(msg);
94    
95     totalTv.setText("" + total);
96     processedTv.setText("" + processed);
97    
98     progress.setMax(total);
99     progress.setProgress(processed);
100    
101     if (processed == total) {
102     scanBtn.setEnabled(true);
103     targetSpinner.setEnabled(true);
104 torben 786 } else {
105     updateHandler.sendEmptyMessageDelayed(0, 500);
106 torben 784 }
107     }
108     };
109    
110    
111     void findFilesRecursive(String path, List<String> files) {
112     Log.i(TAG, "scanRecursive " + path);
113    
114     File dir = new File(path);
115    
116     String entries[] = dir.list();
117    
118     File nomedia = new File(dir.getPath() + "/.nomedia" );
119     if (nomedia.exists()) {
120     Log.e(TAG, nomedia.getPath() + " exists, ignoring folder");
121     return;
122     }
123    
124    
125     for (String entry : entries) {
126    
127     if (entry.charAt(0) == '.') {
128     Log.e(TAG, "ignore " + entry);
129     continue;
130     }
131    
132     File current = new File(dir.getPath() + "/" + entry);
133     //Log.v(TAG, current.getPath());
134    
135    
136     if (current.isDirectory()) {
137     findFilesRecursive(current.getPath(), files);
138     } else {
139     files.add(current.getPath());
140     }
141     }
142     }
143    
144     class FileEnqueuer extends AsyncTask<Void, Void, Void> {
145    
146     @Override
147     protected Void doInBackground(Void... arg0) {
148    
149     List<String> files = new ArrayList<String>();
150    
151     findFilesRecursive(Environment.getExternalStorageDirectory().getPath(), files);
152    
153     total = files.size();
154 torben 786 updateHandler.sendEmptyMessage(0);
155    
156 torben 784 for (String f : files) {
157     scan.scanFile(f, null);
158     }
159    
160     scan.disconnect();
161     scan = null;
162    
163     files.clear();
164    
165     return null;
166     }
167    
168     }
169    
170    
171     class ScanListener implements OnClickListener {
172     @Override
173     public void onClick(View arg0) {
174     if (checkSdCard() == true) {
175     total = 0;
176     processed = 0;
177    
178     scan = new MediaScannerConnection(RescanMain.this, new ScannerClient() );
179     scan.connect();
180    
181     scanBtn.setEnabled(false);
182     targetSpinner.setEnabled(false);
183     }
184     }
185     }
186    
187     class ScannerClient implements MediaScannerConnectionClient {
188    
189     @Override
190     public void onMediaScannerConnected() {
191     Log.i(TAG, "scannerConnected");
192    
193     new FileEnqueuer().execute();
194     }
195    
196     @Override
197     public void onScanCompleted(String path, Uri uri) {
198     //Log.v(TAG, "scanCompleted " + path + " " + uri);
199     processed++;
200     }
201     }
202     }

  ViewVC Help
Powered by ViewVC 1.1.20