-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Note that Compose is still not supported, but having this sample in place ensures that we keep the build working for Compose going forward. Example of output for an app that has a Compose view: ``` com.squareup.radiography.sample.compose.MainActivity: window-focus:false DecorView { 1080×2160px } +-LinearLayout { 1080×2028px } | +-ViewStub { id:action_mode_bar_stub, GONE, 0×0px } | `-FrameLayout { 1080×1962px } | `-FitWindowsLinearLayout { id:action_bar_root, | +-ViewStubCompat { id:action_mode_bar_stub, GONE, | `-ContentFrameLayout { id:content, 1080×1962px } | `-AndroidComposeView { 770×425px, focused } | `-AndroidViewsHandler { 770×425px } | `-ViewBlockHolder { 160×53px } | `-TextView { 160×53px, text-length:9 } +-View { id:navigationBarBackground, 1080×132px } `-View { id:statusBarBackground, 1080×66px } ```
- Loading branch information
1 parent
0075651
commit 5ae1bdd
Showing
8 changed files
with
250 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id("com.android.application") | ||
kotlin("android") | ||
} | ||
|
||
android { | ||
compileSdkVersion(30) | ||
|
||
compileOptions { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
defaultConfig { | ||
minSdkVersion(21) | ||
targetSdkVersion(30) | ||
applicationId = "com.squareup.radiography.sample.compose" | ||
} | ||
|
||
buildFeatures { | ||
compose = true | ||
} | ||
|
||
composeOptions { | ||
kotlinCompilerVersion = Versions.KotlinCompiler | ||
kotlinCompilerExtensionVersion = Versions.Compose | ||
} | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
freeCompilerArgs = listOf( | ||
"-Xallow-jvm-ir-dependencies", | ||
"-Xskip-prerelease-check", | ||
"-Xopt-in=kotlin.RequiresOptIn" | ||
) | ||
} | ||
} | ||
|
||
dependencies { | ||
// Don't use Versions.KotlinStdlib, since this module actually uses the Compose compiler and needs | ||
// the modern stdlib. | ||
implementation(kotlin("stdlib")) | ||
implementation(project(":radiography")) | ||
implementation(Dependencies.Compose.Tooling) | ||
implementation("androidx.compose.material:material:${Versions.Compose}") | ||
implementation("androidx.appcompat:appcompat:1.2.0") | ||
} |
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,17 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.squareup.radiography.sample.compose"> | ||
|
||
<application android:label="Radiography Compose"> | ||
|
||
<activity | ||
android:name=".MainActivity" | ||
android:theme="@style/AppTheme"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
</activity> | ||
|
||
</application> | ||
</manifest> |
133 changes: 133 additions & 0 deletions
133
sample-compose/src/main/java/com/squareup/radiography/sample/compose/ComposeSampleApp.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,133 @@ | ||
package com.squareup.radiography.sample.compose | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.graphics.Typeface | ||
import android.util.Log | ||
import android.widget.LinearLayout | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AlertDialog.Builder | ||
import androidx.compose.foundation.Box | ||
import androidx.compose.foundation.Text | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.material.Button | ||
import androidx.compose.material.Checkbox | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Composer | ||
import androidx.compose.runtime.currentComposer | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.ContextAmbient | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.viewinterop.emitView | ||
import radiography.Radiography | ||
import radiography.ViewFilter | ||
import radiography.ViewFilters.FocusedWindowViewFilter | ||
import radiography.ViewFilters.and | ||
import radiography.ViewStateRenderers.CheckableRenderer | ||
import radiography.ViewStateRenderers.DefaultsIncludingPii | ||
import radiography.ViewStateRenderers.DefaultsNoPii | ||
import radiography.ViewStateRenderers.ViewRenderer | ||
import radiography.ViewStateRenderers.textViewRenderer | ||
import radiography.ViewStateRenderers.viewStateRendererFor | ||
|
||
@Composable fun ComposeSampleApp() { | ||
val (isChecked, onCheckChanged) = remember { mutableStateOf(false) } | ||
var text by remember { mutableStateOf("") } | ||
val context = ContextAmbient.current | ||
val composer = currentComposer | ||
|
||
Column { | ||
Text("The password is Baguette", style = MaterialTheme.typography.body2) | ||
Row(verticalGravity = Alignment.CenterVertically) { | ||
Checkbox(checked = isChecked, onCheckedChange = onCheckChanged) | ||
Text("Check me, or don't.") | ||
} | ||
TextField(value = text, onValueChange = { text = it }, label = { Text("Text Field") }) | ||
// Include a classic Android view in the composition. | ||
emitView(::TextView) { | ||
@SuppressLint("SetTextI18n") | ||
it.text = "inception" | ||
} | ||
Box(Modifier.testTag("show-rendering")) { | ||
Button(onClick = { showSelectionDialog(context, composer) }) { | ||
Text("Show string rendering dialog") | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun showSelectionDialog( | ||
context: Context, | ||
composer: Composer<*> | ||
) { | ||
val renderings = listOf( | ||
"Default" to { | ||
Radiography.scan() | ||
}, | ||
"Focused window" to { | ||
Radiography.scan(viewFilter = FocusedWindowViewFilter) | ||
}, | ||
"Focused window and custom filter" to { | ||
Radiography.scan(viewFilter = FocusedWindowViewFilter and object : ViewFilter { | ||
override fun matches(view: Any): Boolean = view !is LinearLayout | ||
}) | ||
}, | ||
"Include PII" to { | ||
Radiography.scan(viewStateRenderers = DefaultsIncludingPii) | ||
}, | ||
"Include PII ellipsized" to { | ||
Radiography.scan( | ||
viewStateRenderers = listOf( | ||
ViewRenderer, | ||
textViewRenderer(includeTextViewText = true, textViewTextMaxLength = 4), | ||
CheckableRenderer, | ||
) | ||
) | ||
}, | ||
"Custom LinearLayout renderer" to { | ||
Radiography.scan( | ||
viewStateRenderers = DefaultsNoPii + viewStateRendererFor<LinearLayout> { | ||
append(if (it.orientation == LinearLayout.HORIZONTAL) "horizontal" else "vertical") | ||
}) | ||
} | ||
) | ||
|
||
val items = renderings.map { it.first } | ||
.toTypedArray() | ||
Builder(context) | ||
.setTitle("Choose rendering") | ||
.setItems(items) { _, index -> | ||
val rendering = renderings[index].second() | ||
// Print each line as a separate logcat entry so the total output doesn't get truncated. | ||
rendering.lineSequence().forEach { | ||
Log.d("MainActivity", it) | ||
} | ||
showResult(context, composer, rendering) | ||
} | ||
.show() | ||
} | ||
|
||
private fun showResult( | ||
context: Context, | ||
composer: Composer<*>, | ||
rendering: String | ||
) { | ||
val renderingDialog = Builder(context) | ||
.setTitle("Rendering (also printed to Logcat)") | ||
.setMessage(rendering) | ||
.setPositiveButton("Ok") { _, _ -> | ||
showSelectionDialog(context, composer) | ||
} | ||
.show() | ||
val messageView = renderingDialog.findViewById<TextView>(android.R.id.message)!! | ||
messageView.textSize = 9f | ||
messageView.typeface = Typeface.MONOSPACE | ||
} |
14 changes: 14 additions & 0 deletions
14
sample-compose/src/main/java/com/squareup/radiography/sample/compose/MainActivity.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,14 @@ | ||
package com.squareup.radiography.sample.compose | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.ui.platform.setContent | ||
|
||
class MainActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
ComposeSampleApp() | ||
} | ||
} | ||
} |
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,8 @@ | ||
<resources> | ||
|
||
<!-- Base application theme. --> | ||
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | ||
<!-- Customize your theme here. --> | ||
</style> | ||
|
||
</resources> |
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