From 760729c33e8139f11c2e2e64870d62a169bf5fc8 Mon Sep 17 00:00:00 2001 From: chen xu Date: Thu, 11 Oct 2018 13:18:26 -0700 Subject: [PATCH] refactor radioPowerstate 1. new System API for getRadioPowerState with permision check 2. new System API for onRadioPowerStateChanged in PhoneStateListener 3. replace radioState enum in CommandsInterface to telephonyMgr defined int. Bug: 117349311 Test: telephony unit test Change-Id: I502568280fbf9213ca6bf1f7fb204d0d4ce86580 Merged-in: I502568280fbf9213ca6bf1f7fb204d0d4ce86580 --- api/system-current.txt | 9 ++++ .../com/android/server/TelephonyRegistry.java | 37 +++++++++++++ .../android/telephony/PhoneStateListener.java | 29 ++++++++++ .../android/telephony/TelephonyManager.java | 54 +++++++++++++++++++ .../telephony/IPhoneStateListener.aidl | 1 + .../internal/telephony/ITelephony.aidl | 6 +++ .../telephony/ITelephonyRegistry.aidl | 1 + 7 files changed, 137 insertions(+) diff --git a/api/system-current.txt b/api/system-current.txt index fd5aa7b59741..8c7ca76b1907 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -5137,6 +5137,11 @@ package android.telephony { field public static final int RESULT_SUCCESS = 0; // 0x0 } + public class PhoneStateListener { + method public void onRadioPowerStateChanged(int); + field public static final int LISTEN_RADIO_POWER_STATE_CHANGED = 4194304; // 0x400000 + } + public class ServiceState implements android.os.Parcelable { method public android.telephony.NetworkRegistrationState getNetworkRegistrationState(int, int); method public java.util.List getNetworkRegistrationStates(); @@ -5262,6 +5267,7 @@ package android.telephony { method public boolean getEmergencyCallbackMode(); method public java.lang.String getIsimDomain(); method public int getPreferredNetworkType(int); + method public int getRadioPowerState(); method public int getSimApplicationState(); method public int getSimCardState(); method public java.util.List getTelephonyHistograms(); @@ -5325,6 +5331,9 @@ package android.telephony { field public static final int NETWORK_MODE_TDSCDMA_WCDMA = 14; // 0xe field public static final int NETWORK_MODE_WCDMA_ONLY = 2; // 0x2 field public static final int NETWORK_MODE_WCDMA_PREF = 0; // 0x0 + field public static final int RADIO_POWER_OFF = 0; // 0x0 + field public static final int RADIO_POWER_ON = 1; // 0x1 + field public static final int RADIO_POWER_UNAVAILABLE = 2; // 0x2 field public static final int SIM_ACTIVATION_STATE_ACTIVATED = 2; // 0x2 field public static final int SIM_ACTIVATION_STATE_ACTIVATING = 1; // 0x1 field public static final int SIM_ACTIVATION_STATE_DEACTIVATED = 3; // 0x3 diff --git a/services/core/java/com/android/server/TelephonyRegistry.java b/services/core/java/com/android/server/TelephonyRegistry.java index 0f9fe83ba6a5..09277474fd9c 100644 --- a/services/core/java/com/android/server/TelephonyRegistry.java +++ b/services/core/java/com/android/server/TelephonyRegistry.java @@ -211,6 +211,9 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { private PhoneCapability mPhoneCapability = null; + @TelephonyManager.RadioPowerState + private int mRadioPowerState = TelephonyManager.RADIO_POWER_UNAVAILABLE; + private final LocalLog mLocalLog = new LocalLog(100); private PreciseDataConnectionState mPreciseDataConnectionState = @@ -749,6 +752,13 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { remove(r.binder); } } + if ((events & PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED) != 0) { + try { + r.callback.onRadioPowerStateChanged(mRadioPowerState); + } catch (RemoteException ex) { + remove(r.binder); + } + } } } } else { @@ -1570,6 +1580,32 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { } } + public void notifyRadioPowerStateChanged(@TelephonyManager.RadioPowerState int state) { + if (!checkNotifyPermission("notifyRadioPowerStateChanged()")) { + return; + } + + if (VDBG) { + log("notifyRadioPowerStateChanged: state= " + state); + } + + synchronized (mRecords) { + mRadioPowerState = state; + + for (Record r : mRecords) { + if (r.matchPhoneStateListenerEvent( + PhoneStateListener.LISTEN_RADIO_POWER_STATE_CHANGED)) { + try { + r.callback.onRadioPowerStateChanged(state); + } catch (RemoteException ex) { + mRemoveList.add(r.binder); + } + } + } + handleRemoveListLocked(); + } + } + @Override public void dump(FileDescriptor fd, PrintWriter writer, String[] args) { @@ -1607,6 +1643,7 @@ class TelephonyRegistry extends ITelephonyRegistry.Stub { pw.println("mBackgroundCallState=" + mBackgroundCallState); pw.println("mVoLteServiceState=" + mVoLteServiceState); pw.println("mPhoneCapability=" + mPhoneCapability); + pw.println("mRadioPowerState=" + mRadioPowerState); pw.decreaseIndent(); diff --git a/telephony/java/android/telephony/PhoneStateListener.java b/telephony/java/android/telephony/PhoneStateListener.java index bd6a59d7492c..c06832e931b8 100644 --- a/telephony/java/android/telephony/PhoneStateListener.java +++ b/telephony/java/android/telephony/PhoneStateListener.java @@ -17,6 +17,7 @@ package android.telephony; import android.annotation.NonNull; +import android.annotation.SystemApi; import android.annotation.UnsupportedAppUsage; import android.os.Bundle; import android.os.Handler; @@ -280,6 +281,15 @@ public class PhoneStateListener { */ public static final int LISTEN_PHONE_CAPABILITY_CHANGE = 0x00200000; + /** + * Listen for changes to the radio power state. + * + * @see #onRadioPowerStateChanged + * @hide + */ + @SystemApi + public static final int LISTEN_RADIO_POWER_STATE_CHANGED = 0x00400000; + /* * Subscription used to listen to the phone state changes * @hide @@ -406,6 +416,9 @@ public class PhoneStateListener { PhoneStateListener.this.onPhoneCapabilityChanged( (PhoneCapability) msg.obj); break; + case LISTEN_RADIO_POWER_STATE_CHANGED: + PhoneStateListener.this.onRadioPowerStateChanged((int) msg.obj); + break; } } }; @@ -645,6 +658,18 @@ public class PhoneStateListener { // default implementation empty } + /** + * Callback invoked when modem radio power state changes. Requires + * the READ_PRIVILEGED_PHONE_STATE permission. + * @param state the modem radio power state + * @hide + */ + @SystemApi + public void onRadioPowerStateChanged(@TelephonyManager.RadioPowerState int state) { + // default implementation empty + } + + /** * Callback invoked when telephony has received notice from a carrier * app that a network action that could result in connectivity loss @@ -776,6 +801,10 @@ public class PhoneStateListener { public void onPhoneCapabilityChanged(PhoneCapability capability) { send(LISTEN_PHONE_CAPABILITY_CHANGE, 0, 0, capability); } + + public void onRadioPowerStateChanged(@TelephonyManager.RadioPowerState int state) { + send(LISTEN_RADIO_POWER_STATE_CHANGED, 0, 0, state); + } } @UnsupportedAppUsage diff --git a/telephony/java/android/telephony/TelephonyManager.java b/telephony/java/android/telephony/TelephonyManager.java index f83ea68af1c5..fe75e4da8ea5 100644 --- a/telephony/java/android/telephony/TelephonyManager.java +++ b/telephony/java/android/telephony/TelephonyManager.java @@ -6842,6 +6842,60 @@ public class TelephonyManager { return false; } + /** @hide */ + @Retention(RetentionPolicy.SOURCE) + @IntDef(prefix = {"RADIO_POWER_"}, + value = {RADIO_POWER_OFF, + RADIO_POWER_ON, + RADIO_POWER_UNAVAILABLE, + }) + public @interface RadioPowerState {} + + /** + * Radio explicitly powered off (e.g, airplane mode). + * @hide + */ + @SystemApi + public static final int RADIO_POWER_OFF = 0; + + /** + * Radio power is on. + * @hide + */ + @SystemApi + public static final int RADIO_POWER_ON = 1; + + /** + * Radio power unavailable (eg, modem resetting or not booted). + * @hide + */ + @SystemApi + public static final int RADIO_POWER_UNAVAILABLE = 2; + + /** + * @return current modem radio state. + * + *

Requires permission: {@link android.Manifest.permission#READ_PRIVILEGED_PHONE_STATE} or + * {@link android.Manifest.permission#READ_PHONE_STATE} or that the calling + * app has carrier privileges (see {@link #hasCarrierPrivileges}). + * + * @hide + */ + @SystemApi + @RequiresPermission(anyOf = {android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, + android.Manifest.permission.READ_PHONE_STATE}) + public @RadioPowerState int getRadioPowerState() { + try { + ITelephony telephony = getITelephony(); + if (telephony != null) { + return telephony.getRadioPowerState(getSlotIndex(), mContext.getOpPackageName()); + } + } catch (RemoteException ex) { + // This could happen if binder process crashes. + } + return RADIO_POWER_UNAVAILABLE; + } + /** @hide */ @SystemApi @SuppressLint("Doclava125") diff --git a/telephony/java/com/android/internal/telephony/IPhoneStateListener.aidl b/telephony/java/com/android/internal/telephony/IPhoneStateListener.aidl index 1ebb6976b45e..86818593a32b 100644 --- a/telephony/java/com/android/internal/telephony/IPhoneStateListener.aidl +++ b/telephony/java/com/android/internal/telephony/IPhoneStateListener.aidl @@ -52,5 +52,6 @@ oneway interface IPhoneStateListener { void onCarrierNetworkChange(in boolean active); void onUserMobileDataStateChanged(in boolean enabled); void onPhoneCapabilityChanged(in PhoneCapability capability); + void onRadioPowerStateChanged(in int state); } diff --git a/telephony/java/com/android/internal/telephony/ITelephony.aidl b/telephony/java/com/android/internal/telephony/ITelephony.aidl index 627dfaa5d36e..254ba434d91f 100644 --- a/telephony/java/com/android/internal/telephony/ITelephony.aidl +++ b/telephony/java/com/android/internal/telephony/ITelephony.aidl @@ -1477,4 +1477,10 @@ interface ITelephony { * Return the network selection mode on the subscription with id {@code subId}. */ int getNetworkSelectionMode(int subId); + + /** + * Return the modem radio power state for slot index. + * + */ + int getRadioPowerState(int slotIdex, String callingPackage); } diff --git a/telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl b/telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl index 43d56b39e0c4..2f40fcc844f3 100644 --- a/telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl +++ b/telephony/java/com/android/internal/telephony/ITelephonyRegistry.aidl @@ -79,4 +79,5 @@ interface ITelephonyRegistry { void notifyCarrierNetworkChange(in boolean active); void notifyUserMobileDataStateChangedForPhoneId(in int phoneId, in int subId, in boolean state); void notifyPhoneCapabilityChanged(in PhoneCapability capability); + void notifyRadioPowerStateChanged(in int state); }