目录

ConsentsController

Class Info

public class ConsentsController

Provides the API for authorization management. You can use this API to view and cancel the granted scopes.

Public Method Summary

Qualifier and Type

Method Name and Description

Task<ScopeLangItem>

get(String lang, String appId)

Queries the list of scopes granted to your app. After the list is updated, wait for 2 min for the latest list to cache.

Task<Void>

cancelAuthorization(boolean deleteData)

Cancels all scopes granted to your app. You can specify whether to delete user data when the scopes are canceled.

Task<Void>

cancelAuthorization(String appId, List<String> scopes)

Cancels certain Health Kit related scopes granted to your app.

Public Methods

[h2]get(String lang, String appId)

Method

public Task<ScopeLangItem> get(String lang, String appId)

Queries the list of scopes granted to your app. After the list is updated, wait for 2 min for the latest list to cache.

Parameters

Name

Description

lang

Language code. If the specified value is invalid, en-us will be used.

appId

ID of your app.

Returns

Type

Description

Task<ScopeLangItem>

List of scopes that have been granted to your app.

The sample code for querying the list of scopes granted to your app is as follows:
Java
// Log tag
private static final String TAG = "HealthKitConsents";

// Save the obtained list of the granted scopes.
private ScopeLangItem scopeLangItem;

public void getAppScopes() {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    ConsentsController consentsController = HuaweiHiHealth.getConsentsController(this);

    // 2. Query the list of scopes that have been granted to your app.
    // Replace APP ID with your app ID.
    Task<ScopeLangItem> task = consentsController.get("zh-cn", "APP ID");
    // 3. Add a listener for the task result.
    task.addOnSuccessListener(new OnSuccessListener<ScopeLangItem>() {
        @Override
        public void onSuccess(ScopeLangItem result) {
            Log.i(TAG, "getAppScopes success");
            scopeLangItem = result;
            // 4. Obtain the list of scopes granted to your app.
            if (result.getUrl2Desc() != null) {
                Set<String> scopeUrl = result.getUrl2Desc().keySet();
                Log.i(TAG, "getAppScopes size " + scopeUrl.size());
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.e(TAG, "getAppScopes exception");
        }
    });
}
Kotlin
// Save the obtained list of the granted scopes.
private var scopeLangItem: ScopeLangItem? = null
fun getAppScopes() {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    val consentsController = HuaweiHiHealth.getConsentsController(this)

    // 2. Query the list of scopes that have been granted to your app.
    // Replace APP ID with your app ID.
    val task = consentsController.get("zh-cn", "APP ID")
    // 3. Add a listener for the task result.
    task.addOnSuccessListener { result ->
        Log.i(TAG, "getAppScopes success")
        scopeLangItem = result
        // 4. Obtain the list of scopes granted to your app.
        if (result.url2Desc != null) {
            val scopeUrl = result.url2Desc.keys
            Log.i(TAG, "getAppScopes size " + scopeUrl.size)
        }
    }.addOnFailureListener { Log.e(TAG, "getAppScopes exception") }
}

companion object {
    // Log tag
    private val TAG = "HealthKitConsents"
}

[h2]cancelAuthorization(String appId, List<String> scopes)

Method

public Task<Void> cancelAuthorization(String appId, List<String> scopes)

Cancels certain Health Kit related scopes granted to your app.

Parameters

Name

Description

appId

ID of your app.

scopes

List of Health Kit related scopes to be canceled. The value is the key value of url2Desc in ScopeLangItem.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

The sample code for canceling certain scopes granted to your app is as follows:
Java
// Log tag
private static final String TAG = "HealthKitConsents";

// Save the obtained list of the granted scopes.
private ScopeLangItem scopeLangItem;

public void revokeScopes(View view) {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    ConsentsController consentsController = HuaweiHiHealth.getConsentsController(this);

    // 2. Cancel certain scopes granted to your app.
    // Replace APP ID with your app ID.
    Task<Void> task = consentsController.cancelAuthorization("APP ID", getRevokeScopes());
    task.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.i(TAG, "revokeScopes success");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.e(TAG, "revokeApp exception");
        }
    });
}

