Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .idea/libraries/Flutter_Plugins.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 2.1.1
* feat: 适配 OHOS (HarmonyOS) 平台

# 2.1.0
* fix: #4

Expand Down
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

[![Pub](https://img.shields.io/pub/v/fk_user_agent.svg)](https://pub.dartlang.org/packages/fk_user_agent)
[![Awesome Flutter](https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square)]()
[![Awesome Flutter](https://img.shields.io/badge/Platform-Android_iOS-blue.svg?longCache=true&style=flat-square)]()
[![Awesome Flutter](https://img.shields.io/badge/Platform-Android_iOS_OHOS-blue.svg?longCache=true&style=flat-square)]()
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](/LICENSE)

Retrieve Android/iOS device user agents in Flutter.
Retrieve Android/iOS/OHOS device user agents in Flutter.

### Example user-agents:

| System | User-Agent | WebView User-Agent |
| ------ | ---------- | ------------------ |
| iOS | CFNetwork/897.15 Darwin/17.5.0 (iPhone/6s iOS/11.3) | Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E217 |
| Android | Dalvik/2.1.0 (Linux; U; Android 5.1.1; Android SDK built for x86 Build/LMY48X) | Mozilla/5.0 (Linux; Android 5.1.1; Android SDK built for x86 Build/LMY48X) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/39.0.0.0 Mobile Safari/537.36 |
| OHOS | HarmonyOS/6.0.0 (phone; HUAWEI BLK-AL80; OpenHarmony-6.0.2.130) | Mozilla/5.0 (Phone; OpenHarmony 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 ArkWeb/6.0.0.130 Mobile |

### Additionally:

Expand Down Expand Up @@ -40,6 +41,18 @@ Android version returns:
- applicationBuildNumber
- packageUserAgent

OHOS (HarmonyOS) version returns:
- systemName
- systemVersion
- packageName
- shortPackageName
- applicationName
- applicationVersion
- applicationBuildNumber
- packageUserAgent
- userAgent
- webViewUserAgent

### Credits 👍

Based of https://github.com/j0j00/flutter_user_agent
34 changes: 27 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';

import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:flutter/material.dart';
Expand All @@ -14,7 +15,9 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
String _userAgent = 'Unknown';
String _webViewUserAgent = 'Unknown';
String _properties = 'Unknown';

@override
void initState() {
Expand All @@ -27,13 +30,18 @@ class _MyAppState extends State<MyApp> {

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
String userAgent, webViewUserAgent, properties;
// Platform messages may fail, so we use a try/catch PlatformException.
try {
platformVersion = FkUserAgent.userAgent!;
print(platformVersion);
userAgent = FkUserAgent.userAgent ?? 'Unknown';
webViewUserAgent = FkUserAgent.webViewUserAgent ?? 'Unknown';
properties =
const JsonEncoder.withIndent(' ').convert(FkUserAgent.properties);
print('userAgent: $userAgent');
print('webViewUserAgent: $webViewUserAgent');
print('properties: $properties');
} on PlatformException {
platformVersion = 'Failed to get platform version.';
userAgent = webViewUserAgent = properties = 'Failed to get platform version.';
}

// If the widget was removed from the tree while the asynchronous platform
Expand All @@ -42,7 +50,9 @@ class _MyAppState extends State<MyApp> {
if (!mounted) return;

setState(() {
_platformVersion = platformVersion;
_userAgent = userAgent;
_webViewUserAgent = webViewUserAgent;
_properties = properties;
});
}

Expand All @@ -54,7 +64,17 @@ class _MyAppState extends State<MyApp> {
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('userAgent:'),
Text('$_userAgent\n'),
Text('webViewUserAgent:'),
Text('$_webViewUserAgent\n'),
Text('properties:'),
Text('$_properties\n'),
],
),
),
),
);
Expand Down
19 changes: 19 additions & 0 deletions example/ohos/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/node_modules
/oh_modules
/local.properties
/.idea
**/build
/.hvigor
.cxx
/.clangd
/.clang-format
/.clang-tidy
**/.test
*.har
**/BuildProfile.ets
**/oh-package-lock.json5

**/src/main/resources/rawfile/flutter_assets/
**/libs/arm64-v8a/libapp.so
**/libs/arm64-v8a/libflutter.so
**/libs/arm64-v8a/libvmservice_snapshot.so
10 changes: 10 additions & 0 deletions example/ohos/AppScope/app.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"app": {
"bundleName": "io.flutterfastkit.fk_user_agent.fk_user_agent_ohos_example",
"vendor": "example",
"versionCode": 1000000,
"versionName": "1.0.0",
"icon": "$media:app_icon",
"label": "$string:app_name"
}
}
8 changes: 8 additions & 0 deletions example/ohos/AppScope/resources/base/element/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"string": [
{
"name": "app_name",
"value": "fk_user_agent_ohos_example"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions example/ohos/build-profile.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"app": {
"signingConfigs": [
{
"name": "default",
"type": "HarmonyOS",
"material": {
"certpath": "C:\\Users\\dqh0334\\.ohos\\config\\default_ohos_DtFdLho8PHcsZv1JTh3N5Z4Pn8IUtW2fXtEFc3pCPi8=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001BB78A7ACB355051C4AAE8D45409334F5DEBA6697D194EE7C08ADDF30125B0EFB740A65DC00C14B9",
"profile": "C:\\Users\\dqh0334\\.ohos\\config\\default_ohos_DtFdLho8PHcsZv1JTh3N5Z4Pn8IUtW2fXtEFc3pCPi8=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\dqh0334\\.ohos\\config\\default_ohos_DtFdLho8PHcsZv1JTh3N5Z4Pn8IUtW2fXtEFc3pCPi8=.p12",
"storePassword": "0000001B8CCB238AEB70CEFC976533B15145FE9AFCF304CCE62C50ACA5DA05153B465195C7691D30EF278B"
}
}
],
"products": [
{
"name": "default",
"signingConfig": "default",
"compatibleSdkVersion": "5.0.0(12)",
"runtimeOS": "HarmonyOS"
}
]
},
"modules": [
{
"name": "entry",
"srcPath": "./entry",
"targets": [
{
"name": "default",
"applyToProducts": [
"default"
]
}
]
}
]
}
7 changes: 7 additions & 0 deletions example/ohos/entry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

/node_modules
/oh_modules
/.preview
/build
/.cxx
/.test
14 changes: 14 additions & 0 deletions example/ohos/entry/build-profile.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"apiType": 'stageMode',
"buildOption": {
},
"targets": [
{
"name": "default",
"runtimeOS": "HarmonyOS"
},
{
"name": "ohosTest",
}
]
}
2 changes: 2 additions & 0 deletions example/ohos/entry/hvigorfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
export { hapTasks } from '@ohos/hvigor-ohos-plugin';
12 changes: 12 additions & 0 deletions example/ohos/entry/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "entry",
"version": "1.0.0",
"description": "Please describe the basic information.",
"main": "",
"author": "",
"license": "",
"dependencies": {
"fk_user_agent": "file:../har/fk_user_agent.har",
"@ohos/flutter_ohos": "har/flutter.har"
}
}
9 changes: 9 additions & 0 deletions example/ohos/entry/src/main/ets/entryability/EntryAbility.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { FlutterAbility, FlutterEngine } from '@ohos/flutter_ohos';
import { GeneratedPluginRegistrant } from '../plugins/GeneratedPluginRegistrant';

export default class EntryAbility extends FlutterAbility {
configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}
23 changes: 23 additions & 0 deletions example/ohos/entry/src/main/ets/pages/Index.ets
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import common from '@ohos.app.ability.common';
import { FlutterPage } from '@ohos/flutter_ohos'

let storage = LocalStorage.getShared()
const EVENT_BACK_PRESS = 'EVENT_BACK_PRESS'

@Entry(storage)
@Component
struct Index {
private context = getContext(this) as common.UIAbilityContext
@LocalStorageLink('viewId') viewId: string = "";

build() {
Column() {
FlutterPage({ viewId: this.viewId })
}
}

onBackPress(): boolean {
this.context.eventHub.emit(EVENT_BACK_PRESS)
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FlutterEngine, Log } from '@ohos/flutter_ohos';
import FkUserAgentPlugin from 'fk_user_agent';

/**
* Generated file. Do not edit.
* This file is generated by the Flutter tool based on the
* plugins that support the Ohos platform.
*/

const TAG = "GeneratedPluginRegistrant";

export class GeneratedPluginRegistrant {

static registerWith(flutterEngine: FlutterEngine) {
try {
flutterEngine.getPlugins()?.add(new FkUserAgentPlugin());
} catch (e) {
Log.e(
TAG,
"Tried to register plugins with FlutterEngine ("
+ flutterEngine
+ ") failed.");
Log.e(TAG, "Received exception while registering", e);
}
}
}
39 changes: 39 additions & 0 deletions example/ohos/entry/src/main/module.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"module": {
"name": "entry",
"type": "entry",
"description": "$string:module_desc",
"mainElement": "EntryAbility",
"deviceTypes": [
"phone"
],
"deliveryWithInstall": true,
"installationFree": false,
"pages": "$profile:main_pages",
"abilities": [
{
"name": "EntryAbility",
"srcEntry": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:icon",
"label": "$string:EntryAbility_label",
"startWindowIcon": "$media:icon",
"startWindowBackground": "$color:start_window_background",
"exported": true,
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
]
}
],
"requestPermissions": [
{"name" : "ohos.permission.INTERNET"},
]
}
}
8 changes: 8 additions & 0 deletions example/ohos/entry/src/main/resources/base/element/color.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"color": [
{
"name": "start_window_background",
"value": "#FFFFFF"
}
]
}
16 changes: 16 additions & 0 deletions example/ohos/entry/src/main/resources/base/element/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"string": [
{
"name": "module_desc",
"value": "module description"
},
{
"name": "EntryAbility_desc",
"value": "description"
},
{
"name": "EntryAbility_label",
"value": "fk_user_agent_ohos_example"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"src": [
"pages/Index"
]
}
Loading