wif_service.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #ifndef WIF_SERVICE_H
  2. #define WIF_SERVICE_H
  3. #include <QObject>
  4. #include <QString>
  5. #include <QJsonObject>
  6. #include <QJsonDocument>
  7. /**
  8. * 集成网飞 (WIF) 系统基础服务 AIDL 的 C++ 包装.
  9. *
  10. * 调用 {@link #bind} 在 Activity 上下文上绑定服务, 之后可通过
  11. * {@link #request} 发起一次异步调用; 响应会通过 {@link responseReceived} 信号返回.
  12. *
  13. * 该类在 Android 以外的平台为空操作, 所有方法可安全调用, 信号永远不会触发.
  14. */
  15. class WifService : public QObject
  16. {
  17. Q_OBJECT
  18. public:
  19. explicit WifService(QObject *parent = nullptr);
  20. ~WifService() override;
  21. /** 绑定 AIDL 服务. 多次调用安全, 只生效一次. */
  22. void bind();
  23. /** 解绑. */
  24. void unbind();
  25. /**
  26. * 发起一次请求.
  27. *
  28. * @param type 对应 AidlConstants.TYPE_*, 例如 "location" / "reboot" / "device_info".
  29. * @param param 可选, 请求 param 字段, 序列化为 JSON 对象. 传空对象表示无 param.
  30. * @return 0 表示提交成功, -1 表示服务未就绪.
  31. */
  32. Q_INVOKABLE int request(const QString &type, const QJsonObject &param = QJsonObject());
  33. /** QML 友好的 request 重载, param 使用 QVariantMap, 会自动转为 JSON 对象. */
  34. Q_INVOKABLE int requestMap(const QString &type, const QVariantMap &param = QVariantMap());
  35. /**
  36. * 发起一次请求 (高级). 用于直接控制完整 JSON 负载的场景.
  37. *
  38. * @param json 形如 {"param":{...},"type":"xxx"} 的完整请求 JSON.
  39. */
  40. Q_INVOKABLE int requestRaw(const QString &json);
  41. /** 便捷封装: reboot (无 param). */
  42. Q_INVOKABLE int reboot();
  43. /** 便捷封装: 获取位置信息. */
  44. Q_INVOKABLE int requestLocation();
  45. /** 便捷封装: 获取设备信息 (SN / cpu_serial / ethernet_mac). */
  46. Q_INVOKABLE int requestDeviceInfo();
  47. /** 便捷封装: 设置系统时间. timeMs 为 epoch millis. */
  48. Q_INVOKABLE int setTime(qint64 timeMs);
  49. /** 便捷封装: 设置热点开关 / 配置. */
  50. Q_INVOKABLE int setHotspot(const QString &ssid, const QString &password);
  51. /** 便捷封装: 设置 WiFi. */
  52. Q_INVOKABLE int setWifi(const QString &ssid, const QString &password,
  53. bool isStatic = false,
  54. const QString &ip = QString(),
  55. const QString &dns1 = QString(),
  56. const QString &dns2 = QString());
  57. /** 便捷封装: 设置以太网. */
  58. Q_INVOKABLE int setEthernet(const QString &ip, const QString &netmask,
  59. const QString &gateway,
  60. const QString &dns1, const QString &dns2);
  61. /** 便捷封装: 切换以太网接口开关. */
  62. Q_INVOKABLE int setEthernetEnable(const QString &iface, bool enable);
  63. /** 便捷封装: 状态栏显示/隐藏. visible=true 显示, false 隐藏. */
  64. Q_INVOKABLE int setStatusBarVisible(bool visible);
  65. /** 便捷封装: 设置屏幕亮度 (0..255). */
  66. Q_INVOKABLE int setBrightness(int brightness);
  67. /** 便捷封装: 设置音量 (0..100). */
  68. Q_INVOKABLE int setVolume(int volume);
  69. /** 便捷封装: 开关定位. */
  70. Q_INVOKABLE int setLocationEnable(bool enable);
  71. /** 便捷封装: 启动桌面 (返回 launcher). */
  72. Q_INVOKABLE int startHome();
  73. /** 便捷封装: 恢复出厂设置. */
  74. Q_INVOKABLE int factoryReset();
  75. /** 便捷封装: 安装应用. apkPath 完整路径. */
  76. Q_INVOKABLE int install(const QString &apkPath, bool autoStart = false);
  77. /** 便捷封装: 卸载应用. */
  78. Q_INVOKABLE int uninstall(const QString &packageName);
  79. /** 便捷封装: 启动应用. */
  80. Q_INVOKABLE int startApp(const QString &packageName);
  81. /** 便捷封装: 锁定 / 解锁某项功能. */
  82. Q_INVOKABLE int setFunctionLock(const QString &function, const QString &password);
  83. /** 便捷封装: 设置锁屏方式. */
  84. Q_INVOKABLE int setScreenLock(const QString &newLockType, const QString &newLock,
  85. const QString &currentLockType = QString(),
  86. const QString &currentLock = QString());
  87. /** 便捷封装: 添加桌面 widget. */
  88. Q_INVOKABLE int addAppWidget(const QString &packageName, const QString &className,
  89. int screen, int cellX, int cellY, int spanX, int spanY);
  90. /** 便捷封装: 配置安装白名单. */
  91. Q_INVOKABLE int configInstallWhitelist(const QString &action, const QString &packageName);
  92. /** 便捷封装: 自启动 Launcher. */
  93. Q_INVOKABLE int setCustomLauncher(const QString &packageName);
  94. /** 便捷封装: 重启 4G 模组. */
  95. Q_INVOKABLE int rebootModem();
  96. /** 便捷封装: 自定义开机动画, filePath 完整路径. */
  97. Q_INVOKABLE int setCustomBootAnimation(const QString &filePath);
  98. /** 便捷封装: 自动校时开关. */
  99. Q_INVOKABLE int setAutoTime(bool enable);
  100. /** 便捷封装: 风扇控制. level: 0..N. */
  101. Q_INVOKABLE int setFanControl(int level);
  102. /** 便捷封装: 清除应用数据. */
  103. Q_INVOKABLE int clearAppStorage(const QString &packageName);
  104. /** 便捷封装: Kiosk (自助模式) 开关. */
  105. Q_INVOKABLE int setKioskMode(bool enable);
  106. /** 便捷封装: OTA 更新, filePath 完整路径. */
  107. Q_INVOKABLE int otaUpdate(const QString &filePath);
  108. /** 服务是否已连接 (仅 Android 有效). */
  109. Q_PROPERTY(bool connected READ isConnected NOTIFY connectedChanged)
  110. bool isConnected() const;
  111. /** 由 JNI 回调设置连接状态. */
  112. void setConnected(bool connected);
  113. /** 便捷封装: 重命名包别名. */
  114. Q_INVOKABLE int setAlias(const QString &packageName, const QString &alias);
  115. /** AidlConstants 对应常量 (与 Java 端 com.wif.bslib.AidlConstants 一致). */
  116. static QString ACTION_BASE_SERVICE() { return QStringLiteral("com.wif.aidl.baseservice"); }
  117. static QString PACKAGE_BASE_SERVICE() { return QStringLiteral("com.wif.baseservice"); }
  118. static QString TYPE_REBOOT() { return QStringLiteral("reboot"); }
  119. static QString TYPE_STATUS_BAR() { return QStringLiteral("status_bar"); }
  120. static QString TYPE_HOTSPOT() { return QStringLiteral("hotspot"); }
  121. static QString TYPE_HOTSPOT_CONFIG() { return QStringLiteral("hotspot_config"); }
  122. static QString TYPE_WIFI() { return QStringLiteral("wifi"); }
  123. static QString TYPE_VOLUME() { return QStringLiteral("volume"); }
  124. static QString TYPE_BRIGHTNESS() { return QStringLiteral("brightness"); }
  125. static QString TYPE_FACTORY_RESET() { return QStringLiteral("factory_reset"); }
  126. static QString TYPE_INSTALL() { return QStringLiteral("install"); }
  127. static QString TYPE_INSTALL_AND_START() { return QStringLiteral("install_and_start"); }
  128. static QString TYPE_UNINSTALL() { return QStringLiteral("uninstall"); }
  129. static QString TYPE_ALIAS() { return QStringLiteral("alias"); }
  130. static QString TYPE_WIFI_CONFIG() { return QStringLiteral("wifi_config"); }
  131. static QString TYPE_ETHERNET_CONFIG() { return QStringLiteral("ethernet_config"); }
  132. static QString TYPE_ETHERNET_ENABLE() { return QStringLiteral("ethernet_enable"); }
  133. static QString TYPE_LOCATION() { return QStringLiteral("location"); }
  134. static QString TYPE_SET_TIME() { return QStringLiteral("set_time"); }
  135. static QString TYPE_AUTO_TIME() { return QStringLiteral("auto_time"); }
  136. static QString TYPE_FAN_CONTROL() { return QStringLiteral("fan_control"); }
  137. static QString TYPE_START_HOME() { return QStringLiteral("start_home"); }
  138. static QString TYPE_REBOOT_MODEM() { return QStringLiteral("reboot_modem"); }
  139. static QString TYPE_CUSTOM_LAUNCHER() { return QStringLiteral("custom_launcher"); }
  140. static QString TYPE_DEVICE_INFO() { return QStringLiteral("device_info"); }
  141. static QString TYPE_CLEAR_SCREEN_LOCK() { return QStringLiteral("clear_screen_lock"); }
  142. static QString TYPE_SET_SCREEN_LOCK() { return QStringLiteral("set_screen_lock"); }
  143. static QString TYPE_CLEAR_APP_STORAGE() { return QStringLiteral("clear_app_storage"); }
  144. static QString TYPE_CONFIG_INSTALL_WHITELIST() { return QStringLiteral("config_install_whitelist"); }
  145. static QString TYPE_LOCATION_ENABLE() { return QStringLiteral("location_enable"); }
  146. static QString TYPE_ADD_APP_WIDGET() { return QStringLiteral("add_app_widget"); }
  147. static QString TYPE_CUSTOM_BOOT_ANIMATION() { return QStringLiteral("custom_boot_animation"); }
  148. static QString TYPE_OTA_UPDATE() { return QStringLiteral("ota_update"); }
  149. static QString TYPE_FUNCTION_LOCK() { return QStringLiteral("function_lock"); }
  150. static QString TYPE_KIOSK_MODE() { return QStringLiteral("kiosk_mode"); }
  151. /** 响应 code. */
  152. static int SUCCESS() { return 1; }
  153. static int FAIL() { return 0; }
  154. static int PARAM_ERROR() { return -1; }
  155. signals:
  156. /**
  157. * 服务连接状态变化.
  158. * @param connected true=已连接, false=已断开.
  159. */
  160. void connectedChanged(bool connected);
  161. /**
  162. * 服务回调响应.
  163. * @param type 对应请求的 type.
  164. * @param code 1 成功 / 0 失败 / -1 参数错误.
  165. * @param msg 失败时的提示消息, 可能为空.
  166. * @param json 完整响应 JSON 字符串, 业务层 data 字段以 JSON 形式承载.
  167. */
  168. void responseReceived(const QString &type, int code, const QString &msg, const QString &json);
  169. private slots:
  170. void onResponseFromJni(const QString &type, int code,
  171. const QString &msg, const QString &json);
  172. private:
  173. class Private;
  174. Private *d;
  175. Q_DISABLE_COPY(WifService)
  176. };
  177. #endif // WIF_SERVICE_H