package com.miui.guardprovider.engine.mi.antidefraud;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import com.miui.guardprovider.GuardApplication;
import com.miui.guardprovider.d.b;
import com.miui.guardprovider.engine.mi.antivirus.NetworkApiHelper;
import com.xiaomi.onetrack.OneTrack;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
public class AntiDefraudAppManager {
private static final boolean DEBUG = GuardApplication.c;
private static final String DETECT_APPS_URL = "https://flash.sec.miui.com/detect/app";
private static final String MIUI_VERSION = ("MIUI-" + Build.VERSION.INCREMENTAL);
private static final String SALT_FLASH = "6988567a-4220-4b51-bc2d-ccdec27a74a1";
private static final String TAG = "AntiDefraudAppManager";
private AntiDefraudAppManager() {
}
public static String[] getAllDetectUnsafeApps() {
try {
String allUnSystemAppsStatus = getAllUnSystemAppsStatus(getUnSystemAppList(GuardApplication.d));
if (TextUtils.isEmpty(allUnSystemAppsStatus)) {
return new String[0];
}
MiDetectAppsManager.getInstance(GuardApplication.d).saveDetectApps(allUnSystemAppsStatus);
JSONObject jSONObject = new JSONObject(allUnSystemAppsStatus);
if (jSONObject.has("error_code")) {
if (DEBUG) {
Log.d("TAG", "error_code: " + jSONObject.getInt("error_code"));
}
return new String[0];
}
JSONArray optJSONArray = jSONObject.optJSONArray("data");
ArrayList arrayList = new ArrayList();
if (optJSONArray != null) {
for (int i = 0; i < optJSONArray.length(); i++) {
JSONObject optJSONObject = optJSONArray.optJSONObject(i);
if (optJSONObject != null) {
if (optJSONObject.optInt("basisOf") == 2) {
arrayList.add(optJSONObject.optString(OneTrack.Param.PKG));
}
}
}
}
String[] strArr = new String[arrayList.size()];
for (int i2 = 0; i2 < arrayList.size(); i2++) {
strArr[i2] = (String) arrayList.get(i2);
}
return strArr;
} catch (Exception e) {
Log.e(TAG, "getAllDetectUnsafeApps error, ", e);
return new String[0];
}
}
public static String getAllUnSystemAppsStatus(List<AntiDefraudAppInfo> list) {
try {
JSONObject jSONObject = new JSONObject();
jSONObject.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
jSONObject.put("os", MIUI_VERSION);
jSONObject.put("biz_id", "virus_scan");
jSONObject.put("uuid", b.a());
JSONArray jSONArray = new JSONArray();
for (int i = 0; i < list.size(); i++) {
JSONObject jSONObject2 = new JSONObject();
AntiDefraudAppInfo antiDefraudAppInfo = list.get(i);
jSONObject2.put(OneTrack.Param.PKG, antiDefraudAppInfo.getPkgName());
jSONObject2.put("version", antiDefraudAppInfo.getVersion());
jSONObject2.put("signature", antiDefraudAppInfo.getSign());
jSONObject2.put("appname", antiDefraudAppInfo.getAppName());
jSONArray.put(jSONObject2);
}
jSONObject.put("content", jSONArray);
String doPost = NetworkApiHelper.doPost(GuardApplication.d, DETECT_APPS_URL, jSONObject, SALT_FLASH);
if (DEBUG) {
Log.w("TAG", "responseDetectApp get: " + doPost);
}
return doPost;
} catch (Exception e) {
Log.e(TAG, "getAllUnSystemAppsStatus error, ", e);
return null;
}
}
public static String getDetectUnsafeAppStatus(String str) {
if (MiDetectAppsManager.getInstance(GuardApplication.d).virusInMiEngineRiskList(str)) {
return str;
}
return null;
}
public static String getMessageDigest(byte[] bArr) {
char[] cArr = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
try {
MessageDigest instance = MessageDigest.getInstance("MD5");
instance.update(bArr);
byte[] digest = instance.digest();
int length = digest.length;
char[] cArr2 = new char[(length * 2)];
int i = 0;
for (byte b2 : digest) {
int i2 = i + 1;
cArr2[i] = cArr[(b2 >>> 4) & 15];
i = i2 + 1;
cArr2[i2] = cArr[b2 & 15];
}
return new String(cArr2);
} catch (Exception e) {
Log.e(TAG, "getMessageDigest error, ", e);
return null;
}
}
private static Signature[] getRawSignature(Context context, String str) {
if (!(str == null || str.length() == 0)) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(str, 64);
if (packageInfo == null) {
return null;
}
return packageInfo.signatures;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, "getRawSignature error, ", e);
}
}
return null;
}
public static String getSign(Context context, String str) {
Signature[] rawSignature = getRawSignature(context, str);
if (rawSignature == null || rawSignature.length == 0) {
return null;
}
return getMessageDigest(rawSignature[0].toByteArray());
}
public static List<AntiDefraudAppInfo> getUnSystemAppList(Context context) {
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> installedPackages = packageManager.getInstalledPackages(0);
ArrayList arrayList = new ArrayList();
for (PackageInfo packageInfo : installedPackages) {
if ((packageInfo.applicationInfo.flags & 1) != 1) {
String str = packageInfo.packageName;
AntiDefraudAppInfo antiDefraudAppInfo = new AntiDefraudAppInfo();
antiDefraudAppInfo.setPkgName(str);
antiDefraudAppInfo.setVersion(String.valueOf(packageInfo.versionCode));
antiDefraudAppInfo.setSign(getSign(context, str));
try {
antiDefraudAppInfo.setAppName(URLEncoder.encode(packageInfo.applicationInfo.loadLabel(packageManager).toString(), "UTF-8"));
} catch (Exception e) {
Log.e(TAG, "getUnSystemAppList error, ", e);
}
arrayList.add(antiDefraudAppInfo);
}
}
return arrayList;
}
public static void updateAllDetectApps(Context context) {
try {
String allUnSystemAppsStatus = getAllUnSystemAppsStatus(getUnSystemAppList(GuardApplication.d));
if (!TextUtils.isEmpty(allUnSystemAppsStatus)) {
MiDetectAppsManager.getInstance(context).saveDetectApps(allUnSystemAppsStatus);
}
} catch (Exception e) {
Log.e(TAG, "updateAllDetectApps error, ", e);
}
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.