1 /*
2  * Copyright (C) 2021 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 com.android.cts.overlay.target;
18 
19 import android.app.Activity;
20 import android.app.KeyguardManager;
21 import android.content.Intent;
22 import android.content.res.Configuration;
23 import android.os.Bundle;
24 import android.view.WindowManager;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 
29 import java.util.function.BiConsumer;
30 
31 /**
32  * A test activity to verify that the assets paths configuration changes are received if the
33  * overlay targeting state is changed.
34  */
35 public class OverlayTargetActivity extends Activity {
36     private BiConsumer<OverlayTargetActivity, Configuration> mConfigurationChangedCallback;
37 
38     /**
39      * A boolean value to determine whether a stub service can be started when the activity
40      * is launched.
41      */
42     public static final String EXTRA_START_SERVICE =
43             "com.android.cts.overlay.intent.extra.START_SERVICE";
44 
45     @Override
onCreate(@ullable Bundle savedInstanceState)46     protected void onCreate(@Nullable Bundle savedInstanceState) {
47         super.onCreate(savedInstanceState);
48         keepScreenOn();
49         if (savedInstanceState == null) {
50             startServiceIfNecessary(getIntent());
51         }
52     }
53 
54     @Override
onConfigurationChanged(@onNull Configuration newConfig)55     public void onConfigurationChanged(@NonNull Configuration newConfig) {
56         super.onConfigurationChanged(newConfig);
57         final BiConsumer<OverlayTargetActivity, Configuration> callback =
58                 mConfigurationChangedCallback;
59         if (callback != null) {
60             callback.accept(this, newConfig);
61         }
62     }
63 
64     /** Registers the callback of onConfigurationChanged. */
setConfigurationChangedCallback( BiConsumer<OverlayTargetActivity, Configuration> callbacks)65     public void setConfigurationChangedCallback(
66             BiConsumer<OverlayTargetActivity, Configuration> callbacks) {
67         mConfigurationChangedCallback = callbacks;
68     }
69 
keepScreenOn()70     private void keepScreenOn() {
71         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
72         setTurnScreenOn(true);
73         KeyguardManager km = getSystemService(KeyguardManager.class);
74         if (km != null) {
75             km.requestDismissKeyguard(this, null);
76         }
77     }
78 
startServiceIfNecessary(Intent intent)79     private void startServiceIfNecessary(Intent intent) {
80         if (intent == null) {
81             return;
82         }
83         final boolean startService = intent.getBooleanExtra(EXTRA_START_SERVICE, false);
84         if (!startService) {
85             return;
86         }
87         final Intent serviceIntent = new Intent(this, OverlayTargetService.class);
88         startService(serviceIntent);
89     }
90 }
91