Merge "Update location app stats" into qt-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
5bfa4b24e2
@@ -1,11 +1,12 @@
|
||||
package com.android.settings.location;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_BACKGROUND_LOCATION;
|
||||
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.LocationManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.permission.PermissionControllerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
@@ -13,11 +14,12 @@ import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class AppLocationPermissionPreferenceController extends
|
||||
@@ -29,7 +31,12 @@ public class AppLocationPermissionPreferenceController extends
|
||||
int mNumTotal = -1;
|
||||
/** Total number of apps that has background location permission. */
|
||||
@VisibleForTesting
|
||||
int mNumBackground = -1;
|
||||
int mNumHasLocation = -1;
|
||||
|
||||
final AtomicInteger loadingInProgress = new AtomicInteger(0);
|
||||
private int mNumTotalLoading = 0;
|
||||
private int mNumHasLocationLoading = 0;
|
||||
|
||||
private final LocationManager mLocationManager;
|
||||
private Preference mPreference;
|
||||
|
||||
@@ -52,52 +59,76 @@ public class AppLocationPermissionPreferenceController extends
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
if (mLocationManager.isLocationEnabled()) {
|
||||
if (mNumTotal == -1 || mNumBackground == -1) {
|
||||
if (mNumTotal == -1 || mNumHasLocation == -1) {
|
||||
return mContext.getString(R.string.location_settings_loading_app_permission_stats);
|
||||
}
|
||||
return mContext.getResources().getQuantityString(
|
||||
R.plurals.location_app_permission_summary_location_on, mNumBackground,
|
||||
mNumBackground, mNumTotal);
|
||||
R.plurals.location_app_permission_summary_location_on, mNumHasLocation,
|
||||
mNumHasLocation, mNumTotal);
|
||||
} else {
|
||||
return mContext.getString(R.string.location_app_permission_summary_location_off);
|
||||
}
|
||||
}
|
||||
|
||||
private void setAppCounts(int numTotal, int numHasLocation) {
|
||||
mNumTotal = numTotal;
|
||||
mNumHasLocation = numHasLocation;
|
||||
refreshSummary(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
super.updateState(preference);
|
||||
mPreference = preference;
|
||||
final AtomicInteger loadingInProgress = new AtomicInteger(2);
|
||||
refreshSummary(preference);
|
||||
// Bail out if location has been disabled.
|
||||
if (!mLocationManager.isLocationEnabled()) {
|
||||
// Bail out if location has been disabled, or there's another loading request in progress.
|
||||
if (!mLocationManager.isLocationEnabled() ||
|
||||
loadingInProgress.get() != 0) {
|
||||
return;
|
||||
}
|
||||
PermissionControllerManager permController =
|
||||
mContext.getSystemService(PermissionControllerManager.class);
|
||||
permController.countPermissionApps(
|
||||
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION), 0,
|
||||
(numApps) -> {
|
||||
mNumTotal = numApps;
|
||||
mNumTotalLoading = 0;
|
||||
mNumHasLocationLoading = 0;
|
||||
// Retrieve a list of users inside the current user profile group.
|
||||
final List<UserHandle> users = mContext.getSystemService(
|
||||
UserManager.class).getUserProfiles();
|
||||
loadingInProgress.set(2 * users.size());
|
||||
for (UserHandle user : users) {
|
||||
final Context userContext = Utils.createPackageContextAsUser(mContext,
|
||||
user.getIdentifier());
|
||||
if (userContext == null) {
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
refreshSummary(preference);
|
||||
setAppCounts(mNumTotalLoading, mNumHasLocationLoading);
|
||||
}
|
||||
}, null);
|
||||
|
||||
permController.countPermissionApps(
|
||||
Collections.singletonList(ACCESS_BACKGROUND_LOCATION),
|
||||
PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED,
|
||||
(numApps) -> {
|
||||
mNumBackground = numApps;
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
refreshSummary(preference);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
final PermissionControllerManager permController =
|
||||
userContext.getSystemService(PermissionControllerManager.class);
|
||||
permController.countPermissionApps(
|
||||
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION), 0,
|
||||
(numApps) -> {
|
||||
mNumTotalLoading += numApps;
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
setAppCounts(mNumTotalLoading, mNumHasLocationLoading);
|
||||
}
|
||||
}, null);
|
||||
permController.countPermissionApps(
|
||||
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
|
||||
PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED,
|
||||
(numApps) -> {
|
||||
mNumHasLocationLoading += numApps;
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
setAppCounts(mNumTotalLoading, mNumHasLocationLoading);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationModeChanged(int mode, boolean restricted) {
|
||||
// 'null' is checked inside updateState(), so no need to check here.
|
||||
updateState(mPreference);
|
||||
if (mPreference != null) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -8,18 +8,23 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.location.LocationManager;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.permission.PermissionControllerManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
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;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class TopLevelLocationPreferenceController extends BasePreferenceController implements
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
@@ -28,8 +33,10 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
||||
private final LocationManager mLocationManager;
|
||||
/** Total number of apps that has location permission. */
|
||||
private int mNumTotal = -1;
|
||||
private int mNumTotalLoading = 0;
|
||||
private BroadcastReceiver mReceiver;
|
||||
private Preference mPreference;
|
||||
private AtomicInteger loadingInProgress = new AtomicInteger(0);
|
||||
|
||||
public TopLevelLocationPreferenceController(Context context, String preferenceKey) {
|
||||
super(context, preferenceKey);
|
||||
@@ -66,16 +73,37 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
||||
super.updateState(preference);
|
||||
mPreference = preference;
|
||||
refreshSummary(preference);
|
||||
// Bail out if location has been disabled.
|
||||
if (!mLocationManager.isLocationEnabled()) {
|
||||
// Bail out if location has been disabled, or there's another loading request in progress.
|
||||
if (!mLocationManager.isLocationEnabled() ||
|
||||
loadingInProgress.get() != 0) {
|
||||
return;
|
||||
}
|
||||
mContext.getSystemService(PermissionControllerManager.class).countPermissionApps(
|
||||
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
|
||||
PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED,
|
||||
(numApps) -> {
|
||||
setLocationAppCount(numApps);
|
||||
}, null);
|
||||
mNumTotalLoading = 0;
|
||||
// Retrieve a list of users inside the current user profile group.
|
||||
final List<UserHandle> users = mContext.getSystemService(
|
||||
UserManager.class).getUserProfiles();
|
||||
loadingInProgress.set(users.size());
|
||||
for (UserHandle user : users) {
|
||||
final Context userContext = Utils.createPackageContextAsUser(mContext,
|
||||
user.getIdentifier());
|
||||
if (userContext == null) {
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
setLocationAppCount(mNumTotalLoading);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
final PermissionControllerManager permController =
|
||||
userContext.getSystemService(PermissionControllerManager.class);
|
||||
permController.countPermissionApps(
|
||||
Arrays.asList(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION),
|
||||
PermissionControllerManager.COUNT_ONLY_WHEN_GRANTED,
|
||||
(numApps) -> {
|
||||
mNumTotalLoading += numApps;
|
||||
if (loadingInProgress.decrementAndGet() == 0) {
|
||||
setLocationAppCount(mNumTotalLoading);
|
||||
}
|
||||
}, null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -98,7 +126,8 @@ public class TopLevelLocationPreferenceController extends BasePreferenceControll
|
||||
}
|
||||
|
||||
private void refreshLocationMode() {
|
||||
// 'null' is checked inside updateState(), so no need to check here.
|
||||
updateState(mPreference);
|
||||
if (mPreference != null) {
|
||||
updateState(mPreference);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user