Skip to content

Commit

Permalink
refactoring of activities as per #3
Browse files Browse the repository at this point in the history
  • Loading branch information
chebizarro committed Nov 1, 2018
1 parent f318224 commit 4646092
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 144 deletions.
10 changes: 5 additions & 5 deletions app/src/main/assets/maps/Campus_Geofences.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
}
},
{
"name": "Future_Supported_Campus",
"name": "Evergreen_State_College",
"geofence": {
"minLatitude": -1,
"minLongitude": -1,
"maxLatitude": -1,
"maxLongitude": -1
"minLatitude": 47.07037,
"minLongitude": -122.98438,
"maxLatitude": 47.07601,
"maxLongitude": -122.96873
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"buildings": [
{
"name": "tesc_ad458c6d_b4f5_4a1f_8311_10e7bc6c5e3a",
"geofence": {
"minLatitude": 47.073002,
"minLongitude": -122.978612,
"maxLatitude": 47.074387,
"maxLongitude": -122.977103
}
}
]
}
Binary file not shown.
241 changes: 125 additions & 116 deletions app/src/main/java/com/navatar/MapSelectActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
public class MapSelectActivity extends Activity {
// Manual selection variables
private Spinner mapSpinner,campusSpinner;
private ArrayAdapter<String> mapArrayAdapter,campusArrayAdapter;
private ArrayAdapter<String> mapArrayAdapter;
private ArrayList<String> maplist;
private MapService mapService;
private Intent mapIntent;
private PendingIntent pendingIntent;
public static boolean ActivityDestryoed;
//public static boolean ActivityDestryoed;
private ArrayList<String> campusNames;

// Auto-location variables
Expand Down Expand Up @@ -83,31 +83,144 @@ protected void onDestroy() {
pendingIntent.cancel();
}

@Override
protected void onResume(){
super.onResume();

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Welcome to Navatar");

setTitle(R.string.welcome_to_navatar);
setContentView(R.layout.map_select);

// Auto-locate ui items
autoLocateButton = (Button) findViewById(R.id.button);
getQrsCodeButton = (Button) findViewById(R.id.qrButton);
spinner = (ProgressBar)findViewById(R.id.progressBar);

// Verify storage for route history
verifyStoragePermissions(this);

setupCampuses();

setupMapService();

configureQrCodeButton();

// Setup location manager for auto-location
configureLocationManager();

// Setup autoLocateButton
configureAutoLocateButton();

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
// Location permission
case 10:
// Accepted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Click autoLocateButton again now that we have location permission
autoLocateButton.performClick();
}
// Denied
else {
// Inform user we need permissions
Toast.makeText(getBaseContext(), "Location permission is required for auto-locating.",
Toast.LENGTH_LONG).show();
}

break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {

// Get the results of the QR Code scanner
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, R.string.cancelled, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}

maplist.clear();
maplist.add("Select a building");

// If maps loaded for selected campus
if(data.hasExtra("maps")) {
// Add maps to spinner
maplist.addAll((ArrayList<String>) data.getSerializableExtra("maps"));

// If we have a location with high enough accuracy (campus was auto selected)
// and building geofences were loaded
if (CampusAutoSelected && data.hasExtra("geofences")) {
// Get building geofences and convert back to json array
String geofencesString = data.getStringExtra("geofences");
JSONArray buildingGeofences = new JSONArray(geofencesString);

// Send in location, get out name of building if supported or null
String foundBuilding = checkIfLocationIsInsideJSONGeofence(buildingGeofences);

// If location in supported building
if (foundBuilding != null) {
// Replace underscores with spaces, MapService does this for mapList
foundBuilding = foundBuilding.replaceAll("_"," ");

// Get index of located building name for spinner selection
for (int i = 1; i < maplist.size(); i++) { // skip building label at 0
if (maplist.get(i).equals(foundBuilding)) {
// Select building
mapSpinner.setSelection(i);
}
}
}
// Not found in supported building
else {
// Assuming location is inaccurate
Toast.makeText(getBaseContext(), "Location is not accurate enough to auto-select building.",
Toast.LENGTH_LONG).show();
}
}
}
pendingIntent = null;
} catch (Exception e) {
e.printStackTrace();
}
}

