wif_service.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #include "wif_service.h"
  2. #include <QCoreApplication>
  3. #include <QDateTime>
  4. #include <QDebug>
  5. #include <QJsonDocument>
  6. #include <QJsonObject>
  7. #include <QLoggingCategory>
  8. #include <QPointer>
  9. #include <QVariantMap>
  10. #include <atomic>
  11. #ifdef Q_OS_ANDROID
  12. # include <QJniEnvironment>
  13. # include <QJniObject>
  14. # include <QtCore/private/qandroidextras_p.h>
  15. # include <QtJniTypes>
  16. # include <jni.h>
  17. #else
  18. # include <QJniObject>
  19. #endif
  20. Q_LOGGING_CATEGORY(lcWif, "wif.service")
  21. namespace {
  22. // 由 onNativeResponse 设置/读取的全局单例指针. Binder 线程会读取它,
  23. // 然后用 QueuedConnection 把响应派发回主线程的 WifService 实例.
  24. std::atomic<WifService *> g_instance{nullptr};
  25. // 标记 native 方法是否已成功注册到 Java 类. 在 JNI_OnLoad 时注册一次,
  26. // 如果失败, 会在首次 bind() 时重试 (此时 App 类已确保加载).
  27. std::atomic<bool> g_nativesRegistered{false};
  28. } // namespace
  29. // ---------------------------------------------------------------------------
  30. // JNI native 方法实现. 必须与 Java 侧 WifServiceBridge.onNativeResponse 严格一致.
  31. // 参数类型会被 Q_DECLARE_JNI_NATIVE_METHOD 自动转换: QString <-> jstring.
  32. // ---------------------------------------------------------------------------
  33. static void onNativeResponse(JNIEnv *env, jclass /*clazz*/,
  34. QString type, int code,
  35. QString msg, QString json)
  36. {
  37. Q_UNUSED(env);
  38. auto *svc = g_instance.load(std::memory_order_acquire);
  39. if (!svc) {
  40. return;
  41. }
  42. // 派发到 WifService 所在线程 (一般是主线程). 使用 QueuedConnection.
  43. QMetaObject::invokeMethod(svc, "onResponseFromJni",
  44. Qt::QueuedConnection,
  45. Q_ARG(QString, std::move(type)),
  46. Q_ARG(int, code),
  47. Q_ARG(QString, std::move(msg)),
  48. Q_ARG(QString, std::move(json)));
  49. }
  50. // ---------------------------------------------------------------------------
  51. // JNI native 方法: 服务连接状态变化回调.
  52. // ---------------------------------------------------------------------------
  53. static void onConnectionChangedNative(JNIEnv *env, jclass /*clazz*/, bool connected)
  54. {
  55. Q_UNUSED(env);
  56. auto *svc = g_instance.load(std::memory_order_acquire);
  57. if (!svc) {
  58. return;
  59. }
  60. QMetaObject::invokeMethod(svc, [svc, connected]() {
  61. emit svc->connectedChanged(connected);
  62. }, Qt::QueuedConnection);
  63. }
  64. // ---------------------------------------------------------------------------
  65. // 注册 native 方法 (到 Java 类 WifServiceBridge).
  66. // 必须在能正确解析 WifServiceBridge 这个 Java 类的线程上调用. 在 JNI_OnLoad
  67. // 阶段不一定能拿到 App 的 ClassLoader, 所以保留 tryRegisterNatives() 供
  68. // WifService::bind() 在主线程再次尝试, 确保 native 方法注册成功.
  69. // ---------------------------------------------------------------------------
  70. Q_DECLARE_JNI_CLASS(WifServiceBridgeJ, "com/yfkj/bzyv1/WifServiceBridge")
  71. Q_DECLARE_JNI_NATIVE_METHOD(onNativeResponse)
  72. Q_DECLARE_JNI_NATIVE_METHOD(onConnectionChangedNative)
  73. static bool tryRegisterNatives()
  74. {
  75. if (g_nativesRegistered.load(std::memory_order_acquire)) {
  76. return true;
  77. }
  78. QJniEnvironment qenv;
  79. if (!qenv.jniEnv()) {
  80. return false;
  81. }
  82. bool ok = qenv.registerNativeMethods<QtJniTypes::WifServiceBridgeJ>({
  83. Q_JNI_NATIVE_METHOD(onNativeResponse),
  84. Q_JNI_NATIVE_METHOD(onConnectionChangedNative)
  85. });
  86. if (ok) {
  87. g_nativesRegistered.store(true, std::memory_order_release);
  88. }
  89. qCDebug(lcWif) << "WifService registerNativeMethods:" << ok;
  90. return ok;
  91. }
  92. Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/)
  93. {
  94. // 尝试立即注册; 若失败会在 bind() 时再次尝试 (此时 app 类必定已加载).
  95. tryRegisterNatives();
  96. return JNI_VERSION_1_6;
  97. }
  98. // ---------------------------------------------------------------------------
  99. // PIMPL
  100. // ---------------------------------------------------------------------------
  101. class WifService::Private
  102. {
  103. public:
  104. bool bound = false;
  105. };
  106. WifService::WifService(QObject *parent)
  107. : QObject(parent), d(new Private)
  108. {
  109. g_instance.store(this, std::memory_order_release);
  110. }
  111. WifService::~WifService()
  112. {
  113. unbind();
  114. if (g_instance.load(std::memory_order_acquire) == this) {
  115. g_instance.store(nullptr, std::memory_order_release);
  116. }
  117. delete d;
  118. }
  119. void WifService::bind()
  120. {
  121. if (d->bound) {
  122. return;
  123. }
  124. #ifdef Q_OS_ANDROID
  125. QJniObject ctx = QNativeInterface::QAndroidApplication::context();
  126. if (!ctx.isValid()) {
  127. qCWarning(lcWif) << "bind: Android context not available";
  128. return;
  129. }
  130. // 确保 native 方法已注册 (如果 JNI_OnLoad 时失败, 这里再补一次).
  131. tryRegisterNatives();
  132. QJniObject::callStaticMethod<void>(
  133. "com/yfkj/bzyv1/WifServiceBridge",
  134. "bind",
  135. "(Landroid/content/Context;)V",
  136. ctx.object());
  137. d->bound = true;
  138. qCDebug(lcWif) << "WifService bound";
  139. #else
  140. qCDebug(lcWif) << "bind: non-Android platform, no-op";
  141. #endif
  142. }
  143. void WifService::unbind()
  144. {
  145. if (!d->bound) {
  146. return;
  147. }
  148. #ifdef Q_OS_ANDROID
  149. QJniObject ctx = QNativeInterface::QAndroidApplication::context();
  150. if (ctx.isValid()) {
  151. QJniObject::callStaticMethod<void>(
  152. "com/yfkj/bzyv1/WifServiceBridge",
  153. "unbind",
  154. "(Landroid/content/Context;)V",
  155. ctx.object());
  156. }
  157. #endif
  158. d->bound = false;
  159. }
  160. int WifService::request(const QString &type, const QJsonObject &param)
  161. {
  162. QJsonObject root;
  163. root.insert(QStringLiteral("type"), type);
  164. if (!param.isEmpty()) {
  165. root.insert(QStringLiteral("param"), param);
  166. }
  167. return requestRaw(QString::fromUtf8(QJsonDocument(root).toJson(QJsonDocument::Compact)));
  168. }
  169. int WifService::requestMap(const QString &type, const QVariantMap &param)
  170. {
  171. return request(type, QJsonObject::fromVariantMap(param));
  172. }
  173. int WifService::requestRaw(const QString &json)
  174. {
  175. #ifdef Q_OS_ANDROID
  176. QJniObject jstr = QJniObject::fromString(json);
  177. jint ret = QJniObject::callStaticMethod<jint>(
  178. "com/yfkj/bzyv1/WifServiceBridge",
  179. "request",
  180. "(Ljava/lang/String;)I",
  181. jstr.object());
  182. return static_cast<int>(ret);
  183. #else
  184. Q_UNUSED(json);
  185. qCDebug(lcWif) << "requestRaw (no-op):" << json;
  186. return -1;
  187. #endif
  188. }
  189. void WifService::onResponseFromJni(const QString &type, int code,
  190. const QString &msg, const QString &json)
  191. {
  192. emit responseReceived(type, code, msg, json);
  193. }
  194. // ===========================================================================
  195. // 便捷封装
  196. // ===========================================================================
  197. int WifService::reboot()
  198. {
  199. QJsonObject p; p.insert(QStringLiteral("delay"), 0);
  200. return request(TYPE_REBOOT(), p);
  201. }
  202. int WifService::requestLocation()
  203. {
  204. return request(TYPE_LOCATION());
  205. }
  206. int WifService::requestDeviceInfo()
  207. {
  208. return request(TYPE_DEVICE_INFO());
  209. }
  210. int WifService::setTime(qint64 timeMs)
  211. {
  212. QJsonObject p;
  213. p.insert(QStringLiteral("time"), timeMs);
  214. return request(TYPE_SET_TIME(), p);
  215. }
  216. int WifService::setHotspot(const QString &ssid, const QString &password)
  217. {
  218. QJsonObject p;
  219. p.insert(QStringLiteral("ssid"), ssid);
  220. p.insert(QStringLiteral("password"), password);
  221. return request(TYPE_HOTSPOT_CONFIG(), p);
  222. }
  223. int WifService::setWifi(const QString &ssid, const QString &password,
  224. bool isStatic, const QString &ip,
  225. const QString &dns1, const QString &dns2)
  226. {
  227. QJsonObject p;
  228. p.insert(QStringLiteral("ssid"), ssid);
  229. p.insert(QStringLiteral("password"), password);
  230. if (isStatic) {
  231. p.insert(QStringLiteral("isStatic"), true);
  232. p.insert(QStringLiteral("ip"), ip);
  233. p.insert(QStringLiteral("dns1"), dns1);
  234. p.insert(QStringLiteral("dns2"), dns2);
  235. } else {
  236. p.insert(QStringLiteral("isStatic"), false);
  237. }
  238. return request(TYPE_WIFI_CONFIG(), p);
  239. }
  240. int WifService::setEthernet(const QString &ip, const QString &netmask,
  241. const QString &gateway,
  242. const QString &dns1, const QString &dns2)
  243. {
  244. QJsonObject p;
  245. p.insert(QStringLiteral("ip"), ip);
  246. p.insert(QStringLiteral("netmask"), netmask);
  247. p.insert(QStringLiteral("gateway"), gateway);
  248. p.insert(QStringLiteral("dns1"), dns1);
  249. p.insert(QStringLiteral("dns2"), dns2);
  250. return request(TYPE_ETHERNET_CONFIG(), p);
  251. }
  252. int WifService::setEthernetEnable(const QString &iface, bool enable)
  253. {
  254. QJsonObject p;
  255. p.insert(QStringLiteral("iface"), iface);
  256. p.insert(QStringLiteral("enable"), enable);
  257. return request(TYPE_ETHERNET_ENABLE(), p);
  258. }
  259. int WifService::setStatusBarVisible(bool visible)
  260. {
  261. QJsonObject p;
  262. p.insert(QStringLiteral("show"), visible);
  263. return request(TYPE_STATUS_BAR(), p);
  264. }
  265. int WifService::setBrightness(int brightness)
  266. {
  267. QJsonObject p;
  268. p.insert(QStringLiteral("brightness"), brightness);
  269. return request(TYPE_BRIGHTNESS(), p);
  270. }
  271. int WifService::setVolume(int volume)
  272. {
  273. QJsonObject p;
  274. p.insert(QStringLiteral("volume"), volume);
  275. return request(TYPE_VOLUME(), p);
  276. }
  277. int WifService::setLocationEnable(bool enable)
  278. {
  279. QJsonObject p;
  280. p.insert(QStringLiteral("enable"), enable);
  281. return request(TYPE_LOCATION_ENABLE(), p);
  282. }
  283. int WifService::startHome()
  284. {
  285. return request(TYPE_START_HOME());
  286. }
  287. int WifService::factoryReset()
  288. {
  289. return request(TYPE_FACTORY_RESET());
  290. }
  291. int WifService::install(const QString &apkPath, bool autoStart)
  292. {
  293. QJsonObject p;
  294. p.insert(QStringLiteral("path"), apkPath);
  295. return request(autoStart ? TYPE_INSTALL_AND_START() : TYPE_INSTALL(), p);
  296. }
  297. int WifService::uninstall(const QString &packageName)
  298. {
  299. QJsonObject p;
  300. p.insert(QStringLiteral("packageName"), packageName);
  301. return request(TYPE_UNINSTALL(), p);
  302. }
  303. int WifService::startApp(const QString &packageName)
  304. {
  305. QJsonObject p;
  306. p.insert(QStringLiteral("packageName"), packageName);
  307. return request(TYPE_INSTALL_AND_START(), p);
  308. }
  309. int WifService::setFunctionLock(const QString &function, const QString &password)
  310. {
  311. QJsonObject p;
  312. p.insert(QStringLiteral("function"), function);
  313. p.insert(QStringLiteral("new_password"), password);
  314. return request(TYPE_FUNCTION_LOCK(), p);
  315. }
  316. int WifService::setScreenLock(const QString &newLockType, const QString &newLock,
  317. const QString &currentLockType, const QString &currentLock)
  318. {
  319. QJsonObject p;
  320. p.insert(QStringLiteral("newLockType"), newLockType);
  321. p.insert(QStringLiteral("newLock"), newLock);
  322. if (!currentLockType.isEmpty()) {
  323. p.insert(QStringLiteral("currentLockType"), currentLockType);
  324. p.insert(QStringLiteral("currentLock"), currentLock);
  325. }
  326. return request(TYPE_SET_SCREEN_LOCK(), p);
  327. }
  328. int WifService::addAppWidget(const QString &packageName, const QString &className,
  329. int screen, int cellX, int cellY, int spanX, int spanY)
  330. {
  331. QJsonObject p;
  332. p.insert(QStringLiteral("packageName"), packageName);
  333. p.insert(QStringLiteral("className"), className);
  334. p.insert(QStringLiteral("screen"), screen);
  335. p.insert(QStringLiteral("cellX"), cellX);
  336. p.insert(QStringLiteral("cellY"), cellY);
  337. p.insert(QStringLiteral("spanX"), spanX);
  338. p.insert(QStringLiteral("spanY"), spanY);
  339. return request(TYPE_ADD_APP_WIDGET(), p);
  340. }
  341. int WifService::configInstallWhitelist(const QString &action, const QString &packageName)
  342. {
  343. QJsonObject p;
  344. p.insert(QStringLiteral("action"), action);
  345. p.insert(QStringLiteral("package_name"), packageName);
  346. return request(TYPE_CONFIG_INSTALL_WHITELIST(), p);
  347. }
  348. int WifService::setCustomLauncher(const QString &packageName)
  349. {
  350. QJsonObject p;
  351. p.insert(QStringLiteral("packageName"), packageName);
  352. return request(TYPE_CUSTOM_LAUNCHER(), p);
  353. }
  354. int WifService::rebootModem()
  355. {
  356. return request(TYPE_REBOOT_MODEM());
  357. }
  358. int WifService::setCustomBootAnimation(const QString &filePath)
  359. {
  360. QJsonObject p;
  361. p.insert(QStringLiteral("path"), filePath);
  362. return request(TYPE_CUSTOM_BOOT_ANIMATION(), p);
  363. }
  364. int WifService::setAutoTime(bool enable)
  365. {
  366. QJsonObject p;
  367. p.insert(QStringLiteral("enable"), enable);
  368. return request(TYPE_AUTO_TIME(), p);
  369. }
  370. int WifService::setFanControl(int level)
  371. {
  372. QJsonObject p;
  373. p.insert(QStringLiteral("level"), level);
  374. return request(TYPE_FAN_CONTROL(), p);
  375. }
  376. int WifService::clearAppStorage(const QString &packageName)
  377. {
  378. QJsonObject p;
  379. p.insert(QStringLiteral("packageName"), packageName);
  380. return request(TYPE_CLEAR_APP_STORAGE(), p);
  381. }
  382. int WifService::setKioskMode(bool enable)
  383. {
  384. QJsonObject root;
  385. root.insert(QStringLiteral("type"), TYPE_KIOSK_MODE());
  386. root.insert(QStringLiteral("param"), QJsonValue(enable));
  387. return requestRaw(QString::fromUtf8(QJsonDocument(root).toJson(QJsonDocument::Compact)));
  388. }
  389. int WifService::otaUpdate(const QString &filePath)
  390. {
  391. QJsonObject p;
  392. p.insert(QStringLiteral("path"), filePath);
  393. return request(TYPE_OTA_UPDATE(), p);
  394. }
  395. int WifService::setAlias(const QString &packageName, const QString &alias)
  396. {
  397. QJsonObject p;
  398. p.insert(QStringLiteral("packageName"), packageName);
  399. p.insert(QStringLiteral("alias"), alias);
  400. return request(TYPE_ALIAS(), p);
  401. }
  402. #include "moc_wif_service.cpp"