use of android.os.StrictMode.ThreadPolicy in project glide by bumptech.
the class GlideExecutor method calculateBestThreadCount.
/**
* 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));
}
use of android.os.StrictMode.ThreadPolicy in project meter by OleksandrKucherenko.
the class TestCase method fixMainThreadPolicy.
/* [ STATIC METHODS ] ============================================================================================ */
/**
* Disable exception raising during execution of AsyncTasks that are designed only for background execution.
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static void fixMainThreadPolicy() {
// Dirty fix: NetworkOnMainThreadException
// http://stackoverflow.com/questions/12650921/quick-fix-for-networkonmainthreadexception
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}
use of android.os.StrictMode.ThreadPolicy in project glide by bumptech.
the class RuntimeCompat method getCoreCountPre17.
/**
* 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);
}
use of android.os.StrictMode.ThreadPolicy in project weiui by kuaifan.
the class RuntimeCompat method getCoreCountPre17.
/**
* 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);
}
use of android.os.StrictMode.ThreadPolicy in project Rocket by mozilla-tw.
the class GlideExecutor method calculateBestThreadCount.
/**
* 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));
}
Aggregations