package com.example.piliplus import android.app.PendingIntent import android.app.PictureInPictureParams import android.app.SearchManager import android.content.ComponentName import android.content.Intent import android.content.pm.PackageManager import android.content.pm.ShortcutInfo import android.content.pm.ShortcutManager import android.content.res.Configuration import android.graphics.BitmapFactory import android.graphics.Point import android.graphics.drawable.Icon import android.os.Build import android.os.Bundle import android.provider.MediaStore import android.provider.Settings import android.view.WindowManager.LayoutParams import androidx.core.net.toUri import com.ryanheise.audioservice.AudioServiceActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel import kotlin.math.roundToInt import kotlin.system.exitProcess import java.io.File class MainActivity : AudioServiceActivity() { private lateinit var methodChannel: MethodChannel private var isFoldable = false override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) methodChannel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, "PiliPlus") methodChannel.setMethodCallHandler { call, result -> when (call.method) { "back" -> back(); "biliSendCommAntifraud" -> { try { val action = call.argument("action") ?: 0 val oid = call.argument("oid") ?: 0L val type = call.argument("type") ?: 0 val rpid = call.argument("rpid") ?: 0L val root = call.argument("root") ?: 0L val parent = call.argument("parent") ?: 0L val ctime = call.argument("ctime") ?: 0L val commentText = call.argument("comment_text") ?: "" val pictures = call.argument("pictures") val sourceId = call.argument("source_id") ?: "" val uid = call.argument("uid") ?: 0L val cookies = call.argument>("cookies") ?: emptyList() val intent = Intent().apply { component = ComponentName( "icu.freedomIntrovert.biliSendCommAntifraud", "icu.freedomIntrovert.biliSendCommAntifraud.ByXposedLaunchedActivity" ) putExtra("action", action) putExtra("oid", oid.toLong()) putExtra("type", type) putExtra("rpid", rpid.toLong()) putExtra("root", root.toLong()) putExtra("parent", parent.toLong()) putExtra("ctime", ctime.toLong()) putExtra("comment_text", commentText) if (pictures != null) putExtra("pictures", pictures) putExtra("source_id", sourceId) putExtra("uid", uid.toLong()) putStringArrayListExtra("cookies", ArrayList(cookies)) } startActivity(intent) } catch (_: Exception) { } } "linkVerifySettings" -> { val uri = ("package:" + context.packageName).toUri() try { val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { Intent(Settings.ACTION_APP_OPEN_BY_DEFAULT_SETTINGS, uri) } else { Intent("android.intent.action.MAIN", uri).setClassName( "com.android.settings", "com.android.settings.applications.InstalledAppOpenByDefaultActivity" ) } context.startActivity(intent) } catch (_: Throwable) { val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri) context.startActivity(intent) } } "music" -> { val title = call.argument("title") val intent = Intent(MediaStore.INTENT_ACTION_MEDIA_SEARCH).apply { putExtra(SearchManager.QUERY, title) putExtra(MediaStore.EXTRA_MEDIA_TITLE, title) call.argument("artist") ?.let { putExtra(MediaStore.EXTRA_MEDIA_ARTIST, it) } call.argument("album") ?.let { putExtra(MediaStore.EXTRA_MEDIA_ALBUM, it) } addCategory(Intent.CATEGORY_DEFAULT) } try { if (packageManager.resolveActivity( intent, PackageManager.MATCH_DEFAULT_ONLY ) != null ) { startActivity(intent) result.success(true) return@setMethodCallHandler } } catch (_: Throwable) { } try { intent.action = MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH if (packageManager.resolveActivity( intent, PackageManager.MATCH_DEFAULT_ONLY ) != null ) { startActivity(intent) result.success(true) return@setMethodCallHandler } } catch (_: Throwable) { } result.success(false) } "setPipAutoEnterEnabled" -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { val params = PictureInPictureParams.Builder() .setAutoEnterEnabled(call.argument("autoEnable") ?: false) .build() setPictureInPictureParams(params) } } "createShortcut" -> { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { try { val shortcutManager = context.getSystemService(ShortcutManager::class.java) if (shortcutManager.isRequestPinShortcutSupported) { val id = call.argument("id")!! val uri = call.argument("uri")!! val label = call.argument("label")!! val icon = call.argument("icon")!! val bitmap = BitmapFactory.decodeFile(icon) val shortcut = ShortcutInfo.Builder(context, id) .setShortLabel(label) .setIcon(Icon.createWithAdaptiveBitmap(bitmap)) .setIntent(Intent(Intent.ACTION_VIEW, uri.toUri())) .build() val pinIntent = shortcutManager.createShortcutResultIntent(shortcut) val pendingIntent = PendingIntent.getBroadcast( context, 0, pinIntent, PendingIntent.FLAG_IMMUTABLE ) shortcutManager.requestPinShortcut( shortcut, pendingIntent.intentSender ) } } catch (e: Exception) { } } } "maxScreenSize" -> { maxScreenSize()?.let { result.success(it) } } "isFoldable" -> { result.success(isFoldable) } else -> result.notImplemented() } } } override fun onConfigurationChanged(newConfig: Configuration) { super.onConfigurationChanged(newConfig) if (isFoldable) { maxScreenSize()?.let { MethodChannel( flutterEngine!!.dartExecutor.binaryMessenger, "ScreenChannel" ).invokeMethod("onConfigChanged", it) } } } private fun maxScreenSize(): Map? { try { val density = resources.displayMetrics.density if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { val maxBounds = windowManager.maximumWindowMetrics.bounds return mapOf( "maxWidth" to (maxBounds.width() / density).roundToInt(), "maxHeight" to (maxBounds.height() / density).roundToInt(), ) } else { val realSizePoint = Point() windowManager.defaultDisplay.getRealSize(realSizePoint) return mapOf( "maxWidth" to (realSizePoint.x / density).roundToInt(), "maxHeight" to (realSizePoint.y / density).roundToInt(), ) } } catch (e: Exception) { return null } } private fun back() { val intent = Intent(Intent.ACTION_MAIN).apply { addCategory(Intent.CATEGORY_HOME) flags = Intent.FLAG_ACTIVITY_NEW_TASK } startActivity(intent) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { window.attributes.layoutInDisplayCutoutMode = LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { try { isFoldable = packageManager.hasSystemFeature(PackageManager.FEATURE_SENSOR_HINGE_ANGLE) } catch (e: Exception) { } } } override fun onDestroy() { stopService(Intent(this, com.ryanheise.audioservice.AudioService::class.java)) super.onDestroy() android.os.Process.killProcess(android.os.Process.myPid()) exitProcess(0) } override fun onUserLeaveHint() { super.onUserLeaveHint() methodChannel.invokeMethod("onUserLeaveHint", null) } override fun onPictureInPictureModeChanged( isInPictureInPictureMode: Boolean, newConfig: Configuration? ) { super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig) MethodChannel( flutterEngine!!.dartExecutor.binaryMessenger, "floating" ).invokeMethod("onPipChanged", isInPictureInPictureMode) } }