4242public class MapSelectActivity extends Activity {
4343 // Manual selection variables
4444 private Spinner mapSpinner ,campusSpinner ;
45- private ArrayAdapter <String > mapArrayAdapter , campusArrayAdapter ;
45+ private ArrayAdapter <String > mapArrayAdapter ;
4646 private ArrayList <String > maplist ;
4747 private MapService mapService ;
4848 private Intent mapIntent ;
4949 private PendingIntent pendingIntent ;
50- public static boolean ActivityDestryoed ;
50+ // public static boolean ActivityDestryoed;
5151 private ArrayList <String > campusNames ;
5252
5353 // Auto-location variables
@@ -83,31 +83,144 @@ protected void onDestroy() {
8383 pendingIntent .cancel ();
8484 }
8585
86+ @ Override
87+ protected void onResume (){
88+ super .onResume ();
89+
90+ }
91+
8692 @ Override
8793 protected void onCreate (Bundle savedInstanceState ) {
8894 super .onCreate (savedInstanceState );
89- setTitle ("Welcome to Navatar" );
95+
96+ setTitle (R .string .welcome_to_navatar );
9097 setContentView (R .layout .map_select );
9198
99+ // Auto-locate ui items
100+ autoLocateButton = (Button ) findViewById (R .id .button );
101+ getQrsCodeButton = (Button ) findViewById (R .id .qrButton );
102+ spinner = (ProgressBar )findViewById (R .id .progressBar );
103+
104+ // Verify storage for route history
105+ verifyStoragePermissions (this );
106+
107+ setupCampuses ();
108+
109+ setupMapService ();
110+
111+ configureQrCodeButton ();
112+
113+ // Setup location manager for auto-location
114+ configureLocationManager ();
115+
116+ // Setup autoLocateButton
117+ configureAutoLocateButton ();
118+
119+ }
120+
121+ @ Override
122+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
123+ switch (requestCode ) {
124+ // Location permission
125+ case 10 :
126+ // Accepted
127+ if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
128+ // Click autoLocateButton again now that we have location permission
129+ autoLocateButton .performClick ();
130+ }
131+ // Denied
132+ else {
133+ // Inform user we need permissions
134+ Toast .makeText (getBaseContext (), "Location permission is required for auto-locating." ,
135+ Toast .LENGTH_LONG ).show ();
136+ }
137+
138+ break ;
139+ }
140+ }
141+
142+ @ Override
143+ protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
144+ try {
145+
146+ // Get the results of the QR Code scanner
147+ IntentResult result = IntentIntegrator .parseActivityResult (requestCode , resultCode , data );
148+
149+ if (result != null ) {
150+ if (result .getContents () == null ) {
151+ Toast .makeText (this , R .string .cancelled , Toast .LENGTH_LONG ).show ();
152+ } else {
153+ Toast .makeText (this , "Scanned: " + result .getContents (), Toast .LENGTH_LONG ).show ();
154+ }
155+ } else {
156+ super .onActivityResult (requestCode , resultCode , data );
157+ }
158+
159+ maplist .clear ();
160+ maplist .add ("Select a building" );
161+
162+ // If maps loaded for selected campus
163+ if (data .hasExtra ("maps" )) {
164+ // Add maps to spinner
165+ maplist .addAll ((ArrayList <String >) data .getSerializableExtra ("maps" ));
166+
167+ // If we have a location with high enough accuracy (campus was auto selected)
168+ // and building geofences were loaded
169+ if (CampusAutoSelected && data .hasExtra ("geofences" )) {
170+ // Get building geofences and convert back to json array
171+ String geofencesString = data .getStringExtra ("geofences" );
172+ JSONArray buildingGeofences = new JSONArray (geofencesString );
173+
174+ // Send in location, get out name of building if supported or null
175+ String foundBuilding = checkIfLocationIsInsideJSONGeofence (buildingGeofences );
176+
177+ // If location in supported building
178+ if (foundBuilding != null ) {
179+ // Replace underscores with spaces, MapService does this for mapList
180+ foundBuilding = foundBuilding .replaceAll ("_" ," " );
181+
182+ // Get index of located building name for spinner selection
183+ for (int i = 1 ; i < maplist .size (); i ++) { // skip building label at 0
184+ if (maplist .get (i ).equals (foundBuilding )) {
185+ // Select building
186+ mapSpinner .setSelection (i );
187+ }
188+ }
189+ }
190+ // Not found in supported building
191+ else {
192+ // Assuming location is inaccurate
193+ Toast .makeText (getBaseContext (), "Location is not accurate enough to auto-select building." ,
194+ Toast .LENGTH_LONG ).show ();
195+ }
196+ }
197+ }
198+ pendingIntent = null ;
199+ } catch (Exception e ) {
200+ e .printStackTrace ();
201+ }
202+ }
203+
204+ private void setupMapService () {
92205 mapIntent = new Intent (this , MapService .class );
206+ maplist = new ArrayList <String >();
207+ maplist .add (0 ,"Select Building" );
208+ mapArrayAdapter = new ArrayAdapter <String >(this , android .R .layout .simple_spinner_dropdown_item ,
209+ maplist );
210+ startService (mapIntent );
211+ bindService (mapIntent , mMapConnection , BIND_AUTO_CREATE );
212+ }
93213
214+ private void setupCampuses () {
94215 campusNames = new ArrayList <String >();
95216 campusSpinner = (Spinner )findViewById (R .id .campusSpinner );
96- campusArrayAdapter = new ArrayAdapter <String >(this , android .R .layout .simple_spinner_dropdown_item ,
217+ ArrayAdapter < String > campusArrayAdapter = new ArrayAdapter <String >(this , android .R .layout .simple_spinner_dropdown_item ,
97218 new ArrayList <String >());
98219 campusArrayAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
99220 ArrayList <String > campuslist = new ArrayList <String >();
100221
101222 CampusAutoSelected = false ;
102223
103- // Verify storage for route history
104- verifyStoragePermissions (this );
105-
106- // Auto-locate ui items
107- autoLocateButton = (Button ) findViewById (R .id .button );
108- getQrsCodeButton = (Button ) findViewById (R .id .qrButton );
109- spinner = (ProgressBar )findViewById (R .id .progressBar );
110-
111224 try {
112225 // Get campus files
113226 String [] campusFiles = getAssets ().list ("maps" );
@@ -124,27 +237,12 @@ protected void onCreate(Bundle savedInstanceState) {
124237 campusNames .add (campusFile );
125238 }
126239 }
127-
128240 // Load campus geofences
129241 loadCampusGeofencesJSONFromAsset ();
130242
131- // Setup location manager for auto-location
132- configureLocationManager ();
133-
134- // Setup autoLocateButton
135- configureAutoLocateButton ();
136-
137- configureQrCodeButton ();
138-
139243 campusArrayAdapter .addAll (campuslist );
140244 campusSpinner .setAdapter (campusArrayAdapter );
141245 campusSpinner .setOnItemSelectedListener (campusSpinnerSelected );
142- maplist = new ArrayList <String >();
143- maplist .add (0 ,"Select Building" );
144- mapArrayAdapter = new ArrayAdapter <String >(this , android .R .layout .simple_spinner_dropdown_item ,
145- maplist );
146- startService (mapIntent );
147- bindService (mapIntent , mMapConnection , BIND_AUTO_CREATE );
148246 }
149247 catch (IOException e ) {
150248 e .printStackTrace ();
@@ -287,27 +385,6 @@ public void onProviderDisabled(String provider) {
287385 };
288386 }
289387
290- @ Override
291- public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
292- switch (requestCode ) {
293- // Location permission
294- case 10 :
295- // Accepted
296- if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
297- // Click autoLocateButton again now that we have location permission
298- autoLocateButton .performClick ();
299- }
300- // Denied
301- else {
302- // Inform user we need permissions
303- Toast .makeText (getBaseContext (), "Location permission is required for auto-locating." ,
304- Toast .LENGTH_LONG ).show ();
305- }
306-
307- break ;
308- }
309- }
310-
311388 private void configureAutoLocateButton (){
312389 // Setup autoLocateButton to request location
313390 autoLocateButton .setOnClickListener (new View .OnClickListener () {
@@ -357,12 +434,6 @@ public void onClick(View v) {
357434 });
358435 }
359436
360- @ Override
361- protected void onResume (){
362- super .onResume ();
363-
364- }
365-
366437 public OnItemSelectedListener campusSpinnerSelected = new OnItemSelectedListener () {
367438 @ Override
368439 public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
@@ -400,68 +471,6 @@ public void onNothingSelected(AdapterView<?> parent) {
400471 }
401472 };
402473
403- @ Override
404- protected void onActivityResult (int requestCode , int resultCode , Intent data ) {
405- try {
406-
407- // Get the results of the QR Code scanner
408- IntentResult result = IntentIntegrator .parseActivityResult (requestCode , resultCode , data );
409-
410- if (result != null ) {
411- if (result .getContents () == null ) {
412- Toast .makeText (this , "Cancelled" , Toast .LENGTH_LONG ).show ();
413- } else {
414- Toast .makeText (this , "Scanned: " + result .getContents (), Toast .LENGTH_LONG ).show ();
415- }
416- } else {
417- super .onActivityResult (requestCode , resultCode , data );
418- }
419-
420- maplist .clear ();
421- maplist .add ("Select a building" );
422-
423- // If maps loaded for selected campus
424- if (data .hasExtra ("maps" )) {
425- // Add maps to spinner
426- maplist .addAll ((ArrayList <String >) data .getSerializableExtra ("maps" ));
427-
428- // If we have a location with high enough accuracy (campus was auto selected)
429- // and building geofences were loaded
430- if (CampusAutoSelected && data .hasExtra ("geofences" )) {
431- // Get building geofences and convert back to json array
432- String geofencesString = data .getStringExtra ("geofences" );
433- JSONArray buildingGeofences = new JSONArray (geofencesString );
434-
435- // Send in location, get out name of building if supported or null
436- String foundBuilding = checkIfLocationIsInsideJSONGeofence (buildingGeofences );
437-
438- // If location in supported building
439- if (foundBuilding != null ) {
440- // Replace underscores with spaces, MapService does this for mapList
441- foundBuilding = foundBuilding .replaceAll ("_" ," " );
442-
443- // Get index of located building name for spinner selection
444- for (int i = 1 ; i < maplist .size (); i ++) { // skip building label at 0
445- if (maplist .get (i ).equals (foundBuilding )) {
446- // Select building
447- mapSpinner .setSelection (i );
448- }
449- }
450- }
451- // Not found in supported building
452- else {
453- // Assuming location is inaccurate
454- Toast .makeText (getBaseContext (), "Location is not accurate enough to auto-select building." ,
455- Toast .LENGTH_LONG ).show ();
456- }
457- }
458- }
459- pendingIntent = null ;
460- } catch (Exception e ) {
461- e .printStackTrace ();
462- }
463- }
464-
465474 public OnItemSelectedListener mapSpinnerItemSelected = new OnItemSelectedListener () {
466475 public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
467476 // If building is selected
0 commit comments