Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release #60

Merged
merged 13 commits into from
Jun 21, 2022
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ build
temp
/.semaphore/
/.github/
/gradlew
/gradlew.bat
7 changes: 5 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdk 21
targetSdk 31
versionCode 10
versionName "2.0.0"
versionName "2.0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -30,6 +30,9 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
viewBinding {
enabled = true
}
}

dependencies {
Expand Down Expand Up @@ -63,5 +66,5 @@ dependencies {
//获取wifi需要
implementation("io.github.thanosfisherman.wifiutils:wifiutils:1.6.6")

compileOnly fileTree(dir: 'libs', include: ['*.jar'])
compileOnly fileTree(dir: 'src/libs', include: ['*.jar'])
}
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="org.cloud.sonic.android">

<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <!-- 获取wifi需要 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Expand Down
46 changes: 24 additions & 22 deletions app/src/main/java/org/cloud/sonic/android/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,35 @@ import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.Utils
import com.gyf.immersionbar.ktx.immersionBar
import org.cloud.sonic.android.databinding.ActivityMainBinding
import org.cloud.sonic.android.service.SonicManagerService

class MainActivity : AppCompatActivity() {
private val REQUEST_CODE_START_CAPTURE = 2

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
immersionBar {
statusBarColor(R.color.white)
navigationBarColor(R.color.white)
statusBarDarkFont(true)
}

SonicManagerService.start(this)

Handler(Looper.getMainLooper()) {
finish()
false
}.sendEmptyMessageDelayed(0,1500)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

immersionBar {
statusBarColor(R.color.white)
navigationBarColor(R.color.white)
statusBarDarkFont(true)
}

SonicManagerService.start(this)

}
Handler(Looper.getMainLooper()) {
finish()
false
}.sendEmptyMessageDelayed(0, 1500)
binding.version.text = AppUtils.getAppVersionName()
}

