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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ Feature of [Clash.Meta](https://github.com/MetaCubeX/Clash.Meta)
4. (Optional) Custom app package name. Add the following configuration to `local.properties`.

```properties
# config your ownn applicationId, or it will be 'com.github.metacubex.clash'
# config your own applicationId, or it will be 'com.github.metacubex.clash.meta.widget'
custom.application.id=com.my.compile.clash
# remove application id suffix, or the applicaion id will be 'com.github.metacubex.clash.alpha'
# remove the version name suffix ('.Alpha' / '.Meta')
remove.suffix=true

5. Create `signing.properties` in project root with
Expand All @@ -57,14 +57,14 @@ Feature of [Clash.Meta](https://github.com/MetaCubeX/Clash.Meta)

### Automation

APP package name is `com.github.metacubex.clash.meta`
APP package name is `com.github.metacubex.clash.meta.widget`

- Toggle Clash.Meta service status
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.TOGGLE_CLASH`
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.widget.action.TOGGLE_CLASH`
- Start Clash.Meta service
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.START_CLASH`
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.widget.action.START_CLASH`
- Stop Clash.Meta service
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.action.STOP_CLASH`
- Send intent to activity `com.github.kr328.clash.ExternalControlActivity` with action `com.github.metacubex.clash.meta.widget.action.STOP_CLASH`
- Import a profile
- URL Scheme `clash://install-config?url=<encoded URI>` or `clashmeta://install-config?url=<encoded URI>`

Expand Down
39 changes: 36 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,32 @@
<data android:host="install-config"/>
</intent-filter>
<intent-filter>
<action android:name="com.github.metacubex.clash.meta.action.START_CLASH" />
<action android:name="${applicationId}.action.START_CLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.github.metacubex.clash.meta.action.STOP_CLASH" />
<action android:name="${applicationId}.action.STOP_CLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.github.metacubex.clash.meta.action.TOGGLE_CLASH" />
<action android:name="${applicationId}.action.TOGGLE_CLASH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".widget.WidgetToggleActivity"
android:excludeFromRecents="true"
android:exported="false"
android:theme="@style/WidgetToggleTheme" />
<activity
android:name=".widget.WidgetConfigActivity"
android:exported="true"
android:label="@string/widget_config_title"
android:theme="@style/WidgetConfigTheme">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity
android:name=".ApkBrokenActivity"
android:configChanges="uiMode"
Expand Down Expand Up @@ -207,6 +221,25 @@
</intent-filter>
</service>

<receiver
android:name=".widget.ClashWidgetProvider"
android:exported="false"
android:label="@string/widget_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="${applicationId}.intent.action.CLASH_STARTED" />
<action android:name="${applicationId}.intent.action.CLASH_STOPPED" />
<action android:name="${applicationId}.intent.action.CLASH_RECREATED" />
<action android:name="${applicationId}.intent.action.PROFILE_LOADED" />
<action android:name="${applicationId}.intent.action.SELECTION_CHANGED" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/clash_widget_info" />
</receiver>

<receiver
android:name=".RestartReceiver"
android:enabled="false"
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/github/kr328/clash/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package com.github.kr328.clash

import android.app.Application
import android.content.Context
import android.content.res.Configuration
import com.github.kr328.clash.common.Global
import com.github.kr328.clash.common.compat.currentProcessName
import com.github.kr328.clash.common.log.Log
import com.github.kr328.clash.remote.Remote
import com.github.kr328.clash.service.util.sendServiceRecreated
import com.github.kr328.clash.util.clashDir
import com.github.kr328.clash.widget.WidgetUpdater
import java.io.File
import java.io.FileOutputStream

@Suppress("unused")
class MainApplication : Application() {
private var lastNightMode: Boolean? = null

override fun attachBaseContext(base: Context?) {
super.attachBaseContext(base)
Expand All @@ -24,6 +27,8 @@ class MainApplication : Application() {
super.onCreate()

val processName = currentProcessName
lastNightMode = resources.configuration.isNight()

extractGeoFiles()

Log.d("Process $processName started")
Expand All @@ -35,6 +40,22 @@ class MainApplication : Application() {
}
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

// The widget follows the system night mode independently of the app theme,
// so refresh it when the configuration changes while the app is alive.
val nightMode = newConfig.isNight()

if (nightMode != lastNightMode) {
lastNightMode = nightMode

if (currentProcessName == packageName) {
Thread { WidgetUpdater.updateAll(this) }.start()
}
}
}

private fun extractGeoFiles() {
clashDir.mkdirs()

Expand Down Expand Up @@ -84,3 +105,6 @@ class MainApplication : Application() {
Global.destroy()
}
}

private fun Configuration.isNight(): Boolean =
(uiMode and Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
3 changes: 3 additions & 0 deletions app/src/main/java/com/github/kr328/clash/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.kr328.clash

import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.design.SettingsDesign
import com.github.kr328.clash.widget.WidgetConfigActivity
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select

Expand All @@ -26,6 +27,8 @@ class SettingsActivity : BaseActivity<SettingsDesign>() {
startActivity(OverrideSettingsActivity::class.intent)
SettingsDesign.Request.StartMetaFeature ->
startActivity(MetaFeatureSettingsActivity::class.intent)
SettingsDesign.Request.StartWidget ->
startActivity(WidgetConfigActivity::class.intent)
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/com/github/kr328/clash/remote/StatusClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ class StatusClient(private val context: Context) {
null
}
}

fun currentProxy(): String? {
return try {
val result = context.contentResolver.call(
uri,
StatusProvider.METHOD_CURRENT_PROXY,
null,
null
)

result?.getString("name")
} catch (e: Exception) {
Log.w("Query current proxy: $e", e)

null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.github.kr328.clash.widget

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.Context
import android.content.Intent
import com.github.kr328.clash.common.constants.Intents
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class ClashWidgetProvider : AppWidgetProvider() {
override fun onUpdate(
context: Context,
appWidgetManager: AppWidgetManager,
appWidgetIds: IntArray,
) {
val pendingResult = goAsync()

CoroutineScope(Dispatchers.IO).launch {
try {
WidgetUpdater.update(context, appWidgetManager, appWidgetIds)
} finally {
pendingResult.finish()
}
}
}

override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intents.ACTION_CLASH_STARTED,
Intents.ACTION_CLASH_STOPPED,
Intents.ACTION_SERVICE_RECREATED,
Intents.ACTION_PROFILE_LOADED,
Intents.ACTION_SELECTION_CHANGED -> {
val pendingResult = goAsync()

CoroutineScope(Dispatchers.IO).launch {
try {
WidgetUpdater.updateAll(context)
} finally {
pendingResult.finish()
}
}
}
else -> super.onReceive(context, intent)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.github.kr328.clash.widget

import android.content.Context
import android.content.res.Configuration
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import com.github.kr328.clash.R

object WidgetAppearanceResolver {
fun isNight(context: Context, appearance: WidgetAppearance): Boolean {
return when (appearance) {
WidgetAppearance.FollowSystem ->
(context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) ==
Configuration.UI_MODE_NIGHT_YES
WidgetAppearance.Light -> false
WidgetAppearance.Dark -> true
}
}

@DrawableRes
fun background(
night: Boolean,
transparency: WidgetTransparency,
): Int {
return when (transparency) {
WidgetTransparency.Low ->
if (night) R.drawable.widget_bg_dark_low else R.drawable.widget_bg_light_low
WidgetTransparency.Medium ->
if (night) R.drawable.widget_bg_dark_medium else R.drawable.widget_bg_light_medium
WidgetTransparency.High ->
if (night) R.drawable.widget_bg_dark_high else R.drawable.widget_bg_light_high
}
}

@ColorRes
fun foreground(night: Boolean, running: Boolean): Int {
return when {
running && night -> R.color.widget_active_dark
running -> R.color.widget_active_light
night -> R.color.widget_inactive_dark
else -> R.color.widget_inactive_light
}
}
}
Loading