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 package com.android.server.telecom.testapps; 17 18 import android.app.Activity; 19 import android.app.ProgressDialog; 20 import android.content.Context; 21 import android.os.AsyncTask; 22 import android.os.Bundle; 23 import android.telephony.ImsiEncryptionInfo; 24 import android.telephony.TelephonyManager; 25 import android.text.TextUtils; 26 import android.util.Base64; 27 import android.util.Log; 28 import android.view.View; 29 import android.view.View.OnClickListener; 30 import android.widget.ArrayAdapter; 31 import android.widget.EditText; 32 import android.widget.ListView; 33 import android.widget.Toast; 34 35 import org.json.JSONArray; 36 import org.json.JSONException; 37 import org.json.JSONObject; 38 39 import java.io.BufferedInputStream; 40 import java.io.BufferedReader; 41 import java.io.IOException; 42 import java.io.InputStream; 43 import java.io.InputStreamReader; 44 import java.net.HttpURLConnection; 45 import java.net.MalformedURLException; 46 import java.net.ProtocolException; 47 import java.net.URL; 48 import java.util.ArrayList; 49 import java.util.Date; 50 51 public class TestCertActivity extends Activity { 52 53 private EditText mCertUrlView; 54 public static final String LOG_TAG = "TestCertActivity"; 55 56 private ProgressDialog progressDialog; 57 private ArrayList<String> keyList = new ArrayList<String>(); 58 59 // URL to get the json 60 private String mURL = ""; 61 62 @Override onCreate(Bundle savedInstanceState)63 protected void onCreate(Bundle savedInstanceState) { 64 super.onCreate(savedInstanceState); 65 setContentView(R.layout.testcert_main); 66 findViewById(R.id.get_key_button).setOnClickListener(new OnClickListener() { 67 @Override 68 public void onClick(View v) { 69 new GetKeys().execute(); 70 } 71 }); 72 73 mCertUrlView = (EditText) findViewById(R.id.text); 74 mCertUrlView.setText(mURL); 75 } 76 77 /** 78 * Class to get json by making HTTP call 79 */ 80 private class GetKeys extends AsyncTask<Void, Void, Void> { 81 82 @Override onPreExecute()83 protected void onPreExecute() { 84 super.onPreExecute(); 85 progressDialog = new ProgressDialog(TestCertActivity.this); 86 progressDialog.setMessage("Downloading..."); 87 progressDialog.setCancelable(false); 88 progressDialog.show(); 89 } 90 getCertificateList()91 public String getCertificateList() { 92 String response = null; 93 String mURL = mCertUrlView.getText().toString(); 94 try { 95 URL url = new URL(mURL); 96 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 97 conn.setRequestMethod("GET"); 98 // read the response 99 InputStream in = new BufferedInputStream(conn.getInputStream()); 100 response = convertToString(in); 101 } catch (ProtocolException e) { 102 Log.e(LOG_TAG, "ProtocolException: " + e.getMessage()); 103 } catch (MalformedURLException e) { 104 Log.e(LOG_TAG, "MalformedURLException: " + e.getMessage()); 105 } catch (IOException e) { 106 Log.e(LOG_TAG, "IOException: " + e.getMessage()); 107 } catch (Exception e) { 108 Log.e(LOG_TAG, "Exception: " + e.getMessage()); 109 } 110 return response; 111 } 112 convertToString(InputStream is)113 private String convertToString(InputStream is) { 114 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 115 StringBuilder sb = new StringBuilder(); 116 117 String line; 118 try { 119 while ((line = reader.readLine()) != null) { 120 sb.append(line).append('\n'); 121 } 122 } catch (IOException e) { 123 e.printStackTrace(); 124 } finally { 125 try { 126 is.close(); 127 } catch (IOException e) { 128 e.printStackTrace(); 129 } 130 } 131 return sb.toString(); 132 } 133 savePublicKey(String key, int type, String identifier)134 private void savePublicKey(String key, int type, String identifier) { 135 byte[] keyBytes = Base64.decode(key.getBytes(), Base64.DEFAULT); 136 final TelephonyManager telephonyManager = 137 (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 138 139 String mcc = ""; 140 String mnc = ""; 141 String networkOperator = telephonyManager.getNetworkOperator(); 142 int carrierId = telephonyManager.getSimCarrierId(); 143 if (!TextUtils.isEmpty(networkOperator)) { 144 mcc = networkOperator.substring(0, 3); 145 mnc = networkOperator.substring(3); 146 Log.i(LOG_TAG, "using values for mnc, mcc: " + mnc + "," + mcc + ", carrierId = " 147 + carrierId); 148 } 149 ImsiEncryptionInfo imsiEncryptionInfo = new ImsiEncryptionInfo(mcc, 150 mnc, type, identifier, keyBytes, new Date(), carrierId); 151 telephonyManager.setCarrierInfoForImsiEncryption(imsiEncryptionInfo); 152 keyList.add(imsiEncryptionInfo.getKeyType() + "," + 153 imsiEncryptionInfo.getKeyIdentifier()); 154 Log.i(LOG_TAG,"calling telephonymanager complete"); 155 } 156 157 @Override doInBackground(Void... arg0)158 protected Void doInBackground(Void... arg0) { 159 // Making a request to url and getting response 160 String jsonStr = getCertificateList(); 161 Log.d(LOG_TAG, "Response from url: " + jsonStr); 162 163 if (jsonStr != null) { 164 try { 165 JSONObject jsonObj = new JSONObject(jsonStr); 166 // Getting JSON Array node 167 JSONArray certificates = jsonObj.getJSONArray("certificates"); 168 169 // looping through the certificates 170 for (int i = 0; i < certificates.length(); i++) { 171 JSONObject cert = certificates.getJSONObject(i); 172 String key = cert.getString("key"); 173 int type = cert.getInt("type"); 174 String identifier = cert.getString("identifier"); 175 savePublicKey(key, type, identifier); 176 } 177 } catch (final JSONException e) { 178 Log.e(LOG_TAG, "Json parsing error: " + e.getMessage()); 179 runOnUiThread(new Runnable() { 180 @Override 181 public void run() { 182 Toast.makeText(getApplicationContext(), 183 "Json parsing error: " + e.getMessage(), 184 Toast.LENGTH_LONG) 185 .show(); 186 } 187 }); 188 } 189 } else { 190 Log.e(LOG_TAG, "Unable to get JSON from server " + mURL); 191 runOnUiThread(new Runnable() { 192 @Override 193 public void run() { 194 Toast.makeText(getApplicationContext(), 195 "Unable to get JSON from server!", 196 Toast.LENGTH_LONG) 197 .show(); 198 } 199 }); 200 } 201 return null; 202 } 203 204 @Override onPostExecute(Void result)205 protected void onPostExecute(Void result) { 206 207 super.onPostExecute(result); 208 if (progressDialog.isShowing()) { 209 progressDialog.dismiss(); 210 } 211 ListView listView = (ListView) findViewById(R.id.keylist); 212 ArrayAdapter arrayAdapter = 213 new ArrayAdapter(TestCertActivity.this, R.layout.key_list, keyList); 214 listView.setAdapter(arrayAdapter); 215 } 216 } 217 } 218 219 220