• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.test.util.esimutility;
18 
19 import android.app.Activity;
20 import android.app.Instrumentation;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.telephony.TelephonyManager;
24 import android.util.Log;
25 
26 public class ESimUtilityInstrumentation extends Instrumentation {
27     private static final String TAG = ESimUtilityInstrumentation.class.getCanonicalName();
28     private Bundle mArguments;
29 
30     @Override
onCreate(Bundle arguments)31     public void onCreate(Bundle arguments) {
32         super.onCreate(arguments);
33         mArguments = arguments;
34         start();
35     }
36 
37     @Override
onStart()38     public void onStart() {
39         super.onStart();
40         try {
41             Log.d(TAG, "starting instrumentation");
42             TelephonyManager telephonyManager = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
43             updateStart();
44             telephonyManager.setSimPowerState(getState());
45             Log.d(TAG, "ending instrumentation");
46             updateAllGood();
47         } catch (Exception e) {
48             updateWithException(e);
49         }
50     }
51 
updateAllGood()52     private void updateAllGood() {
53         Bundle allGood = new Bundle();
54         allGood.putString("all_is", "good");
55         finish(Activity.RESULT_OK, allGood);
56     }
57 
updateWithException(Exception e)58     private void updateWithException(Exception e) {
59         Bundle result = new Bundle();
60         result.putString("sim_utility_exception", e.getMessage());
61         StackTraceElement[] elements = e.getStackTrace();
62         StringBuilder builder = new StringBuilder();
63         for (StackTraceElement element: elements) {
64             builder.append(System.lineSeparator());
65             builder.append("        ").append(element.toString());
66         }
67         result.putString("sim_utility_exception_stack_trace", builder.toString());
68         finish(Activity.RESULT_CANCELED, result);
69     }
70 
updateStart()71     private void updateStart() {
72         Bundle result = new Bundle();
73         result.putString("setting_state_to", getRawState());
74         sendStatus(Activity.RESULT_OK, result);
75     }
76 
getRawState()77     public String getRawState() {
78         return mArguments.getString("state", "no-provided");
79     }
80 
getState()81     public int getState() {
82         switch (getRawState()) {
83             case "down":
84                 return TelephonyManager.CARD_POWER_DOWN;
85             case "up":
86                 return TelephonyManager.CARD_POWER_UP;
87             case "pass-through":
88                 return TelephonyManager.CARD_POWER_UP_PASS_THROUGH;
89         }
90 
91         throw new IllegalArgumentException(
92                 "Invalid or missing state option. Use as: -e state " + "up/down/pass-through");
93     }
94 }
95