From f18d13e635e39aff0137153742e2f5772eb9795d Mon Sep 17 00:00:00 2001 From: lisonge Date: Mon, 16 Oct 2023 10:23:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20HTTP=E6=9C=8D=E5=8A=A1=E6=97=A0=E6=9D=83?= =?UTF-8?q?=E9=99=90=E8=8E=B7=E5=8F=96host=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/li/songe/gkd/util/Ext.kt | 133 ++------------------- 1 file changed, 9 insertions(+), 124 deletions(-) diff --git a/app/src/main/java/li/songe/gkd/util/Ext.kt b/app/src/main/java/li/songe/gkd/util/Ext.kt index bc9826a..c552e8d 100644 --- a/app/src/main/java/li/songe/gkd/util/Ext.kt +++ b/app/src/main/java/li/songe/gkd/util/Ext.kt @@ -1,25 +1,11 @@ package li.songe.gkd.util -import android.app.NotificationChannel -import android.app.NotificationManager -import android.app.PendingIntent -import android.app.Service -import android.content.Intent import android.content.pm.ApplicationInfo import android.content.pm.PackageManager -import android.content.pm.ServiceInfo import android.graphics.Bitmap -import android.media.ImageReader import android.os.Build -import android.os.Handler -import android.os.Looper -import androidx.compose.runtime.* -import androidx.core.app.NotificationCompat -import androidx.core.app.NotificationManagerCompat -import kotlinx.coroutines.suspendCancellableCoroutine -import li.songe.gkd.MainActivity +import com.blankj.utilcode.util.ToastUtils import java.net.NetworkInterface -import kotlin.coroutines.resume object Ext { @@ -32,7 +18,7 @@ object Ext { packageName, PackageManager.ApplicationInfoFlags.of(value.toLong()) ) } else { - @Suppress("DEPRECATION") getApplicationInfo( + getApplicationInfo( packageName, value ) } @@ -43,70 +29,14 @@ object Ext { return this.sameAs(emptyBitmap) } - suspend fun ImageReader.awaitImageAvailable(screenWidth: Int, screenHeight: Int): Bitmap = - suspendCancellableCoroutine { block -> - setOnImageAvailableListener({ imageReader -> - - val image = imageReader.acquireLatestImage() - ?: throw Error("get screen image result failed") - - val planes = image.planes - val buffer = planes[0].buffer - - val pixelStride = planes[0].pixelStride - val rowStride = planes[0].rowStride - val rowPadding: Int = rowStride - pixelStride * screenWidth - var bitmap = Bitmap.createBitmap( - screenWidth + rowPadding / pixelStride, screenHeight, Bitmap.Config.ARGB_8888 - ) - bitmap.copyPixelsFromBuffer(buffer) - bitmap = Bitmap.createBitmap(bitmap, 0, 0, screenWidth, screenHeight) - image.close() - - if (!bitmap.isEmptyBitmap()) { - setOnImageAvailableListener(null, null) - block.resume(bitmap) - } - }, Handler(Looper.getMainLooper())) - - block.invokeOnCancellation { setOnImageAvailableListener(null, null) } - } - - fun createNotificationChannel(context: Service, notificationId: Int) { - val channelId = "CHANNEL_ID" - val intent = Intent(context, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - } - - val pendingIntent: PendingIntent = PendingIntent.getActivity( - context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - ) - - val builder = NotificationCompat.Builder(context, channelId).setSmallIcon(SafeR.ic_launcher) - .setContentTitle("调试模式").setContentText("正在录制您的屏幕内容") - .setContentIntent(pendingIntent).setPriority(NotificationCompat.PRIORITY_DEFAULT) - .setOngoing(true).setAutoCancel(false) - - val name = "调试模式" - val descriptionText = "屏幕录制" - val importance = NotificationManager.IMPORTANCE_DEFAULT - val channel = NotificationChannel(channelId, name, importance).apply { - description = descriptionText - } - val notificationManager = NotificationManagerCompat.from(context) - notificationManager.createNotificationChannel(channel) - val notification = builder.build() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - context.startForeground( - notificationId, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION - ) - } else { - context.startForeground(notificationId, notification) - } - } - fun getIpAddressInLocalNetwork(): Sequence { - val networkInterfaces = NetworkInterface.getNetworkInterfaces().iterator().asSequence() + val networkInterfaces = try { + // android.system.ErrnoException: getifaddrs failed: EACCES (Permission denied) + NetworkInterface.getNetworkInterfaces().iterator().asSequence() + } catch (e: Exception) { + ToastUtils.showShort("获取host失败:" + e.message) + emptySequence() + } val localAddresses = networkInterfaces.flatMap { it.inetAddresses.asSequence().filter { inetAddress -> inetAddress.isSiteLocalAddress && !(inetAddress.hostAddress?.contains(":") @@ -116,49 +46,4 @@ object Ext { return localAddresses } - fun createNotificationChannel(context: Service) { -// 通知渠道 - val channelId = "无障碍服务" - val name = "无障碍服务" - val desc = "显示无障碍服务状态" - - val importance = NotificationManager.IMPORTANCE_DEFAULT - val channel = NotificationChannel(channelId, name, importance) - channel.description = desc - val notificationManager = NotificationManagerCompat.from(context) - notificationManager.createNotificationChannel(channel) - - val icon = SafeR.ic_launcher - val title = "搞快点" - val text = "无障碍正在运行" - val id = 100 - val ongoing = true - val autoCancel = false - - notificationManager.cancel(id) - - val intent = Intent(context, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - } - val pendingIntent: PendingIntent = PendingIntent.getActivity( - context, 0, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - ) - - val builder = - NotificationCompat.Builder(context, channelId).setSmallIcon(icon).setContentTitle(title) - .setContentText(text).setContentIntent(pendingIntent) - .setPriority(NotificationCompat.PRIORITY_DEFAULT).setOngoing(ongoing) - .setAutoCancel(autoCancel) - - val notification = builder.build() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - context.startForeground( - id, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST - ) - } else { - context.startForeground(id, notification) - } - - } - } \ No newline at end of file