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.phone.testapps.telephonyregistry; 18 19 import android.app.Activity; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.app.NotificationManager; 23 import android.os.Bundle; 24 import android.telephony.CellInfo; 25 import android.telephony.CellLocation; 26 import android.telephony.PhoneStateListener; 27 import android.telephony.ServiceState; 28 import android.telephony.TelephonyManager; 29 import android.util.SparseArray; 30 import android.widget.Button; 31 import android.widget.CheckBox; 32 import android.widget.LinearLayout; 33 import android.widget.Toast; 34 35 import java.util.List; 36 import java.util.stream.Collectors; 37 38 public class TelephonyRegistryTestApp extends Activity { 39 private TelephonyManager telephonyManager; 40 private NotificationManager notificationManager; 41 private int mSelectedEvents = 0; 42 private static final String NOTIFICATION_CHANNEL = "registryUpdate"; 43 44 private static final SparseArray<String> EVENTS = new SparseArray<String>() {{ 45 put(PhoneStateListener.LISTEN_SERVICE_STATE, "SERVICE_STATE"); 46 put(PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR, "MESSAGE_WAITING_INDICATOR"); 47 put(PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR, "CALL_FORWARDING_INDICATOR"); 48 put(PhoneStateListener.LISTEN_CELL_LOCATION, "CELL_LOCATION"); 49 put(PhoneStateListener.LISTEN_CALL_STATE, "CALL_STATE"); 50 put(PhoneStateListener.LISTEN_DATA_CONNECTION_STATE, "DATA_CONNECTION_STATE"); 51 put(PhoneStateListener.LISTEN_DATA_ACTIVITY, "DATA_ACTIVITY"); 52 put(PhoneStateListener.LISTEN_SIGNAL_STRENGTHS, "SIGNAL_STRENGTHS"); 53 put(PhoneStateListener.LISTEN_OTASP_CHANGED, "OTASP_CHANGED"); 54 put(PhoneStateListener.LISTEN_CELL_INFO, "CELL_INFO"); 55 put(PhoneStateListener.LISTEN_PRECISE_CALL_STATE, "PRECISE_CALL_STATE"); 56 put(PhoneStateListener.LISTEN_PRECISE_DATA_CONNECTION_STATE, 57 "PRECISE_DATA_CONNECTION_STATE"); 58 put(PhoneStateListener.LISTEN_SRVCC_STATE_CHANGED, "SRVCC_STATE"); 59 put(PhoneStateListener.LISTEN_CARRIER_NETWORK_CHANGE, "CARRIER_NETWORK_CHANGE"); 60 put(PhoneStateListener.LISTEN_VOICE_ACTIVATION_STATE, "VOICE_ACTIVATION_STATE"); 61 put(PhoneStateListener.LISTEN_DATA_ACTIVATION_STATE, "DATA_ACTIVATION_STATE"); 62 }}; 63 64 private final PhoneStateListener phoneStateListener = new PhoneStateListener() { 65 @Override 66 public void onCellLocationChanged(CellLocation location) { 67 notify("onCellLocationChanged", location); 68 } 69 70 @Override 71 public void onCellInfoChanged(List<CellInfo> cellInfo) { 72 notify("onCellInfoChanged", cellInfo); 73 } 74 75 @Override 76 public void onSrvccStateChanged(int srvccState) { 77 notify("onSrvccStateChanged", srvccState); 78 } 79 80 @Override 81 public void onServiceStateChanged(ServiceState state) { 82 notify("onServiceStateChanged", state); 83 } 84 85 private void notify(String method, Object data) { 86 Notification.Builder builder = new Notification.Builder(TelephonyRegistryTestApp.this, 87 NOTIFICATION_CHANNEL); 88 Notification notification = builder.setSmallIcon(android.R.drawable.sym_def_app_icon) 89 .setContentTitle("Registry update: " + method) 90 .setContentText(data == null ? "null" : data.toString()) 91 .build(); 92 notificationManager.notify(0, notification); 93 } 94 }; 95 96 @Override onCreate(Bundle savedInstanceState)97 protected void onCreate(Bundle savedInstanceState) { 98 super.onCreate(savedInstanceState); 99 setContentView(R.layout.activity_main); 100 101 telephonyManager = getSystemService(TelephonyManager.class); 102 103 LinearLayout eventContainer = (LinearLayout) findViewById(R.id.events); 104 for (int i = 0; i < EVENTS.size(); i++) { 105 CheckBox box = new CheckBox(this); 106 box.setText(EVENTS.valueAt(i)); 107 final int eventCode = EVENTS.keyAt(i); 108 box.setOnCheckedChangeListener((buttonView, isChecked) -> { 109 if (buttonView.isChecked()) { 110 mSelectedEvents |= eventCode; 111 } else { 112 mSelectedEvents &= ~eventCode; 113 } 114 }); 115 eventContainer.addView(box); 116 } 117 118 Button registerButton = (Button) findViewById(R.id.registerButton); 119 registerButton.setOnClickListener(v -> 120 telephonyManager.listen(phoneStateListener, mSelectedEvents)); 121 122 Button queryCellLocationButton = findViewById(R.id.queryCellLocationButton); 123 queryCellLocationButton.setOnClickListener(v -> { 124 List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); 125 String cellInfoText; 126 if (cellInfos == null || cellInfos.size() == 0) { 127 cellInfoText = "null"; 128 } else { 129 cellInfoText = cellInfos.stream().map(CellInfo::toString) 130 .collect(Collectors.joining(",")); 131 } 132 Toast.makeText(TelephonyRegistryTestApp.this, "queryCellInfo: " + cellInfoText, 133 Toast.LENGTH_SHORT).show(); 134 }); 135 136 notificationManager = getSystemService(NotificationManager.class); 137 NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, 138 "Telephony Registry updates", NotificationManager.IMPORTANCE_HIGH); 139 notificationManager.createNotificationChannel(channel); 140 } 141 } 142