YCCommonLib

Introduction: 组件化开发中基础公共库,activity 栈管理;分区存储;Log 日志打印和存储;通用缓存库(支持 sp,mmkv,lru,disk,fastsp 等多种存储方式切换);App 重启;通用全面的工具类 Utils;通用基类 fragment,adpater,activity 等简单封装;intent 内容打印到控制台库;通用基础接口
More: Author   ReportBugs   
Tags:

目录介绍

01.框架公共组件层

  • 组件化开发中基础公共库
    • activity 栈管理;Log 日志;通用缓存库(支持 sp,mmkv,lru,disk,fastsp 等多种存储方式切换);App 重启;
    • 通用全面的工具类 Utils;通用基类 fragment,adpater,activity 等简单封装;intent 内容打印到控制台库

02.组件化建设

  • 按照不同层级架构图如下所示
    • image

03.公共组件库依赖

  • 关于依赖库如下所示,可以根据需求选择性使用:
      //base 基类
      implementation 'com.github.yangchong211.YCCommonLib:BaseClassLib:1.4.8'
      implementation 'com.github.yangchong211.YCCommonLib:ComponentLib:1.4.8'
      //工具类 utils
      implementation 'com.github.yangchong211.YCCommonLib:ToolUtilsLib:1.4.8'
      //activity 栈管理
      implementation 'com.github.yangchong211.YCCommonLib:ActivityManager:1.4.8'
      //通用缓存存储库,支持 sp,fastsp,mmkv,lruCache,DiskLruCache 等
      implementation 'com.github.yangchong211.YCCommonLib:AppBaseStore:1.4.8'
      //通用日志输出库
      implementation 'com.github.yangchong211.YCCommonLib:AppLogLib:1.4.8'
      //app 重启库
      implementation 'com.github.yangchong211.YCCommonLib:AppRestartLib:1.4.8'
      //intent 内容输出到控制台
      implementation 'com.github.yangchong211.YCCommonLib:SafeIntentLib:1.4.8'
      //通用组件接口库
      implementation 'com.github.yangchong211.YCCommonLib:AppCommonInter:1.4.8'
      //各种广播监听哭
      implementation 'com.github.yangchong211.YCCommonLib:AppStatusLib:1.4.8'
      //基建库
      implementation 'com.github.yangchong211.YCCommonLib:ArchitectureLib:1.4.8'
      //同上上报库
      implementation 'com.github.yangchong211.YCCommonLib:EventUploadLib:1.4.8'
      //权限库
      implementation 'com.github.yangchong211.YCCommonLib:AppPermission:1.4.8'
      //Lru 磁盘缓存库
      implementation 'com.github.yangchong211.YCCommonLib:AppLruDisk:1.4.8'
      //Lru 内存缓存库
      implementation 'com.github.yangchong211.YCCommonLib:AppLruCache:1.4.8'
      //fragment 生命周期监听库
      implementation 'com.github.yangchong211.YCCommonLib:FragmentManager:1.4.8'
      //反射工具库
      implementation 'com.github.yangchong211.YCCommonLib:ReflectionLib:1.4.8'
      //App 启动优化库
      implementation 'com.github.yangchong211.YCCommonLib:ParallelTaskLib:1.4.8'
      //Context 上下文库
      implementation 'com.github.yangchong211.YCCommonLib:AppContextLib:1.4.8'
      //加解密库
      implementation 'com.github.yangchong211.YCCommonLib:AppEncryptLib:1.4.8'
      //handler 包装库
      implementation 'com.github.yangchong211.YCCommonLib:AppHandlerLib:1.4.8'
      //Application 库
      implementation 'com.github.yangchong211.YCCommonLib:ApplicationLib:1.4.8'
      //store 磁盘分区库
      implementation 'com.github.yangchong211.YCCommonLib:AppMediaStore:1.4.8'
      //内存
      implementation 'com.github.yangchong211.YCCommonLib:ToolMemoryLib:1.4.8'
      //屏幕截屏库
      implementation 'com.github.yangchong211.YCCommonLib:AppScreenLib:1.4.8'
      //Wi-Fi 库
      implementation 'com.github.yangchong211.YCCommonLib:AppWiFiLib:1.4.8'
      //Vp 相关适配器库
      implementation 'com.github.yangchong211.YCCommonLib:BaseVpAdapter:1.4.8'
      //io 流读写库
      implementation 'com.github.yangchong211.YCCommonLib:FileIoHelper:1.4.8'
      //图片工具库
      implementation 'com.github.yangchong211.YCCommonLib:ImageToolLib:1.4.8'
      //网络判断库
      implementation 'com.github.yangchong211.YCCommonLib:NetWorkLib:1.4.8'
      //手机
      implementation 'com.github.yangchong211.YCCommonLib:PhoneSensor:1.4.8'
      //File 文件库
      implementation 'com.github.yangchong211.YCCommonLib:ToolFileLib:1.4.8'
      //Zip 压缩库
      implementation 'com.github.yangchong211.YCCommonLib:ZipFileLib:1.4.8'
      //图片压缩
      implementation 'com.github.yangchong211.YCCommonLib:AppCompress:1.4.8'
    

