Merge "Cache summary text to avoid flickering" into tm-dev
This commit is contained in:
@@ -31,9 +31,9 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
|||||||
private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
|
private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
|
||||||
new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
|
new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
|
||||||
private final LocationManager mLocationManager;
|
private final LocationManager mLocationManager;
|
||||||
/** Total number of apps that has location permission. */
|
|
||||||
private int mNumTotal = -1;
|
|
||||||
private int mNumTotalLoading = 0;
|
private int mNumTotalLoading = 0;
|
||||||
|
/** Summary text. */
|
||||||
|
private static String sSummary = null;
|
||||||
private BroadcastReceiver mReceiver;
|
private BroadcastReceiver mReceiver;
|
||||||
private Preference mPreference;
|
private Preference mPreference;
|
||||||
private AtomicInteger loadingInProgress = new AtomicInteger(0);
|
private AtomicInteger loadingInProgress = new AtomicInteger(0);
|
||||||
@@ -51,12 +51,11 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
|||||||
@Override
|
@Override
|
||||||
public CharSequence getSummary() {
|
public CharSequence getSummary() {
|
||||||
if (mLocationManager.isLocationEnabled()) {
|
if (mLocationManager.isLocationEnabled()) {
|
||||||
if (mNumTotal == -1) {
|
if (sSummary == null) {
|
||||||
return mContext.getString(R.string.location_settings_loading_app_permission_stats);
|
sSummary = mContext.getString(
|
||||||
|
R.string.location_settings_loading_app_permission_stats);
|
||||||
}
|
}
|
||||||
return mContext.getResources().getQuantityString(
|
return sSummary;
|
||||||
R.plurals.location_settings_summary_location_on,
|
|
||||||
mNumTotal, mNumTotal);
|
|
||||||
} else {
|
} else {
|
||||||
return mContext.getString(R.string.location_settings_summary_location_off);
|
return mContext.getString(R.string.location_settings_summary_location_off);
|
||||||
}
|
}
|
||||||
@@ -64,7 +63,8 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
|||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
void setLocationAppCount(int numApps) {
|
void setLocationAppCount(int numApps) {
|
||||||
mNumTotal = numApps;
|
sSummary = mContext.getResources().getQuantityString(
|
||||||
|
R.plurals.location_settings_summary_location_on, numApps, numApps);
|
||||||
refreshSummary(mPreference);
|
refreshSummary(mPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -56,31 +56,41 @@ public class TopLevelLocationPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSummary_whenLocationIsOn_shouldShowLoadingString() {
|
public void getSummary_whenLocationIsOn_shouldPreservePreviousText() {
|
||||||
|
final int locationAppCount = 5;
|
||||||
|
// Retrieve summary text once.
|
||||||
|
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
||||||
|
mController.setLocationAppCount(locationAppCount);
|
||||||
|
mController.getSummary();
|
||||||
|
// Turn off location.
|
||||||
|
mLocationManager.setLocationEnabledForUser(false, android.os.Process.myUserHandle());
|
||||||
|
// Turn on location again and check if the previous summary text is still cached.
|
||||||
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
||||||
assertThat(mController.getSummary()).isEqualTo(
|
assertThat(mController.getSummary()).isEqualTo(
|
||||||
mContext.getString(R.string.location_settings_loading_app_permission_stats));
|
mContext.getResources().getQuantityString(
|
||||||
|
R.plurals.location_settings_summary_location_on, locationAppCount,
|
||||||
|
locationAppCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSummary_whenLocationAppCountIsOne_shouldShowSingularString() {
|
public void getSummary_whenLocationAppCountIsOne_shouldShowSingularString() {
|
||||||
final int LOCATION_APP_COUNT = 1;
|
final int locationAppCount = 1;
|
||||||
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
||||||
mController.setLocationAppCount(LOCATION_APP_COUNT);
|
mController.setLocationAppCount(locationAppCount);
|
||||||
assertThat(mController.getSummary()).isEqualTo(
|
assertThat(mController.getSummary()).isEqualTo(
|
||||||
mContext.getResources().getQuantityString(
|
mContext.getResources().getQuantityString(
|
||||||
R.plurals.location_settings_summary_location_on,
|
R.plurals.location_settings_summary_location_on,
|
||||||
LOCATION_APP_COUNT, LOCATION_APP_COUNT));
|
locationAppCount, locationAppCount));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString() {
|
public void getSummary_whenLocationAppCountIsGreaterThanOne_shouldShowPluralString() {
|
||||||
final int LOCATION_APP_COUNT = 5;
|
final int locationAppCount = 5;
|
||||||
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
mLocationManager.setLocationEnabledForUser(true, android.os.Process.myUserHandle());
|
||||||
mController.setLocationAppCount(LOCATION_APP_COUNT);
|
mController.setLocationAppCount(locationAppCount);
|
||||||
assertThat(mController.getSummary()).isEqualTo(
|
assertThat(mController.getSummary()).isEqualTo(
|
||||||
mContext.getResources().getQuantityString(
|
mContext.getResources().getQuantityString(
|
||||||
R.plurals.location_settings_summary_location_on,
|
R.plurals.location_settings_summary_location_on,
|
||||||
LOCATION_APP_COUNT, LOCATION_APP_COUNT));
|
locationAppCount, locationAppCount));
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user