/**
* Obtain the list of scopes to be canceled. In this example, the last scope URL returned by getAppScopes is used. You need to customize the logic.
 *
 * @return List of scopes to be canceled.
 */
private List<String> getRevokeScopes() {
    List<String> scopeUrls = new ArrayList<>(Optional.ofNullable(scopeLangItem)
        .map(ScopeLangItem::getUrl2Desc)
        .map(Map::keySet)
        .orElse(Collections.emptySet()));

    if (scopeUrls.isEmpty()) {
        return scopeUrls;
    }

    Collections.sort(scopeUrls);
    List<String> revokeScopeUrls = scopeUrls.subList(scopeUrls.size() - 1, scopeUrls.size());

    return revokeScopeUrls;
}
Kotlin
// Save the obtained list of the granted scopes.
private val scopeLangItem: ScopeLangItem? = null

fun revokeScopes(view: View) {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    val consentsController = HuaweiHiHealth.getConsentsController(this)

    // 2. Cancel certain scopes granted to your app.
    // Replace APP ID with your app ID.
    val task = consentsController.cancelAuthorization("APP ID", getRevokeScopes())
    task.addOnSuccessListener { Log.i(TAG, "revokeScopes success") }
        .addOnFailureListener { Log.e(TAG, "revokeApp exception") }
}

/**
* Obtain the list of scopes to be canceled. In this example, the ID of the last scope returned by getAppScopes is used. You need to customize the logic.
 *
 * @return List of scopes to be canceled.
 */
private fun getRevokeScopes(): List<String> {
    val scopeUrls = ArrayList(
        Optional.ofNullable(scopeLangItem)
            .map { scopeLangItem -> scopeLangItem.url2Desc }
            .map { url2Desc -> url2Desc.keys }
            .orElse(Collections.emptySet())
    )

    if (scopeUrls.isEmpty()) {
        return scopeUrls
    }

    scopeUrls.sort()
    return scopeUrls.subList(scopeUrls.size - 1, scopeUrls.size)
}

companion object {
    // Log tag
    private val TAG = "HealthKitConsents"
}

[h2]cancelAuthorization(boolean deleteData)

Method

public Task<Void> cancelAuthorization(boolean deleteData)

Cancels all scopes granted to your app. You can specify whether to delete user data when the scopes are canceled.

Parameters

Name

Description

deleteData

Whether to delete user data. true if so; false otherwise.

Returns

Type

Description

Task<Void>

Task object, which is used to check whether a request is successful, fails, or is complete.

The sample code for canceling all scopes granted to your app is as follows:
Java
// Log tag
private static final String TAG = "HealthKitConsents";

public void cancelAuthorization(View view) {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    ConsentsController mConsentsController = HuaweiHiHealth.getConsentsController(this);
    // 2. Specify whether to delete user data. true if so; false otherwise.
    boolean clearUserData = true;
    // 3. Cancel all authorization granted to the app and delete user data.
    Task<Void> task = mConsentsController.cancelAuthorization(clearUserData);
    task.addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.i(TAG, "cancelAuthorization success");
            if(clearUserData){
                Log.i(TAG, "clearUserData success");
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(Exception e) {
            Log.e(TAG, "cancelAuthorization exception");
        }
    });
}
Kotlin
fun cancelAuthorization(view: View) {
    // 1. Obtain a ConsentsController object.
    // Note that this refers to an Activity object.
    val mConsentsController = HuaweiHiHealth.getConsentsController(this)
    // 2. Specify whether to delete user data. true if so; false otherwise.
    val clearUserData = true
    // 3. Cancel all authorization granted to the app and delete user data.
    val task = mConsentsController!!.cancelAuthorization(clearUserData)
    task.addOnSuccessListener {
        Log.i(TAG, "cancelAuthorization success")
        if (clearUserData) {
            Log.i(TAG, "clearUserData success")
        }
    }.addOnFailureListener { e ->
        Log.e(TAG, "cancelAuthorization exception")
    }
}
companion object {
    // Log tag
    private val TAG = "HealthKitConsents"
}