Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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.telephony.euicc.cts;
     18 
     19 import android.app.Activity;
     20 import android.app.PendingIntent;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentSender;
     24 import android.os.Bundle;
     25 import android.telephony.euicc.EuiccManager;
     26 
     27 /**
     28  * A dummy activity started by {@link EuiccManagerTest#testStartResolutionActivity()} for testing
     29  * {@link android.telephony.euicc.EuiccManager#startResolutionActivity(Activity, int, Intent,
     30  * PendingIntent)}. Sends {@link EuiccTestResolutionActivity#RESULT_CODE_TEST_PASSED} if the
     31  * resolution activity is successfully started, {@link
     32  * EuiccTestResolutionActivity#RESULT_CODE_TEST_FAILED} otherwise.
     33  */
     34 public class EuiccTestResolutionActivity extends Activity {
     35 
     36     public static final String EXTRA_ACTIVITY_CALLBACK_INTENT = "extra_activity_callback_intent";
     37     public static final int RESULT_CODE_TEST_PASSED = 101;
     38     public static final int RESULT_CODE_TEST_FAILED = 102;
     39 
     40     private static final int REQUEST_CODE = 100;
     41     private static final String ACTION_START_RESOLUTION_ACTIVITY = "cts_start_resolution_activity";
     42 
     43     private PendingIntent mCallbackIntent;
     44 
     45     @Override
     46     protected void onCreate(Bundle savedInstanceState) {
     47         super.onCreate(savedInstanceState);
     48         mCallbackIntent = getIntent().getParcelableExtra(EXTRA_ACTIVITY_CALLBACK_INTENT);
     49     }
     50 
     51     @Override
     52     protected void onResume() {
     53         super.onResume();
     54         testStartResolutionActivity();
     55     }
     56 
     57     @Override
     58     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     59         super.onActivityResult(requestCode, resultCode, data);
     60         if (requestCode == REQUEST_CODE) {
     61             sendCallbackAndFinish(
     62                     resultCode == RESULT_OK ? RESULT_CODE_TEST_PASSED : RESULT_CODE_TEST_FAILED);
     63         }
     64     }
     65 
     66     private void testStartResolutionActivity() {
     67         EuiccManager euiccManager = (EuiccManager) getSystemService(Context.EUICC_SERVICE);
     68 
     69         // specify activity to start
     70         Intent resolutionActivityIntent =
     71                 new Intent(getApplicationContext(), EuiccResolutionActivity.class);
     72         PendingIntent resolutionIntent =
     73                 PendingIntent.getActivity(
     74                         getApplicationContext(),
     75                         0 /* requestCode */,
     76                         resolutionActivityIntent,
     77                         PendingIntent.FLAG_ONE_SHOT);
     78 
     79         // add pending intent to extra
     80         Intent resultIntent = new Intent();
     81         resultIntent.putExtra(
     82                 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_RESOLUTION_INTENT, resolutionIntent);
     83 
     84         // call startResolutionActivity()
     85         PendingIntent callbackIntent = createCallbackIntent(ACTION_START_RESOLUTION_ACTIVITY);
     86         try {
     87             euiccManager.startResolutionActivity(this, REQUEST_CODE, resultIntent, callbackIntent);
     88         } catch (IntentSender.SendIntentException e) {
     89             sendCallbackAndFinish(RESULT_CODE_TEST_FAILED);
     90         }
     91     }
     92 
     93     private PendingIntent createCallbackIntent(String action) {
     94         Intent intent = new Intent(action);
     95         return PendingIntent.getBroadcast(
     96                 getApplicationContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
     97     }
     98 
     99     private void sendCallbackAndFinish(int resultCode) {
    100         if (mCallbackIntent != null) {
    101             try {
    102                 mCallbackIntent.send(resultCode);
    103             } catch (PendingIntent.CanceledException e) {
    104                 // Caller canceled the callback; do nothing.
    105             }
    106         }
    107         finish();
    108     }
    109 }
    110