diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/contract/MainServiceContract.kt b/app/src/main/kotlin/jackall/overlaymulticounter/contract/MainServiceContract.kt new file mode 100644 index 0000000..bb9adf7 --- /dev/null +++ b/app/src/main/kotlin/jackall/overlaymulticounter/contract/MainServiceContract.kt @@ -0,0 +1,10 @@ +package jackall.overlaymulticounter.contract + +/** + * Created by matsumoto_k on 2017/11/08. + */ +interface MainServiceContract { + fun showMoveView() + fun showCounterView() + fun finish() +} \ No newline at end of file diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt b/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt index 92bb2e8..5bb350c 100644 --- a/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt +++ b/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt @@ -16,22 +16,28 @@ import jackall.overlaymulticounter.activity.MainActivity import jackall.overlaymulticounter.common.PreferenceKeys import jackall.overlaymulticounter.common.PreferenceNames +import jackall.overlaymulticounter.contract.MainServiceContract import jackall.overlaymulticounter.databinding.OverlayCounterBinding import jackall.overlaymulticounter.utils.MySharedPref +import jackall.overlaymulticounter.viewmodel.OverlayCounterViewModel +import jackall.overlaymulticounter.viewmodel.OverlayViewModel import kotlinx.android.synthetic.main.overlay_move.view.* /** * Created by matsumoto_k on 2017/11/08. */ -class MainService : Service() { +class MainService : Service(), MainServiceContract { val mySharedPref by lazy { MySharedPref(this, PreferenceNames.CONFIG) } + val overlayMoveView: ViewGroup by lazy { LayoutInflater.from(this).inflate(R.layout.overlay_move, null) as ViewGroup } - var overlayMoveViewParams: WindowManager.LayoutParams? = null val counterBinding by lazy { DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.overlay_counter, null, false) } + var overlayMoveViewParams: WindowManager.LayoutParams? = null var overlayCounterViewParams: WindowManager.LayoutParams? = null + val overlayViewModel by lazy { OverlayViewModel(this as MainServiceContract) } + val overlayCounterViewModel by lazy { OverlayCounterViewModel() } val windowManager: WindowManager by lazy { applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager } val displaySize: Point by lazy { val display = windowManager.defaultDisplay @@ -55,7 +61,7 @@ return START_STICKY } - fun showMoveView() { + override fun showMoveView() { overlayMoveViewParams?.x = mySharedPref.getValue(PreferenceKeys.VIEWX, Int::class.java, 0) as Int overlayMoveViewParams?.y = mySharedPref.getValue(PreferenceKeys.VIEWY, Int::class.java, 0) as Int @@ -66,7 +72,7 @@ windowManager.addView(overlayMoveView, overlayMoveViewParams) } - fun showCounterView() { + override fun showCounterView() { if (overlayMoveView.isShown) { windowManager.removeView(overlayMoveView) } @@ -122,7 +128,8 @@ WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT) } - + counterBinding.overlayViewModel = overlayViewModel + counterBinding.counterViewModel = overlayCounterViewModel overlayCounterViewParams?.gravity = Gravity.TOP } @@ -173,4 +180,18 @@ override fun onBind(intent: Intent?): IBinder? { return null } + + override fun onDestroy() { + super.onDestroy() + if (overlayMoveView.isShown) { + windowManager.removeView(overlayMoveView) + } + if (counterBinding.root.isShown) { + windowManager.removeView(counterBinding.root) + } + } + + override fun finish() { + stopSelf() + } } \ No newline at end of file diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayViewModel.kt b/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayViewModel.kt new file mode 100644 index 0000000..92c05cc --- /dev/null +++ b/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayViewModel.kt @@ -0,0 +1,20 @@ +package jackall.overlaymulticounter.viewmodel + +import android.databinding.BaseObservable +import android.view.View +import jackall.overlaymulticounter.contract.MainServiceContract + +/** + * オーバーレイ表示で共通部分のViewModel + * + * Created by matsumoto_k on 2017/11/08. + */ +class OverlayViewModel(val mainServiceContract: MainServiceContract) : BaseObservable() { + fun onClickMin(view: View) { + mainServiceContract.showMoveView() + } + + fun onClickFinish(view: View) { + mainServiceContract.finish() + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/overlay_counter.xml b/app/src/main/res/layout/overlay_counter.xml index 322e260..5e8d1f2 100644 --- a/app/src/main/res/layout/overlay_counter.xml +++ b/app/src/main/res/layout/overlay_counter.xml @@ -1,16 +1,43 @@ - + + + + android:layout_height="match_parent" + android:paddingTop="@dimen/status_bar_height"> + +