Skip to content

Commit

Permalink
fix display issues and bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xuduo committed Dec 6, 2023
1 parent ba5ccc7 commit 577dfa1
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private fun ActionImageScreenContent(
val logger = LocalLogger.current
val actions by viewModel.getActionsByRecordingId(recordingId)
.observeAsState()
actions?.value?.let {
actions?.let {
val pagerState =
rememberPagerState(initialPage = it.indexOfFirst { item -> item.id == actionId }) { it.size }
HorizontalPager(
Expand All @@ -81,7 +81,7 @@ private fun ActionImageScreenContent(
.fillMaxSize()
.background(Color.Black)
) { page ->
val action = actions?.value?.get(page)
val action = actions?.get(page)
action?.let { theAction ->
LaunchedEffect(pagerState.currentPage) {
// This block is called when the currentPage changes
Expand All @@ -92,40 +92,59 @@ private fun ActionImageScreenContent(
}.observeAsState()
Log.d(
"ActionImageScreen",
"draw page ${actionImage?.id} ${theAction.id} ${theAction.cords} ${actionImage?.screenShot?.size}"
"draw action page cords:${theAction.cords} bounds:${theAction.viewBounds} screen:${theAction.screenWidth}x${theAction.screenHeight} ${actionImage?.screenShot?.size}"
)
actionImage?.let {
actionImage?.let { theActionImage ->
val imageBitmap =
BitmapFactory.decodeByteArray(it.screenShot, 0, it.screenShot.size)
BitmapFactory.decodeByteArray(
theActionImage.screenShot,
0,
theActionImage.screenShot.size
)
.asImageBitmap()
BoxWithConstraints {
BoxWithConstraints(
modifier
.fillMaxSize()) {
// Calculate aspect ratio
val aspectRatioBox = maxWidth / maxHeight
val aspectRatioImage =
imageBitmap.width.toFloat() / imageBitmap.height.toFloat()
val mod = if (aspectRatioImage > aspectRatioBox) {
val ratio = aspectRatioBox / aspectRatioImage
val mod = if (ratio < 1) {
// Image is wider than box, add vertical padding
val scaledImageHeight = maxWidth.value / aspectRatioImage
val scaledImageHeight = maxHeight.value * ratio
val totalPadding = maxHeight.value - scaledImageHeight
val padding = totalPadding / 2
Log.d("ActionImageScreen", "padding v $padding")
Log.d(
"ActionImageScreen",
"padding v $ratio $padding $aspectRatioBox $aspectRatioImage ${imageBitmap.width}x${imageBitmap.height} $maxWidth"
)
modifier
.padding(top = padding.dp, bottom = padding.dp)
} else {
// Image is taller than box, add horizontal padding
val scaledImageWidth = maxHeight.value * aspectRatioImage
val scaledImageWidth = maxWidth.value / ratio
val totalPadding = maxWidth.value - scaledImageWidth
val padding = totalPadding / 2
Log.d("ActionImageScreen", "padding h $padding")
Log.d(
"ActionImageScreen",
"padding h $ratio $padding $aspectRatioBox $aspectRatioImage ${imageBitmap.width}x${imageBitmap.height} $maxWidth"
)
modifier
.padding(start = padding.dp, end = padding.dp)
}
BoxWithConstraints(modifier = mod) {
Image(
bitmap = imageBitmap,
modifier = modifier
.padding(0.dp)
.fillMaxSize(),
contentDescription = "Screenshot", // Provide a description for accessibility
contentScale = ContentScale.Crop // Adjust content scale as needed
contentScale = ContentScale.FillBounds // Adjust content scale as needed
)
// Box(modifier = modifier
// .background(Color.Yellow)
// .fillMaxSize())
Image(
painter = painterResource(id = R.drawable.touch_app),
contentDescription = null, // Provide a suitable content description
Expand All @@ -139,7 +158,7 @@ private fun ActionImageScreenContent(
val rect = action.getRelativeViewBounds(
size
)
logger.d("rect ${action.bounds} $rect")
logger.d("rect ${action.viewBounds} $rect")
drawRect(
color = Color.Red,
size = Size(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.xd.mvvm.testrecorder.R
import com.xd.mvvm.testrecorder.data.Action
import com.xd.mvvm.testrecorder.goToActionImage
import com.xd.mvvm.testrecorder.logger.L
import com.xd.mvvm.testrecorder.util.LiveDataLoadingContent
import com.xd.mvvm.testrecorder.util.RefreshingLoadingContent
import com.xd.mvvm.testrecorder.widget.AppBar

Expand All @@ -37,7 +38,7 @@ fun ActionListScreen(
scaffoldState = scaffoldState,
modifier = modifier.fillMaxSize(),
topBar = {
AppBar(R.string.choose_app_to_record) // Change the string resource to your app's name
AppBar(R.string.action_list) // Change the string resource to your app's name
},
) {
ActionListScreenContent(
Expand All @@ -53,11 +54,10 @@ private fun ActionListScreenContent(
viewModel: RecordingViewModel = hiltViewModel(),
modifier: Modifier,
) {
val data by viewModel.getActionsByRecordingId(recordingId).observeAsState()
val data = viewModel.getActionsByRecordingId(recordingId)
L.d("RecordingViewModel.getActionsByRecordingId() ${data?.value}")
RefreshingLoadingContent(
data,
onRefresh = { viewModel.getAllRecordings() }
LiveDataLoadingContent(
data
) {
LazyColumn {
L.d("WeatherContent RecordingListScreenContent ${it.size}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ import androidx.hilt.navigation.compose.hiltViewModel
import com.xd.mvvm.testrecorder.LocalNavController
import com.xd.mvvm.testrecorder.R
import com.xd.mvvm.testrecorder.data.Recording
import com.xd.mvvm.testrecorder.data.RecordingWithActionCount
import com.xd.mvvm.testrecorder.goToActionList
import com.xd.mvvm.testrecorder.logger.L
import com.xd.mvvm.testrecorder.util.LiveDataLoadingContent
import com.xd.mvvm.testrecorder.util.RefreshingLoadingContent
import com.xd.mvvm.testrecorder.widget.AppBar

Expand All @@ -38,7 +40,7 @@ fun RecordingListScreen(
scaffoldState = scaffoldState,
modifier = modifier.fillMaxSize(),
topBar = {
AppBar(R.string.choose_app_to_record) // Change the string resource to your app's name
AppBar(R.string.recording_list) // Change the string resource to your app's name
},
) {
RecordingListScreenContent(
Expand All @@ -52,10 +54,9 @@ private fun RecordingListScreenContent(
viewModel: RecordingViewModel = hiltViewModel(),
modifier: Modifier,
) {
val data by viewModel.getAllRecordings().observeAsState()
RefreshingLoadingContent(
data,
onRefresh = { viewModel.getAllRecordings() }
val data = viewModel.getAllRecordings()
LiveDataLoadingContent(
data
) {
LazyColumn {
L.d("WeatherContent RecordingListScreenContent ${it.size}")
Expand All @@ -70,22 +71,22 @@ private fun RecordingListScreenContent(

@Composable
private fun Item(
recording: Recording
recording: RecordingWithActionCount
) {
val nav = LocalNavController.current
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable {
nav.goToActionList(recording.id)
nav.goToActionList(recording.recording.id)
}
.padding(
horizontal = dimensionResource(id = R.dimen.horizontal_margin),
vertical = dimensionResource(id = R.dimen.list_item_padding),
)
) {
recording.getIconBitmap()?.let {
recording.recording.getIconBitmap()?.let {
Image(
bitmap = it,
contentDescription = "App Icon",
Expand All @@ -94,12 +95,12 @@ private fun Item(
.size(40.dp)
)
Text(
text = recording.name ?: "",
text = "${recording.actionCount} Actions",
style = MaterialTheme.typography.h6,
modifier = Modifier.weight(1f)
)
Text(
text = recording.getFormattedCreateTime(),
text = recording.recording.getFormattedCreateTime(),
style = MaterialTheme.typography.h6,
)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,7 @@
<string name="overlay">Overlay</string>
<string name="choose_app_to_record">Choose App to Record</string>
<string name="actions">Actions</string>
<string name="action_list">Action List</string>
<string name="recording_list">Recording List</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TouchAccessibilityService : AccessibilityService() {
}

override fun onAccessibilityEvent(event: AccessibilityEvent?) {
logger.d(
logger.v(
"onAccessibilityEvent $foregroundPackageName ${event?.eventType?.toString(16)} ${event?.className} ${
formatEventTime(
event?.eventTime!!
Expand Down Expand Up @@ -108,31 +108,39 @@ class TouchAccessibilityService : AccessibilityService() {
path.computeBounds(gestureBounds, true)
logger.d("getViewForGesture $gestureBounds $rootInActiveWindow")
val rootInActiveWindow = rootInActiveWindow ?: return null
val validViews = mutableListOf<AccessibilityNodeInfo>()
return findViewForGesture(rootInActiveWindow, gestureBounds.toRect())
}

private fun findViewForGesture(
node: AccessibilityNodeInfo,
gestureBounds: Rect
): AccessibilityNodeInfo? {
val bounds = Rect()
node.getBoundsInScreen(bounds)
if (bounds.contains(gestureBounds)) {
for (i in 0 until node.childCount) {
val childNode = node.getChild(i)
childNode?.let {
val result = findViewForGesture(it, gestureBounds)
if (result != null) {
return result
private fun findViewForGesture(node: AccessibilityNodeInfo, bounds: Rect): AccessibilityNodeInfo? {
// Base case: if the node is null or not visible, return null
if (node.isVisibleToUser.not()) {
return null
}

// Check if the node is clickable and its bounds contain the specified bounds
val nodeBounds = Rect()
node.getBoundsInScreen(nodeBounds)
val isClickable = node.isClickable && nodeBounds.contains(bounds)

var bestMatch: AccessibilityNodeInfo? = if (isClickable) node else null

// Recursively search child nodes
for (i in 0 until node.childCount) {
node.getChild(i)?.let { childNode ->
val foundNode = findViewForGesture(childNode, bounds)
if (foundNode != null) {
// Compare the bounds to find the smallest node
val foundNodeBounds = Rect()
foundNode.getBoundsInScreen(foundNodeBounds)
if (bestMatch == null || foundNodeBounds.width() * foundNodeBounds.height() < nodeBounds.width() * nodeBounds.height()) {
bestMatch = foundNode
}
}
}
if (node.packageName == recordingPackageName) {
return node
}
}

return null
return bestMatch
}

fun testDispatch() {
Expand Down Expand Up @@ -165,48 +173,6 @@ class TouchAccessibilityService : AccessibilityService() {
// サービスが中断された時の処理
}

fun createGestureDescription(motionEvents: List<MotionEvent>): GestureDescription? {
if (motionEvents.size < 2) return null

val actionDown = motionEvents.firstOrNull { it.action == MotionEvent.ACTION_DOWN }
val actionUp = motionEvents.lastOrNull { it.action == MotionEvent.ACTION_UP }

if (actionDown == null || actionUp == null) return null

val clickDurationThreshold = 200 // milliseconds
if (actionUp.eventTime - actionDown.eventTime > clickDurationThreshold) return null

val movementThreshold = 10 // pixels
if (Math.abs(actionUp.x - actionDown.x) > movementThreshold ||
Math.abs(actionUp.y - actionDown.y) > movementThreshold
) {
return null
}

// Create a path for the click gesture at the position of the ACTION_DOWN event
val clickPath = Path().apply {
moveTo(actionDown.x, actionDown.y + statusBarHeight)
}

// Create a GestureDescription for the click
val clickGesture = GestureDescription.Builder().apply {
addStroke(GestureDescription.StrokeDescription(clickPath, 0, 100)) // 1ms duration
}.build()

return clickGesture
}

// Implement this method to calculate the duration of your gesture
// private fun calculateGestureDuration(): Long {
// // Example implementation
// return motionEventList.last().eventTime - motionEventList.first().downTime
// }

private fun logEvent(message: String) {
// ログ記録のためのメソッド(実際の実装に合わせてください)
println(message)
}

fun getStatusBarHeight(): Int {
var result = 0
val resourceId = this.resources.getIdentifier("status_bar_height", "dimen", "android")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.room.Query
import androidx.room.Update
import androidx.room.Delete
import com.xd.mvvm.testrecorder.data.Recording
import com.xd.mvvm.testrecorder.data.RecordingWithActionCount

@Dao
interface RecordingDao {
Expand All @@ -27,6 +28,9 @@ interface RecordingDao {
@Query("SELECT * FROM recordings order by id desc")
fun getAllRecordings(): LiveData<List<Recording>>

@Query("SELECT recordings.*, (SELECT COUNT(*) FROM actions WHERE recordingId = recordings.id) AS actionCount FROM recordings order by id desc")
fun getRecordingsWithActionCount(): LiveData<List<RecordingWithActionCount>>

// Query a single recording by ID
@Query("SELECT * FROM recordings WHERE id = :id")
suspend fun getRecordingById(id: Long): Recording?
Expand Down
Loading

0 comments on commit 577dfa1

Please sign in to comment.