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() { public void handleBackgroundTaskLifeCycle(boolean isLocationEnabled) {
if (PrefUtils.isEnableSharingLocalization(getApplicationContext())) { if (isLocationEnabled) {
startBackgroundLocalizationTask(); startBackgroundLocalizationTask();
} else { } else {
stopBackgroundLocalizationTask(); stopBackgroundLocalizationTask();

View File

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

View File

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

View File

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

View File

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

View File

@ -2,8 +2,10 @@ package com.uam.wmi.findmytutor.utils;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.ListPreference;
import android.preference.PreferenceFragment;
import android.util.Log; import android.util.Log;
import com.uam.wmi.findmytutor.activity.SharingFragment;
import com.auth0.android.jwt.Claim; import com.auth0.android.jwt.Claim;
import com.auth0.android.jwt.JWT; import com.auth0.android.jwt.JWT;
@ -15,7 +17,7 @@ public class PrefUtils {
} }
public static SharedPreferences getSharedPreferences(Context context) { 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) { 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) { public static void storeIsTutor(Context applicationContext, boolean isTutor) {
@ -79,12 +86,20 @@ public class PrefUtils {
} }
public static boolean isEnableSharingLocalization(Context context) { 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) { public static void storeEnableSharingLocalization(Context context,Boolean isChecked) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit(); 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(); editor.apply();
} }

View File

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

View File

@ -123,6 +123,11 @@
<string name="action_log_in">Zaloguj!</string> <string name="action_log_in">Zaloguj!</string>
<string name="error_invalid_login_name">Niepoprawny format loginu.</string> <string name="error_invalid_login_name">Niepoprawny format loginu.</string>
<string name="user_list_nav">Lista użytkowników</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> </resources>

View File

@ -47,6 +47,8 @@
<string name="title_sharing">Sharing</string> <string name="title_sharing">Sharing</string>
<string name="settings_category_location">Location 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="title_location_level">Sharing level</string>
<string name="location_level_presence">Only presence</string> <string name="location_level_presence">Only presence</string>
<string name="location_level_approximated">Approximated</string> <string name="location_level_approximated">Approximated</string>