Skip to content

Commit

Permalink
feat: setup android checks on CI (#14)
Browse files Browse the repository at this point in the history
* feat: setup android checks on CI

* feat: upgrade ktlint to 1.3.1 version
  • Loading branch information
IvanIhnatsiuk authored Sep 24, 2024
1 parent bc3e242 commit c3f462c
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 177 deletions.
56 changes: 0 additions & 56 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,62 +50,6 @@ jobs:
- name: Build package
run: yarn prepare

build-android:
runs-on: ubuntu-latest
env:
TURBO_CACHE_DIR: .turbo/android
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Cache turborepo for Android
uses: actions/cache@v4
with:
path: ${{ env.TURBO_CACHE_DIR }}
key: ${{ runner.os }}-turborepo-android-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-turborepo-android-
- name: Check turborepo cache for Android
run: |
TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status")
if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
echo "turbo_cache_hit=1" >> $GITHUB_ENV
fi
- name: Install JDK
if: env.turbo_cache_hit != 1
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'

- name: Finalize Android SDK
if: env.turbo_cache_hit != 1
run: |
/bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null"
- name: Cache Gradle
if: env.turbo_cache_hit != 1
uses: actions/cache@v4
with:
path: |
~/.gradle/wrapper
~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('example/android/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build example for Android
env:
JAVA_OPTS: "-XX:MaxHeapSize=6g"
run: |
yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}"
build-ios:
runs-on: macos-14
env:
Expand Down
82 changes: 82 additions & 0 deletions .github/workflows/verify-android.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: 📱 Validate Android

on:
push:
branches:
- main
paths:
- ".github/workflows/verify-android.yml"
- "android/**"
pull_request:
branches:
- main
paths:
- ".github/workflows/verify-android.yml"
- "android/**"

jobs:
ktlint:
runs-on: ubuntu-latest
name: 🔎 Kotlin Lint
steps:
- uses: actions/checkout@v4
- uses: touchlab-lab/ktlint-action-setup@1.0.0
with:
ktlint_version: 1.3.1
- run: ktlint "android/src/**/*.kt"
build-android:
runs-on: ubuntu-latest
name: 🚀 Build Android
env:
TURBO_CACHE_DIR: .turbo/android
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup
uses: ./.github/actions/setup

- name: Cache turborepo for Android
uses: actions/cache@v4
with:
path: ${{ env.TURBO_CACHE_DIR }}
key: ${{ runner.os }}-turborepo-android-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-turborepo-android-
- name: Check turborepo cache for Android
run: |
TURBO_CACHE_STATUS=$(node -p "($(yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}" --dry=json)).tasks.find(t => t.task === 'build:android').cache.status")
if [[ $TURBO_CACHE_STATUS == "HIT" ]]; then
echo "turbo_cache_hit=1" >> $GITHUB_ENV
fi
- name: Install JDK
if: env.turbo_cache_hit != 1
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'

