Java源码示例:com.facebook.common.util.ByteConstants

示例1
private int getMaxCacheSize() {
    final int maxMemory =
            Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
    if (maxMemory < 32 * ByteConstants.MB) {
        return 4 * ByteConstants.MB;
    } else if (maxMemory < 64 * ByteConstants.MB) {
        return 6 * ByteConstants.MB;
    } else {
        // We don't want to use more ashmem on Gingerbread for now, since it doesn't respond well to
        // native memory pressure (doesn't throw exceptions, crashes app, crashes phone)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return 8 * ByteConstants.MB;
        } else {
            return maxMemory / 4;
        }
    }
}
 
示例2
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference_screen);

    colsDetail = (int) SPUtils.get(getActivity(), AppConfig.cols_detail, 0);
    RadioPreference cols = (RadioPreference) findPreference(AppConfig.cols_detail);
    cols.setSummary("当前显示列数: " + (colsDetail + 1) + "\n图册页加载图片一般较大,同时显示多列对设备性能要求较高,请酌情选择");

    EditTextPreference downloadPath = (EditTextPreference) findPreference(getResources().getString(R.string.download_path));
    downloadPath.setSummary((String) SPUtils.get(getActivity(), AppConfig.download_path, AppConfig.DOWNLOAD_PATH));

    noMedia = new File((String) SPUtils.get(getActivity(), AppConfig.download_path, AppConfig.DOWNLOAD_PATH) + File.separatorChar + ".nomedia");
    SwitchPreference noMediaSwitch = (SwitchPreference) findPreference(AppConfig.hide_pic);
    if (noMedia.exists())
        noMediaSwitch.setChecked(true);
    else
        noMediaSwitch.setChecked(false);

    Fresco.getImagePipelineFactory().getMainFileCache().trimToMinimum();
    float size = (float) Fresco.getImagePipelineFactory().getMainFileCache().getSize() / ByteConstants.MB;
    cacheSize = (EditTextPreference) findPreference(getResources().getString(R.string.cache_size));
    cacheSize.setSummary(String.format("已使用 %.2f MB", size));
}
 
示例3
private void loadLibrary() {
  // bugly
  Context context = getApplicationContext();
  // 获取当前包名
  String packageName = context.getPackageName();
  // 获取当前进程名
  String processName = Util.getProcessName(android.os.Process.myPid());
  // 设置是否为上报进程
  UserStrategy strategy = new UserStrategy(context);
  strategy.setUploadProcess(processName == null || processName.equals(packageName));
  CrashReport.initCrashReport(this, BuildConfig.BUGLY_APPID, BuildConfig.DEBUG, strategy);
  CrashReport.setIsDevelopmentDevice(this, BuildConfig.DEBUG);

  // fresco
  final int cacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);
  ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
      .setBitmapMemoryCacheParamsSupplier(
          () -> new MemoryCacheParams(cacheSize, Integer.MAX_VALUE, cacheSize, Integer.MAX_VALUE,
              2 * ByteConstants.MB))
      .setBitmapsConfig(Bitmap.Config.RGB_565)
      .setDownsampleEnabled(true)
      .build();
  Fresco.initialize(this, config);

}
 
