Explorar el Código

-Define google api client service for send request location

ilhamitubagoes hace 4 años
padre
commit
669bccba48
Se han modificado 1 ficheros con 125 adiciones y 12 borrados
  1. 125 12
      app/src/main/java/com/fusi24/rfid/ui/ScanActivity.java

+ 125 - 12
app/src/main/java/com/fusi24/rfid/ui/ScanActivity.java

@@ -2,12 +2,16 @@ package com.fusi24.rfid.ui;
2 2
 
3 3
 import android.Manifest;
4 4
 import android.content.Intent;
5
+import android.content.IntentSender;
5 6
 import android.content.pm.PackageManager;
7
+import android.location.Location;
6 8
 import android.os.Bundle;
7 9
 import android.view.View;
8 10
 import android.widget.TextView;
9 11
 import android.widget.Toast;
10 12
 
13
+import androidx.annotation.NonNull;
14
+import androidx.annotation.Nullable;
11 15
 import androidx.appcompat.widget.Toolbar;
12 16
 import androidx.core.app.ActivityCompat;
13 17
 
@@ -16,12 +20,18 @@ import com.fusi24.rfid.config.Constant;
16 20
 import com.fusi24.rfid.databinding.ActivityScanBinding;
17 21
 import com.fusi24.rfid.ui.scanresult.ScanResultActivity;
18 22
 import com.fusi24.rfid.util.LFScanProcessing;
23
+import com.google.android.gms.common.ConnectionResult;
24
+import com.google.android.gms.common.api.GoogleApiClient;
19 25
 import com.google.android.gms.location.FusedLocationProviderClient;
26
+import com.google.android.gms.location.LocationListener;
27
+import com.google.android.gms.location.LocationRequest;
20 28
 import com.google.android.gms.location.LocationServices;
21 29
 
22 30
 import timber.log.Timber;
23 31
 