04.Activity 栈管理库

  • 非常好用的 activity 任务栈管理库,自动化注册。

    • 完全解耦合的 activity 栈管理,拿来即可用,或者栈顶 Activity,移除添加,推出某个页面,获取应用注册 Activity 列表等,可以注册监听某个页面的生命周期,小巧好用。

      //退出应用程序
      ActivityManager.getInstance().appExist();
      //查找指定的 Activity
      Activity commonActivity = ActivityManager.getInstance().get(CommonActivity.class);
      //判断界面 Activity 是否存在
      boolean exist = ActivityManager.getInstance().isExist(CommonActivity.class);
      //移除栈顶的 activity
      ActivityManager.getInstance().pop();
      //结束所有 Activity
      ActivityManager.getInstance().finishAll();
      //结束指定的 Activity
      ActivityManager.getInstance().finish(CommonActivity.this);
      //判断 activity 任务栈是否为空
      ActivityManager.getInstance().isEmpty();
      //获取栈顶的 Activity
      Activity activity = ActivityManager.getInstance().peek();
      //判断 activity 是否处于栈顶
      ActivityManager.getInstance().isActivityTop(this,"MainActivity");
      //添加 activity 入栈
      ActivityManager.getInstance().add(CommonActivity.this);
      //移除 activity 出栈
      ActivityManager.getInstance().remove(CommonActivity.this);
      //监听某个 activity 的生命周期,完全解耦合
      ActivityManager.getInstance().registerActivityLifecycleListener(CommonActivity.class, new ActivityLifecycleListener() {
        @Override
        public void onActivityCreated(@Nullable Activity activity, Bundle savedInstanceState) {
            super.onActivityCreated(activity, savedInstanceState);
        }
      
        @Override
        public void onActivityStarted(@Nullable Activity activity) {
            super.onActivityStarted(activity);
        }
      });
      //移除某个 activity 的生命周期,完全解耦合
      //ActivityManager.getInstance().registerActivityLifecycleListener(CommonActivity.this,listener);
      
  • 如何依赖该库

    
    
    
  • 相关文档连接

05.通用存储库

  • 业务背景:
    • 项目中有用到 sp,mmkv,file,内存缓存,缓存的使用场景有很多,那么能否可以打造一套通用 api 的缓存方案。
  • 通用缓存方案:
    • 不管是 sp,mmkv,lru,disk,file,内存缓存,都使用同一套 api。大大简化了磁盘缓存应用流程……
  • 无缝切换:
    • 常见一套 api+不同接口实现+代理类+工厂模型
  • 方案替代稳定性:
    • 该库异常上报,容错性,已经暴露外部 config 配置,打造通用的轮子
  • 内部开源该库:
    • 作为技术沉淀,当作专项来推动进展。高复用低耦合,便于拓展,可快速移植,解决各个项目使用内存缓存,sp,mmkv,sql,lru,DataStore 的凌乱。抽象一套统一的 API 接口。

06.Log 日志打印库

  • 通用日志库框架,专用 LogCat 工具,主要功能全局配置 log 输出, 个性化设置 Tag,可以设置日志打印级别,支持打印复杂对象,可以实现自定义日志接口,支持简化版本将日志写入到文件中。小巧好用!
  • 第一步:初始化操作
      String ycLogPath = AppFileUtils.getCacheFilePath(this, "ycLog");
      AppLogConfig config = new AppLogConfig.Builder()
              //设置日志 tag 总的标签
              .setLogTag("yc")
              //是否将 log 日志写到文件
              .isWriteFile(true)
              //是否是 debug
              .enableDbgLog(true)
              //设置日志最小级别
              .minLogLevel(Log.VERBOSE)
              //设置输出日志到 file 文件的路径。前提是将 log 日志写入到文件设置成 true
              .setFilePath(ycLogPath)
              .build();
      //配置
      AppLogFactory.init(config);
    
  • 第二步:使用 Log 日志,十分简单,如下所示

      //自己带有 tag 标签
      AppLogHelper.d("MainActivity: ","debug log");
      //使用全局 tag 标签
      AppLogHelper.d("MainActivity log info no tag");
      //当然,如果不满足你的要求,开发者可以自己实现日志输出的形式。
      AppLogFactory.addPrinter(new AbsPrinter() {
          @NonNull
          @Override
          public String name() {
              return "yc";
          }
    
          @Override
          public void println(int level, String tag, String message, Throwable tr) {
              //todo 这块全局回调日志,你可以任意实现自定义操作
          }
      });
    

