FileDocCategorySizeDatePackage
LocationControllerImpl.javaAPI DocAndroid 5.1 API8959Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.policy

LocationControllerImpl

public class LocationControllerImpl extends android.content.BroadcastReceiver implements LocationController
A controller to manage changes of location related states and update the views accordingly.

Fields Summary
public static final String
LOCATION_STATUS_ICON_PLACEHOLDER
public static final int
LOCATION_STATUS_ICON_ID
private static final int[]
mHighPowerRequestAppOpArray
private android.content.Context
mContext
private android.app.AppOpsManager
mAppOpsManager
private android.app.StatusBarManager
mStatusBarManager
private boolean
mAreActiveLocationRequests
private ArrayList
mSettingsChangeCallbacks
Constructors Summary
public LocationControllerImpl(android.content.Context context)


       
        mContext = context;

        IntentFilter filter = new IntentFilter();
        filter.addAction(LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION);
        context.registerReceiverAsUser(this, UserHandle.ALL, filter, null, null);

        mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        mStatusBarManager
                = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);

        // Register to listen for changes in location settings.
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LocationManager.MODE_CHANGED_ACTION);
        context.registerReceiverAsUser(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (LocationManager.MODE_CHANGED_ACTION.equals(action)) {
                    locationSettingsChanged();
                }
            }
        }, UserHandle.ALL, intentFilter, null, new Handler());

        // Examine the current location state and initialize the status view.
        updateActiveLocationRequests();
        refreshViews();
    
Methods Summary
public voidaddSettingsChangedCallback(LocationSettingsChangeCallback cb)
Add a callback to listen for changes in location settings.

        mSettingsChangeCallbacks.add(cb);
        locationSettingsChanged(cb);
    
private booleanareActiveHighPowerLocationRequests()
Returns true if there currently exist active high power location requests.

        List<AppOpsManager.PackageOps> packages
            = mAppOpsManager.getPackagesForOps(mHighPowerRequestAppOpArray);
        // AppOpsManager can return null when there is no requested data.
        if (packages != null) {
            final int numPackages = packages.size();
            for (int packageInd = 0; packageInd < numPackages; packageInd++) {
                AppOpsManager.PackageOps packageOp = packages.get(packageInd);
                List<AppOpsManager.OpEntry> opEntries = packageOp.getOps();
                if (opEntries != null) {
                    final int numOps = opEntries.size();
                    for (int opInd = 0; opInd < numOps; opInd++) {
                        AppOpsManager.OpEntry opEntry = opEntries.get(opInd);
                        // AppOpsManager should only return OP_MONITOR_HIGH_POWER_LOCATION because
                        // of the mHighPowerRequestAppOpArray filter, but checking defensively.
                        if (opEntry.getOp() == AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION) {
                            if (opEntry.isRunning()) {
                                return true;
                            }
                        }
                    }
                }
            }
        }

        return false;
    
public booleanisLocationEnabled()
Returns true if location isn't disabled in settings.

        ContentResolver resolver = mContext.getContentResolver();
        // QuickSettings always runs as the owner, so specifically retrieve the settings
        // for the current foreground user.
        int mode = Settings.Secure.getIntForUser(resolver, Settings.Secure.LOCATION_MODE,
                Settings.Secure.LOCATION_MODE_OFF, ActivityManager.getCurrentUser());
        return mode != Settings.Secure.LOCATION_MODE_OFF;
    
private booleanisUserLocationRestricted(int userId)
Returns true if the current user is restricted from using location.

        final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
        return um.hasUserRestriction(
                UserManager.DISALLOW_SHARE_LOCATION,
                new UserHandle(userId));
    
private voidlocationSettingsChanged()

        boolean isEnabled = isLocationEnabled();
        for (LocationSettingsChangeCallback cb : mSettingsChangeCallbacks) {
            cb.onLocationSettingsChanged(isEnabled);
        }
    
private voidlocationSettingsChanged(LocationSettingsChangeCallback cb)

        cb.onLocationSettingsChanged(isLocationEnabled());
    
public voidonReceive(android.content.Context context, android.content.Intent intent)

        final String action = intent.getAction();
        if (LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)) {
            updateActiveLocationRequests();
        }
    
private voidrefreshViews()

        if (mAreActiveLocationRequests) {
            mStatusBarManager.setIcon(LOCATION_STATUS_ICON_PLACEHOLDER, LOCATION_STATUS_ICON_ID, 0,
                    mContext.getString(R.string.accessibility_location_active));
        } else {
            mStatusBarManager.removeIcon(LOCATION_STATUS_ICON_PLACEHOLDER);
        }
    
public voidremoveSettingsChangedCallback(LocationSettingsChangeCallback cb)

        mSettingsChangeCallbacks.remove(cb);
    
public booleansetLocationEnabled(boolean enabled)
Enable or disable location in settings.

This will attempt to enable/disable every type of location setting (e.g. high and balanced power).

If enabling, a user consent dialog will pop up prompting the user to accept. If the user doesn't accept, network location won't be enabled.

return
true if attempt to change setting was successful.

        int currentUserId = ActivityManager.getCurrentUser();
        if (isUserLocationRestricted(currentUserId)) {
            return false;
        }
        final ContentResolver cr = mContext.getContentResolver();
        // When enabling location, a user consent dialog will pop up, and the
        // setting won't be fully enabled until the user accepts the agreement.
        int mode = enabled
                ? Settings.Secure.LOCATION_MODE_HIGH_ACCURACY : Settings.Secure.LOCATION_MODE_OFF;
        // QuickSettings always runs as the owner, so specifically set the settings
        // for the current foreground user.
        return Settings.Secure
                .putIntForUser(cr, Settings.Secure.LOCATION_MODE, mode, currentUserId);
    
private voidupdateActiveLocationRequests()

        boolean hadActiveLocationRequests = mAreActiveLocationRequests;
        mAreActiveLocationRequests = areActiveHighPowerLocationRequests();
        if (mAreActiveLocationRequests != hadActiveLocationRequests) {
            refreshViews();
        }