Java Code Examples for android.content.Context#enforceCallingOrSelfPermission()

The following examples show how to use android.content.Context#enforceCallingOrSelfPermission() . 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: 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 2
Source File: Client.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
private static boolean isBackgroundInstall(Context context) {
	try {
		context.enforceCallingOrSelfPermission(
				android.Manifest.permission.INSTALL_PACKAGES, null);
		return true;
	} catch (SecurityException e) {
		return false;
	}
}
 
Example 3
Source File: DeviceStateReceiver.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {

    PreferencesProviderWrapper prefWrapper = new PreferencesProviderWrapper(context);
    String intentAction = intent.getAction();

    //
    // ACTION_DATA_STATE_CHANGED
    // Data state change is used to detect changes in the mobile
    // network such as a switch of network type (GPRS, EDGE, 3G)
    // which are not detected by the Connectivity changed broadcast.
    //
    //
    // ACTION_CONNECTIVITY_CHANGED
    // Connectivity change is used to detect changes in the overall
    // data network status as well as a switch between wifi and mobile
    // networks.
    //
    if (/*intentAction.equals(ACTION_DATA_STATE_CHANGED) ||*/
            intentAction.equals(ConnectivityManager.CONNECTIVITY_ACTION) ||
            intentAction.equals(Intent.ACTION_BOOT_COMPLETED)) {
        
        if (prefWrapper.isValidConnectionForIncoming()
                &&
                !prefWrapper
                        .getPreferenceBooleanValue(PreferencesProviderWrapper.HAS_BEEN_QUIT)) {
            Log.d(THIS_FILE, "Try to start service if not already started");
            Intent sip_service_intent = new Intent(context, SipService.class);
            context.startService(sip_service_intent);
        }

    } else if (intentAction.equals(SipManager.INTENT_SIP_ACCOUNT_ACTIVATE)) {
        context.enforceCallingOrSelfPermission(SipManager.PERMISSION_CONFIGURE_SIP, null);

        long accId;
        accId = intent.getLongExtra(SipProfile.FIELD_ID, SipProfile.INVALID_ID);

        if (accId == SipProfile.INVALID_ID) {
            // allow remote side to send us integers.
            // previous call will warn, but that's fine, no worries
            accId = intent.getIntExtra(SipProfile.FIELD_ID, (int) SipProfile.INVALID_ID);
        }

        if (accId != SipProfile.INVALID_ID) {
            boolean active = intent.getBooleanExtra(SipProfile.FIELD_ACTIVE, true);
            ContentValues cv = new ContentValues();
            cv.put(SipProfile.FIELD_ACTIVE, active);
            int done = context.getContentResolver().update(
                    ContentUris.withAppendedId(SipProfile.ACCOUNT_ID_URI_BASE, accId), cv,
                    null, null);
            if (done > 0) {
                if (prefWrapper.isValidConnectionForIncoming()) {
                    Intent sipServiceIntent = new Intent(context, SipService.class);
                    context.startService(sipServiceIntent);
                }
            }
        }
    } else if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(intentAction) ||
            Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(intentAction)) {
        CallHandlerPlugin.clearAvailableCallHandlers();
        RewriterPlugin.clearAvailableRewriters();
        ExtraPlugins.clearDynPlugins();
        PhoneCapabilityTester.deinit();
    } else if (APPLY_NIGHTLY_UPLOAD.equals(intentAction)) {
        NightlyUpdater nu = new NightlyUpdater(context);
        nu.applyUpdate(intent);
    }
}
 
Example 4
Source File: FixWakeLock.java    From AppOpsXposed with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable
{
	final IBinder lock = (IBinder) param.args[0];
	final String tag = (String) param.args[2];
	final String packageName = param.args[3] instanceof String ?
			(String) param.args[3] : null;

	final int uid = Binder.getCallingUid();

	// Since we want this hack to replicate the expected behaviour,
	// we have to do some sanity checks first. On error we return
	// and let the hooked function throw an exception.

	if(lock == null || packageName == null)
		return;

	final Context ctx = getContextFromThis(param.thisObject);
	if(ctx == null)
		return;

	ctx.enforceCallingOrSelfPermission(
			android.Manifest.permission.WAKE_LOCK, null);

	final AppOpsManagerWrapper appOps = AppOpsManagerWrapper.from(ctx);
	if(appOps.checkOpNoThrow(AppOpsManagerWrapper.OP_WAKE_LOCK, uid, packageName) != AppOpsManager.MODE_ALLOWED)
	{
		if(tag != null && canAcquire(packageName, tag))
		{
			if(DEBUG)
			{
				log("Allowing acquisition of WakeLock '" + tag +
						"' for app " + packageName);
			}
			return;
		}

		if(DEBUG)
		{
			log("Prevented acquisition of WakeLock '" + tag +
					"' for app " + packageName);
		}

		param.setResult(null);
	}
}