This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 828
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Converted classes to Kotlin (#2405)
* converted classes to kotlin * fixed code according to codestyle
- Loading branch information
1 parent
36e1ae0
commit 2d259b7
Showing
20 changed files
with
1,159 additions
and
1,198 deletions.
There are no files selected for viewing
95 changes: 0 additions & 95 deletions
95
android/app/src/main/java/org/fossasia/openevent/common/api/JWTUtils.java
This file was deleted.
Oops, something went wrong.
101 changes: 101 additions & 0 deletions
101
android/app/src/main/java/org/fossasia/openevent/common/api/JWTUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.fossasia.openevent.common.api | ||
|
||
import android.support.v4.util.SparseArrayCompat | ||
|
||
import org.json.JSONException | ||
import org.json.JSONObject | ||
|
||
object JWTUtils { | ||
|
||
private fun decode(token: String): SparseArrayCompat<String> { | ||
val decoded = SparseArrayCompat<String>(2) | ||
|
||
val split = token.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
decoded.append(0, getJson(split[0])) | ||
decoded.append(1, getJson(split[1])) | ||
|
||
return decoded | ||
} | ||
|
||
@Throws(JSONException::class) | ||
private fun getExpiry(token: String): Long { | ||
val decoded = decode(token) | ||
|
||
// We are using JSONObject instead of GSON as it takes about 5 ms instead of 150 ms taken by GSON | ||
return JSONObject(decoded.get(1)).get("exp").toString().toLong() | ||
} | ||
|
||
@Throws(JSONException::class) | ||
@JvmStatic | ||
fun getIdentity(token: String): Int { | ||
val decoded = decode(token) | ||
|
||
return JSONObject(decoded.get(1)).get("identity").toString().toInt() | ||
} | ||
|
||
@JvmStatic | ||
fun isExpired(token: String): Boolean { | ||
val expiry: Long | ||
|
||
try { | ||
expiry = getExpiry(token) | ||
} catch (jse: JSONException) { | ||
return true | ||
} | ||
|
||
return System.currentTimeMillis() / 1000 >= expiry | ||
} | ||
|
||
private fun getJson(strEncoded: String): String { | ||
val decodedBytes = Base64Utils.decode(strEncoded) | ||
return String(decodedBytes) | ||
} | ||
|
||
/** | ||
* Base64 class because we can't test Android class and this is faster | ||
*/ | ||
private object Base64Utils { | ||
|
||
private val ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray() | ||
|
||
private val toInt = IntArray(128) | ||
|
||
init { | ||
for (i in ALPHABET.indices) { | ||
toInt[ALPHABET[i].toInt()] = i | ||
} | ||
} | ||
|
||
/** | ||
* Translates the specified Base64 string into a byte array. | ||
* | ||
* @param s the Base64 string (not null) | ||
* @return the byte array (not null) | ||
*/ | ||
fun decode(s: String): ByteArray { | ||
val delta = if (s.endsWith("==")) 2 else if (s.endsWith("=")) 1 else 0 | ||
val buffer = ByteArray(s.length * 3 / 4 - delta) | ||
val mask = 0xFF | ||
var index = 0 | ||
var i = 0 | ||
while (i < s.length) { | ||
val c0 = toInt[s[i].toInt()] | ||
val c1 = toInt[s[i + 1].toInt()] | ||
buffer[index++] = (c0 shl 2 or (c1 shr 4) and mask).toByte() | ||
if (index >= buffer.size) { | ||
return buffer | ||
} | ||
val c2 = toInt[s[i + 2].toInt()] | ||
buffer[index++] = (c1 shl 4 or (c2 shr 2) and mask).toByte() | ||
if (index >= buffer.size) { | ||
return buffer | ||
} | ||
val c3 = toInt[s[i + 3].toInt()] | ||
buffer[index++] = (c2 shl 6 or c3 and mask).toByte() | ||
i += 4 | ||
} | ||
return buffer | ||
} | ||
|
||
} | ||
} |
85 changes: 0 additions & 85 deletions
85
android/app/src/main/java/org/fossasia/openevent/common/api/Urls.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
android/app/src/main/java/org/fossasia/openevent/common/api/Urls.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.fossasia.openevent.common.api | ||
|
||
import android.webkit.URLUtil | ||
|
||
import org.fossasia.openevent.common.utils.Utils | ||
|
||
object Urls { | ||
|
||
const val API_VERSION = "v1" | ||
|
||
/** | ||
* Change EVENT Id Here * | ||
*/ | ||
const val EVENT_ID = 1 | ||
|
||
const val WEB_APP_URL_BASIC = "http://fossasia.github.io/open-event-webapp/#/" | ||
|
||
const val EVENT = "event" | ||
|
||
const val SPEAKERS = "speakers" | ||
|
||
const val TRACKS = "tracks" | ||
|
||
const val SESSIONS = "sessions" | ||
|
||
const val SPONSORS = "sponsors" | ||
|
||
const val BOOKMARKS = "bookmarks" | ||
|
||
const val MICROLOCATIONS = "microlocations" | ||
|
||
const val SESSION_TYPES = "session_types" | ||
|
||
const val MAP = "map" | ||
|
||
@JvmField | ||
var BASE_URL = "https://eventyay.com/api/v1/events/6" | ||
|
||
const val FACEBOOK_BASE_URL = "https://graph.facebook.com" | ||
|
||
const val LOKLAK_BASE_URL = "https://api.loklak.org" | ||
|
||
@JvmField | ||
val BASE_GET_URL = "$BASE_URL/api/$API_VERSION" | ||
|
||
const val BASE_GET_URL_ALT = "https://raw.githubusercontent.com/fossasia/open-event/master/testapi/" | ||
|
||
// Replace the template in getAppLink() if changing it | ||
private const val APP_LINK = "https://app_link_goes_here.com" | ||
|
||
const val INVALID_LINK = "http://abc//" | ||
|
||
const val EMPTY_LINK = "http://xyz//" | ||
|
||
private const val GOOGLE_PLAY_HOME = "https://play.google.com/store" | ||
|
||
var baseUrl: String | ||
@JvmStatic | ||
get() = BASE_URL | ||
@JvmStatic | ||
set(baseUrl) = if (URLUtil.isValidUrl(baseUrl)) { | ||
BASE_URL = if (!baseUrl.endsWith("/")) { | ||
"$baseUrl/" | ||
} else { | ||
baseUrl | ||
} | ||
} else { | ||
BASE_URL = if (!Utils.isEmpty(baseUrl)) { | ||
INVALID_LINK | ||
} else { | ||
EMPTY_LINK | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the app link is replaced by the generator, if not | ||
* returns null which is to be checked by caller to make decision | ||
* accordingly. | ||
* @return String | ||
*/ | ||
val appLink: String | ||
@JvmStatic | ||
get() = if (APP_LINK == "https://app_link_goes_here.com") GOOGLE_PLAY_HOME else APP_LINK | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.