1313import android .os .Bundle ;
1414import android .view .View ;
1515import android .widget .Button ;
16+ import android .widget .CompoundButton ;
1617import android .widget .ImageView ;
1718import android .widget .LinearLayout ;
19+ import android .widget .Switch ;
1820import android .widget .TextView ;
1921import 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 ;
0 commit comments