Skip to content

Instantly share code, notes, and snippets.

@danignibus
Created April 20, 2015 20:36
Show Gist options
  • Save danignibus/2f70dce1b59f6ab3f4a0 to your computer and use it in GitHub Desktop.
Save danignibus/2f70dce1b59f6ab3f4a0 to your computer and use it in GitHub Desktop.
A snippet of one of the activities that I contributed to OnTime, the final project my group developed for CS65. OnTime is a location coordination and management app that helps you and all your friends arrive at a destination at the same time.
/**
* This is the eventDisplayActivity, it displays a specific event's information along with each
* invitee's current status with regard to reaching the final location.
*/
public class EventDisplayActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
//for this code snippet, I removed all the variable declarations.
@Override
/**
* When the location is changed, update the locations and distances on the cloud
*/
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged");
current_location = new ParseGeoPoint(location.getLatitude(), location.getLongitude());
ParseQuery<Event> currentEventQuery = ParseQuery.getQuery("event");
currentEventQuery.whereEqualTo("objectId", object_id);
currentEventQuery.getFirstInBackground(new GetCallback<Event>() {
@Override
public void done(Event event, ParseException e) {
if (event == null) {
Log.d("score", "The getFirst request failed.");
} else {
displayedEvent = event;
init_distances = displayedEvent.getInitDistances();
userLocations = displayedEvent.getUserLocations();
userDistances = displayedEvent.getUserDistances();
Log.d(TAG, userDistances.toString());
if (userDistances.get(position) == -1.1) {
Log.d(TAG, "INIT LOCATIONS");
distance = current_location.distanceInMilesTo(finalGeoPoint);
init_distances.set(position, distance);
userLocations.set(position, current_location);
userDistances.set(position, distance);
}
else {
Log.d(TAG, "UPDATE LOCATIONS and POSITION: " + position);
distance = current_location.distanceInMilesTo(finalGeoPoint);
userLocations.set(position, current_location);
userDistances.set(position, distance);
}
}
}
});
// Save distances to cloud
displayedEvent.setUserLocations(userLocations);
displayedEvent.setUserDistances(userDistances);
displayedEvent.saveInBackground();
for (int i = 0; i < userDistances.size(); i++) {
ProgressBar currentUserBar = progBarArrayList.get(i);
double status = userDistances.get(i) / init_distances.get(i);
if (status > 1){
status = 1;
}
int intStatus = (int)((1-status) * 100);
currentUserBar.setProgress(intStatus);
}
}
/**
* Gets the necessary information from the cloud including the corresponding position on the
* array list and the userlocations. If userlocations don't exist, init a new one
*/
private void getParseInformation() {
currentUser = ParseUser.getCurrentUser();
currentFbId = currentUser.getString("fbId");
for (int i = 0; i < attendees.size(); i++) {
if (attendees.get(i).equals(currentFbId)) {
position = i;
Log.d(TAG, "POSITION: " + Integer.toString(position));
}
}
userLocations = displayedEvent.getUserLocations();
userDistances = displayedEvent.getUserDistances();
init_distances = displayedEvent.getInitDistances();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_display);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.hide();
getWindow().setBackgroundDrawableResource(R.drawable.bokeh1copy3);
initLocationTracking();
//Gets event information from the MainActivity
if (savedInstanceState == null) {
object_id = getIntent().getStringExtra(OBJECT_ID);
title = getIntent().getStringExtra(TITLE);
finalLocation = getIntent().getParcelableExtra(LOCATION);
finalGeoPoint = new ParseGeoPoint(finalLocation.getLatitude(),finalLocation.getLongitude());
date = getIntent().getStringExtra(DATE);
time = getIntent().getStringExtra(TIME);
latitude = finalLocation.getLatitude();
longitude = finalLocation.getLongitude();
latLngLocation = new LatLng(latitude, longitude);
Log.d(TAG, "ObjectID: " + object_id);
ParseQuery<Event> currentEventQuery = ParseQuery.getQuery("event");
currentEventQuery.whereEqualTo("objectId", object_id);
try{
displayedEvent = currentEventQuery.getFirst();
}
catch (ParseException e){
}
attendees = displayedEvent.getAcceptedList();
}
if (savedInstanceState != null) {
attendees = savedInstanceState.getStringArrayList(ATTENDEES_INSTANCE_STATE_KEY);
title = savedInstanceState.getString(EVENT_TITLE_INSTANCE_STATE_KEY);
date = savedInstanceState.getString(DATE_INSTANCE_STATE_KEY);
latLngLocation = savedInstanceState.getParcelable(LATLNG_INSTANCE_STATE_KEY);
}
getParseInformation();
//get event title
eventDisplayTextView = (TextView) findViewById(R.id.event_display_text_view);
eventDisplayTextView.setTextColor(Color.WHITE);
eventDisplayTextView.setText(" " + title.toUpperCase());
//get event date and time
eventDisplayDate = (TextView) findViewById(R.id.event_display_date);
eventDisplayDate.setTextColor(Color.BLACK);
eventDisplayDate.setText(date);
//set event location
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
GoogleMap mmap = supportMapFragment.getMap();
mmap.addMarker(new MarkerOptions()
.position(latLngLocation)
.title("Marker"));
mmap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngLocation,
17));
progressBarLinearLayout = (LinearLayout) findViewById(R.id.progress_bar_linear_layout);
// get events corresponding to attendees
ParseQuery query_names = ParseUser.getQuery();
query_names.whereContainedIn("fbId", attendees);
parse_users = new ArrayList<ParseUser>();
try {
parse_users = (ArrayList<ParseUser>) query_names.find();
} catch (ParseException e) {
}
//rearrange progress bar according to invite order
for (int a = 0; a < parse_users.size(); a++) {
String fbId = parse_users.get(a).getString("fbId");
for (int b = 0; b < attendees.size(); b++) {
if (attendees.get(b).equals(fbId)) {
ParseUser tmp = parse_users.get(b);
parse_users.set(b,parse_users.get(a));
parse_users.set(a,tmp);
}
}
}
progBarArrayList = new ArrayList<ProgressBar>();
//dynamically add friends names and progress bars
for (int i = 0; i < parse_users.size(); i++) {
TextView newView = new TextView(this, null, R.style.CustomTextViewDani);
//query parse for the user with the matching Fbook idea
newView.setText(parse_users.get(i).getString("name"));
newView.setTextSize(15);
newView.setTextAppearance(this, R.style.boldText);
progressBarLinearLayout.addView(newView);
ProgressBar newBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
newBar.setMinimumWidth(40);
newBar.setMinimumHeight(50);
newBar.setId(i);
newBar.setMax(100);
newBar.setScrollBarSize(200);
progressBarLinearLayout.addView(newBar);
progBarArrayList.add(newBar);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment