Files
app_Settings/src/com/android/settings/location/TopLevelLocationPreferenceController.java
Joel Galenson db516d98c5 Only get apps with permission granted.
The subtitle gives the number of apps that can access location, but it
currently collects all apps that request location, including those
that are denied.  Ensure that we only count granted apps.

Bug: 129296799
Test: Open Settings, see the count is much lower.
Change-Id: I9a18bbb62be742731205dab9f5b8e9a703ea030c
2019-04-17 11:12:26 -07:00

105 lines
3.6 KiB
Java

package com.android.settings.location;
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.permission.PermissionControllerManager;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop;
import java.util.Arrays;
public class TopLevelLocationPreferenceController extends BasePreferenceController implements
LifecycleObserver, OnStart, OnStop {
private static final IntentFilter INTENT_FILTER_LOCATION_MODE_CHANGED =
new IntentFilter(LocationManager.MODE_CHANGED_ACTION);
private final LocationManager mLocationManager;
/** Total number of apps that has location permission. */
private int mNumTotal = -1;
private BroadcastReceiver mReceiver;
private Preference mPreference;
public TopLevelLocationPreferenceController(Context context, String preferenceKey) {
super(context, preferenceKey);
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
}
@Override
public int getAvailabilityStatus() {
return AVAILABLE;
}
@Override
public CharSequence getSummary() {
if (mLocationManager.isLocationEnabled()) {
if (mNumTotal == -1) {
return mContext.getString(R.string.location_settings_loading_app_permission_stats);
}
return mContext.getResources().getQuantityString(
R.plurals.location_settings_summary_location_on,
mNumTotal, mNumTotal);
} else {
return mContext.getString(R.string.location_settings_summary_location_off);
}
}
@VisibleForTesting
void setLocationAppCount(int numApps) {
mNumTotal = numApps;
refreshSummary(mPreference);
}
@Override
public void updateState(Preference preference) {
super.updateState(preference);
mPreference = preference;
refreshSummary(preference);
// Bail out if location has been disabled.
if (!mLocationManager.isLocationEnabled()) {
return;
}
mContext.getSystemService(PermissionControllerManager.class).countPermissionApps(
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED,
(numApps) -> {
setLocationAppCount(numApps);
}, null);
}
@Override
public void onStart() {
if (mReceiver == null) {
mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
refreshLocationMode();
}
};
}
mContext.registerReceiver(mReceiver, INTENT_FILTER_LOCATION_MODE_CHANGED);
refreshLocationMode();
}
@Override
public void onStop() {
mContext.unregisterReceiver(mReceiver);
}
private void refreshLocationMode() {
// 'null' is checked inside updateState(), so no need to check here.
updateState(mPreference);
}
}