Skip to content

Instantly share code, notes, and snippets.

View AliAzaz's full-sized avatar
🇵🇰
Passion to help others 😊

Ali Azaz Alam AliAzaz

🇵🇰
Passion to help others 😊
View GitHub Profile
@AliAzaz
AliAzaz / CoroutineDispatcherHelper.kt
Last active July 25, 2023 06:49
Coroutine Dispatcher in a HILT injection Environment
class CoroutineDispatcherHelper : IDispatcher {
override fun dispatcherIO(): CoroutineDispatcher = Dispatchers.IO
override fun dispatcherMain(): CoroutineDispatcher = Dispatchers.Main
override fun dispatcherDefault(): CoroutineDispatcher = Dispatchers.Default
}
@AliAzaz
AliAzaz / FlowThrottleDebounce.kt
Created October 22, 2021 08:09 — forked from chibatching/FlowThrottleDebounce.kt
Throttle and Debounce on Flow Kotlin Coroutines
fun <T> Flow<T>.throttle(waitMillis: Int) = flow {
coroutineScope {
val context = coroutineContext
var nextMillis = 0L
var delayPost: Deferred<Unit>? = null
collect {
val current = SystemClock.uptimeMillis()
if (nextMillis < current) {
nextMillis = current + waitMillis
@AliAzaz
AliAzaz / EncryptFiles.kt
Created October 9, 2021 22:50
Encrypted Files in Jetpack Security
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
val encrypFile = EncryptedFile.Builder(
File(directoryPath, "file_name"),
context,
mainKeyAlias,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB
@AliAzaz
AliAzaz / EncryptSharedPref.kt
Created October 9, 2021 20:49
Encrypted SharedPreference in Jetpack Security
// Although you can define your own key generation parameter specification, it's
// recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val mainKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
val sharedPref = EncryptedSharedPreferences.create(
"preference_name",
mainKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
@AliAzaz
AliAzaz / AdvMasterKey.kt
Last active October 6, 2021 22:36
Master key with advanced specification in Jetpack Security
fun generateAdvMasterKey(context: Context): MasterKey {
val advSpec = KeyGenParameterSpec.Builder(
"key_name", // define your master key name
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT
).apply {
setBlockModes(KeyProperties.BLOCK_MODE_GCM)
setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
setKeySize(256)
setUserAuthenticationRequired(true)
setUserAuthenticationValidityDurationSeconds(120)
@AliAzaz
AliAzaz / MasterKey.kt
Created October 5, 2021 22:02
Get MasterKey using JetpackSecurity
fun getMasterKey(context: Context): MasterKey {
return
MasterKey.Builder(context)
.setKeyGenParameterSpec(AES256_GCM_SPEC)
.build()
}
@AliAzaz
AliAzaz / DiffUtilsActivity.kt
Last active July 25, 2023 06:48
Generic RecyclerAdapter implementtion using DiffUtils
private lateinit var adapter: GenericListAdapter<ImagesInfo>
adapter = GenericListAdapter(R.layout.product_view) { item, position ->
viewModel.setSelectedProduct(item)
findNavController().navigate(ImageListFragmentDirections.actionImageListFragmentToImageDetailFragment())
}
@AliAzaz
AliAzaz / MediaStorageFetchingFromManager.kt
Created August 9, 2021 09:00
Get full file path from a URI that points to a any document
/*
* Credit to: https://stackoverflow.com/a/60642994/9764941
* */
object ImageMediaStoreFetching {
@JvmStatic
@SuppressLint("NewApi")
fun getPath(uri: Uri): String? {
val selection: String?
val selectionArgs: Array<String>?
@AliAzaz
AliAzaz / Keys.kt
Created July 7, 2021 11:10
Use this file to load cpp library file in NDK api_key process
package edu.practice.utils.shared
object Keys {
init {
System.loadLibrary("native-lib")
}
external fun apiKey(): String
}
@AliAzaz
AliAzaz / lib.cpp
Created July 7, 2021 09:34
.cpp file to secure key
#include <jni.h>
#include <string>
extern "C"
JNIEXPORT jstring JNICALL
Java_edu_practice_utils_shared_Keys_apiKey(JNIEnv *env, jobject thiz) {
std::string api_key = "c527aa8fadcb58f1cccef75e3a64a3ae";
return env->NewStringUTF(api_key.c_str());
}