Add android connection shortcut

This commit is contained in:
problematicconsumer
2023-09-09 20:51:02 +03:30
parent 08e0e7f72a
commit 4aacde2a43
6 changed files with 115 additions and 4 deletions

View File

@@ -19,6 +19,11 @@
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_rounded" android:roundIcon="@mipmap/ic_launcher_rounded"
tools:targetApi="31"> tools:targetApi="31">
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true"
@@ -27,6 +32,11 @@
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true" android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"> android:windowSoftInputMode="adjustResize">
<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
<!-- Specifies an Android theme to apply to this Activity as soon as <!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues while the Flutter UI initializes. After that, this theme continues
@@ -57,6 +67,19 @@
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" /> <action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".ShortcutActivity"
android:excludeFromRecents="true"
android:exported="true"
android:label="@string/quick_toggle"
android:launchMode="singleTask"
android:taskAffinity="">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>
<service <service
android:name=".bg.TileService" android:name=".bg.TileService"
android:directBootAware="true" android:directBootAware="true"
@@ -79,6 +102,10 @@
<action android:name="android.net.VpnService" /> <action android:name="android.net.VpnService" />
</intent-filter> </intent-filter>
</service> </service>
<service
android:name=".bg.ProxyService"
android:exported="false">
</service>
<!-- Don't delete the meta-data below. <!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data <meta-data

View File

@@ -9,6 +9,7 @@ import androidx.lifecycle.lifecycleScope
import com.hiddify.hiddify.bg.ServiceConnection import com.hiddify.hiddify.bg.ServiceConnection
import com.hiddify.hiddify.bg.ServiceNotification import com.hiddify.hiddify.bg.ServiceNotification
import com.hiddify.hiddify.constant.Alert import com.hiddify.hiddify.constant.Alert
import com.hiddify.hiddify.constant.ServiceMode
import com.hiddify.hiddify.constant.Status import com.hiddify.hiddify.constant.Status
import io.flutter.embedding.android.FlutterFragmentActivity import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine import io.flutter.embedding.engine.FlutterEngine
@@ -58,9 +59,11 @@ class MainActivity : FlutterFragmentActivity(), ServiceConnection.Callback {
if (Settings.rebuildServiceMode()) { if (Settings.rebuildServiceMode()) {
reconnect() reconnect()
} }
if (prepare()) { if (Settings.serviceMode == ServiceMode.VPN) {
Log.d(TAG, "VPN permission required") if (prepare()) {
return@launch Log.d(TAG, "VPN permission required")
return@launch
}
} }
val intent = Intent(Application.application, Settings.serviceClass()) val intent = Intent(Application.application, Settings.serviceClass())

View File

@@ -0,0 +1,67 @@
package com.hiddify.hiddify
import android.app.Activity
import android.content.Intent
import android.content.pm.ShortcutManager
import android.os.Build
import android.os.Bundle
import androidx.core.content.getSystemService
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import com.hiddify.hiddify.bg.BoxService
import com.hiddify.hiddify.bg.ServiceConnection
import com.hiddify.hiddify.constant.Status
class ShortcutActivity : Activity(), ServiceConnection.Callback {
private val connection = ServiceConnection(this, this, false)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.action == Intent.ACTION_CREATE_SHORTCUT) {
setResult(
RESULT_OK, ShortcutManagerCompat.createShortcutResultIntent(
this,
ShortcutInfoCompat.Builder(this, "toggle")
.setIntent(
Intent(
this,
ShortcutActivity::class.java
).setAction(Intent.ACTION_MAIN)
)
.setIcon(
IconCompat.createWithResource(
this,
R.mipmap.ic_launcher
)
)
.setShortLabel(getString(R.string.quick_toggle))
.build()
)
)
finish()
} else {
connection.connect()
if (Build.VERSION.SDK_INT >= 25) {
getSystemService<ShortcutManager>()?.reportShortcutUsed("toggle")
}
}
moveTaskToBack(true)
}
override fun onServiceStatusChanged(status: Status) {
when (status) {
Status.Started -> BoxService.stop()
Status.Stopped -> BoxService.start()
else -> {}
}
finish()
}
override fun onDestroy() {
connection.disconnect()
super.onDestroy()
}
}

View File

@@ -73,7 +73,7 @@ class BoxService(
fun start() { fun start() {
val intent = runBlocking { val intent = runBlocking {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
Intent(Application.application, VPNService::class.java) Intent(Application.application, Settings.serviceClass())
} }
} }
ContextCompat.startForegroundService(Application.application, intent) ContextCompat.startForegroundService(Application.application, intent)

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string name="stop">Stop</string> <string name="stop">Stop</string>
<string name="quick_toggle">Toggle</string>
</resources> </resources>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:icon="@mipmap/ic_launcher"
android:shortcutId="toggle"
android:shortcutLongLabel="@string/quick_toggle"
android:shortcutShortLabel="@string/quick_toggle">
<intent
android:action="android.intent.action.MAIN"
android:targetClass="com.hiddify.hiddify.ShortcutActivity"
android:targetPackage="app.hiddify.com" />
</shortcut>
</shortcuts>