private void setupMapService() {
mapIntent= new Intent(this, MapService.class);
maplist = new ArrayList<String>();
maplist.add(0,"Select Building");
mapArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
maplist);
startService(mapIntent);
bindService(mapIntent, mMapConnection, BIND_AUTO_CREATE);
}

private void setupCampuses() {
campusNames = new ArrayList<String>();
campusSpinner = (Spinner)findViewById(R.id.campusSpinner);
campusArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
ArrayAdapter<String> campusArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
new ArrayList<String>());
campusArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayList<String> campuslist = new ArrayList<String>();

CampusAutoSelected = false;

// Verify storage for route history
verifyStoragePermissions(this);

// Auto-locate ui items
autoLocateButton = (Button) findViewById(R.id.button);
getQrsCodeButton = (Button) findViewById(R.id.qrButton);
spinner = (ProgressBar)findViewById(R.id.progressBar);

try {
// Get campus files
String[] campusFiles = getAssets().list("maps");
Expand All @@ -124,27 +237,12 @@ protected void onCreate(Bundle savedInstanceState) {
campusNames.add(campusFile);
}
}

// Load campus geofences
loadCampusGeofencesJSONFromAsset();

// Setup location manager for auto-location
configureLocationManager();

// Setup autoLocateButton
configureAutoLocateButton();

configureQrCodeButton();

campusArrayAdapter.addAll(campuslist);
campusSpinner.setAdapter(campusArrayAdapter);
campusSpinner.setOnItemSelectedListener(campusSpinnerSelected);
maplist = new ArrayList<String>();
maplist.add(0,"Select Building");
mapArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
maplist);
startService(mapIntent);
bindService(mapIntent, mMapConnection, BIND_AUTO_CREATE);
}
catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -287,27 +385,6 @@ public void onProviderDisabled(String provider) {
};
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
// Location permission
case 10:
// Accepted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Click autoLocateButton again now that we have location permission
autoLocateButton.performClick();
}
// Denied
else {
// Inform user we need permissions
Toast.makeText(getBaseContext(), "Location permission is required for auto-locating.",
Toast.LENGTH_LONG).show();
}

break;
}
}

private void configureAutoLocateButton(){
// Setup autoLocateButton to request location
autoLocateButton.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -357,12 +434,6 @@ public void onClick(View v) {
});
}

@Override
protected void onResume(){
super.onResume();

}

public OnItemSelectedListener campusSpinnerSelected = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Expand Down Expand Up @@ -400,68 +471,6 @@ public void onNothingSelected(AdapterView<?> parent) {
}
};

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {

// Get the results of the QR Code scanner
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}

maplist.clear();
maplist.add("Select a building");

// If maps loaded for selected campus
if(data.hasExtra("maps")) {
// Add maps to spinner
maplist.addAll((ArrayList<String>) data.getSerializableExtra("maps"));

// If we have a location with high enough accuracy (campus was auto selected)
// and building geofences were loaded
if (CampusAutoSelected && data.hasExtra("geofences")) {
// Get building geofences and convert back to json array
String geofencesString = data.getStringExtra("geofences");
JSONArray buildingGeofences = new JSONArray(geofencesString);

// Send in location, get out name of building if supported or null
String foundBuilding = checkIfLocationIsInsideJSONGeofence(buildingGeofences);

// If location in supported building
if (foundBuilding != null) {
// Replace underscores with spaces, MapService does this for mapList
foundBuilding = foundBuilding.replaceAll("_"," ");

// Get index of located building name for spinner selection
for (int i = 1; i < maplist.size(); i++) { // skip building label at 0
if (maplist.get(i).equals(foundBuilding)) {
// Select building
mapSpinner.setSelection(i);
}
}
}
// Not found in supported building
else {
// Assuming location is inaccurate
Toast.makeText(getBaseContext(), "Location is not accurate enough to auto-select building.",
Toast.LENGTH_LONG).show();
}
}
}
pendingIntent = null;
} catch (Exception e) {
e.printStackTrace();
}
}

public OnItemSelectedListener mapSpinnerItemSelected = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// If building is selected
Expand Down
Loading

0 comments on commit 4646092

Please sign in to comment.