@Override public void onCreate() { super.onCreate(); Log.d(TAG, "onCreate start"); if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); } applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); // Fabric.with(this, new Crashlytics()); Stetho.initializeWithDefaults(this.getApplicationContext()); weatherApplicationInstance = this; //初始化ApiClient ApiConfiguration apiConfiguration = ApiConfiguration.builder() .dataSourceType(ApiConstants.WEATHER_DATA_SOURCE_TYPE_KNOW) .build(); ApiClient.init(apiConfiguration); Log.d(TAG, "onCreate end"); }
/** * Determines the number of cores available on the device (pre-v17). * * <p>Before Jellybean, {@link Runtime#availableProcessors()} returned the number of awake cores, * which may not be the number of available cores depending on the device's current state. See * https://stackoverflow.com/a/30150409. * * @return the maximum number of processors available to the VM; never smaller than one */ @SuppressWarnings("PMD") private static int getCoreCountPre17() { // We override the current ThreadPolicy to allow disk reads. // This shouldn't actually do disk-IO and accesses a device file. // See: https://github.com/bumptech/glide/issues/1170 File[] cpus = null; ThreadPolicy originalPolicy = StrictMode.allowThreadDiskReads(); try { File cpuInfo = new File(CPU_LOCATION); final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX); cpus = cpuInfo.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String s) { return cpuNamePattern.matcher(s).matches(); } }); } catch (Throwable t) { if (Log.isLoggable(TAG, Log.ERROR)) { Log.e(TAG, "Failed to calculate accurate cpu count", t); } } finally { StrictMode.setThreadPolicy(originalPolicy); } return Math.max(1, cpus != null ? cpus.length : 0); }
@Override public synchronized Thread newThread(@NonNull Runnable runnable) { final Thread result = new Thread(runnable, "glide-" + name + "-thread-" + threadNum) { @Override public void run() { android.os.Process.setThreadPriority(DEFAULT_PRIORITY); if (preventNetworkOperations) { StrictMode.setThreadPolicy( new ThreadPolicy.Builder() .detectNetwork() .penaltyDeath() .build()); } try { super.run(); } catch (Throwable t) { uncaughtThrowableStrategy.handle(t); } } }; threadNum++; return result; }
/** * 需要设置哪个页面的限制缓存到VM中 */ @TargetApi(11) public static void enableStrictMode(Class<?> clazz) { if (Utils.hasGingerbread()) { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog(); StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog(); if (Utils.hasHoneycomb()) { threadPolicyBuilder.penaltyFlashScreen(); vmPolicyBuilder.setClassInstanceLimit(clazz, 1); } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); StrictMode.setVmPolicy(vmPolicyBuilder.build()); } }
public static void init(Context context) { // check if android:debuggable is set to true int appFlags = context.getApplicationInfo().flags; if ((appFlags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .penaltyLog() .penaltyDeath() .build()); } }
private void initStrictMode() { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .detectCustomSlowCalls() .penaltyDeath(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { threadPolicyBuilder.detectResourceMismatches(); } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .detectLeakedRegistrationObjects() .detectActivityLeaks() .penaltyDeath() .build() ); }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); setContentView(R.layout.socket_connection_build); ed_host = (EditText) findViewById(R.id.ed_host); ed_port = (EditText) findViewById(R.id.ed_port); btn_connect = (Button) findViewById(R.id.btn_connect); /* Fetch earlier defined host ip and port numbers and write them as default * (to avoid retyping this, typically standard, info) */ SharedPreferences mSharedPreferences = getSharedPreferences("list",MODE_PRIVATE); String oldHost = mSharedPreferences.getString("host", null); if (oldHost != null) ed_host.setText(oldHost); int oldPortCommunicator = mSharedPreferences.getInt("port",0); if (oldPortCommunicator != 0) ed_port.setText(Integer.toString(oldPortCommunicator)); }
/** * @return the public downloads directory. */ @SuppressWarnings("unused") @CalledByNative private static String getDownloadsDirectory(Context appContext) { // Temporarily allowing disk access while fixing. TODO: http://crbug.com/508615 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); String downloadsPath; try { long time = SystemClock.elapsedRealtime(); downloadsPath = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS).getPath(); RecordHistogram.recordTimesHistogram("Android.StrictMode.DownloadsDir", SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS); } finally { StrictMode.setThreadPolicy(oldPolicy); } return downloadsPath; }
/** @see TraceEvent#MaybeEnableEarlyTracing(). */ @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") static void maybeEnable() { boolean shouldEnable = false; // Checking for the trace config filename touches the disk. StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); try { if (CommandLine.isInitialized() && CommandLine.getInstance().hasSwitch("trace-startup")) { shouldEnable = true; } else { try { shouldEnable = (new File(TRACE_CONFIG_FILENAME)).exists(); } catch (SecurityException e) { // Access denied, not enabled. } } } finally { StrictMode.setThreadPolicy(oldPolicy); } if (shouldEnable) enable(); }
@SuppressFBWarnings("DM_GC") // Used to trigger strictmode detecting leaked closeables @Override protected void tearDown() throws Exception { try { NativeTestServer.shutdownNativeTestServer(); mTestFramework.mCronetEngine.shutdown(); assertTrue(mFile.delete()); // Run GC and finalizers a few times to pick up leaked closeables for (int i = 0; i < 10; i++) { System.gc(); System.runFinalization(); } System.gc(); System.runFinalization(); super.tearDown(); } finally { StrictMode.setVmPolicy(mOldVmPolicy); } }
public Thread newThread(final Runnable r) { mNetworkQualityThread = new Thread(new Runnable() { @Override public void run() { StrictMode.ThreadPolicy threadPolicy = StrictMode.getThreadPolicy(); try { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectNetwork() .penaltyLog() .penaltyDeath() .build()); r.run(); } finally { StrictMode.setThreadPolicy(threadPolicy); } } }); return mNetworkQualityThread; }
@Override public void onCreate() { super.onCreate(); context = getApplicationContext(); if (startupTimestamp == 0) startupTimestamp = System.currentTimeMillis(); if (SysUtils.isDebug(this)) { L.debug = true; //内存泄漏监控 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); builder.detectAll(); builder.penaltyLog(); StrictMode.setVmPolicy(builder.build()); } installMonitor(); }
@SuppressLint("NewApi") public static void enableStrictMode() { if (Utils.hasGingerbread()) { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog(); StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog(); if (Utils.hasHoneycomb()) { threadPolicyBuilder.penaltyFlashScreen(); vmPolicyBuilder .setClassInstanceLimit(ImageGridActivity.class, 1); } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); StrictMode.setVmPolicy(vmPolicyBuilder.build()); } }
@Override public void onCreate() { super.onCreate(); DebugSettings.initialize(this); if (DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build()); } }
@Override public void onCreate() { super.onCreate(); if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(DISK_THREAD_POLICY); StrictMode.setVmPolicy(DISK_VM_POLICY); } applicationComponent = DaggerApplicationComponent.builder() .commonModule(new CommonModule(this)) .build(); for (final ProfilingProcessor processor : applicationComponent.profilingProcessors()) { processor.enable(); } }
public boolean isNetworkAvailable() { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); return true; // ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); // NetworkInfo netInfo = cm.getActiveNetworkInfo(); // if (netInfo != null && netInfo.isConnected()) { // try { // URL url = new URL("http://www.google.com"); // HttpURLConnection urlc = (HttpURLConnection) url.openConnection(); // urlc.setConnectTimeout(3000); // urlc.connect(); // if (urlc.getResponseCode() == 200) { // return new Boolean(true); // } // } catch (MalformedURLException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // return false; }
/** * Saves the tab data out to a file. */ void saveState(File activityDirectory) { String tabFileName = TabState.getTabStateFilename(getActivityTab().getId(), false); File tabFile = new File(activityDirectory, tabFileName); // Temporarily allowing disk access while fixing. TODO: http://crbug.com/525781 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { long time = SystemClock.elapsedRealtime(); TabState.saveState(tabFile, getActivityTab().getState(), false); RecordHistogram.recordTimesHistogram("Android.StrictMode.WebappSaveState", SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS); } finally { StrictMode.setThreadPolicy(oldPolicy); } }
@TargetApi(VERSION_CODES.HONEYCOMB) public static void enableStrictMode() { if (Utils.hasGingerbread()) { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog(); StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog(); if (Utils.hasHoneycomb()) { threadPolicyBuilder.penaltyFlashScreen(); vmPolicyBuilder .setClassInstanceLimit(ImageGridActivity.class, 1) .setClassInstanceLimit(ImageDetailActivity.class, 1); } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); StrictMode.setVmPolicy(vmPolicyBuilder.build()); } }
/** * Returns the directory for a web app, creating it if necessary. * @param webappId ID for the web app. Used as a subdirectory name. * @return File for storing information about the web app. */ File getWebappDirectory(Context context, String webappId) { // Temporarily allowing disk access while fixing. TODO: http://crbug.com/525781 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { long time = SystemClock.elapsedRealtime(); File webappDirectory = new File(getBaseWebappDirectory(context), webappId); if (!webappDirectory.exists() && !webappDirectory.mkdir()) { Log.e(TAG, "Failed to create web app directory."); } RecordHistogram.recordTimesHistogram("Android.StrictMode.WebappDir", SystemClock.elapsedRealtime() - time, TimeUnit.MILLISECONDS); return webappDirectory; } finally { StrictMode.setThreadPolicy(oldPolicy); } }
private static boolean removeScheduledTasks(GcmNetworkManager scheduler) { // Third-party code causes broadcast to touch disk. http://crbug.com/614679 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); try { scheduler.cancelTask(TASK_TAG, ChromeBackgroundService.class); } catch (IllegalArgumentException e) { // This occurs when BackgroundSyncLauncherService is not found in the application // manifest. This should not happen in code that reaches here, but has been seen in // the past. See https://crbug.com/548314 // Disable GCM for the remainder of this session. setGCMEnabled(false); // Return false so that the failure will be logged. return false; } finally { StrictMode.setThreadPolicy(oldPolicy); } return true; }
/** * @return Which flavor of Herb is being tested. * See {@link ChromeSwitches#HERB_FLAVOR_ELDERBERRY} and its related switches. */ public static String getHerbFlavor() { Context context = ContextUtils.getApplicationContext(); if (isHerbDisallowed(context)) return ChromeSwitches.HERB_FLAVOR_DISABLED; if (!sIsHerbFlavorCached) { sCachedHerbFlavor = null; // Allowing disk access for preferences while prototyping. StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); try { sCachedHerbFlavor = ChromePreferenceManager.getInstance(context).getCachedHerbFlavor(); } finally { StrictMode.setThreadPolicy(oldPolicy); } sIsHerbFlavorCached = true; Log.d(TAG, "Retrieved cached Herb flavor: " + sCachedHerbFlavor); } return sCachedHerbFlavor; }
/** * Determines the number of cores available on the device. * * <p>{@link Runtime#availableProcessors()} returns the number of awake cores, which may not * be the number of available cores depending on the device's current state. See * http://goo.gl/8H670N. */ public static int calculateBestThreadCount() { // We override the current ThreadPolicy to allow disk reads. // This shouldn't actually do disk-IO and accesses a device file. // See: https://github.com/bumptech/glide/issues/1170 ThreadPolicy originalPolicy = StrictMode.allowThreadDiskReads(); File[] cpus = null; try { File cpuInfo = new File(CPU_LOCATION); final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX); cpus = cpuInfo.listFiles(new FilenameFilter() { @Override public boolean accept(File file, String s) { return cpuNamePattern.matcher(s).matches(); } }); } catch (Throwable t) { if (Log.isLoggable(TAG, Log.ERROR)) { Log.e(TAG, "Failed to calculate accurate cpu count", t); } } finally { StrictMode.setThreadPolicy(originalPolicy); } int cpuCount = cpus != null ? cpus.length : 0; int availableProcessors = Math.max(1, Runtime.getRuntime().availableProcessors()); return Math.min(MAXIMUM_AUTOMATIC_THREAD_COUNT, Math.max(availableProcessors, cpuCount)); }
private void turnOnStrictMode() { if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); } }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) private void setStrictMode() { if (Integer.valueOf(Build.VERSION.SDK) > 3) { Log.d(LOG_TAG, "Enabling StrictMode policy over Sample application"); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build()); } }
private void enableThreadPolicy() { StrictMode.setThreadPolicy( new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build() ); }
@TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressWarnings("unused") @Override public void onCreate() { if (Constants.Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build()); } super.onCreate(); initImageLoader(getApplicationContext()); }
@Override public void execute(@NonNull final Runnable command) { delegate.execute(new Runnable() { @Override public void run() { StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); StrictMode.setThreadPolicy(THREAD_POLICY); try { command.run(); } finally { StrictMode.setThreadPolicy(oldPolicy); } } }); }
private void initStrictMode() { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build()); }
@Override public void onCreate() { super.onCreate(); //Enable strict mode StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build()); //Init shetho Stetho.initializeWithDefaults(this); //Enable timber Timber.plant(new Timber.DebugTree() { @Override protected String createStackElementTag(StackTraceElement element) { return super.createStackElementTag(element) + ":" + element.getMethodName() + " ->L" + element.getLineNumber(); } }); }
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB) private void setStrictMode() { if (Integer.valueOf(Build.VERSION.SDK) > 3) { Log.d(LOG_TAG, "Enabling StrictMode policy over Sample application"); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectAll() .penaltyLog() .build()); } }
/** * Is only required by locale aware activities, AND Application. In most cases you should be * using LocaleAwareAppCompatActivity or friends. * @param context */ public static void initializeLocale(Context context) { final LocaleManager localeManager = LocaleManager.getInstance(); final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads(); StrictMode.allowThreadDiskWrites(); try { localeManager.getAndApplyPersistedLocale(context); } finally { StrictMode.setThreadPolicy(savedPolicy); } }
protected Thread sendParcel(ParcelContainer container, int code, int flags) throws IOException { //Prevent from casting exceptions with respect to "Network Access on Main Thread" StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build()); String resource = "/parcel/" + container.getParcelID(); URL url = ConnectionUtils.getBadIntentURL(resource, sPrefs, port); String jsonData = SerializationUtils.getJson(container, code, flags); final HttpURLConnection conn = ConnectionUtils.getBadIntentHttpURLConnection(url, sPrefs); conn.setRequestMethod("POST"); conn.setRequestProperty("__BadIntent__", "Parcel"); conn.setRequestProperty("__BadIntent__.package", lpparam.packageName); conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); conn.connect(); DataOutputStream writer = new DataOutputStream(conn.getOutputStream()); writer.writeBytes(jsonData); writer.flush(); writer.close(); //create new thread, which reads HTTP result return ConnectionUtils.readResponseAndCloseConnection(conn); }
private void enableStrictMode() { if (TESTING) { ThreadPolicy.Builder threadPolicy = new ThreadPolicy.Builder(); threadPolicy.detectAll(); threadPolicy.penaltyLog(); StrictMode.setThreadPolicy(threadPolicy.build()); VmPolicy.Builder vmPolicy = new VmPolicy.Builder(); vmPolicy.detectAll(); vmPolicy.penaltyLog(); StrictMode.setVmPolicy(vmPolicy.build()); } }
private void init() { if (BuildConfig.DEBUG) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build()); } applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build(); StrictMode.setThreadPolicy(policy); ActionBar ab = this.getActionBar(); // Enable the Up button if (ab != null) { ab.setDisplayHomeAsUpEnabled(true); } // TODO remove ugly test code :D // Calendar calendar = Calendar.getInstance(Locale.GERMANY); // calendar.set(2017, 9, 31); // try { // ParticluateMatters particluateMatters = new ParticluateMatters(); // particluateMatters.read(5837, calendar.getTime()); // calendar.add(Calendar.DAY_OF_YEAR, -1); // particluateMatters.read(5837, calendar.getTime()); // calendar.add(Calendar.DAY_OF_YEAR, -1); // particluateMatters.read(5837, calendar.getTime()); // System.out.println(particluateMatters.getMinValue() + " - " + particluateMatters.getMaxValue()); // } catch (Exception e) { // e.printStackTrace(); // } }
private void enabledStrictMode() { if (SDK_INT >= GINGERBREAD) { StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectAll() .penaltyLog() .penaltyDeath() .build()); } }