- name: Finalize Android SDK
if: env.turbo_cache_hit != 1
run: |
/bin/bash -c "yes | $ANDROID_HOME/cmdline-tools/latest/bin/sdkmanager --licenses > /dev/null"
- name: Cache Gradle
if: env.turbo_cache_hit != 1
uses: actions/cache@v4
with:
path: |
~/.gradle/wrapper
~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('example/android/gradle/wrapper/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Build example for Android
env:
JAVA_OPTS: "-XX:MaxHeapSize=6g"
run: |
yarn turbo run build:android --cache-dir="${{ env.TURBO_CACHE_DIR }}"
180 changes: 95 additions & 85 deletions android/src/main/java/com/fastsecurestorage/FastSecureStorageBridge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,115 +14,125 @@ import java.io.IOException
import java.security.GeneralSecurityException

@OptIn(FrameworkAPI::class)
class FastSecureStorageBridge(private val context: ReactApplicationContext) {
private var sharedPreferences: SharedPreferences? = null

private external fun installNativeJsi(jsContextNativePointer: Long,
jsCallInvokerHolder: CallInvokerHolderImpl)

init {
try {
this.sharedPreferences = this.secureSharedPreferences
} catch (e: Exception) {
e.printStackTrace()
}
class FastSecureStorageBridge(
private val context: ReactApplicationContext,
) {
private var sharedPreferences: SharedPreferences? = null

private external fun installNativeJsi(
jsContextNativePointer: Long,
jsCallInvokerHolder: CallInvokerHolderImpl,
)

init {
try {
this.sharedPreferences = this.secureSharedPreferences
} catch (e: Exception) {
e.printStackTrace()
}

fun setItem(alias: String, value: String): Boolean {
try {
sharedPreferences!!.edit().putString(alias, value).apply()
return true;
} catch (e: Exception) {
e.printStackTrace()
return false;
}
}

fun setItem(
alias: String,
value: String,
): Boolean {
try {
sharedPreferences?.edit()?.putString(alias, value)?.apply()
return true
} catch (e: Exception) {
return false
}
}

fun getAllKeys(): String {
val keys = this.sharedPreferences?.all?.keys ?: return JSONArray().toString()
fun getAllKeys(): String {
val keys = this.sharedPreferences?.all?.keys ?: return JSONArray().toString()

return JSONArray(keys).toString()
}
return JSONArray(keys).toString()
}

fun getAllItems(): String {
val entries = this.sharedPreferences?.all?.entries ?: return JSONArray().toString()
fun getAllItems(): String {
val entries = this.sharedPreferences?.all?.entries ?: return JSONArray().toString()

val keyValueList = entries.map { element ->
val keyValueList =
entries.map { element ->
val jsonObject = JSONObject()
jsonObject.put("key", element.key)
jsonObject.put("value", element.value)
}

return JSONArray(keyValueList).toString()
}
return JSONArray(keyValueList).toString()
}

fun hasItem(key: String): Boolean {
return this.sharedPreferences?.contains(key) ?: false
}
fun hasItem(key: String): Boolean = this.sharedPreferences?.contains(key) ?: false

@get:Throws(GeneralSecurityException::class, IOException::class)
private val secureSharedPreferences: SharedPreferences
get() {
val key = MasterKey.Builder(context)
@get:Throws(GeneralSecurityException::class, IOException::class)
private val secureSharedPreferences: SharedPreferences
get() {
val key =
MasterKey
.Builder(context)
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
.build()

return EncryptedSharedPreferences.create(
context,
"secret_shared_prefs",
key,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
return EncryptedSharedPreferences.create(
context,
"secret_shared_prefs",
key,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM,
)
}

fun getItem(key: String): String {
try {
val value =
sharedPreferences!!.getString(key, null)
if (value == null) {
throw FileNotFoundException("$key has not been set")
} else {
return value;
}
} catch (e: Exception) {
e.printStackTrace()
return "";
fun getItem(key: String): String {
try {
val value =
sharedPreferences!!.getString(key, null)
if (value == null) {
throw FileNotFoundException("$key has not been set")
} else {
return value
}
} catch (e: Exception) {
e.printStackTrace()
return ""
}
}

fun removeItem(key: String): Boolean {
try {
if(!hasItem(key)){
return true
}
sharedPreferences!!.edit().remove(key).apply()
fun removeItem(key: String): Boolean {
try {
if (!hasItem(key)) {
return true
} catch (e: Exception) {
e.printStackTrace()
return false
}
sharedPreferences?.edit()?.remove(key)?.apply()
return true
} catch (e: Exception) {
return false
}

fun clearStorage() {
try {
if(this.sharedPreferences?.all?.keys?.size == 0) return

this.sharedPreferences?.edit()?.clear()?.apply()
} catch (e: Exception) {
e.printStackTrace()
}

fun clearStorage() {
try {
if (this.sharedPreferences
?.all
?.keys
?.size == 0
) {
return
}
}

fun install(context: ReactContext) {
val jsContextPointer = context.javaScriptContextHolder!!.get();
val jsCallInvokerHolder: CallInvokerHolderImpl =
context.catalystInstance.jsCallInvokerHolder as CallInvokerHolderImpl;
installNativeJsi(jsContextPointer, jsCallInvokerHolder)
}

companion object {

this.sharedPreferences
?.edit()
?.clear()
?.apply()
} catch (e: Exception) {
e.printStackTrace()
}
}

fun install(context: ReactContext) {
val jsContextPointer = context.javaScriptContextHolder!!.get()
val jsCallInvokerHolder =
context.catalystInstance.jsCallInvokerHolder
installNativeJsi(jsContextPointer, jsCallInvokerHolder as CallInvokerHolderImpl)
}
}

Loading

0 comments on commit c3f462c

Please sign in to comment.