diff --git a/app/src/main/kotlin/jackall/moncalc/service/MainService.kt b/app/src/main/kotlin/jackall/moncalc/service/MainService.kt index c40c353..e74d903 100644 --- a/app/src/main/kotlin/jackall/moncalc/service/MainService.kt +++ b/app/src/main/kotlin/jackall/moncalc/service/MainService.kt @@ -22,8 +22,10 @@ val mySharedPref by lazy { MySharedPref(this, PreferenceNames.CONFIG) } val moveOverlayView: ViewGroup by lazy { LayoutInflater.from(this).inflate(R.layout.move_overlay, null) as ViewGroup } + val registOverlayView: ViewGroup by lazy { LayoutInflater.from(this).inflate(R.layout.regist_overlay, null) as ViewGroup } val windowManager: WindowManager by lazy { applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager } - var params: WindowManager.LayoutParams? = null + var moveViewParams: WindowManager.LayoutParams? = null + var registViewParams: WindowManager.LayoutParams? = null val displaySize: Point by lazy { val display = windowManager.defaultDisplay val size = Point() @@ -38,16 +40,19 @@ .setSmallIcon(R.mipmap.ic_launcher) // TODO:白抜きのアイコンをセット startForeground(Const.notificationId, notificationBuilder.build()) - setMoveOverlayView() + setMoveViewConfig() + setRegistViewConfig() + + showMoveView() return START_STICKY } - private fun setMoveOverlayView() { - moveOverlayView.apply(clickListener()) + private fun setMoveViewConfig() { + moveOverlayView.apply(setMoveViewClickListener()) if (Build.VERSION.SDK_INT >= 26) { - params = WindowManager.LayoutParams( + moveViewParams = WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, @@ -57,7 +62,7 @@ WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT) } else { - params = WindowManager.LayoutParams( + moveViewParams = WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, @@ -67,14 +72,54 @@ WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT) } - - params?.x = mySharedPref.getValue(PreferenceKeys.VIEWX, Int::class.java, 0) as Int - params?.y = mySharedPref.getValue(PreferenceKeys.VIEWY, Int::class.java, 0) as Int - - windowManager.addView(moveOverlayView, params) } - private fun clickListener(): View.() -> Unit { + private fun setRegistViewConfig() { + if (Build.VERSION.SDK_INT >= 26) { + registViewParams = WindowManager.LayoutParams( + WindowManager.LayoutParams.MATCH_PARENT, + WindowManager.LayoutParams.WRAP_CONTENT, + WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or + WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, + PixelFormat.TRANSLUCENT) + } else { + registViewParams = WindowManager.LayoutParams( + WindowManager.LayoutParams.MATCH_PARENT, + WindowManager.LayoutParams.WRAP_CONTENT, + WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN or + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or + WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, + PixelFormat.TRANSLUCENT) + } + + registViewParams?.gravity = Gravity.TOP + } + + private fun showMoveView() { + moveViewParams?.x = mySharedPref.getValue(PreferenceKeys.VIEWX, Int::class.java, 0) as Int + moveViewParams?.y = mySharedPref.getValue(PreferenceKeys.VIEWY, Int::class.java, 0) as Int + + if (registOverlayView.isShown) { + windowManager.removeView(registOverlayView) + } + + windowManager.addView(moveOverlayView, moveViewParams) + } + + private fun showRegistView() { + if (moveOverlayView.isShown) { + windowManager.removeView(moveOverlayView) + } + + windowManager.addView(registOverlayView, registViewParams) + } + + private fun setMoveViewClickListener(): View.() -> Unit { return { setOnLongClickListener { view -> isLongClick = true @@ -93,17 +138,19 @@ val centerX = x - (displaySize.x / 2) val centerY = y - (displaySize.y / 2) - params?.x = centerX - params?.y = centerY + moveViewParams?.x = centerX + moveViewParams?.y = centerY - windowManager.updateViewLayout(moveOverlayView, params) + windowManager.updateViewLayout(moveOverlayView, moveViewParams) } } MotionEvent.ACTION_UP -> { if (isLongClick) { - mySharedPref.putValue(PreferenceKeys.VIEWX, Int::class.java, params?.x ?: 0) - mySharedPref.putValue(PreferenceKeys.VIEWY, Int::class.java, params?.y ?: 0) + mySharedPref.putValue(PreferenceKeys.VIEWX, Int::class.java, moveViewParams?.x ?: 0) + mySharedPref.putValue(PreferenceKeys.VIEWY, Int::class.java, moveViewParams?.y ?: 0) + } else { + showRegistView() } isLongClick = false }