07.App 重新启动库

  • 使用场景说明
    • 使用到了简单工厂模式,通过工厂类,只要提供一个参数传给工厂,就可以直接得到一个想要的产品对象,并且可以按照接口规范来调用产品对象的所有功能(方法)。
    • 比如 App 切换了 debug 或者 release 环境,需要进行 app 重新启动,可以使用该库,小巧好用。一行代码搞定,傻瓜式使用!
  • 第一种方式,开启一个新的服务 KillSelfService,用来重启本 APP。
      RestartAppHelper.restartApp(this,RestartFactory.SERVICE);
    
  • 第二种方式,使用闹钟延时,使用 PendingIntent 延迟意图,然后重启 app
      RestartAppHelper.restartApp(this,RestartFactory.ALARM);
    
  • 第三种方式,检索获取项目中 LauncherActivity,然后设置该 activity 的 flag 和 component 启动 app
      RestartAppHelper.restartApp(this,RestartFactory.MAINIFEST);
    

08.通用工具类库

  • ToolUtils 是一个 Android 工具库。便于开发人员,快捷高效开发需求。
  • 常用的基本上都有,还包括图片压缩,日历事件,异常上报工具,网络,手机方向监听,防爆力点击等工具类。

      FastClickUtils              防爆力点击,可以自定义设置间距时间
      PerfectClickListener        避免在 1 秒内出发多次点击
      AppFileUtils                file 文件工具类
      FileShareUtils              file 文件分享工具类
      ScreenShotUtils             监听屏幕被截图事件
      SensorManagerUtils          手机系统方向监听器工具类
      AppNetworkUtils             网络工具类
      AppProcessUtils             进程工具类,高效获取进程名称
      CompressUtils               图片压缩工具类
      ExceptionReporter           异常上报工具类
      OnceInvokeUtils             避免多次执行工具类
      StatusBarUtils              状态栏工具类
    
      //还有很多其他常用工具类,这里就不一一罗列呢
      ……
    

09.通用基类封装库

  • 通用 PagerAdapter 封装
    • 方便快速 tabLayout+ViewPager。针对页面较少的 Fragment 选择使用 BaseFragmentPagerAdapter,针对页面较多的 Fragment 选择使用 BasePagerStateAdapter。
  • BaseLazyFragment 懒加载 fragment
    • 就是等到该页面的 UI 展示给用户时,再加载该页面的数据(从网络、数据库等)。
  • BaseVisibilityFragment
    • fragment 是否可见封装类,比如之前需求在页面可见时埋点就需要这个。弥补了 setUserVisibleHint 遇到的 bug……
  • ViewPager2 封装库
    • DiffFragmentStateAdapter,可以用作 diff 操作的适配器;ViewPagerDiffCallback 用来做 diff 计算的工具类

10.反射工具类库

11.Intent 封装库

  • 通用打印 Intent 对象内容到 log 日志栏中,支持普通 intent 和延迟 PendingIntent。超级方便检查,可以打印 action,category,data,flags,extras 等等

      //打印 intent 所有的数据
      IntentLogger.print("intent test : ", intent)
      //打印 intent 中 component
      IntentLogger.printComponentName("intent component : " , intent)
      //打印 intent 中 extras 参数
      IntentLogger.printExtras("intent test : ", intent)
      //打印 intent 中 flags 参数
      IntentLogger.printFlags("intent test : ", intent)
    
      //PendingIntent
      //打印 intent 所有的数据
      PendingIntentLogger.print("intent test : ", intent)
      //打印 intent 中 content
      PendingIntentLogger.printContentIntent("intent content : " , intent)
      //打印 intent 的 tag
      PendingIntentLogger.printTag("intent tag : " , intent)
    

