Skip to content

Commit

Permalink
add network loggin adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alireza Tizfahm Fard committed Dec 30, 2019
1 parent bafdd31 commit 508daca
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 47 deletions.
24 changes: 24 additions & 0 deletions app/src/main/java/alirezat775/app/networkmonitor/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package alirezat775.app.networkmonitor

import alirezat775.library.networkmonitor.NetworkMonitor
import alirezat775.library.networkmonitor.core.NetworkMonitorInterceptor
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import okhttp3.*
import java.io.IOException

class MainActivity : AppCompatActivity() {

Expand All @@ -12,6 +15,27 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
networkMonitor.register()

okHttp()
}

private fun okHttp() {
val client = OkHttpClient.Builder()
.addInterceptor(NetworkMonitorInterceptor())
.build()

val request: Request = Request.Builder()
.url("https://www.google.com")
.build()

client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {

}

override fun onResponse(call: Call, response: Response) {
}
})
}

override fun onDestroy() {
Expand Down
1 change: 1 addition & 0 deletions networkmonitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
api 'com.squareup:seismic:1.0.2'
api 'com.squareup.okhttp3:okhttp:3.12.3'
testImplementation 'junit:junit:4.12'
Expand Down
4 changes: 3 additions & 1 deletion networkmonitor/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alirezat775.library.networkmonitor">

<uses-permission android:name="android.permission.INTERNET" />

<application>
<activity android:name=".NetworkMonitorActivity" />
<activity android:name=".view.NetworkMonitorActivity" />
</application>

</manifest>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alirezat775.library.networkmonitor

import alirezat775.library.networkmonitor.view.NetworkMonitorActivity
import android.content.Context
import android.content.Context.SENSOR_SERVICE
import android.content.Intent
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package alirezat775.library.networkmonitor.core

/**
* Author: Alireza Tizfahm Fard
* Date: 2019-12-30
*/

object NetworkLogging {

internal val list = mutableListOf<NetworkModel>()

fun clear() {
list.clear()
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package alirezat775.library.networkmonitor
package alirezat775.library.networkmonitor.core

import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.RequestBody
import okhttp3.ResponseBody

/**
* Author: Alireza Tizfahm Fard
* Date: 2019-12-30
*/

data class NetworkModel(
val request: RequestNetworkModel,
val response: ResponseNetworkModel
)

data class RequestNetworkModel(
val url: HttpUrl,
val method: String,
Expand All @@ -21,5 +25,5 @@ data class ResponseNetworkModel(
val code: Int,
val message: String,
val headers: Headers,
val body: ResponseBody?
val body: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package alirezat775.library.networkmonitor.core

import okhttp3.Interceptor
import okhttp3.Response

/**
* Author: Alireza Tizfahm Fard
* Date: 2019-12-29
* Email: [email protected]
*/

class NetworkMonitorInterceptor : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val rq = chain.request()
val rqModel =
RequestNetworkModel(
rq.url(),
rq.method(),
rq.headers(),
rq.body()
)
val rs = chain.proceed(rq)
val rsModel =
ResponseNetworkModel(
rs.code(),
rs.message(),
rs.headers(),
rs.body()?.string()
)
NetworkLogging.list.add(NetworkModel(rqModel, rsModel))
return rs
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package alirezat775.library.networkmonitor.view

import alirezat775.library.networkmonitor.R
import alirezat775.library.networkmonitor.core.NetworkModel
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.log_item.view.*

/**
* Author: Alireza Tizfahm Fard
* Date: 2019-12-30
*/

class NetworkLoggingAdapter : RecyclerView.Adapter<NetworkLoggingAdapter.ViewHolder>() {

private var list = mutableListOf<NetworkModel>()

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val url = itemView.network_url_log
}

fun addItems(list: MutableList<NetworkModel>) {
this.list = list
}

fun addItem(networkModel: NetworkModel) {
this.list.add(networkModel)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.log_item,
parent,
false
)
)
}

override fun getItemCount(): Int {
return list.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.url.text = list[position].request.url.encodedPath()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package alirezat775.library.networkmonitor
package alirezat775.library.networkmonitor.view

import alirezat775.library.networkmonitor.R
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

Expand Down
14 changes: 14 additions & 0 deletions networkmonitor/src/main/res/layout/log_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/network_url_log"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textSize="20sp" />

</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TEST"
android:textSize="30sp" />
android:layout_height="match_parent" />

</LinearLayout>

0 comments on commit 508daca

Please sign in to comment.