Skip to content

Instantly share code, notes, and snippets.

View Audhil's full-sized avatar
🎯
Focusing

Mohammed Audhil Audhil

🎯
Focusing
View GitHub Profile
@Audhil
Audhil / LifecycleCoroutines.kt
Created October 16, 2018 09:44 — forked from LouisCAD/LifecycleCoroutines.kt
CoroutineScope and Job integration with Lifecycle for Android. Meant to be used for your coroutines in lifecycle aware components. Ongoing PR: https://github.com/Kotlin/kotlinx.coroutines/pull/655
import android.arch.lifecycle.GenericLifecycleObserver
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.Lifecycle.Event.ON_DESTROY
import android.arch.lifecycle.LifecycleOwner
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Dispatchers
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.Main
fun Lifecycle.createJob(cancelEvent: Lifecycle.Event = ON_DESTROY): Job {
@Audhil
Audhil / CustomSSLFactory.kt
Created October 22, 2018 14:08
TLS v1.2 support in all Pre-lollipop devices
// file 2
import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.net.UnknownHostException
import java.security.KeyManagementException
import java.security.NoSuchAlgorithmException
import javax.net.ssl.SSLContext
@Audhil
Audhil / proguard.pro
Created April 23, 2019 09:49
ProGuard rules to keep Room DB entities
# Keep all Room Entities.
-keep @androidx.room.Entity class * {*;}
-keepclasseswithmembers class * {
@androidx.room.Entity <methods>;
}
-keepclasseswithmembers class * {
@androidx.room.Entity <fields>;
}
@Audhil
Audhil / build.gradle
Created April 23, 2019 11:52
clean task -> which prevents ant.props, build.xml, and library.xml from getting deleted on "Clean Project" -> add this in "project_dir/build.gradle"
task clean(type: Delete) {
delete fileTree(rootProject.buildDir) {
exclude 'ant.properties', 'build.xml', 'library.xml'
}
}
@Audhil
Audhil / stringIs.java
Created June 22, 2019 14:35
String is immutable - practical check-up
String t = "foo"; // newer obj created and added string pool
System.out.println("hashCode of t: " + t.hashCode());
t = "foo"; // shares the same memory location - same hashCode()
System.out.println("hashCode of 2 t: " + t.hashCode());
t = "fuc"; // newer value added - newer obj created and added string pool - different hashCode()
System.out.println("hashCode of 3 t: " + t.hashCode());
t = new String("foo"); // shares the same memory location - same hashCode()
open class GitHubDelegate : Application() {
open lateinit var appDaggerComponent: GitHubAppComponent
override fun onCreate() {
super.onCreate()
appDaggerComponent = getAppComponent()
appDaggerComponent.inject(this)
}
@Module
class TestApplicationModule(application: GitHubDelegate) : ApplicationModule(application) {
@Singleton
@Provides
fun giveRetrofit(okHttpClient: OkHttpClient): Retrofit =
Retrofit.Builder()
.baseUrl("http://localhost:8080/")
.addConverterFactory(GsonConverterFactory.create(GsonBuilder().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
open class UiGitHubDelegate : GitHubDelegate() {
override fun getAppComponent(): GitHubAppComponent {
return DaggerGitHubAppComponent.builder()
.applicationModule(TestApplicationModule(this))
.build()
}
}
open class UiRunner : AndroidJUnitRunner() {
override fun onCreate(arguments: Bundle?) {
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder().permitAll().build())
super.onCreate(arguments)
}
override fun newApplication(cl: ClassLoader?, className: String?, context: Context?): Application {
return super.newApplication(cl, UiGitHubDelegate::class.java.name, context)
}
class MockServerDispatcher {
// response dispatcher
class ResponseDispatcher : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
if (request.path.equals("/todos/1", true))
return MockResponse().setResponseCode(200).setBody(
"{\n" +
" \"id\" : 1,\n" +
" \"title\" : \"delectus aut autem\",\n" +