12.基础接口库

  • 背景说明:由于组件化开发中有很多基础组件,由于某些需求,需要统计一些事件,异常上报到平台上,获取控制降级,自定义日志打印等,因此采用接口回调方式实现
  • IEventTrack,event 事件接口,一般用于特殊事件上报作用
  • IExceptionTrack,异常事件接口,一般可以用在组件库中 catch 捕获的时候,上报日志到服务平台操作
  • ILogger,log 自定义日志接口,一般用于组件库日志打印,暴露给外部开发者
  • IMonitorToggle,AB 测试开关接口,也可以叫降级开关,一般用于组件库某功能降级操作,暴露给开发者设置

13.异常上报接口库

  • 基础库作为一个功能比较独立的 lib,总不可能依赖 APM 组件吧。因此,就采用抽象类隔离!App 壳工程继承抽象类,lib 库直接调用抽象类。
  • api 调用如下所示,直接拿来用即可。下面这些调用可以用在 lib 库中,特轻量级上报
      //上报日志
      LoggerReporter.report("DiskLruCacheHelper" , "lru disk file path : " + directory.getPath());
      LoggerReporter.report("DiskLruCacheHelper clear cache");
      //上报异常
      ExceptionReporter.report("Unable to get from disk cache", e);
      ExceptionReporter.report(ioe);
      //上报 event 事件,通常用来埋点
      EventReporter.report("initCacheSuccess")
      EventReporter.report("initCacheSuccess",map)
    
  • 需要在壳工程代码中,添加具体实现类。代码如下所示:

      //LoggerReporter,ExceptionReporter,EventReporter 都是类似的
      public class LoggerReporterImpl extends LoggerReporter {
          @Override
          protected void reportLog(String eventName) {
    
          }
    
          @Override
          protected void reportLog(String eventName, String message) {
    
          }
      }
    

14.File 流读写库

14.1 从文件中读数据

  • 从文件中读数据,使用普通字节流或者字符流方式,如下所示
      //字节流读取 file 文件,转化成字符串
      String file2String = FileIoUtils.readFile2String1(fileName);
      //字符流读取 file 文件,转化成字符串
      String file2String = FileIoUtils.readFile2String2(fileName);
    
  • 从文件中读数据,使用高效流方式,如下所示
      //高效字节流读取 file 文件,转化成字符串
      String file2String = BufferIoUtils.readFile2String1(fileName);
      //高效字符流读取 file 文件,转化成字符串
      String file2String = BufferIoUtils.readFile2String2(fileName);
    

14.2 将内容写入文件

  • 从文件中读数据,使用普通字节流或者字符流方式,如下所示
      //使用字节流,写入字符串内容到文件中
      FileIoUtils.writeString2File1(content,fileName);
      //使用字符流,写入字符串内容到文件中
      FileIoUtils.writeString2File2(content,fileName);
    
  • 从文件中读数据,使用高效流方式,如下所示
      //高效字节流写入字符串内容到文件中
      BufferIoUtils.writeString2File1(content,fileName);
      //高效字符流写入字符串内容到文件中
      BufferIoUtils.writeString2File2(content,fileName);
    

14.3 文件复制操作

  • 使用字节流&字符流复制
      //使用字节流复制文件,根据文件路径拷贝文件。
      FileIoUtils.copyFile1(fileName,newFileName);
      //使用字符流复制文件,根据文件路径拷贝文件。
      FileIoUtils.copyFile2(fileName,newFileName);
    
  • 使用高效流复制
      //使用高效字符缓冲流,根据文件路径拷贝文件。
      BufferIoUtils.copyFile1(fileName,newFileName);
      //使用高效字节缓冲流,根据文件路径拷贝文件
      BufferIoUtils.copyFile2(fileName,newFileName);
    

14.4 将流对象写入文件

  • 将 InputStream 流对象写入到本地文件中
      //使用字符流读取流数据到新的 file 文件中
      FileIoUtils.writeFileFromIS1(is,srcFile);
      //使用字节流将流数据写到文件中
      FileIoUtils.writeFileFromIS1(is,fileName);
    

