├── .gitignore ├── .idea ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── elyeproj │ │ └── networkexperiment │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── elyeproj │ │ │ └── networkexperiment │ │ │ ├── MainActivity.kt │ │ │ ├── MainComponent.kt │ │ │ ├── MainModule.kt │ │ │ ├── Model.kt │ │ │ ├── NoConnectionInterceptor.kt │ │ │ └── Repository.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── network_security_config.xml │ └── test │ └── java │ └── com │ └── elyeproj │ └── networkexperiment │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion 28 8 | defaultConfig { 9 | applicationId "com.elyeproj.networkexperiment" 10 | minSdkVersion 21 11 | targetSdkVersion 28 12 | versionCode 1 13 | versionName "1.0" 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | implementation fileTree(dir: 'libs', include: ['*.jar']) 26 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 27 | implementation 'com.android.support:appcompat-v7:28.0.0' 28 | implementation 'com.android.support.constraint:constraint-layout:1.1.3' 29 | implementation 'com.squareup.okhttp3:okhttp:3.12.0' 30 | implementation 'com.google.code.gson:gson:2.8.5' 31 | implementation "io.reactivex.rxjava2:rxjava:2.2.8" 32 | implementation "io.reactivex.rxjava2:rxandroid:2.1.1" 33 | implementation 'com.google.dagger:dagger:2.21' 34 | kapt 'com.google.dagger:dagger-compiler:2.21' 35 | testImplementation 'junit:junit:4.12' 36 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 37 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 38 | } 39 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/elyeproj/networkexperiment/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import android.support.test.InstrumentationRegistry 4 | import android.support.test.runner.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getTargetContext() 22 | assertEquals("com.elyeproj.networkexperiment", appContext.packageName) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import android.content.Context 4 | import android.os.Bundle 5 | import android.support.v7.app.AppCompatActivity 6 | import android.view.View 7 | import kotlinx.android.synthetic.main.activity_main.* 8 | import android.net.ConnectivityManager 9 | 10 | 11 | 12 | class MainActivity : AppCompatActivity() { 13 | 14 | private val repository = Repository() 15 | 16 | override fun onCreate(savedInstanceState: Bundle?) { 17 | super.onCreate(savedInstanceState) 18 | setContentView(R.layout.activity_main) 19 | 20 | val component = DaggerMainComponent.builder().mainModule(MainModule(this)).build() 21 | component.inject(repository) 22 | } 23 | 24 | fun beginSearch(view: View) { 25 | if (searchText.text.isNotBlank()) { 26 | resultText.text = "" 27 | progressIndicator.visibility = View.VISIBLE 28 | repository.performFetch(searchText.text.toString(), 29 | {showResult("Count is $it")}, 30 | {showResult(it.localizedMessage)}) 31 | } 32 | } 33 | 34 | private fun showResult(result: String) { 35 | progressIndicator.visibility = View.GONE 36 | resultText.text = result 37 | } 38 | 39 | override fun onPause() { 40 | repository.cancel() 41 | super.onPause() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/MainComponent.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import dagger.Component 4 | import javax.inject.Singleton 5 | 6 | @Singleton 7 | @Component(modules = [MainModule::class]) 8 | interface MainComponent { 9 | fun inject(repository: Repository) 10 | } 11 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/MainModule.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import android.content.Context 4 | import dagger.Module 5 | import dagger.Provides 6 | import okhttp3.OkHttpClient 7 | import javax.inject.Singleton 8 | 9 | @Module 10 | class MainModule(private val context: Context) { 11 | 12 | @Provides 13 | @Singleton 14 | fun context(): Context { 15 | return context 16 | } 17 | 18 | @Provides 19 | @Singleton 20 | fun provideHttpClient(noConnectionInterceptor: NoConnectionInterceptor) = 21 | OkHttpClient.Builder().addInterceptor(noConnectionInterceptor).build() 22 | } 23 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/Model.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | object Model { 4 | data class Result(val query: Query) 5 | data class Query(val searchinfo: SearchInfo) 6 | data class SearchInfo(val totalhits: Int) 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/NoConnectionInterceptor.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import android.content.Context 4 | import okhttp3.Interceptor 5 | import okhttp3.Response 6 | import android.net.ConnectivityManager 7 | import android.net.NetworkCapabilities 8 | import java.io.IOException 9 | import java.net.InetAddress 10 | import java.net.InetSocketAddress 11 | import java.net.Socket 12 | import java.net.UnknownHostException 13 | import javax.inject.Inject 14 | import javax.inject.Singleton 15 | 16 | @Singleton 17 | class NoConnectionInterceptor @Inject constructor(private val context: Context) : Interceptor { 18 | override fun intercept(chain: Interceptor.Chain): Response { 19 | return if (!isConnectionOn()) { 20 | throw NoConnectivityException() 21 | } else if(!isInternetAvailable()) { 22 | throw NoInternetException() 23 | } else { 24 | chain.proceed(chain.request()) 25 | } 26 | } 27 | 28 | private fun isConnectionOn(): Boolean { 29 | val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager 30 | 31 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 32 | val network = connectivityManager.activeNetwork 33 | val connection = connectivityManager.getNetworkCapabilities(network) 34 | return connection != null && ( 35 | connection.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || 36 | connection.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) 37 | } else { 38 | val activeNetwork = connectivityManager.activeNetworkInfo 39 | if (activeNetwork != null) { 40 | return (activeNetwork.type == ConnectivityManager.TYPE_WIFI || 41 | activeNetwork.type == ConnectivityManager.TYPE_MOBILE) 42 | } 43 | return false 44 | } 45 | } 46 | 47 | private fun isInternetAvailable(): Boolean { 48 | return try { 49 | val timeoutMs = 1500 50 | val sock = Socket() 51 | val sockaddr = InetSocketAddress("8.8.8.8", 53) 52 | 53 | sock.connect(sockaddr, timeoutMs) 54 | sock.close() 55 | 56 | true 57 | } catch (e: IOException) { 58 | false 59 | } 60 | 61 | } 62 | 63 | class NoConnectivityException : IOException() { 64 | override val message: String 65 | get() = "No network available, please check your WiFi or Data connection" 66 | } 67 | 68 | class NoInternetException() : IOException() { 69 | override val message: String 70 | get() = "No internet available, please check your connected WIFi or Data" 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /app/src/main/java/com/elyeproj/networkexperiment/Repository.kt: -------------------------------------------------------------------------------- 1 | package com.elyeproj.networkexperiment 2 | 3 | import android.view.View 4 | import com.google.gson.Gson 5 | import com.google.gson.JsonSyntaxException 6 | import io.reactivex.Single 7 | import io.reactivex.android.schedulers.AndroidSchedulers 8 | import io.reactivex.disposables.Disposables 9 | import io.reactivex.schedulers.Schedulers 10 | import okhttp3.HttpUrl 11 | import okhttp3.OkHttpClient 12 | import okhttp3.Request 13 | import javax.inject.Inject 14 | 15 | class Repository { 16 | 17 | @Inject 18 | lateinit var httpClient: OkHttpClient 19 | 20 | private var disposable = Disposables.disposed() 21 | 22 | private val httpUrlBuilder = HttpUrl.Builder() 23 | .scheme("https") 24 | .host("en.wikipedia.org") 25 | .addPathSegment("w") 26 | .addPathSegment("api.php") 27 | .addQueryParameter("action", "query") 28 | .addQueryParameter("format", "json") 29 | .addQueryParameter("list", "search") 30 | 31 | 32 | fun performFetch(searchText: String, onSuccess: (String) -> Unit, onError: (Throwable) -> Unit) { 33 | disposable.dispose() 34 | disposable = Single.just(searchText) 35 | .map{fetchOnBackground(it)} 36 | .subscribeOn(Schedulers.io()) 37 | .observeOn(AndroidSchedulers.mainThread()) 38 | .subscribe( 39 | { onSuccess(it) }, 40 | { onError(it) }) 41 | } 42 | 43 | private fun fetchOnBackground(searchText: String): String { 44 | val httpUrl = httpUrlBuilder.addQueryParameter("srsearch", searchText) 45 | .build() 46 | val request = Request.Builder().get().url(httpUrl).build() 47 | val response = httpClient.newCall(request).execute() 48 | 49 | return if (response.isSuccessful) { 50 | val raw = response.body()?.string() 51 | try { 52 | val result = Gson().fromJson(raw, Model.Result::class.java) 53 | result.query.searchinfo.totalhits.toString() 54 | } catch (exception: JsonSyntaxException) { 55 | "No data found " 56 | } 57 | } else { 58 | response.message() 59 | } 60 | } 61 | 62 | fun cancel() { 63 | disposable.dispose() 64 | } 65 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 20 | 21 |