Android Rom識別各類品牌
阿新 • • 發佈:2018-12-21
import android.annotation.SuppressLint; import android.app.AppOpsManager; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.pm.ApplicationInfo; import android.net.Uri; import android.os.Build; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; import com.xinlu.vkeeper.R; import com.xinlu.vkeeper.views.CommomDialog; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * The type Rom. */ public class Rom { private static final String TAG = "Rom"; /** * The constant ROM_MIUI. */ public static final String ROM_MIUI = "MIUI"; /** * The constant ROM_EMUI. */ public static final String ROM_EMUI = "EMUI"; /** * The constant ROM_FLYME. */ public static final String ROM_FLYME = "FLYME"; /** * The constant ROM_OPPO. */ public static final String ROM_OPPO = "OPPO"; /** * The constant ROM_SMARTISAN. */ public static final String ROM_SMARTISAN = "SMARTISAN"; /** * The constant ROM_VIVO. */ public static final String ROM_VIVO = "VIVO"; /** * The constant ROM_QIKU. */ public static final String ROM_QIKU = "QIKU"; /** * The constant ROM_SAMSUNG. */ public static final String ROM_SAMSUNG = "SAMSUNG"; /** * The constant ROM_ZTE. */ public static final String ROM_ZTE = "ZTE"; private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name"; private static final String KEY_VERSION_EMUI = "ro.build.version.emui"; private static final String KEY_VERSION_OPPO = "ro.build.version.opporom"; private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version"; private static final String KEY_VERSION_VIVO = "ro.vivo.os.version"; private static String sName; private static String sVersion; private Context context; /** * Is emui boolean. * * @return the boolean */ public boolean isEmui() { return check(ROM_EMUI); } /** * Is miui boolean. * * @return the boolean */ public boolean isMiui() { return check(ROM_MIUI); } /** * Is vivo boolean. * * @return the boolean */ public boolean isVivo() { return check(ROM_VIVO); } /** * Is oppo boolean. * * @return the boolean */ public boolean isOppo() { return check(ROM_OPPO); } /** * Is flyme boolean. * * @return the boolean */ public boolean isFlyme() { return check(ROM_FLYME); } /** * Is sam sung boolean. * * @return the boolean */ public boolean isSamSung() { return check(ROM_SAMSUNG); } /** * Is zte boolean. * * @return the boolean */ public boolean isZTE() { return check(ROM_ZTE)||check("中興"); } /** * Is 360 boolean. * * @return the boolean */ public boolean is360() { return check(ROM_QIKU) || check("360"); } /** * Is smartisan boolean. * * @return the boolean */ public boolean isSmartisan() { return check(ROM_SMARTISAN); } private Rom rom = null; @SuppressLint("StaticFieldLeak") private static Rom rom1 = null; /** * Gets instance. * * @return the instance */ public static Rom getInstances() { if (rom1 == null) { rom1 = new Rom(); } return rom1; } /** * Gets instance. * * @return the instance */ public Rom getInstance() { if (rom == null) { rom = new Rom(); } return rom; } /** * Gets name. * * @return the name */ public String getName() { if (sName == null) { check(""); } return sName; } /** * Gets version. * * @return the version */ public String getVersion() { if (sVersion == null) { check(""); } return sVersion; } /** * Check boolean. * * @param rom the rom * @return the boolean */ public boolean check(String rom) { if (sName != null) { return sName.equals(rom); } if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) { sName = ROM_MIUI; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) { sName = ROM_EMUI; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) { sName = ROM_OPPO; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) { sName = ROM_VIVO; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) { sName = ROM_SMARTISAN; } else { sVersion = Build.DISPLAY; if (sVersion.toUpperCase().contains(ROM_FLYME)) { sName = ROM_FLYME; } else { sVersion = Build.UNKNOWN; sName = Build.MANUFACTURER.toUpperCase(); } } return sName.equals(rom); } /** * Gets prop. * * @param name the name * @return the prop */ public String getProp(String name) { String line = null; BufferedReader input = null; try { Process p = Runtime.getRuntime().exec("getprop " + name); input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); line = input.readLine(); input.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read prop " + name, ex); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } return line; } /** * 檢測VPN撥號手機通知提示 * * @param context the context */ public void checkVpnPhoneRom(Context context) { this.context = context; if (!isNotificationEnabled(context)) { if (isEmui()) { VpnNotificationDialogShow("華為手機需要開啟\n1.優先通知\n2.允許快速檢視"); } else if (isVivo()) { VpnNotificationDialogShow("vivo手機需要開啟許可權\n1.允許通知"); } else if (isMiui()) { VpnNotificationDialogShow("小米手機需要開啟\n1.視為優先通知\n2.允許快速檢視\n3.設為重要通知"); } else if (isOppo()) { VpnNotificationDialogShow("oppo手機需要前往通知管理開啟許可權\n1.允許通知"); } else if (isFlyme()) { VpnNotificationDialogShow("魅族手機需要開啟\n1.允許通知\n2.優先顯示\n3.懸浮通知"); } else { VpnNotificationDialogShow("當前手機需前往通知管理開啟許可權\n1.允許通知"); } } } /** * vpn通知許可權彈框提示 */ private void VpnNotificationDialogShow(final String name) { new CommomDialog(context, R.style.dialogs, "VPN撥號需要手動開啟通知全部許可權,否則會導致頻繁掉線。\n請點選開啟通知許可權。\n" + name, new CommomDialog.OnCloseListener() { @Override public void onClick(Dialog dialog, boolean confirm) { if (confirm) { checkRom(name); dialog.dismiss(); SPUtils.getInstance().setLocalString("notification","notification","notification"); } else { dialog.dismiss(); } } }).setTitle("溫馨提示").setTitleColor(false).setPositiveButton("立即開啟").setNegativeButton("我知道了").show(); } /** * 檢查rom跳轉的介面 */ private void checkRom(String name) { if (name.contains("oppo") || name.contains("vivo") || name.contains("當前")) { startSetting(); } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Intent intent = new Intent(); intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS"); intent.putExtra("app_package", context.getPackageName()); intent.putExtra("app_uid", context.getApplicationInfo().uid); context.startActivity(intent); } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) { Intent intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setData(Uri.parse("package:" + context.getPackageName())); context. startActivity(intent); } } } /** * 跳轉設定介面 */ private void startSetting() { Intent localIntent = new Intent(); localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS"); localIntent.setData(Uri.fromParts("package", context.getPackageName(), null)); context.startActivity(localIntent); } /** * 獲取通知欄許可權是否開啟 */ private static final String CHECK_OP_NO_THROW = "checkOpNoThrow"; private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION"; @SuppressLint("NewApi") private boolean isNotificationEnabled(Context context) { AppOpsManager mAppOps = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); ApplicationInfo appInfo = context.getApplicationInfo(); String pkg = context.getApplicationContext().getPackageName(); int uid = appInfo.uid; Class appOpsClass = null; /* Context.APP_OPS_MANAGER */ try { appOpsClass = Class.forName(AppOpsManager.class.getName()); Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class); Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION); int value = (Integer) opPostNotificationValue.get(Integer.class); return ((Integer) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED); } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } return false; } }