Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2013 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.admin.app;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.view.WindowManager;
     23 
     24 import com.google.common.annotations.VisibleForTesting;
     25 
     26 /**
     27  * Helper {@link Activity} for CTS tests of Device Admin activation. The {@code Activity}
     28  * enables tests to capture the invocations of its {@link #onActivityResult(int, int, Intent)} by
     29  * providing a {@link OnActivityResultListener}.
     30  */
     31 public class CtsDeviceAdminActivationTestActivity extends Activity {
     32     public interface OnActivityResultListener {
     33         void onActivityResult(int requestCode, int resultCode, Intent data);
     34     }
     35 
     36     private OnActivityResultListener mOnActivityResultListener;
     37 
     38     @Override
     39     protected void onCreate(Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41 
     42         // Dismiss keyguard and keep screen on while this Activity is displayed.
     43         // This is needed because on older platforms, when the keyguard is on, onActivityResult is
     44         // not invoked when a Device Admin activation is rejected without user interaction.
     45         getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
     46                 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
     47                 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     48     }
     49 
     50     @VisibleForTesting
     51     public void setOnActivityResultListener(OnActivityResultListener listener) {
     52         mOnActivityResultListener = listener;
     53     }
     54 
     55     @Override
     56     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     57         if (mOnActivityResultListener != null) {
     58             mOnActivityResultListener.onActivityResult(requestCode, resultCode, data);
     59             return;
     60         }
     61 
     62         super.onActivityResult(requestCode, resultCode, data);
     63     }
     64 }
     65