下面列出了android.os.StrictMode#VmPolicy ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initStrictMode() {
// Complain loudly of strict mode violations when in debug mode.
if (BuildConfig.DEBUG) {
final StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyFlashScreen()
.penaltyLog()
.build();
StrictMode.setThreadPolicy(policy);
final StrictMode.VmPolicy vm_policy = new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setVmPolicy(vm_policy);
logger.info("Strict Mode in effect for the main thread");
}
}
@Override
public void onCreate() {
super.onCreate();
DaggerDaggerComponent.builder()
.syncthingModule(new SyncthingModule(this))
.build()
.inject(this);
new Languages(this).setLanguage(this);
// Set VM policy to avoid crash when sending folder URI to file manager.
StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build();
StrictMode.setVmPolicy(policy);
}
@Override
public void onCreate() {
super.onCreate();
Settings.init(this);
final StrictMode.VmPolicy vmPolicy = new StrictMode.VmPolicy.Builder().build();
StrictMode.setVmPolicy(vmPolicy);
}
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);
}
}
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);
}
}
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);
}
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;
}
}
}
private StrictModeContext(StrictMode.ThreadPolicy threadPolicy, StrictMode.VmPolicy vmPolicy) {
mThreadPolicy = threadPolicy;
mVmPolicy = vmPolicy;
}
private StrictModeContext(StrictMode.VmPolicy vmPolicy) {
this(null, vmPolicy);
}
/**
* 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);
}