Newer
Older
MonCalc / app / src / main / kotlin / jackall / moncalc / service / MainService.kt
package jackall.moncalc.service

import android.app.PendingIntent
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 com.google.android.gms.ads.AdSize
import jackall.moncalc.Const
import jackall.moncalc.R
import jackall.moncalc.activity.MainActivity
import jackall.moncalc.adapter.CustomSpinnerAdapter
import jackall.moncalc.common.PreferenceKeys
import jackall.moncalc.common.PreferenceNames
import jackall.moncalc.contract.MainServiceContract
import jackall.moncalc.databinding.RegistOverlayBinding
import jackall.moncalc.db.MonstDataRealmHelper
import jackall.moncalc.utils.AdUtil
import jackall.moncalc.utils.MySharedPref
import jackall.moncalc.viewmodel.OverlayRegistViewModel
import jackall.moncalc.viewmodel.OverlayViewModel
import jackall.moncalc.vo.Temple
import kotlinx.android.synthetic.main.move_overlay.view.*


/**
 * 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

    val overlayRegistViewModel by lazy { OverlayRegistViewModel(this as MainServiceContract, this) }
    val overlayViewModel by lazy { OverlayViewModel(this as MainServiceContract) }

    val monstDataRealmHelper = MonstDataRealmHelper()

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notificationBuilder = NotificationCompat.Builder(this, Const.serviceChannelId)
                .setSmallIcon(R.drawable.notification_icon) // TODO:白抜きのアイコンをセット
        notificationBuilder.setContentIntent(PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT))
        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.MATCH_PARENT,
                    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.MATCH_PARENT,
                    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)
        }

        setSpinnerConfig()

        registViewParams?.gravity = Gravity.TOP
    }

    fun setSpinnerConfig() {
        registBinding.registViewModel = overlayRegistViewModel
        registBinding.viewModel = overlayViewModel
        val templeSpinnerAdapter = CustomSpinnerAdapter(this, monstDataRealmHelper.findNames(Temple::class.java))
        registBinding.templeSpinner.adapter = templeSpinnerAdapter
    }

    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() {
        AdUtil.startBannerAd(this, registBinding.overlayBannerLayout, AdSize.BANNER, Const.overlayBannerUnitId)
        if (moveOverlayView.isShown) {
            windowManager.removeView(moveOverlayView)
        }

        windowManager.addView(registBinding.root, registViewParams)
    }

    private fun setMoveViewClickListener(): View.() -> Unit {
        return {
            setOnLongClickListener { view ->
                isLongClick = true
                view.move_button.alpha = Const.moveButtonActiveAlpha
                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) {
                                view.move_button.alpha = Const.moveButtonInactiveAlpha
                                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 (registBinding.root.isShown) {
            windowManager.removeView(registBinding.root)
        }
        registBinding.registViewModel?.realmInstanceClose()
    }

    override fun finish() {
        stopSelf()
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}