diff options
| author | Peter Visontay <pvisontay@google.com> | 2017-12-14 14:02:53 +0000 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-12-14 14:02:53 +0000 |
| commit | e746637688399f65fc5dcfe9b49705b0fbf892bc (patch) | |
| tree | bf1a5df307e88e5fd719e5cf61778c840ba4466c /samples/ApiDemos | |
| parent | 18faccea6b1578b0c7c0f0fbc7ffa7240137ba6a (diff) | |
| parent | ad360d40be811afe289ca7c250e593890b33bfb6 (diff) | |
| download | android_development-e746637688399f65fc5dcfe9b49705b0fbf892bc.tar.gz android_development-e746637688399f65fc5dcfe9b49705b0fbf892bc.tar.bz2 android_development-e746637688399f65fc5dcfe9b49705b0fbf892bc.zip | |
Merge "Add an ApiDemo for showing an overlay window."
Diffstat (limited to 'samples/ApiDemos')
| -rw-r--r-- | samples/ApiDemos/AndroidManifest.xml | 9 | ||||
| -rw-r--r-- | samples/ApiDemos/res/layout/overlay_window.xml | 41 | ||||
| -rw-r--r-- | samples/ApiDemos/src/com/example/android/apis/view/OverlayWindowActivity.java | 130 |
3 files changed, 180 insertions, 0 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml index ff65b75e0..030184982 100644 --- a/samples/ApiDemos/AndroidManifest.xml +++ b/samples/ApiDemos/AndroidManifest.xml @@ -41,6 +41,8 @@ <uses-permission android:name="android.permission.WAKE_LOCK"/> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/> + <!-- Required for drawing overlay windows (TYPE_SYSTEM_ALERT, TYPE_APPLICATION_OVERLAY) --> + <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <!-- For android.media.audiofx.Visualizer --> <uses-permission android:name="android.permission.RECORD_AUDIO" /> @@ -2530,6 +2532,13 @@ </intent-filter> </activity> + <activity android:name=".view.OverlayWindowActivity" android:label="Views/Overlay Window"> + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.SAMPLE_CODE" /> + </intent-filter> + </activity> + <activity android:name=".view.RatingBar1" android:label="Views/Rating Bar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> diff --git a/samples/ApiDemos/res/layout/overlay_window.xml b/samples/ApiDemos/res/layout/overlay_window.xml new file mode 100644 index 000000000..ffa3fde71 --- /dev/null +++ b/samples/ApiDemos/res/layout/overlay_window.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2017 The Android Open Source Project + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> + + +<!-- Demonstrates displaying overlay windows. --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" + android:gravity="center_horizontal" + android:layout_width="match_parent" android:layout_height="match_parent"> + + <TextView + android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_weight="0" + android:paddingBottom="4dip" + android:textAppearance="?android:attr/textAppearanceMedium" + android:text="Demonstrates displaying overlay windows (on top of other apps)."/> + + <Button android:id="@+id/show_overlay" + android:layout_width="wrap_content" android:layout_height="wrap_content" + android:text="Show Overlay"> + </Button> + + <Button android:id="@+id/hide_overlay" + android:layout_width="wrap_content" android:layout_height="wrap_content" + android:text="Hide Overlay"> + </Button> + +</LinearLayout>
\ No newline at end of file diff --git a/samples/ApiDemos/src/com/example/android/apis/view/OverlayWindowActivity.java b/samples/ApiDemos/src/com/example/android/apis/view/OverlayWindowActivity.java new file mode 100644 index 000000000..df6c91440 --- /dev/null +++ b/samples/ApiDemos/src/com/example/android/apis/view/OverlayWindowActivity.java @@ -0,0 +1,130 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example.android.apis.view; + +import com.example.android.apis.R; + +import android.app.Activity; +import android.content.Context; +import android.content.Intent; +import android.graphics.Color; +import android.graphics.PixelFormat; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.provider.Settings; +import android.view.Gravity; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.WindowManager; +import android.view.WindowManager.LayoutParams; +import android.widget.Button; +import android.widget.TextView; + +/** + * Demonstrates the display of overlay windows, i.e. windows that are drawn on top of other apps. + */ +public class OverlayWindowActivity extends Activity { + + private static int MANAGE_OVERLAY_PERMISSION_REQUEST_CODE = 1; + private View mOverlayView; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.overlay_window); + + Button button = (Button)findViewById(R.id.show_overlay); + button.setOnClickListener(mShowOverlayListener); + button = (Button)findViewById(R.id.hide_overlay); + button.setOnClickListener(mHideOverlayListener); + } + + private OnClickListener mShowOverlayListener = new OnClickListener() { + @Override + public void onClick(View view) { + if (Settings.canDrawOverlays(OverlayWindowActivity.this)) { + drawOverlay(); + } else { + // Need to ask the user's permission first. We'll redirect them to Settings. + Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, + Uri.parse("package:" + getPackageName())); + startActivityForResult(intent, MANAGE_OVERLAY_PERMISSION_REQUEST_CODE); + } + } + }; + + private OnClickListener mHideOverlayListener = new OnClickListener() { + @Override + public void onClick(View view) { + if (mOverlayView != null) { + WindowManager wm = getWindowManager(); + wm.removeView(mOverlayView); + mOverlayView = null; + } + } + }; + + /** + * This is called after the user chooses whether they grant permission to the app to display + * overlays or not. + */ + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) { + // Check if the user granted permission to draw overlays. + if (Settings.canDrawOverlays(this)) { + drawOverlay(); + } + } + } + + private void drawOverlay() { + if (mOverlayView != null) { + // Already shown. + return; + } + + TextView textView = new TextView(this); + textView.setText("I'm an overlay"); + textView.setBackgroundColor(Color.WHITE); + textView.setTextColor(Color.BLACK); + textView.setPadding(10, 10, 10, 10); + + WindowManager wm = getWindowManager(); + LayoutParams params = new LayoutParams(); + + params.type = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + ? LayoutParams.TYPE_APPLICATION_OVERLAY + : LayoutParams.TYPE_PHONE; + params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; + params.format = PixelFormat.TRANSPARENT; + + params.width = LayoutParams.WRAP_CONTENT; + params.height = LayoutParams.WRAP_CONTENT; + // Snap to the upper right corner of the screen. + params.gravity = Gravity.TOP | Gravity.RIGHT; + // Set position relative to upper right corner. + params.x = 10; + params.y = 10; + + wm.addView(textView, params); + mOverlayView = textView; + } +} |
