Compare commits

...
121 Commits
Author SHA1 Message Date
2dust aa4b05503a up 2.3.14 2026-10-28 10:40:04 +08:00
Yuriy Tumanov 7179f0838f Use the default port for Hysteria2 links without a port (#6358) 2026-10-28 10:12:18 +08:00
Miheichev Aleksandr Sergeevich 0d1c57000f build(deps): bump OkHttp from 5.5.0 to 5.5.1 (#6351) 2026-10-28 10:11:05 +08:00
Figim 40b8706ccd Update subscription page string in Azerbaijani (#6349)
Edit the translation added yesterday.
2026-10-28 10:10:50 +08:00
Miheichev Aleksandr Sergeevich 0f961c54c8 fix(deps): update hev-socks5-tunnel to 2.19.3 (#6357)
* chore(deps): update hev-socks5-tunnel to 2.19.2

* fix(root): disable jumbo for LAN forwarding

* fix(root): guard IPv6 LAN TSO fallback

* chore(deps): update hev-socks5-tunnel to 2.19.3
2026-10-25 18:49:47 +08:00
2dust 1b78a510f2 Revert "chore(deps): update hev-socks5-tunnel to 2.19.1 (#6328)"
This reverts commit 552cc2425d.
2026-10-24 18:45:21 +08:00
2dust 01b4630fdd up 2.3.13 2026-10-22 10:16:56 +08:00
Miheichev Aleksandr Sergeevich 6214bc19d4 ci: update actions/setup-java to v6.1.0 (#6327) 2026-10-22 10:00:24 +08:00
rasa68andrasa68 b8d13bf52c Show subscription usage info box (traffic, expiry, support link) on subscription groups (#6333)
* Show subscription usage info box on subscription groups

* Float the info box over the server list and make it translucent

---------

Co-authored-by: rasa68 <noreply@example.com>
2026-10-22 09:59:00 +08:00
Miheichev Aleksandr Sergeevich 3165bcf4fe chore(build): update Gradle wrapper to 9.8.2 (#6343) 2026-10-22 09:48:18 +08:00
rasa68andrasa68 eedb287cc9 Redesign subscription group tabs as a floating pill bar with count badges (#6336)
* Redesign subscription group tabs as a floating pill bar with count badges

* Group tabs: dark-mode bar tone, equal-width tabs when they fit, room around selected tab

---------

Co-authored-by: rasa68 <noreply@example.com>
2026-10-22 09:48:09 +08:00
Figim 59ff87606f Fix small symbol in Azerbaijani localization (#6341)
* Fix small symbol

* Fix space
2026-10-22 09:39:52 +08:00
dependabot[bot] 700740a187 Bump actions/setup-java from 6.0.1 to 6.1.0 (#6334)
Bumps [actions/setup-java](https://github.com/actions/setup-java) from 6.0.1 to 6.1.0.
- [Release notes](https://github.com/actions/setup-java/releases)
- [Commits](https://github.com/actions/setup-java/compare/v6.0.1...v6.1.0)

---
updated-dependencies:
- dependency-name: actions/setup-java
  dependency-version: 6.1.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-10-22 09:38:56 +08:00
fanymagnetandfanymagnet bb59a71cb9 fix(subscription): refresh main list after updating subscriptions (#6329)
Updating subscriptions from the subscription screen without auto-test replaced
profile GUIDs but did not request a main list refresh. The main screen kept stale
rows, and editing one opened an empty editor because its payloads had been removed.

Co-authored-by: fanymagnet <>
2026-10-22 09:38:35 +08:00
Miheichev Aleksandr Sergeevich 552cc2425d chore(deps): update hev-socks5-tunnel to 2.19.1 (#6328)
* chore(deps): update hev-socks5-tunnel to 2.19.0

* chore(deps): update hev-socks5-tunnel to 107a508
2026-10-22 09:38:02 +08:00
2dust c8f373bd3f Update AndroidLibXrayLite 2026-10-22 09:36:56 +08:00
2dust 118cf5bc7d up 2.3.12 2026-10-18 16:37:41 +08:00
2dust 58ca4cfe23 Update AndroidLibXrayLite 2026-10-18 16:32:00 +08:00
2dust e1b73f2fa8 up 2.3.11 2026-10-11 15:00:39 +08:00
Miheichev Aleksandr Sergeevich 143ab036bf chore(build): update Gradle wrapper to 9.8.1 and Kotlin to 2.4.21 (#6301)
Gradle 9.8.1 fixes three high-severity advisories that affect Gradle
9.0.0 to 9.8.0 (GHSA-mvvg-497x-hmj8, GHSA-xwqc-3h47-hg64,
GHSA-j5m7-59rp-24f5). Kotlin 2.4.21 is a bug-fix release for 2.4.20;
the app's Kotlin compiler output is byte-identical to the 2.4.20 build.
2026-10-11 14:26:09 +08:00
2dust a258ba8bc1 Update AndroidLibXrayLite 2026-10-11 14:21:26 +08:00
Figimand2dust e1b190d816 Azerbaijani localization support (#6302)
* Azerbaijani localization support

Azerbaijani localization support

* Add Azerbaijani language support in arrays.xml

* Fix

---------

Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
2026-10-11 14:17:38 +08:00
Ray 4c9c9afdc9 fix(root): DNAT DNS queries from LAN resolvers to public IP (#6298)
In root mode, DNS (port 53) is marked into the tun and reaches the
core's SOCKS inbound as a UDP relay to the original resolver address
(e.g. 192.168.1.1:53). The core's routing rule
{inboundTag=socks, port=53, outboundTag=dns-out} should intercept it
and resolve via the configured remote DNS (e.g. Cloudflare DoH).

However, when the system DNS server is a private/LAN IP and the user
has a geoip:private → direct routing rule, that rule matches the
destination IP (192.168.1.1) and sends the DNS query straight to the
LAN resolver (ISP DNS) — bypassing dns-out entirely. This causes:
  - DNS poisoning for domains the ISP blocks/poisons
  - CDN mis-resolution (wrong POP)
  - Intermittent 'some sites work, some don't' behavior

Fix: add iptables nat OUTPUT DNAT to rewrite DNS queries destined for
private/LAN CIDRs to the configured remote DNS server (or 1.1.1.1
fallback), mirroring the existing tethered-client DNAT in
buildLanShareSetup. This prevents geoip:private from matching, so
dns-out can resolve via the proxy.

Verified on OnePlus 13R (OxygenOS 16, WildKernel v2.2.0-r4):
  - Before: youtube.com → 185.45.5.35 (fake ISP IP), timeout
  - After DNAT to 1.1.1.1: youtube.com → 142.251.45.14 (real), 301
  - tcpdump confirms response source changes from 192.168.1.1 to
    correct resolution through the proxy
2026-10-11 14:04:49 +08:00
Eliot the Cougar 6fe3893bfd Fix policy group fallback isolation (#6283)
Give least-ping and least-load groups their own first member as fallback so missing or failed probes cannot send a matched routing rule through the main profile. Keep least-load on its burst observatory and preserve random and round-robin fallback behavior. Cover group isolation and observatory selection with regression tests.
2026-10-06 15:32:19 +08:00
Miheichev Aleksandr Sergeevich 61c4e40ec1 test(root): fix flaky RootProcessRunner inherited-stdout test (#6282)
The test gave RootProcessRunner.run a 250 ms deadline. When sh takes
longer than that to start and finish, the runner hits the deadline,
destroys it and returns -1, and the test fails with
"expected: <0> but was: <-1>". With the sh from Git for Windows this
happened in 6 of 30 runs.

The test's real guard is the 1500 ms elapsed bound against the
background "sleep 5". A 3000 ms deadline leaves that bound alone to
decide the result, so a runner that keeps a finished command open until
its deadline now fails the test as well; with 250 ms it passed.
2026-10-06 15:29:09 +08:00
2dust 4a1510eb65 Refresh switch widget styling and icons 2026-10-06 15:23:04 +08:00
2dust 44a56f56bc Revert "Feat: Azerbaijani localization support and language selection (#6286)"
This reverts commit 3687436792.
2026-10-06 14:56:50 +08:00
Figim 3687436792 Feat: Azerbaijani localization support and language selection (#6286)
* Azerbaijani localization

Azerbaijani language support added

* Delete V2rayNG/app/src/main/res/values-az

* Azerbaijani localization support

Azerbaijani translations are now available

* Fix

* Add Azerbaijani language selection

* Update Azerbaijani strings in strings.xml

* Fix string formatting in Azerbaijani resource file
2026-10-06 14:48:39 +08:00
cin ffc9614417 feat(wireguard): add features from the new core version (#6290)
* feat(wireguard): add features from the new core version

- The `local` value is no longer allowed for `remoteDNS`. If this value is passed in the profile, the core will panic while calling `netip.MustParseAddr`.

- Add the `targetStrategy` field to `OutboundBean`.

- The `domainStrategy` field has been removed from `OutSettingsBean`. Any value passed for `domainStrategy` is now ignored.

* update locales

- ru,en - handwritten.
- other - AI-generated.
2026-10-06 14:43:02 +08:00
JaroWorldand2dust fcacb9cfb0 Keep fractional numbers when saving JSON array subscriptions (#6293)
* Keep fractional numbers when saving JSON array subscriptions

toJsonPretty wrote every Double through toInt(), so 0.4 became 0 and
values above Int.MAX_VALUE were clamped. Write whole numbers as Long
and leave other values as Double.

Fixes #6291

* Add a JsonUtil test for numbers in config arrays

* Update JsonUtil.kt

---------

Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
2026-10-06 14:41:29 +08:00
2dust a4dd3d08de up 2.3.10 2026-10-01 17:00:28 +08:00
2dust ab09f816a0 Update libs.versions.toml 2026-10-01 16:17:33 +08:00
2dust 504f22d75b Fix server row text overlap in main pager 2026-10-01 16:13:46 +08:00
2dust 996b039b48 Update AndroidLibXrayLite 2026-10-01 15:58:47 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5.5 e151890c55 ci: update Android NDK to r30 (30.0.16248370) (#6275)
r30 is the current NDK LTS; r29 is no longer supported. The NDK path
is part of the libhevtun cache key, so the cached libraries are rebuilt
with the new toolchain.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:27:02 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5.5 04d407f5a1 chore(deps): update hev-socks5-tunnel to 2.18.0 (#6272)
64cc609 (2.16.0+24) -> d9dca26 (tag 2.18.0), 16 commits. Changes on
the paths the app uses:

- HevSocks5SessionUDP: skip datagrams without an address or with an
  empty payload, and set the IPv4 address type for a fixed UDP peer
  (c02e3fd)
- lwIP 2a11c14 -> e22c9d2: fix PRETEND handling of IPv6 extension
  headers and fragments, validate UDP PRETEND packets before accepting
  new sessions
- HevSocks5Tunnel: assert on the stop event write result (c362232)

The rest is tunnel code for Windows, macOS, FreeBSD and NetBSD, an
optional Windows-only tunnel.guid key, mapped DNS, the upstream AAR
build, docs and version bumps. JNI entry points, Android.mk/build.mk
and the config keys the app writes are unchanged.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:25:50 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5.5 5029d75475 test: migrate unit tests from JUnit 4 to JUnit 6 (#6276)
JUnit 4 is in maintenance mode, and 4.13.2 (February 2021) is its last
release. Move the unit tests to JUnit Jupiter 6.1.3: the JUnit BOM,
junit-jupiter and the Platform launcher replace junit:junit, and the
unit test tasks run on the JUnit Platform. The test sources change only
mechanically: imports, lifecycle annotations, TemporaryFolder to
@TempDir, and the message argument moved last in three assertions.

Jupiter skips an invalid @Test method, such as one that returns a
value, with only a warning, where JUnit 4 failed the run. Treat
discovery warnings as failures to keep that behavior.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:20:41 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5.5 85b1f694c9 chore(build): update Gradle wrapper to 9.8.0 (#6274)
AGP 9.4.1 is in the AGP range Gradle 9.8.0 was tested with (9.0 to
9.5.0-alpha02). AGP 9.4 requires at least Gradle 9.6.0.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:19:26 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5.5 c63e18b751 chore(deps): update mockito-kotlin to 6.4.0, drop mockito-inline (#6273)
mockito-kotlin shared the mockitoMockitoInline version key with
mockito-inline, which was discontinued after 5.2.0 because Mockito 5
made the inline mock maker the default in mockito-core. The shared key
kept mockito-kotlin on 5.2.0.

- org.mockito:mockito-inline 5.2.0 -> org.mockito:mockito-core 5.24.0
- org.mockito.kotlin:mockito-kotlin 5.2.0 -> 6.4.0, on its own key

On the test classpath mockito-core moves from 5.7.0 to 5.24.0 and
Byte Buddy from 1.14.9 to 1.17.7, so the unit tests also run on JDK 25.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 15:17:16 +08:00
Eliot the Cougar 89f53233cb Fix navigation bar theme API lint errors (#6259) 2026-10-01 15:08:56 +08:00
Hossein Abaspanahandeliotcougar be1f2cf00b Update Luri Bakhtiari translation (#6169)
* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Update strings.xml

* Fixed reported issue #1

* Reported issue 3, section 2, fixed

* Update V2rayNG/app/src/main/res/values-bqi-rIR/strings.xml

Co-authored-by: eliotcougar <eliotfur@gmail.com>

---------

Co-authored-by: eliotcougar <eliotfur@gmail.com>
2026-10-01 15:04:14 +08:00
2dust bd21bbc3f9 up 2.3.9 2026-09-22 14:49:01 +08:00
2dust 6e5d742e40 Fix
https://github.com/2dust/v2rayNG/issues/6240
2026-09-22 14:48:05 +08:00
2dust 4472162614 Preserve groups when replacing profiles 2026-09-22 14:30:22 +08:00
2dust 1f7cef32dc Animate start FAB icon spin on tap 2026-09-22 11:11:59 +08:00
2dust 7e68e2eaad Adjust toast background opacity
Increase toast background opacity from 70% to 85% for all theme variants in Theme.kt. This improves readability and keeps toast notifications visually consistent without changing their content or behavior.
2026-09-22 10:42:01 +08:00
2dust 4c7c964e65 Show service start failure details
Pass the optional failure message from the service layer through `MainServiceEvent` and show it in `MainViewModel` when available. This preserves the existing generic error toast as a fallback while surfacing more specific startup failures to the user.
2026-09-22 10:41:31 +08:00
2dust 1bd1bceadb Delete ServerActivity.kt 2026-09-21 19:13:13 +08:00
2dust 0e80f6b431 Add snackbar-safe toast dispatch
Refactor toast helpers to route through a shared dispatcher that first tries the app snackbar manager and falls back to Android Toasts on the main thread. This adds a `long` duration option across normal/success/error/info toast APIs and avoids posting to a new Handler for every message by reusing a single main-thread handler.
2026-09-21 19:10:33 +08:00
2dust 1ad5f30ede Show inline validation errors in forms
Move several save-time validation failures from toast messages to field error states in the server and subscription editors. Password validation now marks the relevant server form field, and subscription editing highlights invalid URLs and update intervals before save instead of relying on transient toasts.
2026-09-21 19:10:10 +08:00
2dust e4c0b09e1b Show inline errors in Compose forms
Replace toast-only required-field validation with Compose text field error states across routing, server, subscription, and user asset editors. The shared form field now supports `isError` and supporting text so save actions can block invalid input and highlight missing remarks, address, port, and URL values directly in the form.
2026-09-21 17:00:54 +08:00
Eliot the Cougar 073376e46d Localize notification-channel names (#6195)
Replace hard-coded channel labels with dedicated resource names for the main service, subscription updates, and connection checks in all nine supported locales. Resolve names through AppLocaleManager for service and application contexts.

Reuse the existing channel when refreshing its localized name, preserving channel IDs and user-controlled importance, sound, vibration, and visibility. Apply initial defaults only to newly created channels and remove the obsolete hard-coded service-channel name.

Extract channel localization from local/talkback-all-10 without adding its transient-message channel, snackbar fallback, or accessibility announcement infrastructure.
2026-09-21 14:55:32 +08:00
Miheichev Aleksandr SergeevichandClaude Opus 5 08c2a4deb1 refactor(ui): migrate Coil 2 to Coil 3 (#6248)
Coil 2 had its last release in July 2024 (2.7.0); Coil 3 is the
maintained line and uses new coordinates, io.coil-kt.coil3, with the
package prefix coil3.

AppIconFetcher now returns ImageFetchResult with drawable.asImage(),
since Coil 3 models images through its own Image type. Fetcher,
Fetcher.Factory, Options, DataSource and the ImageRequest builder chain
keep the same shapes, so the rest is imports.

No network artifact is required: AppListItem is the only AsyncImage call
site and both callers pass icon = null, so every model is the local
appicon: request served by AppIconFetcher.

Coil 3.6.3 declares minSdkVersion 23 and ships no native code, so
neither minSdk 24 nor the four app ABIs are affected.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 14:49:06 +08:00
wttommy-ctrl 60ab1af384 fix(root): enforce command deadlines while draining output (#6241)
* fix(root): bound command execution and output collection

* test(root): cover command deadlines and output limits
2026-09-21 14:47:18 +08:00
cinand2dust c48fbd3fc8 feat(wireguard): add remoteDNS field to server config UI (#6230)
Expose the `remoteDNS` option (added in Xray-core 26.9.9) in the wireguard.

- Accepts a comma-separated list of IPv4/IPv6 addresses
- Accepts "local" as a single value to resolve via the device's
  local DNS instead of routing lookups through the tunnel
- Falls back to WIREGUARD_LOCAL_REMOTE_DNS when the field is empty
  (Xray-core upstream value)

Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
2026-09-21 14:42:56 +08:00
Eliot the Cougar b98895219a Fix WebView Browser Dialer recovery after network handovers (#6229)
* Confine WebView dialer lifecycle to its main looper

Preserve synchronous start/stop completion and failure propagation for worker-thread network reloads. Supersede queued lifecycle requests and prevent callbacks from reviving destroyed views. Add JVM and real-WebView regression coverage for issue #6228.

* Keep WebView device tests focused on framework thread ownership

* Remove WebView dialer instrumentation tests
2026-09-21 14:32:15 +08:00
solokot 0f65b1d348 Update Russian translation [reviewed] (#6210)
* Update Russian translation

Some improvements

* fix typo

* Update Russian translation [reviewed]
2026-09-21 11:50:22 +08:00
Eliot the Cougar a1cfa2aa7a UI/UX: Add item names to deletion confirmations (#6202)
* Name the targets in deletion confirmations

Extract the visible named-confirmation wording from the accessibility assembly onto current upstream. Cover main-list profiles, standard and custom profile editors, policy groups, proxy-chain members, subscriptions, routing-rule editors, and asset sources. Reuse the six localized named variants in all nine catalogs; preserve existing generic strings and the already-named asset-file confirmation.

Carry display names alongside existing GUIDs in main/subscription list confirmation requests, and retain the main target across recreation with its two-string saver. Use saved names in editors rather than uncommitted renames. Do not import accessibility semantics, action menus, live regions, the separate routing-list Delete button, or the proxy-chain stable-key migration.

Validation: 72 JVM tests passed, including two deletion-target saver regressions; Play Store debug compilation and x86_64 assembly passed. An API 33 emulator verified all 54 compiled localized resources with Unicode and percent-containing names, the main profile confirmation across rotation, and subscription confirmation/cancel. No deletion was confirmed. Other editors, double-column menus, keyboard/D-pad, and TalkBack were not exercised. Original APK and rotation settings were restored.

* Standardize named delete confirmation layout

Center delete confirmation messages while retaining a single Text node for the question and target. Separate each item name with a blank line in all nine locales, including asset files, which previously embedded the filename in the question.

Keep the title-free dialog and existing Cancel/Delete actions, and leave ordinary confirmation messages start-aligned. Bulk-delete prompts retain their collective wording.

Validation: 72 JVM tests, Kotlin compilation and Play Store debug assembly passed. All 63 compiled localized templates preserved an exact Unicode/percent target. Emulator checks covered profile and asset message nodes, two action targets, cancellation and profile dialog recreation after rotation. Actual spoken TalkBack output and the full theme/input matrix were not verified.

* Resolve named proxy-chain confirmations by stable member key

Upstream #6200 replaced pending member indices with stable keys. The textual rebase of #6202 retained an out-of-scope index reference in the confirmation text, breaking compilation.

Look up the current member name through memberKeys, using the same pending identity as deletion. Reordering no longer separates the displayed target from the member removed on confirmation.

* Reuse deletion questions instead of duplicating named translations

Let DeleteConfirmDialog append an optional item name after a blank line while keeping the complete message in one centered Text. Reuse the existing localized questions at all named deletion sites and remove six duplicate resource keys from all nine locales. Asset-file questions use the same shared layout. Unnamed and bulk prompts remain unchanged.

Retain stable-key proxy-chain target lookup and the corrected Russian policy-group wording in the reused question. Validation: existing ServerDeleteTargetTest, all 79 JVM tests, x86_64 Play Store debug assembly and a Pixel 9 Pro emulator check passed. The profile dialog exposes one question/name text node plus Cancel/Delete, and canceling preserves the lab profiles. TalkBack was enabled for the smoke check; actual speech and the full device/theme/locale matrix were not independently verified.
2026-09-21 11:49:32 +08:00
dependabot[bot] 53fce9a8f5 Bump actions/setup-java from 6.0.0 to 6.0.1 (#6232)
Bumps [actions/setup-java](https://github.com/actions/setup-java) from 6.0.0 to 6.0.1.
- [Release notes](https://github.com/actions/setup-java/releases)
- [Commits](https://github.com/actions/setup-java/compare/v6.0.0...v6.0.1)

---
updated-dependencies:
- dependency-name: actions/setup-java
  dependency-version: 6.0.1
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-09-19 16:01:23 +08:00
2dust cb39a10ada Upgrade Gradle, AGP, and core dependencies 2026-09-19 15:56:26 +08:00
Mohammad c1c93b62d1 Add finalMask parsing to Wireguard configuration handling (#6242) 2026-09-19 15:02:56 +08:00
eliotcougar a9b1242686 Coalesce bulk probe result updates (#6205) 2026-09-13 09:51:57 +08:00
eliotcougar 173f60a1da Keep connection-test requests tied to their lifecycle (#6203)
Extract request ownership and cancellation from the accessibility work without changing result wording or presentation. Correlate current and bulk replies with the request that produced them so older progress, cancellation, and completion cannot finish a replacement test.

Own current probes in the core manager and cancel them on replacement, stop, or reload. Use the existing ordered service acknowledgement to retire requests when the daemon is absent. Cancel pending bulk preparation when the UI cancels or its ViewModel is cleared, and snapshot the filtered target list before asynchronous preparation.

Tag bulk cancellation per worker, including service destruction, instead of reporting cancellation as successful completion or issuing an untagged reply. Report rejected bulk-service starts through the same cancellation path. Preserve the existing service modes, IPC payload classes, and group post-test policy.

Validation: 74 JVM tests, Kotlin compilation and Play Store debug assembly passed. External emulator probes exercised current and bulk replacement, stale replies, cancellation during preparation, daemon-absent acknowledgement, and real notification-command cancellation over 15 Lab profiles. VPN smoke testing returned a current result with IP information and preserved disconnected status after stop. Root/proxy-only and network-handover runtime checks were not run.
2026-09-11 19:46:40 +08:00
DHR60 b9c2decb34 Delete redundant fields (#6216) 2026-09-11 19:32:23 +08:00
2dust 200c59f773 up 2.3.8 2026-09-10 14:23:35 +08:00
eliotcougar 49835a5cdb Use completed-action wording and plurals for update results (#6196)
Extract the config and asset update-result resources from local/talkback-all-10 in all nine supported locales. Describe completed updates instead of an update command, and select singular or plural forms using the actual config or successful asset count.

Route both subscription update entry points through the existing localized ViewModel context and use activity resources for asset results. Preserve update execution, counts, partial-failure summaries, error handling, and toast delivery; no accessibility announcement infrastructure is included.
2026-09-10 13:56:49 +08:00
2dust 53c6506308 Update AndroidLibXrayLite 2026-09-10 12:19:02 +08:00
eliotcougar 01b47581c0 Keep proxy-chain deletion targets tied to stable row keys (#6200)
Store the pending member key instead of its list position and resolve that key when deletion is confirmed. Remove the member and its corresponding row key together; if the target no longer exists, leave the draft unchanged.

Extract the deletion fix from the accessibility assembly without importing accessibility actions, dialog wording changes, or drag refactors. Reuse the same key-based removal for blank members.

Validation: 75 JVM tests passed, including five deletion regressions; Play Store debug Kotlin compilation and x86_64 assembly passed. Emulator smoke check covered confirmation across rotation and immediate blank-member removal. Keyboard/D-pad and runtime reorder-during-confirmation scenarios were not run.
2026-09-10 12:16:17 +08:00
eliotcougar 362e541cb6 Keep built-in asset row identities stable across reloads (#6199)
Use a builtin: filename-based ID instead of generating a fresh UUID each time the asset list is rebuilt. This lets the existing GUID-keyed list and file-metadata map recognize the same built-in asset after reloads and download-source changes.

Extract the fix and its two JVM regression tests from local/talkback-all-10. Preserve stored asset GUIDs, matching built-in replacement, locked flags, and URL behavior. No persisted IDs, accessibility semantics, metadata formatting, or editor behavior are changed.
2026-09-10 11:50:59 +08:00
eliotcougar 157acc8daa Format asset metadata with localized file sizes and minute precision (#6197)
Extract the visible asset metadata formatting from local/talkback-all-10. Use Android file-size formatting and derive the date/time pattern from the app configuration locale and the device 12/24-hour preference, without seconds.

Remove the metadata-only remember cache so recomposition uses the current formatting context. Preserve the existing size/date separator, missing-file message, row layout, action buttons, and accessibility semantics. No update-result or asset-editor changes are included.
2026-09-10 11:49:29 +08:00
eliotcougar 5d76fcd0fa Generate subscription QR codes off the UI thread (#6194)
Move subscription QR bitmap generation from the synchronous share callback into the existing ViewModel, running CPU-bound encoding on Dispatchers.Default and exposing the completed bitmap through StateFlow.

Cancel pending result delivery when the dialog is dismissed, a newer share request replaces it, or the ViewModel is cleared. Keep dialog rendering lifecycle-aware and reuse the existing localized failure message when generation returns no bitmap.

Extract only QR generation and dialog-state wiring from local/talkback-all-10. Leave subscription row accessibility, custom actions, update handling, and clipboard sharing unchanged.
2026-09-10 11:35:59 +08:00
eliotcougar 4004cee279 UI/UX: Preserve asset-editor drafts during recompose (#6193)
* Preserve asset-editor drafts and handle the keyboard

Keep the asset name, URL, and delete-confirmation visibility in saveable Compose state so activity recreation does not discard an unfinished edit. Key the state by asset identity and initial field values.

Make the form vertically scrollable, consume the Scaffold padding, and apply IME padding so its fields remain reachable when the keyboard reduces the available height. Extract these presentation changes from local/talkback-all-10 without changing accessibility labels, deletion wording, or asset-save validation order.

* Resize the asset editor when the keyboard opens

Declare stateUnchanged|adjustResize for UserAssetUrlActivity, matching the other editor activities. Without an explicit resize policy, opening the IME in landscape pans the entire window and moves the toolbar Save action off-screen despite the scrollable Compose form.

Verified on Android 13 Pixel 6a: rotate with an unsaved draft, reopen the landscape keyboard, scroll between fields, edit the first field, save without hiding the keyboard, and reopen persisted values. Portrait keyboard and Save also pass. Debug assembly succeeded.
2026-09-10 11:34:20 +08:00
eliotcougar 06bcd4c16b Show per-app proxy help in a dialog (#6192)
Replace the short-lived info toast with the existing titleless confirmation dialog and a single OK button so users can read the routing explanation at their own pace. Keep dialog visibility in saveable screen state and reuse the existing localized help and action strings.

Extract only the help-dialog presentation from local/talkback-all-10; leave app-row semantics, switches, routing descriptions, and notification infrastructure in the accessibility branches.
2026-09-10 11:33:58 +08:00
eliotcougar 8d61aedeca fix: validate asset edits before deleting the cached file (#6190)
Reject duplicate names and empty names or URLs before invalidating the existing asset cache. Previously a rejected save left the stored source unchanged but deleted its downloaded file.

Preserve the validation messages and their order, duplicate-name exclusion by asset ID, and successful-save behavior. Renames still invalidate the old filename before updating the asset record.
2026-09-10 11:33:22 +08:00
2dust 2020807c25 up 2.3.7 2026-09-05 15:52:22 +08:00
2dust ca4185abad Rename daemon process and split task services
Standardizes manifest process names by replacing the long `:RunSoLibV2RayDaemon` suffix with `:daemon` for shortcut activities, VPN/proxy services, and related receivers. It also moves `CoreTestService` and `SubscriptionUpdateService` to a separate `:tasks` process to isolate task-oriented work from the main daemon process.
2026-09-05 15:47:34 +08:00
eliotcougar 8d9152bb4e Align default settings hints with actual app behavior (#6183) 2026-09-05 15:06:19 +08:00
eliotcougar 5bfcdf90ec Clarify boot auto-connect and link system VPN settings (#6179)
* Clarify device-boot connection and link system VPN settings

* Match Bakhtiari system spelling in VPN settings strings
2026-09-05 14:59:06 +08:00
eliotcougar f4f992eb9b Deduplicate subscription IDs when reading the saved index (#6172)
A saved SUB_IDS array can contain the same GUID more than once. Loading
it verbatim creates repeated subscription rows with identical Compose
and reorderable-list keys, which can crash the subscription screen.

Deduplicate IDs in the shared reader while preserving the first-seen
order and mutable-list contract. This protects every index consumer
without changing subscription records, generating replacement IDs, or
adding writes during reads. Different subscriptions with identical or
blank names remain separate.

Add six regression tests for duplicate rows, stable ordering, read-only
decoding, mutable round trips, repeated names, and existing empty/invalid
input behavior. Three assertions fail on unchanged upstream; all six
pass with the fix. The full 63-test suite and Play Store debug build pass.

On the API 37.1 Pixel 9 Pro emulator, a deliberately duplicated saved
index loaded each subscription once, retained first-occurrence order,
reopened safely, and edited the correct record. The raw index remained
duplicated throughout the check; no app crash was observed.
2026-09-05 14:54:46 +08:00
2dust 1af8f4677c Revert "Use ruleset IDs for routing edits"
This reverts commit c548e5f615.
2026-09-05 14:50:28 +08:00
eliotcougar 4d8ee1a916 Fix Logcat crashes on duplicate log lines (#6170)
Logcat used the complete log text as a Compose lazy-list key after 258de2097. Identical timestamped messages and repeated stack frames are valid log output, but collide as keys and crash the main thread while browsing logs.

Give each occurrence its own key in a small ViewModel-owned presentation model. Count oldest-first so prepending newer logs and filtering unrelated lines retain existing occurrence keys. Build the projection off the main thread and keep raw text, duplicates, ordering, copy and share behavior unchanged.

Add six regression tests covering duplicate messages, repeated frames, filtering, refresh, empty input and key-like text. All 63 Play Store unit tests pass; the debug APK was checked on the API 37.1 emulator with duplicate-line scrolling, filtering, refresh, reopening and lossless export. The original failure is documented by the user-provided physical-device crash log.
2026-09-05 14:46:10 +08:00
eliotcougar 95bcf32c0b Fix Logcat refresh button navigation inset (#6155) 2026-09-05 14:26:39 +08:00
2dust c548e5f615 Use ruleset IDs for routing edits
Switch routing ruleset lookup, save, and delete flows from list index to stable ruleset ID. Routing edit intents now pass `ruleset_id`, editing reloads by ID, and list item edit actions use each rule’s ID. This avoids index mismatch bugs when list order changes and keeps update/delete targeting the correct ruleset.
2026-08-30 19:38:43 +08:00
2dust 258de20972 Use stable list keys in Logcat and Tasker 2026-08-30 19:29:03 +08:00
2dust 3dc0173b9f Fix
https://github.com/2dust/v2rayNG/issues/6153
2026-08-30 17:44:53 +08:00
2dust 13138dddca up 2.3.6 2026-08-29 18:11:06 +08:00
2dust d5fc104e1d Fix
https://github.com/2dust/v2rayNG/issues/6140
2026-08-29 18:10:03 +08:00
2dust 384553e74e Code clean 2026-08-29 18:09:50 +08:00
eliotcougar 11b5784321 Keep HEV credentials out of root scripts (#6138) 2026-08-29 16:06:28 +08:00
eliotcougar e27941558e fix: prevent ZIP path traversal during backup restore (#6134)
* fix: harden ZIP backup extraction

* fix: tune backup ZIP limits

* refactor: simplify ZIP extraction validation
2026-08-29 16:03:57 +08:00
eliotcougar c73498b064 Docs/agent guidelines (#6129)
* docs: refresh agent guidelines

* docs: add agent harness adapters

* Update local-only block markers in AGENTS.md

DO NOT COMMIT phrase was confusing most LLMs and preventing them from committing anything without an additional explicit permission.
2026-08-29 15:59:49 +08:00
eliotcougar e2dc37ba26 Prepare server rows outside composition (#6107)
Build presentation-ready server row models in the ViewModel and publish each group's raw data and row data as one atomic state.

This removes storage and description work from composition, reduces the server-row API, and keeps reordering consistent across both representations.
2026-08-29 15:58:33 +08:00
eliotcougar 26099c198e Improve Compose group paging (#6105) 2026-08-29 15:53:37 +08:00
2dust 3c9336f671 Bump Android Gradle Plugin to 9.3.2 2026-08-29 15:49:36 +08:00
patterniha 767f88667c Add happyEyeballs to the freedom outbound (#6148) 2026-08-29 11:08:24 +08:00
patterniha e3fa861571 Do not remove policy levels when traffic statistics is disabled (#6146)
Disabling traffic statistics removed the whole policy section from the
generated config, including the levels block (uplinkOnly, downlinkOnly,
connIdle, handshake) from the template or from a user's custom config.
Only the stats-related policy.system block should be stripped; keep
policy.levels intact in both the unified and the custom config paths.
2026-08-29 11:07:00 +08:00
dependabot[bot] fdacd58ed0 Bump actions/setup-java from 5.7.0 to 6.0.0 (#6145)
Bumps [actions/setup-java](https://github.com/actions/setup-java) from 5.7.0 to 6.0.0.
- [Release notes](https://github.com/actions/setup-java/releases)
- [Commits](https://github.com/actions/setup-java/compare/v5.7.0...v6.0.0)

---
updated-dependencies:
- dependency-name: actions/setup-java
  dependency-version: 6.0.0
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2026-08-29 11:05:30 +08:00
DHR60 38da97e031 Fix (#6150) 2026-08-29 11:05:07 +08:00
eliotcougar a1b45bbfb2 Use stable identifiers to locate servers (#6103) 2026-08-23 19:33:27 +08:00
eliotcougar dac65a972c Fix IP and CIDR validation (#6124) 2026-08-23 15:56:32 +08:00
Tamim Hossain 63f557242b closes #6070 (#6114)
cc @2dust
2026-08-21 10:21:03 +08:00
2dust 9fcb1a30f8 up 2.3.5 2026-08-20 17:09:55 +08:00
2dust d4fc91502c Update AndroidLibXrayLite 2026-08-20 15:21:33 +08:00
2dust 75c9194ec0 Log CoreCallback events and noop shutdown 2026-08-20 15:16:47 +08:00
aliRahimi1997 590be2e9ae fix(accessibility): announce selected server state (#6108) 2026-08-19 20:28:38 +08:00
aliRahimi1997 1ee1213563 fix(accessibility): label more options buttons (#6095) 2026-08-19 20:14:59 +08:00
eliotcougar 37b42f04a7 Load user asset metadata off the UI thread (#6106)
Load asset metadata on Dispatchers.IO and publish the asset list and file details as one lifecycle-aware UI state.

Remove refresh-key workarounds, await reloads before downloads, and keep the content composable stateless.
2026-08-19 14:16:41 +08:00
eliotcougar 3c91b51489 Optimize large-group probing startup (#6102)
Clear visible delays immediately, but move the per-profile MMKV reset to the existing I/O coroutine. Finish the reset before starting the test service so newly reported results cannot be erased.
2026-08-19 13:57:06 +08:00
eliotcougar 06a27ec4d4 Allow TCPing mixed H3/H2 profiles (#6101) 2026-08-19 13:55:26 +08:00
eliotcougar 7a07565f72 Harden profile replacement ordering (#6093) 2026-08-17 10:20:43 +08:00
eliotcougar 0456f835bc Add manual profile storage cleanup (#6094) 2026-08-17 10:06:56 +08:00
aliRahimi1997 e4e3a36e93 fix(accessibility): label server action buttons (#6091) 2026-08-16 19:44:24 +08:00
2dust 4b3c8177db Add Duration-based delay extension helpers 2026-08-16 15:32:35 +08:00
eliotcougarand2dust 780d194213 Make explicit service restart daemon-authoritative (#6072)
* Make service restarts daemon-authoritative

* Update CoreServiceManager.kt

---------

Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
2026-08-16 15:26:58 +08:00
eliotcougar c1374a73fb fix(ui): use asset-specific update toast (#6088) 2026-08-16 10:55:31 +08:00
eliotcougar 49fe45c49e Recover MMKV data on storage errors (#6085) 2026-08-16 10:30:56 +08:00
eliotcougar 36ab5615d9 Fix custom real-delay probe crash (#6083) 2026-08-16 10:21:17 +08:00
2dust 91c2c93a62 Adjust string 2026-08-16 10:14:32 +08:00
aliRahimi1997 10932f5764 fix(accessibility): announce server test status changes (#6082) 2026-08-15 19:53:38 +08:00
Xiaochang 8dcff4238f Fix bottom bar background v2 (#6075)
* fix: extend bottom bar background and content to screen edge for edge-to-edge

* Extract NavigationBarsSpacer and navigationBarsBottomPadding to reduce duplicate code

* Rename navigationBarsBottomPadding to NavigationBarsBottomPadding for naming consistency
2026-08-15 10:39:14 +08:00
eliotcougar 809bdcdb9d Use action labels for service shortcuts (#6074) 2026-08-15 10:32:03 +08:00
172 changed files with 6850 additions and 2472 deletions

No files matched your search

+6
View File
@@ -0,0 +1,6 @@
# Agent-guide adapter
Before starting any repository task, open and follow the repository-root `AGENTS.md`.
That guide defines the scoped `AGENTS.md` files that must also be opened before editing
their paths. Treat the root and applicable scoped guides as mandatory. Do not duplicate
or replace their rules in this file.
+4 -4
View File
@@ -35,11 +35,11 @@ jobs:
run: |
echo "y" | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager \
--channel=0 \
--install "ndk;29.0.14206865"
echo "NDK_HOME=$ANDROID_HOME/ndk/29.0.14206865" >> $GITHUB_ENV
--install "ndk;30.0.16248370"
echo "NDK_HOME=$ANDROID_HOME/ndk/30.0.16248370" >> $GITHUB_ENV
sed -i '10i\
\
ndkVersion = "29.0.14206865"' ${{ github.workspace }}/V2rayNG/app/build.gradle.kts
ndkVersion = "30.0.16248370"' ${{ github.workspace }}/V2rayNG/app/build.gradle.kts
- name: Restore cached libhevtun
id: cache-libhevtun-restore
@@ -81,7 +81,7 @@ jobs:
out-file-path: V2rayNG/app/libs/
- name: Setup Java
uses: actions/setup-java@v5.7.0
uses: actions/setup-java@v6.1.0
with:
distribution: 'temurin'
java-version: '21'
+3
View File
@@ -21,6 +21,9 @@ V2rayNG/app/build
V2rayNG/build
V2rayNG/local.properties
# Ignore prebuilt native libraries (built from submodules by compile-hevtun.sh / AndroidLibXrayLite)
V2rayNG/app/libs
# Ignore APK and AAR files
*.apk
*.aar
+188
View File
@@ -0,0 +1,188 @@
# v2rayNG agent guide
## Scope and precedence
This file applies to the entire repository. Read the applicable scoped guide before editing any
listed path, including when the agent starts at the repository root:
- Read `V2rayNG/app/src/main/java/com/v2ray/ang/service/AGENTS.md` for
`V2rayNG/app/src/main/java/com/v2ray/ang/service/`,
`V2rayNG/app/src/test/java/com/v2ray/ang/service/`, and these paths under
`V2rayNG/app/src/main/java/com/v2ray/ang/`: `core/CoreServiceManager.kt`,
`core/LauncherManager.kt`, `root/`, `contracts/ServiceControl.kt`,
`contracts/Tun2SocksControl.kt`, `handler/NotificationManager.kt`,
`helper/MessageHelper.kt`, and `helper/NotificationHelper.kt`.
- Read `V2rayNG/app/src/main/java/com/v2ray/ang/ui/AGENTS.md` for
`V2rayNG/app/src/main/java/com/v2ray/ang/ui/` and
`V2rayNG/app/src/test/java/com/v2ray/ang/ui/`.
Follow both guides in scoped paths. A scoped rule overrides a root rule only when they require
incompatible actions; every other root rule remains active.
## Requirement language
- `must`, `must not`, `only`, and `never` are mandatory. Only the current user request or linked
acceptance criteria can state an exception; agent inference cannot.
- `Task scope` is the named files and behavior plus dependency, test, or build edits that a
compiler or required validation proves necessary. Every other change is outside scope.
- `Changed behavior` includes direct and downstream build-time or runtime effects through callers,
consumers, storage, IPC, and resources.
- `Supported` versions, ABIs, distributions, and locales are exactly those declared by
`V2rayNG/app/build.gradle.kts` and `.github/workflows/build.yml` inputs.
- A behavior is `verified` only when its named check passed on branch HEAD. Otherwise list the
exact unrun check and blocker, and do not call the behavior verified.
## Existing code is not a compliance baseline
- These guides define the target state; they do not claim that this checkout, an older commit, or
another worktree complies. Existing code shows current behavior and is not permission to repeat
a pattern.
- A `touched declaration` is any function, property, class, composable, resource entry, manifest
element, or Gradle block containing an added or removed line. Inspect it and every caller or
consumer whose contract changes against all applicable rules before editing.
- Every new declaration and changed behavior must comply. Inspect a helper or pattern before
reusing it. Tests prove only their assertions; names, green builds, and prior approval prove
nothing else.
- Do not repair unrelated violations. Fix a violation left on a task-changed execution path in the
same branch; if that would change behavior outside scope, request approval first. Report any
remaining violation that blocks required validation.
## Agent-guide synchronization and PR boundary
- `AGENTS.md` files are the only rule source. `CLAUDE.md`, `GEMINI.md`, and
`.github/copilot-instructions.md` are routing shims and must only load or point to applicable
`AGENTS.md` files.
- Feature, fix, and release branches use instruction files from fetched `upstream/master` as their
baseline. If `upstream` is absent, run
`git remote add upstream https://github.com/2dust/v2rayNG.git`; otherwise require
`git remote get-url upstream` to print exactly that URL. Successfully run
`git fetch upstream master` before comparison. An agent-instruction branch uses its HEAD and
working diff as the proposal.
- At task start and after each upstream merge or rebase, synchronize canonical instructions into
the worktree without deleting local-only text. Compare them with:
```sh
git diff upstream/master -- ':(glob)**/AGENTS.md' ':(glob)**/CLAUDE.md' ':(glob)**/GEMINI.md' .github/copilot-instructions.md
```
After synchronization, every displayed difference must be inside a local-only block. If a branch
predates the files, keep copied versions untracked or unstaged; acquire tracked copies only by
merging or rebasing their upstream commit.
- User-, machine-, worktree-, and temporary text is local-only. Put it in `AGENTS.md` between
visible lines `LOCAL INSTRUCTIONS - START` and `LOCAL INSTRUCTIONS - END`.
Unless the task targets agent instructions, treat every pre-existing instruction-file difference
as local-only.
- Keep local-only text unstaged and uncommitted. Never use `git add .`, `git add -A`, or
`git commit -a` in that worktree. Stage task paths explicitly; never delete, restore, or overwrite
local instructions to clean the worktree.
- Before each commit, push, or PR update outside agent-instruction scope, run both commands. Both
must print no paths; otherwise stop and remove the instruction change from the staged or branch
diff without altering its working copy.
```sh
git diff --cached --name-only -- ':(glob)**/AGENTS.md' ':(glob)**/CLAUDE.md' ':(glob)**/GEMINI.md' .github/copilot-instructions.md
git diff --name-only upstream/master...HEAD -- ':(glob)**/AGENTS.md' ':(glob)**/CLAUDE.md' ':(glob)**/GEMINI.md' .github/copilot-instructions.md
```
- Commit instruction files only for a task explicitly limited to agent instructions. The branch
and PR may contain only `AGENTS.md` and necessary routing-shim changes; name agent instructions
in the PR title and state that exclusive scope in the body.
## Project boundaries
- The Android project is under `V2rayNG/` and uses Kotlin, Gradle Kotlin DSL, Compose, Material 3,
coroutines, and AndroidX lifecycle. Read dependency versions from
`V2rayNG/gradle/libs.versions.toml`, SDK levels from `V2rayNG/app/build.gradle.kts`, and CI tools
from `.github/workflows/build.yml`; never copy those numbers into a guide.
- `AngApplication` initializes MMKV, app locales, WorkManager, defaults, and theme state. Persist
application data through `MmkvManager` or `SettingsManager`; do not create `SharedPreferences`
or another path for data they own.
- `core/` owns native configuration and lifecycle, `handler/` data and application operations,
`service/` Android services, and `ui/` Compose activities, components, and ViewModels. Put new
code in its owner unless a scoped guide names a narrower owner.
- Shared lifecycle and configuration code must retain VPN, proxy-only, and root-mode branches.
Change one mode only when task scope names it; gate the change and preserve the other modes.
- `AndroidLibXrayLite` is native-core source. `V2rayNG/app/libs/libv2ray.aar` and the HEV libraries
are generated inputs; do not modify or commit them unless task scope upgrades a native dependency.
Such a PR must identify the source revision and verify every app-declared ABI.
## Build and validation
Before building a clean checkout, initialize submodules recursively and reproduce the HEV and AAR
steps in `.github/workflows/build.yml`. The AAR revision must match the `AndroidLibXrayLite` gitlink
from `git ls-tree HEAD AndroidLibXrayLite`. Take build-tool versions from that workflow and
`V2rayNG/gradle/libs.versions.toml`.
Run Gradle from `V2rayNG/`, using `gradlew.bat` in Windows PowerShell or `./gradlew` on POSIX. Use
the Play Store debug variant unless task scope names another distribution.
Apply every matching validation rule:
- Documentation only: run `git diff --check`; no Gradle task is required.
- Kotlin/Java production code: run the test class that asserts each changed behavior, then
`:app:testPlaystoreDebugUnitTest` and `:app:compilePlaystoreDebugKotlin`.
- Resources, manifest, Gradle, dependencies, or native packaging: run
`:app:assemblePlaystoreDebug`.
- F-Droid only: replace `Playstore` with `Fdroid`. Run both variants when shared code branches on a
flavor, `BuildConfig.DISTRIBUTION`, or a flavor-specific resource or dependency.
- Android service lifecycle, framework callback, native interaction, permission, accessibility,
focus, or state restoration: also run an emulator or physical-device check.
Add or update JUnit tests under `V2rayNG/app/src/test/java/` for changed deterministic logic. Report
an unavailable required check under the exact label `Not run`, with its command or scenario and
reason. A compile or assembled APK does not verify runtime behavior.
There is no enforced Kotlin formatter. Preserve local indentation and import order. Do not change
whitespace outside touched declarations or reorder imports except as the diff requires.
## Repository-wide coding rules
- Keep the diff inside task scope; exclude unrelated refactors, upgrades, generated files,
translation cleanup, renames, and formatting-only changes.
- Use server GUIDs and group IDs for persistence, asynchronous work, and UI state. Never use a
list index, adapter position, or paging position as server or group identity.
- Run disk, network, package-manager, native, bitmap, and CPU-intensive work off the main thread.
Own each asynchronous operation with a named lifecycle or ViewModel scope and cancel it when the
owner ends or newer work supersedes it.
- Put all visible and accessibility text in Android resources. Update every locale in
`androidResources.localeFilters` for each changed key, preserving placeholders, plurals, and
formatting tags.
- Never rename or delete a persisted, serialized, routing, import, or export field without a
backward-compatible migration and a regression test reading the preceding released format.
Multi-record writes must complete together or restore/remove partial writes on every failure.
- Log recoverable failures through `LogUtil`, including operation, mode/component, non-secret
stable ID when available, and exception. Never log credentials, full proxy URLs, private keys,
or exported configurations.
- Before adding a constant, helper, manager, repository, or state holder, search its owner. Extend
an owner that controls the same data or lifecycle. Create a new abstraction only for different
ownership or lifetime, and state that distinction in code or the PR.
- A platform workaround needs a comment naming its Android/API or vendor boundary, prevented
failure, and exact removal condition.
## Deprecated, experimental, and version-gated APIs
- Introduce a deprecated API only for a supported-version fallback lacking the replacement, a
required deprecated callback/interface member, or a documented platform/vendor defect that
blocks acceptance criteria. The defect requires a test or linked upstream issue.
- Isolate each permitted deprecated call in one compatibility function or adapter. Comment the
reason and an exact removal trigger. Use the stable API wherever available.
- Scope `@Suppress("DEPRECATION")` or `@SuppressLint("<IssueId>")` to the containing expression or function.
Class scope is allowed only when every member implements the same required deprecated interface;
file, package, and module scope is forbidden. Never suppress permission, background-execution,
lifecycle, or security requirements.
- Guard every API above `minSdk` by SDK or extension version in the same function, or use
`@RequiresApi` only when every entry point is guarded. Mark reusable guards
`@ChecksSdkIntAtLeast`. Lower versions must use an
existing API, return explicit unsupported status, or hide/disable the feature; never crash or
report false success.
- Add or expand an experimental API only when no stable API meets named acceptance criteria, its
dependency is pinned in `libs.versions.toml`, a project-owned stable interface contains it and
excludes it from persisted data, IPC payloads, and public shared contracts, a regression test
covers it, and a comment names the opt-in marker and exact reevaluation condition.
- Put `@OptIn` on the direct function/property, or on a class only when multiple members need it.
File and module opt-ins are forbidden. Existing experimental use outside scope does not permit a
new call site.
- A preview/canary SDK or alpha dependency is allowed in production only when the request or linked
issue requires it. Keep a stable path for supported non-preview devices, gate by runtime API or
feature availability, and test both paths. After an API guard, opt-in, or lint suppression, run
`:app:lintPlaystoreDebug` and also `:app:lintFdroidDebug` when behavior differs by distribution.
+1
View File
@@ -0,0 +1 @@
@AGENTS.md
+1
View File
@@ -0,0 +1 @@
@AGENTS.md
+18 -4
View File
@@ -13,8 +13,8 @@ android {
applicationId = "com.v2ray.ang"
minSdk = 24
targetSdk = 37
versionCode = 744
versionName = "2.3.4"
versionCode = 754
versionName = "2.3.14"
val abiFilterList = (properties["ABI_FILTERS"] as? String)?.split(';')
splits {
@@ -138,6 +138,7 @@ android {
"ru",
"fa",
"ar",
"az",
"bn",
"bqi-rIR"
)
@@ -149,6 +150,17 @@ android {
}
}
testOptions {
unitTests.all {
it.useJUnitPlatform()
// Treat Jupiter discovery warnings as failures: JUnit 4 failed on an invalid @Test
// method, such as one that returns a value, while Jupiter only warns and skips it.
// The key is @API(status = EXPERIMENTAL) in JUnit 6; after a JUnit upgrade, check
// that a temporary `@Test fun probe() = 1` still fails the unit test task.
it.systemProperty("junit.platform.discovery.issue.severity.critical", "WARNING")
}
}
}
dependencies {
@@ -201,10 +213,12 @@ dependencies {
implementation(libs.reorderable)
// Testing Libraries
testImplementation(libs.junit)
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testRuntimeOnly(libs.junit.platform.launcher)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
testImplementation(libs.org.mockito.mockito.inline)
testImplementation(libs.mockito.core)
testImplementation(libs.mockito.kotlin)
coreLibraryDesugaring(libs.desugar.jdk.libs)
}
+7 -7
View File
@@ -33,10 +33,10 @@
<shortcut
android:enabled="true"
android:icon="@drawable/ic_qu_start_24dp"
android:shortcutDisabledMessage="@string/toast_services_start"
android:shortcutDisabledMessage="@string/acc_start"
android:shortcutId="shortcuts_start"
android:shortcutLongLabel="@string/toast_services_start"
android:shortcutShortLabel="@string/toast_services_start">
android:shortcutLongLabel="@string/acc_start"
android:shortcutShortLabel="@string/acc_start">
<!--suppress AndroidDomInspection -->
<intent
@@ -48,10 +48,10 @@
<shortcut
android:enabled="true"
android:icon="@drawable/ic_qu_stop_24dp"
android:shortcutDisabledMessage="@string/toast_services_stop"
android:shortcutDisabledMessage="@string/acc_stop"
android:shortcutId="shortcuts_stop"
android:shortcutLongLabel="@string/toast_services_stop"
android:shortcutShortLabel="@string/toast_services_stop">
android:shortcutLongLabel="@string/acc_stop"
android:shortcutShortLabel="@string/acc_stop">
<!--suppress AndroidDomInspection -->
<intent
@@ -60,4 +60,4 @@
android:targetPackage="com.v2ray.ang.fdroid" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>
</shortcuts>
+15 -11
View File
@@ -153,6 +153,7 @@
android:exported="false" />
<activity
android:name=".ui.userasset.UserAssetUrlActivity"
android:windowSoftInputMode="stateUnchanged|adjustResize"
android:exported="false" />
<activity
@@ -166,19 +167,19 @@
android:name=".ui.shortcut.ScSwitchActivity"
android:excludeFromRecents="true"
android:exported="false"
android:process=":RunSoLibV2RayDaemon"
android:process=":daemon"
android:theme="@style/AppThemeDayNight.NoActionBar.Translucent" />
<activity
android:name=".ui.shortcut.ScStartActivity"
android:excludeFromRecents="true"
android:exported="false"
android:process=":RunSoLibV2RayDaemon"
android:process=":daemon"
android:theme="@style/AppThemeDayNight.NoActionBar.Translucent" />
<activity
android:name=".ui.shortcut.ScStopActivity"
android:excludeFromRecents="true"
android:exported="false"
android:process=":RunSoLibV2RayDaemon"
android:process=":daemon"
android:theme="@style/AppThemeDayNight.NoActionBar.Translucent" />
<activity
@@ -209,6 +210,9 @@
<activity
android:name=".ui.AboutActivity"
android:exported="false" />
<activity
android:name=".ui.TranslatorsActivity"
android:exported="false" />
<service
android:name=".service.CoreVpnService"
@@ -217,7 +221,7 @@
android:foregroundServiceType="specialUse"
android:label="@string/app_name"
android:permission="android.permission.BIND_VPN_SERVICE"
android:process=":RunSoLibV2RayDaemon"
android:process=":daemon"
tools:ignore="VpnServicePolicy">
<intent-filter>
<action android:name="android.net.VpnService" />
@@ -235,7 +239,7 @@
android:exported="false"
android:foregroundServiceType="specialUse"
android:label="@string/app_name"
android:process=":RunSoLibV2RayDaemon">
android:process=":daemon">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="proxy" />
@@ -246,7 +250,7 @@
android:exported="false"
android:foregroundServiceType="specialUse"
android:label="@string/app_name"
android:process=":RunSoLibV2RayDaemon">
android:process=":daemon">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="proxy" />
@@ -256,7 +260,7 @@
android:name=".service.CoreTestService"
android:exported="false"
android:foregroundServiceType="specialUse"
android:process=":RunSoLibV2RayDaemon">
android:process=":tasks">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="test" />
@@ -266,7 +270,7 @@
android:name=".service.SubscriptionUpdateService"
android:exported="false"
android:foregroundServiceType="specialUse"
android:process=":RunSoLibV2RayDaemon">
android:process=":tasks">
<property
android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="test" />
@@ -275,7 +279,7 @@
<receiver
android:name=".receiver.WidgetProvider"
android:exported="true"
android:process=":RunSoLibV2RayDaemon">
android:process=":daemon">
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/app_widget_provider" />
@@ -303,7 +307,7 @@
android:icon="@drawable/ic_stat_name"
android:label="@string/app_tile_name"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"
android:process=":RunSoLibV2RayDaemon">
android:process=":daemon">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
@@ -324,7 +328,7 @@
<receiver
android:name=".receiver.TaskerReceiver"
android:exported="true"
android:process=":RunSoLibV2RayDaemon"
android:process=":daemon"
tools:ignore="ExportedReceiver">
<intent-filter>
<action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
@@ -0,0 +1,68 @@
[
{
"language": "العربية (Arabic)",
"contributors": [
{ "name": "@mribrahem", "displayName": "ibrahem Qasim", "url": "https://github.com/mribrahem" }
]
},
{
"language": "বাংলা (Bengali)",
"contributors": [
{ "name": "@codewithtamim", "displayName": "Tamim Hossain", "url": "https://github.com/codewithtamim" }
]
},
{
"language": "لۊری بختیاری (Luri Bakhtiari)",
"contributors": [
{ "name": "@hosseinabaspanah", "displayName": "Hossein Abaspanah", "url": "https://github.com/hosseinabaspanah" },
{ "name": "@codewithtamim", "displayName": "Tamim Hossain", "url": "https://github.com/codewithtamim" }
]
},
{
"language": "فارسی (Persian)",
"contributors": [
{ "name": "@themrvx", "displayName": "MRVX", "url": "https://github.com/themrvx" },
{ "name": "@skh-web6982", "url": "https://github.com/skh-web6982" },
{ "name": "@ptechgithub", "displayName": "Peyman", "url": "https://github.com/ptechgithub" },
{ "name": "@pk-web6936" },
{ "name": "@alphax-hue3682" },
{ "name": "@phoenix6936" },
{ "name": "@decorativefamily" },
{ "name": "@decorativeman" },
{ "name": "@mh292929", "displayName": "MH", "url": "https://github.com/mh292929" },
{ "name": "@amir-yazdanmanesh", "displayName": "AmirMohammad Yazdanmanesh", "url": "https://github.com/amir-yazdanmanesh" },
{ "name": "@cumoon", "displayName": "C_U_Moon", "url": "https://github.com/cumoon" },
{ "name": "@hadi-norouzi", "displayName": "Hadi Norouzi", "url": "https://github.com/hadi-norouzi" },
{ "name": "Vahid Farid" }
]
},
{
"language": "Русский (Russian)",
"contributors": [
{ "name": "@solokot", "url": "https://github.com/solokot" },
{ "name": "@liniya", "url": "https://github.com/liniya" },
{ "name": "@eliotcougar", "url": "https://github.com/eliotcougar" }
]
},
{
"language": "Tiếng Việt (Vietnamese)",
"contributors": [
{ "name": "@admarty", "url": "https://github.com/admarty" },
{ "name": "@user09283", "url": "https://github.com/user09283" },
{ "name": "@yuhan6665", "url": "https://github.com/yuhan6665" }
]
},
{
"language": "简体中文 (Simplified Chinese)",
"contributors": [
{ "name": "@yau08", "displayName": "David", "url": "https://github.com/yau08" }
]
},
{
"language": "繁體中文 (Traditional Chinese)",
"contributors": [
{ "name": "@yau08", "displayName": "David", "url": "https://github.com/yau08" },
{ "name": "@fubuki0x10de", "url": "https://github.com/fubuki0x10de" }
]
}
]
+71 -87
View File
@@ -1,102 +1,86 @@
{
"stats":{},
"stats": {},
"log": {
"loglevel": "warning"
},
"policy":{
"levels": {
"8": {
"handshake": 4,
"connIdle": 300,
"uplinkOnly": 1,
"downlinkOnly": 1
"policy": {
"levels": {
"8": {
"handshake": 4,
"connIdle": 300,
"uplinkOnly": 1,
"downlinkOnly": 1
}
},
"system": {
"statsOutboundUplink": true,
"statsOutboundDownlink": true
}
},
"inbounds": [
{
"tag": "socks",
"port": 10808,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true,
"userLevel": 8
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
}
],
"outbounds": [
{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"address": "v2ray.cool",
"port": 10086
},
"streamSettings": {
"network": "tcp"
},
"mux": {
"enabled": false
}
},
{
"protocol": "freedom",
"streamSettings": {
"sockopt": {
"domainStrategy": "UseIP",
"happyEyeballs": {
"tryDelayMs": 250,
"interleave": 2
}
}
},
"system": {
"statsOutboundUplink": true,
"statsOutboundDownlink": true
}
},
"inbounds": [{
"tag": "socks",
"port": 10808,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true,
"userLevel": 8
"tag": "direct"
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
}
],
"outbounds": [{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "v2ray.cool",
"port": 10086,
"users": [
{
"id": "a3482e88-686a-4a58-8126-99c9df64b7bf",
"alterId": 0,
"security": "auto",
"level": 8
}
]
{
"protocol": "blackhole",
"tag": "block",
"settings": {
"response": {
"type": "http"
}
],
"servers": [
{
"address": "v2ray.cool",
"method": "chacha20",
"ota": false,
"password": "123456",
"port": 10086,
"level": 8
}
]
},
"streamSettings": {
"network": "tcp"
},
"mux": {
"enabled": false
}
},
{
"protocol": "freedom",
"streamSettings": {
"sockopt": {
"domainStrategy": "UseIP"
}
},
"tag": "direct"
},
{
"protocol": "blackhole",
"tag": "block",
"settings": {
"response": {
"type": "http"
}
}
}
],
"routing": {
"domainStrategy": "AsIs",
"rules": []
"domainStrategy": "AsIs",
"rules": []
},
"dns": {
"hosts": {},
"servers": []
"hosts": {},
"servers": []
}
}
}
@@ -1,119 +1,103 @@
{
"stats":{},
"stats": {},
"log": {
"loglevel": "warning"
},
"policy":{
"levels": {
"8": {
"handshake": 4,
"connIdle": 300,
"uplinkOnly": 1,
"downlinkOnly": 1
"policy": {
"levels": {
"8": {
"handshake": 4,
"connIdle": 300,
"uplinkOnly": 1,
"downlinkOnly": 1
}
},
"system": {
"statsOutboundUplink": true,
"statsOutboundDownlink": true
}
},
"inbounds": [
{
"tag": "socks",
"port": 10808,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true,
"userLevel": 8
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
},
{
"tag": "tun",
"protocol": "tun",
"settings": {
"name": "xray0",
"MTU": 1500,
"userLevel": 8
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
}
],
"outbounds": [
{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"address": "v2ray.cool",
"port": 10086
},
"streamSettings": {
"network": "tcp"
},
"mux": {
"enabled": false
}
},
{
"protocol": "freedom",
"streamSettings": {
"sockopt": {
"domainStrategy": "UseIP",
"happyEyeballs": {
"tryDelayMs": 250,
"interleave": 2
}
}
},
"system": {
"statsOutboundUplink": true,
"statsOutboundDownlink": true
}
},
"inbounds": [{
"tag": "socks",
"port": 10808,
"protocol": "socks",
"settings": {
"auth": "noauth",
"udp": true,
"userLevel": 8
"tag": "direct"
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
},
{
"tag": "tun",
"protocol": "tun",
"settings": {
"name": "xray0",
"MTU": 1500,
"userLevel": 8
},
"sniffing": {
"enabled": true,
"destOverride": [
"http",
"tls",
"quic"
]
}
}
],
"outbounds": [{
"tag": "proxy",
"protocol": "vmess",
"settings": {
"vnext": [
{
"address": "v2ray.cool",
"port": 10086,
"users": [
{
"id": "a3482e88-686a-4a58-8126-99c9df64b7bf",
"alterId": 0,
"security": "auto",
"level": 8
}
]
{
"protocol": "blackhole",
"tag": "block",
"settings": {
"response": {
"type": "http"
}
],
"servers": [
{
"address": "v2ray.cool",
"method": "chacha20",
"ota": false,
"password": "123456",
"port": 10086,
"level": 8
}
]
},
"streamSettings": {
"network": "tcp"
},
"mux": {
"enabled": false
}
},
{
"protocol": "freedom",
"streamSettings": {
"sockopt": {
"domainStrategy": "UseIP"
}
},
"tag": "direct"
},
{
"protocol": "blackhole",
"tag": "block",
"settings": {
"response": {
"type": "http"
}
}
}
],
"routing": {
"domainStrategy": "AsIs",
"rules": []
"domainStrategy": "AsIs",
"rules": []
},
"dns": {
"hosts": {},
"servers": []
"hosts": {},
"servers": []
}
}
}
@@ -5,9 +5,9 @@ import android.content.Context
import androidx.core.content.ContextCompat
import androidx.work.Configuration
import androidx.work.WorkManager
import com.tencent.mmkv.MMKV
import com.v2ray.ang.AppConfig.ANG_PACKAGE
import com.v2ray.ang.handler.AppLocaleManager
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.handler.SettingsManager
import com.v2ray.ang.ui.compose.ThemeManager
@@ -35,7 +35,7 @@ class AngApplication : Application() {
override fun onCreate() {
super.onCreate()
MMKV.initialize(this)
MmkvManager.initialize(this)
AppLocaleManager.initialize(this)
@@ -166,8 +166,16 @@ object AppConfig {
const val WIREGUARD_LOCAL_ADDRESS_V4 = "172.16.0.2/32"
const val WIREGUARD_LOCAL_ADDRESS_V6 = "2606:4700:110:8f81:d551:a0:532e:a2b3/128"
const val WIREGUARD_LOCAL_MTU = "1420"
const val WIREGUARD_LOCAL_REMOTE_DNS = "1.1.1.1,1.0.0.1,2606:4700:4700::1111,2606:4700:4700::1001"
const val LOOPBACK = "127.0.0.1"
/** Shared defaults for settings shown in the UI and consumed by config generation. */
const val DEFAULT_SOCKS_ENABLE_UDP = true
const val DEFAULT_OUTBOUND_DOMAIN_RESOLVE_METHOD = "1"
const val DEFAULT_VPN_BYPASS_LAN = "1"
const val DEFAULT_HEV_TUNNEL_LOGLEVEL = "warn"
const val DEFAULT_MUX_XUDP_CONCURRENCY = "8"
/** Message constants for communication. */
const val MSG_REGISTER_CLIENT = 1
const val MSG_STATE_RUNNING = 11
@@ -181,6 +189,7 @@ object AppConfig {
const val MSG_STATE_RESTART = 5
const val MSG_MEASURE_DELAY = 6
const val MSG_MEASURE_DELAY_RESULT = 61
const val MSG_MEASURE_DELAY_CANCEL = 62
const val MSG_MEASURE_CONFIG_START = 7
const val MSG_MEASURE_CONFIG_CANCEL = 71
const val MSG_MEASURE_CONFIG_SUCCESS = 72
@@ -193,7 +202,6 @@ object AppConfig {
/** Notification channel IDs and names. */
// Use a new ID because Android does not let an app raise an existing channel's importance.
const val RAY_NG_CHANNEL_ID = "CORE_M_CH_ID_V2"
const val RAY_NG_CHANNEL_NAME = "Core Background Service"
/** Protocols Scheme **/
const val VMESS = "vmess://"
@@ -232,6 +240,7 @@ object AppConfig {
const val ROOT_TUN2SOCKS_BIN = "libhevsockstun.so"
const val ROOT_FWD_CHAIN = "CORE_FWD" // FORWARD chain for LAN/tethering sharing
const val ROOT_DNS_CHAIN = "CORE_DNS" // nat chain for tethered-client DNS DNAT
const val ROOT_DNS_OUT_CHAIN = "CORE_DNS_OUT" // nat chain for device DNS DNAT (root mode)
const val ROOT_V6_CHAIN = "CORE6_FILTER" // ip6tables filter/OUTPUT chain: blackhole native IPv6 when it isn't tunneled
const val ROOT_V6_FWD_CHAIN = "CORE6_FWD" // ip6tables FORWARD chain: route or reject tethered clients' native IPv6
const val ROOT_V6_PRE_CHAIN = "CORE6_PRE" // ip6tables mangle/PREROUTING chain: mark forwarded clients' IPv6 into the tun
@@ -113,7 +113,13 @@ object CoreConfigManager {
}
} else {
json.remove("stats")
json.remove("policy")
// Keep user-defined policy levels, only strip the stats-related system block
json.get("policy")?.takeIf { it.isJsonObject }?.asJsonObject?.let { policy ->
policy.remove("system")
if (policy.entrySet().isEmpty()) {
json.remove("policy")
}
}
}
if (!needTun()) {
@@ -407,13 +413,7 @@ object CoreConfigManager {
"${AppConfig.TAG_BALANCER_PRE}-${resolvedOutbound.tag}"
}
val strategyType = BalancerStrategyType.from(resolvedOutbound.profile.policyGroupType)
val fallbackTag = if (strategyType.supportsObservatory && resolvedOutbound.profile.policyGroupTestOutbounds != false) {
resolvedOutbound.profile.policyGroupFallbackTag
?.takeIf { it.isNotEmpty() && it != AppConfig.TAG_PROXY }
// Xray excludes dead random/roundRobin candidates only when fallbackTag is set;
// without this default, an enabled empty field creates no observatory.
?: membersToAdd.first().tag
} else null
val fallbackTag = resolvePolicyGroupFallbackTag(strategyType, resolvedOutbound.profile, membersToAdd.first().tag)
val strategy = buildBalancerStrategy(
strategyType = strategyType,
selector = listOf(memberTagPrefix),
@@ -429,6 +429,27 @@ object CoreConfigManager {
policyGroupBalancerTags[resolvedOutbound.tag] = balancerTag
}
internal fun resolvePolicyGroupFallbackTag(
strategyType: BalancerStrategyType,
profile: ProfileItem,
firstMemberTag: String,
): String? {
if (strategyType.requiresObservatory || strategyType.requiresBurstObservatory) {
// Missing or failed probes must not let a matched rule use Xray's default outbound.
return firstMemberTag
}
return if (strategyType.supportsObservatory && profile.policyGroupTestOutbounds != false) {
profile.policyGroupFallbackTag
?.takeIf { it.isNotEmpty() && it != AppConfig.TAG_PROXY }
// Xray excludes dead random/roundRobin candidates only when fallbackTag is set;
// without this default, an enabled empty field creates no observatory.
?: firstMemberTag
} else null
}
internal fun shouldUseStandardObservatory(strategyType: BalancerStrategyType, fallbackTag: String?): Boolean =
strategyType.requiresObservatory || (strategyType.supportsObservatory && fallbackTag != null)
/**
* Trim runtime sections that are not needed for latency testing.
*/
@@ -511,7 +532,7 @@ object CoreConfigManager {
inbound1.listen = AppConfig.LOOPBACK
}
inbound1.port = socksPort
inbound1.settings?.udp = MmkvManager.decodeSettingsBool(AppConfig.PREF_SOCKS_ENABLE_UDP, true)
inbound1.settings?.udp = MmkvManager.decodeSettingsBool(AppConfig.PREF_SOCKS_ENABLE_UDP, AppConfig.DEFAULT_SOCKS_ENABLE_UDP)
if (socksUsername != null && socksPassword != null) {
inbound1.settings?.auth = "password"
inbound1.settings?.accounts = listOf(
@@ -709,7 +730,7 @@ object CoreConfigManager {
private fun applySpeedDisabled(v2rayConfig: V2rayConfig) {
if (MmkvManager.decodeSettingsBool(AppConfig.PREF_SPEED_ENABLED) != true) {
v2rayConfig.stats = null
v2rayConfig.policy = null
v2rayConfig.policy?.system = null
}
}
@@ -1052,7 +1073,7 @@ object CoreConfigManager {
* Resolve outbound domains to IPs and write resolved hosts to DNS map.
*/
private fun resolveOutboundDomainsToHosts(v2rayConfig: V2rayConfig) {
if (MmkvManager.decodeSettingsString(AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD, "1") != "1") {
if (MmkvManager.decodeSettingsString(AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD, AppConfig.DEFAULT_OUTBOUND_DOMAIN_RESOLVE_METHOD) != "1") {
return
}
@@ -1243,7 +1264,7 @@ object CoreConfigManager {
fallbackTag = fallbackTag,
strategy = V2rayConfig.RoutingBean.StrategyObject(type = strategyType.policyGroupType)
)
val observatory = if (strategyType.requiresObservatory || fallbackTag != null) {
val observatory = if (shouldUseStandardObservatory(strategyType, fallbackTag)) {
V2rayConfig.ObservatoryObject(
subjectSelector = selector,
probeUrl = probeUrl,
@@ -62,7 +62,7 @@ object CoreOutboundBuilder {
if (muxEnabled) {
outbound.mux?.enabled = true
outbound.mux?.concurrency = MmkvManager.decodeSettingsString(AppConfig.PREF_MUX_CONCURRENCY, "8").orEmpty().toInt()
outbound.mux?.xudpConcurrency = MmkvManager.decodeSettingsString(AppConfig.PREF_MUX_XUDP_CONCURRENCY, "16").orEmpty().toInt()
outbound.mux?.xudpConcurrency = MmkvManager.decodeSettingsString(AppConfig.PREF_MUX_XUDP_CONCURRENCY, AppConfig.DEFAULT_MUX_XUDP_CONCURRENCY).orEmpty().toInt()
outbound.mux?.xudpProxyUDP443 = MmkvManager.decodeSettingsString(AppConfig.PREF_MUX_XUDP_QUIC, "reject")
if (protocol.equals(EConfigType.VLESS.name, true) && outbound.settings?.flow?.isNotEmpty() == true) {
outbound.mux?.concurrency = -1
@@ -252,15 +252,31 @@ object CoreOutboundBuilder {
ipv4Addresses.ifEmpty { listOf(AppConfig.WIREGUARD_LOCAL_ADDRESS_V4) }
}
val rawDNS = profileItem.remoteDNS
?.split(",")
?.map { it.trim() }
?.filter { it.isNotEmpty() }
?.ifEmpty { null }
?: listOf(AppConfig.WIREGUARD_LOCAL_REMOTE_DNS)
val remotes = if (MmkvManager.decodeSettingsBool(AppConfig.PREF_IPV6_ENABLED) == true) {
rawDNS
} else {
val ipv4Dns = rawDNS.filter { !it.contains(":") }
ipv4Dns.ifEmpty { listOf(AppConfig.WIREGUARD_LOCAL_REMOTE_DNS) }
}
outboundBean?.settings?.let { wireguard ->
wireguard.secretKey = profileItem.secretKey
wireguard.address = addresses
wireguard.port = null
wireguard.peers?.firstOrNull()?.let { peer ->
peer.publicKey = profileItem.publicKey.orEmpty()
peer.preSharedKey = profileItem.preSharedKey?.nullIfBlank()
peer.endpoint = Utils.getIpv6Address(profileItem.server) + ":${profileItem.serverPort}"
}
wireguard.mtu = profileItem.mtu
wireguard.remoteDNS = remotes
wireguard.reserved = profileItem.reserved?.takeIf { it.isNotBlank() }?.split(",")?.filter { it.isNotBlank() }?.map { it.trim().toInt() }
}
@@ -667,7 +683,7 @@ object CoreOutboundBuilder {
}
val domain = HttpUtil.toIdnDomain(profileItem.server.orEmpty())
if (MmkvManager.decodeSettingsString(AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD, "1") != "2") {
if (MmkvManager.decodeSettingsString(AppConfig.PREF_OUTBOUND_DOMAIN_RESOLVE_METHOD, AppConfig.DEFAULT_OUTBOUND_DOMAIN_RESOLVE_METHOD) != "2") {
return domain
}
//Resolve and replace domain
@@ -1,5 +1,6 @@
package com.v2ray.ang.core
import android.app.Activity
import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
@@ -17,6 +18,7 @@ import com.v2ray.ang.dto.ConnectionTestResult
import com.v2ray.ang.dto.OutboundTrafficStat
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.enums.BrowserDialerMode
import com.v2ray.ang.extension.delay
import com.v2ray.ang.extension.isNotNullEmpty
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.handler.NotificationManager
@@ -29,8 +31,13 @@ import com.v2ray.ang.service.NetworkMonitor
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import libv2ray.CoreCallbackHandler
import libv2ray.CoreController
import libv2ray.ProcessFinder
@@ -45,6 +52,7 @@ object CoreServiceManager {
private var processFinder: XrayProcessFinder? = null
private var browserDialer: IDialerService? = null
private var networkMonitor: NetworkMonitor? = null
private val connectionTestScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
@Volatile
private var isReloading = false
@@ -182,6 +190,7 @@ object CoreServiceManager {
* @return True if the core was stopped successfully, false otherwise.
*/
fun stopCoreLoop(): Boolean {
connectionTestScope.coroutineContext.cancelChildren()
val service = getService() ?: return false
networkMonitor?.unregister()
@@ -252,6 +261,7 @@ object CoreServiceManager {
val tunFd = currentVpnInterface
isReloading = true
connectionTestScope.coroutineContext.cancelChildren()
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Core reload start...")
coreController.stopLoop()
@@ -306,13 +316,15 @@ object CoreServiceManager {
* Tests with primary URL first, then falls back to alternative URL if needed.
* Also fetches remote IP information if the delay test was successful.
*/
private fun measureV2rayDelay() {
if (!isRunning()) {
private fun measureV2rayDelay(requestId: String) {
val service = getService() ?: return
if (!isRunning() || isReloading) {
MessageHelper.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_CANCEL, "", requestId)
return
}
CoroutineScope(Dispatchers.IO).launch {
val service = getService() ?: return@launch
connectionTestScope.coroutineContext.cancelChildren()
connectionTestScope.launch {
var time = -1L
var errorStr = ""
@@ -323,6 +335,7 @@ object CoreServiceManager {
errorStr = e.message?.substringAfter("\":").orEmpty()
}
if (time == -1L) {
ensureActive()
try {
time = coreController.measureDelay(SettingsManager.getDelayTestUrl(true))
} catch (e: Exception) {
@@ -331,25 +344,25 @@ object CoreServiceManager {
}
}
ensureActive()
val endpoint = if (time >= 0) SpeedtestManager.getRemoteIPInfo() else null
val result = ConnectionTestResult(
delayMillis = time,
errorMessage = errorStr,
country = endpoint?.country,
ipAddress = endpoint?.ipAddress,
)
MessageHelper.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_RESULT, result)
// Only fetch IP info if the delay test was successful
if (time >= 0) {
SpeedtestManager.getRemoteIPInfo()?.let { ip ->
MessageHelper.sendMsg2UI(
service,
AppConfig.MSG_MEASURE_DELAY_RESULT,
result.copy(
country = ip.country,
ipAddress = ip.ipAddress,
),
)
withContext(Dispatchers.Main.immediate) {
if (isRunning()) {
MessageHelper.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_RESULT, result, requestId)
} else {
MessageHelper.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_CANCEL, "", requestId)
}
}
}.invokeOnCompletion { cause ->
if (cause is CancellationException) {
MessageHelper.sendMsg2UI(service, AppConfig.MSG_MEASURE_DELAY_CANCEL, "", requestId)
}
}
}
@@ -371,6 +384,7 @@ object CoreServiceManager {
* @return 0 for success, any other value for failure.
*/
override fun startup(): Long {
LogUtil.i(AppConfig.TAG, "StartCore-Manager: CoreCallback startup")
return 0
}
@@ -379,14 +393,8 @@ object CoreServiceManager {
* @return 0 for success, any other value for failure.
*/
override fun shutdown(): Long {
val serviceControl = serviceControl?.get() ?: return -1
return try {
serviceControl.stopService()
0
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "StartCore-Manager: Failed to stop service", e)
-1
}
LogUtil.i(AppConfig.TAG, "StartCore-Manager: CoreCallback shutdown")
return 0
}
/**
@@ -396,6 +404,7 @@ object CoreServiceManager {
* @return Always returns 0.
*/
override fun onEmitStatus(l: Long, s: String?): Long {
LogUtil.i(AppConfig.TAG, "StartCore-Manager: CoreCallback onEmitStatus $s")
return 0
}
}
@@ -474,13 +483,25 @@ object CoreServiceManager {
AppConfig.MSG_STATE_RESTART -> {
LogUtil.i(AppConfig.TAG, "StartCore-Manager: Restart service")
serviceControl.stopService()
Thread.sleep(500L)
LauncherManager.startService(serviceControl.getService())
// The UI and daemon run in separate processes, so acknowledge the active
// daemon before stopping it instead of relying on possibly stale UI state.
if (isOrderedBroadcast) resultCode = Activity.RESULT_OK
val pendingResult = goAsync()
CoroutineScope(Dispatchers.Default).launch {
try {
serviceControl.stopService()
delay(500L)
LauncherManager.startService(serviceControl.getService())
} finally {
pendingResult.finish()
}
}
}
AppConfig.MSG_MEASURE_DELAY -> {
measureV2rayDelay()
if (isOrderedBroadcast) resultCode = Activity.RESULT_OK
measureV2rayDelay(intent.getStringExtra("content").orEmpty())
}
}
@@ -56,6 +56,18 @@ object LauncherManager {
MessageHelper.sendMsg2Service(context, AppConfig.MSG_STATE_STOP, "")
}
/** Restarts the active daemon without starting a stopped service. */
fun restartService(context: Context) {
MessageHelper.sendMsg2Service(context, AppConfig.MSG_STATE_RESTART, "")
}
/** Restarts the active daemon, or delegates to the caller's permission-aware start flow. */
fun restartServiceOrStart(context: Context, startIfStopped: () -> Unit) {
MessageHelper.sendMsg2ServiceForResult(context, AppConfig.MSG_STATE_RESTART, "") { handled ->
if (!handled) startIfStopped()
}
}
@Throws(Exception::class)
private fun startContextService(context: Context) {
// Note: isRunning check is removed here to avoid loading Native libraries in the UI process.
@@ -1,6 +1,9 @@
package com.v2ray.ang.dto
import com.v2ray.ang.dto.entities.SubscriptionUsageInfo
data class GroupMapItem(
var id: String,
var remarks: String,
val usageInfo: SubscriptionUsageInfo? = null,
)
@@ -2,6 +2,5 @@ package com.v2ray.ang.dto
data class LocateTarget(
val groupId: String,
val groupIndex: Int,
val itemPosition: Int
)
val serverGuid: String,
)
@@ -0,0 +1,9 @@
package com.v2ray.ang.dto
import java.io.Serializable
/** One persisted delay-test result delivered from the task process to the UI. */
data class RealPingResult(
val guid: String,
val delayMillis: Long,
) : Serializable
@@ -0,0 +1,36 @@
package com.v2ray.ang.dto
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.v2ray.ang.AppConfig
import com.v2ray.ang.util.LogUtil
data class Contributor(
val name: String,
val displayName: String? = null,
val url: String? = null
)
data class TranslatorsCredit(
val language: String,
val contributors: List<Contributor>
)
object TranslatorsParser {
private val gson = Gson()
fun parse(json: String): List<TranslatorsCredit> {
if (json.isBlank()) return emptyList()
return try {
val type = object : TypeToken<List<TranslatorsCredit>>() {}.type
gson.fromJson<List<TranslatorsCredit>>(json, type).orEmpty()
.filter { it.language.isNotBlank() && it.contributors.isNotEmpty() }
.distinctBy { it.language }
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse translators.json", e)
emptyList()
}
}
}
@@ -69,7 +69,8 @@ data class V2rayConfig(
var settings: OutSettingsBean? = null,
var streamSettings: StreamSettingsBean? = null,
val sendThrough: String? = null,
var mux: MuxBean? = MuxBean(false)
var mux: MuxBean? = MuxBean(false),
var targetStrategy: String? = null
) {
data class OutSettingsBean(
/*Common */
@@ -98,7 +99,7 @@ data class V2rayConfig(
val peers: List<WireGuardBean>? = null,
var reserved: List<Int>? = null,
var mtu: Int? = null,
var domainStrategy: String? = null,
var remoteDNS: List<String>? = null,
) {
data class WireGuardBean(
var publicKey: String = "",
@@ -7,7 +7,7 @@ import com.v2ray.ang.enums.NetworkType
@Suppress("PropertyName")
data class V2rayNShareItem(
// val IndexId: String?,
val IndexId: String?,
val ConfigType: Int?,
// val CoreType: Int?,
val ConfigVersion: Int?,
@@ -56,6 +56,7 @@ data class ProfileItem(
var localAddress: String? = null,
var reserved: String? = null,
var mtu: Int? = null,
var remoteDNS: String? = null,
var obfsPassword: String? = null,
var portHopping: String? = null,
@@ -14,5 +14,6 @@ data class SubscriptionItem(
var allowInsecureUrl: Boolean = false,
var userAgent: String? = null,
var requestHeaders: String? = null,
var usageInfo: SubscriptionUsageInfo? = null,
)
@@ -0,0 +1,28 @@
package com.v2ray.ang.dto.entities
/**
* Account information a subscription panel (3x-ui, Marzban, Remnawave, ...) publishes in the
* HTTP response headers of the subscription URL. Every field is optional because panels send
* only the headers they have a value for.
*
* Persisted as JSON inside [SubscriptionItem]; only add fields, never rename or remove one.
*/
data class SubscriptionUsageInfo(
/** Bytes uploaded, from the `Subscription-Userinfo` header. */
val upload: Long = 0,
/** Bytes downloaded, from the `Subscription-Userinfo` header. */
val download: Long = 0,
/** Traffic quota in bytes; 0 means unlimited or not provided. */
val total: Long = 0,
/** Expiry as Unix epoch seconds; 0 means no expiry or not provided. */
val expire: Long = 0,
/** Decoded `Profile-Title` header. */
val title: String? = null,
/** Validated `Support-Url` header (usually a Telegram link). */
val supportUrl: String? = null,
/** Validated `Profile-Web-Page-Url` header. */
val webPageUrl: String? = null,
) {
/** Bytes consumed so far (upload + download). */
val used: Long get() = upload + download
}
@@ -12,6 +12,7 @@ enum class Language(val code: String, val languageTag: String) {
RUSSIAN("ru", "ru"),
PERSIAN("fa", "fa"),
ARABIC("ar", "ar"),
AZERBAIJANI("az", "az"),
BANGLA("bn", "bn"),
BAKHTIARI("bqi-rIR", "bqi-IR");
@@ -48,6 +49,7 @@ enum class Language(val code: String, val languageTag: String) {
"ru" -> RUSSIAN
"fa" -> PERSIAN
"ar" -> ARABIC
"az" -> AZERBAIJANI
"bn" -> BANGLA
"bqi" -> BAKHTIARI
else -> AUTO
@@ -1,22 +1,25 @@
package com.v2ray.ang.enums
import androidx.annotation.StringRes
import com.v2ray.ang.R
/**
* Enum defining different notification channels.
* Each channel has a unique channelId, notificationId, and display name.
*/
enum class NotificationChannelType(
val channelId: String,
val channelName: String,
@StringRes val channelNameRes: Int,
val notificationId: Int
) {
SUBSCRIPTION_UPDATE(
channelId = "subscription_update_channel",
channelName = "Subscription Update Service",
channelNameRes = R.string.notification_channel_subscription_updates,
notificationId = 13
),
CORE_TEST(
channelId = "core_test_channel",
channelName = "Core Test Service",
channelNameRes = R.string.notification_channel_connection_checks,
notificationId = 12
)
}
}
@@ -7,111 +7,20 @@ import android.widget.Toast
import com.v2ray.ang.ui.compose.AppSnackbarManager
import com.v2ray.ang.ui.compose.ToastType
/**
* Shows a toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
*/
fun Context.toast(message: Int) {
val text = getString(message)
dispatchMessage(text, ToastType.NORMAL) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
private val mainHandler by lazy { Handler(Looper.getMainLooper()) }
/**
* Shows a toast message with the given text.
*
* @param message The text of the message to show.
*/
fun Context.toast(message: CharSequence) {
dispatchMessage(message, ToastType.NORMAL) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows a toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
*/
fun Context.toastSuccess(message: Int) {
val text = getString(message)
dispatchMessage(text, ToastType.SUCCESS) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows a toast message with the given text.
*
* @param message The text of the message to show.
*/
fun Context.toastSuccess(message: CharSequence) {
dispatchMessage(message, ToastType.SUCCESS) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows a toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
*/
fun Context.toastError(message: Int) {
val text = getString(message)
dispatchMessage(text, ToastType.ERROR) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows a toast message with the given text.
*
* @param message The text of the message to show.
*/
fun Context.toastError(message: CharSequence) {
dispatchMessage(message, ToastType.ERROR) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows an info toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
*/
fun Context.toastInfo(message: Int) {
val text = getString(message)
dispatchMessage(text, ToastType.INFO) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}
}
/**
* Shows an info toast message with the given text.
*
* @param message The text of the message to show.
*/
fun Context.toastInfo(message: CharSequence) {
dispatchMessage(message, ToastType.INFO) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}
}
private inline fun runOnMain(crossinline block: () -> Unit) {
private fun runOnMain(block: () -> Unit) {
if (Looper.myLooper() == Looper.getMainLooper()) {
block()
} else {
Handler(Looper.getMainLooper()).post { block() }
mainHandler.post(block)
}
}
private inline fun dispatchMessage(
private fun Context.dispatchMessage(
message: CharSequence,
type: ToastType,
long: Boolean = false,
crossinline fallback: () -> Unit
long: Boolean = false
) {
val handledBySnackbar = AppSnackbarManager.show(
message = message,
@@ -119,6 +28,90 @@ private inline fun dispatchMessage(
long = long
)
if (!handledBySnackbar) {
runOnMain { fallback() }
val appContext = applicationContext
val duration = if (long) Toast.LENGTH_LONG else Toast.LENGTH_SHORT
runOnMain {
Toast.makeText(appContext, message, duration).show()
}
}
}
/**
* Shows a toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toast(message: Int, long: Boolean = false) {
dispatchMessage(getString(message), ToastType.NORMAL, long)
}
/**
* Shows a toast message with the given text.
*
* @param message The text of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toast(message: CharSequence, long: Boolean = false) {
dispatchMessage(message, ToastType.NORMAL, long)
}
/**
* Shows a success toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastSuccess(message: Int, long: Boolean = false) {
dispatchMessage(getString(message), ToastType.SUCCESS, long)
}
/**
* Shows a success toast message with the given text.
*
* @param message The text of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastSuccess(message: CharSequence, long: Boolean = false) {
dispatchMessage(message, ToastType.SUCCESS, long)
}
/**
* Shows an error toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastError(message: Int, long: Boolean = false) {
dispatchMessage(getString(message), ToastType.ERROR, long)
}
/**
* Shows an error toast message with the given text.
*
* @param message The text of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastError(message: CharSequence, long: Boolean = false) {
dispatchMessage(message, ToastType.ERROR, long)
}
/**
* Shows an info toast message with the given resource ID.
*
* @param message The resource ID of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastInfo(message: Int, long: Boolean = false) {
dispatchMessage(getString(message), ToastType.INFO, long)
}
/**
* Shows an info toast message with the given text.
*
* @param message The text of the message to show.
* @param long Whether to display the message for a longer duration.
*/
fun Context.toastInfo(message: CharSequence, long: Boolean = false) {
dispatchMessage(message, ToastType.INFO, long)
}
@@ -7,6 +7,7 @@ import com.v2ray.ang.enums.EConfigType
import java.io.Serializable
import java.net.URI
import java.util.Locale
import kotlin.time.Duration.Companion.milliseconds
const val THRESHOLD = 1000L
const val DIVISOR = 1024.0
@@ -76,3 +77,19 @@ fun EConfigType.isGroupType(): Boolean {
fun EConfigType.isComplexType(): Boolean {
return this == EConfigType.CUSTOM || this == EConfigType.POLICYGROUP || this == EConfigType.PROXYCHAIN
}
/**
* Shorthand for delay with Int milliseconds using Duration to avoid legacy Long overload warning.
*/
suspend fun delay(millis: Int) {
kotlinx.coroutines.delay(millis.toLong().milliseconds)
}
/**
* Shorthand for delay with Long milliseconds using Duration to avoid legacy Long overload warning.
*/
suspend fun delay(millis: Long) {
kotlinx.coroutines.delay(millis.milliseconds)
}
@@ -23,7 +23,7 @@ object Hysteria2Fmt : FmtBase() {
val uri = URI(Utils.fixIllegalUrl(str))
config.remarks = Utils.decodeURIComponent(uri.fragment.orEmpty()).let { it.ifEmpty { "none" } }
config.server = uri.idnHost
config.serverPort = uri.port.toString()
config.serverPort = (if (uri.port == -1) AppConfig.DEFAULT_PORT else uri.port).toString()
config.password = uri.userInfo
config.security = AppConfig.TLS
config.network = NetworkType.HYSTERIA.type
@@ -3,20 +3,46 @@ package com.v2ray.ang.fmt
import com.v2ray.ang.AppConfig
import com.v2ray.ang.dto.V2rayNShareItem
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.extension.isNotNullEmpty
import com.v2ray.ang.util.JsonUtil
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
object V2rayNFmt : FmtBase() {
fun parse(str: String): ProfileItem? {
try {
val jsonBase64Payload = str.substringAfterLast('/')
val jsonPayload = Utils.decode(jsonBase64Payload)
val v2rayNShareItem = JsonUtil.fromJson(jsonPayload, V2rayNShareItem::class.java)
return v2rayNShareItem?.toProfileItem()
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse V2rayN format", e)
fun parse(list: List<String>, subId: String): List<ProfileItem> {
val idMap = mutableMapOf<String, V2rayNShareItem>()
list.mapNotNull(::parseShareItem).forEach { item ->
idMap.putIfAbsent(item.IndexId.orEmpty(), item)
}
return null
return idMap.values.map { item ->
item.toProfileItem().apply {
val proto = item.ProtoExtraObj
policyGroupSubscriptionId = if (proto?.SubChildItems == "self") {
subId
} else {
null
}
proto?.ChildItems?.takeIf { it.isNotNullEmpty() }?.let { ids ->
val remarks = ids.split(",")
.mapNotNull { idMap[it]?.Remarks }
.filter { it.isNotNullEmpty() }
if (remarks.isNotEmpty()) {
when (item.ConfigType) {
101 -> policyGroupFilter = remarks.joinToString("|", "^(", ")$") { Regex.escape(it) }
102 -> proxyChainProfiles = remarks.joinToString(",")
}
}
}
}
}
}
private fun parseShareItem(str: String): V2rayNShareItem? = try {
JsonUtil.fromJson(Utils.decode(str.substringAfterLast('/')), V2rayNShareItem::class.java)
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse V2rayN share item", e)
null
}
}
@@ -32,7 +32,9 @@ object WireguardFmt : FmtBase() {
config.publicKey = queryParam["publickey"].orEmpty()
config.preSharedKey = queryParam["presharedkey"]?.nullIfBlank()
config.mtu = Utils.parseInt(queryParam["mtu"] ?: AppConfig.WIREGUARD_LOCAL_MTU)
config.remoteDNS = queryParam["dns"] ?: AppConfig.WIREGUARD_LOCAL_REMOTE_DNS
config.reserved = queryParam["reserved"] ?: "0,0,0"
config.finalMask = (queryParam["fm"] ?: queryParam["finalmask"] ?: queryParam["finalMask"])?.nullIfBlank()
return config
}
@@ -81,6 +83,7 @@ object WireguardFmt : FmtBase() {
config.remarks = System.currentTimeMillis().toString()
config.localAddress = interfaceParams["address"] ?: AppConfig.WIREGUARD_LOCAL_ADDRESS_V4
config.mtu = Utils.parseInt(interfaceParams["mtu"] ?: AppConfig.WIREGUARD_LOCAL_MTU)
config.remoteDNS = interfaceParams["dns"] ?: AppConfig.WIREGUARD_LOCAL_REMOTE_DNS
config.publicKey = peerParams["publickey"].orEmpty()
config.preSharedKey = peerParams["presharedkey"]?.nullIfBlank()
val endpoint = peerParams["endpoint"].orEmpty()
@@ -93,6 +96,7 @@ object WireguardFmt : FmtBase() {
config.serverPort = ""
}
config.reserved = peerParams["reserved"] ?: "0,0,0"
config.finalMask = (peerParams["finalmask"] ?: peerParams["fm"] ?: interfaceParams["finalmask"] ?: interfaceParams["fm"])?.nullIfBlank()
return config
}
@@ -118,6 +122,10 @@ object WireguardFmt : FmtBase() {
if (config.preSharedKey != null) {
dicQuery["presharedkey"] = config.preSharedKey.removeWhiteSpace().orEmpty()
}
if (config.remoteDNS != null) {
dicQuery["dns"] = config.remoteDNS.removeWhiteSpace().orEmpty()
}
config.finalMask?.nullIfBlank()?.let { dicQuery["fm"] = it }
return toUri(config, config.secretKey, dicQuery)
}
@@ -4,13 +4,13 @@ import android.content.Context
import android.graphics.Bitmap
import android.text.TextUtils
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.core.CoreConfigManager
import com.v2ray.ang.dto.SubscriptionUpdateResult
import com.v2ray.ang.dto.UrlContentRequest
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.dto.entities.SubscriptionCache
import com.v2ray.ang.dto.entities.SubscriptionItem
import com.v2ray.ang.dto.entities.SubscriptionUsageInfo
import com.v2ray.ang.enums.EConfigType
import com.v2ray.ang.extension.isNotNullEmpty
import com.v2ray.ang.fmt.CustomFmt
@@ -27,10 +27,16 @@ import com.v2ray.ang.util.JsonUtil
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.QRCodeDecoder
import com.v2ray.ang.util.Utils
import okhttp3.Headers
import java.net.URI
object AngConfigManager {
private data class ParsedProfile(
val profile: ProfileItem,
val rawConfig: String? = null,
)
// Parser mapping for different config types (lazy initialized)
private val configFmtParsers: Map<String, (String) -> ProfileItem?> by lazy {
mapOf(
@@ -44,7 +50,6 @@ object AngConfigManager {
EConfigType.WIREGUARD.protocolScheme to WireguardFmt::parse,
EConfigType.HYSTERIA2.protocolScheme to Hysteria2Fmt::parse,
AppConfig.HY2 to Hysteria2Fmt::parse,
AppConfig.V2RAYNFMTS to V2rayNFmt::parse
)
}
@@ -177,23 +182,28 @@ object AngConfigManager {
* @return A pair containing the number of configurations and subscriptions imported.
*/
fun importBatchConfig(server: String?, subid: String, append: Boolean): Pair<Int, Int> {
var count = parseBatchConfig(Utils.decode(server), subid, append)
if (count <= 0) {
count = parseBatchConfig(server, subid, append)
}
if (count <= 0) {
count = parseCustomConfigServer(server, subid, append)
}
return try {
var count = parseBatchConfig(Utils.decode(server), subid, append)
if (count <= 0) {
count = parseBatchConfig(server, subid, append)
}
if (count <= 0) {
count = parseCustomConfigServer(server, subid, append)
}
var countSub = parseBatchSubscription(server)
if (countSub <= 0) {
countSub = parseBatchSubscription(Utils.decode(server))
}
if (countSub > 0) {
updateConfigViaSubAll()
}
var countSub = parseBatchSubscription(server)
if (countSub <= 0) {
countSub = parseBatchSubscription(Utils.decode(server))
}
if (countSub > 0) {
updateConfigViaSubAll()
}
return count to countSub
count to countSub
} catch (e: ProfileStorageException) {
LogUtil.e(AppConfig.TAG, "Failed to store imported profiles", e)
0 to 0
}
}
/**
@@ -236,34 +246,40 @@ object AngConfigManager {
if (servers == null) {
return 0
}
// Find the currently selected server that belongs to the same subscription before replacement.
val removedSelected = getRemovedSelectedProfile(subid, append)
val subItem = MmkvManager.decodeSubscription(subid)
// Parse all configs first (no I/O during parsing)
val configs = mutableListOf<ProfileItem>()
val v2raynLines = mutableListOf<String>()
servers.lines()
.distinct()
.reversed()
.forEach {
val config = parseConfig(it, subid, subItem)
if (config != null) {
configs.add(config)
if (it.startsWith(AppConfig.V2RAYNFMTS, ignoreCase = true)) {
v2raynLines.add(it)
} else {
val config = parseConfig(it, subid, subItem)
if (config != null) {
configs.add(config)
}
}
}
// Batch save all parsed configs (only one serverList read/write)
if (configs.isNotEmpty()) {
if (!append) {
MmkvManager.removeServerViaSubid(subid)
}
val keyToProfile = batchSaveConfigs(configs, subid)
val matchKey = findMatchedProfileKey(keyToProfile, removedSelected)
matchKey?.let { MmkvManager.setSelectServer(it) }
val v2raynConfigs = V2rayNFmt.parse(v2raynLines, subid)
val allConfigs = v2raynConfigs + configs
if (allConfigs.isNotEmpty()) {
commitProfiles(
configs = allConfigs.map(::ParsedProfile),
subid = subid,
append = append,
)
}
return configs.size
return allConfigs.size
} catch (e: ProfileStorageException) {
throw e
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse batch config", e)
}
@@ -271,112 +287,32 @@ object AngConfigManager {
}
/**
* Batch save configurations to reduce serverList read/write operations.
* Reads serverList once, saves all configs, then writes serverList once.
* Commits parsed profiles before removing the profiles they replace.
*
* @param configs The list of ProfileItem to save.
* @param configs The parsed profiles to save.
* @param subid The subscription ID.
* @return Map of generated keys to their corresponding ProfileItem.
* @param append Whether to append to the existing server list.
*/
private fun batchSaveConfigs(configs: List<ProfileItem>, subid: String): Map<String, ProfileItem> {
val keyToProfile = mutableMapOf<String, ProfileItem>()
private fun commitProfiles(
configs: List<ParsedProfile>,
subid: String,
append: Boolean,
) {
val keyToProfile = linkedMapOf<String, ProfileItem>()
val rawConfigs = mutableMapOf<String, String>()
// Read serverList once
val serverList = MmkvManager.decodeServerList(subid)
configs.forEach { config ->
configs.forEach { parsed ->
val key = Utils.getUuid()
// Save profile directly without updating serverList
MmkvManager.encodeProfileDirect(key, JsonUtil.toJson(config))
if (!serverList.contains(key)) {
serverList.add(0, key)
}
keyToProfile[key] = config
keyToProfile[key] = parsed.profile
parsed.rawConfig?.let { raw -> rawConfigs[key] = raw }
}
// Write serverList once
MmkvManager.encodeServerList(serverList, subid)
return keyToProfile
}
/**
* Finds a matched profile key from the given key-profile map using multi-level matching.
* Matching priority (from highest to lowest):
* 1. Exact match: server + port + password
* 2. Match by remarks (exact match)
* 3. Match by server + port
* 4. Match by server only
*
* @param keyToProfile Map of server keys to their ProfileItem
* @param target Target profile to match
* @return Matched key or null
*/
private fun findMatchedProfileKey(keyToProfile: Map<String, ProfileItem>, target: ProfileItem?): String? {
if (keyToProfile.isEmpty()) return null
if (target == null) return null
// Level 0: Full match (remarks + server + port + password)
if (target.remarks.isNotBlank()) {
keyToProfile.entries.firstOrNull { (_, saved) ->
isSameText(saved.remarks, target.remarks) &&
isSameText(saved.server, target.server) &&
isSameText(saved.serverPort, target.serverPort) &&
isSameText(saved.password, target.password)
}?.key?.let { return it }
}
// Level 1: Match by remarks
if (target.remarks.isNotBlank()) {
keyToProfile.entries.firstOrNull { (_, saved) ->
isSameText(saved.remarks, target.remarks)
}?.key?.let { return it }
}
// Level 2: Exact match (server + port + password)
keyToProfile.entries.firstOrNull { (_, saved) ->
isSameText(saved.server, target.server) &&
isSameText(saved.serverPort, target.serverPort) &&
isSameText(saved.password, target.password)
}?.key?.let { return it }
// Level 3: Match by server + port
keyToProfile.entries.firstOrNull { (_, saved) ->
isSameText(saved.server, target.server) &&
isSameText(saved.serverPort, target.serverPort)
}?.key?.let { return it }
// Level 4: Match by server only
keyToProfile.entries.firstOrNull { (_, saved) ->
isSameText(saved.server, target.server)
}?.key?.let { return it }
// If old selected node cannot be matched, fall back to the first imported config.
return keyToProfile.keys.firstOrNull()
}
/**
* Returns the currently selected profile if it belongs to the target subscription and will be replaced.
*/
private fun getRemovedSelectedProfile(subid: String, append: Boolean): ProfileItem? {
if (subid.isBlank() || append) return null
return MmkvManager.getSelectServer()
.takeIf { it?.isNotBlank() == true }
?.let { MmkvManager.decodeServerConfig(it) }
?.takeIf { it.subscriptionId == subid }
}
/**
* Case-insensitive trimmed string comparison.
*
* @param left First string
* @param right Second string
* @return True if both are non-empty and equal (case-insensitive, trimmed)
*/
private fun isSameText(left: String?, right: String?): Boolean {
if (left.isNullOrBlank() || right.isNullOrBlank()) return false
return left.trim().equals(right.trim(), ignoreCase = true)
MmkvManager.saveServerProfiles(
profiles = keyToProfile,
rawConfigs = rawConfigs,
subscriptionId = subid,
append = append,
)
}
/**
@@ -400,56 +336,54 @@ object AngConfigManager {
JsonUtil.fromJson(server, Array<Any>::class.java) ?: arrayOf()
if (serverList.isNotEmpty()) {
val removedSelected = getRemovedSelectedProfile(subid, append)
if (!append) {
MmkvManager.removeServerViaSubid(subid)
}
var count = 0
val keyToProfile = mutableMapOf<String, ProfileItem>()
for (srv in serverList.reversed()) {
val config = CustomFmt.parse(JsonUtil.toJson(srv)) ?: continue
val configs = serverList.reversed().map { srv ->
val config = CustomFmt.parse(JsonUtil.toJson(srv))
config.subscriptionId = subid
config.description = generateDescription(config)
val key = MmkvManager.encodeServerConfig("", config)
MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv) ?: "")
keyToProfile[key] = config
count += 1
ParsedProfile(
profile = config,
rawConfig = JsonUtil.toJsonPretty(srv) ?: "",
)
}
if (count > 0) {
val matchKey = findMatchedProfileKey(keyToProfile, removedSelected)
matchKey?.let { MmkvManager.setSelectServer(it) }
}
return count
commitProfiles(configs, subid, append)
return configs.size
}
} catch (e: ProfileStorageException) {
throw e
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse custom config server JSON array", e)
}
try {
// For compatibility
val config = CustomFmt.parse(server) ?: return 0
val config = CustomFmt.parse(server)
config.subscriptionId = subid
config.description = generateDescription(config)
if (!append) {
MmkvManager.removeServerViaSubid(subid)
}
val key = MmkvManager.encodeServerConfig("", config)
MmkvManager.encodeServerRaw(key, server)
commitProfiles(
configs = listOf(ParsedProfile(config, server)),
subid = subid,
append = append,
)
return 1
} catch (e: ProfileStorageException) {
throw e
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse custom config server as single config", e)
}
return 0
} else if (server.startsWith("[Interface]") && server.contains("[Peer]")) {
try {
val config = WireguardFmt.parseWireguardConfFile(server) ?: return R.string.toast_incorrect_protocol
val config = WireguardFmt.parseWireguardConfFile(server)
config.subscriptionId = subid
config.description = generateDescription(config)
if (!append) {
MmkvManager.removeServerViaSubid(subid)
}
val key = MmkvManager.encodeServerConfig("", config)
MmkvManager.encodeServerRaw(key, server)
commitProfiles(
configs = listOf(ParsedProfile(config, server)),
subid = subid,
append = append,
)
return 1
} catch (e: ProfileStorageException) {
throw e
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse WireGuard config file", e)
}
@@ -496,12 +430,6 @@ object AngConfigManager {
config.subscriptionId = subid
config.description = generateDescription(config)
if (str.startsWith(AppConfig.V2RAYNFMTS, ignoreCase = true)
&& config.policyGroupSubscriptionId == "self"
) {
config.policyGroupSubscriptionId = subid
}
return config
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to parse config", e)
@@ -562,6 +490,17 @@ object AngConfigManager {
val proxyUsername = SettingsManager.getSocksUsername()
val proxyPassword = SettingsManager.getSocksPassword()
// Account info (traffic, expiry, support link) the panel sends in the response headers.
var usageInfo: SubscriptionUsageInfo? = null
val captureUsageInfo: (Headers) -> Unit = { headers ->
usageInfo = SubscriptionInfoParser.parse(
userInfo = headers["Subscription-Userinfo"],
profileTitle = headers["Profile-Title"],
supportUrl = headers["Support-Url"],
webPageUrl = headers["Profile-Web-Page-Url"],
)
}
var configText = try {
val httpPort = SettingsManager.getHttpPort()
HttpUtil.getUrlContentWithUserAgent(
@@ -573,7 +512,8 @@ object AngConfigManager {
httpPort = httpPort,
proxyUsername = proxyUsername,
proxyPassword = proxyPassword
)
),
captureUsageInfo
)
} catch (e: Exception) {
LogUtil.e(AppConfig.ANG_PACKAGE, "Update subscription: proxy not ready or other error", e)
@@ -586,7 +526,8 @@ object AngConfigManager {
url = url,
userAgent = userAgent,
requestHeaders = requestHeaders
)
),
captureUsageInfo
)
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Update subscription: Failed to get URL content with user agent", e)
@@ -600,6 +541,8 @@ object AngConfigManager {
val count = parseConfigViaSub(configText, it.guid, false)
if (count > 0) {
it.subscription.lastUpdated = System.currentTimeMillis()
// Replaced on every successful update so the box disappears if the panel stops sending the headers.
it.subscription.usageInfo = usageInfo
MmkvManager.encodeSubscription(it.guid, it.subscription)
LogUtil.i(AppConfig.TAG, "Subscription updated: ${it.subscription.remarks}, $count configs")
return SubscriptionUpdateResult(
@@ -1,5 +1,7 @@
package com.v2ray.ang.handler
import android.content.Context
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
@@ -7,12 +9,18 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVHandler
import com.tencent.mmkv.MMKVLogLevel
import com.tencent.mmkv.MMKVRecoverStrategic
import com.v2ray.ang.AppConfig.DEFAULT_SUBSCRIPTION_ID
import com.v2ray.ang.AppConfig.PREF_IS_BOOTED
import com.v2ray.ang.AppConfig.PREF_ROUTING_RULESET
import com.v2ray.ang.AppConfig.TAG
import com.v2ray.ang.BuildConfig
import com.v2ray.ang.dto.entities.AssetUrlCache
import com.v2ray.ang.dto.entities.AssetUrlItem
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.extension.isGroupType
import com.v2ray.ang.dto.entities.RulesetItem
import com.v2ray.ang.dto.entities.ServerAffiliationInfo
import com.v2ray.ang.dto.entities.SubscriptionCache
@@ -24,6 +32,8 @@ import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.drop
internal class ProfileStorageException(message: String) : IllegalStateException(message)
object MmkvManager {
//region private
@@ -41,6 +51,24 @@ object MmkvManager {
private const val KEY_SUB_IDS = "SUB_IDS"
private const val KEY_WEBDAV_CONFIG = "WEBDAV_CONFIG"
private val recoveryHandler = object : MMKVHandler {
override fun onMMKVCRCCheckFail(mmapID: String) =
recoverFromStorageError(mmapID, "CRC check")
override fun onMMKVFileLengthError(mmapID: String) =
recoverFromStorageError(mmapID, "file length check")
override fun wantLogRedirecting(): Boolean = false
override fun mmkvLog(
level: MMKVLogLevel,
file: String,
line: Int,
function: String,
message: String
) = Unit
}
private val mainStorage by lazy { MMKV.mmkvWithID(ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
private val profileFullStorage by lazy { MMKV.mmkvWithID(ID_PROFILE_FULL_CONFIG, MMKV.MULTI_PROCESS_MODE) }
private val serverRawStorage by lazy { MMKV.mmkvWithID(ID_SERVER_RAW, MMKV.MULTI_PROCESS_MODE) }
@@ -49,8 +77,83 @@ object MmkvManager {
private val assetStorage by lazy { MMKV.mmkvWithID(ID_ASSET, MMKV.MULTI_PROCESS_MODE) }
private val settingsStorage by lazy { MMKV.mmkvWithID(ID_SETTING, MMKV.MULTI_PROCESS_MODE) }
private inline fun <T> withProfileIndexLock(block: () -> T): T {
return synchronized(mainStorage) {
mainStorage.lock()
try {
block()
} finally {
mainStorage.unlock()
}
}
}
private fun removeProfilePayloads(guids: Collection<String>) {
if (guids.isEmpty()) return
val keys = guids.toTypedArray()
profileFullStorage.removeValuesForKeys(keys)
serverAffStorage.removeValuesForKeys(keys)
serverRawStorage.removeValuesForKeys(keys)
}
private fun requireStorageWrite(success: Boolean, message: String) {
if (!success) throw ProfileStorageException(message)
}
private fun persistServerList(serverList: List<String>, subscriptionId: String): Boolean {
return mainStorage.encode(serverListKey(subscriptionId), JsonUtil.toJson(serverList))
}
private fun serverListKey(subscriptionId: String): String {
return "$KEY_SUB_SERVER_PREFIX${getSubscriptionId(subscriptionId)}"
}
/**
* Returns every server referenced outside the target group, or null if the raw indexes
* cannot provide a complete view.
*/
private fun decodeServersReferencedByOtherGroups(subscriptionId: String): Set<String>? {
val targetKey = serverListKey(subscriptionId)
val keys = mainStorage.allKeys() ?: return null
if (targetKey !in keys) return null
val referencedServers = mutableSetOf<String>()
for (key in keys) {
if (!key.startsWith(KEY_SUB_SERVER_PREFIX) || key == targetKey) continue
val json = mainStorage.decodeString(key)
if (json.isNullOrBlank()) return null
val serverIds = JsonUtil.fromJsonSafe(json, Array<String>::class.java) ?: return null
referencedServers.addAll(serverIds)
}
return referencedServers
}
//endregion
/**
* Initializes MMKV with best-effort recovery so a damaged store is not silently discarded.
*/
fun initialize(context: Context) {
val logLevel = if (BuildConfig.DEBUG) {
MMKVLogLevel.LevelDebug
} else {
MMKVLogLevel.LevelInfo
}
MMKV.initialize(
context,
context.filesDir.resolve("mmkv").absolutePath,
null,
logLevel,
recoveryHandler
)
}
private fun recoverFromStorageError(mmapID: String, error: String): MMKVRecoverStrategic {
Log.e(TAG, "MMKV $error failed for $mmapID; attempting data recovery")
return MMKVRecoverStrategic.OnErrorRecover
}
//region Server
/**
@@ -79,7 +182,9 @@ object MmkvManager {
* @param guid The server GUID.
*/
fun setSelectServer(guid: String) {
mainStorage.encode(KEY_SELECTED_SERVER, guid)
withProfileIndexLock {
mainStorage.encode(KEY_SELECTED_SERVER, guid)
}
}
/**
@@ -90,9 +195,9 @@ object MmkvManager {
* @param subscriptionId The subscription ID.
*/
fun encodeServerList(serverList: MutableList<String>, subscriptionId: String) {
val subId = getSubscriptionId(subscriptionId)
val key = "$KEY_SUB_SERVER_PREFIX$subId"
mainStorage.encode(key, JsonUtil.toJson(serverList))
withProfileIndexLock {
persistServerList(serverList, subscriptionId)
}
}
@@ -105,9 +210,7 @@ object MmkvManager {
* @return The list of server GUIDs.
*/
fun decodeServerList(subscriptionId: String): MutableList<String> {
val subId = getSubscriptionId(subscriptionId)
val key = "$KEY_SUB_SERVER_PREFIX$subId"
val json = mainStorage.decodeString(key)
val json = mainStorage.decodeString(serverListKey(subscriptionId))
return if (json.isNullOrBlank()) {
mutableListOf()
} else {
@@ -166,17 +269,28 @@ object MmkvManager {
*/
fun encodeServerConfig(guid: String, config: ProfileItem): String {
val key = guid.ifBlank { Utils.getUuid() }
profileFullStorage.encode(key, JsonUtil.toJson(config))
withProfileIndexLock {
requireStorageWrite(
profileFullStorage.encode(key, JsonUtil.toJson(config)),
"Failed to save profile payload",
)
// Use default subscription for servers without subscription
val subId = getSubscriptionId(config.subscriptionId)
val serverList = decodeServerList(subId)
// Use default subscription for servers without subscription
val subId = getSubscriptionId(config.subscriptionId)
val serverList = decodeServerList(subId)
if (!serverList.contains(key)) {
serverList.add(0, key)
encodeServerList(serverList, subId)
if (getSelectServer().isNullOrBlank()) {
mainStorage.encode(KEY_SELECTED_SERVER, key)
if (!serverList.contains(key)) {
serverList.add(0, key)
requireStorageWrite(
persistServerList(serverList, subId),
"Failed to publish profile index",
)
if (getSelectServer().isNullOrBlank()) {
requireStorageWrite(
mainStorage.encode(KEY_SELECTED_SERVER, key),
"Failed to update selected profile",
)
}
}
}
@@ -184,13 +298,90 @@ object MmkvManager {
}
/**
* Encodes the server configuration directly without updating serverList.
* Saves a profile batch before publishing its group index and removing replaced payloads.
*
* @param key The server GUID.
* @param configJson The server configuration JSON string.
* @param profiles Generated GUIDs and parsed profiles, in insertion order.
* @param rawConfigs Optional raw configuration payloads keyed by profile GUID.
* @param subscriptionId The destination subscription ID.
* @param append Whether to append to the existing group index.
*/
fun encodeProfileDirect(key: String, configJson: String) {
profileFullStorage.encode(key, configJson)
internal fun saveServerProfiles(
profiles: Map<String, ProfileItem>,
rawConfigs: Map<String, String>,
subscriptionId: String,
append: Boolean,
) {
if (profiles.isEmpty()) return
withProfileIndexLock {
val replacedServers = if (append) {
emptyList()
} else {
decodeServerList(subscriptionId).toList()
}
val previousSelection = getSelectServer()
val selectedProfile = if (!append &&
previousSelection != null &&
previousSelection in replacedServers
) {
decodeServerConfig(previousSelection)
} else {
null
}
val replacementSelection = ProfileReplacement.findSelectedReplacement(
profiles = profiles,
currentSelection = previousSelection,
selectedProfile = selectedProfile,
)
profiles.forEach { (guid, profile) ->
requireStorageWrite(
profileFullStorage.encode(guid, JsonUtil.toJson(profile)),
"Failed to save profile payload",
)
rawConfigs[guid]?.let { raw ->
requireStorageWrite(
serverRawStorage.encode(guid, raw),
"Failed to save raw profile payload",
)
}
}
val serverList = if (append) {
decodeServerList(subscriptionId)
} else {
replacedServers.filter { guid ->
decodeServerConfig(guid)?.configType?.isGroupType() == true
}.toMutableList()
}
val indexedServers = serverList.toHashSet()
profiles.keys.forEach { guid ->
if (indexedServers.add(guid)) {
serverList.add(0, guid)
}
}
requireStorageWrite(
persistServerList(serverList, subscriptionId),
"Failed to publish profile index",
)
replacementSelection?.let { guid ->
requireStorageWrite(
mainStorage.encode(KEY_SELECTED_SERVER, guid),
"Failed to update selected profile",
)
}
if (replacedServers.isEmpty()) return@withProfileIndexLock
val protectedServer = replacementSelection ?: previousSelection
val referencedByOtherGroups = decodeServersReferencedByOtherGroups(subscriptionId)
val removablePayloads = ProfileReplacement.findRemovablePayloads(
replacedServers = replacedServers,
replacementServers = serverList.toSet(),
protectedServer = protectedServer,
serversReferencedByOtherGroups = referencedByOtherGroups,
)
removeProfilePayloads(removablePayloads)
}
}
/**
@@ -378,6 +569,56 @@ object MmkvManager {
return serverRawStorage.decodeString(guid)
}
/**
* Removes profile payloads that are provably absent from their raw SUB_SERVERS_* index.
*
* SUB_IDS and SUB are intentionally ignored: either store can be missing after MMKV
* recovery while the group indexes still identify live profiles. If any group index or
* profile payload needed for a decision is unreadable, that data is preserved.
*
* @return The number of profile payloads removed, or null if cleanup could not run safely.
*/
internal fun removeOrphanedServerProfiles(): Int? = synchronized(mainStorage) {
mainStorage.lock()
try {
val indexedServersBySubscription = mainStorage.allKeys().orEmpty()
.asSequence()
.filter { key -> key.startsWith(KEY_SUB_SERVER_PREFIX) }
.associate { key ->
val subscriptionId = key.removePrefix(KEY_SUB_SERVER_PREFIX)
val json = mainStorage.decodeString(key)
val serverIds = if (json.isNullOrBlank()) {
null
} else {
JsonUtil.fromJsonSafe(json, Array<String>::class.java)?.toSet()
}
subscriptionId to serverIds
}
val profiles = profileFullStorage.allKeys().orEmpty().map { guid ->
StoredProfileReference(
guid = guid,
subscriptionId = decodeServerConfig(guid)?.subscriptionId,
)
}
val orphans = OrphanProfileCleaner.findOrphans(
profiles = profiles,
indexedServersBySubscription = indexedServersBySubscription,
selectedServer = getSelectServer(),
) ?: return@synchronized null
if (orphans.isNotEmpty()) {
val keys = orphans.toTypedArray()
profileFullStorage.removeValuesForKeys(keys)
serverAffStorage.removeValuesForKeys(keys)
serverRawStorage.removeValuesForKeys(keys)
}
orphans.size
} finally {
mainStorage.unlock()
}
}
//endregion
//region Subscriptions
@@ -480,7 +721,8 @@ object MmkvManager {
return if (json.isNullOrBlank()) {
mutableListOf()
} else {
JsonUtil.fromJsonSafe(json, Array<String>::class.java)?.toMutableList() ?: mutableListOf()
// Keep the first occurrence so a damaged index cannot produce duplicate Compose keys.
JsonUtil.fromJsonSafe(json, Array<String>::class.java)?.distinct()?.toMutableList() ?: mutableListOf()
}
}
@@ -1,7 +1,6 @@
package com.v2ray.ang.handler
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
@@ -15,13 +14,14 @@ import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.core.CoreServiceManager
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.extension.delay
import com.v2ray.ang.extension.toSpeedString
import com.v2ray.ang.helper.NotificationHelper
import com.v2ray.ang.ui.main.MainActivity
import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlin.math.min
@@ -157,12 +157,17 @@ object NotificationManager {
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(): String {
val channelId = AppConfig.RAY_NG_CHANNEL_ID
val channelName = AppConfig.RAY_NG_CHANNEL_NAME
// Foreground-service notifications must remain visible; LOW is silent but valid.
val chan = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_LOW)
chan.lightColor = Color.DKGRAY
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
getNotificationManager()?.createNotificationChannel(chan)
val service = getService() ?: return channelId
NotificationHelper.ensureNotificationChannel(
context = service,
channelId = channelId,
channelNameRes = R.string.notification_channel_service,
// Foreground-service notifications must remain visible; LOW is silent but valid.
importance = NotificationManager.IMPORTANCE_LOW,
) {
lightColor = Color.DKGRAY
lockscreenVisibility = Notification.VISIBILITY_PRIVATE
}
return channelId
}
@@ -0,0 +1,53 @@
package com.v2ray.ang.handler
import com.v2ray.ang.AppConfig.DEFAULT_SUBSCRIPTION_ID
internal data class StoredProfileReference(
val guid: String,
val subscriptionId: String?,
)
internal object OrphanProfileCleaner {
/**
* Finds profile payloads that are provably unreachable from the raw group indexes.
*
* A null subscription ID means that the profile payload could not be decoded and is
* preserved. A null server set means that a group index could not be decoded, in which
* case the entire classification returns null and no cleanup should run.
* Subscription metadata is deliberately not an input because SUB and SUB_IDS can be
* missing while the raw SUB_SERVERS_* indexes remain intact.
*/
fun findOrphans(
profiles: Collection<StoredProfileReference>,
indexedServersBySubscription: Map<String, Set<String>?>,
selectedServer: String?,
): Set<String>? {
if (profiles.isEmpty()) return emptySet()
if (indexedServersBySubscription.isEmpty() ||
indexedServersBySubscription.values.any { it == null }
) {
return null
}
val indexedServers = indexedServersBySubscription.values
.filterNotNull()
.flatten()
.toSet()
return profiles.mapNotNullTo(linkedSetOf()) { profile ->
if (profile.guid == selectedServer || profile.guid in indexedServers) {
return@mapNotNullTo null
}
val subscriptionId = profile.subscriptionId ?: return@mapNotNullTo null
val groupId = subscriptionId.ifEmpty { DEFAULT_SUBSCRIPTION_ID }
if (!indexedServersBySubscription.containsKey(groupId)) {
return@mapNotNullTo null
}
profile.guid
}
}
}
@@ -0,0 +1,76 @@
package com.v2ray.ang.handler
import com.v2ray.ang.dto.entities.ProfileItem
internal object ProfileReplacement {
/**
* Finds the profile that should become selected after publishing a replacement batch.
* The first profile becomes selected when the store has no current selection.
*/
fun findSelectedReplacement(
profiles: Map<String, ProfileItem>,
currentSelection: String?,
selectedProfile: ProfileItem?,
): String? {
if (profiles.isEmpty()) return null
if (currentSelection.isNullOrBlank()) return profiles.keys.first()
if (selectedProfile == null) return null
if (selectedProfile.remarks.isNotBlank()) {
profiles.entries.firstOrNull { (_, candidate) ->
isSameText(candidate.remarks, selectedProfile.remarks) &&
isSameText(candidate.server, selectedProfile.server) &&
isSameText(candidate.serverPort, selectedProfile.serverPort) &&
isSameText(candidate.password, selectedProfile.password)
}?.key?.let { return it }
profiles.entries.firstOrNull { (_, candidate) ->
isSameText(candidate.remarks, selectedProfile.remarks)
}?.key?.let { return it }
}
profiles.entries.firstOrNull { (_, candidate) ->
isSameText(candidate.server, selectedProfile.server) &&
isSameText(candidate.serverPort, selectedProfile.serverPort) &&
isSameText(candidate.password, selectedProfile.password)
}?.key?.let { return it }
profiles.entries.firstOrNull { (_, candidate) ->
isSameText(candidate.server, selectedProfile.server) &&
isSameText(candidate.serverPort, selectedProfile.serverPort)
}?.key?.let { return it }
profiles.entries.firstOrNull { (_, candidate) ->
isSameText(candidate.server, selectedProfile.server)
}?.key?.let { return it }
return profiles.keys.firstOrNull()
}
/**
* Finds replaced payloads that are safe to remove.
*
* A null cross-group reference set means that at least one raw group index could not
* be read. In that case deletion fails closed.
*/
fun findRemovablePayloads(
replacedServers: Collection<String>,
replacementServers: Set<String>,
protectedServer: String?,
serversReferencedByOtherGroups: Set<String>?,
): Set<String> {
if (serversReferencedByOtherGroups == null) return emptySet()
return replacedServers.filterTo(linkedSetOf()) { guid ->
guid != protectedServer &&
guid !in replacementServers &&
guid !in serversReferencedByOtherGroups
}
}
private fun isSameText(left: String?, right: String?): Boolean {
if (left.isNullOrBlank() || right.isNullOrBlank()) return false
return left.trim().equals(right.trim(), ignoreCase = true)
}
}
@@ -176,7 +176,7 @@ object SettingsManager {
* @return True if bypassing LAN, false otherwise.
*/
fun routingRulesetsBypassLan(): Boolean {
val vpnBypassLan = MmkvManager.decodeSettingsString(AppConfig.PREF_VPN_BYPASS_LAN) ?: "1"
val vpnBypassLan = MmkvManager.decodeSettingsString(AppConfig.PREF_VPN_BYPASS_LAN, AppConfig.DEFAULT_VPN_BYPASS_LAN)
if (vpnBypassLan == "1") {
return true
} else if (vpnBypassLan == "2") {
@@ -467,7 +467,7 @@ object SettingsManager {
ensureDefaultValue(AppConfig.PREF_IP_API_URL, AppConfig.IP_API_URL)
ensureDefaultValue(AppConfig.PREF_HEV_TUNNEL_RW_TIMEOUT, AppConfig.HEVTUN_RW_TIMEOUT)
ensureDefaultValue(AppConfig.PREF_MUX_CONCURRENCY, "8")
ensureDefaultValue(AppConfig.PREF_MUX_XUDP_CONCURRENCY, "8")
ensureDefaultValue(AppConfig.PREF_MUX_XUDP_CONCURRENCY, AppConfig.DEFAULT_MUX_XUDP_CONCURRENCY)
ensureDefaultValue(AppConfig.PREF_FRAGMENT_LENGTH, "50-100")
ensureDefaultValue(AppConfig.PREF_FRAGMENT_INTERVAL, "10-20")
ensureDefaultValue(AppConfig.PREF_FRAGMENT_MAXSPLIT, "10")
@@ -0,0 +1,121 @@
package com.v2ray.ang.handler
import com.v2ray.ang.dto.entities.SubscriptionUsageInfo
import com.v2ray.ang.util.Utils
import java.net.URI
import java.net.URLDecoder
import java.util.Locale
/**
* Parses the account information subscription panels put in the response headers of the
* subscription URL (the de-facto standard shared by 3x-ui, Marzban, Remnawave, Clash, v2rayN,
* Hiddify and others):
*
* - `Subscription-Userinfo: upload=<bytes>; download=<bytes>; total=<bytes>; expire=<unix seconds>`
* - `Profile-Title: <text>` or `base64:<base64 text>`
* - `Support-Url: <link>` (usually a Telegram link)
* - `Profile-Web-Page-Url: <link>` (the panel's subscription information page)
*
* Pure functions only, so the logic is covered by plain JVM tests.
*/
object SubscriptionInfoParser {
private const val BASE64_PREFIX = "base64:"
private const val MAX_TITLE_LENGTH = 100
private const val MAX_URL_LENGTH = 2048
/** Values at or above this are epoch milliseconds, not seconds (year 5138 in seconds). */
private const val MILLIS_THRESHOLD = 100_000_000_000L
private val SAFE_SCHEMES = setOf("http", "https", "tg")
private val TELEGRAM_HOSTS = setOf("t.me", "telegram.me", "telegram.dog", "telegram.org")
/**
* Builds usage info from raw header values.
*
* @return null when the panel sent none of the supported headers, so the UI shows nothing.
*/
fun parse(
userInfo: String?,
profileTitle: String?,
supportUrl: String?,
webPageUrl: String?,
decodeBase64: (String) -> String = Utils::decode,
): SubscriptionUsageInfo? {
val fields = parseUserInfoFields(userInfo)
val title = decodeTitle(profileTitle, decodeBase64)
val support = sanitizeLink(supportUrl)
val webPage = sanitizeLink(webPageUrl)
if (fields == null && title == null && support == null && webPage == null) return null
return SubscriptionUsageInfo(
upload = fields?.get("upload") ?: 0,
download = fields?.get("download") ?: 0,
total = fields?.get("total") ?: 0,
expire = normalizeExpire(fields?.get("expire") ?: 0),
title = title,
supportUrl = support,
webPageUrl = webPage,
)
}
/**
* The subscription URL itself answers with the panel's information page when it is opened in
* a browser (3x-ui, Marzban), so it is the fallback target of the "info page" button.
*/
fun withWebPageFallback(info: SubscriptionUsageInfo, subscriptionUrl: String): SubscriptionUsageInfo {
if (info.webPageUrl != null) return info
return info.copy(webPageUrl = sanitizeLink(subscriptionUrl))
}
/** Returns [raw] when it is an http(s) or tg:// link that is safe to hand to ACTION_VIEW. */
fun sanitizeLink(raw: String?): String? {
val value = raw?.trim().orEmpty()
if (value.isEmpty() || value.length > MAX_URL_LENGTH) return null
val uri = runCatching { URI(value) }.getOrNull() ?: return null
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return null
if (scheme !in SAFE_SCHEMES) return null
if (scheme != "tg" && uri.host.isNullOrEmpty()) return null
return value
}
/** True for links that open in the Telegram app. */
fun isTelegramLink(url: String?): Boolean {
val uri = runCatching { URI(url.orEmpty().trim()) }.getOrNull() ?: return false
val scheme = uri.scheme?.lowercase(Locale.ROOT) ?: return false
if (scheme == "tg") return true
return uri.host?.lowercase(Locale.ROOT) in TELEGRAM_HOSTS
}
private fun parseUserInfoFields(header: String?): Map<String, Long>? {
if (header.isNullOrBlank()) return null
val result = HashMap<String, Long>()
for (part in header.split(';')) {
val separator = part.indexOf('=')
if (separator <= 0) continue
val key = part.substring(0, separator).trim().lowercase(Locale.ROOT)
val raw = part.substring(separator + 1).trim()
val number = raw.toLongOrNull() ?: raw.toDoubleOrNull()?.takeIf { it.isFinite() }?.toLong()
if (number != null) result[key] = number.coerceAtLeast(0)
}
return result.takeIf { it.isNotEmpty() }
}
private fun normalizeExpire(value: Long): Long =
if (value >= MILLIS_THRESHOLD) value / 1000 else value
private fun decodeTitle(raw: String?, decodeBase64: (String) -> String): String? {
val value = raw?.trim().orEmpty()
if (value.isEmpty()) return null
val decoded = when {
value.startsWith(BASE64_PREFIX, ignoreCase = true) ->
decodeBase64(value.substring(BASE64_PREFIX.length).trim())
value.contains('%') ->
runCatching { URLDecoder.decode(value, "UTF-8") }.getOrDefault(value)
else -> value
}
return decoded.trim().take(MAX_TITLE_LENGTH).ifEmpty { null }
}
}
@@ -1,5 +1,7 @@
package com.v2ray.ang.helper
import android.app.Activity
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
@@ -14,7 +16,7 @@ import com.v2ray.ang.util.LogUtil
import java.io.Serializable
object MessageHelper {
const val EXTRA_REQUEST_ID = "requestId"
/**
* Sends a message to the service.
@@ -27,6 +29,37 @@ object MessageHelper {
sendMsg(ctx, AppConfig.BROADCAST_ACTION_SERVICE, what, content)
}
/**
* Sends an ordered service message and reports whether a daemon receiver handled it.
* With no running daemon, the initial canceled result reaches [onResult] unchanged.
*/
internal fun sendMsg2ServiceForResult(
ctx: Context,
what: Int,
content: Serializable,
onResult: (handled: Boolean) -> Unit,
) {
val resultReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
onResult(resultCode == Activity.RESULT_OK)
}
}
try {
ctx.sendOrderedBroadcast(
messageIntent(AppConfig.BROADCAST_ACTION_SERVICE, what, content),
null,
resultReceiver,
null,
Activity.RESULT_CANCELED,
null,
null,
)
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to send ordered message to service", e)
onResult(false)
}
}
/**
* Sends a message to the UI.
*
@@ -34,8 +67,8 @@ object MessageHelper {
* @param what The message identifier.
* @param content The message content.
*/
fun sendMsg2UI(ctx: Context, what: Int, content: Serializable) {
sendMsg(ctx, AppConfig.BROADCAST_ACTION_ACTIVITY, what, content)
fun sendMsg2UI(ctx: Context, what: Int, content: Serializable, requestId: String? = null) {
sendMsg(ctx, AppConfig.BROADCAST_ACTION_ACTIVITY, what, content, requestId)
}
/**
@@ -44,11 +77,12 @@ object MessageHelper {
* @param ctx The context.
* @param message The test service message containing key, subscriptionId, and serverGuids.
*/
fun sendMsg2TestService(ctx: Context, message: TestServiceMessage) {
fun sendMsg2TestService(ctx: Context, message: TestServiceMessage, requestId: String? = null) {
try {
val intent = Intent()
intent.component = ComponentName(ctx, CoreTestService::class.java)
intent.putExtra("content", message)
requestId?.let { intent.putExtra(EXTRA_REQUEST_ID, it) }
when (message.key) {
AppConfig.MSG_MEASURE_CONFIG_START -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@@ -69,6 +103,9 @@ object MessageHelper {
}
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to send message to test service", e)
if (message.key == AppConfig.MSG_MEASURE_CONFIG_START) {
sendMsg2UI(ctx, AppConfig.MSG_MEASURE_CONFIG_CANCEL, "", requestId)
}
}
}
@@ -113,16 +150,20 @@ object MessageHelper {
* @param what The message identifier.
* @param content The message content.
*/
private fun sendMsg(ctx: Context, action: String, what: Int, content: Serializable) {
private fun sendMsg(ctx: Context, action: String, what: Int, content: Serializable, requestId: String? = null) {
try {
val intent = Intent()
intent.action = action
intent.`package` = AppConfig.ANG_PACKAGE
intent.putExtra("key", what)
intent.putExtra("content", content)
ctx.sendBroadcast(intent)
ctx.sendBroadcast(messageIntent(action, what, content).apply {
requestId?.let { putExtra(EXTRA_REQUEST_ID, it) }
})
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to send message with action: $action", e)
}
}
}
private fun messageIntent(action: String, what: Int, content: Serializable): Intent =
Intent(action).apply {
`package` = AppConfig.ANG_PACKAGE
putExtra("key", what)
putExtra("content", content)
}
}
@@ -6,9 +6,11 @@ import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.os.Build
import androidx.annotation.StringRes
import androidx.core.app.NotificationCompat
import com.v2ray.ang.R
import com.v2ray.ang.enums.NotificationChannelType
import com.v2ray.ang.handler.AppLocaleManager
/**
* Unified notification helper for different notification channels.
@@ -124,20 +126,43 @@ object NotificationHelper {
return cachedNotificationManager!!
}
private fun ensureChannelCreated(channelType: NotificationChannelType, context: Context) {
private fun ensureChannelCreated(channelType: NotificationChannelType, context: Context) =
ensureNotificationChannel(
context = context,
channelId = channelType.channelId,
channelNameRes = channelType.channelNameRes,
importance = NotificationManager.IMPORTANCE_LOW,
)
/**
* Creates a channel or updates only its localized name.
*
* Android lets apps rename an existing channel, while its behavior remains under user control.
*/
internal fun ensureNotificationChannel(
context: Context,
channelId: String,
@StringRes channelNameRes: Int,
importance: Int,
configureNewChannel: NotificationChannel.() -> Unit = {},
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager.getNotificationChannel(channelType.channelId) != null) return
val channel = NotificationChannel(
channelType.channelId,
channelType.channelName,
NotificationManager.IMPORTANCE_LOW
).apply {
lockscreenVisibility = Notification.VISIBILITY_PRIVATE
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val localizedName = AppLocaleManager.localizedContext(context).getString(channelNameRes)
val existingChannel = notificationManager.getNotificationChannel(channelId)
if (existingChannel == null) {
NotificationChannel(channelId, localizedName, importance)
.apply {
lockscreenVisibility = Notification.VISIBILITY_PRIVATE
configureNewChannel()
}
.also(notificationManager::createNotificationChannel)
} else if (existingChannel.name.toString() != localizedName) {
existingChannel.name = localizedName
notificationManager.createNotificationChannel(existingChannel)
}
notificationManager.createNotificationChannel(channel)
}
private fun buildNotificationBuilder(
@@ -6,7 +6,6 @@ import com.v2ray.ang.root.RootManager.refresh
import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.concurrent.TimeUnit
/**
* Detects whether the device grants root (`su`) access.
@@ -43,21 +42,17 @@ object RootManager {
private fun probe(): Boolean {
return try {
val process = ProcessBuilder("su", "-c", "id -u")
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().use { it.readText() }.trim()
val finished = process.waitFor(10, TimeUnit.SECONDS)
if (!finished) {
process.destroy()
val result = RootProcessRunner.run(listOf("su", "-c", "id -u"), 10000, 1024)
if (result.code == -1) {
LogUtil.w(AppConfig.TAG, "RootManager: su probe timed out")
return false
}
val isRoot = process.exitValue() == 0 && output.lineSequence().lastOrNull()?.trim() == "0"
val isRoot = result.code == 0 && result.output.lineSequence().filter { it.isNotBlank() }.lastOrNull()?.trim() == "0"
LogUtil.i(AppConfig.TAG, "RootManager: root available = $isRoot")
isRoot
} catch (e: Exception) {
LogUtil.w(AppConfig.TAG, "RootManager: no root access (${e.message})")
if (e is InterruptedException) Thread.currentThread().interrupt()
LogUtil.w(AppConfig.TAG, "RootManager: root probe failed", e)
false
}
}
@@ -0,0 +1,41 @@
package com.v2ray.ang.root
import java.io.ByteArrayOutputStream
import java.util.concurrent.TimeUnit
/** Owns one bounded root command. Neither inherited stdout nor output volume extends its deadline. */
internal object RootProcessRunner {
data class Outcome(val code: Int, val output: String)
fun run(command: List<String>, timeoutMillis: Long, outputLimit: Int = 65536): Outcome {
val process = ProcessBuilder(command).redirectErrorStream(true).start()
val captured = ByteArrayOutputStream()
val buffer = ByteArray(4096)
val deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis)
var code = -1
try {
// Process.waitFor(timeout, unit) is API 26. Polling supports API 24–25 too.
// Read only available bytes: a detached helper may retain stdout after su exits.
while (System.nanoTime() < deadline) {
val available = process.inputStream.available()
if (available > 0) {
val size = process.inputStream.read(buffer, 0, minOf(buffer.size, available))
val remaining = outputLimit - captured.size()
if (size > 0 && remaining > 0) captured.write(buffer, 0, minOf(size, remaining))
}
try {
code = process.exitValue()
if (process.inputStream.available() == 0) break
} catch (_: IllegalThreadStateException) {
if (available == 0) Thread.sleep(10)
}
}
} finally {
if (code == -1) process.destroy()
process.inputStream.close()
process.outputStream.close()
process.errorStream.close()
}
return Outcome(code, captured.toString("UTF-8"))
}
}
@@ -2,6 +2,7 @@ package com.v2ray.ang.root
import android.content.Context
import android.os.Process
import android.util.AtomicFile
import com.v2ray.ang.AppConfig
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.handler.SettingsManager
@@ -112,15 +113,25 @@ object RootProxyManager {
val socksUsername = SettingsManager.getSocksUsername()
val socksPassword = SettingsManager.getSocksPassword()
val port = SettingsManager.getSocksPort()
val runDir = File(context.filesDir, AppConfig.ROOT_RUNTIME_DIR).apply { mkdirs() }
val runDir = File(context.filesDir, AppConfig.ROOT_RUNTIME_DIR)
if (!runDir.isDirectory && !runDir.mkdirs()) {
LogUtil.e(AppConfig.TAG, "RootProxyManager: failed to create runtime directory at ${runDir.absolutePath}")
return null
}
val pidFile = File(runDir, "tun2socks.pid").absolutePath
val logFile = File(runDir, "tun2socks.log").absolutePath
val cfgFile = File(runDir, "tun2socks.yml").absolutePath
val cfgFile = File(runDir, "tun2socks.yml")
val oomGuardPid = File(runDir, "oomguard.pid").absolutePath
val ipv6 = MmkvManager.decodeSettingsBool(AppConfig.PREF_IPV6_ENABLED)
val lanShare = forceLanShare || MmkvManager.decodeSettingsBool(AppConfig.PREF_ROOT_LAN_SHARING)
val corePid = Process.myPid()
val config = buildHevConfig(socksUsername, socksPassword, port, ipv6, SettingsManager.getVpnMtu(), lanShare)
if (!writeHevConfig(cfgFile, config)) {
return null
}
val cfgPath = cfgFile.absolutePath
// Per-app proxy/bypass (mirrors what VpnService does via allowed/disallowed apps).
val perAppEnabled = MmkvManager.decodeSettingsBool(AppConfig.PREF_PER_APP_PROXY)
val bypassApps = MmkvManager.decodeSettingsBool(AppConfig.PREF_BYPASS_APPS)
@@ -141,15 +152,9 @@ object RootProxyManager {
appendLine("echo \$! > '$oomGuardPid'")
// tun device node
appendLine("if [ ! -e /dev/net/tun ]; then mkdir -p /dev/net; mknod /dev/net/tun c 10 200; chmod 666 /dev/net/tun; fi")
// hev-socks5-tunnel config: it creates the tun ($TUN) itself and forwards it to the
// in-process core's SOCKS inbound on loopback. MTU comes from the existing VPN MTU
// setting. No fwmark on hev's sockets: its only upstream connection is to 127.0.0.1
// (loopback, already RETURNed by the 127.0.0.0/8 bypass) and the core's real outbound
// runs as the app uid (RETURNed by the uid-owner rule), so traffic can't loop.
appendLine("cat > '$cfgFile' <<'HEVCFG'")
append(buildHevConfig(socksUsername = socksUsername, socksPassword = socksPassword, socksPort = port, ipv6 = ipv6))
appendLine("HEVCFG")
appendLine("nohup \"\$BIN\" '$cfgFile' >'$logFile' 2>&1 &")
// The HEV config is written separately by the app process. Never place credentials
// in this root shell script, even when YAML quoting would otherwise be valid.
appendLine("nohup \"\$BIN\" '$cfgPath' >'$logFile' 2>&1 &")
appendLine("T2S_PID=\$!")
appendLine("echo \$T2S_PID > '$pidFile'")
appendLine("echo ${AppConfig.ROOT_OOM_SCORE} > /proc/\$T2S_PID/oom_score_adj 2>/dev/null || true")
@@ -167,6 +172,12 @@ object RootProxyManager {
// mark the device's own packets into the tun (Root mode only)
if (captureDeviceTraffic) {
append(buildMangleMarking("iptables", appUid, perAppEnabled, bypassApps, selectedUids))
// DNS DNAT: rewrite DNS queries destined for LAN/private resolvers to a
// public IP so the core's routing engine can resolve them via the configured
// remote DNS (dns-out) instead of falling through to a geoip:private → direct
// rule that sends the query back to the LAN resolver (ISP DNS poisoning /
// CDN mis-resolution). Mirrors the tethered-client DNAT in buildLanShareSetup.
append(buildDnsDnat("iptables"))
}
// optionally route hotspot / USB-tethered clients through the tun too
if (lanShare) {
@@ -181,6 +192,7 @@ object RootProxyManager {
appendLine("ip -6 route replace default dev $TUN table $TABLE 2>/dev/null || true")
appendLine("ip -6 rule add fwmark $MARK table $TABLE priority $PRIORITY 2>/dev/null || true")
append(buildMangleMarking("ip6tables", appUid, perAppEnabled, bypassApps, selectedUids))
append(buildDnsDnat("ip6tables"))
} else {
// v6 disabled: blackhole native v6 egress for the captured apps so they
// fall back to v4-through-proxy, matching what a v4-only VpnService does.
@@ -197,14 +209,28 @@ object RootProxyManager {
* tun address when IPv6 is enabled; whether v6 actually flows in is decided separately by
* the v6 route into [TABLE].
*/
private fun buildHevConfig(socksUsername: String?, socksPassword: String?, socksPort: Int, ipv6: Boolean): String {
internal fun buildHevConfig(
socksUsername: String?,
socksPassword: String?,
socksPort: Int,
ipv6: Boolean,
mtu: Int,
lanSharing: Boolean,
): String {
val v4 = AppConfig.ROOT_TUN_ADDR_V4.substringBefore("/")
val v6 = AppConfig.ROOT_TUN_ADDR_V6.substringBefore("/")
return buildString {
appendLine("tunnel:")
appendLine(" name: '$TUN'")
appendLine(" mtu: ${SettingsManager.getVpnMtu()}")
appendLine(" mtu: $mtu")
appendLine(" multi-queue: true")
if (lanSharing) {
// VNET setup or TCP-GSO EINVAL on Android can leave IPv6 packets above the LAN MTU.
// Remove when both fallback forwarding tests pass with jumbo and TSO enabled.
appendLine(" offload:")
appendLine(" jumbo: false")
if (ipv6) appendLine(" tso: false")
}
appendLine(" ipv4: '$v4'")
if (ipv6) appendLine(" ipv6: '$v6'")
appendLine("socks5:")
@@ -212,13 +238,33 @@ object RootProxyManager {
appendLine(" address: '${AppConfig.LOOPBACK}'")
appendLine(" udp: 'udp'")
if (socksUsername != null && socksPassword != null) {
appendLine(" username: '$socksUsername'")
appendLine(" password: '$socksPassword'")
appendLine(" username: ${socksUsername.toSingleQuotedYamlScalar()}")
appendLine(" password: ${socksPassword.toSingleQuotedYamlScalar()}")
}
appendLine(" tcp-fastopen: true")
}
}
private fun writeHevConfig(file: File, config: String): Boolean {
val atomicFile = AtomicFile(file)
val output = try {
atomicFile.startWrite()
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "RootProxyManager: failed to open HEV config at ${file.absolutePath}", e)
return false
}
return try {
output.write(config.toByteArray(Charsets.UTF_8))
atomicFile.finishWrite(output)
true
} catch (e: Exception) {
atomicFile.failWrite(output)
LogUtil.e(AppConfig.TAG, "RootProxyManager: failed to write HEV config at ${file.absolutePath}", e)
false
}
}
/**
* mangle OUTPUT marking chain (ipv4/ipv6). Mirrors VpnService's capture behavior:
* - all-apps (no per-app): mark EVERY remaining uid (incl uid 0 + all system uids), so
@@ -276,6 +322,40 @@ object RootProxyManager {
}
}
/**
* DNAT DNS queries (port 53) destined for LAN/private resolvers to a public IP.
*
* In root mode, DNS is marked into the tun and reaches the core's SOCKS inbound as a
* UDP relay to the original resolver address (e.g. 192.168.1.1:53). The core's routing
* rule `{inboundTag=socks, port=53, outboundTag=dns-out}` should intercept it, but when
* the resolver is a private IP the query also matches a user's `geoip:private → direct`
* routing rule, which sends it straight to the LAN resolver (ISP DNS) — causing DNS
* poisoning, CDN mis-resolution, and the "some sites work, some don't" symptom.
*
* Rewriting the destination to a public IP (the configured remote DNS or 1.1.1.1
* fallback) prevents `geoip:private` from matching, so `dns-out` can resolve via the
* configured remote DNS servers (e.g. Cloudflare DoH) through the proxy.
*
* Uses the same resolver-selection logic as [buildLanShareSetup].
*/
private fun buildDnsDnat(cmd: String): String {
val dnsChain = AppConfig.ROOT_DNS_OUT_CHAIN
val dns = SettingsManager.getRemoteDnsServers()
.firstOrNull { Utils.isPureIpAddress(it) && !it.contains(":") }
?: AppConfig.ROOT_LAN_DNS
val lanCidrs = if (cmd == "ip6tables") bypassCidrsV6 else bypassCidrs
return buildString {
appendLine("$cmd -t nat -N $dnsChain 2>/dev/null || true")
appendLine("$cmd -t nat -F $dnsChain")
lanCidrs.forEach {
appendLine("$cmd -t nat -A $dnsChain -d $it -p udp --dport 53 -j DNAT --to $dns")
appendLine("$cmd -t nat -A $dnsChain -d $it -p tcp --dport 53 -j DNAT --to $dns")
}
appendLine("$cmd -t nat -D OUTPUT -j $dnsChain 2>/dev/null || true")
appendLine("$cmd -t nat -A OUTPUT -j $dnsChain")
}
}
/**
* Blackhole native IPv6 egress for the captured app population when IPv6 is NOT routed
* into the tun. A v4-only VpnService has no v6 route, so the kernel rejects apps' v6 and
@@ -452,6 +532,10 @@ object RootProxyManager {
appendLine("iptables -t nat -D PREROUTING -j ${AppConfig.ROOT_DNS_CHAIN} 2>/dev/null || true")
appendLine("iptables -t nat -F ${AppConfig.ROOT_DNS_CHAIN} 2>/dev/null || true")
appendLine("iptables -t nat -X ${AppConfig.ROOT_DNS_CHAIN} 2>/dev/null || true")
// device DNS DNAT chain (root mode)
appendLine("iptables -t nat -D OUTPUT -j ${AppConfig.ROOT_DNS_OUT_CHAIN} 2>/dev/null || true")
appendLine("iptables -t nat -F ${AppConfig.ROOT_DNS_OUT_CHAIN} 2>/dev/null || true")
appendLine("iptables -t nat -X ${AppConfig.ROOT_DNS_OUT_CHAIN} 2>/dev/null || true")
// IPv6 LAN-sharing chains (forward accept/reject + forwarded-client marking)
appendLine("ip6tables -D FORWARD -j ${AppConfig.ROOT_V6_FWD_CHAIN} 2>/dev/null || true")
appendLine("ip6tables -F ${AppConfig.ROOT_V6_FWD_CHAIN} 2>/dev/null || true")
@@ -473,3 +557,5 @@ object RootProxyManager {
}
}
}
internal fun String.toSingleQuotedYamlScalar(): String = "'${replace("'", "''")}'"
@@ -4,7 +4,6 @@ import android.content.Context
import com.v2ray.ang.AppConfig
import com.v2ray.ang.util.LogUtil
import java.io.File
import java.util.concurrent.TimeUnit
/**
* Minimal root command runner backed by the `su` binary.
@@ -24,32 +23,29 @@ object RootShell {
val dir = File(context.filesDir, AppConfig.ROOT_RUNTIME_DIR).apply { mkdirs() }
val file = File(dir, name).apply {
writeText(script)
setExecutable(true, false)
setReadable(false, false)
setReadable(true, true)
setWritable(false, false)
setWritable(true, true)
}
val safePath = file.absolutePath.replace("'", "'\\''")
return exec("sh '$safePath'")
// Android toybox timeout owns the root shell, so killing the su client cannot leave
// a timed-out setup shell installing rules after rollback has already started.
return exec("timeout -s TERM -k 2 25 sh '$safePath'")
}
private fun exec(command: String, timeoutSeconds: Long = 30): Result {
return try {
val process = ProcessBuilder("su", "-c", command)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().use { it.readText() }
val finished = process.waitFor(timeoutSeconds, TimeUnit.SECONDS)
if (!finished) {
process.destroy()
LogUtil.e(AppConfig.TAG, "RootShell: timed out: $command")
return Result(-1, output)
}
val result = Result(process.exitValue(), output)
val outcome = RootProcessRunner.run(listOf("su", "-c", command), timeoutSeconds * 1000)
val result = Result(outcome.code, outcome.output)
if (!result.success) {
LogUtil.w(AppConfig.TAG, "RootShell: '$command' exited ${result.code}: ${output.trim()}")
LogUtil.w(AppConfig.TAG, "RootShell: root command exited ${result.code}: ${result.output.trim()}")
}
result
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "RootShell: failed to run '$command'", e)
Result(-1, e.message ?: e.javaClass.simpleName)
if (e is InterruptedException) Thread.currentThread().interrupt()
LogUtil.e(AppConfig.TAG, "RootShell: root command failed", e)
Result(-1, e.javaClass.simpleName)
}
}
}
@@ -0,0 +1,92 @@
# Service code agent guide
These rules apply to the service and service-adjacent paths listed in the repository-root
`AGENTS.md`. Follow both guides; apply the precedence rule defined by the root guide.
## Process and lifecycle invariants
- The daemon process `:RunSoLibV2RayDaemon` is the only authority for whether the native
core is running. Do not use a UI-process singleton, cached Boolean, bound-service
assumption, or successful broadcast send as proof of daemon state.
- Route every app-initiated VPN, proxy-only, and root start through `LauncherManager`.
Route service commands and their results through `MessageHelper`. Put lifecycle state
shared by two or more run modes in `CoreServiceManager` or `ServiceControl`. Do not
load or query the native core from the UI process to decide start, stop, or restart.
- Keep Android-owned `VpnService` entry paths intact. `CoreVpnService` must accept a null
restart intent and the `VpnService.SERVICE_INTERFACE` action without requiring the
app-initiated `LauncherManager` path.
- A start path is idempotent only when a second equivalent command leaves exactly one
native core, tunnel, foreground notification update loop, worker set, and root rule
set. Every edit to start dispatch or `onStartCommand` must preserve that result.
- In each `onStartCommand`, enter foreground before reading configuration, opening a
file or socket, starting a coroutine that performs setup, invoking native code, or
executing a root command. `CoreVpnService`, `CoreProxyOnlyService`, and
`CoreRootService` must call `NotificationManager.ensureForeground()`;
`CoreTestService` and `SubscriptionUpdateService` must call
`NotificationHelper.startForeground()`.
- On setup failure, cancel the setup job, release every resource created by that attempt,
remove partial root or VPN state, and stop the failed service instance. Do not return a
sticky restart mode unless `CoreServiceManager.isRunning()` is true, the service owns
every tunnel or root resource required by its run mode, and the Android restart path
reconstructs configuration, native core, notification, and mode-owned resources
without UI-process state.
- Preserve these ownership boundaries: `CoreVpnService` owns the VPN interface and
socket protection; `CoreProxyOnlyService` owns local-proxy mode; `CoreRootService`
owns root routing. Code used by two or more of these services belongs in their common
lifecycle layer. Code used by one mode remains in that mode's service.
## Cleanup, concurrency, networking, and IPC
- Teardown must prevent new work before releasing dependencies. Mark the service as
stopping, reject or invalidate pending start/reload work, cancel and join setup jobs,
then release owned resources. Remove root routing before stopping its core listener.
Close each VPN descriptor and tun2socks resource once on setup failure, revoke, stop,
and destroy; make repeated teardown calls no-ops after the first close.
- Every new coroutine launched by a service must be a child of a job or scope stored by
that service or the shared lifecycle owner. Cancel that owner during stop and
`onDestroy`. Do not use `GlobalScope`, an anonymous standalone `CoroutineScope`, or a
job whose parent outlives the service and later recreates routes, notifications,
files, or native state.
- Do not add root commands, file I/O, network probes, or native calls directly to
`onCreate`, `onStartCommand`, `onRevoke`, or `onDestroy` on the main thread. Perform
the operation in the service-owned scope. If traffic-leak prevention requires the
callback to await completion, the call site must contain an explicit timeout and a
comment naming the leak-prevention ordering; an unbounded wait is prohibited.
- A handover belongs to the `NetworkMonitor` instance that scheduled it.
`NetworkMonitor.unregister()` must cancel pending handover work and prevent its
callback from entering after `unregister()` returns. Immediately before reload, the
handover handler must verify that its monitor instance is still the instance owned by
`CoreServiceManager` and that the core is running. Stop and destroy must unregister
the monitor before clearing it or stopping the core.
- Every app-internal broadcast intent must set its package to `AppConfig.ANG_PACKAGE`;
every app-internal service intent must use an explicit `ComponentName`. Do not add an
implicit broadcast or service intent. Each payload must implement the serialization
contract used by its receiver. When a caller must distinguish `handled by daemon`
from `daemon absent`, return an acknowledgement/result from the daemon. Broadcast
delivery, bind success, and command enqueue success are not acknowledgements.
## Native resources, logging, and validation
- `TProxyService` loads the JNI library `libhev-socks5-tunnel.so`; root mode executes
the separately packaged `libhevsockstun.so`. A native or packaging diff must inspect
the produced APK as a ZIP and confirm both HEV files and `libgojni.so` for every ABI
selected by `ABI_FILTERS` or, when that property is absent, every ABI in the app's
default `splits.abi` block.
- A recoverable start, stop, reload, handover, or cleanup failure log must include the
run mode, lifecycle phase, stable non-secret profile/group identifier when one exists,
failed operation, and exception. Apply the root guide's secret-redaction rule.
- Move deterministic lifecycle decisions into pure helpers and add a JVM regression test
for each changed decision. Apply this scenario mapping to service changes:
- Start dispatch, `onStartCommand`, or command deduplication: cold start and two
equivalent consecutive start commands.
- Foreground setup or setup-error handling: successful setup and failure after at
least one resource has been acquired.
- Stop or teardown: normal stop, repeated stop, and stop while setup is in flight.
- Restart or network handover: successful restart/handover and a stop racing the
pending restart/handover.
- Shared lifecycle code: run the mapped scenarios in VPN, proxy-only, and root modes.
Mode-owned code: run them in the owning mode.
- Every service lifecycle scenario above requires an emulator or physical device; a JVM
test or assembled APK is not a substitute. Record each scenario that did not run under
`Not run`, with the device requirement or blocker. Do not claim the service change is
runtime-verified when any mapped scenario is unrun.
@@ -0,0 +1 @@
@AGENTS.md
@@ -10,6 +10,7 @@ import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.core.CoreNativeManager
import com.v2ray.ang.dto.RealPingEvent
import com.v2ray.ang.dto.RealPingResult
import com.v2ray.ang.dto.TestServiceMessage
import com.v2ray.ang.enums.NotificationChannelType
import com.v2ray.ang.extension.serializable
@@ -19,7 +20,7 @@ import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.helper.MessageHelper
import com.v2ray.ang.helper.NotificationHelper
import com.v2ray.ang.util.LogUtil
import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
class CoreTestService : Service() {
@@ -28,7 +29,7 @@ class CoreTestService : Service() {
}
// manage active batch workers so each batch is independent and cancellable
private val activeWorkers = Collections.synchronizedList(mutableListOf<RealPingWorkerService>())
private val activeWorkers = ConcurrentHashMap<RealPingWorkerService, String>()
private val cancelAction by lazy {
val intent = Intent(this, CoreTestService::class.java).putExtra(
"content",
@@ -69,10 +70,7 @@ class CoreTestService : Service() {
*/
override fun onDestroy() {
LogUtil.i(AppConfig.TAG, "CoreTestService is being destroyed, cancelling ${activeWorkers.size} active workers")
// cancel any active workers
val snapshot = ArrayList(activeWorkers)
snapshot.forEach { it.cancel() }
activeWorkers.clear()
cancelWorkers()
NotificationHelper.stopForeground(this)
super.onDestroy()
}
@@ -99,7 +97,9 @@ class CoreTestService : Service() {
}
when (message.key) {
AppConfig.MSG_MEASURE_CONFIG_START -> handleMeasureStart(message, startId)
AppConfig.MSG_MEASURE_CONFIG_START -> handleMeasureStart(
message, startId, intent.getStringExtra(MessageHelper.EXTRA_REQUEST_ID).orEmpty()
)
AppConfig.MSG_MEASURE_CONFIG_CANCEL -> handleMeasureCancel()
else -> {
NotificationHelper.stopForeground(this); stopSelf(startId)
@@ -108,7 +108,7 @@ class CoreTestService : Service() {
return START_NOT_STICKY
}
private fun handleMeasureStart(message: TestServiceMessage, startId: Int) {
private fun handleMeasureStart(message: TestServiceMessage, startId: Int, requestId: String) {
LogUtil.i(AppConfig.TAG, "CoreTestService starting worker subscription ${message.subscriptionId}")
val guidsList = when {
@@ -123,17 +123,22 @@ class CoreTestService : Service() {
context = this,
guids = guidsList,
onlyTcp = message.onlyTcp,
onEvent = { event -> handleWorkerEvent(event, message) { activeWorkers.remove(worker) } }
onEvent = { event ->
if (activeWorkers.containsKey(worker)) {
handleWorkerEvent(event, message, requestId) { activeWorkers.remove(worker) }
}
}
)
activeWorkers.add(worker)
activeWorkers[worker] = requestId
worker.start()
} else {
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_CANCEL, "", requestId)
NotificationHelper.stopForeground(this)
stopSelf(startId)
}
}
private fun handleWorkerEvent(event: RealPingEvent, message: TestServiceMessage, onWorkerDone: () -> Unit) {
private fun handleWorkerEvent(event: RealPingEvent, message: TestServiceMessage, requestId: String, onWorkerDone: () -> Unit) {
when (event) {
is RealPingEvent.Progress -> {
NotificationHelper.updateNotification(
@@ -142,12 +147,17 @@ class CoreTestService : Service() {
title = getString(R.string.app_name),
content = getString(R.string.connection_running_task_left, event.text)
)
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_NOTIFY, event.text)
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_NOTIFY, event.text, requestId)
}
is RealPingEvent.Result -> {
MmkvManager.encodeServerTestDelayMillis(event.guid, event.delayMillis)
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_SUCCESS, event.guid)
MessageHelper.sendMsg2UI(
this,
AppConfig.MSG_MEASURE_CONFIG_SUCCESS,
RealPingResult(event.guid, event.delayMillis),
requestId,
)
}
is RealPingEvent.Finish -> {
@@ -161,7 +171,7 @@ class CoreTestService : Service() {
}
}
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_FINISH, event.status)
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_FINISH, event.status, requestId)
onWorkerDone()
if (activeWorkers.isEmpty()) {
NotificationHelper.stopForeground(this)
@@ -172,12 +182,19 @@ class CoreTestService : Service() {
}
private fun handleMeasureCancel() {
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_FINISH, "0")
LogUtil.i(AppConfig.TAG, "CoreTestService received cancel message, cancelling ${activeWorkers.size} active workers")
val snapshot = ArrayList(activeWorkers)
snapshot.forEach { it.cancel() }
activeWorkers.clear()
cancelWorkers()
NotificationHelper.stopForeground(this)
stopSelf()
}
private fun cancelWorkers() {
// Reply for each cancelled request, never for a newer UI request that has not started here.
activeWorkers.entries.toList().forEach { (worker, requestId) ->
if (activeWorkers.remove(worker, requestId)) {
worker.cancel()
MessageHelper.sendMsg2UI(this, AppConfig.MSG_MEASURE_CONFIG_CANCEL, "", requestId)
}
}
}
}
@@ -3,12 +3,12 @@ package com.v2ray.ang.service
import android.content.Context
import com.v2ray.ang.AppConfig
import com.v2ray.ang.contracts.IDialerService
import com.v2ray.ang.extension.delay
import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import okhttp3.Call
@@ -8,18 +8,21 @@ import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.v2ray.ang.contracts.IDialerService
import java.util.concurrent.ExecutionException
import java.util.concurrent.FutureTask
import java.util.concurrent.atomic.AtomicLong
class DialerWebviewService : IDialerService {
private var webView: WebView? = null
private val handler = Handler(Looper.getMainLooper())
private val lifecycleVersion = AtomicLong()
private val keepAliveInterval = 30_000L // 30 seconds
private val keepAliveRunnable = object : Runnable {
override fun run() {
webView?.let {
it.resumeTimers()
it.onResume()
}
val view = webView ?: return
view.resumeTimers()
view.onResume()
handler.postDelayed(this, keepAliveInterval)
}
}
@@ -30,45 +33,88 @@ class DialerWebviewService : IDialerService {
* @param context Service context
*/
@SuppressLint("SetJavaScriptEnabled")
override fun start(context: Context, dialerAddr: String) {
if (webView != null) stop()
if (dialerAddr.isEmpty()) return
override fun start(context: Context, dialerAddr: String) = updateOnMainThread {
stopOnMainThread()
if (dialerAddr.isEmpty()) return@updateOnMainThread
val dialerUrl = "http://$dialerAddr/"
webView = WebView(context.applicationContext).apply {
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
// Allow JS to run even if not triggered by user
mediaPlaybackRequiresUserGesture = false
// Prevent aggressive caching issues
cacheMode = WebSettings.LOAD_DEFAULT
}
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
view?.onResume()
view?.resumeTimers()
val view = WebView(context.applicationContext)
webView = view
try {
view.apply {
settings.apply {
javaScriptEnabled = true
domStorageEnabled = true
// Allow JS to run even if not triggered by user
mediaPlaybackRequiresUserGesture = false
// Prevent aggressive caching issues
cacheMode = WebSettings.LOAD_DEFAULT
}
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
if (view === webView) {
view?.onResume()
view?.resumeTimers()
}
}
}
loadUrl(dialerUrl)
}
loadUrl(dialerUrl)
handler.post(keepAliveRunnable)
} catch (e: Exception) {
try {
stopOnMainThread()
} catch (cleanupError: Exception) {
e.addSuppressed(cleanupError)
}
throw e
}
handler.post(keepAliveRunnable)
}
override fun stop() {
override fun stop() = updateOnMainThread { stopOnMainThread() }
private fun stopOnMainThread() {
handler.removeCallbacks(keepAliveRunnable)
webView?.apply {
stopLoading()
pauseTimers()
// Important to call onPause to stop internal Chromium threads properly
onPause()
destroy()
}
val view = webView ?: return
webView = null
view.apply {
try {
stopLoading()
pauseTimers()
// Important to call onPause to stop internal Chromium threads properly
onPause()
} finally {
destroy()
}
}
}
private fun updateOnMainThread(action: () -> Unit) {
val version = lifecycleVersion.incrementAndGet()
val update = {
// A stop or replacement can overtake a worker's queued start on the main thread.
if (lifecycleVersion.get() == version) action()
}
if (Looper.myLooper() == handler.looper) {
update()
return
}
// WebView requires its creating looper on all Android versions. Keep the dialer's
// synchronous contract so reload waits for cleanup/setup and receives any failure.
val task = FutureTask(update)
check(handler.post(task)) { "WebView looper is shutting down" }
try {
task.get()
} catch (e: ExecutionException) {
throw e.cause ?: e
} catch (e: InterruptedException) {
task.cancel(false)
Thread.currentThread().interrupt()
throw e
}
}
}
@@ -0,0 +1 @@
@AGENTS.md
@@ -5,12 +5,12 @@ import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import com.v2ray.ang.AppConfig
import com.v2ray.ang.extension.delay
import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
@@ -18,9 +18,26 @@ import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.isActive
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
internal object RealPingExecutionLimiter {
private val customConfigMutex = Mutex()
suspend fun <T> run(configType: EConfigType, block: () -> T): T {
// Custom profiles bypass speed-test trimming and start complete Xray configs.
// Parallel teardown can abort the native probe process, so serialize their
// JNI measurements globally across batches.
return if (configType == EConfigType.CUSTOM) {
customConfigMutex.withLock { block() }
} else {
block()
}
}
}
/**
* Worker that runs a batch of real-ping tests independently.
* Each batch owns its own CoroutineScope/dispatcher and can be cancelled separately.
@@ -87,7 +104,7 @@ class RealPingWorkerService(
}
}
private fun startRealPing(guid: String): Long {
private suspend fun startRealPing(guid: String): Long {
val retFailure = -1L
val config = MmkvManager.decodeServerConfig(guid) ?: return retFailure
@@ -110,7 +127,9 @@ class RealPingWorkerService(
if (!configResult.status) {
return retFailure
}
return CoreNativeManager.measureOutboundDelay(configResult.content, SettingsManager.getDelayTestUrl())
return RealPingExecutionLimiter.run(config.configType) {
CoreNativeManager.measureOutboundDelay(configResult.content, SettingsManager.getDelayTestUrl())
}
}
private fun startTcping(guid: String): Long {
@@ -120,7 +139,7 @@ class RealPingWorkerService(
if (!config.configType.isComplexType()
&& config.configType != EConfigType.HYSTERIA2
&& config.configType != EConfigType.WIREGUARD
&& config.alpn?.startsWith("h3") != true
&& config.alpn?.split(',')?.all { it.trim().startsWith("h3") } != true
&& config.server.isNotNullEmpty()
&& config.serverPort?.toIntOrNull() != null
) {
@@ -97,7 +97,7 @@ class TProxyService(
appendLine("misc:")
appendLine(" tcp-read-write-timeout: ${tcpTimeout * 1000}")
appendLine(" udp-read-write-timeout: ${udpTimeout * 1000}")
appendLine(" log-level: ${MmkvManager.decodeSettingsString(AppConfig.PREF_HEV_TUNNEL_LOGLEVEL) ?: "warn"}")
appendLine(" log-level: ${MmkvManager.decodeSettingsString(AppConfig.PREF_HEV_TUNNEL_LOGLEVEL, AppConfig.DEFAULT_HEV_TUNNEL_LOGLEVEL)}")
}
}
@@ -0,0 +1,95 @@
# UI code agent guide
These rules apply to the UI paths listed in the repository-root `AGENTS.md`. Follow both
guides; apply the precedence rule defined by the root guide.
## Compose architecture and state
- Implement every new screen and component with the existing Compose and Material 3
stack. A new screen activity must extend `BaseComponentActivity` or
`HelperBaseComponentActivity` and place its screen, ViewModel, and action contract in
the package that owns its navigation entry. Do not add a fragment, ViewBinding layout,
XML screen layout, or RecyclerView adapter. Do not migrate existing legacy UI outside
task scope, and do not extend it or use it as the template for new UI.
- Keep durable screen state and business work in a ViewModel/repository and expose it as
immutable observable state. Collect flows with `collectAsStateWithLifecycle()`.
Compose-local state is only for transient presentation state; use `rememberSaveable`
when that state must survive activity recreation.
- Durable state includes loaded data, selection by domain ID, validation results,
progress, errors, and any value written to settings or a profile. Transient
presentation state is limited to visual expansion, animation, scroll/focus position,
and an uncommitted input value owned solely by the visible component. A composable
must not perform repository, MMKV, import/export, routing, or native-core work.
- Send user operations through the owning feature's action/event contract and
ViewModel. Main-screen operations must use `MainAction` and `MainViewModel`.
Composables must not mutate a repository, `MmkvManager`, or `SettingsManager`
directly.
- Use server GUIDs and group IDs for Compose keys, selection, saved state, and action
parameters. Do not persist or dispatch a visible index, adapter position, or paging
offset as identity. After filtering, sorting, subscription replacement, paging, or an
asynchronous result, resolve the item again by its domain ID.
- Run blocking file, network, package-manager, and bitmap-decoding operations on
`Dispatchers.IO`. Run CPU-bound parsing, sorting, filtering, and transformation on
`Dispatchers.Default`. Launch both from the owning ViewModel or lifecycle scope.
Publish results through a private `MutableStateFlow` exposed as `StateFlow`; do not
read or write Compose snapshot state on either background dispatcher.
## Interaction, accessibility, and layout
- Give every icon with a click, long-click, toggle, or custom accessibility action a
localized accessible name that states its action. Set `contentDescription = null` on
every icon with no user action. Do not expose a raw URL,
package ID, GUID, or duplicated descendant label when a row-level semantic node
supplies the name.
- A row with one activation behavior must expose one focusable semantic node, one
localized name, its current selected/on/off state, and one activation action. Merge or
clear descendant semantics so a label, icon, checkbox, and switch do not become
duplicate focus targets. Keep a descendant as a separate node only when it performs a
different user action; give that node its own name and role. TalkBack, touch, Enter,
Space, and D-pad center must invoke the same action for the same node.
- Key each focusable list item by its domain ID. Reordering or recomposition must keep
focus on the node with that ID if it still exists. If a selection change removes or
replaces the focused node, move focus exactly once to the final active item after the
state update; do not announce or focus an intermediate item.
- Use `stringResource` for text created inside a composable and localized
`Context.getString`/resources outside Compose. This rule covers visible labels,
semantic labels, state descriptions, validation messages, errors, dialogs, snackbars,
and toasts. Apply the root guide's locale and placeholder requirements to every key.
- Every form containing a text-editing control must use a vertically scrollable
container. Inside a `Scaffold`, apply its `innerPadding`, call
`consumeWindowInsets(innerPadding)`, and
apply `imePadding()` to that container. Outside a `Scaffold`, apply `imePadding()` and
consume the navigation-bar inset exactly once. With the IME visible, the user must be
able to focus, read, edit, and activate both the first and last form controls without
dismissing the IME.
- A shared component used by server screens must preserve edge-to-edge system-bar
insets, light and dark themes, dynamic color enabled and disabled, and both single-
and double-column layouts. A change to such a component requires rendering and
interaction checks in each of those configurations; dynamic-color checks require a
device or emulator that supports dynamic color.
## Validation mapping
Apply every row whose trigger matches the UI diff:
- ViewModel state, action mapping, validation, filtering, parsing, or selection logic:
add or update a JVM test that asserts the initial state and every success, invalid-
input, empty-input, and failure branch added or modified by the diff.
- `rememberSaveable`, `SavedStateHandle`, activity recreation, or restoration logic:
recreate the activity and verify the final state by domain ID, not list position.
- Loading or data presentation: exercise every state represented by the owning state
model among empty, loading, error, and populated; do not invent states absent from
that model.
- Click, toggle, selection, or navigation behavior: verify touch, keyboard focus and
activation, and D-pad focus and activation.
- Semantics, focus, accessible text, row merging, or icon meaning: inspect the semantics
tree and run TalkBack on an emulator or device. Record the spoken label, state, focus
order, and activation result.
- Form fields, scrolling, or insets: open the IME, traverse from the first control to the
last control, and activate the primary action with the IME still visible.
- A setting, profile, routing rule, or other persisted value: perform the action, close
and reopen the screen, and verify the value through the owning ViewModel/repository.
A screenshot verifies appearance only. It does not verify semantics, focus, activation,
state ownership, persistence, or recreation. Record every mapped check that did not run
under `Not run`; do not claim the corresponding behavior is verified.
@@ -1,8 +1,10 @@
package com.v2ray.ang.ui
import android.content.Intent
import android.os.Bundle
import android.webkit.WebView
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
@@ -12,7 +14,6 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
@@ -32,6 +33,7 @@ import com.v2ray.ang.R
import com.v2ray.ang.core.CoreNativeManager
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SettingsMenuItem
import com.v2ray.ang.ui.compose.VersionInfoBlock
import com.v2ray.ang.util.Utils
@@ -44,12 +46,20 @@ class AboutActivity : BaseComponentActivity() {
@Composable
override fun ScreenContent() {
AboutScreen(onBackClick = { finish() })
AboutScreen(
onBackClick = { finish() },
onTranslatorsClick = {
startActivity(Intent(this, TranslatorsActivity::class.java))
}
)
}
}
@Composable
fun AboutScreen(onBackClick: () -> Unit) {
fun AboutScreen(
onBackClick: () -> Unit,
onTranslatorsClick: () -> Unit
) {
val context = LocalContext.current
var showOssDialog by remember { mutableStateOf(false) }
@@ -58,7 +68,7 @@ fun AboutScreen(onBackClick: () -> Unit) {
val appIdText = BuildConfig.APPLICATION_ID
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.title_about),
@@ -82,6 +92,11 @@ fun AboutScreen(onBackClick: () -> Unit) {
title = stringResource(R.string.title_oss_license),
onClick = { showOssDialog = true }
)
SettingsMenuItem(
icon = painterResource(R.drawable.ic_translate_24dp),
title = stringResource(R.string.title_translators),
onClick = onTranslatorsClick
)
SettingsMenuItem(
icon = painterResource(R.drawable.ic_feedback_24dp),
title = stringResource(R.string.title_pref_feedback),
@@ -101,6 +116,7 @@ fun AboutScreen(onBackClick: () -> Unit) {
versionText = versionText,
appIdText = appIdText
)
NavigationBarsSpacer()
}
}
@@ -0,0 +1 @@
@AGENTS.md
@@ -0,0 +1 @@
@AGENTS.md
@@ -0,0 +1,155 @@
package com.v2ray.ang.ui
import android.os.Bundle
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import com.v2ray.ang.R
import com.v2ray.ang.dto.TranslatorsCredit
import com.v2ray.ang.dto.TranslatorsParser
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.util.Utils
class TranslatorsActivity : BaseComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
@Composable
override fun ScreenContent() {
TranslatorsScreen(onBackClick = { finish() })
}
}
@Composable
fun TranslatorsScreen(onBackClick: () -> Unit) {
val context = LocalContext.current
val credits = remember {
TranslatorsParser.parse(Utils.readTextFromAssets(context, "translators.json"))
}
Scaffold(
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.title_translators),
onBackClick = onBackClick
)
}
) { innerPadding ->
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
items(credits, key = { it.language }) { credit ->
TranslationCreditCard(credit)
}
item {
NavigationBarsSpacer()
}
}
}
}
@Composable
private fun TranslationCreditCard(credit: TranslatorsCredit) {
Card(
modifier = Modifier.fillMaxWidth(),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceContainerLow
)
) {
Column {
Text(
text = credit.language,
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 12.dp)
)
credit.contributors.forEachIndexed { index, contributor ->
if (index > 0) {
HorizontalDivider(
modifier = Modifier.padding(horizontal = 16.dp),
color = MaterialTheme.colorScheme.outlineVariant,
thickness = 0.5.dp
)
}
ContributorRow(
displayName = contributor.displayName ?: contributor.name,
linkUrl = contributor.url
)
}
}
}
}
@Composable
private fun ContributorRow(displayName: String, linkUrl: String?) {
val context = LocalContext.current
val isLink = linkUrl != null
Row(
modifier = Modifier
.fillMaxWidth()
.then(
if (isLink) {
Modifier.clickable(role = Role.Button) {
Utils.openUri(context, linkUrl)
}
} else {
Modifier
}
)
.padding(horizontal = 16.dp, vertical = 12.dp),
verticalAlignment = Alignment.CenterVertically
) {
if (isLink) {
Icon(
painter = painterResource(R.drawable.ic_github_24dp),
contentDescription = null,
tint = MaterialTheme.colorScheme.primary,
modifier = Modifier.size(20.dp)
)
}
Text(
text = displayName,
style = MaterialTheme.typography.bodyLarge,
color = if (isLink) {
MaterialTheme.colorScheme.primary
} else {
MaterialTheme.colorScheme.onSurface
},
modifier = Modifier.padding(start = if (isLink) 12.dp else 0.dp)
)
}
}
@@ -6,6 +6,7 @@ import android.os.Bundle
import androidx.activity.viewModels
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
@@ -16,7 +17,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -35,6 +35,7 @@ import com.v2ray.ang.ui.compose.AppDropdownMenuItems
import com.v2ray.ang.ui.compose.AppListItem
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.ItemDivider
import com.v2ray.ang.ui.compose.NavigationBarsBottomPadding
import com.v2ray.ang.ui.compose.verticalScrollbar
private enum class AppPickerMenuAction(@StringRes val labelRes: Int) {
@@ -127,7 +128,7 @@ fun AppPickerScreen(
}
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = title,
@@ -184,7 +185,8 @@ fun AppPickerScreen(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.verticalScrollbar(listState)
.verticalScrollbar(listState),
contentPadding = NavigationBarsBottomPadding()
) {
items(items = apps, key = { it.packageName }) { app ->
val checked = selectedPackages.contains(app.packageName)
@@ -7,13 +7,13 @@ import androidx.activity.viewModels
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
@@ -38,8 +38,10 @@ import com.v2ray.ang.extension.toastSuccess
import com.v2ray.ang.handler.SettingsManager
import com.v2ray.ang.ui.base.HelperBaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.InputDialog
import com.v2ray.ang.ui.compose.InputField
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SelectListDialog
import com.v2ray.ang.ui.compose.SettingsMenuItem
import com.v2ray.ang.util.LogUtil
@@ -105,6 +107,7 @@ class BackupActivity : HelperBaseComponentActivity() {
BackupLocation.WebDav -> viewModel.restoreViaWebDav(cacheDir)
}
},
onCleanupProfiles = viewModel::cleanupProfileStorage,
onWebDavSave = { config -> viewModel.saveWebDavConfig(config) },
onBackClick = { finish() }
)
@@ -186,6 +189,7 @@ fun BackupScreen(
onBackupOptionSelected: (BackupLocation) -> Unit,
onShareClick: () -> Unit,
onRestoreOptionSelected: (BackupLocation) -> Unit,
onCleanupProfiles: () -> Unit,
onWebDavSave: (WebDavConfig) -> Unit,
onBackClick: () -> Unit
) {
@@ -193,12 +197,13 @@ fun BackupScreen(
val currentWebDavConfig by webDavConfigState.collectAsState()
var showBackupDialog by remember { mutableStateOf(false) }
var showRestoreDialog by remember { mutableStateOf(false) }
var showCleanupDialog by remember { mutableStateOf(false) }
var showWebDavDialog by remember { mutableStateOf(false) }
val webDavSummary = currentWebDavConfig?.baseUrl
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.title_configuration_backup_restore),
@@ -228,6 +233,12 @@ fun BackupScreen(
title = stringResource(R.string.title_configuration_restore),
onClick = { showRestoreDialog = true }
)
SettingsMenuItem(
icon = painterResource(R.drawable.ic_delete_24dp),
title = stringResource(R.string.title_profile_storage_cleanup),
subtitle = stringResource(R.string.summary_profile_storage_cleanup),
onClick = { showCleanupDialog = true }
)
Spacer(modifier = Modifier.height(16.dp))
SettingsMenuItem(
icon = painterResource(R.drawable.ic_settings_24dp),
@@ -235,6 +246,7 @@ fun BackupScreen(
subtitle = webDavSummary,
onClick = { showWebDavDialog = true }
)
NavigationBarsSpacer()
}
}
@@ -262,6 +274,13 @@ fun BackupScreen(
onDismiss = { showRestoreDialog = false }
)
}
if (showCleanupDialog) {
DeleteConfirmDialog(
message = stringResource(R.string.message_profile_storage_cleanup),
onConfirm = onCleanupProfiles,
onDismiss = { showCleanupDialog = false }
)
}
if (showWebDavDialog) {
WebDavInputDialog(
initialConfig = currentWebDavConfig,
@@ -13,9 +13,11 @@ import com.v2ray.ang.ui.base.BaseViewModel
import com.v2ray.ang.ui.base.ViewModelEvent
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.ZipUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.withContext
import java.io.File
import java.text.SimpleDateFormat
import java.util.Locale
@@ -37,6 +39,24 @@ class BackupViewModel(application: Application) : BaseViewModel(application) {
toastSuccess(R.string.toast_success)
}
fun cleanupProfileStorage() {
launchLoading {
try {
val removed = withContext(Dispatchers.IO) {
MmkvManager.removeOrphanedServerProfiles()
}
if (removed == null) {
toastError(R.string.toast_profile_storage_cleanup_skipped)
} else {
toastSuccess(getString(R.string.toast_profile_storage_cleanup, removed))
}
} catch (e: Exception) {
LogUtil.e(AppConfig.TAG, "Failed to clean up profile storage", e)
toastError(R.string.toast_failure)
}
}
}
fun shareBackup(cacheDir: File, appName: String) {
launchLoading {
val ret = backupConfigurationToCache(cacheDir, appName)
@@ -165,17 +185,20 @@ class BackupViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun performRestore(cacheDir: File, zipFile: File): Boolean {
val backupDir = cacheDir.absolutePath + "/${System.currentTimeMillis()}"
private suspend fun performRestore(cacheDir: File, zipFile: File): Boolean =
withContext(Dispatchers.IO) {
val backupDir = File(cacheDir, "restore_${System.nanoTime()}")
try {
if (!ZipUtil.unzipToFolder(zipFile, backupDir.absolutePath)) {
return@withContext false
}
if (!ZipUtil.unzipToFolder(zipFile, backupDir)) {
return false
val count = MMKV.restoreAllFromDirectory(backupDir.absolutePath)
SettingsChangeManager.makeSetupGroupTab()
SettingsChangeManager.makeRestartService()
count > 0
} finally {
backupDir.deleteRecursively()
}
}
val count = MMKV.restoreAllFromDirectory(backupDir)
SettingsChangeManager.makeSetupGroupTab()
SettingsChangeManager.makeRestartService()
return count > 0
}
}
}
@@ -96,6 +96,13 @@ abstract class BaseViewModel(application: Application) : AndroidViewModel(applic
return localizedContext.getString(resId, *formatArgs)
}
/**
* Get a localized quantity string from a plurals resource.
*/
fun getQuantityString(resId: Int, quantity: Int, vararg formatArgs: Any): String {
return localizedContext.resources.getQuantityString(resId, quantity, *formatArgs)
}
/**
* Send finish activity event.
*/
@@ -3,6 +3,7 @@ package com.v2ray.ang.ui.checkupdate
import android.os.Bundle
import androidx.activity.viewModels
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
@@ -11,7 +12,6 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
@@ -26,6 +26,7 @@ import com.v2ray.ang.R
import com.v2ray.ang.core.CoreNativeManager
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SettingsMenuItem
import com.v2ray.ang.ui.compose.SettingsSwitchItem
import com.v2ray.ang.ui.compose.VersionInfoBlock
@@ -65,7 +66,7 @@ fun CheckUpdateScreen(
val versionText = "v${BuildConfig.VERSION_NAME} ($libVersion)"
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.update_check_for_update),
@@ -92,6 +93,7 @@ fun CheckUpdateScreen(
onClick = { viewModel.checkForUpdates() }
)
VersionInfoBlock(versionText = versionText)
NavigationBarsSpacer()
}
}
@@ -32,7 +32,6 @@ class CheckUpdateViewModel(application: Application) : BaseViewModel(application
fun checkForUpdates() {
launchLoading {
toast(R.string.update_checking_for_update)
try {
val result = UpdateCheckerManager.checkForUpdate(_checkPreRelease.value)
if (result.hasUpdate) {
@@ -6,14 +6,19 @@ import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.windowInsetsBottomHeight
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.text.selection.TextSelectionColors
import androidx.compose.material3.Checkbox
@@ -50,8 +55,8 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.AsyncImage
import coil.request.ImageRequest
import coil3.compose.AsyncImage
import coil3.request.ImageRequest
import com.v2ray.ang.R
import com.v2ray.ang.util.AppIconFetcher
import sh.calvin.reorderable.ReorderableCollectionItemScope
@@ -225,6 +230,17 @@ fun AppDivider(modifier: Modifier = Modifier) {
HorizontalDivider(modifier = modifier.fillMaxWidth(), thickness = 1.dp, color = color)
}
@Composable
fun NavigationBarsSpacer(modifier: Modifier = Modifier) {
Spacer(modifier = modifier.windowInsetsBottomHeight(WindowInsets.navigationBars))
}
@Composable
fun NavigationBarsBottomPadding(): PaddingValues {
val bottom = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()
return PaddingValues(bottom = bottom)
}
@Composable
fun VersionInfoBlock(
versionText: String,
@@ -37,6 +37,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.v2ray.ang.R
@@ -47,6 +48,7 @@ fun ConfirmDialog(
confirmText: String = stringResource(R.string.action_ok),
dismissText: String? = stringResource(R.string.action_cancel),
confirmIcon: @Composable (() -> Unit)? = null,
messageTextAlign: TextAlign = TextAlign.Start,
onConfirm: () -> Unit,
onDismiss: () -> Unit
) {
@@ -61,7 +63,14 @@ fun ConfirmDialog(
AlertDialog(
onDismissRequest = onDismiss,
title = title?.let { { Text(it) } },
text = { Text(message, style = MaterialTheme.typography.bodyMedium) },
text = {
Text(
text = message,
modifier = Modifier.fillMaxWidth(),
style = MaterialTheme.typography.bodyMedium,
textAlign = messageTextAlign
)
},
confirmButton = {
TextButton(
onClick = { onConfirm(); onDismiss() },
@@ -91,9 +100,11 @@ fun DeleteConfirmDialog(
message: String,
onConfirm: () -> Unit,
onDismiss: () -> Unit,
itemName: String? = null,
) {
ConfirmDialog(
message = message,
message = if (itemName.isNullOrBlank()) message else "$message\n\n$itemName",
messageTextAlign = TextAlign.Center,
confirmText = stringResource(R.string.action_delete),
confirmIcon = {
Icon(
@@ -38,6 +38,8 @@ fun FormTextField(
keyboardType: KeyboardType = KeyboardType.Text,
placeholder: String? = null,
maxLines: Int = 5,
isError: Boolean = false,
supportingText: String? = null,
) {
Box(
modifier = modifier
@@ -49,6 +51,8 @@ fun FormTextField(
onValueChange = onValueChange,
label = { Text(label) },
placeholder = placeholder?.let { { Text(it) } },
isError = isError,
supportingText = supportingText?.let { { Text(it) } },
singleLine = false,
maxLines = maxLines,
enabled = enabled,
@@ -26,7 +26,7 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.compose.currentStateAsState
import kotlinx.coroutines.delay
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.flow.collectLatest
data class ScrollbarConfig(
@@ -31,13 +31,12 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.compose.LocalLifecycleOwner
import androidx.lifecycle.repeatOnLifecycle
import com.v2ray.ang.extension.delay
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlin.time.Duration.Companion.milliseconds
enum class ToastType {
NORMAL, SUCCESS, ERROR, INFO
@@ -88,7 +87,7 @@ class AppSnackbarController(
if (currentShowTime != 0L) {
val elapsed = System.currentTimeMillis() - currentShowTime
if (elapsed < SnackbarThrottleMs) {
delay((SnackbarThrottleMs - elapsed).milliseconds)
delay((SnackbarThrottleMs - elapsed))
}
}
@@ -111,12 +111,12 @@ val colorFabInactiveDark = Color(0xFF646464) // Dark Gray
val dividerColorLight = Color(0xFFE0E0E0) // Light Gray
val dividerColorDark = Color(0xFF424242) // Dark Gray
// Toast Colors 70%
val toastNormalBgLight = Color(0xB3353A3E) // Dark Gray
val toastNormalBgDark = Color(0xB34A4F54) // Darker Gray
val toastSuccessBg = Color(0xB3388E3C) // Green
val toastErrorBg = Color(0xB3D50000) // Red
val toastInfoBg = Color(0xB33F51B5) // Indigo Blue
// Toast Colors 85%
val toastNormalBgLight = Color(0xD9353A3E) // Dark Gray
val toastNormalBgDark = Color(0xD94A4F54) // Darker Gray
val toastSuccessBg = Color(0xD9388E3C) // Green
val toastErrorBg = Color(0xD9D50000) // Red
val toastInfoBg = Color(0xD93F51B5) // Indigo Blue
val toastIconCircleBg = Color(0x33FFFFFF) // Semi-transparent White
val toastTextColor = Color.White // White
@@ -8,12 +8,14 @@ import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FloatingActionButton
@@ -21,7 +23,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
@@ -43,6 +44,7 @@ import com.v2ray.ang.extension.toastError
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.ItemDivider
import com.v2ray.ang.ui.compose.NavigationBarsBottomPadding
import com.v2ray.ang.ui.compose.verticalScrollbar
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
@@ -130,7 +132,7 @@ fun LogcatScreen(
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val logs by viewModel.filteredLogs.collectAsStateWithLifecycle()
val logs by viewModel.logEntries.collectAsStateWithLifecycle()
val isLoading by viewModel.isLoading.collectAsStateWithLifecycle()
var searchQuery by remember { mutableStateOf("") }
@@ -139,7 +141,7 @@ fun LogcatScreen(
val listState = rememberLazyListState()
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.title_logcat),
@@ -190,9 +192,10 @@ fun LogcatScreen(
)
},
floatingActionButton = {
FloatingActionButton(onClick = {
viewModel.loadLogcat()
}) {
FloatingActionButton(
onClick = { viewModel.loadLogcat() },
modifier = Modifier.navigationBarsPadding()
) {
Icon(
painterResource(R.drawable.ic_restore_24dp),
contentDescription = stringResource(R.string.acc_refresh)
@@ -209,10 +212,11 @@ fun LogcatScreen(
state = listState,
modifier = Modifier
.fillMaxSize()
.verticalScrollbar(listState)
.verticalScrollbar(listState),
contentPadding = NavigationBarsBottomPadding()
) {
itemsIndexed(items = logs, key = { index, _ -> index }) { _, log ->
LogcatItem(log = log, onLongClick = { Utils.setClipboard(context, log) })
items(items = logs, key = { it.key }) { log ->
LogcatItem(log = log.text, onLongClick = { Utils.setClipboard(context, log.text) })
ItemDivider()
}
}
@@ -1,14 +1,20 @@
package com.v2ray.ang.ui.logcat
import android.app.Application
import androidx.lifecycle.viewModelScope
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.ui.base.BaseViewModel
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import java.io.IOException
class LogcatViewModel(application: Application) : BaseViewModel(application) {
@@ -17,6 +23,10 @@ class LogcatViewModel(application: Application) : BaseViewModel(application) {
private val _filteredLogs = MutableStateFlow<List<String>>(emptyList())
val filteredLogs: StateFlow<List<String>> = _filteredLogs.asStateFlow()
val logEntries: StateFlow<List<LogcatEntry>> = filteredLogs
.map(::createLogcatEntries)
.flowOn(Dispatchers.Default)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
fun loadLogcat() {
launchLoading {
@@ -73,4 +83,17 @@ class LogcatViewModel(application: Application) : BaseViewModel(application) {
logsetsAll.filter { it.contains(currentFilter) }
}
}
}
}
data class LogcatEntry(val key: String, val text: String)
internal fun createLogcatEntries(logs: List<String>): List<LogcatEntry> {
val occurrences = HashMap<String, Int>()
// Identical lines are valid. Count oldest-first so filtering other lines or
// prepending newer entries does not change the keys of existing occurrences.
return logs.asReversed().map { line ->
val occurrence = occurrences.getOrDefault(line, 0)
occurrences[line] = occurrence + 1
LogcatEntry("$occurrence:$line", line)
}.asReversed()
}
@@ -24,6 +24,7 @@ import com.v2ray.ang.handler.AngConfigManager
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.handler.SettingsChangeManager
import com.v2ray.ang.handler.SettingsManager
import com.v2ray.ang.handler.SubscriptionInfoParser
import com.v2ray.ang.ui.AboutActivity
import com.v2ray.ang.ui.backup.BackupActivity
import com.v2ray.ang.ui.base.HelperBaseComponentActivity
@@ -75,10 +76,10 @@ class MainActivity : HelperBaseComponentActivity() {
val restartService = data.getBooleanExtra(
ProfileEditorResult.EXTRA_RESTART_SERVICE, false
)
val selectedProfileSaved = action == ProfileEditorResult.ACTION_SAVED &&
data.getStringExtra(ProfileEditorResult.EXTRA_GUID) == mainViewModel.uiState.value.selectedGuid
mainViewModel.onAction(MainAction.RefreshGroups)
if (restartService && mainViewModel.uiState.value.isRunning) {
restartV2Ray()
}
if (restartService || selectedProfileSaved) LauncherManager.restartService(this)
}
private val settingsActivityLauncher =
@@ -87,7 +88,7 @@ class MainActivity : HelperBaseComponentActivity() {
val refreshGroups = SettingsChangeManager.consumeSetupGroupTab()
mainViewModel.refreshUiSettings()
if (refreshGroups) mainViewModel.onAction(MainAction.RefreshGroups)
if (restartService && mainViewModel.uiState.value.isRunning) restartV2Ray()
if (restartService) LauncherManager.restartService(this)
}
override fun onCreate(savedInstanceState: Bundle?) {
@@ -110,12 +111,13 @@ class MainActivity : HelperBaseComponentActivity() {
MainAction.ImportClipboard -> importClipboard()
MainAction.ImportConfigLocal -> importConfigLocal()
is MainAction.ImportManually -> importManually(action.type)
MainAction.RestartService -> restartV2Ray()
MainAction.RestartService -> LauncherManager.restartServiceOrStart(this, ::requestServiceStart)
MainAction.LocateSelectedServer -> mainViewModel.triggerLocateSelectedServer()
is MainAction.SelectServer -> setSelectServer(action.guid)
is MainAction.EditServer -> editServer(action.guid, action.profile)
is MainAction.ShareClipboard -> shareToClipboard(action.guid)
is MainAction.ShareFullContent -> shareFullContentAsync(action.guid)
is MainAction.OpenSubscriptionLink -> openSubscriptionLink(action.url)
else -> mainViewModel.onAction(action)
}
},
@@ -136,6 +138,12 @@ class MainActivity : HelperBaseComponentActivity() {
}
}
private fun openSubscriptionLink(url: String) {
// Links come from a remote panel, so only http(s) and tg:// are ever handed to ACTION_VIEW.
val safeUrl = SubscriptionInfoParser.sanitizeLink(url) ?: return
Utils.openUri(this, safeUrl)
}
private fun navigateTo(destination: MainDestination) {
val intent = when (destination) {
MainDestination.Subscriptions -> Intent(this, SubSettingActivity::class.java)
@@ -161,14 +169,20 @@ class MainActivity : HelperBaseComponentActivity() {
private fun handleFabAction() {
if (mainViewModel.uiState.value.isRunning) {
LauncherManager.stopService(this)
} else if (SettingsManager.isVpnMode()) {
val intent = VpnService.prepare(this)
if (intent == null) startV2Ray() else requestVpnPermission.launch(intent)
} else {
startV2Ray()
requestServiceStart()
}
}
private fun requestServiceStart() {
if (!SettingsManager.isVpnMode()) {
startV2Ray()
return
}
val intent = VpnService.prepare(this)
if (intent == null) startV2Ray() else requestVpnPermission.launch(intent)
}
private fun handleLayoutTestClick() {
if (mainViewModel.uiState.value.isRunning) {
mainViewModel.testCurrentServerRealPing()
@@ -180,22 +194,13 @@ class MainActivity : HelperBaseComponentActivity() {
toast(R.string.title_file_chooser)
return
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CINNAMON_BUN &&
MmkvManager.decodeSettingsBool(AppConfig.PREF_PROXY_SHARING)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CINNAMON_BUN
) {
checkAndRequestPermission(PermissionType.ACCESS_LOCAL_NETWORK) {}
}
LauncherManager.startService(this)
}
private fun restartV2Ray() {
if (mainViewModel.uiState.value.isRunning) LauncherManager.stopService(this)
lifecycleScope.launch {
kotlinx.coroutines.delay(500)
startV2Ray()
}
}
private fun importManually(createConfigType: Int) {
val intent = when (createConfigType) {
EConfigType.POLICYGROUP.value -> Intent(this, ServerGroupActivity::class.java)
@@ -275,7 +280,7 @@ class MainActivity : HelperBaseComponentActivity() {
val selected = mainViewModel.uiState.value.selectedGuid
if (guid != selected) {
mainViewModel.updateSelectedGuid(guid)
if (mainViewModel.uiState.value.isRunning) restartV2Ray()
LauncherManager.restartService(this)
}
}
@@ -1,5 +1,8 @@
package com.v2ray.ang.ui.main
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
@@ -17,20 +20,26 @@ import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.v2ray.ang.R
import com.v2ray.ang.ui.compose.AppDivider
import com.v2ray.ang.ui.compose.colorFabActive
import com.v2ray.ang.ui.compose.colorFabInactiveDark
import com.v2ray.ang.ui.compose.colorFabInactiveLight
import kotlinx.coroutines.launch
@Composable
fun MainBottomBar(
@@ -39,31 +48,53 @@ fun MainBottomBar(
isDarkTheme: Boolean,
onAction: (MainAction) -> Unit
) {
val scope = rememberCoroutineScope()
val rotationAnim = remember { Animatable(0f) }
LaunchedEffect(isRunning) {
if (!isRunning) {
rotationAnim.snapTo(0f)
}
}
Box(modifier = Modifier.fillMaxWidth()) {
Column(modifier = Modifier.fillMaxWidth()) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.surface)
.clickable(onClick = { onAction(MainAction.TestCurrentServer) })
.windowInsetsPadding(WindowInsets.navigationBars)
) {
AppDivider()
Surface(
Row(
modifier = Modifier
.fillMaxWidth()
.windowInsetsPadding(WindowInsets.navigationBars)
.height(64.dp)
.clickable(onClick = { onAction(MainAction.TestCurrentServer) }),
color = MaterialTheme.colorScheme.surface,
tonalElevation = 0.dp
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(text = displayText, style = MaterialTheme.typography.bodyMedium)
}
Text(
text = displayText,
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.semantics {
contentDescription = displayText
}
)
}
}
FloatingActionButton(
onClick = { onAction(MainAction.ToggleService) },
onClick = {
if (!isRunning) {
scope.launch {
rotationAnim.animateTo(
targetValue = 720f,
animationSpec = tween(durationMillis = 3000)
)
}
}
onAction(MainAction.ToggleService)
},
modifier = Modifier
.align(Alignment.TopEnd)
.padding(end = 24.dp)
@@ -80,7 +111,9 @@ fun MainBottomBar(
if (isRunning) R.string.acc_stop else R.string.acc_start
),
tint = Color.White,
modifier = Modifier.size(24.dp)
modifier = Modifier
.size(24.dp)
.graphicsLayer { rotationZ = rotationAnim.value }
)
}
}
@@ -45,6 +45,7 @@ sealed interface MainAction {
data object RemoveInvalidServers : MainAction
data object SortByTestResults : MainAction
data object UpdateSubscriptions : MainAction
data class UpdateGroupSubscription(val groupId: String) : MainAction
data object ExportAll : MainAction
data object ImportQRcode : MainAction
@@ -63,8 +64,9 @@ sealed interface MainAction {
data class ShareClipboard(val guid: String) : MainAction
data class ShareFullContent(val guid: String) : MainAction
data object DismissQRCodeDialog : MainAction
data class OpenSubscriptionLink(val url: String) : MainAction
data class ImportBatchConfig(val configText: String) : MainAction
data class LocateHandled(val target: LocateTarget) : MainAction
data object LocateHandled : MainAction
}
@@ -57,9 +57,9 @@ interface MainDataSource : Closeable {
fun share2Clipboard(guid: String): Boolean
fun sendMsg2Service(msgId: Int, content: String)
fun sendMsg2TestService(msg: TestServiceMessage)
fun sendMsg2TestService(msg: TestServiceMessage, requestId: String? = null)
fun cancelAllPing()
fun testCurrentServerRealPing()
fun testCurrentServerRealPing(requestId: String)
fun syncSubscriptions()
fun initAssets()
@@ -1,10 +1,23 @@
package com.v2ray.ang.ui.main
import androidx.compose.runtime.Composable
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.ui.res.stringResource
import com.v2ray.ang.R
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
data class ServerDeleteTarget(
val guid: String,
val profileName: String,
) {
companion object {
val Saver = listSaver<ServerDeleteTarget?, String>(
save = { target -> target?.let { listOf(it.guid, it.profileName) }.orEmpty() },
restore = { ServerDeleteTarget(it[0], it[1]) },
)
}
}
@Composable
fun MainDialogs(
showDelAllConfirm: Boolean,
@@ -16,7 +29,7 @@ fun MainDialogs(
showDelInvalidConfirm: Boolean,
onDismissDelInvalid: () -> Unit,
onConfirmDelInvalid: () -> Unit,
showRemoveConfirm: String?,
showRemoveConfirm: ServerDeleteTarget?,
onDismissRemove: () -> Unit,
onConfirmRemove: (String) -> Unit,
) {
@@ -41,11 +54,11 @@ fun MainDialogs(
onDismiss = onDismissDelInvalid
)
}
if (showRemoveConfirm != null) {
val guid = showRemoveConfirm
showRemoveConfirm?.let { target ->
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_profile),
onConfirm = { onConfirmRemove(guid) },
itemName = target.profileName,
onConfirm = { onConfirmRemove(target.guid) },
onDismiss = onDismissRemove
)
}
@@ -1,25 +1,68 @@
package com.v2ray.ang.ui.main
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.relocation.BringIntoViewRequester
import androidx.compose.foundation.relocation.bringIntoViewRequester
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.PrimaryScrollableTabRow
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRowDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Constraints
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.v2ray.ang.dto.GroupMapItem
import com.v2ray.ang.dto.entities.ServersCache
import kotlinx.coroutines.flow.StateFlow
private val TAB_BAR_PADDING = 3.dp
private val TAB_SPACING = 2.dp
private val TAB_HEIGHT = 30.dp
private val SELECTED_TAB_MARGIN = 12.dp
/**
* Subscription group tabs: a floating rounded bar whose selected tab is a tinted pill and whose
* server count is a small badge.
*
* While all tabs fit, they share the bar width equally (like Telegram's folder tabs); once they
* no longer fit, they keep their natural width and the bar scrolls horizontally, keeping the
* selected tab in view.
*/
@Composable
fun GroupTabBar(
groups: List<GroupMapItem>,
@@ -28,62 +71,155 @@ fun GroupTabBar(
onTabClick: (Int) -> Unit,
modifier: Modifier = Modifier
) {
PrimaryScrollableTabRow(
selectedTabIndex = selectedTabIndex.coerceIn(0, groups.lastIndex),
modifier = modifier.fillMaxWidth(),
containerColor = MaterialTheme.colorScheme.surface.copy(alpha = 0.95f),
contentColor = MaterialTheme.colorScheme.onSurface,
edgePadding = 16.dp,
minTabWidth = 56.dp,
indicator = {
TabRowDefaults.PrimaryIndicator(
modifier = Modifier
.tabIndicatorOffset(
selectedTabIndex = selectedTabIndex.coerceIn(0, groups.lastIndex),
matchContentSize = true
)
.clip(RoundedCornerShape(3.dp)),
width = Dp.Unspecified,
color = MaterialTheme.colorScheme.secondary
)
},
divider = {}
val selectedIndex = selectedTabIndex.coerceIn(0, groups.lastIndex)
val colors = MaterialTheme.colorScheme
// The plain surface color equals the screen background in dark mode, so the bar needs its own tone there.
val dark = colors.surface.luminance() < 0.5f
Surface(
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 6.dp),
shape = CircleShape,
color = if (dark) colors.surfaceContainerHigh else colors.surface,
border = BorderStroke(0.5.dp, colors.outlineVariant.copy(alpha = if (dark) 0.6f else 0.35f)),
shadowElevation = 3.dp
) {
groups.forEachIndexed { index, group ->
GroupTabItem(
group = group,
selected = index == selectedTabIndex,
serverFlowProvider = { mainViewModel.serversForGroup(group.id) },
onClick = { onTabClick(index) }
)
BoxWithConstraints {
val barPaddingPx = with(LocalDensity.current) { TAB_BAR_PADDING.roundToPx() }
val availablePx = constraints.maxWidth - 2 * barPaddingPx
Row(
Modifier
.horizontalScroll(rememberScrollState())
.padding(TAB_BAR_PADDING)
) {
EqualOrNaturalTabsLayout(availablePx = availablePx) {
groups.forEachIndexed { index, group ->
key(group.id) {
val serverFlow = remember(group.id, mainViewModel) {
mainViewModel.serversForGroup(group.id)
}
GroupTabItem(
group = group,
selected = index == selectedIndex,
serverFlow = serverFlow,
onClick = { onTabClick(index) }
)
}
}
}
}
}
}
}
/**
* Lays the tabs out in a row. If every tab fits in an equal share of [availablePx] they all get
* that share; otherwise, if they still fit together, the spare space is spread evenly; otherwise
* they keep their natural width (the parent scrolls).
*/
@Composable
private fun EqualOrNaturalTabsLayout(
availablePx: Int,
content: @Composable () -> Unit
) {
Layout(content = content) { measurables, _ ->
if (measurables.isEmpty()) return@Layout layout(0, 0) {}
val spacing = TAB_SPACING.roundToPx()
val tabHeight = TAB_HEIGHT.roundToPx()
val count = measurables.size
val gaps = spacing * (count - 1)
val natural = measurables.map { it.maxIntrinsicWidth(tabHeight) }
val equalShare = (availablePx - gaps) / count
val widths = when {
availablePx <= 0 -> natural
natural.all { it <= equalShare } -> List(count) { equalShare }
natural.sum() + gaps <= availablePx -> {
val extra = (availablePx - gaps - natural.sum()) / count
natural.map { it + extra }
}
else -> natural
}
val placeables = measurables.mapIndexed { i, measurable ->
measurable.measure(Constraints.fixed(widths[i], tabHeight))
}
layout(widths.sum() + gaps, tabHeight) {
var x = 0
placeables.forEach { placeable ->
placeable.placeRelative(x, 0)
x += placeable.width + spacing
}
}
}
}
@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun GroupTabItem(
group: GroupMapItem,
selected: Boolean,
serverFlowProvider: () -> StateFlow<List<ServersCache>>,
serverFlow: StateFlow<List<ServersCache>>,
onClick: () -> Unit
) {
val serverFlow = remember(group.id) { serverFlowProvider() }
val servers by serverFlow.collectAsStateWithLifecycle()
Tab(
selected = selected,
onClick = onClick,
text = {
val text = if (group.id.isEmpty()) {
group.remarks
} else {
"${group.remarks} (${servers.size})"
}
Text(
text = text,
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis
// The default group (empty id) never showed a count.
val count = if (group.id.isEmpty()) 0 else servers.size
val colors = MaterialTheme.colorScheme
val bringIntoView = remember { BringIntoViewRequester() }
var size by remember { mutableStateOf(IntSize.Zero) }
val marginPx = with(LocalDensity.current) { SELECTED_TAB_MARGIN.toPx() }
// Bring the tab in with some room around it so the pill never ends up flush against the bar's
// rounded edge; at the first and last tab the scroll stops at the bar's own inner padding.
LaunchedEffect(selected, size) {
if (selected && size != IntSize.Zero) {
bringIntoView.bringIntoView(
Rect(-marginPx, 0f, size.width + marginPx, size.height.toFloat())
)
}
)
}
// One selectable node: name, count and selected state are announced together.
Row(
modifier = Modifier
.bringIntoViewRequester(bringIntoView)
.onSizeChanged { size = it }
.clip(CircleShape)
.background(if (selected) colors.primary.copy(alpha = 0.12f) else Color.Transparent)
.selectable(selected = selected, role = Role.Tab, onClick = onClick)
.padding(horizontal = 12.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = group.remarks,
style = MaterialTheme.typography.bodySmall,
fontWeight = FontWeight.Medium,
color = if (selected) colors.primary else colors.onSurfaceVariant,
maxLines = 1,
softWrap = false,
overflow = TextOverflow.Ellipsis
)
if (count > 0) {
Spacer(Modifier.width(3.dp))
Box(
modifier = Modifier
.defaultMinSize(minWidth = 16.dp, minHeight = 16.dp)
.clip(CircleShape)
.background(if (selected) colors.primary else colors.onSurfaceVariant.copy(alpha = 0.55f))
.padding(horizontal = 4.dp),
contentAlignment = Alignment.Center
) {
Text(
text = count.toString(),
style = MaterialTheme.typography.labelSmall.copy(fontSize = 10.sp, lineHeight = 12.sp),
color = if (selected) colors.onPrimary else colors.surface,
maxLines = 1
)
}
}
}
}
@@ -79,7 +79,7 @@ fun ShareMethodDialog(
more: Boolean,
onDismiss: () -> Unit,
onAction: (MainAction) -> Unit,
onRemove: (String) -> Unit,
onRemove: (String, String) -> Unit,
) {
val menuActions = serverMenuActions(
isComplexProfile = profile.configType.isComplexType(),
@@ -95,7 +95,7 @@ fun ShareMethodDialog(
ServerMenuAction.ShareClipboard -> onAction(MainAction.ShareClipboard(guid))
ServerMenuAction.ShareFullContent -> onAction(MainAction.ShareFullContent(guid))
ServerMenuAction.Edit -> onAction(MainAction.EditServer(guid, profile))
ServerMenuAction.Delete -> onRemove(guid)
ServerMenuAction.Delete -> onRemove(guid, profile.remarks)
}
},
onDismiss = onDismiss
@@ -9,6 +9,7 @@ import com.v2ray.ang.AngApplication
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.ConnectionTestResult
import com.v2ray.ang.dto.RealPingResult
import com.v2ray.ang.dto.SubscriptionUpdateResult
import com.v2ray.ang.dto.TestServiceMessage
import com.v2ray.ang.dto.entities.ProfileItem
@@ -24,10 +25,9 @@ import com.v2ray.ang.handler.SubscriptionUpdater
import com.v2ray.ang.helper.MessageHelper
import com.v2ray.ang.util.LogUtil
import com.v2ray.ang.util.Utils
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.receiveAsFlow
import java.util.concurrent.atomic.AtomicBoolean
class MainRepository(
@@ -39,40 +39,44 @@ class MainRepository(
private val closed = AtomicBoolean(false)
private val _mainServiceEvent = MutableSharedFlow<MainServiceEvent>(
replay = 0,
extraBufferCapacity = 64,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
// Probe results are finite and must remain lossless until the ViewModel coalesces them.
private val mainServiceEventChannel = Channel<MainServiceEvent>(Channel.UNLIMITED)
override val mainServiceEvent: SharedFlow<MainServiceEvent> = _mainServiceEvent.asSharedFlow()
override val mainServiceEvent: Flow<MainServiceEvent> = mainServiceEventChannel.receiveAsFlow()
private val serviceReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val safeIntent = intent ?: return
val requestId = safeIntent.getStringExtra(MessageHelper.EXTRA_REQUEST_ID).orEmpty()
val event = when (safeIntent.getIntExtra("key", 0)) {
AppConfig.MSG_STATE_RUNNING -> MainServiceEvent.StateRunning
AppConfig.MSG_STATE_NOT_RUNNING -> MainServiceEvent.StateNotRunning
AppConfig.MSG_STATE_START_SUCCESS -> MainServiceEvent.StateStartSuccess
AppConfig.MSG_STATE_START_FAILURE -> MainServiceEvent.StateStartFailure
AppConfig.MSG_STATE_START_FAILURE -> MainServiceEvent.StateStartFailure(
safeIntent.getStringExtra("content")
)
AppConfig.MSG_STATE_STOP_SUCCESS -> MainServiceEvent.StateStopSuccess
AppConfig.MSG_MEASURE_DELAY_RESULT -> safeIntent
.serializable<ConnectionTestResult>("content")
?.let { MainServiceEvent.MeasureDelayResult(it) }
?.let { MainServiceEvent.MeasureDelayResult(it, requestId) }
AppConfig.MSG_MEASURE_DELAY_CANCEL -> MainServiceEvent.MeasureDelayCancelled(requestId)
AppConfig.MSG_MEASURE_CONFIG_SUCCESS -> MainServiceEvent.MeasureConfigSuccess
AppConfig.MSG_MEASURE_CONFIG_SUCCESS -> safeIntent
.serializable<RealPingResult>("content")
?.let { MainServiceEvent.MeasureConfigSuccess(it, requestId) }
AppConfig.MSG_MEASURE_CONFIG_NOTIFY -> MainServiceEvent.MeasureConfigNotify(
safeIntent.getStringExtra("content").orEmpty()
safeIntent.getStringExtra("content").orEmpty(), requestId
)
AppConfig.MSG_MEASURE_CONFIG_FINISH -> MainServiceEvent.MeasureConfigFinish(
safeIntent.getStringExtra("content")
requestId
)
AppConfig.MSG_MEASURE_CONFIG_CANCEL -> MainServiceEvent.MeasureConfigCancelled(requestId)
else -> null
}
event?.let { _mainServiceEvent.tryEmit(it) }
event?.let { mainServiceEventChannel.trySend(it) }
}
}
@@ -98,6 +102,7 @@ class MainRepository(
}.onFailure {
LogUtil.e(AppConfig.TAG, "Failed to unregister main service receiver", it)
}
mainServiceEventChannel.close()
}
override fun getSelectedSubscriptionId(): String =
@@ -205,8 +210,8 @@ class MainRepository(
override fun sendMsg2Service(msgId: Int, content: String) =
MessageHelper.sendMsg2Service(app, msgId, content)
override fun sendMsg2TestService(msg: TestServiceMessage) =
MessageHelper.sendMsg2TestService(app, msg)
override fun sendMsg2TestService(msg: TestServiceMessage, requestId: String?) =
MessageHelper.sendMsg2TestService(app, msg, requestId)
override fun cancelAllPing() {
sendMsg2TestService(
@@ -214,8 +219,10 @@ class MainRepository(
)
}
override fun testCurrentServerRealPing() {
sendMsg2Service(AppConfig.MSG_MEASURE_DELAY, "")
override fun testCurrentServerRealPing(requestId: String) {
MessageHelper.sendMsg2ServiceForResult(app, AppConfig.MSG_MEASURE_DELAY, requestId) { handled ->
if (!handled) mainServiceEventChannel.trySend(MainServiceEvent.MeasureDelayCancelled(requestId))
}
}
override fun syncSubscriptions() {
@@ -21,6 +21,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
@@ -30,7 +31,6 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.ui.compose.LocalDarkTheme
import com.v2ray.ang.ui.compose.QRCodeDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
@@ -58,11 +58,17 @@ fun MainScreen(
var showDelAllConfirm by remember { mutableStateOf(false) }
var showDelDuplicateConfirm by remember { mutableStateOf(false) }
var showDelInvalidConfirm by remember { mutableStateOf(false) }
var showRemoveConfirm by remember { mutableStateOf<String?>(null) }
var showRemoveConfirm by rememberSaveable(stateSaver = ServerDeleteTarget.Saver) {
mutableStateOf<ServerDeleteTarget?>(null)
}
var shareTarget by remember { mutableStateOf<Triple<String, ProfileItem, Boolean>?>(null) }
val removeServer: (String) -> Unit = { guid ->
if (confirmRemove) showRemoveConfirm = guid else onAction(MainAction.RemoveServer(guid))
val removeServer: (String, String) -> Unit = { guid, profileName ->
if (confirmRemove) {
showRemoveConfirm = ServerDeleteTarget(guid, profileName)
} else {
onAction(MainAction.RemoveServer(guid))
}
}
val pagerState = rememberPagerState(
@@ -73,16 +79,12 @@ fun MainScreen(
val lazyListStates = remember { mutableStateMapOf<String, LazyListState>() }
val lazyGridStates = remember { mutableStateMapOf<String, LazyGridState>() }
var locateInProgress by remember { mutableStateOf(false) }
LaunchedEffect(groups) {
val validGroupIds = groups.map { it.id }.toSet()
lazyListStates.keys.retainAll(validGroupIds)
lazyGridStates.keys.retainAll(validGroupIds)
}
val latestDoubleColumnDisplay by rememberUpdatedState(doubleColumnDisplay)
LaunchedEffect(groups, uiState.selectedGroupId) {
if (groups.isEmpty()) return@LaunchedEffect
val selectedIndex = groups.indexOfFirst { it.id == uiState.selectedGroupId }
@@ -93,68 +95,18 @@ fun MainScreen(
}
val latestGroups by rememberUpdatedState(groups)
val latestLocateInProgress by rememberUpdatedState(locateInProgress)
LaunchedEffect(pagerState) {
snapshotFlow { pagerState.settledPage }
.distinctUntilChanged()
.collect { page ->
val currentGroups = latestGroups
if (!latestLocateInProgress && page in currentGroups.indices) {
if (page in currentGroups.indices) {
onAction(MainAction.SelectGroup(currentGroups[page].id))
}
}
}
LaunchedEffect(uiState.locateTarget) {
val target = uiState.locateTarget ?: return@LaunchedEffect
if (target.groupIndex !in 0 until pagerState.pageCount) {
mainViewModel.onAction(MainAction.LocateHandled(target))
return@LaunchedEffect
}
locateInProgress = true
try {
if (pagerState.settledPage != target.groupIndex) {
pagerState.navigateToPageOptimized(
targetPage = target.groupIndex,
animateAdjacentPage = false
)
}
onAction(MainAction.SelectGroup(target.groupId))
repeat(10) {
val ready = if (latestDoubleColumnDisplay) {
lazyGridStates[target.groupId] != null
} else {
lazyListStates[target.groupId] != null
}
if (ready) return@repeat
delay(16L)
}
if (latestDoubleColumnDisplay) {
lazyGridStates[target.groupId]?.let { gridState ->
gridState.scrollToItem(
index = target.itemPosition,
scrollOffset = -gridState.layoutInfo.viewportSize.height / 3
)
}
} else {
lazyListStates[target.groupId]?.let { listState ->
listState.scrollToItem(
index = target.itemPosition,
scrollOffset = -listState.layoutInfo.viewportSize.height / 3
)
}
}
} finally {
delay(32L)
locateInProgress = false
mainViewModel.onAction(MainAction.LocateHandled(target))
}
}
MainDialogs(
showDelAllConfirm = showDelAllConfirm,
onDismissDelAll = { showDelAllConfirm = false },
@@ -279,8 +231,8 @@ fun MainScreen(
groupId = group.id,
mainViewModel = mainViewModel,
selectedGuid = selectedGuid,
locateTarget = uiState.locateTarget,
doubleColumnDisplay = doubleColumnDisplay,
confirmRemove = confirmRemove,
searchQuery = searchQuery,
lazyListStates = lazyListStates,
lazyGridStates = lazyGridStates,
@@ -293,6 +245,9 @@ fun MainScreen(
shareTarget = Triple(guid, profile, true)
},
onRemoveServer = removeServer,
usageInfo = group.usageInfo,
groupTitle = group.remarks,
onOpenLink = { url -> onAction(MainAction.OpenSubscriptionLink(url)) },
contentPadding = PaddingValues(
start = 0.dp,
top = 0.dp,
@@ -9,6 +9,8 @@ import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.calculateEndPadding
import androidx.compose.foundation.layout.calculateStartPadding
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
@@ -30,13 +32,21 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.LineBreak
import androidx.compose.ui.text.style.TextOverflow
@@ -44,12 +54,9 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.v2ray.ang.R
import com.v2ray.ang.dto.LocateTarget
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.dto.entities.ServersCache
import com.v2ray.ang.extension.isComplexType
import com.v2ray.ang.extension.nullIfBlank
import com.v2ray.ang.handler.AngConfigManager
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.dto.entities.SubscriptionUsageInfo
import com.v2ray.ang.ui.compose.ItemDivider
import com.v2ray.ang.ui.compose.ReorderableGridItem
import com.v2ray.ang.ui.compose.ReorderableListItem
@@ -67,8 +74,8 @@ fun GroupPagerPage(
groupId: String,
mainViewModel: MainViewModel,
selectedGuid: String?,
locateTarget: LocateTarget?,
doubleColumnDisplay: Boolean,
confirmRemove: Boolean,
searchQuery: String,
lazyListStates: MutableMap<String, LazyListState>,
lazyGridStates: MutableMap<String, LazyGridState>,
@@ -76,50 +83,98 @@ fun GroupPagerPage(
onEditServer: (String, ProfileItem) -> Unit,
onShareServer: (String, ProfileItem) -> Unit,
onMoreServer: (String, ProfileItem) -> Unit,
onRemoveServer: (String) -> Unit,
onRemoveServer: (String, String) -> Unit,
usageInfo: SubscriptionUsageInfo?,
groupTitle: String,
onOpenLink: (String) -> Unit,
contentPadding: PaddingValues
) {
val serverFlow = remember(groupId) {
mainViewModel.serversForGroup(groupId)
val groupStateFlow = remember(groupId) {
mainViewModel.serverGroupState(groupId)
}
val servers by serverFlow.collectAsStateWithLifecycle()
val groupState by groupStateFlow.collectAsStateWithLifecycle()
val canReorder = groupId.isNotEmpty() && searchQuery.isEmpty()
ServerListPage(
servers = servers,
selectedGuid = selectedGuid,
canReorder = canReorder,
doubleColumnDisplay = doubleColumnDisplay,
subscriptionId = groupId,
confirmRemove = confirmRemove,
groupId = groupId,
lazyListStates = lazyListStates,
lazyGridStates = lazyGridStates,
onSelectServer = onSelectServer,
onEditServer = onEditServer,
onShareServer = onShareServer,
onMoreServer = onMoreServer,
onRemoveServer = onRemoveServer,
onMoveServer = { fromIndex, toIndex -> mainViewModel.moveServer(groupId, fromIndex, toIndex) },
contentPadding = contentPadding
)
val actions = remember(
onSelectServer,
onEditServer,
onShareServer,
onMoreServer,
onRemoveServer,
) {
ServerRowActions(
select = onSelectServer,
edit = onEditServer,
share = onShareServer,
more = onMoreServer,
remove = onRemoveServer,
)
}
// The box floats over the list (the list scrolls underneath it), so the list is padded by the box height.
val density = LocalDensity.current
val layoutDirection = LocalLayoutDirection.current
var infoHeightPx by remember { mutableIntStateOf(0) }
val listPadding = if (usageInfo == null) {
contentPadding
} else {
PaddingValues(
start = contentPadding.calculateStartPadding(layoutDirection),
top = contentPadding.calculateTopPadding() + with(density) { infoHeightPx.toDp() },
end = contentPadding.calculateEndPadding(layoutDirection),
bottom = contentPadding.calculateBottomPadding()
)
}
Box(Modifier.fillMaxSize()) {
ServerListPage(
rows = groupState.rows,
selectedGuid = selectedGuid,
locateTarget = locateTarget?.takeIf { it.groupId == groupId },
canReorder = canReorder,
doubleColumnDisplay = doubleColumnDisplay,
groupId = groupId,
lazyListStates = lazyListStates,
lazyGridStates = lazyGridStates,
actions = actions,
onLocateHandled = { mainViewModel.onAction(MainAction.LocateHandled) },
onMoveServer = { fromIndex, toIndex ->
mainViewModel.moveServer(groupId, fromIndex, toIndex)
},
contentPadding = listPadding
)
if (usageInfo != null) {
SubscriptionInfoCard(
info = usageInfo,
fallbackTitle = groupTitle,
onOpenLink = onOpenLink,
onRefresh = { mainViewModel.onAction(MainAction.UpdateGroupSubscription(groupId)) },
modifier = Modifier
.align(Alignment.TopCenter)
.onSizeChanged { infoHeightPx = it.height }
)
}
}
}
private class ServerRowActions(
val select: (String) -> Unit,
val edit: (String, ProfileItem) -> Unit,
val share: (String, ProfileItem) -> Unit,
val more: (String, ProfileItem) -> Unit,
val remove: (String, String) -> Unit,
)
@Composable
private fun ServerListPage(
servers: List<ServersCache>,
rows: List<ServerRowUiModel>,
selectedGuid: String?,
locateTarget: LocateTarget?,
canReorder: Boolean,
doubleColumnDisplay: Boolean,
subscriptionId: String,
confirmRemove: Boolean,
groupId: String,
lazyListStates: MutableMap<String, LazyListState>,
lazyGridStates: MutableMap<String, LazyGridState>,
onSelectServer: (String) -> Unit,
onEditServer: (String, ProfileItem) -> Unit,
onShareServer: (String, ProfileItem) -> Unit,
onMoreServer: (String, ProfileItem) -> Unit,
onRemoveServer: (String) -> Unit,
actions: ServerRowActions,
onLocateHandled: () -> Unit,
onMoveServer: (Int, Int) -> Unit,
contentPadding: PaddingValues
) {
@@ -133,6 +188,8 @@ private fun ServerListPage(
}
} else null
LocateTargetEffect(locateTarget, rows, gridState, onLocateHandled)
LazyVerticalGrid(
columns = GridCells.Fixed(2),
state = gridState,
@@ -141,24 +198,19 @@ private fun ServerListPage(
.verticalScrollbar(gridState),
contentPadding = contentPadding
) {
itemsIndexed(items = servers, key = { _, item -> item.guid }) { _, serverCache ->
itemsIndexed(items = rows, key = { _, item -> item.guid }) { _, row ->
val content: @Composable () -> Unit = {
ServerItemColumn(
serverCache = serverCache,
selectedGuid = selectedGuid,
subscriptionId = subscriptionId,
row = row,
isSelected = row.guid == selectedGuid,
doubleColumnDisplay = true,
onSelectServer = onSelectServer,
onEditServer = onEditServer,
onShareServer = onShareServer,
onMoreServer = onMoreServer,
onRemoveServer = onRemoveServer
actions = actions
)
}
if (canReorder && reorderableGridState != null) {
ReorderableItem(
reorderableGridState,
key = serverCache.guid
key = row.guid
) { isDragging ->
ReorderableGridItem(
scope = this,
@@ -180,6 +232,8 @@ private fun ServerListPage(
}
} else null
LocateTargetEffect(locateTarget, rows, listState, onLocateHandled)
LazyColumn(
state = listState,
modifier = Modifier
@@ -187,39 +241,29 @@ private fun ServerListPage(
.verticalScrollbar(listState),
contentPadding = contentPadding
) {
itemsIndexed(items = servers, key = { _, item -> item.guid }) { _, serverCache ->
itemsIndexed(items = rows, key = { _, item -> item.guid }) { _, row ->
if (canReorder && reorderableState != null) {
ReorderableItem(
reorderableState,
key = serverCache.guid
key = row.guid
) { isDragging ->
ReorderableListItem(
scope = this,
isDragging = isDragging
) {
ServerItemRow(
serverCache = serverCache,
selectedGuid = selectedGuid,
subscriptionId = subscriptionId,
onSelectServer = onSelectServer,
onEditServer = onEditServer,
onShareServer = onShareServer,
onMoreServer = onMoreServer,
onRemoveServer = onRemoveServer
row = row,
isSelected = row.guid == selectedGuid,
actions = actions
)
}
ItemDivider()
}
} else {
ServerItemRow(
serverCache = serverCache,
selectedGuid = selectedGuid,
subscriptionId = subscriptionId,
onSelectServer = onSelectServer,
onEditServer = onEditServer,
onShareServer = onShareServer,
onMoreServer = onMoreServer,
onRemoveServer = onRemoveServer
row = row,
isSelected = row.guid == selectedGuid,
actions = actions
)
ItemDivider()
}
@@ -229,103 +273,96 @@ private fun ServerListPage(
}
@Composable
private fun ServerItemRow(
serverCache: ServersCache,
selectedGuid: String?,
subscriptionId: String,
onSelectServer: (String) -> Unit,
onEditServer: (String, ProfileItem) -> Unit,
onShareServer: (String, ProfileItem) -> Unit,
onMoreServer: (String, ProfileItem) -> Unit,
onRemoveServer: (String) -> Unit
private fun LocateTargetEffect(
target: LocateTarget?,
rows: List<ServerRowUiModel>,
state: LazyListState,
onHandled: () -> Unit,
) {
val profile = serverCache.profile
val subRemarks = if (subscriptionId.isEmpty()) {
MmkvManager.decodeSubscription(profile.subscriptionId)?.remarks?.firstOrNull()
?.toString() ?: ""
} else ""
if (target == null) return
LaunchedEffect(target, rows) {
val index = rows.indexOfFirst { it.guid == target.serverGuid }
if (index < 0) return@LaunchedEffect
state.scrollToItem(index, -state.layoutInfo.viewportSize.height / 3)
onHandled()
}
}
@Composable
private fun LocateTargetEffect(
target: LocateTarget?,
rows: List<ServerRowUiModel>,
state: LazyGridState,
onHandled: () -> Unit,
) {
if (target == null) return
LaunchedEffect(target, rows) {
val index = rows.indexOfFirst { it.guid == target.serverGuid }
if (index < 0) return@LaunchedEffect
state.scrollToItem(index, -state.layoutInfo.viewportSize.height / 3)
onHandled()
}
}
@Composable
private fun ServerItemRow(
row: ServerRowUiModel,
isSelected: Boolean,
actions: ServerRowActions
) {
ServerListItem(
remarks = profile.remarks,
statistics = profile.description.nullIfBlank()
?: AngConfigManager.generateDescription(profile),
typeDescription = getProtocolDescription(profile),
testDelayMillis = serverCache.testDelayMillis,
isSelected = serverCache.guid == selectedGuid,
subscriptionRemarks = subRemarks,
row = row,
isSelected = isSelected,
doubleColumnDisplay = false,
onClick = { onSelectServer(serverCache.guid) },
onShare = { onShareServer(serverCache.guid, profile) },
onEdit = { onEditServer(serverCache.guid, profile) },
onRemove = { onRemoveServer(serverCache.guid) },
onMore = { onMoreServer(serverCache.guid, profile) }
actions = actions
)
}
@Composable
private fun ServerItemColumn(
serverCache: ServersCache,
selectedGuid: String?,
subscriptionId: String,
row: ServerRowUiModel,
isSelected: Boolean,
doubleColumnDisplay: Boolean,
onSelectServer: (String) -> Unit,
onEditServer: (String, ProfileItem) -> Unit,
onShareServer: (String, ProfileItem) -> Unit,
onMoreServer: (String, ProfileItem) -> Unit,
onRemoveServer: (String) -> Unit
actions: ServerRowActions
) {
val profile = serverCache.profile
val subRemarks = if (subscriptionId.isEmpty()) {
MmkvManager.decodeSubscription(profile.subscriptionId)?.remarks?.firstOrNull()?.toString() ?: ""
} else ""
Column {
ServerListItem(
remarks = profile.remarks,
statistics = profile.description.nullIfBlank() ?: AngConfigManager.generateDescription(profile),
typeDescription = getProtocolDescription(profile),
testDelayMillis = serverCache.testDelayMillis,
isSelected = serverCache.guid == selectedGuid,
subscriptionRemarks = subRemarks,
row = row,
isSelected = isSelected,
doubleColumnDisplay = doubleColumnDisplay,
onClick = { onSelectServer(serverCache.guid) },
onEdit = { onEditServer(serverCache.guid, profile) },
onShare = { onShareServer(serverCache.guid, profile) },
onRemove = { onRemoveServer(serverCache.guid) },
onMore = { onMoreServer(serverCache.guid, profile) }
actions = actions
)
ItemDivider()
}
}
@Composable
fun ServerListItem(
remarks: String,
statistics: String,
typeDescription: String,
testDelayMillis: Long,
private fun ServerListItem(
row: ServerRowUiModel,
isSelected: Boolean,
subscriptionRemarks: String,
doubleColumnDisplay: Boolean,
onClick: () -> Unit,
onEdit: () -> Unit,
onShare: () -> Unit,
onRemove: () -> Unit,
onMore: () -> Unit,
modifier: Modifier = Modifier,
dragModifier: Modifier = Modifier
actions: ServerRowActions
) {
val testResult = if (testDelayMillis == 0L) {
val testResult = if (row.testDelayMillis == 0L) {
""
} else {
stringResource(R.string.server_test_delay_value, testDelayMillis)
stringResource(R.string.server_test_delay_value, row.testDelayMillis)
}
val selectedStateDescription = if (isSelected) {
stringResource(R.string.acc_selected_server)
} else {
null
}
Row(
modifier = modifier
modifier = Modifier
.fillMaxWidth()
.height(IntrinsicSize.Min)
.clickable(onClick = onClick)
.then(dragModifier)
.semantics {
if (selectedStateDescription != null) {
stateDescription = selectedStateDescription
}
}
.clickable { actions.select(row.guid) }
) {
Box(
Modifier
@@ -352,58 +389,83 @@ fun ServerListItem(
.padding(start = 8.dp, end = 12.dp, top = 8.dp, bottom = 8.dp)
) {
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Text(remarks, Modifier.weight(1f), style = MaterialTheme.typography.bodyLarge.copy(lineBreak = LineBreak.Paragraph), maxLines = 2, overflow = TextOverflow.Ellipsis)
Text(row.remarks, Modifier.weight(1f), style = MaterialTheme.typography.bodyLarge.copy(lineBreak = LineBreak.Paragraph), maxLines = 2, overflow = TextOverflow.Ellipsis)
if (doubleColumnDisplay) {
IconButton(onClick = onMore, Modifier.size(36.dp)) {
Icon(painterResource(R.drawable.ic_more_vert_24dp), null, Modifier.size(24.dp))
IconButton(onClick = { actions.more(row.guid, row.profile) }, Modifier.size(36.dp)) {
Icon(
painterResource(R.drawable.ic_more_vert_24dp),
stringResource(R.string.acc_more),
Modifier.size(24.dp)
)
}
} else {
IconButton(onClick = onShare, Modifier.size(36.dp)) { Icon(painterResource(R.drawable.ic_share_24dp), null, Modifier.size(24.dp)) }
IconButton(onClick = onEdit, Modifier.size(36.dp)) { Icon(painterResource(R.drawable.ic_edit_24dp), null, Modifier.size(24.dp)) }
IconButton(onClick = onRemove, Modifier.size(36.dp)) { Icon(painterResource(R.drawable.ic_delete_24dp), null, Modifier.size(24.dp)) }
IconButton(onClick = { actions.share(row.guid, row.profile) }, Modifier.size(36.dp)) {
Icon(
painterResource(R.drawable.ic_share_24dp),
stringResource(R.string.title_configuration_share),
Modifier.size(24.dp)
)
}
IconButton(onClick = { actions.edit(row.guid, row.profile) }, Modifier.size(36.dp)) {
Icon(
painterResource(R.drawable.ic_edit_24dp),
stringResource(R.string.acc_edit),
Modifier.size(24.dp)
)
}
IconButton(onClick = { actions.remove(row.guid, row.remarks) }, Modifier.size(36.dp)) {
Icon(
painterResource(R.drawable.ic_delete_24dp),
stringResource(R.string.acc_delete),
Modifier.size(24.dp)
)
}
}
}
Spacer(modifier = Modifier.height(6.dp))
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
if (subscriptionRemarks.isNotBlank()) {
if (row.subscriptionBadge.isNotBlank()) {
Box(
Modifier
.size(24.dp)
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)), Alignment.Center
) {
Text(subscriptionRemarks.take(1).uppercase(), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.primary)
Text(row.subscriptionBadge.uppercase(), fontSize = 11.sp, fontWeight = FontWeight.Bold, color = MaterialTheme.colorScheme.primary)
}
}
Text(statistics, Modifier.weight(1f), style = MaterialTheme.typography.bodyMedium, color = MaterialTheme.colorScheme.onSurfaceVariant, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(
row.statistics,
Modifier.weight(1f),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
Spacer(modifier = Modifier.height(6.dp))
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
Text(typeDescription, style = MaterialTheme.typography.bodySmall, color = colorConfigType, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(testResult, style = MaterialTheme.typography.bodySmall, color = if (testDelayMillis < 0L) colorPingRed else colorPing, maxLines = 1, overflow = TextOverflow.Ellipsis)
Text(
row.typeDescription,
modifier = Modifier.weight(1f, fill = false),
style = MaterialTheme.typography.bodySmall,
color = colorConfigType,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Spacer(modifier = Modifier.width(8.dp))
Text(
testResult,
style = MaterialTheme.typography.bodySmall,
color = if (row.testDelayMillis < 0L) colorPingRed else colorPing,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}
}
}
private fun getProtocolDescription(profile: ProfileItem): String {
if (profile.configType.isComplexType()) return profile.configType.name
val parts = mutableListOf(profile.configType.name)
profile.network?.let { net ->
if (net.isNotBlank() && !net.equals("tcp", ignoreCase = true)) parts.add(net)
}
profile.security?.let { sec ->
if (sec.isNotBlank()) {
if (profile.insecure == true && sec.equals("tls", ignoreCase = true)) {
parts.add("$sec insecure")
} else {
parts.add(sec)
}
}
}
return parts.joinToString(" / ")
}
internal suspend fun PagerState.navigateToPageOptimized(
targetPage: Int,
animateAdjacentPage: Boolean = true
@@ -0,0 +1,61 @@
package com.v2ray.ang.ui.main
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.dto.entities.ServersCache
import com.v2ray.ang.extension.isComplexType
import com.v2ray.ang.extension.nullIfBlank
import com.v2ray.ang.handler.AngConfigManager
internal data class ServerRowUiModel(
val guid: String,
val profile: ProfileItem,
val remarks: String,
val statistics: String,
val typeDescription: String,
val testDelayMillis: Long,
val subscriptionBadge: String,
)
internal data class ServerGroupUiState(
val servers: List<ServersCache> = emptyList(),
val rows: List<ServerRowUiModel> = emptyList(),
)
internal fun buildServerRowUiModel(
server: ServersCache,
subscriptionRemarks: String,
): ServerRowUiModel {
val profile = server.profile
return ServerRowUiModel(
guid = server.guid,
profile = profile,
remarks = profile.remarks,
statistics = profile.description.nullIfBlank()
?: AngConfigManager.generateDescription(profile),
typeDescription = serverProtocolDescription(profile),
testDelayMillis = server.testDelayMillis,
subscriptionBadge = subscriptionRemarks.firstOrNull()?.toString().orEmpty(),
)
}
private fun serverProtocolDescription(profile: ProfileItem): String {
if (profile.configType.isComplexType()) return profile.configType.name
val parts = mutableListOf(profile.configType.name)
profile.network?.let { network ->
if (network.isNotBlank() && !network.equals("tcp", ignoreCase = true)) {
parts.add(network)
}
}
profile.security?.let { security ->
if (security.isNotBlank()) {
parts.add(
if (profile.insecure == true && security.equals("tls", ignoreCase = true)) {
"$security insecure"
} else {
security
}
)
}
}
return parts.joinToString(" / ")
}
@@ -1,15 +1,18 @@
package com.v2ray.ang.ui.main
import com.v2ray.ang.dto.ConnectionTestResult
import com.v2ray.ang.dto.RealPingResult
sealed class MainServiceEvent {
data object StateRunning : MainServiceEvent()
data object StateNotRunning : MainServiceEvent()
data object StateStartSuccess : MainServiceEvent()
data object StateStartFailure : MainServiceEvent()
data class StateStartFailure(val message: String? = null) : MainServiceEvent()
data object StateStopSuccess : MainServiceEvent()
data class MeasureDelayResult(val result: ConnectionTestResult) : MainServiceEvent()
data object MeasureConfigSuccess : MainServiceEvent()
data class MeasureConfigNotify(val progress: String) : MainServiceEvent()
data class MeasureConfigFinish(val finishedCount: String?) : MainServiceEvent()
data class MeasureDelayResult(val result: ConnectionTestResult, val requestId: String) : MainServiceEvent()
data class MeasureDelayCancelled(val requestId: String) : MainServiceEvent()
data class MeasureConfigSuccess(val result: RealPingResult, val requestId: String) : MainServiceEvent()
data class MeasureConfigNotify(val progress: String, val requestId: String) : MainServiceEvent()
data class MeasureConfigFinish(val requestId: String) : MainServiceEvent()
data class MeasureConfigCancelled(val requestId: String) : MainServiceEvent()
}
@@ -0,0 +1,197 @@
package com.v2ray.ang.ui.main
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.clearAndSetSemantics
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.v2ray.ang.R
import com.v2ray.ang.dto.entities.SubscriptionUsageInfo
import com.v2ray.ang.extension.toTrafficString
import com.v2ray.ang.handler.SubscriptionInfoParser
import com.v2ray.ang.util.Utils
private const val MILLIS_PER_SECOND = 1000L
private const val EXPIRY_DATE_PATTERN = "yyyy-MM-dd"
private const val CARD_ALPHA = 0.97f
/** Locale-neutral values the subscription information box renders. */
internal data class SubscriptionInfoUiModel(
val title: String?,
val usedBytes: Long,
/** Traffic quota in bytes; 0 means unlimited. */
val totalBytes: Long,
/** Used share of the quota in 0..1; null when the quota is unlimited. */
val trafficFraction: Float?,
/** Expiry in epoch milliseconds; null when the subscription never expires. */
val expireMillis: Long?,
val expired: Boolean,
val supportUrl: String?,
val webPageUrl: String?,
) {
val trafficLow: Boolean get() = trafficFraction != null && trafficFraction >= TRAFFIC_WARNING_FRACTION
companion object {
const val TRAFFIC_WARNING_FRACTION = 0.9f
}
}
internal fun buildSubscriptionInfoUiModel(
info: SubscriptionUsageInfo,
nowMillis: Long,
): SubscriptionInfoUiModel {
val unlimited = info.total <= 0L
val expireMillis = info.expire.takeIf { it > 0L }?.let { it * MILLIS_PER_SECOND }
return SubscriptionInfoUiModel(
title = info.title,
usedBytes = info.used,
totalBytes = if (unlimited) 0L else info.total,
trafficFraction = if (unlimited) null else (info.used.toDouble() / info.total).toFloat().coerceIn(0f, 1f),
expireMillis = expireMillis,
expired = expireMillis != null && expireMillis <= nowMillis,
supportUrl = info.supportUrl,
webPageUrl = info.webPageUrl,
)
}
/**
* Fixed box on top of a subscription group showing the account information published by the
* subscription panel: traffic, expiry date and shortcuts (support link, panel page, update).
*/
@Composable
internal fun SubscriptionInfoCard(
info: SubscriptionUsageInfo,
fallbackTitle: String,
onOpenLink: (String) -> Unit,
onRefresh: () -> Unit,
modifier: Modifier = Modifier,
) {
val model = remember(info) { buildSubscriptionInfoUiModel(info, System.currentTimeMillis()) }
Surface(
modifier = modifier
.fillMaxWidth()
.padding(horizontal = 12.dp, vertical = 4.dp),
shape = MaterialTheme.shapes.medium,
// Almost opaque: the list scrolls underneath without showing through readably.
color = MaterialTheme.colorScheme.surfaceContainerHigh.copy(alpha = CARD_ALPHA),
border = BorderStroke(0.5.dp, MaterialTheme.colorScheme.outlineVariant.copy(alpha = 0.5f)),
) {
Column(Modifier.padding(start = 16.dp, end = 8.dp, top = 2.dp, bottom = 10.dp)) {
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
Text(
text = model.title ?: fallbackTitle,
modifier = Modifier.weight(1f),
style = MaterialTheme.typography.bodyMedium,
fontWeight = FontWeight.SemiBold,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
model.supportUrl?.let { url ->
CardIconButton(
iconRes = if (SubscriptionInfoParser.isTelegramLink(url)) {
R.drawable.ic_telegram_24dp
} else {
R.drawable.ic_feedback_24dp
},
contentDescription = stringResource(R.string.sub_info_open_support),
onClick = { onOpenLink(url) },
)
}
model.webPageUrl?.let { url ->
CardIconButton(
iconRes = R.drawable.ic_about_24dp,
contentDescription = stringResource(R.string.sub_info_open_page),
onClick = { onOpenLink(url) },
)
}
CardIconButton(
iconRes = R.drawable.ic_refresh_24dp,
contentDescription = stringResource(R.string.title_sub_update),
onClick = onRefresh,
)
}
// One semantics node for the read-only figures so TalkBack reads them in one go.
Column(
Modifier
.fillMaxWidth()
.padding(end = 8.dp)
.semantics(mergeDescendants = true) {}
) {
StatsRow(model)
model.trafficFraction?.let { fraction ->
Spacer(Modifier.height(4.dp))
LinearProgressIndicator(
progress = { fraction },
modifier = Modifier
.fillMaxWidth()
.height(4.dp)
.clip(RoundedCornerShape(2.dp))
.clearAndSetSemantics {},
color = if (model.trafficLow) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
)
}
}
}
}
}
@Composable
private fun CardIconButton(iconRes: Int, contentDescription: String, onClick: () -> Unit) {
IconButton(onClick = onClick, modifier = Modifier.size(32.dp)) {
Icon(painterResource(iconRes), contentDescription, Modifier.size(20.dp))
}
}
@Composable
private fun StatsRow(model: SubscriptionInfoUiModel) {
val totalText = if (model.totalBytes > 0L) {
model.totalBytes.toTrafficString()
} else {
stringResource(R.string.sub_info_unlimited)
}
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween) {
Text(
text = stringResource(R.string.sub_info_traffic, model.usedBytes.toTrafficString(), totalText),
modifier = Modifier.weight(1f, fill = false),
style = MaterialTheme.typography.bodySmall,
color = if (model.trafficLow) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurface,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
)
model.expireMillis?.let { expireMillis ->
Spacer(Modifier.size(8.dp))
Text(
text = Utils.formatTimestamp(expireMillis, EXPIRY_DATE_PATTERN),
style = MaterialTheme.typography.bodySmall,
color = if (model.expired) MaterialTheme.colorScheme.error else MaterialTheme.colorScheme.onSurfaceVariant,
maxLines = 1,
)
}
}
}
@@ -0,0 +1,37 @@
package com.v2ray.ang.ui.main
import java.util.UUID
/** Main-thread request ownership; a late reply must not complete a newer test. */
internal class MainTestRequests {
data class Bulk(val id: String, val groupId: String)
private var current: String? = null
var bulk: Bulk? = null
private set
val isTesting: Boolean get() = current != null || bulk != null
fun beginCurrent(): String = UUID.randomUUID().toString().also { current = it }
fun completeCurrent(id: String): Boolean {
if (id != current) return false
current = null
return true
}
fun invalidateCurrent() {
current = null
}
fun beginBulk(groupId: String): Bulk = Bulk(UUID.randomUUID().toString(), groupId).also { bulk = it }
fun completeBulk(id: String): Bulk? {
val request = bulk?.takeIf { it.id == id } ?: return null
bulk = null
return request
}
fun cancelBulk() {
bulk = null
}
}
@@ -9,13 +9,16 @@ import com.v2ray.ang.R
import com.v2ray.ang.dto.ConnectionTestResult
import com.v2ray.ang.dto.GroupMapItem
import com.v2ray.ang.dto.LocateTarget
import com.v2ray.ang.dto.RealPingResult
import com.v2ray.ang.dto.TestServiceMessage
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.dto.entities.ServersCache
import com.v2ray.ang.dto.entities.SubscriptionCache
import com.v2ray.ang.extension.delay
import com.v2ray.ang.extension.isComplexType
import com.v2ray.ang.extension.matchesPattern
import com.v2ray.ang.extension.moveItem
import com.v2ray.ang.handler.SubscriptionInfoParser
import com.v2ray.ang.ui.base.BaseViewModel
import com.v2ray.ang.util.LogUtil
import kotlinx.coroutines.CancellationException
@@ -23,12 +26,15 @@ import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
@@ -37,6 +43,30 @@ import kotlinx.coroutines.withContext
import java.util.concurrent.ConcurrentHashMap
import java.util.regex.PatternSyntaxException
private fun applyTestDelayResults(
servers: List<ServersCache>,
updates: Map<String, Long>,
): List<ServersCache> = servers.map { server ->
val delayMillis = updates[server.guid]
if (delayMillis == null || delayMillis == server.testDelayMillis) {
server
} else {
server.copy(testDelayMillis = delayMillis)
}
}
private fun applyTestDelayResultsToRows(
rows: List<ServerRowUiModel>,
updates: Map<String, Long>,
): List<ServerRowUiModel> = rows.map { row ->
val delayMillis = updates[row.guid]
if (delayMillis == null || delayMillis == row.testDelayMillis) {
row
} else {
row.copy(testDelayMillis = delayMillis)
}
}
class MainViewModel(
application: Application,
private val dataSource: MainDataSource
@@ -65,7 +95,8 @@ class MainViewModel(
// ---------- Groups & cache ----------
private val cacheMutex = Mutex()
private val groupDataCache = mutableMapOf<String, List<ServersCache>>()
private val groupPageFlows = ConcurrentHashMap<String, MutableStateFlow<List<ServersCache>>>()
private val groupUiFlows = ConcurrentHashMap<String, MutableStateFlow<ServerGroupUiState>>()
private val groupServerFlows = ConcurrentHashMap<String, StateFlow<List<ServersCache>>>()
private val groupLoadMutexes = ConcurrentHashMap<String, Mutex>()
private val serverOrderPersistenceJobs = mutableMapOf<String, Job>()
@@ -73,9 +104,11 @@ class MainViewModel(
private var preloadJob: Job? = null
private var selectedGroupLoadJob: Job? = null
private var reloadJob: Job? = null
private var testResultFlushJob: Job? = null
private val pendingTestResults = linkedMapOf<String, Long>()
@Volatile
private var testingGroupId: String? = null
private val testRequests = MainTestRequests()
private var bulkTestJob: Job? = null
private val initialPageReady = CompletableDeferred<Unit>()
@@ -102,31 +135,87 @@ class MainViewModel(
updateRunningState(true)
}
MainServiceEvent.StateStartFailure -> {
toastError(R.string.toast_services_failure)
is MainServiceEvent.StateStartFailure -> {
if (!event.message.isNullOrBlank()) {
toastError(event.message)
} else {
toastError(R.string.toast_services_failure)
}
updateRunningState(false)
}
MainServiceEvent.StateStopSuccess -> updateRunningState(false)
is MainServiceEvent.MeasureDelayResult -> {
_uiState.update { it.copy(status = MainStatus.ConnectionTest(event.result)) }
if (!uiState.value.isRunning || !testRequests.completeCurrent(event.requestId)) return
_uiState.update { it.copy(isTesting = testRequests.isTesting, status = MainStatus.ConnectionTest(event.result)) }
}
MainServiceEvent.MeasureConfigSuccess -> {
viewModelScope.launch(ioDispatcher) {
val gid = testingGroupId ?: uiState.value.selectedGroupId
cacheMutex.withLock { groupDataCache.remove(gid) }
updateGroupUi(gid, loadGroup(gid, forceRefresh = true))
}
is MainServiceEvent.MeasureConfigSuccess -> {
val request = testRequests.bulk?.takeIf { it.id == event.requestId } ?: return
queueTestResult(event.result, request)
}
is MainServiceEvent.MeasureConfigNotify -> {
_uiState.update { it.copy(status = MainStatus.TestProgress(event.progress)) }
if (event.requestId == testRequests.bulk?.id) {
_uiState.update { it.copy(status = MainStatus.TestProgress(event.progress)) }
}
}
is MainServiceEvent.MeasureConfigFinish -> {
onTestsFinished()
val request = testRequests.bulk?.takeIf { it.id == event.requestId } ?: return
val scheduledFlush = testResultFlushJob
testResultFlushJob = viewModelScope.launch {
scheduledFlush?.cancelAndJoin()
if (testRequests.bulk?.id != request.id) return@launch
flushPendingTestResults(request)
onTestsFinished(request.id)
}
}
is MainServiceEvent.MeasureDelayCancelled -> {
if (testRequests.completeCurrent(event.requestId)) resetTestStatus()
}
is MainServiceEvent.MeasureConfigCancelled -> {
if (testRequests.completeBulk(event.requestId) != null) {
cancelPendingTestResults()
resetTestStatus()
}
}
}
}
private fun queueTestResult(result: RealPingResult, request: MainTestRequests.Bulk) {
pendingTestResults[result.guid] = result.delayMillis
if (testResultFlushJob?.isActive == true) return
testResultFlushJob = viewModelScope.launch {
while (pendingTestResults.isNotEmpty()) {
delay(TEST_RESULT_FLUSH_INTERVAL_MS)
flushPendingTestResults(request)
}
}
}
private suspend fun flushPendingTestResults(request: MainTestRequests.Bulk) {
if (pendingTestResults.isEmpty()) return
if (testRequests.bulk?.id != request.id) return
val updates = cacheMutex.withLock {
val drained = pendingTestResults.toMap()
pendingTestResults.clear()
groupDataCache[request.groupId]?.let { cached ->
groupDataCache[request.groupId] = applyTestDelayResults(cached, drained)
}
drained
}
if (updates.isEmpty()) return
if (testRequests.bulk?.id != request.id) return
mutableServerGroupState(request.groupId).update { current ->
current.copy(
servers = applyTestDelayResults(current.servers, updates),
rows = applyTestDelayResultsToRows(current.rows, updates),
)
}
}
@@ -163,14 +252,25 @@ class MainViewModel(
// ---------- Public state accessors ----------
fun serversForGroup(groupId: String): StateFlow<List<ServersCache>> =
groupPageFlows.computeIfAbsent(groupId) { MutableStateFlow(emptyList()) }
.asStateFlow()
groupServerFlows.computeIfAbsent(groupId) {
val groupState = mutableServerGroupState(groupId)
groupState
.map { it.servers }
.stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(stopTimeoutMillis = 5_000),
initialValue = groupState.value.servers,
)
}
private fun mutableServersForGroup(groupId: String): MutableStateFlow<List<ServersCache>> =
groupPageFlows.computeIfAbsent(groupId) { MutableStateFlow(emptyList()) }
internal fun serverGroupState(groupId: String): StateFlow<ServerGroupUiState> =
mutableServerGroupState(groupId).asStateFlow()
private fun mutableServerGroupState(groupId: String): MutableStateFlow<ServerGroupUiState> =
groupUiFlows.computeIfAbsent(groupId) { MutableStateFlow(ServerGroupUiState()) }
private fun currentServers(): List<ServersCache> =
mutableServersForGroup(uiState.value.selectedGroupId).value
mutableServerGroupState(uiState.value.selectedGroupId).value.servers
// ---------- Action handler ----------
fun onAction(action: MainAction) {
@@ -184,14 +284,15 @@ class MainViewModel(
MainAction.RemoveDuplicateServers -> removeDuplicateServerAsync()
MainAction.RemoveInvalidServers -> removeInvalidServerAsync()
MainAction.SortByTestResults -> sortByTestResultsAsync()
MainAction.UpdateSubscriptions -> importConfigViaSub()
MainAction.UpdateSubscriptions -> importConfigViaSub(uiState.value.selectedGroupId)
is MainAction.UpdateGroupSubscription -> importConfigViaSub(action.groupId)
MainAction.ExportAll -> exportAllAsync()
is MainAction.SelectGroup -> subscriptionIdChanged(action.groupId)
is MainAction.SelectServer -> updateSelectedGuid(action.guid)
is MainAction.RemoveServer -> removeServerAndRefresh(action.guid)
is MainAction.Search -> filterConfig(action.query)
is MainAction.ImportBatchConfig -> importBatchConfig(action.configText)
is MainAction.LocateHandled -> consumeLocateTarget(action.target)
MainAction.LocateHandled -> consumeLocateTarget()
is MainAction.ShareQRCode -> {
val bitmap = dataSource.share2QRCode(action.guid)
_uiState.update { it.copy(shareQRCodeBitmap = bitmap) }
@@ -211,7 +312,8 @@ class MainViewModel(
MainAction.LocateSelectedServer,
is MainAction.EditServer,
is MainAction.ShareClipboard,
is MainAction.ShareFullContent -> {
is MainAction.ShareFullContent,
is MainAction.OpenSubscriptionLink -> {
// Handled by Activity via its onAction lambda
}
}
@@ -222,7 +324,7 @@ class MainViewModel(
viewModelScope.launch(preloadDispatcher) {
try {
initialPageReady.await()
delay(32L)
delay(32)
dataSource.initAssets()
dataSource.syncSubscriptions()
} catch (cancelled: CancellationException) {
@@ -289,7 +391,31 @@ class MainViewModel(
}
private fun updateGroupUi(groupId: String, servers: List<ServersCache>) {
mutableServersForGroup(groupId).value = applyKeywordFilter(servers)
val filteredServers = applyKeywordFilter(servers)
mutableServerGroupState(groupId).value = ServerGroupUiState(
servers = filteredServers,
rows = buildServerRows(groupId, filteredServers)
)
}
private fun buildServerRows(groupId: String, servers: List<ServersCache>): List<ServerRowUiModel> {
val subscriptionRemarks = if (groupId.isEmpty()) {
servers.asSequence()
.map { it.profile.subscriptionId }
.filter { it.isNotEmpty() }
.distinct()
.associateWith { subscriptionId ->
dataSource.getSubscriptionItem(subscriptionId)?.remarks.orEmpty()
}
} else {
emptyMap()
}
return servers.map { server ->
buildServerRowUiModel(
server = server,
subscriptionRemarks = subscriptionRemarks[server.profile.subscriptionId].orEmpty()
)
}
}
fun getSubscriptions(): List<SubscriptionCache> = dataSource.getSubscriptions()
@@ -330,21 +456,28 @@ class MainViewModel(
cacheMutex.withLock { groupDataCache.clear() }
}
val groups = dataSource.getSubscriptions().map {
GroupMapItem(id = it.guid, remarks = it.subscription.remarks)
GroupMapItem(
id = it.guid,
remarks = it.subscription.remarks,
usageInfo = it.subscription.usageInfo?.let { info ->
SubscriptionInfoParser.withWebPageFallback(info, it.subscription.url)
},
)
}
val selectedGroup = resolveSelectedGroup(groups)
val validIds = groups.mapTo(HashSet()) { it.id }
groupPageFlows.keys.removeAll { it !in validIds }
groupUiFlows.keys.removeAll { it !in validIds }
groupServerFlows.keys.removeAll { it !in validIds }
groupLoadMutexes.keys.removeAll { it !in validIds }
_uiState.update {
it.copy(
groups = groups,
selectedGroupId = selectedGroup,
selectedGuid = dataSource.getSelectServer()
selectedGuid = dataSource.getSelectServer(),
)
}
groups.forEach { mutableServersForGroup(it.id) }
groups.forEach { mutableServerGroupState(it.id) }
if (groups.isEmpty()) {
cacheMutex.withLock { groupDataCache.clear() }
@@ -364,7 +497,7 @@ class MainViewModel(
preloadJob = viewModelScope.launch(preloadDispatcher) {
preloadOrder.forEach { groupId ->
ensureActive()
delay(32L)
delay(32)
val servers = loadGroup(groupId, forceRefresh)
updateGroupUi(groupId, servers)
}
@@ -408,8 +541,7 @@ class MainViewModel(
}
}
private fun importConfigViaSub() {
val subId = uiState.value.selectedGroupId
private fun importConfigViaSub(subId: String) {
launchLoading {
withContext(ioDispatcher) {
try {
@@ -424,7 +556,13 @@ class MainViewModel(
toast(R.string.title_update_subscription_no_subscription)
result.successCount > 0 && result.failureCount + result.skipCount == 0 ->
toast(dataSource.getString(R.string.title_update_config_count, result.configCount))
toast(
getQuantityString(
R.plurals.title_update_config_count,
result.configCount,
result.configCount,
)
)
else ->
toast(dataSource.getString(R.string.title_update_subscription_result, result.configCount, result.successCount, result.failureCount, result.skipCount))
@@ -582,7 +720,7 @@ class MainViewModel(
fun subscriptionIdChanged(id: String) {
if (_uiState.value.groups.none { it.id == id }) return
mutableServersForGroup(id)
mutableServerGroupState(id)
if (uiState.value.selectedGroupId != id) {
dataSource.setSelectedSubscriptionId(id)
_uiState.update { it.copy(selectedGroupId = id) }
@@ -617,7 +755,7 @@ class MainViewModel(
}
order.forEachIndexed { index, groupId ->
ensureActive()
if (index > 0) delay(32L)
if (index > 0) delay(32)
updateGroupUi(groupId, loadGroup(groupId, forceRefresh = true))
}
}
@@ -628,7 +766,7 @@ class MainViewModel(
keywordFilter = keyword
filterJob?.cancel()
filterJob = viewModelScope.launch(defaultDispatcher) {
delay(300L)
delay(300)
val snapshot = cacheMutex.withLock { groupDataCache.toMap() }
ensureActive()
snapshot.forEach { (groupId, servers) ->
@@ -660,10 +798,13 @@ class MainViewModel(
}
fun moveServer(groupId: String, fromPosition: Int, toPosition: Int) {
val servers = mutableServersForGroup(groupId).value.toMutableList()
val groupState = mutableServerGroupState(groupId).value
val servers = groupState.servers.toMutableList()
if (!servers.moveItem(fromPosition, toPosition)) return
val rows = groupState.rows.toMutableList()
rows.moveItem(fromPosition, toPosition)
val guids = servers.map { it.guid }
mutableServersForGroup(groupId).value = servers
mutableServerGroupState(groupId).value = ServerGroupUiState(servers, rows)
// A drag emits several moves; serialize writes so an older order cannot overwrite a newer one.
val previousPersistenceJob = serverOrderPersistenceJobs[groupId]
serverOrderPersistenceJobs[groupId] = viewModelScope.launch(ioDispatcher) {
@@ -675,60 +816,93 @@ class MainViewModel(
// ---------- Testing ----------
fun cancelAllPing() {
bulkTestJob?.cancel()
bulkTestJob = null
testRequests.cancelBulk()
testRequests.invalidateCurrent()
cancelPendingTestResults()
resetTestStatus()
dataSource.cancelAllPing()
testingGroupId = null
}
private fun resetTestStatus() {
_uiState.update {
it.copy(
isTesting = false,
status = if (it.isRunning) MainStatus.Connected else MainStatus.Disconnected
isTesting = testRequests.isTesting,
status = if (testRequests.isTesting) MainStatus.Testing
else if (it.isRunning) MainStatus.Connected else MainStatus.Disconnected
)
}
}
fun testAllRealPing(onlyTcp: Boolean = false) {
dataSource.cancelAllPing()
cancelAllPing()
val groupId = uiState.value.selectedGroupId
val servers = currentServers()
dataSource.clearAllTestDelayResults(servers.map { it.guid })
if (servers.isEmpty()) {
_uiState.update { it.copy(isTesting = false) }
return
}
testingGroupId = groupId
val serverGuids = servers.map { it.guid }
mutableServerGroupState(groupId).update { current ->
current.copy(
servers = current.servers.map { server ->
if (server.testDelayMillis == 0L) server
else server.copy(testDelayMillis = 0L)
},
rows = current.rows.map { row ->
if (row.testDelayMillis == 0L) row
else row.copy(testDelayMillis = 0L)
}
)
}
val request = testRequests.beginBulk(groupId)
val message = TestServiceMessage(
key = AppConfig.MSG_MEASURE_CONFIG_START,
subscriptionId = groupId,
serverGuids = if (keywordFilter.isNotEmpty()) serverGuids else emptyList(),
onlyTcp = onlyTcp
)
_uiState.update {
it.copy(
isTesting = true,
status = MainStatus.Testing
)
}
viewModelScope.launch(ioDispatcher) {
cacheMutex.withLock { groupDataCache.remove(groupId) }
dataSource.sendMsg2TestService(
TestServiceMessage(
key = AppConfig.MSG_MEASURE_CONFIG_START,
subscriptionId = groupId,
serverGuids = if (keywordFilter.isNotEmpty()) servers.map { it.guid } else emptyList(),
onlyTcp = onlyTcp
)
)
bulkTestJob = viewModelScope.launch {
withContext(ioDispatcher) {
dataSource.clearAllTestDelayResults(serverGuids)
val resetGuids = serverGuids.toHashSet()
cacheMutex.withLock {
groupDataCache[groupId]?.let { cached ->
groupDataCache[groupId] = cached.map { server ->
if (server.guid !in resetGuids || server.testDelayMillis == 0L) server
else server.copy(testDelayMillis = 0L)
}
}
}
}
dataSource.sendMsg2TestService(message, request.id)
}
}
fun testCurrentServerRealPing() {
_uiState.update { it.copy(status = MainStatus.Testing) }
dataSource.testCurrentServerRealPing()
private fun cancelPendingTestResults() {
testResultFlushJob?.cancel()
testResultFlushJob = null
pendingTestResults.clear()
}
private fun onTestsFinished() {
fun testCurrentServerRealPing() {
if (!uiState.value.isRunning) return
val requestId = testRequests.beginCurrent()
_uiState.update { it.copy(isTesting = true, status = MainStatus.Testing) }
dataSource.testCurrentServerRealPing(requestId)
}
private fun onTestsFinished(requestId: String) {
if (testRequests.completeBulk(requestId) == null) return
resetTestStatus()
viewModelScope.launch(ioDispatcher) {
cacheMutex.withLock { groupDataCache.clear() }
testingGroupId = null
_uiState.update {
it.copy(
isTesting = false,
status = if (it.isRunning) MainStatus.Connected else MainStatus.Disconnected
)
}
reloadAllGroups(_uiState.value.groups.map { it.id })
}
}
@@ -737,32 +911,31 @@ class MainViewModel(
val selected = dataSource.getSelectServer() ?: return
val profile = dataSource.decodeServerConfig(selected) ?: return
val groupId = profile.subscriptionId
val groupIndex =
_uiState.value.groups.indexOfFirst { it.id == groupId }.takeIf { it >= 0 } ?: return
if (_uiState.value.groups.none { it.id == groupId }) return
viewModelScope.launch(ioDispatcher) {
val position =
loadGroup(groupId).indexOfFirst { it.guid == selected }.takeIf { it >= 0 }
?: return@launch
updateGroupUi(groupId, loadGroup(groupId))
if (_uiState.value.selectedGroupId != groupId) {
dataSource.setSelectedSubscriptionId(groupId)
}
val target = LocateTarget(groupId, selected)
_uiState.update {
it.copy(locateTarget = LocateTarget(groupId, groupIndex, position))
it.copy(selectedGroupId = groupId, locateTarget = target)
}
}
}
fun getPosition(guid: String): Int = currentServers().indexOfFirst { it.guid == guid }
private fun consumeLocateTarget(target: LocateTarget) {
_uiState.update { state ->
if (state.locateTarget == target) state.copy(locateTarget = null) else state
}
private fun consumeLocateTarget() {
_uiState.update { it.copy(locateTarget = null) }
}
// ---------- Running state ----------
private fun updateRunningState(running: Boolean, clearTestingText: Boolean = true) {
if (!running || clearTestingText) testRequests.invalidateCurrent()
_uiState.update { state ->
state.copy(
isRunning = running,
status = if (!clearTestingText && state.isTesting) state.status
isTesting = testRequests.isTesting,
status = if (!clearTestingText && state.isRunning == running) state.status
else if (running) MainStatus.Connected else MainStatus.Disconnected
)
}
@@ -789,4 +962,8 @@ class MainViewModel(
throw IllegalArgumentException("Unknown ViewModel class")
}
}
private companion object {
const val TEST_RESULT_FLUSH_INTERVAL_MS = 500L
}
}
@@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
@@ -20,7 +21,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
@@ -41,14 +41,15 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.v2ray.ang.R
import com.v2ray.ang.dto.AppInfo
import com.v2ray.ang.extension.toastInfo
import com.v2ray.ang.extension.toastSuccess
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppDivider
import com.v2ray.ang.ui.compose.AppDropdownMenuItems
import com.v2ray.ang.ui.compose.AppListItem
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.ConfirmDialog
import com.v2ray.ang.ui.compose.ItemDivider
import com.v2ray.ang.ui.compose.NavigationBarsBottomPadding
import com.v2ray.ang.ui.compose.verticalScrollbar
import com.v2ray.ang.util.Utils
@@ -86,9 +87,6 @@ class PerAppProxyActivity : BaseComponentActivity() {
onBackClick = { finish() },
onPerAppProxyChanged = { viewModel.setPerAppProxyEnabled(it) },
onBypassAppsChanged = { viewModel.setBypassAppsEnabled(it) },
onInfoClick = {
toastInfo(R.string.summary_pref_per_app_proxy)
},
onToggleApp = { viewModel.toggle(it) },
onSearch = { viewModel.filterApps(it) },
onSelectAll = { viewModel.selectAll() },
@@ -117,7 +115,6 @@ fun PerAppProxyScreen(
onBackClick: () -> Unit,
onPerAppProxyChanged: (Boolean) -> Unit,
onBypassAppsChanged: (Boolean) -> Unit,
onInfoClick: () -> Unit,
onToggleApp: (String) -> Unit,
onSearch: (String) -> Unit,
onSelectAll: () -> Unit,
@@ -129,6 +126,8 @@ fun PerAppProxyScreen(
var showSearch by rememberSaveable { mutableStateOf(false) }
var searchQuery by rememberSaveable { mutableStateOf("") }
var showMenu by remember { mutableStateOf(false) }
var showInfoDialog by rememberSaveable { mutableStateOf(false) }
val onInfoClick = { showInfoDialog = true }
val listState = rememberLazyListState()
LaunchedEffect(Unit) {
@@ -136,7 +135,7 @@ fun PerAppProxyScreen(
}
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.per_app_proxy_settings),
@@ -167,7 +166,7 @@ fun PerAppProxyScreen(
IconButton(onClick = { showMenu = true }) {
Icon(
painterResource(R.drawable.ic_more_vert_24dp),
contentDescription = null
contentDescription = stringResource(R.string.acc_more)
)
}
DropdownMenu(
@@ -263,7 +262,8 @@ fun PerAppProxyScreen(
state = listState,
modifier = Modifier
.fillMaxSize()
.verticalScrollbar(listState)
.verticalScrollbar(listState),
contentPadding = NavigationBarsBottomPadding()
) {
items(items = apps, key = { it.packageName }) { app ->
val checked = blacklist.contains(app.packageName)
@@ -279,4 +279,13 @@ fun PerAppProxyScreen(
}
}
}
if (showInfoDialog) {
ConfirmDialog(
message = stringResource(R.string.summary_pref_per_app_proxy),
dismissText = null,
onConfirm = {},
onDismiss = { showInfoDialog = false },
)
}
}
@@ -6,6 +6,7 @@ import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
@@ -18,7 +19,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
@@ -37,7 +37,6 @@ import com.v2ray.ang.AppConfig.TAG_PROXY
import com.v2ray.ang.R
import com.v2ray.ang.dto.entities.RulesetItem
import com.v2ray.ang.extension.nullIfBlank
import com.v2ray.ang.extension.toast
import com.v2ray.ang.extension.toastSuccess
import com.v2ray.ang.handler.SettingsManager
import com.v2ray.ang.ui.apppicker.AppPickerActivity
@@ -46,6 +45,7 @@ import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.FormDropdownField
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SettingsSwitchItem
import com.v2ray.ang.ui.compose.verticalScrollbar
import kotlinx.coroutines.Dispatchers
@@ -85,7 +85,6 @@ class RoutingEditActivity : BaseComponentActivity() {
private fun saveServer(rulesetItem: RulesetItem): Boolean {
if (rulesetItem.remarks.isNullOrEmpty()) {
toast(R.string.sub_setting_remarks)
return false
}
if (position < 0 && rulesetItem.id.isEmpty()) {
@@ -123,6 +122,7 @@ fun RoutingEditScreen(
val scrollState = rememberScrollState()
var remarks by rememberSaveable { mutableStateOf(initial?.remarks ?: "") }
var isRemarksError by rememberSaveable { mutableStateOf(false) }
var locked by rememberSaveable { mutableStateOf(initial?.locked == true) }
var domain by rememberSaveable { mutableStateOf(initial?.domain?.joinToString(",") ?: "") }
var ip by rememberSaveable { mutableStateOf(initial?.ip?.joinToString(",") ?: "") }
@@ -178,7 +178,7 @@ fun RoutingEditScreen(
}
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.routing_settings_rule_title),
@@ -192,7 +192,15 @@ fun RoutingEditScreen(
)
}
}
IconButton(onClick = { onSave(buildRuleset()) }) {
IconButton(onClick = {
val remarksErr = remarks.isBlank()
isRemarksError = remarksErr
val hasError = remarksErr
if (!hasError) {
onSave(buildRuleset())
}
}) {
Icon(
painterResource(R.drawable.ic_fab_check),
contentDescription = stringResource(R.string.acc_save)
@@ -215,7 +223,8 @@ fun RoutingEditScreen(
FormTextField(
label = stringResource(R.string.sub_setting_remarks),
value = remarks,
onValueChange = { remarks = it }
onValueChange = { remarks = it },
isError = isRemarksError
)
SettingsSwitchItem(
title = stringResource(R.string.routing_settings_locked),
@@ -302,11 +311,13 @@ fun RoutingEditScreen(
editable = true
)
Spacer(modifier = Modifier.height(36.dp))
NavigationBarsSpacer()
}
if (showDeleteConfirm) {
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_routing_rule),
itemName = initial?.remarks.orEmpty(),
onConfirm = onDelete,
onDismiss = { showDeleteConfirm = false }
)
@@ -8,6 +8,7 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
@@ -22,7 +23,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Switch
import androidx.compose.material3.SwitchDefaults
import androidx.compose.material3.Text
@@ -54,6 +54,7 @@ import com.v2ray.ang.ui.base.HelperBaseComponentActivity
import com.v2ray.ang.ui.compose.AppDropdownMenuItems
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.ItemDivider
import com.v2ray.ang.ui.compose.NavigationBarsBottomPadding
import com.v2ray.ang.ui.compose.ReorderableListItem
import com.v2ray.ang.ui.compose.SelectListDialog
import com.v2ray.ang.ui.compose.SettingsListItem
@@ -215,7 +216,7 @@ fun RoutingSettingScreen(
}
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = stringResource(R.string.routing_settings_title),
@@ -231,7 +232,7 @@ fun RoutingSettingScreen(
IconButton(onClick = { showMenu = true }) {
Icon(
painterResource(R.drawable.ic_more_vert_24dp),
contentDescription = null
contentDescription = stringResource(R.string.acc_more)
)
}
DropdownMenu(
@@ -259,7 +260,8 @@ fun RoutingSettingScreen(
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.verticalScrollbar(lazyListState)
.verticalScrollbar(lazyListState),
contentPadding = NavigationBarsBottomPadding()
) {
item(key = "domain_strategy") {
SettingsListItem(
@@ -4,6 +4,7 @@ import android.os.Bundle
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.imePadding
@@ -14,7 +15,6 @@ import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
@@ -44,6 +44,7 @@ import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.FormDropdownField
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SettingsSwitchItem
import com.v2ray.ang.ui.compose.verticalScrollbar
import com.v2ray.ang.util.JsonUtil
@@ -104,20 +105,23 @@ abstract class BaseServerActivity : BaseComponentActivity() {
) {
Column(verticalArrangement = Arrangement.spacedBy(8.dp)) {
FormTextField(
stringResource(R.string.server_lab_remarks),
state.remarks,
{ state.remarks = it }
label = stringResource(R.string.server_lab_remarks),
value = state.remarks,
onValueChange = { state.remarks = it },
isError = state.isRemarksError
)
FormTextField(
stringResource(R.string.server_lab_address),
state.address,
{ state.address = it }
label = stringResource(R.string.server_lab_address),
value = state.address,
onValueChange = { state.address = it },
isError = state.isAddressError
)
FormTextField(
stringResource(R.string.server_lab_port),
state.port,
{ state.port = it },
keyboardType = KeyboardType.Number
label = stringResource(R.string.server_lab_port),
value = state.port,
onValueChange = { state.port = it },
keyboardType = KeyboardType.Number,
isError = state.isPortError
)
}
}
@@ -364,33 +368,27 @@ abstract class BaseServerActivity : BaseComponentActivity() {
}
protected fun validateBasicConfig(state: ServerUiState): Boolean {
if (state.remarks.isBlank()) {
toast(R.string.server_lab_remarks)
return false
}
if (state.address.isBlank()) {
toast(R.string.server_lab_address)
return false
}
if (
state.configType != EConfigType.HYSTERIA2 &&
(state.port.toIntOrNull() ?: 0) <= 0
) {
toast(R.string.server_lab_port)
return false
}
return true
val remarksErr = state.remarks.isBlank()
val addressErr = state.address.isBlank()
val portErr = state.configType != EConfigType.HYSTERIA2 && (state.port.toIntOrNull() ?: 0) <= 0
state.isRemarksError = remarksErr
state.isAddressError = addressErr
state.isPortError = portErr
val hasError = remarksErr || addressErr || portErr
return !hasError
}
protected open fun validateProtocolConfig(config: ProfileItem): Boolean = true
protected open fun validateCommonConfig(config: ProfileItem): Boolean {
protected open fun validateCommonConfig(state: ServerUiState, config: ProfileItem): Boolean {
if (config.password.isNullOrBlank()) {
state.isPasswordError = true
if (config.configType == EConfigType.VMESS ||
config.configType == EConfigType.VLESS
) {
toast(R.string.server_lab_id)
return false
}
@@ -398,7 +396,6 @@ abstract class BaseServerActivity : BaseComponentActivity() {
config.configType == EConfigType.SHADOWSOCKS ||
config.configType == EConfigType.HYSTERIA2
) {
toast(R.string.server_lab_id3)
return false
}
}
@@ -424,7 +421,7 @@ abstract class BaseServerActivity : BaseComponentActivity() {
protected fun saveServer(state: ServerUiState): Boolean {
if (!validateBasicConfig(state)) return false
val config = state.toProfileItem(initialConfig)
if (!validateCommonConfig(config)) return false
if (!validateCommonConfig(state, config)) return false
if (!validateProtocolConfig(config)) return false
config.description = AngConfigManager.generateDescription(config)
@@ -448,7 +445,7 @@ abstract class BaseServerActivity : BaseComponentActivity() {
var showDeleteDialog by rememberSaveable { mutableStateOf(false) }
val scrollState = rememberScrollState()
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = title,
@@ -481,13 +478,16 @@ abstract class BaseServerActivity : BaseComponentActivity() {
.verticalScroll(scrollState)
.verticalScrollbar(scrollState)
.padding(bottom = 36.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
content = content
)
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
content()
NavigationBarsSpacer()
}
}
if (showDeleteDialog) {
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_profile),
itemName = initialConfig.remarks,
onConfirm = {
showDeleteDialog = false
deleteServer(editGuid)
@@ -0,0 +1,14 @@
package com.v2ray.ang.ui.server
/** Removes one draft member and its row key together, resolving its current position at confirmation. */
internal fun withoutProxyChainMember(
members: List<String>,
memberKeys: List<String>,
memberKey: String,
): Pair<List<String>, List<String>> {
val index = memberKeys.indexOf(memberKey)
if (index < 0) return members to memberKeys
return members.toMutableList().also { it.removeAt(index) } to
memberKeys.toMutableList().also { it.removeAt(index) }
}
@@ -1,524 +0,0 @@
package com.v2ray.ang.ui.server
import android.os.Bundle
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringArrayResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import com.v2ray.ang.AppConfig.DEFAULT_PORT
import com.v2ray.ang.AppConfig.REALITY
import com.v2ray.ang.AppConfig.TLS
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_ADDRESS_V4
import com.v2ray.ang.AppConfig.WIREGUARD_LOCAL_MTU
import com.v2ray.ang.R
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.enums.EConfigType
import com.v2ray.ang.enums.NetworkType
import com.v2ray.ang.extension.nullIfBlank
import com.v2ray.ang.extension.toast
import com.v2ray.ang.extension.toastSuccess
import com.v2ray.ang.handler.AngConfigManager
import com.v2ray.ang.handler.CertificateFingerprintManager
import com.v2ray.ang.handler.MmkvManager
import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.FormDropdownField
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.SettingsSwitchItem
import com.v2ray.ang.ui.compose.verticalScrollbar
import com.v2ray.ang.util.JsonUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class ServerActivity : BaseComponentActivity() {
private val editGuid by lazy { intent.getStringExtra("guid").orEmpty() }
private val isRunning by lazy {
intent.getBooleanExtra("isRunning", false)
&& editGuid.isNotEmpty()
&& editGuid == MmkvManager.getSelectServer()
}
private val createConfigType by lazy {
EConfigType.fromInt(intent.getIntExtra("createConfigType", EConfigType.VMESS.value))
?: EConfigType.VMESS
}
private val subscriptionId by lazy { intent.getStringExtra("subscriptionId") }
private lateinit var initialConfig: ProfileItem
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val existingConfig = MmkvManager.decodeServerConfig(editGuid)
initialConfig = existingConfig ?: ProfileItem.create(createConfigType)
}
@Composable
override fun ScreenContent() {
ServerScreen(
guid = editGuid,
configType = initialConfig.configType,
initialConfig = initialConfig,
isRunning = isRunning,
onBackClick = { finish() },
onSave = { saveServer(it) },
onDelete = { deleteServer(editGuid, isRunning) }
)
}
private fun saveServer(config: ProfileItem): Boolean {
if (config.remarks.isBlank()) {
toast(R.string.server_lab_remarks)
return false
}
if (config.server.isNullOrBlank()) {
toast(R.string.server_lab_address)
return false
}
if (
config.configType != EConfigType.HYSTERIA2 &&
(config.serverPort?.toIntOrNull() ?: 0) <= 0
) {
toast(R.string.server_lab_port)
return false
}
if (
config.configType != EConfigType.SOCKS &&
config.configType != EConfigType.HTTP &&
config.password.isNullOrBlank()
) {
val message = when (config.configType) {
EConfigType.TROJAN,
EConfigType.SHADOWSOCKS,
EConfigType.HYSTERIA2 -> R.string.server_lab_id3
else -> R.string.server_lab_id
}
toast(message)
return false
}
if (
config.configType == EConfigType.TROJAN &&
config.security.isNullOrBlank()
) {
toast(R.string.server_lab_stream_security)
return false
}
if (
!config.xhttpExtra.isNullOrBlank() &&
JsonUtil.parseString(config.xhttpExtra) == null
) {
toast(R.string.server_lab_xhttp_extra)
return false
}
if (
!config.finalMask.isNullOrBlank() &&
JsonUtil.parseString(config.finalMask) == null
) {
toast(R.string.server_lab_final_mask)
return false
}
config.description =
AngConfigManager.generateDescription(config)
if (
config.subscriptionId.isEmpty() &&
!subscriptionId.isNullOrEmpty()
) {
config.subscriptionId = subscriptionId.orEmpty()
}
val savedGuid = MmkvManager.encodeServerConfig(
editGuid,
config
)
toastSuccess(R.string.toast_success)
ProfileEditorResult.run {
finishSaved(
guid = savedGuid,
restartService = isRunning
)
}
return true
}
private fun deleteServer(
guid: String,
isRunning: Boolean
) {
if (
guid.isEmpty() ||
guid == MmkvManager.getSelectServer()
) {
toast(R.string.toast_action_not_allowed)
return
}
MmkvManager.removeServer(guid)
ProfileEditorResult.run {
finishDeleted(guid)
}
}
}
@Composable
fun ServerScreen(
guid: String,
configType: EConfigType,
initialConfig: ProfileItem,
isRunning: Boolean,
onBackClick: () -> Unit,
onSave: (ProfileItem) -> Boolean,
onDelete: () -> Unit
) {
val context = LocalContext.current
val securityOptions = stringArrayResource(R.array.securitys).toList()
val ssSecurityOptions = stringArrayResource(R.array.ss_securitys).toList()
val flowOptions = stringArrayResource(R.array.flows).toList()
val networkOptions = stringArrayResource(R.array.networks).toList()
val tcpHeaderOptions = stringArrayResource(R.array.header_type_tcp).toList()
val kcpHeaderOptions = stringArrayResource(R.array.header_type_kcp_and_quic).toList()
val grpcModeOptions = stringArrayResource(R.array.mode_type_grpc).toList()
val xhttpModeOptions = stringArrayResource(R.array.xhttp_mode).toList()
val streamSecurityOptions = stringArrayResource(R.array.streamsecurityxs).toList()
val uTlsOptions = stringArrayResource(R.array.streamsecurity_utls).toList()
val alpnOptions = stringArrayResource(R.array.streamsecurity_alpn).toList()
val browserDialerOptions = stringArrayResource(R.array.browser_dialer_mode_value).toList()
var remarks by rememberSaveable { mutableStateOf(initialConfig.remarks) }
var address by rememberSaveable { mutableStateOf(initialConfig.server ?: "") }
var port by rememberSaveable { mutableStateOf(initialConfig.serverPort ?: DEFAULT_PORT.toString()) }
var password by rememberSaveable { mutableStateOf(initialConfig.password ?: "") }
var method by rememberSaveable { mutableStateOf(initialConfig.method ?: "") }
var flow by rememberSaveable { mutableStateOf(initialConfig.flow ?: "") }
var encryption by rememberSaveable { mutableStateOf(initialConfig.method ?: "") }
var username by rememberSaveable { mutableStateOf(initialConfig.username ?: "") }
var secretKey by rememberSaveable { mutableStateOf(initialConfig.secretKey ?: "") }
var publicKey by rememberSaveable { mutableStateOf(initialConfig.publicKey ?: "") }
var preSharedKey by rememberSaveable { mutableStateOf(initialConfig.preSharedKey ?: "") }
var reserved by rememberSaveable { mutableStateOf(initialConfig.reserved ?: "0,0,0") }
var localAddress by rememberSaveable { mutableStateOf(initialConfig.localAddress ?: WIREGUARD_LOCAL_ADDRESS_V4) }
var mtu by rememberSaveable { mutableStateOf(initialConfig.mtu?.toString() ?: WIREGUARD_LOCAL_MTU) }
var obfsPassword by rememberSaveable { mutableStateOf(initialConfig.obfsPassword ?: "") }
var portHopping by rememberSaveable { mutableStateOf(initialConfig.portHopping ?: "") }
var portHoppingInterval by rememberSaveable { mutableStateOf(initialConfig.portHoppingInterval ?: "") }
var bandwidthDown by rememberSaveable { mutableStateOf(initialConfig.bandwidthDown ?: "") }
var bandwidthUp by rememberSaveable { mutableStateOf(initialConfig.bandwidthUp ?: "") }
var network by rememberSaveable { mutableStateOf(initialConfig.network ?: NetworkType.TCP.type) }
var headerType by rememberSaveable { mutableStateOf(initialConfig.headerType ?: "none") }
var host by rememberSaveable { mutableStateOf(initialConfig.host ?: "") }
var path by rememberSaveable { mutableStateOf(initialConfig.path ?: "") }
var seed by rememberSaveable { mutableStateOf(initialConfig.seed ?: "") }
var quicSecurity by rememberSaveable { mutableStateOf(initialConfig.quicSecurity ?: "") }
var quicKey by rememberSaveable { mutableStateOf(initialConfig.quicKey ?: "") }
var mode by rememberSaveable { mutableStateOf(initialConfig.mode ?: "") }
var serviceName by rememberSaveable { mutableStateOf(initialConfig.serviceName ?: "") }
var authority by rememberSaveable { mutableStateOf(initialConfig.authority ?: "") }
var xhttpMode by rememberSaveable { mutableStateOf(initialConfig.xhttpMode ?: "") }
var xhttpExtra by rememberSaveable { mutableStateOf(initialConfig.xhttpExtra ?: "") }
var finalMask by rememberSaveable { mutableStateOf(initialConfig.finalMask ?: "") }
var kcpMtu by rememberSaveable { mutableStateOf(initialConfig.kcpMtu?.toString() ?: "") }
var kcpTti by rememberSaveable { mutableStateOf(initialConfig.kcpTti?.toString() ?: "") }
var browserDialerMode by rememberSaveable { mutableStateOf(initialConfig.browserDialerMode ?: "") }
var streamSecurity by rememberSaveable { mutableStateOf(initialConfig.security ?: "") }
var sni by rememberSaveable { mutableStateOf(initialConfig.sni ?: "") }
var allowInsecure by rememberSaveable { mutableStateOf(initialConfig.insecure == true) }
var fingerPrint by rememberSaveable { mutableStateOf(initialConfig.fingerPrint ?: "") }
var alpn by rememberSaveable { mutableStateOf(initialConfig.alpn ?: "") }
var publicKeyReality by rememberSaveable { mutableStateOf(initialConfig.publicKey ?: "") }
var shortId by rememberSaveable { mutableStateOf(initialConfig.shortId ?: "") }
var spiderX by rememberSaveable { mutableStateOf(initialConfig.spiderX ?: "") }
var mldsa65Verify by rememberSaveable { mutableStateOf(initialConfig.mldsa65Verify ?: "") }
var echConfigList by rememberSaveable { mutableStateOf(initialConfig.echConfigList ?: "") }
var verifyPeerCertByName by rememberSaveable { mutableStateOf(initialConfig.verifyPeerCertByName ?: "") }
var pinnedCA256 by rememberSaveable { mutableStateOf(initialConfig.pinnedCA256 ?: "") }
val isVmess = configType == EConfigType.VMESS
val isVless = configType == EConfigType.VLESS
val isShadowsocks = configType == EConfigType.SHADOWSOCKS
val isSocksOrHttp = configType == EConfigType.SOCKS || configType == EConfigType.HTTP
val isTrojan = configType == EConfigType.TROJAN
val isWireguard = configType == EConfigType.WIREGUARD
val isHysteria2 = configType == EConfigType.HYSTERIA2
var showDeleteDialog by rememberSaveable { mutableStateOf(false) }
var isFetchingCert by rememberSaveable { mutableStateOf(false) }
val scope = rememberCoroutineScope()
val listState = rememberLazyListState()
fun buildProfileItem(): ProfileItem = initialConfig.copy(
remarks = remarks,
server = address,
serverPort = port,
password = password,
method = when {
isVmess || isShadowsocks -> method
isVless -> encryption
else -> null
},
flow = if (isVless) flow else null,
username = if (isSocksOrHttp) username else null,
secretKey = if (isWireguard) secretKey else null,
publicKey = when {
isWireguard -> publicKey
streamSecurity == REALITY -> publicKeyReality
else -> null
},
preSharedKey = if (isWireguard) preSharedKey else null,
reserved = if (isWireguard) reserved else null,
localAddress = if (isWireguard) localAddress else null,
mtu = if (isWireguard) mtu.toIntOrNull() else null,
obfsPassword = if (isHysteria2) obfsPassword else null,
portHopping = if (isHysteria2) portHopping else null,
portHoppingInterval = if (isHysteria2) portHoppingInterval else null,
bandwidthDown = if (isHysteria2) bandwidthDown else null,
bandwidthUp = if (isHysteria2) bandwidthUp else null,
network = network,
headerType = headerType,
host = host,
path = path,
seed = seed,
quicSecurity = quicSecurity,
quicKey = quicKey,
mode = mode,
serviceName = serviceName,
authority = authority,
xhttpMode = xhttpMode,
xhttpExtra = xhttpExtra.nullIfBlank(),
finalMask = finalMask.nullIfBlank(),
kcpMtu = kcpMtu.toIntOrNull(),
kcpTti = kcpTti.toIntOrNull(),
browserDialerMode = if (network in listOf(NetworkType.WS.type, NetworkType.XHTTP.type)) browserDialerMode.nullIfBlank() else null,
security = streamSecurity,
sni = sni,
insecure = allowInsecure,
fingerPrint = fingerPrint,
alpn = alpn,
shortId = shortId,
spiderX = spiderX,
mldsa65Verify = mldsa65Verify,
echConfigList = echConfigList,
verifyPeerCertByName = verifyPeerCertByName,
pinnedCA256 = pinnedCA256
)
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
topBar = {
AppTopBar(
title = configType.toString(),
onBackClick = onBackClick,
actions = {
if (guid.isNotEmpty() && !isRunning) {
IconButton(onClick = { showDeleteDialog = true }) {
Icon(painterResource(R.drawable.ic_delete_24dp), stringResource(R.string.acc_delete))
}
}
IconButton(onClick = {
onSave(buildProfileItem())
}) {
Icon(painterResource(R.drawable.ic_fab_check), stringResource(R.string.acc_save))
}
}
)
}
) { innerPadding ->
LazyColumn(
state = listState,
modifier = Modifier
.fillMaxSize()
.padding(innerPadding)
.consumeWindowInsets(innerPadding)
.imePadding()
.verticalScrollbar(listState),
contentPadding = PaddingValues(bottom = 36.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
item { FormTextField(stringResource(R.string.server_lab_remarks), remarks, { remarks = it }) }
item { FormTextField(stringResource(R.string.server_lab_address), address, { address = it }) }
if (configType != EConfigType.HYSTERIA2) item { FormTextField(stringResource(R.string.server_lab_port), port, { port = it }, keyboardType = KeyboardType.Number) }
when {
isVmess || isTrojan || isShadowsocks || isHysteria2 -> item {
FormTextField(
stringResource(
when {
isTrojan || isShadowsocks || isHysteria2 -> R.string.server_lab_id3
else -> R.string.server_lab_id
}
), password, { password = it })
}
isVless -> {
item { FormTextField(stringResource(R.string.server_lab_id), password, { password = it }) }
item { FormTextField(stringResource(R.string.server_lab_encryption), encryption, { encryption = it }) }
item { FormDropdownField(stringResource(R.string.server_lab_flow), flow, flowOptions, { flow = it }) }
}
isSocksOrHttp -> {
item { FormTextField(stringResource(R.string.server_lab_security4), username, { username = it }) }
item { FormTextField(stringResource(R.string.server_lab_id4), password, { password = it }) }
}
}
if (isVmess) item { FormDropdownField(stringResource(R.string.server_lab_security), method, securityOptions, { method = it }) }
if (isShadowsocks) item { FormDropdownField(stringResource(R.string.server_lab_security), method, ssSecurityOptions, { method = it }) }
if (isWireguard) {
item { FormTextField(stringResource(R.string.server_lab_secret_key), secretKey, { secretKey = it }) }
item { FormTextField(stringResource(R.string.server_lab_public_key), publicKey, { publicKey = it }) }
item { FormTextField(stringResource(R.string.server_lab_preshared_key), preSharedKey, { preSharedKey = it }) }
item { FormTextField(stringResource(R.string.server_lab_reserved), reserved, { reserved = it }) }
item { FormTextField(stringResource(R.string.server_lab_local_address), localAddress, { localAddress = it }) }
item { FormTextField(stringResource(R.string.server_lab_local_mtu), mtu, { mtu = it }, keyboardType = KeyboardType.Number) }
}
if (isHysteria2) {
item { FormTextField(stringResource(R.string.server_obfs_password), obfsPassword, { obfsPassword = it }) }
item { FormTextField(stringResource(R.string.server_lab_port_hop), portHopping, { portHopping = it }) }
item { FormTextField(stringResource(R.string.server_lab_port_hop_interval), portHoppingInterval, { portHoppingInterval = it }) }
item { FormTextField(stringResource(R.string.server_lab_bandwidth_down), bandwidthDown, { bandwidthDown = it }) }
item { FormTextField(stringResource(R.string.server_lab_bandwidth_up), bandwidthUp, { bandwidthUp = it }) }
}
item { FormDropdownField(stringResource(R.string.server_lab_network), network, networkOptions, { network = it }) }
val headerOptions = when (network) {
NetworkType.TCP.type -> tcpHeaderOptions
NetworkType.KCP.type -> kcpHeaderOptions
NetworkType.GRPC.type -> grpcModeOptions
NetworkType.XHTTP.type -> xhttpModeOptions
else -> listOf("---")
}
if (headerOptions.size > 1) {
item {
FormDropdownField(
stringResource(
when (network) {
NetworkType.GRPC.type -> R.string.server_lab_mode_type; NetworkType.XHTTP.type -> R.string.server_lab_xhttp_mode; else -> R.string.server_lab_head_type
}
),
headerType, headerOptions, { headerType = it }
)
}
}
item {
FormTextField(
stringResource(
when (network) {
NetworkType.TCP.type, NetworkType.HTTP_UPGRADE.type, NetworkType.XHTTP.type, NetworkType.H2.type -> R.string.server_lab_request_host_http
NetworkType.WS.type -> R.string.server_lab_request_host_ws
NetworkType.GRPC.type -> R.string.server_lab_request_host_grpc
else -> R.string.server_lab_request_host6
}
), host, { host = it })
}
item {
FormTextField(
stringResource(
when (network) {
NetworkType.KCP.type -> R.string.server_lab_path_kcp
NetworkType.WS.type -> R.string.server_lab_path_ws
NetworkType.HTTP_UPGRADE.type -> R.string.server_lab_path_httpupgrade
NetworkType.XHTTP.type -> R.string.server_lab_path_xhttp
NetworkType.H2.type -> R.string.server_lab_path_h2
NetworkType.GRPC.type -> R.string.server_lab_path_grpc
else -> R.string.server_lab_path
}
), path, { path = it })
}
if (network == NetworkType.XHTTP.type) {
item { FormTextField(stringResource(R.string.server_lab_xhttp_extra), xhttpExtra, { xhttpExtra = it }) }
}
if (network == NetworkType.KCP.type) {
item { FormTextField(stringResource(R.string.server_lab_kcp_mtu), kcpMtu, { kcpMtu = it }, keyboardType = KeyboardType.Number) }
item { FormTextField(stringResource(R.string.server_lab_kcp_tti), kcpTti, { kcpTti = it }, keyboardType = KeyboardType.Number) }
}
item { FormTextField(stringResource(R.string.server_lab_final_mask), finalMask, { finalMask = it }) }
if (network == NetworkType.WS.type || network == NetworkType.XHTTP.type) {
item { FormDropdownField(stringResource(R.string.server_lab_browser_dialer), browserDialerMode, browserDialerOptions, { browserDialerMode = it }) }
}
item { FormDropdownField(stringResource(R.string.server_lab_stream_security), streamSecurity, streamSecurityOptions, { streamSecurity = it }) }
if (streamSecurity.isNotBlank()) {
item { FormTextField(stringResource(R.string.server_lab_sni), sni, { sni = it }) }
item { FormDropdownField(stringResource(R.string.server_lab_stream_fingerprint), fingerPrint, uTlsOptions, { fingerPrint = it }) }
if (streamSecurity == TLS) {
item { SettingsSwitchItem(title = stringResource(R.string.server_lab_allow_insecure), checked = allowInsecure, onCheckedChange = { allowInsecure = it }) }
item { FormDropdownField(stringResource(R.string.server_lab_stream_alpn), alpn, alpnOptions, { alpn = it }) }
item { FormTextField(stringResource(R.string.server_lab_ech_config_list), echConfigList, { echConfigList = it }) }
item { FormTextField(stringResource(R.string.server_lab_verify_peer_cert_by_name), verifyPeerCertByName, { verifyPeerCertByName = it }) }
item { FormTextField(stringResource(R.string.server_lab_pinned_ca256), pinnedCA256, { pinnedCA256 = it }) }
item {
Button(
onClick = {
if (address.isBlank()) {
context.toast(R.string.server_lab_address); return@Button
}
if (configType != EConfigType.HYSTERIA2 && (port.toIntOrNull() ?: 0) <= 0) {
context.toast(R.string.server_lab_port); return@Button
}
val temp = buildProfileItem()
scope.launch {
isFetchingCert = true
try {
val sha256 = withContext(Dispatchers.IO) { CertificateFingerprintManager.fetchForManualFill(temp) }
if (sha256.isNullOrBlank()) context.toast(R.string.toast_fetch_cert_sha256_failed) else {
pinnedCA256 = sha256
context.toastSuccess(R.string.toast_fetch_cert_sha256_success)
}
} finally {
isFetchingCert = false
}
}
},
enabled = !isFetchingCert,
modifier = Modifier.padding(start = 16.dp)
) { Text(stringResource(R.string.pinned_ca256_action_fetch)) }
}
} else if (streamSecurity == REALITY) {
item { FormTextField(stringResource(R.string.server_lab_public_key), publicKeyReality, { publicKeyReality = it }) }
item { FormTextField(stringResource(R.string.server_lab_short_id), shortId, { shortId = it }) }
item { FormTextField(stringResource(R.string.server_lab_spider_x), spiderX, { spiderX = it }) }
item { FormTextField(stringResource(R.string.server_lab_mldsa65_verify), mldsa65Verify, { mldsa65Verify = it }) }
}
}
}
}
if (showDeleteDialog) {
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_profile),
onConfirm = { showDeleteDialog = false; onDelete() },
onDismiss = { showDeleteDialog = false }
)
}
}
@@ -6,6 +6,7 @@ import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
@@ -25,7 +26,6 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
@@ -66,6 +66,7 @@ import com.v2ray.ang.ui.base.BaseComponentActivity
import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.horizontalScrollbar
import com.v2ray.ang.ui.compose.verticalScrollbar
import com.v2ray.ang.util.LogUtil
@@ -108,7 +109,6 @@ class ServerCustomConfigActivity : BaseComponentActivity() {
content: String
): Boolean {
if (remarks.isBlank()) {
toast(R.string.server_lab_remarks)
return false
}
@@ -206,6 +206,7 @@ fun ServerCustomConfigScreen(
onDelete: () -> Unit
) {
var remarks by rememberSaveable { mutableStateOf(initialRemarks) }
var isRemarksError by rememberSaveable { mutableStateOf(false) }
val textFieldState = rememberTextFieldState(initialText = initialContent)
var showDeleteConfirm by remember { mutableStateOf(false) }
val showDelete = editGuid.isNotEmpty() && !isRunning
@@ -310,7 +311,7 @@ fun ServerCustomConfigScreen(
}
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = EConfigType.CUSTOM.toString(),
@@ -324,7 +325,15 @@ fun ServerCustomConfigScreen(
)
}
}
IconButton(onClick = { onSave(remarks, textFieldState.text.toString()) }) {
IconButton(onClick = {
val remarksErr = remarks.isBlank()
isRemarksError = remarksErr
val hasError = remarksErr
if (!hasError) {
onSave(remarks, textFieldState.text.toString())
}
}) {
Icon(
painterResource(R.drawable.ic_fab_check),
contentDescription = stringResource(R.string.acc_save)
@@ -344,7 +353,8 @@ fun ServerCustomConfigScreen(
FormTextField(
label = stringResource(R.string.server_lab_remarks),
value = remarks,
onValueChange = { remarks = it }
onValueChange = { remarks = it },
isError = isRemarksError
)
Box(
@@ -478,12 +488,14 @@ fun ServerCustomConfigScreen(
.horizontalScrollbar(scrollState = horizontalScroll)
)
}
NavigationBarsSpacer()
}
}
if (showDeleteConfirm) {
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_profile),
itemName = initialRemarks,
onConfirm = {
showDeleteConfirm = false
onDelete()
@@ -2,6 +2,7 @@ package com.v2ray.ang.ui.server
import android.os.Bundle
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.imePadding
@@ -11,7 +12,6 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.ScaffoldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -39,6 +39,7 @@ import com.v2ray.ang.ui.compose.AppTopBar
import com.v2ray.ang.ui.compose.DeleteConfirmDialog
import com.v2ray.ang.ui.compose.FormDropdownField
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.NavigationBarsSpacer
import com.v2ray.ang.ui.compose.SettingsSwitchItem
class ServerGroupActivity : BaseComponentActivity() {
@@ -115,7 +116,6 @@ class ServerGroupActivity : BaseComponentActivity() {
fallbackTag: String,
): Boolean {
if (remarks.isBlank()) {
toast(R.string.server_lab_remarks)
return false
}
@@ -226,6 +226,7 @@ fun ServerGroupScreen(
val typeEntries = stringArrayResource(R.array.policy_group_type).toList()
var remarks by rememberSaveable { mutableStateOf(initialRemarks) }
var isRemarksError by rememberSaveable { mutableStateOf(false) }
var filter by rememberSaveable { mutableStateOf(initialFilter) }
var typeValue by rememberSaveable { mutableStateOf(typeEntries.getOrNull(initialType).orEmpty()) }
var subValue by rememberSaveable { mutableStateOf(subDisplay.getOrNull(initialSubIndex).orEmpty()) }
@@ -237,7 +238,7 @@ fun ServerGroupScreen(
val supportsObservatory = BalancerStrategyType.from(selectedType).supportsObservatory
Scaffold(
contentWindowInsets = ScaffoldDefaults.contentWindowInsets,
contentWindowInsets = WindowInsets(0),
topBar = {
AppTopBar(
title = EConfigType.POLICYGROUP.toString(),
@@ -249,9 +250,15 @@ fun ServerGroupScreen(
}
}
IconButton(onClick = {
val typeIdx = typeEntries.indexOf(typeValue).coerceAtLeast(0)
val subIdx = subDisplay.indexOf(subValue).coerceAtLeast(0)
onSave(remarks, filter, typeIdx, subIdx, testOutbounds, fallbackTag)
val remarksErr = remarks.isBlank()
isRemarksError = remarksErr
val hasError = remarksErr
if (!hasError) {
val typeIdx = typeEntries.indexOf(typeValue).coerceAtLeast(0)
val subIdx = subDisplay.indexOf(subValue).coerceAtLeast(0)
onSave(remarks, filter, typeIdx, subIdx, testOutbounds, fallbackTag)
}
}) {
Icon(painterResource(R.drawable.ic_fab_check), contentDescription = stringResource(R.string.acc_save))
}
@@ -268,7 +275,12 @@ fun ServerGroupScreen(
.padding(vertical = 8.dp)
.verticalScroll(rememberScrollState())
) {
FormTextField(stringResource(R.string.server_lab_remarks), remarks, { remarks = it })
FormTextField(
label = stringResource(R.string.server_lab_remarks),
value = remarks,
onValueChange = { remarks = it },
isError = isRemarksError
)
FormDropdownField(
label = stringResource(R.string.title_policy_group_type),
value = typeValue,
@@ -297,6 +309,7 @@ fun ServerGroupScreen(
editable = true
)
}
NavigationBarsSpacer()
}
}
}
@@ -304,6 +317,7 @@ fun ServerGroupScreen(
if (showDeleteConfirm) {
DeleteConfirmDialog(
message = stringResource(R.string.confirm_delete_policy_group),
itemName = initialRemarks,
onConfirm = onDelete,
onDismiss = { showDeleteConfirm = false }
)
@@ -1,14 +1,12 @@
package com.v2ray.ang.ui.server
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.res.stringResource
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.entities.ProfileItem
import com.v2ray.ang.enums.EConfigType
import com.v2ray.ang.extension.toast
import com.v2ray.ang.ui.compose.FormTextField
import com.v2ray.ang.ui.compose.SettingsSwitchItem
@@ -18,7 +16,6 @@ class ServerHysteria2Activity : BaseServerActivity() {
@Composable
override fun ScreenContent() {
val scope = rememberCoroutineScope()
val uiState = rememberSaveable(saver = ServerUiState.Saver) {
ServerUiState.from(
initialConfig = initialConfig
@@ -39,7 +36,6 @@ class ServerHysteria2Activity : BaseServerActivity() {
override fun validateProtocolConfig(config: ProfileItem): Boolean {
if (config.password.isNullOrBlank()) {
toast(R.string.server_lab_id3)
return false
}
if (config.security.isNullOrBlank()) {
@@ -51,9 +47,10 @@ class ServerHysteria2Activity : BaseServerActivity() {
@Composable
private fun Hysteria2ProtocolFields(state: ServerUiState) {
FormTextField(
stringResource(R.string.server_lab_id3),
state.password,
{ state.password = it }
label = stringResource(R.string.server_lab_id3),
value = state.password,
onValueChange = { state.password = it },
isError = state.isPasswordError
)
FormTextField(
stringResource(R.string.server_obfs_password),
@@ -91,6 +88,11 @@ class ServerHysteria2Activity : BaseServerActivity() {
state.sni,
{ state.sni = it }
)
FormTextField(
stringResource(R.string.server_lab_ech_config_list),
state.echConfigList,
{ state.echConfigList = it }
)
FormTextField(
stringResource(R.string.server_lab_pinned_ca256),
state.pinnedCA256,
Loaded 100 of 172 files, more files were not shown because too many files have changed in this diff. Show more