1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.apis.view; 17 18 import com.example.android.apis.R; 19 20 import android.app.Activity; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.graphics.Color; 24 import android.graphics.PixelFormat; 25 import android.net.Uri; 26 import android.os.Build; 27 import android.os.Bundle; 28 import android.provider.Settings; 29 import android.view.Gravity; 30 import android.view.View; 31 import android.view.View.OnClickListener; 32 import android.view.WindowManager; 33 import android.view.WindowManager.LayoutParams; 34 import android.widget.Button; 35 import android.widget.TextView; 36 37 /** 38 * Demonstrates the display of overlay windows, i.e. windows that are drawn on top of other apps. 39 */ 40 public class OverlayWindowActivity extends Activity { 41 42 private static int MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 1; 43 private View mOverlayView; 44 45 @Override onCreate(Bundle savedInstanceState)46 protected void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 setContentView(R.layout.overlay_window); 50 51 Button button = (Button)findViewById(R.id.show_overlay); 52 button.setOnClickListener(mShowOverlayListener); 53 button = (Button)findViewById(R.id.hide_overlay); 54 button.setOnClickListener(mHideOverlayListener); 55 } 56 57 private OnClickListener mShowOverlayListener = new OnClickListener() { 58 @Override 59 public void onClick(View view) { 60 if (Settings.canDrawOverlays(OverlayWindowActivity.this)) { 61 drawOverlay(); 62 } else { 63 // Need to ask the user's permission first. We'll redirect them to Settings. 64 Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, 65 Uri.parse("package:" + getPackageName())); 66 startActivityForResult(intent, MANAGE_OVERLAY_PERMISSION_REQUEST_CODE); 67 } 68 } 69 }; 70 71 private OnClickListener mHideOverlayListener = new OnClickListener() { 72 @Override 73 public void onClick(View view) { 74 if (mOverlayView != null) { 75 WindowManager wm = getWindowManager(); 76 wm.removeView(mOverlayView); 77 mOverlayView = null; 78 } 79 } 80 }; 81 82 /** 83 * This is called after the user chooses whether they grant permission to the app to display 84 * overlays or not. 85 */ 86 @Override onActivityResult(int requestCode, int resultCode, Intent data)87 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 88 if (requestCode == MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) { 89 // Check if the user granted permission to draw overlays. 90 if (Settings.canDrawOverlays(this)) { 91 drawOverlay(); 92 } 93 } 94 } 95 drawOverlay()96 private void drawOverlay() { 97 if (mOverlayView != null) { 98 // Already shown. 99 return; 100 } 101 102 TextView textView = new TextView(this); 103 textView.setText("I'm an overlay"); 104 textView.setBackgroundColor(Color.WHITE); 105 textView.setTextColor(Color.BLACK); 106 textView.setPadding(10, 10, 10, 10); 107 108 WindowManager wm = getWindowManager(); 109 LayoutParams params = new LayoutParams(); 110 111 params.type = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 112 ? LayoutParams.TYPE_APPLICATION_OVERLAY 113 : LayoutParams.TYPE_PHONE; 114 params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 115 | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 116 | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; 117 params.format = PixelFormat.TRANSPARENT; 118 119 params.width = LayoutParams.WRAP_CONTENT; 120 params.height = LayoutParams.WRAP_CONTENT; 121 // Snap to the upper right corner of the screen. 122 params.gravity = Gravity.TOP | Gravity.RIGHT; 123 // Set position relative to upper right corner. 124 params.x = 10; 125 params.y = 10; 126 127 wm.addView(textView, params); 128 mOverlayView = textView; 129 } 130 } 131