diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/activity/MainActivity.kt b/app/src/main/kotlin/jackall/overlaymulticounter/activity/MainActivity.kt index f8f1e76..f376290 100644 --- a/app/src/main/kotlin/jackall/overlaymulticounter/activity/MainActivity.kt +++ b/app/src/main/kotlin/jackall/overlaymulticounter/activity/MainActivity.kt @@ -1,13 +1,18 @@ package jackall.overlaymulticounter.activity +import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity import jackall.overlaymulticounter.R +import jackall.overlaymulticounter.service.MainService class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + + // TODO: RuntimePermissionの処理 + startService(Intent(this, MainService::class.java)) } } diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt b/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt index cd84918..92bb2e8 100644 --- a/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt +++ b/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt @@ -4,6 +4,7 @@ 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 @@ -15,6 +16,7 @@ import jackall.overlaymulticounter.activity.MainActivity import jackall.overlaymulticounter.common.PreferenceKeys import jackall.overlaymulticounter.common.PreferenceNames +import jackall.overlaymulticounter.databinding.OverlayCounterBinding import jackall.overlaymulticounter.utils.MySharedPref import kotlinx.android.synthetic.main.overlay_move.view.* @@ -26,6 +28,10 @@ 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 overlayCounterViewParams: WindowManager.LayoutParams? = null val windowManager: WindowManager by lazy { applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager } val displaySize: Point by lazy { val display = windowManager.defaultDisplay @@ -43,6 +49,7 @@ startForeground(Const.serviceNotificationId, notificationBuilder.build()) setMoveViewConfig() + setCounterViewConfig() showMoveView() return START_STICKY @@ -52,9 +59,21 @@ overlayMoveViewParams?.x = mySharedPref.getValue(PreferenceKeys.VIEWX, Int::class.java, 0) as Int overlayMoveViewParams?.y = mySharedPref.getValue(PreferenceKeys.VIEWY, Int::class.java, 0) as Int + if (counterBinding.root.isShown) { + windowManager.removeView(counterBinding.root) + } + windowManager.addView(overlayMoveView, overlayMoveViewParams) } + fun showCounterView() { + if (overlayMoveView.isShown) { + windowManager.removeView(overlayMoveView) + } + + windowManager.addView(counterBinding.root, overlayCounterViewParams) + } + fun setMoveViewConfig() { overlayMoveView.apply(setMoveViewClickListener()) @@ -81,6 +100,32 @@ } } + fun setCounterViewConfig() { + if (Build.VERSION.SDK_INT >= 26) { + overlayCounterViewParams = 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 { + overlayCounterViewParams = 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) + } + + overlayCounterViewParams?.gravity = Gravity.TOP + } + private fun setMoveViewClickListener(): View.() -> Unit { return { setOnLongClickListener { view -> @@ -114,7 +159,7 @@ mySharedPref.putValue(PreferenceKeys.VIEWX, Int::class.java, overlayMoveViewParams?.x ?: 0) mySharedPref.putValue(PreferenceKeys.VIEWY, Int::class.java, overlayMoveViewParams?.y ?: 0) } else { - + showCounterView() } isLongClick = false } diff --git a/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayCounterViewModel.kt b/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayCounterViewModel.kt new file mode 100644 index 0000000..9c83f3a --- /dev/null +++ b/app/src/main/kotlin/jackall/overlaymulticounter/viewmodel/OverlayCounterViewModel.kt @@ -0,0 +1,9 @@ +package jackall.overlaymulticounter.viewmodel + +import android.databinding.BaseObservable + +/** + * Created by matsumoto_k on 2017/11/08. + */ +class OverlayCounterViewModel : BaseObservable() { +} \ 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 new file mode 100644 index 0000000..322e260 --- /dev/null +++ b/app/src/main/res/layout/overlay_counter.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + \ No newline at end of file