Home | History | Annotate | Download | only in deviceandprofileowner
      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 PrintActivity extends Activity {
     31 
     32     private static final String PRINTING_PACKAGE = "com.android.cts.devicepolicy.printingapp";
     33     private static final String PRINT_ACTIVITY = PRINTING_PACKAGE + ".PrintActivity";
     34     private static final String EXTRA_ERROR_MESSAGE = "error_message";
     35 
     36     private final CountDownLatch mLatch = new CountDownLatch(1);
     37     private String mErrorMessage;
     38 
     39     @Override
     40     protected void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42         startActivityForResult(
     43                 new Intent().setComponent(new ComponentName(PRINTING_PACKAGE, PRINT_ACTIVITY)), 0);
     44     }
     45 
     46     @Override
     47     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     48         // Use RESULT_FIRST_USER for success to avoid false positives.
     49         if (resultCode != RESULT_FIRST_USER) {
     50             if (data != null) {
     51                 mErrorMessage = data.getStringExtra(EXTRA_ERROR_MESSAGE);
     52             }
     53             if (mErrorMessage == null) {
     54                 mErrorMessage = "Unknown error, resultCode: " + resultCode;
     55             }
     56         }
     57         mLatch.countDown();
     58     }
     59 
     60     public String getErrorMessage() throws InterruptedException {
     61         final boolean called = mLatch.await(4, TimeUnit.SECONDS);
     62         if (!called) {
     63             throw new IllegalStateException("PrintActivity didn't finish in time");
     64         }
     65         finish();
     66         return mErrorMessage;
     67     }
     68 }
     69