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.verifier.location.base; 18 19 import android.app.AlertDialog; 20 import android.content.DialogInterface; 21 import android.location.cts.GnssTestCase; 22 import android.os.Bundle; 23 import android.util.Log; 24 import android.view.View; 25 import android.view.LayoutInflater; 26 import android.widget.Button; 27 import android.widget.EditText; 28 import android.widget.FrameLayout; 29 import android.widget.Toast; 30 import com.android.cts.verifier.R; 31 import com.android.cts.verifier.TestResult; 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * An Activity that allows Gnss CTS tests to be executed inside CtsVerifier. 36 * 37 * Sub-classes pass the test class as part of construction. 38 * One JUnit test class is executed per Activity, the test class can still be executed outside 39 * CtsVerifier. 40 */ 41 public abstract class EmergencyCallBaseTestActivity extends GnssCtsTestActivity { 42 private static final String PHONE_NUMBER_KEY = "android.cts.emergencycall.phonenumber"; 43 private static final String defaultPhonePackageName = "com.google.android.dialer"; 44 45 /** 46 * Constructor for a CTS test executor. It will execute a standalone CTS test class. 47 * 48 * @param testClass The test class to execute, it must be a subclass of {@link AndroidTestCase}. 49 */ 50 protected EmergencyCallBaseTestActivity(Class<? extends GnssTestCase> testClass) { 51 super(testClass, R.layout.gnss_emergency_test); 52 } 53 54 protected abstract long getPhoneCallDurationMs(); 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 60 // override the test info 61 mTextView.setText(R.string.location_emergency_call_test_info); 62 EmergencyCallUtil.setDefaultDialer(this, this.getPackageName()); 63 setPassFailButtonClickListeners(); 64 65 } 66 67 @Override 68 protected void onDestroy() { 69 super.onDestroy(); 70 EmergencyCallUtil.setDefaultDialer(this, defaultPhonePackageName); 71 } 72 73 protected abstract boolean showLocalNumberInputbox(); 74 75 @Override 76 public void onClick(View target) { 77 // skip current test if device doesn't support cellular 78 if (!EmergencyCallUtil.isPhoneDevice(this)) { 79 String skipInfo = getResources().getString(R.string.emergency_call_skip_info); 80 TestResult.setPassedResult(this, super.getClass().getName(), skipInfo); 81 Toast toast = Toast.makeText(getApplicationContext(), skipInfo, Toast.LENGTH_LONG); 82 toast.show(); 83 this.finish(); 84 return; 85 } 86 AlertDialog.Builder builder = new AlertDialog.Builder(this); 87 final FrameLayout frameView = new FrameLayout(this); 88 builder.setView(frameView); 89 90 final boolean enableLocalNumberInputBox = showLocalNumberInputbox(); 91 final AlertDialog alertDialog = builder.create(); 92 LayoutInflater inflater = alertDialog.getLayoutInflater(); 93 94 View dialogView; 95 if (enableLocalNumberInputBox) { 96 dialogView = 97 inflater.inflate(R.layout.emergency_call_msg_test_confirm_dialog, frameView); 98 } else { 99 dialogView = inflater.inflate(R.layout.emergency_call_confirm_dialog, frameView); 100 } 101 final EditText targetNumberEditText = 102 (EditText) dialogView.findViewById(R.id.emergency_number); 103 final Button dialButton = (Button) dialogView.findViewById(R.id.dial_button); 104 dialButton.setOnClickListener(new Button.OnClickListener() { 105 @Override 106 public void onClick(View v) { 107 if (enableLocalNumberInputBox) { 108 final EditText currentNumberEditText = 109 (EditText) dialogView.findViewById(R.id.local_phone_number); 110 String currentNumber = currentNumberEditText.getText().toString(); 111 // pass the number to cts tests for cts verifier UI, through System property. 112 System.setProperty(PHONE_NUMBER_KEY, currentNumber); 113 } 114 int targetPhoneNumber = 115 Integer.parseInt(targetNumberEditText.getText().toString()); 116 long callDurationMs = EmergencyCallBaseTestActivity.this.getPhoneCallDurationMs(); 117 EmergencyCallUtil.makePhoneCall( 118 EmergencyCallBaseTestActivity.this, targetPhoneNumber); 119 EmergencyCallBaseTestActivity.super.onClick(target); 120 EmergencyCallUtil.endCallWithDelay( 121 EmergencyCallBaseTestActivity.this.getApplicationContext(), callDurationMs); 122 alertDialog.dismiss(); 123 } 124 }); 125 alertDialog.show(); 126 } 127 } 128