Skip to content

Instantly share code, notes, and snippets.

View kmshack's full-sized avatar

Minsoo Kim kmshack

View GitHub Profile
@kmshack
kmshack / ignore.java
Last active November 26, 2023 10:50
android battery optimization ignore
Intent i = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName){
i.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
} else {
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
@kmshack
kmshack / build.gradle
Last active June 13, 2018 06:16
build.gradle
dependencies{
kapt "android.arch.lifecycle:compiler:1.1.1"
implementation "android.arch.lifecycle:extensions:1.1.1"
}
@kmshack
kmshack / AppApplication.kt
Created June 13, 2018 06:11
AppApplication
class AppApplication : Application() {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle
.addObserver(AppLifecycleObserver())
}
}
@kmshack
kmshack / AppLifecycleObserver.kt
Created June 13, 2018 06:10
AppLifecycleObserver
class AppLifecycleObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onForeground() {
//foreground
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onBackground() {
//background
@kmshack
kmshack / retrofitCoroutines.kt
Last active May 20, 2018 16:34
Retrofit2 + Coroutines
class LoginRepository @Inject constructor(private val apiService: APIService, private val sharedPreferences: SharedPreferences) {
fun getOTPForNumber(phoneNumber: String): LiveData<OTPResult> {
val loginResult = MutableLiveData<OTPResult>()
launch {
try {
val request = apiService.getOTPForNumber(phoneNumber)
val response = request.await()
@kmshack
kmshack / CoroutinesExample.kt
Created May 20, 2018 15:11
coroutines example
private lateinit var job2: Job
fun toDoWorkConcurrent() {
job2 = launch {
try {
val work1 = async { getThingsDone(43) }
val work2 = async { getThingsDoneAgain(123) }
@kmshack
kmshack / coroutinesExample.kt
Created May 20, 2018 05:52
coroutines example
private lateinit var job1: Job
suspend fun getThingsDone(myThings: Int): Int {
// Heavy computation going on here :D
return myThings + 99
}
suspend fun getThingsDoneAgain(myThings: Int): Int {
// Heavy computation going on here :D
return myThings + 50
@kmshack
kmshack / coroutines.kt
Created May 20, 2018 05:50
coroutines
public actual fun launch(
context: CoroutineContext = DefaultDispatcher,
start: CoroutineStart = CoroutineStart.DEFAULT,
parent: Job? = null,
block: suspend CoroutineScope.() -> Unit
): Job {
val newContext = newCoroutineContext(context, parent)
val coroutine = if (start.isLazy)
LazyStandaloneCoroutine(newContext, block) else
StandaloneCoroutine(newContext, active = true)
@kmshack
kmshack / GridFragment.kt
Created March 18, 2018 15:27
GridFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sharedElementEnterTransition = ChangeBounds()
}
@kmshack
kmshack / GridFragment.kt
Created March 18, 2018 15:22
GridFragment
postponeEnterTransition()
recyclerView.doOnPreDraw {
startPostponedEnterTransition()
}