1 /* 2 * Copyright (C) 2020 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 17 package android.server.wm.overlay; 18 19 import static android.server.wm.overlay.Components.OverlayActivity.EXTRA_OPACITY; 20 import static android.server.wm.overlay.Components.OverlayActivity.EXTRA_TOKEN; 21 import static android.server.wm.overlay.Components.OverlayActivity.EXTRA_TOKEN_RECEIVER; 22 import static android.server.wm.overlay.Components.OverlayActivity.EXTRA_TOUCHABLE; 23 import static android.server.wm.overlay.UntrustedTouchTestService.BACKGROUND_COLOR; 24 25 import android.app.Activity; 26 import android.content.Intent; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.os.ResultReceiver; 30 import android.view.View; 31 import android.view.Window; 32 import android.view.WindowManager.LayoutParams; 33 34 import androidx.annotation.Nullable; 35 36 /** This is an activity for which android:windowIsTranslucent is true. */ 37 public class OverlayActivity extends Activity { 38 @Override onCreate(@ullable Bundle savedInstanceState)39 protected void onCreate(@Nullable Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 View view = new View(this); 42 view.setBackgroundColor(BACKGROUND_COLOR); 43 setContentView(view); 44 Window window = getWindow(); 45 Intent intent = getIntent(); 46 window.getAttributes().alpha = intent.getFloatExtra(EXTRA_OPACITY, 1f); 47 if (!intent.getBooleanExtra(EXTRA_TOUCHABLE, false)) { 48 window.addFlags(LayoutParams.FLAG_NOT_TOUCHABLE); 49 } 50 } 51 52 @Override onResume()53 protected void onResume() { 54 super.onResume(); 55 ResultReceiver receiver = getIntent().getParcelableExtra(EXTRA_TOKEN_RECEIVER); 56 if (receiver != null) { 57 // The token field is set as part of resuming the activity (after onResume()), so 58 // posting a runnable to the same thread guarantees that it gets executed when the token 59 // is set. 60 getWindow().getDecorView().post(() -> { 61 Bundle bundle = new Bundle(); 62 bundle.putBinder(EXTRA_TOKEN, getWindow().getAttributes().token); 63 receiver.send(0, bundle); 64 }); 65 } 66 } 67 68 69 } 70