Last active
April 21, 2019 06:05
-
-
Save yogeshgosavi/aad2d6d6e83273ffe6992f94bd8e9bd3 to your computer and use it in GitHub Desktop.
Floating App for Android using KOTLIN optimised for newer android versions too
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"/> | |
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> | |
<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 | |
// all credits goes to this guy https://gist.github.com/nosix/c3baf62bb095410a812ac638413c457d | |
// i just optimised it for my usages | |
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.* | |
import android.content.Context | |
import android.content.Intent | |
import android.graphics.Color | |
import android.os.Build | |
import android.os.IBinder | |
import android.support.annotation.RequiresApi | |
import android.support.v4.app.NotificationCompat | |
import android.view.WindowManager | |
import android.widget.ImageView | |
import java.util.* | |
import xxx | |
// all credits goes to this guy https://gist.github.com/nosix/c3baf62bb095410a812ac638413c457d | |
// i just optimised it for my usages | |
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 channelId = | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
createNotificationChannel("my_service", "My Background Service") | |
} else { | |
// If earlier version channel ID is not used | |
// https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context) | |
"" | |
} | |
val activityIntent = Intent(this, MainActivity::class.java) | |
val pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, 0) | |
val notificationBuilder = NotificationCompat.Builder(this, channelId ) | |
val notification = notificationBuilder.setOngoing(true) | |
.setContentIntent(pendingIntent) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle(FloatingAppService::class.simpleName) | |
.setContentText("Service is running.") | |
.build() | |
startForeground(101, notification) | |
} | |
@RequiresApi(Build.VERSION_CODES.O) | |
private fun createNotificationChannel(channelId: String, channelName: String): String{ | |
val chan = NotificationChannel(channelId, | |
channelName, NotificationManager.IMPORTANCE_NONE) | |
chan.lightColor = Color.BLUE | |
chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE | |
val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager | |
service.createNotificationChannel(chan) | |
return channelId | |
} | |
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.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.util.Log | |
import android.view.MotionEvent | |
// all credits goes to this guy https://gist.github.com/nosix/c3baf62bb095410a812ac638413c457d | |
// i just optimised it for my usages | |
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
All credits go to this guy https://gist.github.com/nosix/c3baf62bb095410a812ac638413c457d
I just optimized it for my use