Add quick toggle shortcut (#950)
Mainly for Android 7.1, also backward compatible.
This commit is contained in:
@@ -53,6 +53,8 @@
|
||||
<intent-filter>
|
||||
<action android:name="android.service.quicksettings.action.QS_TILE" />
|
||||
</intent-filter>
|
||||
<meta-data android:name="android.app.shortcuts"
|
||||
android:resource="@xml/shortcuts"/>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
@@ -116,6 +118,17 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<activity
|
||||
android:name=".QuickToggleShortcut"
|
||||
android:label="@string/quick_toggle"
|
||||
android:theme="@android:style/Theme.NoDisplay"
|
||||
android:excludeFromRecents="true"
|
||||
android:launchMode="singleTask">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.CREATE_SHORTCUT" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
|
||||
<service
|
||||
android:name=".ShadowsocksRunnerService"
|
||||
android:exported="false">
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<shortcut android:shortcutId="toggle"
|
||||
android:enabled="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:shortcutShortLabel="@string/quick_toggle"
|
||||
android:shortcutLongLabel="@string/quick_toggle"
|
||||
android:shortcutDisabledMessage="@string/quick_toggle">
|
||||
<intent android:action="android.intent.action.VIEW"
|
||||
android:targetPackage="com.github.shadowsocks"
|
||||
android:targetClass="com.github.shadowsocks.QuickToggleShortcut" />
|
||||
</shortcut>
|
||||
</shortcuts>
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.github.shadowsocks
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import com.github.shadowsocks.utils.{State, Utils}
|
||||
|
||||
/**
|
||||
* @author Mygod
|
||||
*/
|
||||
class QuickToggleShortcut extends Activity with ServiceBoundContext {
|
||||
override def onCreate(savedInstanceState: Bundle) {
|
||||
super.onCreate(savedInstanceState)
|
||||
getIntent.getAction match {
|
||||
case Intent.ACTION_CREATE_SHORTCUT =>
|
||||
setResult(Activity.RESULT_OK, new Intent()
|
||||
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, classOf[QuickToggleShortcut]))
|
||||
.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.quick_toggle))
|
||||
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
||||
Intent.ShortcutIconResource.fromContext(this, R.mipmap.ic_launcher)))
|
||||
finish()
|
||||
case _ => attachService()
|
||||
}
|
||||
}
|
||||
|
||||
override def onDestroy() {
|
||||
detachService()
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
override def onServiceConnected() {
|
||||
bgService.getState match {
|
||||
case State.STOPPED => Utils.startSsService(this)
|
||||
case State.CONNECTED => Utils.stopSsService(this)
|
||||
case _ => // ignore
|
||||
}
|
||||
finish()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user