forked from zhufucdev/MotionEmulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
715 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
50 changes: 50 additions & 0 deletions
50
cp_plugin/src/androidTest/java/com/zhufucdev/cp_plugin/ContentProviderInstrumentedTest.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,50 @@ | ||
package com.zhufucdev.cp_plugin | ||
|
||
import androidx.test.platform.app.InstrumentationRegistry | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.zhufucdev.cp_plugin.provider.bridge | ||
import com.zhufucdev.stub.EmulationInfo | ||
import com.zhufucdev.stub_plugin.MePlugin | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
|
||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
import org.junit.Assert.* | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* See [testing documentation](http://d.android.com/tools/testing). | ||
*/ | ||
@RunWith(AndroidJUnit4::class) | ||
class ContentProviderInstrumentedTest { | ||
@Test | ||
fun useSelfhosted() { | ||
val context = InstrumentationRegistry.getInstrumentation().context | ||
val scope = CoroutineScope(Dispatchers.Default) | ||
|
||
scope.launch { | ||
val server = MePlugin.queryServer(context) | ||
server.bridge() | ||
} | ||
|
||
useConnect() | ||
} | ||
|
||
@Test | ||
fun useConnect() { | ||
val cr = InstrumentationRegistry.getInstrumentation().context.contentResolver | ||
runBlocking { | ||
delay(1.seconds) | ||
ContentProviderServer(PROVIDER_AUTHORITY, cr).connect("whatever") { | ||
sendStarted(EmulationInfo(10.0, 10.0, BuildConfig.APPLICATION_ID)) | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.zhufucdev.cp_plugin | ||
|
||
const val SHARED_PREFS_NAME = "cp_plugin_prefs" | ||
|
||
const val PROVIDER_AUTHORITY = "com.zhufucdev.motion_emulator.event_provider" |
140 changes: 140 additions & 0 deletions
140
cp_plugin/src/main/java/com/zhufucdev/cp_plugin/ContentProviderServer.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,140 @@ | ||
package com.zhufucdev.cp_plugin | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.ContentResolver | ||
import android.content.ContentValues | ||
import android.database.Cursor | ||
import android.net.Uri | ||
import androidx.core.content.contentValuesOf | ||
import com.zhufucdev.stub.Box | ||
import com.zhufucdev.stub.Emulation | ||
import com.zhufucdev.stub.EmulationInfo | ||
import com.zhufucdev.stub.Intermediate | ||
import com.zhufucdev.stub_plugin.ServerConnection | ||
import com.zhufucdev.stub_plugin.ServerScope | ||
import kotlinx.serialization.json.Json | ||
import java.util.Optional | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.coroutineContext | ||
|
||
const val EMULATION_START = 0x00 | ||
const val EMULATION_STOP = 0x01 | ||
|
||
data class ContentProviderServer(val authority: String, val resolver: ContentResolver) | ||
|
||
@SuppressLint("Recycle") | ||
suspend fun ContentProviderServer.connect( | ||
id: String, | ||
block: suspend ServerScope.() -> Unit | ||
): ServerConnection { | ||
val emulation = | ||
runCatching { | ||
resolver | ||
.query(Uri.parse("content://$authority/next"), null, id, null, null) | ||
.use(::parseEmulation) | ||
}.getOrNull() | ||
val context = coroutineContext | ||
if (emulation == null) { | ||
val scope = object : ServerScope { | ||
override val emulation: Optional<Emulation> = Optional.empty() | ||
|
||
override val coroutineContext: CoroutineContext = context | ||
|
||
override suspend fun sendStarted(info: EmulationInfo) { | ||
throw NotImplementedError() | ||
} | ||
|
||
override suspend fun sendProgress(intermediate: Intermediate) { | ||
throw NotImplementedError() | ||
} | ||
|
||
override suspend fun close() { | ||
throw NotImplementedError() | ||
} | ||
} | ||
block(scope) | ||
return object : ServerConnection { | ||
override val successful: Boolean = false | ||
|
||
override fun close() { | ||
// nothing | ||
} | ||
} | ||
} else { | ||
val scope = object : ServerScope { | ||
override val emulation: Optional<Emulation> = emulation | ||
|
||
override val coroutineContext: CoroutineContext = context | ||
|
||
override suspend fun sendStarted(info: EmulationInfo) { | ||
val uri = Uri.parse("content://$authority/state") | ||
resolver.update(uri, info.contentValues(), id, null) | ||
} | ||
|
||
override suspend fun sendProgress(intermediate: Intermediate) { | ||
val uri = Uri.parse("content://$authority/progress") | ||
resolver.update(uri, intermediate.contentValues(), id, null) | ||
} | ||
|
||
override suspend fun close() { | ||
// does nothing | ||
} | ||
} | ||
block(scope) | ||
resolver.sendStop(authority, id) | ||
return object : ServerConnection { | ||
override val successful: Boolean = false | ||
|
||
override fun close() { | ||
// nothing | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun parseEmulation(cursor: Cursor?): Optional<Emulation>? { | ||
if (cursor == null) { | ||
return null | ||
} | ||
cursor.moveToFirst() | ||
if (cursor.getInt(0) == EMULATION_STOP) { | ||
return Optional.empty() | ||
} | ||
val traceData = cursor.getString(1) | ||
val motionData = cursor.getString(2) | ||
val cellsData = cursor.getString(3) | ||
val velocity = cursor.getDouble(4) | ||
val repeat = cursor.getInt(5) | ||
val satellites = cursor.getInt(6) | ||
return Optional.of( | ||
Emulation( | ||
Json.decodeFromString(traceData), | ||
Box.decodeFromString(motionData), | ||
Box.decodeFromString(cellsData), | ||
velocity, repeat, satellites | ||
) | ||
) | ||
} | ||
|
||
private fun EmulationInfo.contentValues(): ContentValues { | ||
return contentValuesOf( | ||
"running" to true, | ||
"duration" to duration, | ||
"length" to length, | ||
"owner" to owner | ||
) | ||
} | ||
|
||
private fun Intermediate.contentValues(): ContentValues { | ||
return contentValuesOf( | ||
"progress" to progress, | ||
"coord_sys" to location.coordinateSystem.ordinal, | ||
"pos_la" to location.latitude, | ||
"pos_lg" to location.longitude, | ||
"elapsed" to elapsed | ||
) | ||
} | ||
|
||
private fun ContentResolver.sendStop(authority: String, id: String) { | ||
update(Uri.parse("content://$authority/state"), contentValuesOf("running" to false), id, null) | ||
} |
34 changes: 34 additions & 0 deletions
34
cp_plugin/src/main/java/com/zhufucdev/cp_plugin/ControllerReceiver.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,34 @@ | ||
package com.zhufucdev.cp_plugin | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Build | ||
import androidx.work.ExistingWorkPolicy | ||
import androidx.work.OneTimeWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import com.highcapable.yukihookapi.hook.factory.prefs | ||
import com.zhufucdev.cp_plugin.provider.EmulationBridgeService | ||
import com.zhufucdev.stub_plugin.MePlugin | ||
import com.zhufucdev.stub_plugin.PluginBroadcastReceiver | ||
|
||
class ControllerReceiver : PluginBroadcastReceiver() { | ||
override fun onEmulationStart(context: Context) { | ||
context.prefs(SHARED_PREFS_NAME).edit { | ||
putInt("method", MePlugin.queryMethod(context).ordinal) | ||
} | ||
|
||
// Intent(context, EmulationBridgeService::class.java).apply { | ||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
// context.startForegroundService(this) | ||
// } else { | ||
// context.startService(this) | ||
// } | ||
// } | ||
} | ||
|
||
override fun onEmulationStop(context: Context) { | ||
// Intent(context, EmulationBridgeService::class.java).apply { | ||
// context.stopService(this) | ||
// } | ||
} | ||
} |
Oops, something went wrong.