public int appNotResponding(String processName, int pid, String processStats) { if (mVerbose > 0) { StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.err.println("// NOT RESPONDING: " + processName + " (pid " + pid + ")"); System.err.println(processStats); StrictMode.setThreadPolicy(savedPolicy); } synchronized (Monkey.this) { mRequestAnrTraces = true; mRequestDumpsysMemInfo = true; mRequestProcRank = true; if (mRequestBugreport) { mRequestAnrBugreport = true; mReportProcessName = processName; } } if (!mIgnoreTimeouts) { synchronized (Monkey.this) { mAbort = true; } return (mKillProcessAfterError) ? -1 : 0; } return 0; }
private static void tryObtainingDataDirLockOrDie() { StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { String dataPath = PathUtils.getDataDirectory(ContextUtils.getApplicationContext()); File lockFile = new File(dataPath, EXCLUSIVE_LOCK_FILE); boolean success = false; try { // Note that the file is not closed intentionally. RandomAccessFile file = new RandomAccessFile(lockFile, "rw"); sExclusiveFileLock = file.getChannel().tryLock(); success = sExclusiveFileLock != null; } catch (IOException e) { Log.w(TAG, "Failed to create lock file " + lockFile, e); } if (!success) { Log.w( TAG, "The app may have another WebView opened in a separate process. " + "This is not recommended and may stop working in future versions."); } } finally { StrictMode.setThreadPolicy(oldPolicy); } }
public boolean appCrashed( String processName, int pid, String shortMsg, String longMsg, long timeMillis, String stackTrace) { if (mVerbose > 0) { StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.err.println("// CRASH: " + processName + " (pid " + pid + ")"); System.err.println("// Short Msg: " + shortMsg); System.err.println("// Long Msg: " + longMsg); System.err.println("// Build Label: " + Build.FINGERPRINT); System.err.println("// Build Changelist: " + Build.VERSION.INCREMENTAL); System.err.println("// Build Time: " + Build.TIME); System.err.println("// " + stackTrace.replace("\n", "\n// ")); StrictMode.setThreadPolicy(savedPolicy); } if (!mIgnoreCrashes || mRequestBugreport) { synchronized (Monkey.this) { if (!mIgnoreCrashes) { mAbort = true; } if (mRequestBugreport) { mRequestAppCrashBugreport = true; mReportProcessName = processName; } } return !mKillProcessAfterError; } return false; }
public boolean activityResuming(String pkg) { boolean allow = checkEnteringPackage(pkg) || (DEBUG_ALLOW_ANY_RESTARTS != 0); if (mVerbose > 0) { StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.out.println(" // activityResuming(" + pkg + ")"); if (!allow) { System.out.println( " // " + (allow ? "Allowing" : "Rejecting") + " resume of package " + pkg); } StrictMode.setThreadPolicy(savedPolicy); } currentPackage = pkg; return allow; }
/** * Performs all prefs migrations in the background thread to avoid StrictMode exceptions from * reading/writing in the UI thread. This method will block the current thread until the migration * is finished. */ private static synchronized void migrateIfNecessary(final Context context) { if (migrationDone) { return; } // We deliberately perform the migration in the current thread (which // is likely the UI thread) as this is actually cheaper than enforcing a // context switch to another thread (see bug 940575). // Avoid strict mode warnings when doing so. final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { performMigration(context); } finally { StrictMode.setThreadPolicy(savedPolicy); } migrationDone = true; }
/** Saves the tab data out to a file. */ void saveState(File activityDirectory) { File tabFile = getTabFile(activityDirectory, getId()); FileOutputStream foutput = null; // Temporarily allowing disk access while fixing. TODO: http://crbug.com/525781 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { foutput = new FileOutputStream(tabFile); TabState.saveState(foutput, getState(), false); } catch (FileNotFoundException exception) { Log.e(TAG, "Failed to save out tab state.", exception); } catch (IOException exception) { Log.e(TAG, "Failed to save out tab state.", exception); } finally { StreamUtil.closeQuietly(foutput); StrictMode.setThreadPolicy(oldPolicy); } }
public boolean activityStarting(Intent intent, String pkg) { boolean allow = checkEnteringPackage(pkg) || (DEBUG_ALLOW_ANY_STARTS != 0); if (mVerbose > 0) { // StrictMode's disk checks end up catching this on // userdebug/eng builds due to PrintStream going to a // FileOutputStream in the end (perhaps only when // redirected to a file?) So we allow disk writes // around this region for the monkey to minimize // harmless dropbox uploads from monkeys. StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); System.out.println( " // " + (allow ? "Allowing" : "Rejecting") + " start of " + intent + " in package " + pkg); StrictMode.setThreadPolicy(savedPolicy); } currentPackage = pkg; currentIntent = intent; return allow; }
@Override public Notification build() { // A note about RemoteViews and updating notifications. When a notification is passed to the // {@code NotificationManager} with the same tag and id as a previous notification, an // in-place update will be performed. In that case, the actions of all new // {@link RemoteViews} will be applied to the views of the old notification. This is safe // for actions that overwrite old values such as setting the text of a {@code TextView}, but // care must be taken for additive actions. Especially in the case of // {@link RemoteViews#addView} the result could be to append new views below stale ones. In // that case {@link RemoteViews#removeAllViews} must be called before adding new ones. RemoteViews compactView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification); RemoteViews bigView = new RemoteViews(mContext.getPackageName(), R.layout.web_notification_big); float fontScale = mContext.getResources().getConfiguration().fontScale; bigView.setInt(R.id.body, "setMaxLines", calculateMaxBodyLines(fontScale)); int scaledPadding = calculateScaledPadding(fontScale, mContext.getResources().getDisplayMetrics()); String formattedTime = ""; // Temporarily allowing disk access. TODO: Fix. See http://crbug.com/577185 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { long time = SystemClock.elapsedRealtime(); formattedTime = DateFormat.getTimeFormat(mContext).format(new Date()); RecordHistogram.recordTimesHistogram( "Android.StrictMode.NotificationUIBuildTime", SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS); } finally { StrictMode.setThreadPolicy(oldPolicy); } for (RemoteViews view : new RemoteViews[] {compactView, bigView}) { view.setTextViewText(R.id.time, formattedTime); view.setTextViewText(R.id.title, mTitle); view.setTextViewText(R.id.body, mBody); view.setTextViewText(R.id.origin, mOrigin); view.setImageViewBitmap(R.id.icon, getNormalizedLargeIcon()); view.setViewPadding(R.id.title, 0, scaledPadding, 0, 0); view.setViewPadding(R.id.body_container, 0, scaledPadding, 0, scaledPadding); addWorkProfileBadge(view); int smallIconId = useMaterial() ? R.id.small_icon_overlay : R.id.small_icon_footer; view.setViewVisibility(smallIconId, View.VISIBLE); if (mSmallIconBitmap != null) { view.setImageViewBitmap(smallIconId, mSmallIconBitmap); } else { view.setImageViewResource(smallIconId, mSmallIconId); } } addActionButtons(bigView); configureSettingsButton(bigView); // Note: this is not a NotificationCompat builder so be mindful of the // API level of methods you call on the builder. Notification.Builder builder = new Notification.Builder(mContext); builder.setTicker(mTickerText); builder.setContentIntent(mContentIntent); builder.setDeleteIntent(mDeleteIntent); builder.setDefaults(mDefaults); builder.setVibrate(mVibratePattern); builder.setWhen(mTimestamp); builder.setOnlyAlertOnce(!mRenotify); builder.setContent(compactView); // Some things are duplicated in the builder to ensure the notification shows correctly on // Wear devices and custom lock screens. builder.setContentTitle(mTitle); builder.setContentText(mBody); builder.setSubText(mOrigin); builder.setLargeIcon(getNormalizedLargeIcon()); setSmallIconOnBuilder(builder, mSmallIconId, mSmallIconBitmap); for (Action action : mActions) { addActionToBuilder(builder, action); } if (mSettingsAction != null) { addActionToBuilder(builder, mSettingsAction); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Notification.Builder.setPublicVersion was added in Android L. builder.setPublicVersion(createPublicNotification(mContext)); } Notification notification = builder.build(); notification.bigContentView = bigView; return notification; }