Skip to content

Commit

Permalink
Show not set message when device details aren't available
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Feb 24, 2020
1 parent ddd2c07 commit caae730
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.fragment.app.FragmentActivity;
import androidx.preference.EditTextPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -88,9 +91,30 @@ private void initNormalPrefs() {

private void initDangerousPrefs() {
PropertyManager propertyManager = new PropertyManager(getActivity());
findPreference(PROPMGR_DEVICE_ID).setSummaryProvider(preference -> propertyManager.reload(getActivity()).getSingularProperty(PROPMGR_DEVICE_ID));
findPreference(PROPMGR_SIM_SERIAL).setSummaryProvider(preference -> propertyManager.reload(getActivity()).getSingularProperty(PROPMGR_SIM_SERIAL));
findPreference(PROPMGR_SUBSCRIBER_ID).setSummaryProvider(preference -> propertyManager.reload(getActivity()).getSingularProperty(PROPMGR_SUBSCRIBER_ID));
findPreference(KEY_METADATA_PHONENUMBER).setSummaryProvider(preference -> propertyManager.reload(getActivity()).getSingularProperty(PROPMGR_PHONE_NUMBER));
findPreference(PROPMGR_DEVICE_ID).setSummaryProvider(new PropertyManagerPropertySummaryProvider(propertyManager, PROPMGR_DEVICE_ID));
findPreference(PROPMGR_SIM_SERIAL).setSummaryProvider(new PropertyManagerPropertySummaryProvider(propertyManager, PROPMGR_SIM_SERIAL));
findPreference(PROPMGR_SUBSCRIBER_ID).setSummaryProvider(new PropertyManagerPropertySummaryProvider(propertyManager, PROPMGR_SUBSCRIBER_ID));
findPreference(KEY_METADATA_PHONENUMBER).setSummaryProvider(new PropertyManagerPropertySummaryProvider(propertyManager, PROPMGR_PHONE_NUMBER));
}

private class PropertyManagerPropertySummaryProvider implements Preference.SummaryProvider<EditTextPreference> {

private final PropertyManager propertyManager;
private final String propertyKey;

PropertyManagerPropertySummaryProvider(PropertyManager propertyManager, String propertyName) {
this.propertyManager = propertyManager;
this.propertyKey = propertyName;
}

@Override
public CharSequence provideSummary(EditTextPreference preference) {
String value = propertyManager.reload(getActivity()).getSingularProperty(propertyKey);
if (!TextUtils.isEmpty(value)) {
return value;
} else {
return getString(R.string.preference_not_set);
}
}
}
}
1 change: 1 addition & 0 deletions collect_app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,5 @@
<string name="scoped_storage_banner_text">You must migrate Collect forms to private storage by August 2020 to comply with Android requirements.</string>
<string name="scoped_storage_learn_more">Learn more and migrate</string>
<string name="install_id">Install ID - will replace Device ID in August 2020</string>
<string name="preference_not_set">Not set</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.odk.collect.android.R;
import org.odk.collect.android.injection.config.AppDependencyModule;
import org.odk.collect.android.listeners.PermissionListener;
import org.odk.collect.android.support.RobolectricHelpers;
Expand All @@ -22,7 +23,10 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.odk.collect.android.logic.PropertyManager.PROPMGR_DEVICE_ID;
import static org.odk.collect.android.logic.PropertyManager.PROPMGR_SIM_SERIAL;
import static org.odk.collect.android.logic.PropertyManager.PROPMGR_SUBSCRIBER_ID;
import static org.odk.collect.android.preferences.GeneralKeys.KEY_METADATA_PHONENUMBER;
import static org.robolectric.annotation.LooperMode.Mode.PAUSED;

@RunWith(AndroidJUnit4.class)
Expand Down Expand Up @@ -81,6 +85,25 @@ public void recreating_whenPermissionsGrantedPreviously_doesNotShowPermissionDep
verifyNoInteractions(deviceDetailsProvider);
}

@Test
public void whenDeviceDetailsAreMissing_preferenceSummariesAreNotSet() {
when(deviceDetailsProvider.getSimSerialNumber()).thenReturn(null);
when(deviceDetailsProvider.getLine1Number()).thenReturn(null);
when(deviceDetailsProvider.getSubscriberId()).thenReturn(null);
when(deviceDetailsProvider.getDeviceId()).thenReturn(null);

FragmentScenario<FormMetadataFragment> scenario = FragmentScenario.launch(FormMetadataFragment.class);
permissionUtils.grant();
scenario.onFragment(fragment -> {
String notSetMessage = fragment.getContext().getString(R.string.preference_not_set);

assertThat(fragment.findPreference(PROPMGR_SIM_SERIAL).getSummary(), equalTo(notSetMessage));
assertThat(fragment.findPreference(KEY_METADATA_PHONENUMBER).getSummary(), equalTo(notSetMessage));
assertThat(fragment.findPreference(PROPMGR_SUBSCRIBER_ID).getSummary(), equalTo(notSetMessage));
assertThat(fragment.findPreference(PROPMGR_DEVICE_ID).getSummary(), equalTo(notSetMessage));
});
}

private static class FakePhoneStatePermissionUtils extends PermissionUtils {

int timesRequested;
Expand Down

0 comments on commit caae730

Please sign in to comment.