For details about the verification logic, please refer to AppsCheck in Client Sample Code.
|
|
You can obtain a list of malicious apps and evaluate whether to restrict your app's behavior based on risks (of risky apps/virus apps).
You can directly call getMaliciousAppsList of SafetyDetectClient to obtain a list of malicious apps.
private void invokeGetMaliciousApps() { SafetyDetectClient appsCheckClient = SafetyDetect.getClient(MainActivity.this); Task task = appsCheckClient.getMaliciousAppsList(); task.addOnSuccessListener(new OnSuccessListener<MaliciousAppsListResp>() { @Override public void onSuccess(MaliciousAppsListResp maliciousAppsListResp) { // Indicates that communication with the service was successful. // Use resp.getMaliciousApps() to get malicious apps data. List<MaliciousAppsData> appsDataList = maliciousAppsListResp.getMaliciousAppsList(); // Indicates get malicious apps was successful. if(maliciousAppsListResp.getRtnCode() == CommonCode.OK) { if (appsDataList.isEmpty()) { // Indicates there are no known malicious apps. Log.i(TAG, "There are no known potentially malicious apps installed."); } else { Log.i(TAG, "Potentially malicious apps are installed!"); for (MaliciousAppsData maliciousApp : appsDataList) { Log.i(TAG, "Information about a malicious app:"); // Use getApkPackageName() to get APK name of malicious app. Log.i(TAG, "APK: " + maliciousApp.getApkPackageName()); // Use getApkSha256() to get APK sha256 of malicious app. Log.i(TAG, "SHA-256: " + maliciousApp.getApkSha256()); // Use getApkCategory() to get category of malicious app. // Categories are defined in AppsCheckConstants Log.i(TAG, "Category: " + maliciousApp.getApkCategory()); } } }else{ Log.e(TAG,"getMaliciousAppsList failed: "+maliciousAppsListResp.getErrorReason()); } } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { // An error occurred during communication with the service. if (e instanceof ApiException) { // An error with the HMS API contains some // additional details. ApiException apiException = (ApiException) e; // You can retrieve the status code using the apiException.getStatusCode() method. Log.e(TAG, "Error: " + SafetyDetectStatusCodes.getStatusCodeString(apiException.getStatusCode()) + ": " + apiException.getStatusMessage()); } else { // A different, unknown type of error occurred. Log.e(TAG, "ERROR: " + e.getMessage()); } } }); }
private fun getMaliciousApps() { SafetyDetect.getClient(this) .maliciousAppsList .addOnSuccessListener { maliciousAppsListResp -> val appsDataList: List<MaliciousAppsData> = maliciousAppsListResp.maliciousAppsList if (maliciousAppsListResp.rtnCode == CommonCode.OK) { if (appsDataList.isEmpty()) { val text = "No known potentially malicious apps are installed." Toast.makeText(this!!.applicationContext, text, Toast.LENGTH_SHORT).show() } else { for (maliciousApp in appsDataList) { Log.e(TAG, "Information about a malicious app:") Log.e(TAG, " APK: " + maliciousApp.apkPackageName) Log.e(TAG, " SHA-256: " + maliciousApp.apkSha256) Log.e(TAG, " Category: " + maliciousApp.apkCategory) } val maliciousAppAdapter: ListAdapter = MaliciousAppsDataListAdapter(appsDataList, requireContext()) fg_list_app.adapter = maliciousAppAdapter } } else { val msg = ("Get malicious apps list failed! Message: " + maliciousAppsListResp.errorReason) Log.e(com.huawei.hms.safetydetect.sample.SafetyDetectAppsCheckAPIFragment.TAG, msg) Toast.makeText(this!!.applicationContext, msg, Toast.LENGTH_SHORT).show() } } .addOnFailureListener { e -> // There was an error communicating with the service. val errorMsg: String? errorMsg = if (e is ApiException) { // An error with the HMS API contains some additional details. val apiException = e as ApiException SafetyDetectStatusCodes.getStatusCodeString(apiException.statusCode) + ": " + apiException.message // You can use the apiException.getStatusCode() method to get the status code. } else { // Unknown type of error has occurred. e.message } val msg = "Get malicious apps list failed! Message: $errorMsg" Log.e(com.huawei.hms.safetydetect.sample.SafetyDetectAppsCheckAPIFragment.TAG, msg) Toast.makeText(this!!.applicationContext, msg, Toast.LENGTH_SHORT).show() } }
For details about the verification logic, please refer to AppsCheck in Client Sample Code.