Skip to content

Commit a31a0e1

Browse files
committed
Add button to test unpinned requests
1 parent 80c7283 commit a31a0e1

File tree

59 files changed

+121
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+121
-8
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@ android {
3333
}
3434

3535
dependencies {
36-
3736
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.7"
38+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.7"
3839
implementation 'androidx.core:core-ktx:1.3.2'
3940
implementation 'androidx.appcompat:appcompat:1.2.0'
4041
implementation 'com.google.android.material:material:1.3.0'
4142
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
42-
testImplementation 'junit:junit:4.+'
43-
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
44-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
4543
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="tech.httptoolkit.pinning_demo">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,82 @@
11
package tech.httptoolkit.pinning_demo
22

3-
import androidx.appcompat.app.AppCompatActivity
3+
import android.graphics.drawable.Drawable
44
import android.os.Bundle
5+
import android.view.View
6+
import android.widget.Button
7+
import android.widget.Toast
8+
import androidx.annotation.IdRes
9+
import androidx.appcompat.app.AppCompatActivity
10+
import androidx.core.content.ContextCompat
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.GlobalScope
13+
import kotlinx.coroutines.launch
14+
import kotlinx.coroutines.withContext
15+
import java.net.HttpURLConnection
16+
import java.net.URL
17+
518

619
class MainActivity : AppCompatActivity() {
720
override fun onCreate(savedInstanceState: Bundle?) {
821
super.onCreate(savedInstanceState)
922
setContentView(R.layout.activity_main)
1023
}
24+
25+
private suspend fun onStart(@IdRes id: Int) {
26+
withContext(Dispatchers.Main) {
27+
val button = findViewById<Button>(id)
28+
button.setBackgroundColor(
29+
ContextCompat.getColor(this@MainActivity, R.color.purple_500)
30+
)
31+
button.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
32+
}
33+
}
34+
35+
private suspend fun onSuccess(@IdRes id: Int) {
36+
withContext(Dispatchers.Main) {
37+
val button = findViewById<Button>(id)
38+
button.setBackgroundColor(
39+
ContextCompat.getColor(this@MainActivity, R.color.success)
40+
)
41+
val img: Drawable = ContextCompat.getDrawable(this@MainActivity,
42+
R.drawable.baseline_check_circle_24
43+
)!!
44+
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null)
45+
}
46+
}
47+
48+
private suspend fun onError(@IdRes id: Int, message: String) {
49+
withContext(Dispatchers.Main) {
50+
val button = findViewById<Button>(id)
51+
button.setBackgroundColor(
52+
ContextCompat.getColor(this@MainActivity, R.color.failure)
53+
)
54+
val img: Drawable = ContextCompat.getDrawable(this@MainActivity,
55+
R.drawable.baseline_cancel_24
56+
)!!
57+
button.setCompoundDrawablesWithIntrinsicBounds(img, null, null, null)
58+
59+
val duration = Toast.LENGTH_SHORT
60+
val toast = Toast.makeText(applicationContext, message, duration)
61+
toast.show()
62+
}
63+
}
64+
65+
fun sendUnpinned(view: View) {
66+
GlobalScope.launch(Dispatchers.IO) {
67+
onStart(R.id.unpinned)
68+
try {
69+
val mURL = URL("https://badssl.com")
70+
with(mURL.openConnection() as HttpURLConnection) {
71+
println("URL: ${this.url}")
72+
println("Response Code: ${this.responseCode}")
73+
}
74+
75+
onSuccess(R.id.unpinned)
76+
} catch (e: Throwable) {
77+
println(e)
78+
onError(R.id.unpinned, e.toString())
79+
}
80+
}
81+
}
1182
}
367 Bytes
384 Bytes
410 Bytes
631 Bytes
765 Bytes
294 Bytes

0 commit comments

Comments
 (0)