Skip to content

Instantly share code, notes, and snippets.

View kairos34's full-sized avatar
🏠
Working from home

Alper ÖZASLAN kairos34

🏠
Working from home
View GitHub Profile
@kairos34
kairos34 / SessionManagerImpl.java
Last active October 30, 2023 11:38
SessionManager implementation in Java for Android
public interface SessionManager {
void startSession(int timeoutMillis);
void keepAlive();
void stopSession();
void checkSession();
@kairos34
kairos34 / NFC.kt
Created May 25, 2022 09:38
Enable NFC feature if device has it
fun turnOnNfc() {
runBlocking {
var count = 0
NfcAdapter.getDefaultAdapter(get())?.run {
enableNfc()
while (!isEnabled) {
Log.i("TAG", "Waiting for NFC adapter to turn on")
delay(200)
count += 1
if(count > 10) {
@kairos34
kairos34 / SystemUIUtils.kt
Last active April 22, 2022 09:05
Show/Hide System UI
val hardwareId
get() = "getprop ro.hardware".runAsCommand().trim()
fun setSystemUIVisibility(show: Boolean) {
GlobalScope.launch {
if (show) {
if(hardwareId.equals("qcom")) { // ZPAD Plus 4G (7.1.1)
"am broadcast -a com.android.zkteco.SHOW_NAVIGATION".runAsCommand()
"am broadcast -a com.android.zkteco.SHOW_STATUS".runAsCommand()
} else { // ZPAD Plus (6.0.1)
@kairos34
kairos34 / Util.kt
Created March 29, 2022 13:31
Collect flow lifecycle aware way extension function
fun <T> Fragment.collectLifecycleFlow(flow: Flow<T>, collect: suspend (T) -> Unit){
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED){
flow.collectLatest(collect)
}
}
}
@kairos34
kairos34 / CardValueUpdater.kt
Created January 10, 2022 10:42
Update old card values
// We have RfidListener interface where you can get callback when a card is punched.
// This class has two methods:
// fun onRfidRead(rfidInfo : String)
// fun onRfidReadWithOldValue(rfidInfo : String, oldRfidInfo: String, useRfidStandardization: Boolean)
// onRfidRead method is used right now to get card value from SDK but due to some reasons such as supporting new card types etc...
// We have to update old card values with new ones using onRfidReadWithOldValue function
// Launcher applications are good place to handle with update process at least we update old values in that application.
// You have to override onRfidReadWithOldValue function in order to update old values and call
// super.onRfidReadWithOldValue function after update process
// I am sharing code snippet from our implementation
@kairos34
kairos34 / RTCManager.kt
Last active January 3, 2022 10:30
Handle RTC
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.SystemClock
import android.util.Log
import com.zk.android.constant.SdkException
import kotlinx.coroutines.GlobalScope
@kairos34
kairos34 / MCUReset.kt
Created October 25, 2021 11:45
Reset MCU
private val rebootCodesArray = intArrayOf(-1500, -1501, -1502, -4010)
private val resetMcuInterval = 25000
private var isReOpenDevice = false
private var lastResetTime = 0L
//Check error code after calling checkCardEx(buffer) and if there is a error reset it
//////// This block has to be called after checkCardEx(buffer)
(errorCode != -4013).ifTrue {
"Error $errorCode checking card ID".log(TAG)
}
@kairos34
kairos34 / TopSportFinder.kt
Created October 15, 2021 08:19
Write kotlin code to print the top sport by distance excluding eBikes
enum class Sport { HIKE, RUN, TOURING_BICYCLE, E_TOURING_BICYCLE }
data class Summary(val sport: Sport, val distance: Int)
fun main() {
val sportStats = listOf(Summary(Sport.HIKE, 92),
Summary(Sport.RUN, 77),
Summary(Sport.TOURING_BICYCLE, 322),
Summary(Sport.E_TOURING_BICYCLE, 656))
@kairos34
kairos34 / ZipManager
Created March 19, 2019 15:06
Kotlin zip and unzip functions
object ZipManager {
fun zip(files: List<File>, zipFile: File) {
ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile))).use { output ->
files.forEach { file ->
(file.length() > 1).ifTrue {
FileInputStream(file).use { input ->
BufferedInputStream(input).use { origin ->
val entry = ZipEntry(file.name.toRealName())
output.putNextEntry(entry)
@kairos34
kairos34 / args_parser.bash
Created July 15, 2016 14:33
Parsing args via bash script
parse_config_values() {
serial_obtained=false
shift
while [[ $# -gt 1 ]]; do
case "$1" in
--serial)
config_serial=$2
echo $config_serial
serial_obtained=true