package jackall.moncalc.service
import android.app.Service
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.graphics.PixelFormat
import android.graphics.Point
import android.os.Build
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.view.*
import jackall.moncalc.Const
import jackall.moncalc.Contract.MainServiceContract
import jackall.moncalc.R
import jackall.moncalc.common.PreferenceKeys
import jackall.moncalc.common.PreferenceNames
import jackall.moncalc.databinding.RegistOverlayBinding
import jackall.moncalc.utils.MySharedPref
import jackall.moncalc.viewmodel.OverlayRegistViewModel
/**
* Created by matsumoto_k on 2017/11/01.
*/
class MainService : Service(), MainServiceContract {
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 registBinding by lazy {
DataBindingUtil.inflate<RegistOverlayBinding>(LayoutInflater.from(this), R.layout.regist_overlay, null, false)
}
val windowManager: WindowManager by lazy { applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
var moveViewParams: WindowManager.LayoutParams? = null
var registViewParams: WindowManager.LayoutParams? = null
val displaySize: Point by lazy {
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
size
}
var isLongClick: Boolean = false
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val notificationBuilder = NotificationCompat.Builder(this, Const.serviceChannelId)
.setContentTitle("mainService") // TODO:タイトルをセット
.setSmallIcon(R.mipmap.ic_launcher) // TODO:白抜きのアイコンをセット
startForeground(Const.notificationId, notificationBuilder.build())
setMoveViewConfig()
setRegistViewConfig()
showMoveView()
return START_STICKY
}
private fun setMoveViewConfig() {
moveOverlayView.apply(setMoveViewClickListener())
if (Build.VERSION.SDK_INT >= 26) {
moveViewParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
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 {
moveViewParams = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
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)
}
}
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)
}
registBinding.registViewModel = OverlayRegistViewModel(this as MainServiceContract)
registViewParams?.gravity = Gravity.TOP
}
override 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 (registBinding.root.isShown) {
windowManager.removeView(registBinding.root)
}
windowManager.addView(moveOverlayView, moveViewParams)
}
override fun showRegistView() {
if (moveOverlayView.isShown) {
windowManager.removeView(moveOverlayView)
}
windowManager.addView(registBinding.root, registViewParams)
}
private fun setMoveViewClickListener(): View.() -> Unit {
return {
setOnLongClickListener { view ->
isLongClick = true
false
}.apply {
setOnTouchListener { view, motionEvent ->
val x = motionEvent.rawX.toInt()
val y = motionEvent.rawY.toInt()
when (motionEvent.action) {
MotionEvent.ACTION_MOVE -> {
if (isLongClick) {
val centerX = x - (displaySize.x / 2)
val centerY = y - (displaySize.y / 2)
moveViewParams?.x = centerX
moveViewParams?.y = centerY
windowManager.updateViewLayout(moveOverlayView, moveViewParams)
}
}
MotionEvent.ACTION_UP -> {
if (isLongClick) {
mySharedPref.putValue(PreferenceKeys.VIEWX, Int::class.java, moveViewParams?.x ?: 0)
mySharedPref.putValue(PreferenceKeys.VIEWY, Int::class.java, moveViewParams?.y ?: 0)
} else {
showRegistView()
}
isLongClick = false
}
}
false
}
}
}
}
override fun onDestroy() {
super.onDestroy()
if (moveOverlayView.isShown) {
windowManager.removeView(moveOverlayView)
}
if (moveOverlayView.isShown) {
windowManager.removeView(moveOverlayView)
}
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}