This project is the Software Engineering(CS487) team project of Team-J. It was jointly developed by me (Zhu Zisheng) and my team member Xiang Wu. It has not been fully developed yet. The following is a complete introduction to the concern and project.
Project structure :
Usability Requirement
GUI generated by XML in android studio :
Core code of main manu:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDriver = (Button) findViewById(R.id.driver);
mCustomer = (Button) findViewById(R.id.customer);
startService(new Intent(MainActivity.this, onAppKilled.class));
mDriver.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DriverLoginActivity.class);
startActivity(intent);
finish();
return;
}
});
mCustomer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, CustomerLoginActivity.class);
startActivity(intent);
finish();
return;
}
});
}
Core code that allows users to update profile:
private void saveUserInformation() {
mName = mNameField.getText().toString();
mPhone = mPhoneField.getText().toString();
Map userInfo = new HashMap();
userInfo.put("name", mName);
userInfo.put("phone", mPhone);
mCustomerDatabase.updateChildren(userInfo);
if(resultUri != null) {
StorageReference filePath = FirebaseStorage.getInstance().getReference().child("profile_images").child(userID);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte[] data = baos.toByteArray();
UploadTask uploadTask = filePath.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
finish();
return;
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUrl = taskSnapshot.getDownloadUrl();
Map newImage = new HashMap();
newImage.put("profileImageUrl", downloadUrl.toString());
mCustomerDatabase.updateChildren(newImage);
finish();
return;
}
});
}else{
finish();
}
}
Security
Core code of registration function:
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(CustomerLoginActivity.this, "sign up error", Toast.LENGTH_SHORT).show();
}else{
String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers").child(user_id);
current_user_db.setValue(true);
}
}
});
Core code of login function:
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(!task.isSuccessful()){
Toast.makeText(CustomerLoginActivity.this, "sign in error", Toast.LENGTH_SHORT).show();
}
}
});
Functional
Core code of the ride function:
Post order:
if (requestBol){
endRide();
}else{
int selectId = mRadioGroup.getCheckedRadioButtonId();
final RadioButton radioButton = (RadioButton) findViewById(selectId);
if (radioButton.getText() == null){
return;
}
requestService = radioButton.getText().toString();
requestBol = true;
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.setLocation(userId, new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
pickupLocation = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
pickupMarker = mMap.addMarker(new MarkerOptions().position(pickupLocation).title("Pickup Here").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_pickup)));
mRequest.setText("Getting your Driver....");
getClosestDriver(); // Not implement yet...
}
Get map location:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
}else{
checkLocationPermission();
}
}
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);
}
Update location:
LocationCallback mLocationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for(Location location : locationResult.getLocations()){
if(getApplicationContext()!=null){
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
//mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if(!getDriversAroundStarted)
getDriversAround();
}
}
}
};
End ride:
private void endRide(){
requestBol = false;
geoQuery.removeAllListeners();
driverLocationRef.removeEventListener(driverLocationRefListener);
driveHasEndedRef.removeEventListener(driveHasEndedRefListener);
if (driverFoundID != null){
DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Drivers").child(driverFoundID).child("customerRequest");
driverRef.removeValue();
driverFoundID = null;
}
driverFound = false;
radius = 1;
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("customerRequest");
GeoFire geoFire = new GeoFire(ref);
geoFire.removeLocation(userId);
if(pickupMarker != null){
pickupMarker.remove();
}
if (mDriverMarker != null){
mDriverMarker.remove();
}
mRequest.setText("call Uber");
mDriverInfo.setVisibility(View.GONE);
mDriverName.setText("");
mDriverPhone.setText("");
mDriverCar.setText("Destination: --");
mDriverProfileImage.setImageResource(R.mipmap.ic_default_user);
}
Core code of Paypal payment function:
private int PAYPAL_REQUEST_CODE = 1;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId("");
//.clientId(PayPalConfig.PAYPAL_CLIENT_ID);
private void payPalPayment() {
PayPalPayment payment = new PayPalPayment(new BigDecimal(ridePrice), "USD", "Uber Ride",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(this, PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, PAYPAL_REQUEST_CODE);
}
Improvement
Core code of history function:
Get ride information:
private void getRideInformation() {
historyRideInfoDb.addListenerForSingleValueEvent(new ValueEventListener() {
@SuppressLint("SetTextI18n")
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
for (DataSnapshot child:dataSnapshot.getChildren()){
if (child.getKey().equals("customer")){
customerId = child.getValue().toString();
if(!customerId.equals(currentUserId)){
userDriverOrCustomer = "Drivers";
getUserInformation("Customers", customerId);
}
}
if (child.getKey().equals("driver")){
driverId = child.getValue().toString();
if(!driverId.equals(currentUserId)){
userDriverOrCustomer = "Customers";
getUserInformation("Drivers", driverId);
displayCustomerRelatedObjects();
}
}
if (child.getKey().equals("timestamp")){
rideDate.setText(getDate(Long.valueOf(child.getValue().toString())));
}
if (child.getKey().equals("rating")){
mRatingBar.setRating(Integer.valueOf(child.getValue().toString()));
}
if (child.getKey().equals("customerPaid")){
customerPaid =true;
}
if (child.getKey().equals("distance")){
distance = child.getValue().toString();
rideDistance.setText(distance.substring(0, Math.min(distance.length(), 5)) + " km");
ridePrice = Double.valueOf(distance) * 0.5;
}
if (child.getKey().equals("destination")){
rideLocation.setText(child.getValue().toString());
}
if (child.getKey().equals("location")){
pickupLatLng = new LatLng(Double.valueOf(child.child("from").child("lat").getValue().toString()), Double.valueOf(child.child("from").child("lng").getValue().toString()));
destinationLatLng = new LatLng(Double.valueOf(child.child("to").child("lat").getValue().toString()), Double.valueOf(child.child("to").child("lng").getValue().toString()));
if(destinationLatLng != new LatLng(0,0)){
getRouteToMarker();
}
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
For more detail, please read source code.