forked from xuhaoyang/ClashForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add intent filters for tasker automation (#119)
- Loading branch information
1 parent
ccfcb71
commit 0219392
Showing
12 changed files
with
143 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
app/src/main/java/com/github/kr328/clash/ExternalControlActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.github.kr328.clash | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import com.github.kr328.clash.common.constants.Intents | ||
import com.github.kr328.clash.common.util.intent | ||
import com.github.kr328.clash.common.util.setUUID | ||
import com.github.kr328.clash.design.MainDesign | ||
import com.github.kr328.clash.design.ui.ToastDuration | ||
import com.github.kr328.clash.remote.Remote | ||
import com.github.kr328.clash.remote.StatusClient | ||
import com.github.kr328.clash.service.model.Profile | ||
import com.github.kr328.clash.util.startClashService | ||
import com.github.kr328.clash.util.stopClashService | ||
import com.github.kr328.clash.util.withProfile | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.MainScope | ||
import kotlinx.coroutines.launch | ||
import java.util.* | ||
|
||
class ExternalControlActivity : Activity(), CoroutineScope by MainScope() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
when(intent.action) { | ||
Intent.ACTION_VIEW -> { | ||
val uri = intent.data ?: return finish() | ||
val url = uri.getQueryParameter("url") ?: return finish() | ||
|
||
launch { | ||
val uuid = withProfile { | ||
val type = when (uri.getQueryParameter("type")?.lowercase(Locale.getDefault())) { | ||
"url" -> Profile.Type.Url | ||
"file" -> Profile.Type.File | ||
else -> Profile.Type.Url | ||
} | ||
val name = uri.getQueryParameter("name") ?: getString(R.string.new_profile) | ||
|
||
create(type, name).also { | ||
patch(it, name, url, 0) | ||
} | ||
} | ||
startActivity(PropertiesActivity::class.intent.setUUID(uuid)) | ||
finish() | ||
} | ||
} | ||
|
||
Intents.ACTION_TOGGLE_CLASH -> if(Remote.broadcasts.clashRunning) { | ||
stopClash() | ||
} | ||
else { | ||
startClash() | ||
} | ||
|
||
Intents.ACTION_START_CLASH -> if(!Remote.broadcasts.clashRunning) { | ||
startClash() | ||
} | ||
else { | ||
Toast.makeText(this, R.string.external_control_started, Toast.LENGTH_LONG).show() | ||
} | ||
|
||
Intents.ACTION_STOP_CLASH -> if(Remote.broadcasts.clashRunning) { | ||
stopClash() | ||
} | ||
else { | ||
Toast.makeText(this, R.string.external_control_stopped, Toast.LENGTH_LONG).show() | ||
} | ||
} | ||
return finish() | ||
} | ||
|
||
private fun startClash() { | ||
// if (currentProfile == null) { | ||
// Toast.makeText(this, R.string.no_profile_selected, Toast.LENGTH_LONG).show() | ||
// return | ||
// } | ||
val vpnRequest = startClashService() | ||
if (vpnRequest != null) { | ||
Toast.makeText(this, R.string.unable_to_start_vpn, Toast.LENGTH_LONG).show() | ||
return | ||
} | ||
Toast.makeText(this, R.string.external_control_started, Toast.LENGTH_LONG).show() | ||
} | ||
|
||
private fun stopClash() { | ||
stopClashService() | ||
Toast.makeText(this, R.string.external_control_stopped, Toast.LENGTH_LONG).show() | ||
} | ||
} |
44 changes: 0 additions & 44 deletions
44
app/src/main/java/com/github/kr328/clash/ExternalImportActivity.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters