|
| 1 | +package com.symphonyrecords.debugger; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Intent; |
| 5 | +import android.net.Uri; |
| 6 | +import android.os.Build; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.provider.Settings; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import androidx.core.content.ContextCompat; |
| 12 | + |
| 13 | +import com.symphonyrecords.debugger.DebuggerService; |
| 14 | + |
| 15 | + |
| 16 | +public class MainActivity extends Activity { |
| 17 | + |
| 18 | + public final static int Overlay_REQUEST_CODE = 251; |
| 19 | + |
| 20 | + @Override |
| 21 | + protected void onCreate(Bundle savedInstanceState) { |
| 22 | + super.onCreate(savedInstanceState); |
| 23 | + |
| 24 | + checkDrawOverlayPermission(this); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + public void checkDrawOverlayPermission(Activity activity) { |
| 29 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 30 | + if (!Settings.canDrawOverlays(this)) { |
| 31 | + if (null != activity) { |
| 32 | + Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); |
| 33 | + activity.startActivityForResult(intent, Overlay_REQUEST_CODE); |
| 34 | + } else { |
| 35 | + Toast.makeText(this, "Please grant \"Draw over other apps\" permission under application settings", Toast.LENGTH_LONG).show(); |
| 36 | + } |
| 37 | + } else { |
| 38 | + openFloatingWindow(); |
| 39 | + } |
| 40 | + } else { |
| 41 | + openFloatingWindow(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private void openFloatingWindow() { |
| 46 | + Intent intent = new Intent(this, DebuggerService.class); |
| 47 | + this.stopService(intent); |
| 48 | + ContextCompat.startForegroundService(this, intent); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 54 | + super.onActivityResult(requestCode, resultCode, data); |
| 55 | + if (resultCode == RESULT_OK) { |
| 56 | + if (requestCode == Overlay_REQUEST_CODE) { |
| 57 | + if (Build.VERSION.SDK_INT >= 23) { |
| 58 | + if (Settings.canDrawOverlays(this)) { |
| 59 | + openFloatingWindow(); |
| 60 | + } |
| 61 | + } else { |
| 62 | + openFloatingWindow(); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | +} |
0 commit comments