override fun finish() {
super.finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
override fun finish() {
super.finish()
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/org/cloud/sonic/android/model/SonicAppInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.cloud.sonic.android.model

data class SonicAppInfo(
val appName:String,
val packageName:String,
val versionName:String,
val versionCode:Int,
val appIcon:ByteArray
val appName: String? = "",
val packageName: String? = "",
val versionName: String? = "",
val versionCode: Int,
val appIcon: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) [SonicCloudOrg] Sonic Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.cloud.sonic.android.plugin

import android.content.Context
import android.net.LocalServerSocket
import android.net.LocalSocket
import android.os.Handler
import android.os.Looper
import java.io.IOException
import java.lang.reflect.Method


class SonicPluginAllAppListService(var handler: Handler?) : Thread() {
lateinit var appListPlugin: SonicPluginAppList

companion object {
@JvmStatic
fun main(args: Array<String>) {
try {
Looper.prepare()
val handler = Handler()
val m = SonicPluginAllAppListService(handler)
m.start()
println("SonicPluginAllAppListService starting:start()")
Looper.loop()
} catch (e: InterruptedException) {
println("ERROR:${e.message}")
}
}
}

private fun getContext(): Context {
val activityThread = Class.forName("android.app.ActivityThread")
val systemMain: Method = activityThread.getDeclaredMethod("systemMain")
val objectSystemMain: Any = systemMain.invoke(null)
val contextImpl = Class.forName("android.app.ContextImpl")
val createSystemContext: Method =
contextImpl.getDeclaredMethod("createSystemContext", activityThread)
createSystemContext.isAccessible = true
val contextInstance: Context = createSystemContext.invoke(null, objectSystemMain) as Context
return contextInstance.createPackageContext(
"org.cloud.sonic.android",
Context.CONTEXT_INCLUDE_CODE or Context.CONTEXT_IGNORE_SECURITY
)
}

private var serverSocket: LocalServerSocket? = null
private val SOCKET = "sonic_plugin_all_app_list_service"

init {
appListPlugin = SonicPluginAppList(getContext())
}


override fun run() {

try {
println(String.format("creating socket %s", SOCKET))
serverSocket = LocalServerSocket(SOCKET)
} catch (e: IOException) {
println(e.message)
e.printStackTrace()
return
}

manageClientConnection()

try {
serverSocket?.close()
} catch (e: IOException) {
println(e.message)
e.printStackTrace()
}
println("socket closed.")
System.exit(0)
}

private fun manageClientConnection() {
println(String.format("Listening on %s", SOCKET))
val clientSocket: LocalSocket?
try {
clientSocket = serverSocket!!.accept()
processCommandLoop(clientSocket)
println("client connected")
} catch (e: IOException) {
println("error")
}
}

@Throws(IOException::class)
private fun processCommandLoop(clientSocket: LocalSocket) {
appListPlugin.getAllAppInfo(outputStream = clientSocket.outputStream)
}
}
103 changes: 59 additions & 44 deletions app/src/main/java/org/cloud/sonic/android/plugin/SonicPluginAppList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,63 +20,78 @@ import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.util.Base64
import android.util.Log
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.LogUtils
import org.cloud.sonic.android.model.SonicAppInfo
import java.io.IOException
import java.io.OutputStream

class SonicPluginAppList constructor (
private val context:Context):IPlugin {
class SonicPluginAppList constructor(
private val context: Context
) : IPlugin {

fun getAllAppInfo(outputStream: OutputStream){
val packages: List<PackageInfo> =
context.packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)
fun getAllAppInfo(outputStream: OutputStream) {
val packages: List<PackageInfo> =
context.packageManager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES)

for (i in packages.indices) {
val packageInfo = packages[i]
if (packageInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
val tmpInfo = SonicAppInfo(
appName = packageInfo. applicationInfo.loadLabel(context.packageManager).toString(),
packageName = packageInfo.packageName,
versionName = packageInfo.versionName,
versionCode = packageInfo.versionCode,
appIcon = ImageUtils.drawable2Bytes(packageInfo.applicationInfo.loadIcon(context.packageManager))
)

try {
val dataBytes:ByteArray = GsonUtils.toJson(tmpInfo).toByteArray()
// 数据长度转成二进制,存入byte[32]
val lengthBytes = ByteArray(32)
val binStr = Integer.toBinaryString(dataBytes.size).trim { it <= ' ' }
val binArray = binStr.toCharArray()
var x = binArray.size - 1
var y = lengthBytes.size - 1
while (x >= 0) {
try {
lengthBytes[y] = (binArray[x].toString() + "").toByte()
} catch (e: Exception) {
LogUtils.e(String.format("char转byte失败,char为:【%s】", binArray[x].toString() + ""))
}
x--
y--
}
// 先发送长度
outputStream.write(lengthBytes)
outputStream.flush()
for (i in packages.indices) {
val packageInfo = packages[i]
if (packageInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
val tmpInfo = SonicAppInfo(
appName = packageInfo.applicationInfo.loadLabel(context.packageManager).toString(),
packageName = packageInfo.packageName,
versionName = packageInfo.versionName,
versionCode = packageInfo.versionCode,
appIcon = Base64.encodeToString(
ImageUtils.drawable2Bytes(
packageInfo.applicationInfo.loadIcon(context.packageManager),
Bitmap.CompressFormat.PNG,
10
), Base64.NO_WRAP
)
)

// 再发送数据
outputStream.write(dataBytes)
outputStream.flush()
} catch (e: IOException) {
e.printStackTrace()
}
try {
val dataBytes: ByteArray = GsonUtils.toJson(tmpInfo).toByteArray()
// 数据长度转成二进制,存入byte[32]
val lengthBytes = ByteArray(32)
val binStr = Integer.toBinaryString(dataBytes.size).trim { it <= ' ' }
val binArray = binStr.toCharArray()
var x = binArray.size - 1
var y = lengthBytes.size - 1
while (x >= 0) {
try {
lengthBytes[y] = (binArray[x].toString() + "").toByte()
} catch (e: Exception) {
LogUtils.e(
String.format(
"char transfer byte failed, char: %s",
binArray[x].toString() + ""
)
)
}
x--
y--
}
// 先发送长度
outputStream.write(lengthBytes)
outputStream.flush()

// 再发送数据
outputStream.write(dataBytes)
outputStream.flush()
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}

override fun initPlugin(context: Context) {
override fun initPlugin(context: Context) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class SonicPluginAudioService : Service() {
try {
lengthBytes[y] = (binArray[x].toString() + "").toByte()
} catch (e: Exception) {
Log.i(TAG, String.format("char转byte失败,char为:【%s】", binArray[x].toString() + ""))
Log.i(TAG, String.format("char transfer byte failed, char: %s", binArray[x].toString() + ""))
}
x--
y--
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class SonicManagerService : Service() {
}
}
else -> {
Log.e("AudioService", "why are you here?")
Log.e("ManagerService", "why are you here?")
}
}
}
Expand Down
Loading