Add broadcast receiver

This commit is contained in:
Mieszko Wrzeszczyński 2018-09-29 17:36:36 +02:00
parent c73325dd4e
commit b591587e6a
10 changed files with 143 additions and 68 deletions

View File

@ -16,6 +16,14 @@
android:theme="@style/AppTheme">
<receiver android:name=".utils.BroadcastLocalizationHandler">
<intent-filter>
<action android:name="com.uam.wmi.findmytutor">
</action>
</intent-filter>
</receiver>
<activity android:name=".activity.StartupActivity"
android:label="@string/title_activity_startup"

View File

@ -343,7 +343,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
@Override
protected Boolean doInBackground(Void... params) {
LdapUser user = new LdapUser("string",mPassword,"string",(isTutor)?"tutor":"student","string","string",mEmail);
LdapUser user = new LdapUser("admin@admin",mPassword,"admin",(isTutor)?"Tutor":"Student","string","string",mEmail);
disposable.add(
ldapService

View File

@ -19,6 +19,10 @@ import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.LdapUser;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.GoogleService;
import android.support.design.widget.FloatingActionButton;
@ -31,9 +35,23 @@ import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.LdapService;
import com.uam.wmi.findmytutor.service.LocationService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.io.IOException;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
@ -44,17 +62,24 @@ public class MainActivity extends AppCompatActivity {
private ProfileFragment profileFragment;
public static Intent backgroundLocalizationService = null;
;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
Double latitude, longitude;
/* CompositeDisposable disposable = new CompositeDisposable();
CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
fn_permission();
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
@ -113,39 +138,41 @@ public class MainActivity extends AppCompatActivity {
});
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();
}
fn_permission();
this.broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
latitude = intent.getDoubleExtra("latitude",0);
longitude = intent.getDoubleExtra("longitude",0);
Log.e("Background service", String.valueOf(latitude));
}
};
//You should find out what causes this fancy/not reproducible error
/* Coordinate coordinate = new Coordinate(latitude);
disposable.add(coordinateService.postCoordinate(coordinate)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableSingleObserver<Coordinate>() {
@Override
public void onSuccess(Coordinate coordinate) {
Log.e("Service", "onSuccess: " + coordinate);
}
/*backgroundLocalizationService = new Intent(getApplicationContext(), GoogleService.class);
startService(backgroundLocalizationService);*/
/* if (boolean_permission) {
if (!PrefUtils.getIsServiceRunning(getApplicationContext())){
backgroundLocalizationService = new Intent(getApplicationContext(), GoogleService.class);
startService(backgroundLocalizationService);
} else{
Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
@Override
public void onError(Throwable e) {
Log.e("Service", "onError: " + e.getMessage());
}
}));*/
}
} else {
fn_permission();
}*/
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);
}
}

View File

@ -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;
}

View File

@ -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();

View File

@ -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<List<Coordinate>> getOnlineCoordinates();
// Create note
@FormUrlEncoded
@POST("api/coordinates")
Call<ResponseBody> postCoordinate(@Body Coordinate coordinate);
Single <Coordinate> postCoordinate(@Body Coordinate coordinate);
/*
// Create note
@FormUrlEncoded
@POST("api/coordinates")
Observable <Coordinate> postCoordinate(@Field("latitude") Double latitude);
*/
@PUT("api/coordinates/{id}")
Call<Coordinate> putCoordinatesById(@Path("id") String id);

View File

@ -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);
}
}

View File

@ -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.
}
}

View File

@ -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

View File

@ -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