目录

获取用户信息

场景介绍

您的应用可以通过Drive.About.Get接口获取到用户华为云空间信息,例如空间容量大小、允许上传的最大文件大小等。

开发前提

1、请确认您已完成开发准备。

2、在进行开发之前,您可以下载华为云空间客户端示例代码进行快速体验。

创建Drive对象

调用Drive.Builder.build接口创建Drive对象。
private Drive buildDrive() {  
    Drive service = new Drive.Builder(CredentialManager.getInstance().getCredential(), context).build();  
    return service;  
}

获取用户信息

调用Drive.About.Get接口获取用户信息,设置字段setFields("*")表示返回所有字段。
private About getAbout() {  
    About response = null;
    try {  
        Drive drive = buildDrive();  
        Drive.About about = drive.about();  
        response = about.get().setFields("*").execute();  
        String res = response.toString();  
    } catch (Exception e) {  
        Log.e(TAG, "getInfo error: " + e.toString());  
    }  
    return response;
}

获取用户信息接口是联网耗时接口,需要放在子线程中调用。

更新安全策略

如果Drive.About.Get接口返回响应中needUpdate字段值为true,表示需要用户更新安全策略。您需要根据返回的地址,跳转到授权页面,用户同意后可继续使用业务。
/**   
 * 判断是否要提示用户更新协议   
 *   
 * @param about 返回的About响应   
 */  
private void checkUpdateProtocol(About about) {
    if (about == null) {
       return;
    }
    Log.d(TAG, "checkUpdate: " + about.toString());
    Object updateValue = about.get("needUpdate");
    boolean isNeedUpdate = false;
    if (updateValue instanceof Boolean) {
       isNeedUpdate = (Boolean) updateValue;
    }
    if (!isNeedUpdate) {
        return;
    }
    // 如果需要更新,通过url启动授权页面
    Object urlValue = about.get("updateUrl");
    String url = "";
    if (urlValue instanceof String) {
        url = (String) urlValue;
    }
    if (TextUtils.isEmpty(url)) {
        return;
    }
    Uri uri = Uri.parse(url);
    if (!"https".equals(uri.getScheme())) {
        return;
    }
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Logger.e(TAG, "Activity not found");
    }
}

needUpdate字段为可选字段,Drive.About.Get接口可能并不返回该字段。