1
1
package ca .ilanguage .rhok .imageupload .ui ;
2
2
3
3
import java .io .File ;
4
- import java .io .FileNotFoundException ;
5
4
import java .util .ArrayList ;
6
- import java .util .Collections ;
7
5
import java .util .List ;
8
6
import java .util .UUID ;
9
7
10
8
import ca .ilanguage .rhok .imageupload .App ;
11
9
import ca .ilanguage .rhok .imageupload .PetriFilmProcessingIntentService ;
12
10
import ca .ilanguage .rhok .imageupload .R ;
11
+ import ca .ilanguage .rhok .imageupload .Sample ;
12
+ import android .app .AlertDialog ;
13
13
import android .app .Dialog ;
14
14
import android .app .ListActivity ;
15
15
import android .app .ProgressDialog ;
16
+ import android .content .Context ;
17
+ import android .content .DialogInterface ;
16
18
import android .content .Intent ;
17
19
import android .net .Uri ;
18
20
import android .os .AsyncTask ;
19
21
import android .os .Bundle ;
22
+ import android .os .Handler ;
20
23
import android .util .Log ;
24
+ import android .view .LayoutInflater ;
21
25
import android .view .View ;
26
+ import android .view .ViewGroup ;
22
27
import android .widget .ArrayAdapter ;
28
+ import android .widget .EditText ;
23
29
import android .widget .ListView ;
30
+ import android .widget .TextView ;
24
31
import android .widget .Toast ;
25
32
26
33
public class SampleListActivity extends ListActivity {
27
34
private static final String TAG = "ca.ilanguage.rhok" ;
28
35
public static final int POPULATING_DIALOG = 54 ;
29
36
30
- static String [] samples = new String [] { "Water Source #1" ,
31
- "Street #3 Sample" };
37
+ List <Sample > samples ;
32
38
33
39
static int PETRI_IMAGE_REQUEST = 1 ;
34
- static int PROCESS_IMAGE_REQUEST = 2 ;
40
+ // static int PROCESS_IMAGE_REQUEST = 2;
35
41
36
42
/** Called when the activity is first created. */
37
43
@ Override
38
44
public void onCreate (Bundle savedInstanceState ) {
39
45
super .onCreate (savedInstanceState );
40
- setContentView (R .layout .samplelist );
41
- setListAdapter (new ArrayAdapter <String >(this ,
42
- android .R .layout .simple_list_item_1 , samples ));
43
- new PopulateSamplesListTask ().execute (null );
44
-
46
+ setContentView (R .layout .sample_list_activity );
47
+ refreshList ();
45
48
}
46
49
47
50
public void onNewSampleClick (View v ) {
48
- Intent intent = new Intent (this , PetrifilmCameraActivity .class );
49
- String guid = UUID .randomUUID ().toString ();
50
- intent .putExtra ("filename" , "petri_" + guid + ".jpg" );
51
- startActivityForResult (intent , PETRI_IMAGE_REQUEST );
51
+ AlertDialog .Builder alert = new AlertDialog .Builder (this );
52
+
53
+ alert .setTitle ("New Sample" );
54
+ alert .setMessage ("Enter sample name" );
55
+
56
+ // Set an EditText view to get user input
57
+ final EditText input = new EditText (this );
58
+ alert .setView (input );
59
+
60
+ alert .setPositiveButton ("OK" , new DialogInterface .OnClickListener () {
61
+ public void onClick (DialogInterface dialog , int whichButton ) {
62
+ String name = input .getText ().toString ();
63
+ Intent intent = new Intent (SampleListActivity .this ,
64
+ PetrifilmCameraActivity .class );
65
+ intent .putExtra ("filename" , name + ".jpg" );
66
+ startActivityForResult (intent , PETRI_IMAGE_REQUEST );
67
+ }
68
+ });
69
+
70
+ alert .setNegativeButton ("Cancel" ,
71
+ new DialogInterface .OnClickListener () {
72
+ public void onClick (DialogInterface dialog , int whichButton ) {
73
+ // Canceled.
74
+ }
75
+ });
76
+
77
+ alert .show ();
52
78
}
53
79
80
+ void refreshList ()
81
+ {
82
+ final Handler handler =new Handler ();
83
+ final Runnable r = new Runnable ()
84
+ {
85
+ public void run ()
86
+ {
87
+ new PopulateSamplesListTask ().execute ();
88
+ handler .postDelayed (this , 1000 );
89
+ }
90
+ };
91
+
92
+ handler .post (r );
93
+ }
94
+
54
95
@ Override
55
96
protected void onListItemClick (ListView l , View v , int position , long id ) {
56
- Intent intent = new Intent (this , SampleDetailsActivity .class );
57
- intent .putExtra ("filename" , samples [position ].replace ("Pending - " , "" ).replace ("Processed - " , "" )+".jpg" );
58
- Log .d (TAG , "Showing processed image " +samples [position ]);
59
- startActivity (intent );
97
+ Sample sample = samples .get (position );
98
+ if (sample .processed ) {
99
+ Intent intent = new Intent (this , SampleDetailsActivity .class );
100
+ intent .putExtra ("name" , sample .name );
101
+ Log .d (TAG , "Showing processed image " + samples .get (position ).name );
102
+ startActivity (intent );
103
+ } else {
104
+ Toast .makeText (this , "Still processing..." , Toast .LENGTH_SHORT ).show ();
105
+ }
60
106
}
61
107
62
108
@ Override
@@ -72,48 +118,55 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
72
118
intent .putExtra ("outimage" , filename );
73
119
Log .d (TAG , "Calling process image" );
74
120
startService (intent );
75
- new PopulateSamplesListTask ().execute (null );
121
+ new PopulateSamplesListTask ().execute ();
76
122
}
77
123
78
- if (requestCode == PROCESS_IMAGE_REQUEST && resultCode == RESULT_OK ) {
79
- Log .d (TAG , "Called process image" );
80
- String outpath = data .getStringExtra ("outpath" );
81
-
82
- // Launch image viewer
83
- Intent intent = new Intent ();
84
- intent .setAction (Intent .ACTION_VIEW );
85
- intent .setDataAndType (Uri .parse ("file://" + outpath ), "image/*" );
86
- startActivity (intent );
87
- }
124
+ // if (requestCode == PROCESS_IMAGE_REQUEST && resultCode == RESULT_OK) {
125
+ // Log.d(TAG, "Called process image");
126
+ // String outpath = data.getStringExtra("outpath");
127
+ //
128
+ // // Launch image viewer
129
+ // Intent intent = new Intent();
130
+ // intent.setAction(Intent.ACTION_VIEW);
131
+ // intent.setDataAndType(Uri.parse("file://" + outpath), "image/*");
132
+ // startActivity(intent);
133
+ // }
88
134
}
89
135
90
136
public class PopulateSamplesListTask extends AsyncTask <Void , Void , Boolean > {
91
137
@ Override
92
138
protected Boolean doInBackground (Void ... params ) {
93
139
File dir = new File (
94
140
App .getOriginalImageFolder (getApplicationContext ()));
95
- samples = dir .list ();
96
- for (int s = 0 ; s <samples .length ; s ++){
97
- String status = "Pending - " ;
98
- File results = new File (App .getResultsFolder (getApplicationContext ()) + File .separator + samples [s ].replace (".jpg" , ".xml" ));
99
- if (results .exists ()){
100
- status = "Processed - " ;
101
- }
102
- samples [s ] = status + samples [s ].replace (".jpg" , "" );
141
+ String [] sampleFiles = dir .list ();
142
+ samples = new ArrayList <Sample >();
143
+ for (int s = 0 ; s < sampleFiles .length ; s ++) {
144
+ // Trim .jpg
145
+ Sample sample = new Sample ();
146
+ sample .name = sampleFiles [s ].substring (0 ,
147
+ sampleFiles [s ].length () - 4 );
148
+ File results = new File (
149
+ App .getResultsFolder (getApplicationContext ()),
150
+ sample .name + ".xml" );
151
+ if (results .exists ())
152
+ sample .processed = true ;
153
+
154
+ samples .add (sample );
103
155
}
104
156
return true ;
105
157
}
106
158
107
159
protected void onPreExecute () {
108
- showDialog (POPULATING_DIALOG );
160
+ // Temporarily removing for auto-refresh. put back in when done by intent
161
+ //showDialog(POPULATING_DIALOG);
109
162
}
110
163
111
164
protected void onPostExecute (Boolean result ) {
112
- dismissDialog (POPULATING_DIALOG );
113
- setListAdapter (new ArrayAdapter <String >(getApplicationContext (),
114
- android .R .layout .simple_list_item_1 , samples ));
165
+ // Temporarily removing for auto-refresh. put back in when done by intent
166
+ //dismissDialog(POPULATING_DIALOG);
167
+ setListAdapter (new SampleAdapter (SampleListActivity .this ,
168
+ R .layout .sample_list_row , samples ));
115
169
}
116
-
117
170
}
118
171
119
172
@ Override
@@ -130,4 +183,36 @@ protected Dialog onCreateDialog(int id) {
130
183
}
131
184
return dialog ;
132
185
}
186
+
187
+ class SampleAdapter extends ArrayAdapter <Sample > {
188
+ private List <Sample > items ;
189
+
190
+ public SampleAdapter (Context context , int textViewResourceId ,
191
+ List <Sample > samples ) {
192
+ super (context , textViewResourceId , samples );
193
+ this .items = samples ;
194
+ }
195
+
196
+ @ Override
197
+ public View getView (int position , View convertView , ViewGroup parent ) {
198
+ View v = convertView ;
199
+ if (v == null ) {
200
+ LayoutInflater vi = (LayoutInflater ) getSystemService (Context .LAYOUT_INFLATER_SERVICE );
201
+ v = vi .inflate (R .layout .sample_list_row , null );
202
+ }
203
+ Sample o = items .get (position );
204
+ if (o != null ) {
205
+ TextView tt = (TextView ) v .findViewById (R .id .name );
206
+ TextView bt = (TextView ) v .findViewById (R .id .state );
207
+ if (tt != null ) {
208
+ tt .setText ("Name: " + o .name );
209
+ }
210
+ if (bt != null ) {
211
+ bt .setText ("Status: "
212
+ + (o .processed ? "Processed" : "Pending" ));
213
+ }
214
+ }
215
+ return v ;
216
+ }
217
+ }
133
218
}
0 commit comments