15.加密和解密库

  • 关于 MD5 加密 Api 如下所示
      //对字符串 md5 加密
      String md2 = Md5EncryptUtils.encryptMD5ToString(string);
      //对字符串 md5 加密,加盐处理
      String md3 = Md5EncryptUtils.encryptMD5ToString(string,"doubi");
      //对字节数据 md5 加密
      String md4 = Md5EncryptUtils.encryptMD5ToString(bytes);
      //对字节数据 md5 加密,加盐处理
      String md5 = Md5EncryptUtils.encryptMD5ToString(bytes,"doubi".getBytes());
      //对文件进行 md5 加密
      String md6 = Md5EncryptUtils.encryptMD5File1(txt);
      //对文件进行 md5 加密
      String md7 = Md5EncryptUtils.encryptMD5File2(new File(txt));
    
  • 关于 base64 加密和解密的 Api 如下所示
      //字符 Base64 加密
      String strBase64_2 = Base64Utils.encodeToString(str);
      //字符 Base64 解密
      String strBase64_3 = Base64Utils.decodeToString(strBase64_2);
    
  • 关于 DES 加密和解密的 Api 如下所示
      //DES 加密字符串
      String encrypt1 = DesEncryptUtils.encrypt(string,password);
      //DES 解密字符串
      String decrypt1 = DesEncryptUtils.decrypt(encrypt1 , password);
      //DES 加密文件
      String encryptFile1 = DesEncryptUtils.encryptFile(password, fileName, destFile);
      //DES 解密文件
      String decryptFile1 = DesEncryptUtils.decryptFile(password, destFile, destFile3);
      //DES 加密后转为 Base64 编码
      String encrypt2 = DesEncryptUtils.encrypt(string.getBytes(), password.getBytes());
      //DES 解密字符串 Base64 解码
      String decrypt2 = DesEncryptUtils.decrypt(encrypt2.getBytes(), password.getBytes());
    
  • 关于 AES 加密和解密的 Api 如下所示
      //AES 加密字符串
      String encrypt1 = AesEncryptUtils.encrypt(string,password);
      //AES 解密字符串
      String decrypt1 = AesEncryptUtils.decrypt(encrypt1 , password);
    
  • 关于 RC4 加密和解密的 Api 如下所示
      //RC4 加密
      String encrypt1 = Rc4EncryptUtils.encryptString(string, secretKey);
      //RC4 解密
      String decrypt1 = Rc4EncryptUtils.decryptString(encrypt1, secretKey);
      //RC4 加密 base64 编码数据
      String encrypt2 = Rc4EncryptUtils.encryptToBase64(bytes1, secretKey);
      //RC4 解密 base64 解码数据
      byte[] bytes = Rc4EncryptUtils.decryptFromBase64(encrypt2, secretKey);
    

16.Lru 内存缓存库

  • 缓存的大小有限,当缓存被用满时,哪些数据应该被清理出去,哪些数据应该被保留?这就需要缓存淘汰策略来决定。
    • 常见的策略有三种:先进先出策略 FIFO、最少使用策略 LFU、最近最少使用策略 LRU。
  • 如何度量缓存单元的内存占用?
    • 缓存系统应该实时记录当前的内存占用量,在添加数据时增加内存记录,在移除或替换数据时减少内存记录,这就涉及 “如何度量缓存单元的内存占用” 的问题。
  • 计数 or 计量,这是个问题。比如说:
    • 举例 1: 实现图片内存缓存,如何度量一个图片资源的内存占用?
    • 举例 2: 实现数据模型对象内存缓存,如何度量一个数据模型对象的内存占用?
    • 举例 3: 实现资源内存预读,如何度量一个资源的内存占用?
  • 将这个问题总结为 2 种情况:
    • 1、能力复用使用计数: 这类内存缓存场景主要是为了复用对象能力,对象本身持有的数据并不多。而且,再加上引用复用的因素,很难统计对象实际的内存占用。因此,这类内存缓存场景应该使用计数,只统计缓存单元的个数,例如复用数据模型对象,资源预读等;
    • 2、数据复用使用计量: 这类内存缓存场景主要是为了复用对象持有的数据,数据对内存的影响远远大于对象内存结构对内存的影响,是否度量除了数据外的部分内存对缓存几乎没有影响。因此, 这里内存缓存场景应该使用计量,不计算缓存单元的个数,而是计算缓存单元中主数据字段的内存占用量,例如图片的内存缓存就只记录 Bitmap 的像素数据内存占用。

17.Lru 磁盘缓存库

  • DiskLruCache 场景
    • 用于实现存储设备缓存,即磁盘缓存,它通过将缓存对象写入文件系统从而实现缓存的效果。

19.来电和去电监听

  • 业务场景说明
    • 在 App 进行音视频聊天的时,这个时候来电了,电话接通后,需要关闭音视频聊天。这个时候就需要监听电话来电和去电状态。
  • 如何依赖该库

    
    
    
  • 相关文档连接

30.网络判断库

32.File 文件库

34.多次点击判断库

  • 业务场景说明
    • 经常遇到这样的需求类似进入开发者模式,即多次点击后执行操作。
  • 如何依赖该库

    
    
    
  • 相关文档连接
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools