From b591587e6a02d90884b307e8f1a4adde19d8dee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Sat, 29 Sep 2018 17:36:36 +0200 Subject: [PATCH] Add broadcast receiver --- app/src/main/AndroidManifest.xml | 8 ++ .../findmytutor/activity/LoginActivity.java | 2 +- .../findmytutor/activity/MainActivity.java | 100 ++++++++++++------ .../uam/wmi/findmytutor/model/Coordinate.java | 24 +++-- .../wmi/findmytutor/network/ApiClient.java | 2 +- .../service/CoordinateService.java | 16 ++- .../findmytutor/service/GoogleService.java | 29 +---- .../utils/BroadcastLocalizationHandler.java | 24 +++++ build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 10 files changed, 143 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a106ee6..7b82191 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -16,6 +16,14 @@ android:theme="@style/AppTheme"> + + + + + + + + () { + @Override + public void onSuccess(Coordinate coordinate) { + Log.e("Service", "onSuccess: " + coordinate); + } + + @Override + public void onError(Throwable e) { + Log.e("Service", "onError: " + e.getMessage()); + } + }));*/ + } private void setFragment(Fragment fragment) { FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); @@ -168,7 +195,21 @@ public class MainActivity extends AppCompatActivity { } } else { boolean_permission = true; + if (boolean_permission) { + if (!PrefUtils.getIsServiceRunning(getApplicationContext())) { + Intent intent = new Intent(getApplicationContext(), GoogleService.class); + startService(intent); + } else { + Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show(); + } + } else { + Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show(); + + } } + + + } @Override @@ -182,24 +223,21 @@ public class MainActivity extends AppCompatActivity { } else { Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show(); - } } } } - - @Override protected void onResume() { super.onResume(); + registerReceiver(this.broadcastReceiver, new IntentFilter(GoogleService.str_receiver)); } @Override protected void onPause() { super.onPause(); + unregisterReceiver(this.broadcastReceiver); } - - } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java index 95e9b59..8a7fcfb 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/model/Coordinate.java @@ -11,12 +11,12 @@ import io.swagger.annotations.ApiModelProperty; * Coordinate */ -public class Coordinate extends BaseResponse{ +public class Coordinate extends BaseResponse { @SerializedName("coordinateId") private UUID coordinateId = null; @SerializedName("latitude") - private Float latitude = null; + private Double latitude; @SerializedName("longitude") private Float longitude = null; @@ -37,7 +37,19 @@ public class Coordinate extends BaseResponse{ private Long timeStamp = null; @SerializedName("label") - private String label = null; + private String label; + + /* public Coordinate (Float latitude, Float longitude, String userId, String label ) { + this.latitude = latitude; + this.longitude = longitude; + this.userId = userId; + this.label = label; + + }*/ + + public Coordinate (Double latitude) { + this.latitude = latitude; + } public Coordinate coordinateId(UUID coordinateId) { this.coordinateId = coordinateId; @@ -57,7 +69,7 @@ public class Coordinate extends BaseResponse{ this.coordinateId = coordinateId; } - public Coordinate latitude(Float latitude) { + public Coordinate latitude(Double latitude) { this.latitude = latitude; return this; } @@ -67,11 +79,11 @@ public class Coordinate extends BaseResponse{ * @return latitude **/ @ApiModelProperty(required = true, value = "") - public Float getLatitude() { + public Double getLatitude() { return latitude; } - public void setLatitude(Float latitude) { + public void setLatitude(Double latitude) { this.latitude = latitude; } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/network/ApiClient.java b/app/src/main/java/com/uam/wmi/findmytutor/network/ApiClient.java index 008a6cc..70e3a9e 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/network/ApiClient.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/network/ApiClient.java @@ -62,7 +62,7 @@ public class ApiClient { // Adding Authorization token (API Key) // Requests will be denied without API key if (!TextUtils.isEmpty(PrefUtils.getApiKey(context))) { - requestBuilder.addHeader("Authorization", PrefUtils.getApiKey(context)); + requestBuilder.addHeader("Authorization", "Bearer " + PrefUtils.getApiKey(context)); } Request request = requestBuilder.build(); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java index 877bec2..2b24421 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/CoordinateService.java @@ -4,10 +4,14 @@ import com.uam.wmi.findmytutor.model.Coordinate; import java.util.List; +import io.reactivex.Observable; +import io.reactivex.Single; import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.DELETE; +import retrofit2.http.Field; +import retrofit2.http.FormUrlEncoded; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.PUT; @@ -33,8 +37,18 @@ public interface CoordinateService { @GET("api/coordinates/top/online") Call> getOnlineCoordinates(); + + // Create note + @FormUrlEncoded @POST("api/coordinates") - Call postCoordinate(@Body Coordinate coordinate); + Single postCoordinate(@Body Coordinate coordinate); + +/* + // Create note + @FormUrlEncoded + @POST("api/coordinates") + Observable postCoordinate(@Field("latitude") Double latitude); +*/ @PUT("api/coordinates/{id}") Call putCoordinatesById(@Path("id") String id); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/service/GoogleService.java b/app/src/main/java/com/uam/wmi/findmytutor/service/GoogleService.java index 49fc2e9..0d0398d 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/service/GoogleService.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/service/GoogleService.java @@ -37,7 +37,7 @@ public class GoogleService extends Service implements LocationListener{ private Handler mHandler = new Handler(); private Timer mTimer = null; long notify_interval = 5000; - public static String str_receiver = "servicetutorial.service.receiver"; + public static String str_receiver = "background.location.broadcast"; Intent intent; @@ -113,10 +113,6 @@ public class GoogleService extends Service implements LocationListener{ } location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); if (location!=null){ - - Log.e("latitude",location.getLatitude()+""); - Log.e("longitude",location.getLongitude()+""); - latitude = location.getLatitude(); longitude = location.getLongitude(); fn_update(location); @@ -139,37 +135,20 @@ public class GoogleService extends Service implements LocationListener{ } } } - - } } -/* - private void sendLocalizationToApi(Double latitude,Double longitude){ - Log.e("Service google",PrefUtils.getUserId(getApplicationContext())); - Log.e("Service google",latitude + " " + longitude); - }*/ private class TimerTaskToGetLocation extends TimerTask{ @Override public void run() { - - mHandler.post(new Runnable() { - @Override - public void run() { - fn_getlocation(); - } - }); - + mHandler.post(GoogleService.this::fn_getlocation); } } private void fn_update(Location location){ - intent.putExtra("latutide",location.getLatitude()+""); - intent.putExtra("longitude",location.getLongitude()+""); + intent.putExtra("latitude",location.getLatitude()); + intent.putExtra("longitude",location.getLongitude()); sendBroadcast(intent); } - - - } \ No newline at end of file diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java new file mode 100644 index 0000000..3cde8a9 --- /dev/null +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/BroadcastLocalizationHandler.java @@ -0,0 +1,24 @@ +package com.uam.wmi.findmytutor.utils; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.os.Bundle; + +public class BroadcastLocalizationHandler extends BroadcastReceiver { + public static String str_receiver = "servicetutorial.service.receiver"; + + private IntentFilter filter = + new IntentFilter(str_receiver); + + + @Override + public void onReceive(Context arg0, Intent arg1) { + //tutaj operujemy na naszym powiadomieniu, dane które zostały nam przekazane wyciągamy z parametru arg1. + + } + + + +} diff --git a/build.gradle b/build.gradle index 7eb8ea1..d9fe839 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.4' + classpath 'com.android.tools.build:gradle:3.2.0' // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index efbeabf..71be508 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Aug 12 23:17:46 CEST 2018 +#Sat Sep 29 16:56:56 CEST 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip