Java Code Examples for android.provider.Settings#checkAndNoteWriteSettingsOperation()

The following examples show how to use android.provider.Settings#checkAndNoteWriteSettingsOperation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PowerManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Set the setting that determines whether the device stays on when plugged in.
 * The argument is a bit string, with each bit specifying a power source that,
 * when the device is connected to that source, causes the device to stay on.
 * See {@link android.os.BatteryManager} for the list of power sources that
 * can be specified. Current values include
 * {@link android.os.BatteryManager#BATTERY_PLUGGED_AC}
 * and {@link android.os.BatteryManager#BATTERY_PLUGGED_USB}
 *
 * Used by "adb shell svc power stayon ..."
 *
 * @param val an {@code int} containing the bits that specify which power sources
 * should cause the device to stay on.
 */
@Override // Binder call
public void setStayOnSetting(int val) {
    int uid = Binder.getCallingUid();
    // if uid is of root's, we permit this operation straight away
    if (uid != Process.ROOT_UID) {
        if (!Settings.checkAndNoteWriteSettingsOperation(mContext, uid,
                Settings.getPackageNameForUid(mContext, uid), true)) {
            return;
        }
    }

    final long ident = Binder.clearCallingIdentity();
    try {
        setStayOnSettingInternal(val);
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example 2
Source File: ConnectivityManager.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/** {@hide} */
public static final void enforceTetherChangePermission(Context context, String callingPkg) {
    Preconditions.checkNotNull(context, "Context cannot be null");
    Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");

    if (context.getResources().getStringArray(
            com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
        // Have a provisioning app - must only let system apps (which check this app)
        // turn on tethering
        context.enforceCallingOrSelfPermission(
                android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
    } else {
        int uid = Binder.getCallingUid();
        // If callingPkg's uid is not same as Binder.getCallingUid(),
        // AppOpsService throws SecurityException.
        Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
                true /* throwException */);
    }
}
 
Example 3
Source File: SettingsProvider.java    From Study_Android_Demo with Apache License 2.0 4 votes vote down vote up
private boolean mutateSystemSetting(String name, String value, int runAsUserId,
        int operation) {
    if (!hasWriteSecureSettingsPermission()) {
        // If the caller doesn't hold WRITE_SECURE_SETTINGS, we verify whether this
        // operation is allowed for the calling package through appops.
        if (!Settings.checkAndNoteWriteSettingsOperation(getContext(),
                Binder.getCallingUid(), getCallingPackage(), true)) {
            return false;
        }
    }

    // Resolve the userId on whose behalf the call is made.
    final int callingUserId = resolveCallingUserIdEnforcingPermissionsLocked(runAsUserId);

    // Enforce what the calling package can mutate the system settings.
    enforceRestrictedSystemSettingsMutationForCallingPackage(operation, name, callingUserId);

    // Determine the owning user as some profile settings are cloned from the parent.
    final int owningUserId = resolveOwningUserIdForSystemSettingLocked(callingUserId, name);

    // Only the owning user id can change the setting.
    if (owningUserId != callingUserId) {
        return false;
    }

    // Invalidate any relevant cache files
    String cacheName = null;
    if (Settings.System.RINGTONE.equals(name)) {
        cacheName = Settings.System.RINGTONE_CACHE;
    } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
        cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
    } else if (Settings.System.ALARM_ALERT.equals(name)) {
        cacheName = Settings.System.ALARM_ALERT_CACHE;
    }
    if (cacheName != null) {
        final File cacheFile = new File(
                getRingtoneCacheDir(owningUserId), cacheName);
        cacheFile.delete();
    }

    // Mutate the value.
    synchronized (mLock) {
        switch (operation) {
            case MUTATION_OPERATION_INSERT: {
                validateSystemSettingValue(name, value);
                return mSettingsRegistry.insertSettingLocked(SETTINGS_TYPE_SYSTEM,
                        owningUserId, name, value, null, false, getCallingPackage(),
                        false, null);
            }

            case MUTATION_OPERATION_DELETE: {
                return mSettingsRegistry.deleteSettingLocked(SETTINGS_TYPE_SYSTEM,
                        owningUserId, name, false, null);
            }

            case MUTATION_OPERATION_UPDATE: {
                validateSystemSettingValue(name, value);
                return mSettingsRegistry.updateSettingLocked(SETTINGS_TYPE_SYSTEM,
                        owningUserId, name, value, null, false, getCallingPackage(),
                        false, null);
            }
        }

        return false;
    }
}