示例4
public static PoolParams get() {
  SparseIntArray DEFAULT_BUCKETS = new SparseIntArray();
  DEFAULT_BUCKETS.put(1 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(2 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(4 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(8 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(16 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(32 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(64 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(128 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(256 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(512 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(1024 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  return new PoolParams(
      getMaxSizeSoftCap(),
      getMaxSizeHardCap(),
      DEFAULT_BUCKETS);
}
 
示例5
private int getMaxCacheSize() {
  final int maxMemory =
      Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
  if (maxMemory < 32 * ByteConstants.MB) {
    return 4 * ByteConstants.MB;
  } else if (maxMemory < 64 * ByteConstants.MB) {
    return 6 * ByteConstants.MB;
  } else {
    // We don't want to use more ashmem on Gingerbread for now, since it doesn't respond well to
    // native memory pressure (doesn't throw exceptions, crashes app, crashes phone)
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
      return 8 * ByteConstants.MB;
    } else {
      return maxMemory / 4;
    }
  }
}
 
示例6
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  MemoryCacheParams params =
      new MemoryCacheParams(
          4 * ByteConstants.MB,
          256,
          Integer.MAX_VALUE,
          Integer.MAX_VALUE,
          Integer.MAX_VALUE,
          TimeUnit.MINUTES.toMillis(5));
  when(mMemoryCacheParamsSupplier.get()).thenReturn(params);
  CountingMemoryCache<CacheKey, CloseableImage> countingMemoryCache =
      BitmapCountingMemoryCacheFactory.get(
          mMemoryCacheParamsSupplier, mMemoryTrimmableRegistry, null);
  mCacheKey = new SimpleCacheKey("key");
  mAnimatedFrameCache = new AnimatedFrameCache(mCacheKey, countingMemoryCache);
  mFrame1 = CloseableReference.of(mock(CloseableImage.class));
  mFrame2 = CloseableReference.of(mock(CloseableImage.class));
}
 
示例7
private int getMaxCacheSize() {
  final int maxMemory =
      Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
  if (maxMemory < 32 * ByteConstants.MB) {
    return 4 * ByteConstants.MB;
  } else if (maxMemory < 64 * ByteConstants.MB) {
    return 6 * ByteConstants.MB;
  } else {
    // We don't want to use more ashmem on Gingerbread for now, since it doesn't respond well to
    // native memory pressure (doesn't throw exceptions, crashes app, crashes phone)
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
      return 8 * ByteConstants.MB;
    } else {
      return maxMemory / 4;
    }
  }
}
 
示例8
private int getMaxCacheSize() {
    final int maxMemory =
            Math.min(mActivityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);
    if (maxMemory < 32 * ByteConstants.MB) {
        return 4 * ByteConstants.MB;
    } else if (maxMemory < 64 * ByteConstants.MB) {
        return 6 * ByteConstants.MB;
    } else {
        return maxMemory / 5;
    }
}
 
示例9
public static void init(Context context, String name) {
  try {
    File lrcCacheDir = getDiskCacheDir(context, name);
    if (!lrcCacheDir.exists()) {
      lrcCacheDir.mkdir();
    }
    mLrcCache = DiskLruCache.open(lrcCacheDir, getAppVersion(context), 1, 10 * ByteConstants.MB);
  } catch (IOException e) {
    e.printStackTrace();
  }
}
 
示例10
/**
 * {@link NativeMemoryChunkPool} manages memory on the native heap, so we don't need as strict
 * caps as we would if we were on the Dalvik heap. However, since native memory OOMs are
 * significantly more problematic than Dalvik OOMs, we would like to stay conservative.
 */
private static int getMaxSizeSoftCap() {
  final int maxMemory = (int)Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return 3 * ByteConstants.MB;
  } else if (maxMemory < 32 * ByteConstants.MB) {
    return 6 * ByteConstants.MB;
  } else {
    return 12 * ByteConstants.MB;
  }
}
 
示例11
/**
 * We need a smaller cap for devices with less then 16 MB so that we don't run the risk of
 * evicting other processes from the native heap.
 */
private static int getMaxSizeHardCap() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return maxMemory / 2;
  } else {
    return maxMemory / 4 * 3;
  }
}
 
示例12
/**
 * Our Bitmaps live in ashmem, meaning that they are pinned in androids' shared native memory.
 * Therefore, we are not constrained by the max heap size of the dalvik heap, but we want to make
 * sure we don't use too much memory on low end devices, so that we don't force other background
 * process to be evicted.
 */
private static int getMaxSizeHardCap() {
  final int maxMemory = (int)Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory > 16 * ByteConstants.MB) {
    return maxMemory / 4 * 3;
  } else {
    return maxMemory / 2;
  }
}
 
示例13
private int getMaxCacheSize() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return 1 * ByteConstants.MB;
  } else if (maxMemory < 32 * ByteConstants.MB) {
    return 2 * ByteConstants.MB;
  } else {
    return 4 * ByteConstants.MB;
  }
}
 
示例14
private static DiskCacheConfig getDefaultMainDiskCacheConfig(Context context) {
  return DiskCacheConfig.newBuilder()
      .setBaseDirectoryPath(context.getApplicationContext().getCacheDir())
      .setBaseDirectoryName("image_cache")
      .setMaxCacheSize(40 * ByteConstants.MB)
      .setMaxCacheSizeOnLowDiskSpace(10 * ByteConstants.MB)
      .setMaxCacheSizeOnVeryLowDiskSpace(2 * ByteConstants.MB)
      .build();
}
 
示例15
public static void init(Context context, File baseDirectoryPath) {
    ImagePipelineConfig.Builder imagePipelineConfigBuilder = ImagePipelineConfig.newBuilder(context)
            .setMainDiskCacheConfig(DiskCacheConfig.newBuilder(context)
                    .setBaseDirectoryPath(baseDirectoryPath)
                    .setBaseDirectoryName("original")
                    .build())
            .setDownsampleEnabled(true);
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    Supplier<MemoryCacheParams> memoryCacheParamsSupplier = new DefaultBitmapMemoryCacheParamsSupplier(activityManager) {
        @Override
        public MemoryCacheParams get() {
            int maxCacheEntries = 256;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                maxCacheEntries = 64;
            }
            return new MemoryCacheParams(
                    getMaxCacheSize(),
                    maxCacheEntries,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE,
                    Integer.MAX_VALUE);
        }
        private int getMaxCacheSize() {
            final int maxMemory = Math.min(activityManager.getMemoryClass() * ByteConstants.MB, Integer.MAX_VALUE);

            if (maxMemory < 32 * ByteConstants.MB) {
                return 4 * ByteConstants.MB;
            } else if (maxMemory < 64 * ByteConstants.MB) {
                return 6 * ByteConstants.MB;
            } else {
                return maxMemory / 4;
            }
        }
    };
    imagePipelineConfigBuilder.setBitmapMemoryCacheParamsSupplier(memoryCacheParamsSupplier);
    Fresco.initialize(context, imagePipelineConfigBuilder.build());
}
 
示例16
public static PoolParams get() {
  SparseIntArray DEFAULT_BUCKETS = new SparseIntArray();
  DEFAULT_BUCKETS.put(1 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(2 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(4 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(8 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(16 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(32 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(64 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(128 * ByteConstants.KB, SMALL_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(256 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(512 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  DEFAULT_BUCKETS.put(1024 * ByteConstants.KB, LARGE_BUCKET_LENGTH);
  return new PoolParams(getMaxSizeSoftCap(), getMaxSizeHardCap(), DEFAULT_BUCKETS);
}
 
示例17
/**
 * {@link NativeMemoryChunkPool} manages memory on the native heap, so we don't need as strict
 * caps as we would if we were on the Dalvik heap. However, since native memory OOMs are
 * significantly more problematic than Dalvik OOMs, we would like to stay conservative.
 */
private static int getMaxSizeSoftCap() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return 3 * ByteConstants.MB;
  } else if (maxMemory < 32 * ByteConstants.MB) {
    return 6 * ByteConstants.MB;
  } else {
    return 12 * ByteConstants.MB;
  }
}
 
示例18
/**
 * We need a smaller cap for devices with less then 16 MB so that we don't run the risk of
 * evicting other processes from the native heap.
 */
private static int getMaxSizeHardCap() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return maxMemory / 2;
  } else {
    return maxMemory / 4 * 3;
  }
}
 
示例19
/**
 * Our Bitmaps live in ashmem, meaning that they are pinned in androids' shared native memory.
 * Therefore, we are not constrained by the max heap size of the dalvik heap, but we want to make
 * sure we don't use too much memory on low end devices, so that we don't force other background
 * process to be evicted.
 */
private static int getMaxSizeHardCap() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory > 16 * ByteConstants.MB) {
    return maxMemory / 4 * 3;
  } else {
    return maxMemory / 2;
  }
}
 
示例20
private int getMaxCacheSize() {
  final int maxMemory = (int) Math.min(Runtime.getRuntime().maxMemory(), Integer.MAX_VALUE);
  if (maxMemory < 16 * ByteConstants.MB) {
    return 1 * ByteConstants.MB;
  } else if (maxMemory < 32 * ByteConstants.MB) {
    return 2 * ByteConstants.MB;
  } else {
    return 4 * ByteConstants.MB;
  }
}