类 Util


  • public final class Util
    extends Object
    常用工具函数
    作者:
    💎 Li Junchao
    • 字段详细资料

      • SDK_INT

        public static final int SDK_INT
      • DEVICE

        public static final String DEVICE
      • MANUFACTURER

        public static final String MANUFACTURER
      • MODEL

        public static final String MODEL
      • DEVICE_DEBUG_INFO

        public static final String DEVICE_DEBUG_INFO
        设备简要描述信息,for-debug
    • 方法详细资料

      • getUserAgent

        public static String getUserAgent​(Context context,
                                          String applicationName)
        生成用户代理(UA)标识——
             applicationName/versionName + " (Linux;Android " + Build.VERSION.RELEASE + ") " + VERSION_SLASHY
         
        参数:
        context - Context
        applicationName - app name
        返回:
      • constrainValue

        public static int constrainValue​(int value,
                                         int min,
                                         int max)
        限定 value 在指定的范围内 in range(min..max)
        参数:
        value - 要限定的value
        min - 最小值
        max - 最大值
        返回:
        int - 限定返回值
      • constrainValue

        public static long constrainValue​(long value,
                                          long min,
                                          long max)
        限定 value 在指定的范围内 in range(min..max)
        参数:
        value - 要限定的value
        min - 最小值
        max - 最大值
        返回:
        long - 限定返回值
        另请参阅:
        constrainValue(int, int, int)
      • constrainValue

        public static float constrainValue​(float value,
                                           float min,
                                           float max)
        限定 value 在指定的范围内 in range(min..max)
        参数:
        value - 要限定的value
        min - 最小值
        max - 最大值
        返回:
        float - 限定返回值
        另请参阅:
        constrainValue(int, int, int)
      • getBytesFromHexString

        public static byte[] getBytesFromHexString​(String hexString)
        从十六进制字符串解析出二进制字节数据
        参数:
        hexString - Hex 字符串
        返回:
        byte[]
      • toHexString

        public static String toHexString​(byte[] bytes)
        转换 字节数组 到 十六进制 字符串 表示
        参数:
        bytes - 字节数组
        返回:
        String
      • linearSearch

        public static int linearSearch​(int[] array,
                                       int value)
        线性搜索
        参数:
        array - 要搜索的数组
        value - 要搜索的value
        返回:
        int - 在集合中搜索到值的index,如果未找到,则返回 -1
      • binarySearchFloor

        public static int binarySearchFloor​(int[] array,
                                            int value,
                                            boolean inclusive,
                                            boolean stayInBounds)
        使用 二分搜索法,搜索数组中小于指定值的最大元素的索引。

        如果数组包含多个等于 value 且 inclusive 为 true 的元素,则将返回第一个元素的索引。

        参数:
        array - 搜索的数据集合
        value - 搜索的值
        inclusive - 全文搜索
        stayInBounds - 如果为true,则在值小于数组中最小元素的情况下将返回 0。 如果为 false,则返回 -1。
        返回:
      • isTv

        public static boolean isTv​(Context context)
        是否是 TV
        参数:
        context - Context
        返回:
        boolean - true or false
      • isAutomotive

        public static boolean isAutomotive​(Context context)
        是否是车机设备
        参数:
        context - Context
        返回:
        boolean - true or false
      • measureInnerBoxSizeByRatio

        public static Point measureInnerBoxSizeByRatio​(@IntRange(from=0L)
                                                       int divWidth,
                                                       @IntRange(from=1L)
                                                       int divHeight,
                                                       @IntRange(from=0L)
                                                       int videoWidth,
                                                       @IntRange(from=0L)
                                                       int videoHeight)
        根据视频分辨率的比率,计算显示视频的view应该在所在容器的大小。

        自行注意宽高有效值。width、height 不能为 0

        参数:
        divWidth - 容器width
        divHeight - 容器height
        videoWidth - 视频分辨率width
        videoHeight - 视频分辨率height
        返回:
        Point - 适配适配分辨率的大小
      • measureInnerBoxSizeByRatio

        public static Point measureInnerBoxSizeByRatio​(@IntRange(from=0L)
                                                       int divWidth,
                                                       @IntRange(from=1L)
                                                       int divHeight,
                                                       @FloatRange(from=0.0)
                                                       float pixelWidthHeightRatio)
        在一个固定大小的容器内,按照 frame的 宽高率比,计算 内部view 的size
        参数:
        divWidth - 容器width
        divHeight - 容器height
        pixelWidthHeightRatio - pixel 宽高比
        返回:
        Point - 适配适配分辨率的大小
      • getCurrentDisplayModeSize

        public static Point getCurrentDisplayModeSize​(Context context)
        获取Display Size
        参数:
        context - Context
        返回:
        Point
      • getCurrentDisplayModeSize

        public static Point getCurrentDisplayModeSize​(Context context,
                                                      Display display)
        获取显示设备 Display Size
        参数:
        context - Context
        display - Display
        返回:
        Point
      • getSystemProperty

        public static String getSystemProperty​(String name)
      • maybeRequestReadExternalStoragePermission

        public static boolean maybeRequestReadExternalStoragePermission​(Activity activity,
                                                                        Uri... uris)
      • postOrRun

        public static boolean postOrRun​(Handler handler,
                                        Runnable runnable)
        如果调用线程和 Handler 现在不同,则在 指定 Handler 的线程上执行 Runnable 否则,执行执行 Runnable
        参数:
        handler - 指定 Runnable 将post到的 Handler
        runnable - 要执行的 Runnable
        返回:
        boolean - true: 成功执行 post
      • getCurrentOrMainLooper

        public static Looper getCurrentOrMainLooper()
        获取当前线程的 Looper ,如果当前线程未有 Looper,则获取主线程的 Looper
        返回:
        Looper
      • isLocalFileUri

        public static boolean isLocalFileUri​(Uri uri)
        如果 Uri 是一个local file的引用则返回 true
        参数:
        uri - Uri
        返回:
        boolean - true or false
      • isNetworkUri

        public static boolean isNetworkUri​(Uri uri)
        是否是一个网络Uri
        参数:
        uri - Uri
        返回:
        boolean - true or false
      • isAudio

        public static boolean isAudio​(@NonNull
                                      String uriPath)
        是否是一个audio媒体文件路径
        参数:
        uriPath - path
        返回:
        boolean