Skip to content

Commit

Permalink
Merge pull request #18 from iamport/dev
Browse files Browse the repository at this point in the history
머지허락
  • Loading branch information
kjh5833 authored Feb 5, 2021
2 parents 56f1745 + ee994a1 commit 94e2d05
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 27 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ dependencies {
// Koin AndroidX Experimental features
implementation "org.koin:koin-androidx-ext:$koin_version"

implementation project(':sdk')
// implementation 'com.github.iamport:iamport-android:dev-SNAPSHOT'
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.iamport.sdk.data.chai.response
data class ApproveData(
val impUid: String,
val merchantUid: String,
val success: String,
val success: Boolean,
val reason: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ enum class ChaiPaymentStatus {
confirmed, partial_confirmed;

companion object {
fun from(s: String): ChaiPaymentStatus? = values().find { it.name == s }
fun from(displayStatus: String): ChaiPaymentStatus? = values().find { it.name == displayStatus }
}
}
9 changes: 6 additions & 3 deletions sdk/src/main/java/com/iamport/sdk/domain/JsNativeInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class JsNativeInterface(val payment: Payment, val gson: Gson, private val bus: W
*/
@JavascriptInterface
fun customCallback(response: String) {
val impRes = gson.fromJson(response, IamPortResponse::class.java)
Logger.d("customCallback paymentover :: $impRes")
bus.impResponse.postValue(Event(impRes))
Logger.d("customCallback response :: $response")
runCatching {
val impRes = gson.fromJson(response, IamPortResponse::class.java)
Logger.d("customCallback paymentover :: $impRes")
bus.impResponse.postValue(Event(impRes))
}
}
}
1 change: 1 addition & 0 deletions sdk/src/main/java/com/iamport/sdk/domain/core/Iamport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ object Iamport {
/**
* Application instance 를 통해 SDK 생명주기 감지, DI 초기화
*/
// TODO Application 사용하지 않는 방안 모색
fun create(app: Application, koinApp: KoinApplication? = null) {

IamportKoinContext.koinApp = koinApp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ open class ChaiStrategy : BaseStrategy() {
return
}

// TODO approved 한번 더 체크하는거 필요할까?
when (val response =
apiGetChaiStatus(idempotencyKey.toString(), publicAPIKey.toString(), paymentId.toString())) {
is NetworkError -> failureFinish(payment, prepareData, "NetworkError ${response.error}")
Expand Down Expand Up @@ -341,7 +342,7 @@ open class ChaiStrategy : BaseStrategy() {
}
tryCount++

when (ChaiPaymentStatus.from(chaiPayment.status)) {
when (ChaiPaymentStatus.from(chaiPayment.displayStatus)) {
approved -> confirmMerchant(payment, data)

confirmed -> successFinish(payment, prepareData, "가맹점 측 결제 승인 완료 (결제 성공) ${chaiPayment.status}")
Expand Down
34 changes: 14 additions & 20 deletions sdk/src/main/java/com/iamport/sdk/domain/utils/Foreground.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,39 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import com.orhanobut.logger.Logger
import com.orhanobut.logger.Logger.d


object Foreground : ActivityLifecycleCallbacks {

enum class AppStatus {
BACKGROUND, // app is background
RETURNED_TO_FOREGROUND, // app returned to foreground(or first launch)
FOREGROUND, // app is foreground
}

var application: Application? = null
var appStatus: AppStatus? = null

val isBackground: Boolean // 백그라운드 여부
get() = appStatus!!.ordinal == AppStatus.BACKGROUND.ordinal

var isScreenOn: Boolean = true // 스크린 on/off 여부

enum class AppStatus {
BACKGROUND, // app is background
RETURNED_TO_FOREGROUND, // app returned to foreground(or first launch)
FOREGROUND, // app is foreground
}

// running activity count
private var running = 0


fun init(app: Application) {
application = app

// 생명주기 콜백
app.registerActivityLifecycleCallbacks(this)

// 스크린 on/off 감지 intent 필터
app.registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SCREEN_ON -> isScreenOn = true
Intent.ACTION_SCREEN_OFF -> isScreenOn = false
}
d(intent?.action.toString())
}
}, IntentFilter(Intent.ACTION_SCREEN_OFF).apply {
addAction(Intent.ACTION_SCREEN_ON)
})
application?.registerActivityLifecycleCallbacks(this)
// application?.unregisterActivityLifecycleCallbacks(this) // https://stackoverflow.com/questions/17865187/what-is-the-proper-way-to-unregister-activity-lifecycle-callbacks/23299321
}


override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}
override fun onActivityStarted(activity: Activity) {
isScreenOn = true
Expand All @@ -73,4 +66,5 @@ object Foreground : ActivityLifecycleCallbacks {
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) {}
override fun onActivityDestroyed(activity: Activity) {}


}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.iamport.sdk.presentation.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.Uri
Expand Down Expand Up @@ -40,7 +42,8 @@ internal class IamportSdk(
private val hostHelper: HostHelper = HostHelper(activity, fragment)

private val launcherChai: ActivityResultLauncher<Pair<String, String>>? // 차이앱 런처
// private val viewModel: MainViewModel // 요청할 뷰모델

// private val viewModel: MainViewModel // 요청할 뷰모델
private val viewModel: MainViewModel by viewModel(hostHelper.viewModelStoreOwner, MainViewModel::class.java) // 요청할 뷰모델 {


Expand All @@ -50,8 +53,24 @@ internal class IamportSdk(
private val isPolling = MutableLiveData<Event<Boolean>>()
private val preventOverlapRun = PreventOverlapRun() // 딜레이 호출

// 포그라운드 서비스 관련 BroadcastReceiver
private val iamportReceiver: IamportReceiver by inject()

// 스크린 on/off 감지 BroadcastReceiver
private val screenBrReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
Intent.ACTION_SCREEN_ON -> Foreground.isScreenOn = true
Intent.ACTION_SCREEN_OFF -> Foreground.isScreenOn = false
}
d(intent?.action.toString())
}
}

private val screenBrFilter = IntentFilter(Intent.ACTION_SCREEN_OFF).apply {
addAction(Intent.ACTION_SCREEN_ON)
}

init {
// viewModel = ViewModelProvider(hostHelper.viewModelStoreOwner, MainViewModelFactory(get(), get())).get(MainViewModel::class.java)

Expand Down Expand Up @@ -85,6 +104,7 @@ internal class IamportSdk(
hostHelper.lifecycle.removeObserver(this)
runCatching {
hostHelper.context?.unregisterReceiver(iamportReceiver)
hostHelper.context?.applicationContext?.unregisterReceiver(screenBrReceiver)
}
}
}
Expand All @@ -98,6 +118,7 @@ internal class IamportSdk(
IntentFilter().let {
it.addAction(CONST.BROADCAST_FOREGROUND_SERVICE)
it.addAction(CONST.BROADCAST_FOREGROUND_SERVICE_STOP)
hostHelper.context?.applicationContext?.registerReceiver(screenBrReceiver, screenBrFilter)
hostHelper.context?.registerReceiver(iamportReceiver, it)
}

Expand Down

0 comments on commit 94e2d05

Please sign in to comment.