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 
17 package com.android.cts.deviceandprofileowner;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.os.Bundle;
23 
24 import java.util.concurrent.CountDownLatch;
25 import java.util.concurrent.TimeUnit;
26 
27 /**
28  * Wrapper class used to call the activity in the non-test APK and wait for its result.
29  */
30 public class AutofillActivity extends Activity {
31 
32     private static final String AUTOFILL_PACKAGE_NAME = "com.android.cts.devicepolicy.autofillapp";
33     private static final String AUTOFILL_ACTIVITY_NAME = AUTOFILL_PACKAGE_NAME + ".SimpleActivity";
34 
35     private final CountDownLatch mLatch = new CountDownLatch(1);
36     private boolean mEnabled;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41 
42         final Intent launchIntent = new Intent();
43         launchIntent.setComponent(new ComponentName(AUTOFILL_PACKAGE_NAME, AUTOFILL_ACTIVITY_NAME));
44         startActivityForResult(launchIntent, 42);
45     }
46 
47     @Override
onActivityResult(int requestCode, int resultCode, Intent data)48     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
49         mEnabled = resultCode == 1;
50         mLatch.countDown();
51     }
52 
isAutofillEnabled()53     public boolean isAutofillEnabled() throws InterruptedException {
54         final boolean called = mLatch.await(2, TimeUnit.SECONDS);
55         if (!called) {
56             throw new IllegalStateException(AUTOFILL_ACTIVITY_NAME + " didn't finish in 2 seconds");
57         }
58         finish();
59         return mEnabled;
60     }
61 }
62