From 1d05d522db242c760dd9f8091c73e80b871ed585 Mon Sep 17 00:00:00 2001 From: Aravind Chowdary Date: Thu, 30 Sep 2021 14:32:17 +0530 Subject: [PATCH 1/2] kotlin rewrite iteration 1 --- app/build.gradle | 6 + build.gradle | 2 + lib/build.gradle | 9 ++ .../java/me/aravi/libraries/iplib/IPLib.java | 105 ----------------- .../java/me/aravi/libraries/iplib/IPLib.kt | 98 ++++++++++++++++ .../aravi/libraries/iplib/api/Endpoints.java | 15 --- .../me/aravi/libraries/iplib/api/Endpoints.kt | 14 +++ .../libraries/iplib/api/GeoLocatorAPI.java | 45 ------- .../libraries/iplib/api/GeoLocatorAPI.kt | 46 ++++++++ .../iplib/listeners/OnIPResponse.java | 10 -- .../libraries/iplib/listeners/OnIPResponse.kt | 8 ++ .../aravi/libraries/iplib/model/IPInfo.java | 111 ------------------ .../me/aravi/libraries/iplib/model/IPInfo.kt | 50 ++++++++ 13 files changed, 233 insertions(+), 286 deletions(-) delete mode 100644 lib/src/main/java/me/aravi/libraries/iplib/IPLib.java create mode 100644 lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt delete mode 100644 lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.java create mode 100644 lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.kt delete mode 100644 lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.java create mode 100644 lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.kt delete mode 100644 lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.java create mode 100644 lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.kt delete mode 100644 lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.java create mode 100644 lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.kt diff --git a/app/build.gradle b/app/build.gradle index e5b5d86..641d466 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.application' } +apply plugin: 'kotlin-android' android { compileSdk 30 @@ -41,4 +42,9 @@ dependencies { testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + implementation "androidx.core:core-ktx:+" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" +} +repositories { + mavenCentral() } \ No newline at end of file diff --git a/build.gradle b/build.gradle index 1aab959..1666236 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { + ext.kotlin_version = '1.6.0-M1' repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:4.2.2" + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/lib/build.gradle b/lib/build.gradle index 238680c..86a131d 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -2,6 +2,7 @@ plugins { id 'com.android.library' id 'maven-publish' } +apply plugin: 'kotlin-android' task androidSourcesJar(type: Jar) { @@ -50,7 +51,15 @@ dependencies { implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' + implementation "androidx.core:core-ktx:1.6.0" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1' + testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + +} +repositories { + mavenCentral() } \ No newline at end of file diff --git a/lib/src/main/java/me/aravi/libraries/iplib/IPLib.java b/lib/src/main/java/me/aravi/libraries/iplib/IPLib.java deleted file mode 100644 index b054acf..0000000 --- a/lib/src/main/java/me/aravi/libraries/iplib/IPLib.java +++ /dev/null @@ -1,105 +0,0 @@ -package me.aravi.libraries.iplib; - -import android.os.AsyncTask; - -import me.aravi.libraries.iplib.api.GeoLocatorAPI; -import me.aravi.libraries.iplib.listeners.OnIPResponse; -import me.aravi.libraries.iplib.model.IPInfo; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; - -public class IPLib { - private static IPLib instance; - - /** - * Creates a new instance of iplib - * - * @return - */ - public static IPLib getInstance() { - if (instance == null) { - instance = new IPLib(); - } - return instance; - } - - private IPLib() { - } - - /** - * GeoLocator api call happens here - * NOTE : THIS RUNS ON MAIN THREAD - * - * @param listener - */ - public void ipLookup(OnIPResponse listener) { - Call lookup = GeoLocatorAPI.getInstance().getService().lookup(); - lookup.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - listener.onSuccess(response.body()); - } else { - listener.onError(response.message(), response.code()); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - listener.onError(t.getMessage(), 1024); - } - }); - } - - - /** - * Ip Lookup with Ip Address - * - * @param ipAddress - * @param listener - */ - public void ipLookupWith(String ipAddress, OnIPResponse listener) { - Call lookup = GeoLocatorAPI.getInstance().getService().lookupFor(ipAddress); - lookup.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - if (response.isSuccessful()) { - listener.onSuccess(response.body()); - } else { - listener.onError(response.message(), response.code()); - } - } - - @Override - public void onFailure(Call call, Throwable t) { - listener.onError(t.getMessage(), 1024); - } - }); - } - - - /** - * This is same as the other method - * this happens in async thread - * - * @param listener - */ - public void ipLookupAsync(OnIPResponse listener) { - AsyncTask.execute(() -> { - ipLookup(listener); - }); - } - - /** - * @param ipAddress - * @param listener - */ - public void ipLookupWithIpAsync(String ipAddress, OnIPResponse listener) { - AsyncTask.execute(() -> { - ipLookupWith(ipAddress, listener); - }); - } - - -} diff --git a/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt b/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt new file mode 100644 index 0000000..65c5a65 --- /dev/null +++ b/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt @@ -0,0 +1,98 @@ +package me.aravi.libraries.iplib + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import me.aravi.libraries.iplib.api.GeoLocatorAPI +import me.aravi.libraries.iplib.listeners.OnIPResponse +import me.aravi.libraries.iplib.model.IPInfo +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +class IPLib private constructor() { + /** + * GeoLocator api call happens here + * NOTE : THIS RUNS ON MAIN THREAD + * + * @param listener + */ + fun ipLookup(listener: OnIPResponse) { + val lookup: Call = GeoLocatorAPI.apiService!!.lookup() + lookup.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + listener.onSuccess(response.body()) + } else { + listener.onError(response.message(), response.code()) + } + } + + override fun onFailure(call: Call, t: Throwable) { + listener.onError(t.message, 1024) + } + }) + } + + /** + * Ip Lookup with Ip Address + * + * @param ipAddress + * @param listener + */ + fun ipLookupWith(ipAddress: String?, listener: OnIPResponse) { + val lookup: Call = GeoLocatorAPI.apiService!!.lookupFor(ipAddress) + lookup.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if (response.isSuccessful) { + listener.onSuccess(response.body()) + } else { + listener.onError(response.message(), response.code()) + } + } + + override fun onFailure(call: Call, t: Throwable) { + listener.onError(t.message, 1024) + } + }) + } + + /** + * This is same as the other method + * this happens in async thread + * + * @param listener + */ + fun ipLookupAsync(listener: OnIPResponse) { + CoroutineScope(Dispatchers.IO).launch { + ipLookup(listener) + } + } + + /** + * @param ipAddress + * @param listener + */ + fun ipLookupWithIpAsync(ipAddress: String?, listener: OnIPResponse) { + CoroutineScope(Dispatchers.IO).launch { + ipLookupWith(ipAddress, listener) + } + } + + companion object { + /** + * Creates a new instance of iplib + * + * @return + */ + @JvmStatic + var instance: IPLib? = null + get() { + if (field == null) { + field = IPLib() + } + return field + } + private set + } +} \ No newline at end of file diff --git a/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.java b/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.java deleted file mode 100644 index d6367f4..0000000 --- a/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.aravi.libraries.iplib.api; - -import me.aravi.libraries.iplib.model.IPInfo; -import retrofit2.Call; -import retrofit2.http.GET; -import retrofit2.http.Path; - - -public interface Endpoints { - @GET("json/") - Call lookup(); - - @GET("json/{ip}") - Call lookupFor(@Path("ip") String ip); -} diff --git a/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.kt b/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.kt new file mode 100644 index 0000000..6ec04bb --- /dev/null +++ b/lib/src/main/java/me/aravi/libraries/iplib/api/Endpoints.kt @@ -0,0 +1,14 @@ +package me.aravi.libraries.iplib.api + +import me.aravi.libraries.iplib.model.IPInfo +import retrofit2.Call +import retrofit2.http.GET +import retrofit2.http.Path + +interface Endpoints { + @GET("json/") + fun lookup(): Call + + @GET("json/{ip}") + fun lookupFor(@Path("ip") ip: String?): Call +} \ No newline at end of file diff --git a/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.java b/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.java deleted file mode 100644 index a8c91f6..0000000 --- a/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.aravi.libraries.iplib.api; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - -import retrofit2.Retrofit; -import retrofit2.converter.gson.GsonConverterFactory; - -public class GeoLocatorAPI { - private static final String TAG = GeoLocatorAPI.class.getSimpleName(); - public static final String url = "https://geolocation-db.com/"; - public static Endpoints apiService = null; - private static GeoLocatorAPI instance; - - - public static GeoLocatorAPI getInstance() { - if (instance == null) { - instance = new GeoLocatorAPI(); - } - instance.getService(); - return instance; - } - - private GeoLocatorAPI() { - } - - public Endpoints getService() { - if (apiService == null) { - Gson gson = new GsonBuilder() - .setLenient() - .create(); - - Retrofit retrofit = new Retrofit.Builder() - .baseUrl(url) - .addConverterFactory(GsonConverterFactory.create(gson)) - .build(); - - apiService = retrofit.create(Endpoints.class); - - } - return apiService; - } - - -} diff --git a/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.kt b/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.kt new file mode 100644 index 0000000..52b4f69 --- /dev/null +++ b/lib/src/main/java/me/aravi/libraries/iplib/api/GeoLocatorAPI.kt @@ -0,0 +1,46 @@ +package me.aravi.libraries.iplib.api + +import retrofit2.http.GET +import me.aravi.libraries.iplib.model.IPInfo +import me.aravi.libraries.iplib.api.Endpoints +import me.aravi.libraries.iplib.api.GeoLocatorAPI +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import androidx.annotation.Keep +import me.aravi.libraries.iplib.listeners.OnIPResponse +import android.os.AsyncTask +import me.aravi.libraries.iplib.IPLib + +class GeoLocatorAPI private constructor() { + val service: Endpoints? + get() { + if (apiService == null) { + val gson = GsonBuilder() + .setLenient() + .create() + val retrofit = Retrofit.Builder() + .baseUrl(url) + .addConverterFactory(GsonConverterFactory.create(gson)) + .build() + apiService = retrofit.create(Endpoints::class.java) + } + return apiService + } + + companion object { + private val TAG = GeoLocatorAPI::class.java.simpleName + const val url = "https://geolocation-db.com/" + var apiService: Endpoints? = null + var instance: GeoLocatorAPI? = null + get() { + if (field == null) { + field = GeoLocatorAPI() + } + field!!.service + return field + } + private set + } +} \ No newline at end of file diff --git a/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.java b/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.java deleted file mode 100644 index 0edb679..0000000 --- a/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.java +++ /dev/null @@ -1,10 +0,0 @@ -package me.aravi.libraries.iplib.listeners; - -import me.aravi.libraries.iplib.model.IPInfo; - -public interface OnIPResponse { - - void onSuccess(IPInfo ipInfo); - - void onError(String message, int errorCode); -} diff --git a/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.kt b/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.kt new file mode 100644 index 0000000..00849d4 --- /dev/null +++ b/lib/src/main/java/me/aravi/libraries/iplib/listeners/OnIPResponse.kt @@ -0,0 +1,8 @@ +package me.aravi.libraries.iplib.listeners + +import me.aravi.libraries.iplib.model.IPInfo + +interface OnIPResponse { + fun onSuccess(ipInfo: IPInfo?) + fun onError(message: String?, errorCode: Int) +} \ No newline at end of file diff --git a/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.java b/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.java deleted file mode 100644 index 3a69e92..0000000 --- a/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.java +++ /dev/null @@ -1,111 +0,0 @@ -package me.aravi.libraries.iplib.model; - -import androidx.annotation.Keep; -import androidx.annotation.Nullable; - -@Keep -public class IPInfo { - @Nullable - private String IPv4; - @Nullable - private String country_code; - @Nullable - private String country_name; - @Nullable - private String city; - @Nullable - private String postal; - @Nullable - private String latitude; - @Nullable - private String longitude; - @Nullable - private String state; - - public IPInfo() { - // public constructor - } - - public IPInfo(@Nullable String IPv4, @Nullable String country_code, @Nullable String country_name, @Nullable String city, @Nullable String postal, @Nullable String latitude, @Nullable String longitude, @Nullable String state) { - this.IPv4 = IPv4; - this.country_code = country_code; - this.country_name = country_name; - this.city = city; - this.postal = postal; - this.latitude = latitude; - this.longitude = longitude; - this.state = state; - } - - @Nullable - public String getIPv4() { - return IPv4; - } - - public void setIPv4(@Nullable String IPv4) { - this.IPv4 = IPv4; - } - - @Nullable - public String getCountry_code() { - return country_code; - } - - public void setCountry_code(@Nullable String country_code) { - this.country_code = country_code; - } - - @Nullable - public String getCountry_name() { - return country_name; - } - - public void setCountry_name(@Nullable String country_name) { - this.country_name = country_name; - } - - @Nullable - public String getCity() { - return city; - } - - public void setCity(@Nullable String city) { - this.city = city; - } - - @Nullable - public String getPostal() { - return postal; - } - - public void setPostal(@Nullable String postal) { - this.postal = postal; - } - - @Nullable - public String getLatitude() { - return latitude; - } - - public void setLatitude(@Nullable String latitude) { - this.latitude = latitude; - } - - @Nullable - public String getLongitude() { - return longitude; - } - - public void setLongitude(@Nullable String longitude) { - this.longitude = longitude; - } - - @Nullable - public String getState() { - return state; - } - - public void setState(@Nullable String state) { - this.state = state; - } -} diff --git a/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.kt b/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.kt new file mode 100644 index 0000000..d2bc8d4 --- /dev/null +++ b/lib/src/main/java/me/aravi/libraries/iplib/model/IPInfo.kt @@ -0,0 +1,50 @@ +package me.aravi.libraries.iplib.model + +import retrofit2.http.GET +import me.aravi.libraries.iplib.model.IPInfo +import me.aravi.libraries.iplib.api.Endpoints +import me.aravi.libraries.iplib.api.GeoLocatorAPI +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import androidx.annotation.Keep +import me.aravi.libraries.iplib.listeners.OnIPResponse +import android.os.AsyncTask +import me.aravi.libraries.iplib.IPLib + +@Keep +class IPInfo { + var iPv4: String? = null + var country_code: String? = null + var country_name: String? = null + var city: String? = null + var postal: String? = null + var latitude: String? = null + var longitude: String? = null + var state: String? = null + + constructor() { + // public constructor + } + + constructor( + IPv4: String?, + country_code: String?, + country_name: String?, + city: String?, + postal: String?, + latitude: String?, + longitude: String?, + state: String? + ) { + iPv4 = IPv4 + this.country_code = country_code + this.country_name = country_name + this.city = city + this.postal = postal + this.latitude = latitude + this.longitude = longitude + this.state = state + } +} \ No newline at end of file From c12eb6972068f50ed8c9d43ed6b1410a4de66ca5 Mon Sep 17 00:00:00 2001 From: Aravind Chowdary Date: Thu, 30 Sep 2021 14:35:35 +0530 Subject: [PATCH 2/2] java doc updated --- lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt b/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt index 65c5a65..c277e5b 100644 --- a/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt +++ b/lib/src/main/java/me/aravi/libraries/iplib/IPLib.kt @@ -59,7 +59,7 @@ class IPLib private constructor() { /** * This is same as the other method - * this happens in async thread + * this happens in IO CoroutineScope * * @param listener */ @@ -70,6 +70,8 @@ class IPLib private constructor() { } /** + * Gets ip info + * this happens in IO CoroutineScope * @param ipAddress * @param listener */