Skip to content

Commit

Permalink
feat: 增加规则的导入导出功能(简陋)
Browse files Browse the repository at this point in the history
  • Loading branch information
WankkoRee committed Oct 7, 2022
1 parent 8680a4d commit c399167
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cn.wankkoree.xposed.enablewebviewdebugging.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
Expand All @@ -9,6 +11,7 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.transition.Slide
import android.util.Base64
import android.util.Log
import android.view.View
import android.widget.AdapterView
Expand Down Expand Up @@ -81,6 +84,62 @@ class App : AppCompatActivity() {
viewBinding.appToolbarBack.setOnClickListener {
finishAfterTransition()
}
viewBinding.appToolbarShare.setOnClickListener { v ->
PopupMenu(this, v).apply {
menuInflater.inflate(R.menu.app_toolbar_share, menu)
setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.app_toolbar_share_export_to_clip -> {
with(modulePrefs("apps_$pkg")) {
val hooks = getSet(AppSP.hooks).toList()
val clipHeader = Base64.encodeToString(hooks.joinToString("|").encodeToByteArray(), Base64.NO_WRAP or Base64.URL_SAFE or Base64.NO_PADDING)
val clipBody = hooks.joinToString("|") { ruleName ->
Base64.encodeToString(getString("hook_entry_$ruleName", "{}").encodeToByteArray(), Base64.NO_WRAP or Base64.URL_SAFE or Base64.NO_PADDING)
}
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip: ClipData = ClipData.newPlainText("export", "$clipHeader|$clipBody")
clipboard.setPrimaryClip(clip)
}
}
R.id.app_toolbar_share_import_from_clip -> {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val item = clipboard.primaryClip!!.getItemAt(0).text.toString()
val clip = item.split('|')
if (clip.size >= 2) {
val clipHeader = Base64.decode(clip[0], Base64.NO_WRAP or Base64.URL_SAFE or Base64.NO_PADDING).decodeToString().split('|')
val clipBody = clip.drop(1).map { encodingRule ->
Base64.decode(encodingRule, Base64.NO_WRAP or Base64.URL_SAFE or Base64.NO_PADDING).decodeToString()
}
if (clipHeader.size == clipBody.size) {
with(modulePrefs("apps_$pkg")) {
for (i in clipHeader.indices) {
try {
put(AppSP.hooks, clipHeader[i])
putString("hook_entry_${clipHeader[i]}", clipBody[i])
} catch (_: ValueAlreadyExistedInSet) {
toast?.cancel()
toast = Toast.makeText(this@App, getString(R.string.s_already_exists, getString(R.string.rule_name) + """ "${clipHeader[i]}" """), Toast.LENGTH_SHORT)
toast!!.show()
}
}
}
refresh()
} else {
toast?.cancel()
toast = Toast.makeText(this@App, getString(R.string.parse_failed), Toast.LENGTH_SHORT)
toast!!.show()
}
} else {
toast?.cancel()
toast = Toast.makeText(this@App, getString(R.string.parse_failed), Toast.LENGTH_SHORT)
toast!!.show()
}
}
}
true
}
}.show()
}
viewBinding.appToolbarPreset.setOnClickListener { v ->
PopupMenu(this, v).apply {
menuInflater.inflate(R.menu.app_toolbar_preset, menu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ val grayColorFilter = ColorMatrixColorFilter(ColorMatrix().apply {
setSaturation(0f)
})

fun colorStateSingle(color: Int): ColorStateList = ColorStateList(arrayOf(intArrayOf()), intArrayOf(color))

fun getPrimaryColor(d: Drawable, context: Context): Triple<Int, Int, Int> {
// https://stackoverflow.com/a/55852660/15603001
d.state = intArrayOf(android.R.attr.state_enabled)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_round_share_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92s2.92,-1.31 2.92,-2.92 -1.31,-2.92 -2.92,-2.92z"/>
</vector>
17 changes: 16 additions & 1 deletion app/src/main/res/layout/activity_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,29 @@
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintStart_toEndOf="@id/app_toolbar_back"
app:layout_constraintEnd_toStartOf="@id/app_toolbar_preset"
app:layout_constraintEnd_toStartOf="@id/app_toolbar_share"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

style="@style/ThemeComponentTextViewPrimary"
android:textAppearance="?textAppearanceTitleLarge"
android:text=""
/>
<com.google.android.material.textview.MaterialTextView android:id="@+id/app_toolbar_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
app:layout_constraintEnd_toStartOf="@id/app_toolbar_preset"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"

app:drawableStartCompat="@drawable/ic_round_share_24"
app:drawableTint="@color/textPrimary"
android:tooltipText="@string/share_rules"
android:contentDescription="@string/share_rules"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:clickable="true"
/>
<com.google.android.material.textview.MaterialTextView android:id="@+id/app_toolbar_preset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/menu/app_toolbar_share.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!--TODO: 添加更多 hook 方法-->
<item
android:id="@+id/app_toolbar_share_export_to_clip"
android:title="@string/export_to_clip"
app:showAsAction="never"/>
<item
android:id="@+id/app_toolbar_share_import_from_clip"
android:title="@string/import_from_clip"
app:showAsAction="never"/>
</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values-zh-rCN/words.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,7 @@
<string name="ok">好的</string>
<string name="debug_mode">调试模式</string>
<string name="checking_for_rules_updates">检查规则更新</string>
<string name="share_rules">规则分享</string>
<string name="export_to_clip">导出到剪贴板</string>
<string name="import_from_clip">导入自剪贴板</string>
</resources>
3 changes: 3 additions & 0 deletions app/src/main/res/values/words.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@
<string name="ok">OK</string>
<string name="debug_mode">Debug Mode</string>
<string name="checking_for_rules_updates">Checking For Rules Updates</string>
<string name="share_rules">Share Rules</string>
<string name="export_to_clip">Export To Clip</string>
<string name="import_from_clip">Import From Clip</string>
</resources>

0 comments on commit c399167

Please sign in to comment.