Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Feature/add auto start
Browse files Browse the repository at this point in the history
Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
  • Loading branch information
2 people authored and AnGgIt86 committed Sep 30, 2024
1 parent eff0ca8 commit 3b2e1cc
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 14 deletions.
11 changes: 10 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- <useapplications-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
Expand Down Expand Up @@ -305,6 +305,15 @@
</intent-filter>
</receiver>

<receiver
android:exported="true"
android:name=".receiver.BootReceiver"
android:label="BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>

<service
android:exported="true"
android:name=".service.QSTileService"
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ object AppConfig {
const val PREF_ALLOW_INSECURE = "pref_allow_insecure"
const val PREF_SOCKS_PORT = "pref_socks_port"
const val PREF_HTTP_PORT = "pref_http_port"

const val PREF_REMOTE_DNS = "pref_remote_dns"
const val PREF_DOMESTIC_DNS = "pref_domestic_dns"
const val PREF_DELAY_TEST_URL = "pref_delay_test_url"
const val PREF_LOGLEVEL = "pref_core_loglevel"
const val PREF_MODE = "pref_mode"
const val PREF_IS_BOOTED = "pref_is_booted"

/** Cache keys. */
const val CACHE_SUBSCRIPTION_ID = "cache_subscription_id"
Expand Down Expand Up @@ -109,6 +109,7 @@ object AppConfig {
const val DNS_DIRECT = "223.5.5.5"
const val DNS_VPN = "1.1.1.1"


/** Ports and addresses for various services. */
const val PORT_LOCAL_DNS = "10853"
const val PORT_SOCKS = "10808"
Expand Down Expand Up @@ -152,4 +153,8 @@ object AppConfig {
const val WIREGUARD = "wireguard://"
const val TUIC = "tuic://"
const val HYSTERIA2 = "hysteria2://"

/** Give a good name to this, IDK*/
const val VPN = "VPN"

}
18 changes: 18 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/receiver/BootReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.neko.v2ray.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.neko.v2ray.service.V2RayServiceManager
import com.neko.v2ray.util.MmkvManager

class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (Intent.ACTION_BOOT_COMPLETED == intent?.action && MmkvManager.decodeStartOnBoot()) {
if (MmkvManager.getSelectServer().isNullOrEmpty()) {
return
}
V2RayServiceManager.startV2Ray(context!!)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.core.app.NotificationCompat
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.ANG_PACKAGE
import com.neko.v2ray.AppConfig.TAG_DIRECT
import com.neko.v2ray.AppConfig.VPN
import com.neko.v2ray.R
import com.neko.v2ray.dto.ServerConfig
import com.neko.v2ray.extension.toSpeedString
Expand Down Expand Up @@ -72,7 +73,7 @@ object V2RayServiceManager {
} else {
context.toast(R.string.toast_services_start)
}
val intent = if ((settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: "VPN") == "VPN") {
val intent = if ((settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: VPN) == VPN) {
Intent(context.applicationContext, V2RayVpnService::class.java)
} else {
Intent(context.applicationContext, V2RayProxyOnlyService::class.java)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.google.android.material.navigation.NavigationView
import com.google.android.material.tabs.TabLayout
import com.tbruyelle.rxpermissions3.RxPermissions
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.VPN
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivityMainBinding
import com.neko.v2ray.dto.EConfigType
Expand Down Expand Up @@ -140,7 +141,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
binding.fab.setOnClickListener {
if (mainViewModel.isRunning.value == true) {
Utils.stopVService(this)
} else if ((settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: "VPN") == "VPN") {
} else if ((settingsStorage?.decodeString(AppConfig.PREF_MODE) ?: VPN) == VPN) {
val intent = VpnService.prepare(this)
if (intent == null) {
startV2Ray()
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/kotlin/com/neko/v2ray/ui/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.appbar.CollapsingToolbarLayout
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.work.ExistingPeriodicWorkPolicy
import androidx.work.PeriodicWorkRequest
import androidx.work.multiprocess.RemoteWorkManager
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.neko.v2ray.AngApplication
import com.neko.v2ray.AppConfig
import com.neko.v2ray.AppConfig.VPN
import com.neko.v2ray.R
import com.neko.v2ray.extension.toLongEx
import com.neko.v2ray.service.SubscriptionUpdater
Expand Down Expand Up @@ -194,7 +195,7 @@ class SettingsActivity : BaseActivity() {

override fun onStart() {
super.onStart()
updateMode(settingsStorage.decodeString(AppConfig.PREF_MODE, "VPN"))
updateMode(settingsStorage.decodeString(AppConfig.PREF_MODE, VPN))
localDns?.isChecked = settingsStorage.getBoolean(AppConfig.PREF_LOCAL_DNS_ENABLED, false)
fakeDns?.isChecked = settingsStorage.getBoolean(AppConfig.PREF_FAKE_DNS_ENABLED, false)
localDnsPort?.summary = settingsStorage.decodeString(AppConfig.PREF_LOCAL_DNS_PORT, AppConfig.PORT_LOCAL_DNS)
Expand Down Expand Up @@ -252,6 +253,7 @@ class SettingsActivity : BaseActivity() {

listOf(
AppConfig.PREF_ROUTE_ONLY_ENABLED,
AppConfig.PREF_IS_BOOTED,
AppConfig.PREF_BYPASS_APPS,
AppConfig.PREF_SPEED_ENABLED,
AppConfig.PREF_CONFIRM_REMOVE,
Expand Down Expand Up @@ -280,7 +282,7 @@ class SettingsActivity : BaseActivity() {
}

private fun updateMode(mode: String?) {
val vpn = mode == "VPN"
val vpn = mode == VPN
perAppProxy?.isEnabled = vpn
perAppProxy?.isChecked = settingsStorage.getBoolean(AppConfig.PREF_PER_APP_PROXY, false)
localDns?.isEnabled = vpn
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/kotlin/com/neko/v2ray/util/MmkvManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.neko.v2ray.util

import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import com.neko.v2ray.AppConfig.PREF_IS_BOOTED
import com.neko.v2ray.AppConfig.PREF_ROUTING_RULESET
import com.neko.v2ray.dto.AssetUrlItem
import com.neko.v2ray.dto.ProfileItem
Expand Down Expand Up @@ -307,4 +308,14 @@ object MmkvManager {
}

//endregion

//region Others
fun encodeStartOnBoot(startOnBoot: Boolean) {
settingsStorage.encode(PREF_IS_BOOTED, startOnBoot)
}
fun decodeStartOnBoot(): Boolean {
return settingsStorage.decodeBool(PREF_IS_BOOTED, false)
}
//endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application
}

AppConfig.PREF_ROUTE_ONLY_ENABLED,
AppConfig.PREF_IS_BOOTED,
AppConfig.PREF_SPEED_ENABLED,
AppConfig.PREF_PROXY_SHARING,
AppConfig.PREF_LOCAL_DNS_ENABLED,
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/uwu_ic_autobot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:fillColor="#ff000000"
android:pathData="M12.5,2C8.93,2 8.14,5.96 10.13,9.65C9.72,9.97 9.4,10.39 9.21,10.87C8.28,10.68 7.23,10.25 6.73,9.26C5.56,6.89 2,7 2,11.5C2,15.07 5.95,15.85 9.64,13.87C9.96,14.27 10.39,14.59 10.88,14.79C10.68,15.71 10.24,16.75 9.26,17.24C6.9,18.42 7,22 11.5,22C12.31,22 13,21.78 13.5,21.41C13.19,20.67 13,19.86 13,19C13,17.59 13.5,16.3 14.3,15.28C14.17,14.97 14.03,14.65 13.86,14.34C14.26,14 14.57,13.59 14.77,13.11C15.26,13.21 15.78,13.39 16.25,13.67C17.07,13.25 18,13 19,13C20.05,13 21.03,13.27 21.89,13.74C21.95,13.37 22,12.96 22,12.5C22,8.92 18.03,8.13 14.33,10.13C14,9.73 13.59,9.42 13.11,9.22C13.3,8.29 13.74,7.24 14.73,6.75C17.09,5.57 17,2 12.5,2M12,11C12.54,11 13,11.45 13,12C13,12.55 12.54,13 12,13C11.43,13 11,12.55 11,12C11,11.45 11.43,11 12,11M18,15C16.89,15 16,15.9 16,17V23H18V21H20V23H22V17C22,15.9 21.1,15 20,15M18,17H20V19H18Z" />
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<string name="title_vpn_settings">إعدادات VPN</string>
<string name="title_pref_per_app_proxy">الوكيل لكل تطبيق</string>
<string name="summary_pref_per_app_proxy">عام: التطبيق المحدد هو وكيل، ولم يتم تحديد اتصال مباشر؛ \nوضع التجاوز: التطبيق المحدد متصل مباشرة، الوكيل غير محدد. \nخيار تحديد تطبيق الوكيل تلقائيًا في القائمة</string>
<string name="title_pref_is_booted">بدء التشغيل التلقائي</string>
<string name="summary_pref_is_booted">بدء تشغيل VPN تلقائيًا إذا تم تحديد خادم بالفعل</string>

<string name="title_mux_settings">إعدادات مكس</string>
<string name="title_pref_mux_enabled">تمكين مكس</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@
<string name="title_vpn_settings">تنظیمات VPN</string>
<string name="title_pref_per_app_proxy">پروکسی هر برنامه</string>
<string name="summary_pref_per_app_proxy">عمومی: برنامه بررسی شده پروکسی است، اتصال مستقیم بدون علامت است. \nحالت دور زدن: برنامه علامت‌گذاری شده مستقیماً متصل است، پراکسی علامت‌گذاری نشده است. \nگزینه انتخاب خودکار برنامه پراکسی در منو</string>
<string name="title_pref_is_booted">شروع خودکار</string>
<string name="summary_pref_is_booted">اگر سروری قبلاً انتخاب شده باشد، VPN را به طور خودکار راه اندازی کنید</string>

<string name="title_mux_settings">تنظیمات Mux</string>
<string name="title_pref_mux_enabled">Mux را فعال کنید</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<string name="title_vpn_settings">Pengaturan VPN</string>
<string name="title_pref_per_app_proxy">Proxy per aplikasi</string>
<string name="summary_pref_per_app_proxy">Umum: Aplikasi yang dicentang adalah proxy, koneksi langsung tidak dicentang; \nmode bypass: aplikasi yang dicentang terhubung langsung, proxy yang tidak dicentang. \nPilihan untuk memilih aplikasi proxy secara otomatis di menu</string>
<string name="title_pref_is_booted">Mulai otomatis</string>
<string name="summary_pref_is_booted">Mulai VPN secara otomatis jika server telah dipilih</string>

<string name="title_mux_settings">Pengaturan Mux</string>
<string name="title_pref_mux_enabled">Aktifkan Mux</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<string name="title_vpn_settings">VPN設定</string>
<string name="title_pref_per_app_proxy">アプリごとのプロキシ</string>
<string name="summary_pref_per_app_proxy">一般: チェックされているアプリはプロキシ、チェックされていない場合は直接接続です。 \nバイパス モード: チェックされたアプリが直接接続され、チェックされていないプロキシ。 \nメニューでプロキシ アプリケーションを自動的に選択するオプション</string>
<string name="title_pref_is_booted">自動スタート</string>
<string name="summary_pref_is_booted">サーバーがすでに選択されている場合はVPNを自動的に開始します</string>

<string name="title_mux_settings">マルチプレクサ設定</string>
<string name="title_pref_mux_enabled">マルチプレクサを有効にする</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-jw/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
<string name="title_vpn_settings">Setelan VPN</string>
<string name="title_pref_per_app_proxy">Proxy saben app</string>
<string name="summary_pref_per_app_proxy">Umum: Aplikasi sing dicenthang minangka proxy, sambungan langsung ora dicenthang; \nmode bypass: app sing dicenthang nyambung langsung, proxy sing ora dicenthang. \nPilihan kanggo milih aplikasi proxy kanthi otomatis ing menu</string>
<string name="title_pref_is_booted">Miwiti otomatis</string>
<string name="summary_pref_is_booted">Miwiti VPN kanthi otomatis yen server wis dipilih</string>

<string name="title_mux_settings">Setelan Mux</string>
<string name="title_pref_mux_enabled">Aktifake Mux</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
<string name="title_vpn_settings">Настройки VPN</string>
<string name="title_pref_per_app_proxy">Прокси для каждого приложения</string>
<string name="summary_pref_per_app_proxy">Общее: проверенное приложение является прокси-сервером, не отмечено прямое соединение; \nрежим обхода: проверенное приложение подключено напрямую, прокси-сервер не проверен. \nВозможность автоматического выбора прокси-приложения в меню</string>

<string name="title_pref_is_booted">Автоматический запуск</string>
<string name="summary_pref_is_booted">Автоматически запускать VPN, если сервер уже выбран</string>

<string name="title_mux_settings">Настройки мультиплексора</string>
<string name="title_pref_mux_enabled">Включить мультиплексор</string>
<string name="summary_pref_mux_enabled">Быстрее, но это может привести к нестабильному соединению.\nНастройте способы обработки TCP, UDP и QUIC ниже.</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-su/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
<string name="title_vpn_settings">Setélan VPN</string>
<string name="title_pref_per_app_proxy">Proksi per-aplikasi</string>
<string name="summary_pref_per_app_proxy">Umum: Aplikasi anu dipariksa nyaéta proxy, sambungan langsung henteu dicentang; \nmodeu bypass: aplikasi nu dipariksa langsung nyambung, proxy teu dicentang. \nPilihan pikeun otomatis milih aplikasi proxy dina ménu</string>

<string name="title_pref_is_booted">Otomatis ngamimitian</string>
<string name="summary_pref_is_booted">Otomatis ngamimitian VPN upami server parantos dipilih</string>

<string name="title_mux_settings">Setélan Mux</string>
<string name="title_pref_mux_enabled">Aktipkeun Mux</string>
<string name="summary_pref_mux_enabled">Langkung gancang, tapi tiasa nyababkeun konektipitas teu stabil\nsaluyukeun kumaha TCP, UDP sareng QUIC diatur di handap.</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
<string name="title_vpn_settings">Cài đặt VPN</string>
<string name="title_pref_per_app_proxy">Proxy trên mỗi ứng dụng</string>
<string name="summary_pref_per_app_proxy">Chung: Ứng dụng đã kiểm tra là proxy, kết nối trực tiếp không được kiểm tra; \nchế độ bỏ qua: ứng dụng đã chọn được kết nối trực tiếp, proxy không được chọn. \nTùy chọn tự động chọn ứng dụng proxy trong menu</string>

<string name="title_pref_is_booted">Tự động khởi động</string>
<string name="summary_pref_is_booted">Tự động khởi động VPN nếu máy chủ đã được chọn</string>

<string name="title_mux_settings">Cài đặt Mux</string>
<string name="title_pref_mux_enabled">Kích hoạt Mux</string>
<string name="summary_pref_mux_enabled">Nhanh hơn nhưng có thể khiến kết nối không ổn định\ntùy chỉnh cách xử lý TCP, UDP và QUIC bên dưới</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
<string name="title_vpn_settings">VPN 设置</string>
<string name="title_pref_per_app_proxy">每个应用程序代理</string>
<string name="summary_pref_per_app_proxy">General:勾选App为代理,不勾选直连; \n绕过模式:选中应用程序直连,未选中代理。 \n菜单中自动选择代理应用程序的选项</string>

<string name="title_pref_is_booted">自动启动</string>
<string name="summary_pref_is_booted">如果已选择服务器则自动启动 VPN</string>

<string name="title_mux_settings">复用器设置</string>
<string name="title_pref_mux_enabled">启用复用器</string>
<string name="summary_pref_mux_enabled">更快,但可能会导致连接不稳定\n自定义如何处理下面的 TCP、UDP 和 QUIC</string>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@
<string name="title_vpn_settings">VPN 設定</string>
<string name="title_pref_per_app_proxy">每個應用程式代理</string>
<string name="summary_pref_per_app_proxy">General:勾選App為代理,不勾選直連; \n繞過模式:選取應用程式直連,未選取代理程式。 \n選單中自動選擇代理應用程式的選項</string>

<string name="title_pref_is_booted">自動啟動</string>
<string name="summary_pref_is_booted">如果已選擇伺服器,則自動啟動 VPN</string>

<string name="title_mux_settings">多工器設置</string>
<string name="title_pref_mux_enabled">啟用復用器</string>
<string name="summary_pref_mux_enabled">更快,但可能會導致連線不穩定\n自訂如何處理下面的 TCP、UDP 和 QUIC</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<string name="title_advanced">Advanced Settings</string>
<string name="title_vpn_settings">VPN Settings</string>
<string name="title_pref_per_app_proxy">Per-app proxy</string>
<string name="title_pref_is_booted">Auto start</string>
<string name="summary_pref_is_booted">Automatically start VPN if a server has already been selected</string>

<string name="summary_pref_per_app_proxy">General: Checked App is proxy, unchecked direct connection; \nbypass mode: checked app directly connected, unchecked proxy. \nThe option to automatically select the proxy application in the menu</string>

<string name="title_mux_settings">Mux Settings</string>
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/res/xml/pref_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@
android:defaultValue="false"
android:key="pref_route_only_enabled"
android:icon="@drawable/uwu_ic_route"
android:layout="@layout/uwu_bot_switch"
android:layout="@layout/uwu_mid_switch"
android:summary="@string/summary_pref_route_only_enabled"
android:title="@string/title_pref_route_only_enabled" />

<SwitchPreference
android:defaultValue="false"
android:key="pref_is_booted"
android:icon="@drawable/uwu_ic_autobot"
android:layout="@layout/uwu_bot_switch"
android:summary="@string/summary_pref_is_booted"
android:title="@string/title_pref_is_booted" />

</PreferenceCategory>

<PreferenceCategory android:title="@string/title_vpn_settings">
Expand Down

0 comments on commit 3b2e1cc

Please sign in to comment.