Java Code Examples for android.os.StrictMode#getVmPolicy()

The following examples show how to use android.os.StrictMode#getVmPolicy() . 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: ScreenRecordByCodecActivity.java    From ScreenCapture with MIT License 5 votes vote down vote up
private void stopRecordingAndOpenFile(Context context) {
    File file = new File(mRecorder.getSavedPath());
    stopRecorder();
    StrictMode.VmPolicy vmPolicy = StrictMode.getVmPolicy();
    try {
        // disable detecting FileUriExposure on public file
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
        viewResult(file);
    } finally {
        StrictMode.setVmPolicy(vmPolicy);
    }
}
 
Example 2
Source File: ScreenRecordActivity.java    From ScreenCapture with MIT License 5 votes vote down vote up
private void playVideo() {
//        if (mPlayer == null) {
//            mPlayer = new MediaPlayer();
//        } else {
//            mPlayer.stop();
//            mPlayer.reset();
//            mPlayer.release();
//        }
//        try {
//            mPlayer.setDataSource(ScreenUtils.VIDEO_PATH);
//            mPlayer.setSurface(mSurface);
//            mPlayer.prepare();
//            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                @Override
//                public void onPrepared(MediaPlayer mp) {
//                    mPlayer.start();
//                }
//            });
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        File file = new File(ScreenUtils.VIDEO_PATH);
        StrictMode.VmPolicy vmPolicy = StrictMode.getVmPolicy();
        try {
            // disable detecting FileUriExposure on public file
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
            viewResult(file);
        } finally {
            StrictMode.setVmPolicy(vmPolicy);
        }
    }
 
Example 3
Source File: DummyActivity.java    From Shelter with Do What The F*ck You Want To Public License 4 votes vote down vote up
private void actionInstallPackage() {
    Uri uri = null;
    if (getIntent().hasExtra("package")) {
        uri = Uri.fromParts("package", getIntent().getStringExtra("package"), null);
    }
    StrictMode.VmPolicy policy = StrictMode.getVmPolicy();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O || getIntent().hasExtra("direct_install_apk")) {
        if (getIntent().hasExtra("apk")) {
            // I really have no idea about why the "package:" uri do not work
            // after Android O, anyway we fall back to using the apk path...
            // Since I have plan to support pre-O in later versions, I keep this
            // branch in case that we reduce minSDK in the future.
            uri = Uri.fromFile(new File(getIntent().getStringExtra("apk")));
        } else if (getIntent().hasExtra("direct_install_apk")) {
            // Directly install an APK inside the profile
            // The APK will be an Uri from our own FileProviderProxy
            // which points to an opened Fd in another profile.
            // We must close the Fd when we finish.
            uri = getIntent().getParcelableExtra("direct_install_apk");
        }

        // A permissive VmPolicy must be set to work around
        // the limitation on cross-application Uri
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().build());
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        try {
            actionInstallPackageQ(uri);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else {
        Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE, uri);
        intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, getPackageName());
        intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
        intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        startActivityForResult(intent, REQUEST_INSTALL_PACKAGE);
    }

    // Restore the VmPolicy anyway
    StrictMode.setVmPolicy(policy);
}
 
Example 4
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
    try {
        StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
        try {
            // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
            // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
            // is enabled by default on NYC.
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
                    .penaltyLog().build());

            if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
                String id = ((ShortcutInfo) info).getDeepShortcutId();
                String packageName = intent.getPackage();
                DeepShortcutManager.getInstance(this).startShortcut(
                        packageName, id, intent.getSourceBounds(), optsBundle, info.user);
            } else {
                // Could be launching some bookkeeping activity
                startActivity(intent, optsBundle);
            }
        } finally {
            StrictMode.setVmPolicy(oldPolicy);
        }
    } catch (SecurityException e) {
        // Due to legacy reasons, direct call shortcuts require Launchers to have the
        // corresponding permission. Show the appropriate permission prompt if that
        // is the case.

        if (AndroidVersion.isAtLeastMarshmallow) {
            if (intent.getComponent() == null
                    && Intent.ACTION_CALL.equals(intent.getAction())
                    && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
                    PackageManager.PERMISSION_GRANTED) {

                setWaitingForResult(PendingRequestArgs
                        .forIntent(REQUEST_PERMISSION_CALL_PHONE, intent, info));
                requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
                        REQUEST_PERMISSION_CALL_PHONE);
        }

        } else {
            // No idea why this was thrown.
            throw e;
        }
    }
}
 
Example 5
Source File: StrictModeContext.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Convenience method for disabling all VM-level StrictMode checks with try-with-resources.
 * Includes everything listed here:
 *     https://developer.android.com/reference/android/os/StrictMode.VmPolicy.Builder.html
 */
public static StrictModeContext allowAllVmPolicies() {
    StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
    StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX);
    return new StrictModeContext(oldPolicy);
}