Add FusedLocalizationApi

This commit is contained in:
Mieszko Wrzeszczyński 2018-11-06 23:50:13 +01:00
parent b23d82d74d
commit 1094ef942b
13 changed files with 56 additions and 33 deletions

View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/app/src/main/java/com/uam/wmi/findmytutor/activity/MapFragment.java" charset="UTF-8" />
</component>
</project>

View File

@ -57,6 +57,7 @@ dependencies {
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'com.auth0.android:jwtdecode:1.1.1'
implementation 'com.annimon:stream:1.2.1'
implementation 'com.google.android.gms:play-services-location:16.0.0'
// FloatingBarMenu
implementation 'com.getbase:floatingactionbutton:1.10.1'

View File

@ -50,6 +50,10 @@
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings" />
<activity
android:name=".service.GoogleLocalizationActivity"
android:label="@string/title_activity_settings" />
<service
android:name=".service.BackgroundLocalizationService"
android:exported="false"

View File

@ -183,13 +183,14 @@ public abstract class BaseActivity
}
}
public void handleBackgroundTaskLifeCycle() {
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())
&& !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
Boolean shouldServiceRun = PrefUtils.isEnableSharingLocalization(getApplicationContext())
&& !PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext());
if (shouldServiceRun){
startBackgroundLocalizationTask();
} else if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext())) {
} else if(PrefUtils.isBackgroundLocationServiceRunning(getApplicationContext()) &&
!PrefUtils.isEnableSharingLocalization(getApplicationContext())) {
stopBackgroundLocalizationTask();
}
}
@ -296,12 +297,6 @@ public abstract class BaseActivity
overridePendingTransition(0, 0);
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_content, fragment);
fragmentTransaction.commit();
}
private void removeFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(fragment);

View File

@ -86,7 +86,7 @@ import static com.uam.wmi.findmytutor.R.id.activity_content;
public class MapActivity extends BaseActivity
implements PermissionsListener, OnMapReadyCallback {
String currentLanguage = "pl", currentLang;
private String tag = getClass().getName();
private PermissionsManager permissionsManager;
private LocationComponent locationComponent;
@ -137,8 +137,6 @@ public class MapActivity extends BaseActivity
//start background task
handleBackgroundTaskLifeCycle();
currentLanguage = getIntent().getStringExtra(currentLang);
}

View File

@ -52,6 +52,13 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
addPreferencesFromResource(R.layout.pref_main);
Preference languagesList = findPreference(getResources().getString(R.string.key_language));
languagesList.setDefaultValue(0);
if(PrefUtils.getLocale(getActivity()).equals("pl")){
languagesList.setDefaultValue(1);
}
languagesList.setOnPreferenceChangeListener((preference, newValue) -> {

View File

@ -46,8 +46,4 @@ public class StartupActivity extends AppCompatActivity {
finish();
}
}

View File

@ -105,7 +105,6 @@ public class UsersListFragment extends Fragment {
}
}));
}
public void searchUser(String textToSearch) {
@ -217,11 +216,13 @@ public class UsersListFragment extends Fragment {
}
private void showError(Throwable e) {
String message = e.toString();
String message;
if (e instanceof HttpException) {
ResponseBody responseBody = ((HttpException) e).response().errorBody();
message = RestApiHelper.getErrorMessage(responseBody);
}else{
message = "Network Error !";
}
Snackbar.make(coordinatorLayout, message, Snackbar.LENGTH_LONG)

View File

@ -23,6 +23,9 @@ import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.network.ApiClient;
@ -31,6 +34,7 @@ import com.uam.wmi.findmytutor.utils.RestApiHelper;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
@ -58,6 +62,7 @@ public class BackgroundLocalizationService extends Service {
private Handler mHandler = new Handler();
private HandlerThread mHandlerThread = null;
private Runnable mStatusChecker;
private FusedLocationProviderClient mFusedLocationClient;
public BackgroundLocalizationService() {
providers.add(LocationManager.GPS_PROVIDER);
@ -107,6 +112,9 @@ public class BackgroundLocalizationService extends Service {
startForeground(1001, notification);
}
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
initializeLocationManager();
Integer providerIndex = 0;
@ -194,8 +202,21 @@ public class BackgroundLocalizationService extends Service {
Log.e("Best localization:", String.valueOf(bestLocation));
if (bestLocation != null)
/* if (bestLocation != null)
fn_update(bestLocation);
*/
mFusedLocationClient.getLastLocation().addOnSuccessListener(
new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
mLastLocation = location;
fn_update(location);
}
}
});
}
private void fn_update(Location location) {
@ -321,7 +342,5 @@ public class BackgroundLocalizationService extends Service {
return null;
}
}
}

View File

@ -20,7 +20,6 @@ public class PrefUtils {
return context.getSharedPreferences("com.uam.wmi.findmytutor_preferences", Context.MODE_PRIVATE);
}
public static void getAllKeys(Context context){
Map<String,?> keys = getSharedPreferences(context).getAll();
@ -35,9 +34,6 @@ public class PrefUtils {
editor.clear().apply();
}
public static void storeApiKey(Context context, String apiKey) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("API_KEY", apiKey);
@ -150,5 +146,4 @@ public class PrefUtils {
editor.putBoolean("BACKGROUND_SERVICE_STATUS", status);
editor.apply();
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>

View File

@ -189,5 +189,10 @@
<string name="action_log_in">Log in </string>
<string name="user_list_nav">Users list</string>
<string name="error_invalid_login_name">Invalid login format.</string>
<string name="title_locale_utils">Locale utils</string>
<string name="permission_denied_explanation">Permission denied</string>
<string name="permission_rationale">permission should be granted</string>
<string name="launch_activity" />
<string name="remove_location_updates" />
</resources>