Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Converted classes to Kotlin #2405

Merged
merged 2 commits into from
Apr 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

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
}

}
}

This file was deleted.

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object Utils {

@JvmStatic
val isBaseUrlEmpty: Boolean
get() = Urls.getBaseUrl() == Urls.EMPTY_LINK
get() = Urls.baseUrl == Urls.EMPTY_LINK

@JvmStatic
fun isEmpty(string: String?): Boolean {
Expand Down Expand Up @@ -85,15 +85,15 @@ object Utils {
if (isBaseUrlEmpty) {
swipeRefreshLayout.isEnabled = false
} else {
StrategyRegistry.instance.eventBusStrategy!!.eventBus.register(`object`)
StrategyRegistry.instance.eventBusStrategy?.eventBus?.register(`object`)
swipeRefreshLayout.setOnRefreshListener(onRefreshListener)
}
}

@JvmStatic
fun unregisterIfUrlValid(`object`: Any) {
if (!isBaseUrlEmpty) {
StrategyRegistry.instance.eventBusStrategy!!.eventBus.unregister(`object`)
StrategyRegistry.instance.eventBusStrategy?.eventBus?.unregister(`object`)
}
}

Expand Down
Loading