设置KIA文件列表
=========

#### 场景介绍

Enterprise Data Guard Kit为应用提供设置KIA文件列表的能力，HarmonyOS系统根据管控策略对KIA文件列表中的文件实行管控。

#### 接口说明

详细接口说明可参考[接口文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard)。

| 接口名 | 描述 |
| :-- | :-- |
| [setKiaFilelist](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#setkiafilelist)(filelist: string, callback: AsyncCallback<void>): void | 使用Callback方式设置KIA文件列表。 |
| [setKiaFilelist](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#setkiafilelist-1)(filelist: string): Promise<void> | 使用Promise方式设置KIA文件列表。 |
| [isKia](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#iskia)(path: string): boolean | 检查文件或文件夹是否是KIA。 |

#### 开发步骤

1.  导入模块。
    
    import { fileGuard } from '@kit.EnterpriseDataGuardKit';
    import { osAccount, BusinessError } from '@kit.BasicServicesKit';
    
2.  初始化[FileGuard](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#fileguard)对象guard，将KIA文件列表对象转为字符串，调用接口setKiaFilelist，设置KIA文件列表。
    
    *   通过回调函数方式，设置KIA文件列表。
        
        async function setKiaFilelistCallback() {
          let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
          let userId: number = await accountManager.getOsAccountLocalId();
        
          let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
          let fileListStr: string =
            '{"kia\_filelist":\["/data/service/el2/{account\_id}/hmdfs/account/files/Docs/Documents/1.txt",' +
              '"/data/service/el2/{account\_id}/hmdfs/account/files/Docs/Documents/2.txt"\],' +
              '"kia\_keyword":\["key1","key2","key3"\],' +
              '"kia\_suffix":\[".java", ".html", ".cpp", ".docx"\],' +
              '"compress\_suffix":\[".rar", ".zip"\],' +
              \`"user\_id":${userId},\` +
              '"kia\_update\_type":0}';
          guard.setKiaFilelist(fileListStr, (err: BusinessError) => {
            if (err) {
              console.error(\`Failed to set the list of KIA file. Code: ${err.code}, message: ${err.message}.\`);
            } else {
              console.info(\`Succeeded in setting the list of KIA file.\`);
            }
          });
        }
        
    *   通过Promise方式，设置KIA文件列表。
        
        async function setKiaFilelistPromise() {
          let accountManager: osAccount.AccountManager = osAccount.getAccountManager();
          let userId: number = await accountManager.getOsAccountLocalId();
        
          let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
          let fileListStr: string =
            '{"kia\_filelist":\["/data/service/el2/{account\_id}/hmdfs/account/files/Docs/Documents/1.txt",' +
              '"/data/service/el2/{account\_id}/hmdfs/account/files/Docs/Documents/2.txt"\],' +
              '"kia\_keyword":\["key1","key2","key3"\],' +
              '"kia\_suffix":\[".java", ".html", ".cpp", ".docx"\],' +
              '"compress\_suffix":\[".rar", ".zip"\],' +
              \`"user\_id":${userId},\` +
              '"kia\_update\_type":0}';
          guard.setKiaFilelist(fileListStr).then(() => {
            console.info(\`Succeeded in setting the list of KIA file.\`);
          }).catch((err: BusinessError) => {
            console.error(\`Failed to set the list of KIA file. Code: ${err.code}, message: ${err.message}.\`);
          });
        }
        
3.  初始化[FileGuard](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#fileguard)对象guard，调用接口[isKia](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#iskia)，检查该文件或文件夹是否是KIA。
    
    function isKIA() {
      try {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = '/data/service/el2/account\_id/hmdfs/account/files/Docs/Documents/1.txt';
        let isKIA: boolean = guard.isKia(path);
        console.info(\`Succeeded in determining whether the file is a KIA file. isKIA: ${isKIA}\`);
      } catch (e) {
        console.error(\`Failed to determine whether the file is a KIA file. Code: ${e.code}, message: ${e.message}.\`);
      }
    }