package com.example.hamza.hamari_sawari;import android.*;import android.Manifest;import android.content.Intent;import android.content.pm.PackageManager;import android.location.Location;import android.location.LocationListener;import android.support.annotation.NonNull;import android.support.annotation.Nullable;import android.support.v4.app.ActivityCompat;import android.support.v4.app.FragmentActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.CompoundButton;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Switch;import android.widget.TextView;import android.widget.Toast;import com.bumptech.glide.Glide;import com.directions.route.AbstractRouting;import com.directions.route.Route;import com.directions.route.RouteException;import com.directions.route.Routing;import com.directions.route.RoutingListener;import com.firebase.geofire.GeoFire;import com.firebase.geofire.GeoLocation;import com.google.android.gms.common.ConnectionResult;import com.google.android.gms.common.api.GoogleApiClient;import com.google.android.gms.location.LocationRequest;import com.google.android.gms.location.LocationServices;import com.google.android.gms.maps.CameraUpdateFactory;import com.google.android.gms.maps.GoogleMap;import com.google.android.gms.maps.OnMapReadyCallback;import com.google.android.gms.maps.SupportMapFragment;import com.google.android.gms.maps.model.BitmapDescriptorFactory;import com.google.android.gms.maps.model.LatLng;import com.google.android.gms.maps.model.Marker;import com.google.android.gms.maps.model.MarkerOptions;import com.google.android.gms.maps.model.Polyline;import com.google.android.gms.maps.model.PolylineOptions;import com.google.firebase.auth.FirebaseAuth;import com.google.firebase.database.DataSnapshot;import com.google.firebase.database.DatabaseError;import com.google.firebase.database.DatabaseReference;import com.google.firebase.database.FirebaseDatabase;import com.google.firebase.database.ValueEventListener;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Objects;public class DriverMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener, RoutingListener { private GoogleMap mMap; GoogleApiClient mGoogleApiClient; Location mLastLocation; LocationRequest mLocationRequest; private Button mLogout, mSettings, mRideStatus; private Switch mWorkingSwitch; private int status = 0; private String customerId = “”, destination; private LatLng destinationLatLng; private Boolean isLoggingOut = false; private SupportMapFragment mapFragment; private LinearLayout mCustomerInfo; private ImageView mCustomerProfileImage; private TextView mCustomerName, mCustomerPhone, mCustomerDestination; @Override protected void onCreate(Bundle savedInstanceState) { //first state of the activity super.onCreate(savedInstanceState); setContentView(R.layout.activity_driver_map); // Obtain the SupportMapFragment and get notified when the map is ready to be used. polylines = new ArrayList<>(); mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(DriverMapActivity.this, new String{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE); }else{ mapFragment.getMapAsync(this); } mCustomerInfo = (LinearLayout) findViewById(R.id.customerInfo); mCustomerProfileImage = (ImageView) findViewById(R.id.customerProfileImage); mCustomerName = (TextView) findViewById(R.id.customerName); mCustomerPhone = (TextView) findViewById(R.id.customerPhone); mCustomerDestination = (TextView) findViewById(R.id.customerDestination); mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch); mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { //checks switch of working of driver if (b){ connectDriver(); }else { disconnectDriver(); } } }); mSettings = (Button) findViewById(R.id.settings); mLogout = (Button) findViewById(R.id.logout); mRideStatus = (Button) findViewById(R.id.rideStatus); mRideStatus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //new ride initiated by customer switch(status){ case 1: status=2; erasePolylines(); if(destinationLatLng.latitude!=0.0 && destinationLatLng.longitude!=0.0){ getRouteToMarker(destinationLatLng); } mRideStatus.setText(“drive completed”); break; case 2: recordRide(); endRide(); break; } } }); mLogout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { isLoggingOut = true; disconnectDriver(); FirebaseAuth.getInstance().signOut(); Intent intent = new Intent(DriverMapActivity.this, MainActivity.class); startActivity(intent); finish(); return; } }); mSettings.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(DriverMapActivity.this, DriverSettingsActivity.class); startActivity(intent); return; } }); getAssignedCustomer(); } private void getAssignedCustomer(){ //get customer from firebase and give it to driver String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Drivers”).child(driverId).child(“customerRequest”).child(“customerRideId”); assignedCustomerRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists()){ status = 1; customerId = dataSnapshot.getValue().toString(); getAssignedCustomerPickupLocation(); getAssignedCustomerDestination(); getAssignedCustomerInfo(); }else{ endRide(); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } Marker pickupMarker; private DatabaseReference assignedCustomerPickupLocationRef; private ValueEventListener assignedCustomerPickupLocationRefListener; private void getAssignedCustomerPickupLocation(){ assignedCustomerPickupLocationRef = FirebaseDatabase.getInstance().getReference().child(“customerRequest”).child(customerId).child(“l”); assignedCustomerPickupLocationRefListener = assignedCustomerPickupLocationRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists() && !customerId.equals(“”)){ List map = (List) dataSnapshot.getValue(); double locationLat = 0; double locationLng = 0; if(map.get(0) != null){ locationLat = Double.parseDouble(map.get(0).toString()); } if(map.get(1) != null){ locationLng = Double.parseDouble(map.get(1).toString()); } LatLng pickupLatLng = new LatLng(locationLat,locationLng); pickupMarker = mMap.addMarker(new MarkerOptions().position(pickupLatLng).title(“pickup location”).icon(BitmapDescriptorFactory.fromResource(R.mipmap.pickup))); getRouteToMarker(pickupLatLng); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } private void getRouteToMarker(LatLng pickupLatLng) { Routing routing = new Routing.Builder() .travelMode(AbstractRouting.TravelMode.DRIVING) .withListener(this) .alternativeRoutes(false) .waypoints(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), pickupLatLng) .build(); routing.execute(); } private void getAssignedCustomerDestination(){ String driverId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference assignedCustomerRef = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Drivers”).child(driverId).child(“customerRequest”); assignedCustomerRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists()) { Map map = (Map) dataSnapshot.getValue(); if(map.get(“destination”)!=null){ destination = map.get(“destination”).toString(); mCustomerDestination.setText(“Destination: ” + destination); } else{ mCustomerDestination.setText(“Destination: –“); } Double destinationLat = 0.0; Double destinationLng = 0.0; if(map.get(“destinationLat”) != null){ destinationLat = Double.valueOf(map.get(“destinationLat”).toString()); } if(map.get(“destinationLng”) != null){ destinationLng = Double.valueOf(map.get(“destinationLng”).toString()); destinationLatLng = new LatLng(destinationLat, destinationLng); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); } private void getAssignedCustomerInfo(){ mCustomerInfo.setVisibility(View.VISIBLE); DatabaseReference mCustomerDatabase = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Customers”).child(customerId); mCustomerDatabase.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if(dataSnapshot.exists() && dataSnapshot.getChildrenCount()>0){ Map map = (Map) dataSnapshot.getValue(); if(map.get(“name”)!=null){ mCustomerName.setText(map.get(“name”).toString()); } if(map.get(“phone”)!=null){ mCustomerPhone.setText(map.get(“phone”).toString()); } if(map.get(“profileImageUrl”)!=null){ Glide.with(getApplication()).load(map.get(“profileImageUrl”).toString()).into(mCustomerProfileImage); } } } @Override public void onCancelled(DatabaseError databaseError) { } }); } private void endRide(){ mRideStatus.setText(“picked customer”); erasePolylines(); String userId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Drivers”).child(userId).child(“customerRequest”); driverRef.removeValue(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference(“customerRequest”); GeoFire geoFire = new GeoFire(ref); geoFire.removeLocation(customerId); customerId=””; if(pickupMarker != null){ pickupMarker.remove(); } if (assignedCustomerPickupLocationRefListener != null){ assignedCustomerPickupLocationRef.removeEventListener(assignedCustomerPickupLocationRefListener); } mCustomerInfo.setVisibility(View.GONE); mCustomerName.setText(“”); mCustomerPhone.setText(“”); mCustomerDestination.setText(“Destination: –“); mCustomerProfileImage.setImageResource(R.mipmap.user); } private void recordRide(){ String userId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Drivers”).child(userId).child(“history”); DatabaseReference customerRef = FirebaseDatabase.getInstance().getReference().child(“Users”).child(“Customers”).child(customerId).child(“history”); DatabaseReference historyRef = FirebaseDatabase.getInstance().getReference().child(“history”); String requestId = historyRef.push().getKey(); driverRef.child(requestId).setValue(true); customerRef.child(requestId).setValue(true); HashMap map = new HashMap(); map.put(“driver”, userId); map.put(“customer”, customerId); map.put(“rating”, 0); historyRef.child(requestId).updateChildren(map); } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } buildGoogleApiClient(); mMap.setMyLocationEnabled(true); } protected synchronized void buildGoogleApiClient(){ mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); mGoogleApiClient.connect(); } @Override public void onLocationChanged(Location location) { if(getApplicationContext()!=null){ mLastLocation = location; LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude()); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); mMap.animateCamera(CameraUpdateFactory.zoomTo(8)); String userId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference(“driversAvailable”); DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference(“driversWorking”); GeoFire geoFireAvailable = new GeoFire(refAvailable); GeoFire geoFireWorking = new GeoFire(refWorking); switch (customerId){ case “”: geoFireWorking.removeLocation(userId); geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude())); break; default: geoFireAvailable.removeLocation(userId); geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude())); break; } } } @Override public void onConnected(@Nullable Bundle bundle) { mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(1000); mLocationRequest.setFastestInterval(1000); mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(DriverMapActivity.this, new String{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE); } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } private void connectDriver(){ if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(DriverMapActivity.this, new String{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE); } LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } private void disconnectDriver(){ LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); String userId = FirebaseAuth.getInstance().getCurrentUser().getUid(); DatabaseReference ref = FirebaseDatabase.getInstance().getReference(“driversAvailable”); GeoFire geoFire = new GeoFire(ref); geoFire.removeLocation(userId); } final int LOCATION_REQUEST_CODE = 1; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String permissions, @NonNull int grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch(requestCode){ case LOCATION_REQUEST_CODE:{ if(grantResults.length >0 && grantResults0 == PackageManager.PERMISSION_GRANTED){ mapFragment.getMapAsync(this); } else{ Toast.makeText(getApplicationContext(), “Please provide the permission”, Toast.LENGTH_LONG).show(); } break; } } } private List polylines; private static final int COLORS = new int{R.color.primary_dark_material_light}; @Override public void onRoutingFailure(RouteException e) { if(e != null) { Toast.makeText(this, “Error: ” + e.getMessage(), Toast.LENGTH_LONG).show(); }else { Toast.makeText(this, “Something went wrong, Try again”, Toast.LENGTH_SHORT).show(); } } @Override public void onRoutingStart() { } @Override public void onRoutingSuccess(ArrayList route, int shortestRouteIndex) { if(polylines.size()>0) { for (Polyline poly : polylines) { poly.remove(); } } polylines = new ArrayList<>(); //add route(s) to the map. for (int i = 0; i
x

Hi!
I'm Eleanor!

Would you like to get a custom essay? How about receiving a customized one?

Check it out