adjusted background task and shared pref file to work together throught PrefUtils

This commit is contained in:
Marcin Jedynski 2018-11-06 03:04:17 +01:00
parent 98ee8d1a21
commit 5757a58edf
9 changed files with 90 additions and 22 deletions

View File

@ -190,8 +190,8 @@ public abstract class BaseActivity
}
}
public void handleBackgroundTaskLifeCycle() {
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())) {
public void handleBackgroundTaskLifeCycle(boolean isLocationEnabled) {
if (isLocationEnabled) {
startBackgroundLocalizationTask();
} else {
stopBackgroundLocalizationTask();

View File

@ -188,7 +188,8 @@ public class MapActivity extends BaseActivity
latLng.getLongitude(),
latLng.getAltitude(),
PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext())
PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext())
);
disposable.add(

View File

@ -14,13 +14,17 @@ import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@ -28,25 +32,51 @@ import static com.mapbox.mapboxsdk.Mapbox.getApplicationContext;
public class SharingFragment extends PreferenceFragment {
private HashMap<Integer, String> locationLevelMapping;
// private HashMap<Integer, String> statusMapping;
@SuppressLint("ResourceType")
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationLevelMapping = new HashMap<Integer, String>();
locationLevelMapping.put(0,"presence");
locationLevelMapping.put(1,"approximated");
locationLevelMapping.put(2,"exact");
/* statusMapping = new HashMap<Integer, String>();
statusMapping.put(0,"presence");
statusMapping.put(1,"approximated");
statusMapping.put(2,"exact");*/
addPreferencesFromResource(R.layout.pref_sharing);
Preference manualStatus = findPreference("key_manual_status");
Preference locationSharing = findPreference("key_sharing_enabled");
Preference locationMode = findPreference("key_location_level");
Preference statusList = findPreference("key_status_value");
manualStatus.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) findPreference("key_status_value");
updateListPreference(lp, newValue, "manual_statuses");
return true;
});
locationMode.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) preference;
PrefUtils.storeLocationMode(getApplicationContext(),locationLevelMapping.get(Integer.parseInt((String) newValue)));
return true;
});
statusList.setOnPreferenceChangeListener((preference, newValue) -> {
ListPreference lp = (ListPreference) preference;
CharSequence [] entries = lp.getEntries();
PrefUtils.storeStatus(getApplicationContext(),(String) entries[Integer.parseInt((String) newValue)]);
return true;
});
locationSharing.setOnPreferenceChangeListener((buttonView, isChecked) -> {
PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) isChecked);
((MapActivity)getActivity()).handleBackgroundTaskLifeCycle();
locationSharing.setOnPreferenceChangeListener((buttonView, newValue) -> {
// PrefUtils.storeEnableSharingLocalization(getApplicationContext(), (Boolean) isChecked);
// Toast.makeText(getApplicationContext(), Boolean.toString(PrefUtils.isEnableSharingLocalization(getApplicationContext())), Toast.LENGTH_LONG).show();
((MapActivity)getActivity()).handleBackgroundTaskLifeCycle((Boolean) newValue);
return true;
});
}
@ -64,15 +94,22 @@ public class SharingFragment extends PreferenceFragment {
return view;
}
public String getListPreferenceValue(String key){
ListPreference lp = (ListPreference) findPreference(key);
return (String)lp.getEntry();
}
protected void updateListPreference(ListPreference lp,Object newValue,String storageKey){
CharSequence [] entries = lp.getEntries();
Set<String> defaultEntries = new HashSet(Arrays.asList(entries));
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
Set <String> manualStatusSet = sharedPref.getStringSet(storageKey,defaultEntries);
manualStatusSet.add((String) newValue);
String [] manualStatusArr = manualStatusSet.toArray(new String[0]);
Arrays.sort(manualStatusArr);
setListPreferenceData(lp.getKey(),manualStatusArr);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putStringSet(storageKey,manualStatusSet);
editor.apply();
@ -84,7 +121,7 @@ public class SharingFragment extends PreferenceFragment {
CharSequence[] entryValues = new CharSequence [entries.length];
for (int i = 0; i < entries.length; i++){
entryValues[i] = Integer.toString(i+1);
entryValues[i] = Integer.toString(i);
}
lp.setDefaultValue("1");

View File

@ -44,7 +44,7 @@ public class Coordinate extends BaseResponse {
@SerializedName("label")
private String label;
public Coordinate (Double latitude, Double longitude, Double altitude, String label, String userId) {
public Coordinate (Double latitude, Double longitude, Double altitude, String label, String userId, String displayMode) {
//if (!latitudeRange.contains(latitude)) throw new IllegalArgumentException("Inappropriate latitude value" + latitude);
//if (!longtitudeRange.contains(longitude)) throw new IllegalArgumentException("Inappropriate longitude value" + longitude);
@ -53,6 +53,7 @@ public class Coordinate extends BaseResponse {
this.altitude = altitude;
this.label = label;
this.userId = userId;
this.displayMode = displayMode;
}
public Coordinate (Double latitude) {

View File

@ -283,8 +283,10 @@ public class BackgroundLocalizationService extends Service {
latitude,
longitude,
altitude,
PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext())
PrefUtils.getUserFirstName(getApplicationContext()) + " " + PrefUtils.getUserLastName(getApplicationContext()) + " " + PrefUtils.getUserStatus(getApplicationContext()),
PrefUtils.getUserId(getApplicationContext()),
PrefUtils.getLocationLevel(getApplicationContext())
);
disposable.add(

View File

@ -2,8 +2,10 @@ package com.uam.wmi.findmytutor.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.util.Log;
import com.uam.wmi.findmytutor.activity.SharingFragment;
import com.auth0.android.jwt.Claim;
import com.auth0.android.jwt.JWT;
@ -15,7 +17,7 @@ public class PrefUtils {
}
public static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences("APP_PREF", Context.MODE_PRIVATE);
return context.getSharedPreferences("com.uam.wmi.findmytutor_preferences", Context.MODE_PRIVATE);
}
@ -55,7 +57,12 @@ public class PrefUtils {
public static String getUserStatus(Context context) {
return getSharedPreferences(context).getString("USER_STATUS", "Android");
return getSharedPreferences(context).getString("status_entry", "Available");
}
public static void storeStatus(Context context, String status){
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("status_entry", status);
editor.apply();
}
public static void storeIsTutor(Context applicationContext, boolean isTutor) {
@ -79,12 +86,20 @@ public class PrefUtils {
}
public static boolean isEnableSharingLocalization(Context context) {
return getSharedPreferences(context).getBoolean("IS_ENABLE_SHARING_LOCALIZATION", false);
return getSharedPreferences(context).getBoolean("key_sharing_enabled", false);
}
public static void storeEnableSharingLocalization(Context context,Boolean isChecked) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("IS_ENABLE_SHARING_LOCALIZATION", isChecked);
editor.putBoolean("key_sharing_enabled", isChecked);
editor.apply();
}
public static String getLocationLevel(Context context){
return getSharedPreferences(context).getString("location_mode", "exact");
}
public static void storeLocationMode(Context context, String mode){
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putString("location_mode", mode);
editor.apply();
}

View File

@ -5,6 +5,7 @@
tools:ignore="MissingDefaultResource"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory android:title="@string/settings_category_location">
<SwitchPreference
android:defaultValue="false"
@ -20,7 +21,10 @@
android:key="@string/key_location_level"
android:summary="%s"
android:title="@string/title_location_level" />
<SwitchPreference
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_category_status">
<SwitchPreference
android:defaultValue="false"
android:disableDependentsState="false"
android:key="key_status_enabled"
@ -39,7 +43,10 @@
android:singleLine="true"
android:title="@string/title_manual_status"
/>
<SwitchPreference
</PreferenceCategory>
<PreferenceCategory android:title="@string/settings_category_manuallocation">
<SwitchPreference
android:defaultValue="false"
android:disableDependentsState="false"
android:key="key_manual_location_enabled"
@ -47,7 +54,7 @@
android:title="@string/manual_location"/>
<ListPreference
android:defaultValue="1"
android:key="manual_location_list_title"
android:key="key_manual_location_value"
android:entries="@array/manual_location_entries"
android:entryValues="@array/manual_location_values"
android:summary="%s"
@ -58,8 +65,6 @@
android:singleLine="true"
android:title="@string/title_manual_location"
/>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -123,6 +123,11 @@
<string name="action_log_in">Zaloguj!</string>
<string name="error_invalid_login_name">Niepoprawny format loginu.</string>
<string name="user_list_nav">Lista użytkowników</string>
<string name="select_a_location">Wybierz lokalizacje</string>
<string name="user_location_permission_not_granted">Nie dodano uprawnień do lokalizacji.</string>
<string name="user_location_permission_explanation">Ta aplikacja potrzebuje uprawnień do lokalizacji.</string>
<string name="settings_category_status">Ustawienia statusu</string>
<string name="settings_category_manuallocation">Ręczny wybór lokalizacji</string>
</resources>

View File

@ -47,6 +47,8 @@
<string name="title_sharing">Sharing</string>
<string name="settings_category_location">Location sharing</string>
<string name="settings_category_status">Status settings</string>
<string name="settings_category_manuallocation">Manual location override</string>
<string name="title_location_level">Sharing level</string>
<string name="location_level_presence">Only presence</string>
<string name="location_level_approximated">Approximated</string>