Search in sources :

Example 1 with ThreadPolicy

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));
}
Also used : Pattern(java.util.regex.Pattern) FilenameFilter(java.io.FilenameFilter) ThreadPolicy(android.os.StrictMode.ThreadPolicy) File(java.io.File)

Example 2 with ThreadPolicy

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);
}
Also used : ThreadPolicy(android.os.StrictMode.ThreadPolicy) TargetApi(android.annotation.TargetApi)

Example 3 with ThreadPolicy

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);
}
Also used : Pattern(java.util.regex.Pattern) FilenameFilter(java.io.FilenameFilter) ThreadPolicy(android.os.StrictMode.ThreadPolicy) File(java.io.File)

Example 4 with ThreadPolicy

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);
}
Also used : Pattern(java.util.regex.Pattern) FilenameFilter(java.io.FilenameFilter) ThreadPolicy(android.os.StrictMode.ThreadPolicy) File(java.io.File)

Example 5 with ThreadPolicy

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));
}
Also used : Pattern(java.util.regex.Pattern) FilenameFilter(java.io.FilenameFilter) ThreadPolicy(android.os.StrictMode.ThreadPolicy) File(java.io.File)

Aggregations

ThreadPolicy (android.os.StrictMode.ThreadPolicy)7 File (java.io.File)4 FilenameFilter (java.io.FilenameFilter)4 Pattern (java.util.regex.Pattern)4 SuppressLint (android.annotation.SuppressLint)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 SQLiteDoneException (android.database.sqlite.SQLiteDoneException)2 SQLiteException (android.database.sqlite.SQLiteException)2 TargetApi (android.annotation.TargetApi)1 Date (java.util.Date)1