Merge background task into develop #14
@ -26,7 +26,7 @@ dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||||
implementation 'com.android.support:appcompat-v7:27.1.1'
|
||||
implementation 'com.android.support:design:27.1.1'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
|
||||
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
|
||||
testImplementation 'junit:junit:4.12'
|
||||
androidTestImplementation 'com.android.support.test:runner:1.0.2'
|
||||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
|
||||
@ -45,5 +45,6 @@ dependencies {
|
||||
implementation "com.squareup.okhttp3:okhttp:3.11.0"
|
||||
implementation "com.squareup.okhttp3:okhttp-urlconnection:3.10.0"
|
||||
implementation "com.squareup.okhttp3:logging-interceptor:3.11.0"
|
||||
implementation "com.google.android.gms:play-services-location:11.0.4"
|
||||
implementation 'com.auth0.android:jwtdecode:1.1.1'
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
@ -14,6 +15,8 @@
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme">
|
||||
|
||||
|
||||
|
||||
<activity android:name=".activity.StartupActivity"
|
||||
android:label="@string/title_activity_startup"
|
||||
android:launchMode="singleInstance">
|
||||
@ -38,8 +41,8 @@
|
||||
</activity>
|
||||
|
||||
<service
|
||||
android:name=".service.GoogleService"
|
||||
android:exported="false" />
|
||||
android:name=".service.GoogleService"/>
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -31,6 +31,8 @@ import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
import android.widget.ToggleButton;
|
||||
|
||||
import com.auth0.android.jwt.Claim;
|
||||
import com.auth0.android.jwt.JWT;
|
||||
import com.uam.wmi.findmytutor.R;
|
||||
import com.uam.wmi.findmytutor.model.JwtToken;
|
||||
import com.uam.wmi.findmytutor.model.LdapUser;
|
||||
@ -331,16 +333,18 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
||||
UserLoginTask(String email, String password, Boolean loginOption) {
|
||||
mEmail = email;
|
||||
mPassword = password;
|
||||
isTutor=loginOption;
|
||||
isTutor = loginOption;
|
||||
this.ldapService = ApiClient.getClient(getApplicationContext())
|
||||
.create(LdapService.class);
|
||||
this.isAuthorizate = false;
|
||||
|
||||
PrefUtils.storeIsTutor(getApplicationContext(), this.isTutor);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params) {
|
||||
LdapUser user = new LdapUser("string",mPassword,"string",(isTutor)?"tutor":"student","string","string",mEmail);
|
||||
Log.d("LoginRole","Login role is : " + user.getTitle());
|
||||
|
||||
disposable.add(
|
||||
ldapService
|
||||
.fakeValidate(user)
|
||||
@ -349,7 +353,12 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
|
||||
.subscribeWith(new DisposableSingleObserver<JwtToken>() {
|
||||
@Override
|
||||
public void onSuccess(JwtToken jwtToken) {
|
||||
PrefUtils.storeApiKey(getApplicationContext(), jwtToken.getToken());
|
||||
String token = jwtToken.getToken();
|
||||
JWT jwt = new JWT(token);
|
||||
|
||||
PrefUtils.storeApiKey(getApplicationContext(), token);
|
||||
PrefUtils.storeUserId(getApplicationContext(), jwt);
|
||||
|
||||
onPostExecute(true);
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.uam.wmi.findmytutor.activity;
|
||||
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
@ -9,27 +8,20 @@ import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Address;
|
||||
import android.location.Geocoder;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.design.widget.BottomNavigationView;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.uam.wmi.findmytutor.service.GoogleService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentTransaction;
|
||||
|
||||
@ -37,12 +29,10 @@ import android.support.annotation.NonNull;
|
||||
|
||||
import android.view.MenuItem;
|
||||
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.maps.MapView;
|
||||
import com.uam.wmi.findmytutor.R;
|
||||
import com.uam.wmi.findmytutor.model.Coordinate;
|
||||
|
||||
import com.uam.wmi.findmytutor.service.LocationService;
|
||||
import com.uam.wmi.findmytutor.utils.PrefUtils;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@ -52,38 +42,22 @@ public class MainActivity extends AppCompatActivity {
|
||||
private MapFragment mapFragment;
|
||||
private NotificationFragment notificationFragment;
|
||||
private ProfileFragment profileFragment;
|
||||
|
||||
public static Intent backgroundLocalizationService = null;
|
||||
|
||||
private static final int REQUEST_PERMISSIONS = 100;
|
||||
boolean boolean_permission;
|
||||
TextView tv_latitude, tv_longitude, tv_address, tv_area, tv_locality;
|
||||
SharedPreferences mPref;
|
||||
SharedPreferences.Editor medit;
|
||||
private boolean boolean_permission = false;
|
||||
|
||||
Double latitude, longitude;
|
||||
Geocoder geocoder;
|
||||
|
||||
|
||||
private MapView mapView;
|
||||
|
||||
public List<Coordinate> getCoordinates() {
|
||||
return this.coordinates;
|
||||
}
|
||||
|
||||
public void setCoordinates(List<Coordinate> coordinates) {
|
||||
this.coordinates = coordinates;
|
||||
}
|
||||
|
||||
public List<Coordinate> coordinates;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
Mapbox.getInstance(this, getString(R.string.access_token));
|
||||
setContentView(R.layout.activity_main);
|
||||
Bundle extras = getIntent().getExtras();
|
||||
fn_permission();
|
||||
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
|
||||
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
|
||||
isTutor = (boolean) extras.get("is_tutor");
|
||||
isTutor = PrefUtils.getIsTutor(getApplicationContext());
|
||||
|
||||
if (!isTutor) {
|
||||
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
|
||||
@ -97,7 +71,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
setFragment(mapFragment);
|
||||
mMainNav.setSelectedItemId(R.id.nav_map);
|
||||
|
||||
/* code below is resposible for changing colours of tabs in main tab menu */
|
||||
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||
@ -124,6 +97,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
PrefUtils.storeIsServiceRunning(getApplicationContext(), false);
|
||||
|
||||
stopService(MainActivity.backgroundLocalizationService);
|
||||
SharedPreferences preferences = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = preferences.edit();
|
||||
editor.clear().commit();
|
||||
@ -136,15 +112,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
geocoder = new Geocoder(this, Locale.getDefault());
|
||||
mPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
||||
medit = mPref.edit();
|
||||
|
||||
if (boolean_permission) {
|
||||
|
||||
if (mPref.getString("service", "").matches("")) {
|
||||
medit.putString("service", "service").commit();
|
||||
|
||||
if (!PrefUtils.getIsServiceRunning(getApplicationContext())) {
|
||||
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
|
||||
startService(intent);
|
||||
|
||||
@ -153,30 +123,36 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
|
||||
|
||||
}
|
||||
|
||||
fn_permission();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*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();
|
||||
}
|
||||
|
||||
} else {
|
||||
fn_permission();
|
||||
}*/
|
||||
|
||||
|
||||
private void setFragment(Fragment fragment) {
|
||||
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
|
||||
fragmentTransaction.replace(R.id.main_frame, fragment);
|
||||
fragmentTransaction.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
unregisterReceiver(broadcastReceiver);
|
||||
}
|
||||
|
||||
private void fn_permission() {
|
||||
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
|
||||
|
||||
@ -212,38 +188,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
|
||||
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
|
||||
latitude = Double.valueOf(intent.getStringExtra("latutide"));
|
||||
longitude = Double.valueOf(intent.getStringExtra("longitude"));
|
||||
|
||||
List<Address> addresses = null;
|
||||
|
||||
try {
|
||||
addresses = geocoder.getFromLocation(latitude, longitude, 1);
|
||||
String cityName = addresses.get(0).getAddressLine(0);
|
||||
String stateName = addresses.get(0).getAddressLine(1);
|
||||
String countryName = addresses.get(0).getAddressLine(2);
|
||||
|
||||
tv_area.setText(addresses.get(0).getAdminArea());
|
||||
tv_locality.setText(stateName);
|
||||
tv_address.setText(countryName);
|
||||
|
||||
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
|
||||
tv_latitude.setText(latitude + "");
|
||||
tv_longitude.setText(longitude + "");
|
||||
tv_address.getText();
|
||||
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import android.content.Intent;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
|
||||
import com.uam.wmi.findmytutor.service.ApplicationServiceWatcher;
|
||||
|
||||
|
||||
public class StartupActivity extends AppCompatActivity {
|
||||
private static final int AUTHENTICATION_REQUEST_CODE = 666;
|
||||
@ -13,6 +15,7 @@ public class StartupActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
||||
|
||||
if (isLoggedIn()){
|
||||
Intent startupIntent = new Intent(this, MainActivity.class);
|
||||
startupIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
@ -25,8 +28,6 @@ public class StartupActivity extends AppCompatActivity {
|
||||
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
//to nie ma miec layoutu wiec elo
|
||||
// setContentView(R.layout.activity_startup);
|
||||
}
|
||||
|
||||
private boolean isLoggedIn() {
|
||||
|
@ -0,0 +1,41 @@
|
||||
package com.uam.wmi.findmytutor.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import com.uam.wmi.findmytutor.activity.MainActivity;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class ApplicationServiceWatcher extends Service {
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Timber.e("App Started");
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
;
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
Timber.e("App destroyed");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTaskRemoved(Intent rootIntent) {
|
||||
Timber.e("App killed");
|
||||
android.os.Process.killProcess(android.os.Process.myPid());
|
||||
//Code here
|
||||
stopSelf();
|
||||
}
|
||||
}
|
||||
|
@ -13,8 +13,12 @@ import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.uam.wmi.findmytutor.utils.PrefUtils;
|
||||
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
|
||||
@ -22,7 +26,8 @@ import java.util.TimerTask;
|
||||
* Created by deepshikha on 24/11/16.
|
||||
*/
|
||||
|
||||
public class GoogleService extends Service implements LocationListener {
|
||||
public class GoogleService extends Service implements LocationListener{
|
||||
|
||||
|
||||
boolean isGPSEnable = false;
|
||||
boolean isNetworkEnable = false;
|
||||
@ -36,9 +41,7 @@ public class GoogleService extends Service implements LocationListener {
|
||||
Intent intent;
|
||||
|
||||
|
||||
public GoogleService() {
|
||||
|
||||
}
|
||||
public GoogleService() {}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
@ -96,8 +99,18 @@ public class GoogleService extends Service implements LocationListener {
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
|
||||
if (locationManager!=null){
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, (LocationListener) this);
|
||||
if (locationManager != null) {
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
// int[] grantResults)
|
||||
// to handle the case where the user grants the permission. See the documentation
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
if (location!=null){
|
||||
|
||||
@ -115,14 +128,13 @@ public class GoogleService extends Service implements LocationListener {
|
||||
|
||||
if (isGPSEnable){
|
||||
location = null;
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0, (LocationListener) this);
|
||||
if (locationManager!=null){
|
||||
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
|
||||
if (location!=null){
|
||||
Log.e("latitude",location.getLatitude()+"");
|
||||
Log.e("longitude",location.getLongitude()+"");
|
||||
latitude = location.getLatitude();
|
||||
longitude = location.getLongitude();
|
||||
|
||||
fn_update(location);
|
||||
}
|
||||
}
|
||||
@ -132,6 +144,11 @@ 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
|
||||
@ -148,11 +165,11 @@ public class GoogleService extends Service implements LocationListener {
|
||||
}
|
||||
|
||||
private void fn_update(Location location){
|
||||
|
||||
intent.putExtra("latutide",location.getLatitude()+"");
|
||||
intent.putExtra("longitude",location.getLongitude()+"");
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,173 @@
|
||||
package com.uam.wmi.findmytutor.service;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Location;
|
||||
import android.location.LocationListener;
|
||||
import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class LocationService extends Service {
|
||||
public static final String BROADCAST_ACTION = "Hello World";
|
||||
private static final int TWO_MINUTES = 1000 * 60 * 2;
|
||||
public LocationManager locationManager;
|
||||
public MyLocationListener listener;
|
||||
public Location previousBestLocation = null;
|
||||
|
||||
Intent intent;
|
||||
int counter = 0;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
intent = new Intent(BROADCAST_ACTION);
|
||||
|
||||
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
||||
listener = new MyLocationListener();
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
// TODO: Consider calling
|
||||
// ActivityCompat#requestPermissions
|
||||
// here to request the missing permissions, and then overriding
|
||||
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||
// int[] grantResults)
|
||||
// to handle the case where the user grants the permission. See the documentation
|
||||
// for ActivityCompat#requestPermissions for more details.
|
||||
return;
|
||||
}
|
||||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
|
||||
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
|
||||
if (currentBestLocation == null) {
|
||||
// A new location is always better than no location
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check whether the new location fix is newer or older
|
||||
long timeDelta = location.getTime() - currentBestLocation.getTime();
|
||||
boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
|
||||
boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
|
||||
boolean isNewer = timeDelta > 0;
|
||||
|
||||
// If it's been more than two minutes since the current location, use the new location
|
||||
// because the user has likely moved
|
||||
if (isSignificantlyNewer) {
|
||||
return true;
|
||||
// If the new location is more than two minutes older, it must be worse
|
||||
} else if (isSignificantlyOlder) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check whether the new location fix is more or less accurate
|
||||
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
|
||||
boolean isLessAccurate = accuracyDelta > 0;
|
||||
boolean isMoreAccurate = accuracyDelta < 0;
|
||||
boolean isSignificantlyLessAccurate = accuracyDelta > 200;
|
||||
|
||||
// Check if the old and new location are from the same provider
|
||||
boolean isFromSameProvider = isSameProvider(location.getProvider(),
|
||||
currentBestLocation.getProvider());
|
||||
|
||||
// Determine location quality using a combination of timeliness and accuracy
|
||||
if (isMoreAccurate) {
|
||||
return true;
|
||||
} else if (isNewer && !isLessAccurate) {
|
||||
return true;
|
||||
} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** Checks whether two providers are the same */
|
||||
private boolean isSameProvider(String provider1, String provider2) {
|
||||
if (provider1 == null) {
|
||||
return provider2 == null;
|
||||
}
|
||||
return provider1.equals(provider2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
// handler.removeCallbacks(sendUpdatesToUI);
|
||||
super.onDestroy();
|
||||
Log.v("STOP_SERVICE", "DONE");
|
||||
locationManager.removeUpdates((LocationListener) listener);
|
||||
}
|
||||
|
||||
public static Thread performOnBackgroundThread(final Runnable runnable) {
|
||||
final Thread t = new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runnable.run();
|
||||
} finally {
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public class MyLocationListener implements LocationListener
|
||||
{
|
||||
|
||||
public void onLocationChanged(final Location loc)
|
||||
{
|
||||
Log.i("*******", "Location changed");
|
||||
if(isBetterLocation(loc, previousBestLocation)) {
|
||||
loc.getLatitude();
|
||||
loc.getLongitude();
|
||||
|
||||
Log.e("service", String.valueOf(loc));
|
||||
intent.putExtra("Latitude", loc.getLatitude());
|
||||
intent.putExtra("Longitude", loc.getLongitude());
|
||||
intent.putExtra("Provider", loc.getProvider());
|
||||
sendBroadcast(intent);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void onProviderDisabled(String provider)
|
||||
{
|
||||
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
|
||||
}
|
||||
|
||||
|
||||
public void onProviderEnabled(String provider)
|
||||
{
|
||||
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
|
||||
public void onStatusChanged(String provider, int status, Bundle extras)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
package com.uam.wmi.findmytutor.service;
|
||||
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.gms.location.LocationResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Receiver for handling location updates.
|
||||
*
|
||||
* For apps targeting API level O
|
||||
* {@link android.app.PendingIntent#getBroadcast(Context, int, Intent, int)} should be used when
|
||||
* requesting location updates. Due to limits on background services,
|
||||
* {@link android.app.PendingIntent#getService(Context, int, Intent, int)} should not be used.
|
||||
*
|
||||
* Note: Apps running on "O" devices (regardless of targetSdkVersion) may receive updates
|
||||
* less frequently than the interval specified in the
|
||||
* {@link com.google.android.gms.location.LocationRequest} when the app is no longer in the
|
||||
* foreground.
|
||||
*/
|
||||
public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
|
||||
private static final String TAG = "LUBroadcastReceiver";
|
||||
|
||||
public static final String ACTION_PROCESS_UPDATES =
|
||||
"com.google.android.gms.location.sample.backgroundlocationupdates.action" +
|
||||
".PROCESS_UPDATES";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent != null) {
|
||||
final String action = intent.getAction();
|
||||
if (ACTION_PROCESS_UPDATES.equals(action)) {
|
||||
LocationResult result = LocationResult.extractResult(intent);
|
||||
if (result != null) {
|
||||
List<Location> locations = result.getLocations();
|
||||
LocationResultHelper locationResultHelper = new LocationResultHelper(
|
||||
context, locations);
|
||||
// Save the location data to SharedPreferences.
|
||||
locationResultHelper.saveResults();
|
||||
// Show notification with the location data.
|
||||
locationResultHelper.showNotification();
|
||||
Log.i(TAG, LocationResultHelper.getSavedLocationResult(context));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,74 +0,0 @@
|
||||
package com.uam.wmi.findmytutor.service;/*
|
||||
* Copyright 2017 Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.location.Location;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.android.gms.location.LocationResult;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Handles incoming location updates and displays a notification with the location data.
|
||||
*
|
||||
* For apps targeting API level 25 ("Nougat") or lower, location updates may be requested
|
||||
* using {@link android.app.PendingIntent#getService(Context, int, Intent, int)} or
|
||||
* {@link android.app.PendingIntent#getBroadcast(Context, int, Intent, int)}. For apps targeting
|
||||
* API level O, only {@code getBroadcast} should be used.
|
||||
*
|
||||
* Note: Apps running on "O" devices (regardless of targetSdkVersion) may receive updates
|
||||
* less frequently than the interval specified in the
|
||||
* {@link com.google.android.gms.location.LocationRequest} when the app is no longer in the
|
||||
* foreground.
|
||||
*/
|
||||
public class LocationUpdatesIntentService extends IntentService {
|
||||
|
||||
static final String ACTION_PROCESS_UPDATES =
|
||||
"com.google.android.gms.location.sample.backgroundlocationupdates.action" +
|
||||
".PROCESS_UPDATES";
|
||||
private static final String TAG = LocationUpdatesIntentService.class.getSimpleName();
|
||||
|
||||
|
||||
public LocationUpdatesIntentService() {
|
||||
// Name the worker thread.
|
||||
super(TAG);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
if (intent != null) {
|
||||
final String action = intent.getAction();
|
||||
if (ACTION_PROCESS_UPDATES.equals(action)) {
|
||||
LocationResult result = LocationResult.extractResult(intent);
|
||||
if (result != null) {
|
||||
List<Location> locations = result.getLocations();
|
||||
LocationResultHelper locationResultHelper = new LocationResultHelper(this,
|
||||
locations);
|
||||
// Save the location data to SharedPreferences.
|
||||
locationResultHelper.saveResults();
|
||||
// Show notification with the location data.
|
||||
locationResultHelper.showNotification();
|
||||
Log.i(TAG, LocationResultHelper.getSavedLocationResult(this));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,10 @@ package com.uam.wmi.findmytutor.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import com.auth0.android.jwt.Claim;
|
||||
import com.auth0.android.jwt.JWT;
|
||||
|
||||
public class PrefUtils {
|
||||
/**
|
||||
@ -24,4 +28,35 @@ public class PrefUtils {
|
||||
public static String getApiKey(Context context) {
|
||||
return getSharedPreferences(context).getString("API_KEY", null);
|
||||
}
|
||||
|
||||
public static void storeUserId(Context applicationContext, JWT jwtToken) {
|
||||
SharedPreferences.Editor editor = getSharedPreferences(applicationContext).edit();
|
||||
Claim role = jwtToken.getClaim("userid");
|
||||
editor.putString("USER_ID", role.asString());
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
public static String getUserId(Context context) {
|
||||
return getSharedPreferences(context).getString("USER_ID", null);
|
||||
}
|
||||
|
||||
public static void storeIsTutor(Context applicationContext, boolean isTutor) {
|
||||
SharedPreferences.Editor editor = getSharedPreferences(applicationContext).edit();
|
||||
editor.putBoolean("IS_TUTOR", isTutor);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
public static boolean getIsTutor(Context context) {
|
||||
return getSharedPreferences(context).getBoolean("IS_TUTOR", false);
|
||||
}
|
||||
|
||||
public static void storeIsServiceRunning(Context applicationContext, boolean isRunning) {
|
||||
SharedPreferences.Editor editor = getSharedPreferences(applicationContext).edit();
|
||||
editor.putBoolean("IS_BACKGROUND_SERVICE_RUNNING", isRunning);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
public static boolean getIsServiceRunning(Context context) {
|
||||
return getSharedPreferences(context).getBoolean("IS_BACKGROUND_SERVICE_RUNNING", false);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user