|
| 1 | +#pragma once |
| 2 | +#include <algorithm> |
| 3 | +#include <cctype> |
| 4 | +#include <cstddef> |
| 5 | +#include <string> |
| 6 | +#include <string_view> |
| 7 | +#include <sys/system_properties.h> |
| 8 | + |
| 9 | +namespace device_detect { |
| 10 | + |
| 11 | +static inline std::string lower_copy(std::string s) { |
| 12 | + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { |
| 13 | + return static_cast<char>(std::tolower(c)); |
| 14 | + }); |
| 15 | + return s; |
| 16 | +} |
| 17 | + |
| 18 | +static inline bool equals_ignore_case(std::string_view a, std::string_view b) { |
| 19 | + return lower_copy(std::string(a)) == lower_copy(std::string(b)); |
| 20 | +} |
| 21 | + |
| 22 | +static inline bool contains_ignore_case(std::string_view a, std::string_view b) { |
| 23 | + auto aa = lower_copy(std::string(a)); |
| 24 | + auto bb = lower_copy(std::string(b)); |
| 25 | + return aa.find(bb) != std::string::npos; |
| 26 | +} |
| 27 | + |
| 28 | +static inline std::string get_prop(const char* key) { |
| 29 | + char value[PROP_VALUE_MAX] = {0}; |
| 30 | + int len = __system_property_get(key, value); |
| 31 | + if (len <= 0) return {}; |
| 32 | + return std::string(value, static_cast<size_t>(len)); |
| 33 | +} |
| 34 | + |
| 35 | +static inline bool equals_any_ignore_case( |
| 36 | + std::string_view value, |
| 37 | + const char* const* candidates, |
| 38 | + size_t candidate_count |
| 39 | +) { |
| 40 | + for (size_t i = 0; i < candidate_count; ++i) { |
| 41 | + if (equals_ignore_case(value, candidates[i])) { |
| 42 | + return true; |
| 43 | + } |
| 44 | + } |
| 45 | + return false; |
| 46 | +} |
| 47 | + |
| 48 | +static inline bool contains_any_ignore_case( |
| 49 | + std::string_view value, |
| 50 | + const char* const* candidates, |
| 51 | + size_t candidate_count |
| 52 | +) { |
| 53 | + for (size_t i = 0; i < candidate_count; ++i) { |
| 54 | + if (contains_ignore_case(value, candidates[i])) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + return false; |
| 59 | +} |
| 60 | + |
| 61 | +static inline bool is_device_by_brand_tokens( |
| 62 | + const char* const* brand_tokens, |
| 63 | + size_t brand_token_count |
| 64 | +) { |
| 65 | + const char* strong_keys[] = { |
| 66 | + "ro.product.brand", |
| 67 | + "ro.product.manufacturer", |
| 68 | + "ro.product.vendor.brand", |
| 69 | + "ro.product.vendor.manufacturer", |
| 70 | + "ro.product.odm.brand", |
| 71 | + "ro.product.odm.manufacturer", |
| 72 | + "ro.product.system.brand", |
| 73 | + "ro.product.system.manufacturer", |
| 74 | + "ro.product.system_ext.brand", |
| 75 | + "ro.product.system_ext.manufacturer", |
| 76 | + }; |
| 77 | + |
| 78 | + for (const char* key : strong_keys) { |
| 79 | + std::string v = get_prop(key); |
| 80 | + if (equals_any_ignore_case(v, brand_tokens, brand_token_count)) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // 弱兜底:某些 ROM brand / manufacturer 不完整,但 model / name / fingerprint 里仍带品牌名。 |
| 86 | + const char* weak_keys[] = { |
| 87 | + "ro.product.model", |
| 88 | + "ro.product.vendor.model", |
| 89 | + "ro.product.name", |
| 90 | + "ro.product.device", |
| 91 | + "ro.build.product", |
| 92 | + "ro.build.fingerprint", |
| 93 | + "ro.vendor.build.fingerprint", |
| 94 | + "ro.product.build.fingerprint", |
| 95 | + }; |
| 96 | + |
| 97 | + for (const char* key : weak_keys) { |
| 98 | + std::string v = get_prop(key); |
| 99 | + if (contains_any_ignore_case(v, brand_tokens, brand_token_count)) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | +} |
| 106 | + |
| 107 | +static inline bool is_oneplus_device() { |
| 108 | + const char* brands[] = { |
| 109 | + "oneplus", |
| 110 | + }; |
| 111 | + return is_device_by_brand_tokens(brands, sizeof(brands) / sizeof(brands[0])); |
| 112 | +} |
| 113 | + |
| 114 | +static inline bool is_oppo_device() { |
| 115 | + const char* brands[] = { |
| 116 | + "oppo", |
| 117 | + }; |
| 118 | + return is_device_by_brand_tokens(brands, sizeof(brands) / sizeof(brands[0])); |
| 119 | +} |
| 120 | + |
| 121 | +// 推荐业务层用这个:识别 OnePlus / OPPO 设备。 |
| 122 | +static inline bool is_oplus_device() { |
| 123 | + const char* brands[] = { |
| 124 | + "oneplus", |
| 125 | + "oppo", |
| 126 | + }; |
| 127 | + return is_device_by_brand_tokens(brands, sizeof(brands) / sizeof(brands[0])); |
| 128 | +} |
| 129 | + |
| 130 | +} // namespace device_detect |