diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 0971d0c..52d181a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + , val key: String) { + VIEWX(PreferenceNames.CONFIG, Int::class.java, "viewX"), + VIEWY(PreferenceNames.CONFIG, Int::class.java, "viewY") } \ 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 666ca47..cd84918 100644 --- a/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt +++ b/app/src/main/kotlin/jackall/overlaymulticounter/service/MainService.kt @@ -2,26 +2,129 @@ import android.app.PendingIntent import android.app.Service +import android.content.Context import android.content.Intent +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.overlaymulticounter.Const import jackall.overlaymulticounter.R import jackall.overlaymulticounter.activity.MainActivity +import jackall.overlaymulticounter.common.PreferenceKeys +import jackall.overlaymulticounter.common.PreferenceNames +import jackall.overlaymulticounter.utils.MySharedPref +import kotlinx.android.synthetic.main.overlay_move.view.* /** * Created by matsumoto_k on 2017/11/08. */ class MainService : Service() { + 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 windowManager: WindowManager by lazy { applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager } + 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) .setSmallIcon(R.mipmap.ic_launcher) // TODO:白抜きのアイコンをセット notificationBuilder.setContentIntent(PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT)) startForeground(Const.serviceNotificationId, notificationBuilder.build()) + + setMoveViewConfig() + showMoveView() + return START_STICKY } + 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 + + windowManager.addView(overlayMoveView, overlayMoveViewParams) + } + + fun setMoveViewConfig() { + overlayMoveView.apply(setMoveViewClickListener()) + + if (Build.VERSION.SDK_INT >= 26) { + overlayMoveViewParams = 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 { + overlayMoveViewParams = 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 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) + + overlayMoveViewParams?.x = centerX + overlayMoveViewParams?.y = centerY + + windowManager.updateViewLayout(overlayMoveView, overlayMoveViewParams) + } + } + + MotionEvent.ACTION_UP -> { + if (isLongClick) { + view.move_button.alpha = Const.moveButtonInactiveAlpha + mySharedPref.putValue(PreferenceKeys.VIEWX, Int::class.java, overlayMoveViewParams?.x ?: 0) + mySharedPref.putValue(PreferenceKeys.VIEWY, Int::class.java, overlayMoveViewParams?.y ?: 0) + } else { + + } + isLongClick = false + } + } + false + } + } + } + } + override fun onBind(intent: Intent?): IBinder? { return null } diff --git a/app/src/main/res/drawable/overlay_move_button.png b/app/src/main/res/drawable/overlay_move_button.png new file mode 100644 index 0000000..9ce316d --- /dev/null +++ b/app/src/main/res/drawable/overlay_move_button.png Binary files differ diff --git a/app/src/main/res/layout/overlay_move.xml b/app/src/main/res/layout/overlay_move.xml new file mode 100644 index 0000000..3197c96 --- /dev/null +++ b/app/src/main/res/layout/overlay_move.xml @@ -0,0 +1,13 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml new file mode 100644 index 0000000..b42e21a --- /dev/null +++ b/app/src/main/res/values/dimens.xml @@ -0,0 +1,4 @@ + + + 48dp + \ No newline at end of file