24
-public class ScanActivity extends LFScanProcessing {
32
+public class ScanActivity extends LFScanProcessing implements GoogleApiClient.ConnectionCallbacks,
33
+        GoogleApiClient.OnConnectionFailedListener,
34
+        LocationListener {
25 35
 
26 36
     public static final String ID_PERMIT = "id_permit";
27 37
     public static final String ID_DEVICE = "id_device";
@@ -32,6 +42,13 @@ public class ScanActivity extends LFScanProcessing {
32 42
     private ActivityScanBinding binding;
33 43
     private FusedLocationProviderClient locationClient;
34 44
 
45
+    //Define a request code to send to Google Play services
46
+    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
47
+    private GoogleApiClient mGoogleApiClient;
48
+    private LocationRequest mLocationRequest;
49
+    private double currentLatitude;
50
+    private double currentLongitude;
51
+
35 52
     @Override
36 53
     protected void onCreate(Bundle savedInstanceState) {
37 54
         super.onCreate(savedInstanceState);
@@ -42,21 +59,35 @@ public class ScanActivity extends LFScanProcessing {
42 59
         Toolbar toolbar = findViewById(R.id.toolbar);
43 60
         TextView toolbarTitle = findViewById(R.id.toolbar_text);
44 61
 
45
-        if(toolbarTitle !=null && toolbar !=null) {
62
+        if (toolbarTitle != null && toolbar != null) {
46 63
             toolbarTitle.setText(getString(R.string.app_name));
47 64
             setSupportActionBar(toolbar);
48 65
         }
49 66
 
50
-        if (getSupportActionBar() != null){
67
+        if (getSupportActionBar() != null) {
51 68
             getSupportActionBar().setDisplayShowTitleEnabled(false);
52 69
         }
53 70
 
71
+        mGoogleApiClient = new GoogleApiClient.Builder(this)
72
+                // The next two lines tell the new client that “this” current class will handle connection stuff
73
+                .addConnectionCallbacks(this)
74
+                .addOnConnectionFailedListener(this)
75
+                //fourth line adds the LocationServices API endpoint from GooglePlayServices
76
+                .addApi(LocationServices.API)
77
+                .build();
78
+
79
+        // Create the LocationRequest object
80
+        mLocationRequest = LocationRequest.create()
81
+                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
82
+                .setInterval(10 * 1000)        // 10 seconds, in milliseconds
83
+                .setFastestInterval(1000); // 1 second, in milliseconds
84
+
54 85
         initView();
55 86
         initSound();
56 87
     }
57 88
 
58 89
     private void initView() {
59
-        if (getIntent().getExtras() != null){
90
+        if (getIntent().getExtras() != null) {
60 91
             idPermit = getIntent().getExtras().getString(ID_PERMIT);
61 92
             idDevice = getIntent().getExtras().getString(ID_DEVICE);
62 93
             idCheckType = String.valueOf(getIntent().getExtras().getInt(ID_CHECK_TYPE));
@@ -69,12 +100,29 @@ public class ScanActivity extends LFScanProcessing {
69 100
         binding.toggleScan.setOnCheckedChangeListener((buttonView, isChecked) -> statusScanner());
70 101
     }
71 102
 
72
-    private void setStatusScan(){
73
-        switch (Integer.parseInt(idCheckType)){
74
-            case 649 :
103
+    @Override
104
+    protected void onResume() {
105
+        super.onResume();
106
+        //Now lets connect to the API
107
+        mGoogleApiClient.connect();
108
+    }
109
+
110
+    @Override
111
+    protected void onPause() {
112
+        super.onPause();
113
+        //Disconnect from API onPause()
114
+        if (mGoogleApiClient.isConnected()) {
115
+            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
116
+            mGoogleApiClient.disconnect();
117
+        }
118
+    }
119
+
120
+    private void setStatusScan() {
121
+        switch (Integer.parseInt(idCheckType)) {
122
+            case 649:
75 123
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_green));
76 124
                 break;
77
-            case 650 :
125
+            case 650:
78 126
             default:
79 127
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_blue));
80 128
                 break;
@@ -82,14 +130,14 @@ public class ScanActivity extends LFScanProcessing {
82 130
         binding.tvStatusScan.setText(checkTypeName);
83 131
     }
84 132
 
85
-    private void statusScanner(){
86
-        if(binding.toggleScan.isChecked()){
133
+    private void statusScanner() {
134
+        if (binding.toggleScan.isChecked()) {
87 135
             onStart();
88 136
         } else
89 137
             onStop();
90 138
     }
91 139
 
92
-    void startInstance(String rfidCardNumber){
140
+    void startInstance(String rfidCardNumber) {
93 141
         Intent intent = new Intent(this, ScanResultActivity.class);
94 142
         intent.putExtra(ScanResultActivity.RFID_CARD_NUMBER, rfidCardNumber);
95 143
         intent.putExtra(ScanResultActivity.ID_PERMIT, idPermit);
@@ -131,7 +179,7 @@ public class ScanActivity extends LFScanProcessing {
131 179
             return;
132 180
         }
133 181
         locationClient.getLastLocation().addOnSuccessListener(this, location -> {
134
-            if (location != null){
182
+            if (location != null) {
135 183
                 double latitude = location.getLatitude();
136 184
                 double longitude = location.getLongitude();
137 185
                 longLat = longitude + "," + latitude;
@@ -147,4 +195,69 @@ public class ScanActivity extends LFScanProcessing {
147 195
         super.onDestroy();
148 196
         binding = null;
149 197
     }
198
+
199
+    @Override
200
+    public void onConnected(@Nullable Bundle bundle) {
201
+        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
202
+            ActivityCompat.requestPermissions(this, Constant.PERMISSIONS_RFID, 1000);
203
+            return;
204
+        }
205
+        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
206
+
207
+        if (location == null) {
208
+            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
209
+
210
+        } else {
211
+            //If everything went fine lets get latitude and longitude
212
+            currentLatitude = location.getLatitude();
213
+            currentLongitude = location.getLongitude();
214
+
215
+            longLat = currentLongitude + "," + currentLatitude;
216
+
217
+            Toast.makeText(this, "Latitude : " + currentLatitude + ", Longitude : " + currentLongitude, Toast.LENGTH_LONG).show();
218
+        }
219
+    }
220
+
221
+    @Override
222
+    public void onConnectionSuspended(int i) {
223
+
224
+    }
225
+
226
+    @Override
227
+    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
228
+        /*
229
+         * Google Play services can resolve some errors it detects.
230
+         * If the error has a resolution, try sending an Intent to
231
+         * start a Google Play services activity that can resolve
232
+         * error.
233
+         */
234
+        if (connectionResult.hasResolution()) {
235
+            try {
236
+                // Start an Activity that tries to resolve the error
237
+                connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
238
+                /*
239
+                 * Thrown if Google Play services canceled the original
240
+                 * PendingIntent
241
+                 */
242
+            } catch (IntentSender.SendIntentException e) {
243
+                // Log the error
244
+                e.printStackTrace();
245
+            }
246
+        } else {
247
+            /*
248
+             * If no resolution is available, display a dialog to the
249
+             * user with the error.
250
+             */
251
+            Timber.tag("Error").e("Location services connection failed with code %s", connectionResult.getErrorCode());
252
+        }
253
+    }
254
+
255
+    @Override
256
+    public void onLocationChanged(Location location) {
257
+        currentLatitude = location.getLatitude();
258
+        currentLongitude = location.getLongitude();
259
+
260
+        longLat = currentLongitude + "," + currentLatitude;
261
+        Toast.makeText(this, "Latitude : " + currentLatitude + ", Longitude : " + currentLongitude, Toast.LENGTH_LONG).show();
262
+    }
150 263
 }