Last active
September 24, 2021 06:42
-
-
Save nosix/c3baf62bb095410a812ac638413c457d to your computer and use it in GitHub Desktop.
Floating App for Android (SDK 21) in Kotlin 1.0.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="xxx"> | |
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> | |
<application | |
... | |
<service android:name=".FloatingAppService"/> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.app.Activity | |
import android.content.Intent | |
import android.net.Uri | |
import android.os.Build | |
import android.provider.Settings | |
fun Activity.hasOverlayPermission(): Boolean = | |
if (Build.VERSION.SDK_INT >= 23) Settings.canDrawOverlays(this) else true | |
fun Activity.requestOverlayPermission(requestCode: Int) { | |
if (Build.VERSION.SDK_INT >= 23) { | |
val intent = Intent( | |
Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:$packageName")) | |
startActivityForResult(intent, requestCode) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.app.Notification | |
import android.app.PendingIntent | |
import android.app.Service | |
import android.content.Intent | |
import android.os.IBinder | |
import android.view.WindowManager | |
import android.widget.ImageView | |
import java.util.* | |
import xxx.FloatingButton | |
class FloatingAppService : Service() { | |
companion object { | |
val ACTION_START = "start" | |
val ACTION_STOP = "stop" | |
} | |
private val notificationId = Random().nextInt() | |
private var button: FloatingButton? = null | |
override fun onCreate() { | |
super.onCreate() | |
startNotification() | |
} | |
private fun startNotification() { | |
val activityIntent = Intent(this, MainActivity::class.java) | |
val pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0) | |
val notification = Notification.Builder(this) | |
.setContentIntent(pendingIntent) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle(FloatingAppService::class.simpleName) | |
.setContentText("Service is running.") | |
.build() | |
startForeground(notificationId, notification) | |
} | |
override fun onBind(intent: Intent?): IBinder? = null | |
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { | |
if (intent == null || intent.action == ACTION_START) { | |
startOverlay() | |
} else { | |
stopSelf() | |
} | |
return Service.START_STICKY | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
stopOverlay() | |
} | |
private fun startOverlay() { | |
ImageView(this).run { | |
val windowManager = getSystemService(Service.WINDOW_SERVICE) as WindowManager | |
setImageResource(android.R.drawable.ic_menu_add) | |
button = FloatingButton(windowManager, this).apply { | |
visible = true | |
} | |
} | |
} | |
private fun stopOverlay() { | |
button?.run { | |
visible = false | |
} | |
button = null | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xxx | |
import android.content.Intent | |
import android.os.Bundle | |
import android.support.v7.app.AppCompatActivity | |
import android.util.Log | |
import android.view.MotionEvent | |
import xxx.FloatingAppService | |
import xxx.hasOverlayPermission | |
import xxx.requestOverlayPermission | |
class MainActivity : AppCompatActivity() { | |
companion object { | |
private val TAG = MainActivity::class.qualifiedName | |
private val REQUEST_OVERLAY_PERMISSION = 1 | |
} | |
private var enabled = true | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
} | |
override fun onStart() { | |
super.onStart() | |
if (hasOverlayPermission()) { | |
val intent = Intent(this, FloatingAppService::class.java) | |
.setAction(FloatingAppService.ACTION_STOP) | |
startService(intent) | |
} else { | |
requestOverlayPermission(REQUEST_OVERLAY_PERMISSION) | |
} | |
} | |
override fun onStop() { | |
super.onStop() | |
if (enabled && hasOverlayPermission()) { | |
val intent = Intent(this, FloatingAppService::class.java) | |
.setAction(FloatingAppService.ACTION_START) | |
startService(intent) | |
} | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (resultCode == RESULT_OK) { | |
when (requestCode) { | |
REQUEST_OVERLAY_PERMISSION -> Log.d(TAG, "enable overlay permission") | |
} | |
} | |
} | |
override fun onTouchEvent(event: MotionEvent?): Boolean { | |
enabled = false | |
return super.onTouchEvent(event) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment