/** * Enable or disable location in settings to a specific mode. * * <p>This will attempt to enable/disable every type of location setting (e.g. high and balanced * power). * * <p>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. */ public boolean setLocationMode(int mode) { 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. // 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); }
/** * Enable or disable location in settings. * * <p>This will attempt to enable/disable every type of location setting (e.g. high and balanced * power). * * <p>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. */ public boolean setLocationEnabled(boolean enabled) { int currentUserId = ActivityManager.getCurrentUser(); if (isUserLocationRestricted(currentUserId)) { return false; } final ContentResolver cr = mContext.getContentResolver(); // Store last active mode if we are switching off // so we can restore it at the next enable if (!enabled) mLastActiveMode = getLocationCurrentState(); // 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 ? mLastActiveMode : 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); }