Skip to content

Commit e68cd95

Browse files
- Fixing driver location updates bug
1 parent d4dd397 commit e68cd95

File tree

5 files changed

+91
-25
lines changed

5 files changed

+91
-25
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
android:roundIcon="@mipmap/ic_launcher_round"
2020
android:supportsRtl="true"
2121
android:theme="@style/AppTheme">
22+
<service android:name=".onAppKilled" android:stopWithTask="false"/>
2223
<activity android:name=".MainActivity">
2324
<intent-filter>
2425
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/simcoder/uber/DriverMapActivity.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import android.os.Bundle;
1414
import android.view.View;
1515
import android.widget.Button;
16+
import android.widget.CompoundButton;
1617
import android.widget.ImageView;
1718
import android.widget.LinearLayout;
19+
import android.widget.Switch;
1820
import android.widget.TextView;
1921
import android.widget.Toast;
2022

@@ -63,6 +65,8 @@ public class DriverMapActivity extends FragmentActivity implements OnMapReadyCal
6365

6466
private Button mLogout, mSettings, mRideStatus;
6567

68+
private Switch mWorkingSwitch;
69+
6670
private int status = 0;
6771

6872
private String customerId = "", destination;
@@ -102,6 +106,18 @@ protected void onCreate(Bundle savedInstanceState) {
102106
mCustomerPhone = (TextView) findViewById(R.id.customerPhone);
103107
mCustomerDestination = (TextView) findViewById(R.id.customerDestination);
104108

109+
mWorkingSwitch = (Switch) findViewById(R.id.workingSwitch);
110+
mWorkingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
111+
@Override
112+
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
113+
if (isChecked){
114+
connectDriver();
115+
}else{
116+
disconnectDriver();
117+
}
118+
}
119+
});
120+
105121
mSettings = (Button) findViewById(R.id.settings);
106122
mLogout = (Button) findViewById(R.id.logout);
107123
mRideStatus = (Button) findViewById(R.id.rideStatus);
@@ -387,12 +403,6 @@ public void onConnected(@Nullable Bundle bundle) {
387403
mLocationRequest.setInterval(1000);
388404
mLocationRequest.setFastestInterval(1000);
389405
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
390-
391-
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) {
392-
ActivityCompat.requestPermissions(DriverMapActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
393-
}
394-
395-
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
396406
}
397407

398408
@Override
@@ -403,6 +413,12 @@ public void onConnectionSuspended(int i) {
403413
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
404414
}
405415

416+
private void connectDriver(){
417+
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) {
418+
ActivityCompat.requestPermissions(DriverMapActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE);
419+
}
420+
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
421+
}
406422

407423
private void disconnectDriver(){
408424
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
@@ -430,14 +446,6 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
430446
}
431447
}
432448

433-
@Override
434-
protected void onStop() {
435-
super.onStop();
436-
if (!isLoggingOut){
437-
disconnectDriver();
438-
}
439-
}
440-
441449

442450

443451
private List<Polyline> polylines;

app/src/main/java/com/simcoder/uber/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ protected void onCreate(Bundle savedInstanceState) {
1616
mDriver = (Button) findViewById(R.id.driver);
1717
mCustomer = (Button) findViewById(R.id.customer);
1818

19+
startService(new Intent(MainActivity.this, onAppKilled.class));
1920
mDriver.setOnClickListener(new View.OnClickListener() {
2021
@Override
2122
public void onClick(View v) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.simcoder.uber;
2+
3+
import android.app.Service;
4+
import android.bluetooth.BluetoothClass;
5+
import android.content.Intent;
6+
import android.os.IBinder;
7+
import android.support.annotation.Nullable;
8+
9+
import com.firebase.geofire.GeoFire;
10+
import com.google.firebase.auth.FirebaseAuth;
11+
import com.google.firebase.database.DatabaseReference;
12+
import com.google.firebase.database.FirebaseDatabase;
13+
14+
import java.security.Provider;
15+
16+
/**
17+
* Created by manel on 11/3/2017.
18+
*/
19+
20+
public class onAppKilled extends Service {
21+
@Nullable
22+
@Override
23+
public IBinder onBind(Intent intent) {
24+
return null;
25+
}
26+
27+
@Override
28+
public void onTaskRemoved(Intent rootIntent) {
29+
super.onTaskRemoved(rootIntent);
30+
31+
String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
32+
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("driversAvailable");
33+
GeoFire geoFire = new GeoFire(ref);
34+
geoFire.removeLocation(userId);
35+
}
36+
}

app/src/main/res/layout/activity_driver_map.xml

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,42 @@
55
android:layout_height="match_parent"
66
tools:context="com.simcoder.uber.DriverMapActivity" >
77

8-
<Button
9-
android:layout_width="wrap_content"
10-
android:layout_height="wrap_content"
11-
android:id="@+id/logout"
12-
android:text="logout"/>
13-
<Button
14-
android:layout_gravity="end"
15-
android:layout_width="wrap_content"
16-
android:layout_height="wrap_content"
17-
android:id="@+id/settings"
18-
android:text="Settings"/>
198
<fragment
209
android:id="@+id/map"
2110
android:name="com.google.android.gms.maps.SupportMapFragment"
2211
android:layout_width="match_parent"
2312
android:layout_height="match_parent"/>
13+
<LinearLayout
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:orientation="vertical">
17+
<LinearLayout
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content">
20+
<Button
21+
android:layout_weight="1"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"
24+
android:id="@+id/logout"
25+
android:text="logout"/>
26+
<Button
27+
android:layout_weight="1"
28+
android:layout_gravity="end"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:id="@+id/settings"
32+
android:text="Settings"/>
33+
</LinearLayout>
34+
<Switch
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:text="Working"
38+
android:layout_margin="10sp"
39+
android:id="@+id/workingSwitch"/>
40+
</LinearLayout>
41+
42+
43+
2444
<LinearLayout
2545
android:layout_width="match_parent"
2646
android:layout_height="wrap_content"

0 commit comments

Comments
 (0)