最近产品提出来了一个需求,就是将来电转接这个功能的入口往外移。之后我就通过adb命令,获知来电界面属于GsmUmtsCallForwardOptions类。之后查看源码,再将这块代码弄得一知半解的时候,我将来电转接这块的代码移到自己代码中,一运行直接报错。查看日志如下:
AndroidRuntime: Caused by: java.lang.IllegalStateException: Default phones haven't been made yet!
AndroidRuntime: at com.android.internal.telephony.PhoneFactory.getPhone(PhoneFactory.java:510)
PhoneFactory.getPhone方法如下,sMadeDefaults是一个Boolean值,当为false的时候,就会直接报出异常,跟之前报错所抓的日志一致。
public static Phone getPhone(int phoneId) {
Phone phone;
String dbgInfo = "";
synchronized (sLockProxyPhones) {
if (!sMadeDefaults) {
throw new IllegalStateException("Default phones haven't been made yet!");
// CAF_MSIM FIXME need to introduce default phone id ?
} else if (phoneId == SubscriptionManager.DEFAULT_PHONE_INDEX) {
if (DBG) dbgInfo = "phoneId == DEFAULT_PHONE_ID return sProxyPhone";
phone = sProxyPhone;
} else {
if (DBG) dbgInfo = "phoneId != DEFAULT_PHONE_ID return sProxyPhones[phoneId]";
phone = (((phoneId >= 0)
&& (phoneId < TelephonyManager.getDefault().getPhoneCount()))
? sProxyPhones[phoneId] : null);
}
if (DBG) {
Rlog.d(LOG_TAG, "getPhone:- " + dbgInfo + " phoneId=" + phoneId +
" phone=" + phone);
}
return phone;
}
/**
* FIXME replace this with some other way of making these
* instances
*/
public static void makeDefaultPhone(Context context) {
synchronized (sLockProxyPhones) {
if (!sMadeDefaults) {
sContext = context;
// create the telephony device controller.
TelephonyDevController.create();
int retryCount = 0;
for(;;) {
boolean hasException = false;
retryCount ++;
try {
// use UNIX domain socket to
// prevent subsequent initialization
new LocalServerSocket("com.android.internal.telephony");
} catch (java.io.IOException ex) {
hasException = true;
}
if ( !hasException ) {
break;
} else if (retryCount > SOCKET_OPEN_MAX_RETRY) {
throw new RuntimeException("PhoneFactory probably already running");
} else {
try {
Thread.sleep(SOCKET_OPEN_RETRY_MILLIS);
} catch (InterruptedException er) {
}
}
}
sPhoneNotifier = new DefaultPhoneNotifier();
int cdmaSubscription = CdmaSubscriptionSourceManager.getDefault(context);
Rlog.i(LOG_TAG, "Cdma Subscription set to " + cdmaSubscription);
/* In case of multi SIM mode two instances of PhoneProxy, RIL are created,
where as in single SIM mode only instance. isMultiSimEnabled() function checks
whether it is single SIM or multi SIM mode */
int numPhones = TelephonyManager.getDefault().getPhoneCount();
int[] networkModes = new int[numPhones];
sProxyPhones = new PhoneProxy[numPhones];
sCommandsInterfaces = new RIL[numPhones];
for (int i = 0; i < numPhones; i++) {
// reads the system properties and makes commandsinterface
// Get preferred network type.
networkModes[i] = RILConstants.PREFERRED_NETWORK_MODE;
Rlog.i(LOG_TAG, "Network Mode set to " + Integer.toString(networkModes[i]));
sCommandsInterfaces[i] = new RIL(context, networkModes[i],
cdmaSubscription, i);
}
Rlog.i(LOG_TAG, "Creating SubscriptionController");
SubscriptionController.init(context, sCommandsInterfaces);
// Instantiate UiccController so that all other classes can just
// call getInstance()
mUiccController = UiccController.make(context, sCommandsInterfaces);
for (int i = 0; i < numPhones; i++) {
PhoneBase phone = null;
int phoneType = TelephonyManager.getPhoneType(networkModes[i]);
if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
phone = new GSMPhone(context,
sCommandsInterfaces[i], sPhoneNotifier, i);
phone.startMonitoringImsService();
} else if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
phone = new CDMALTEPhone(context,
sCommandsInterfaces[i], sPhoneNotifier, i);
phone.startMonitoringImsService();
}
Rlog.i(LOG_TAG, "Creating Phone with type = " + phoneType + " sub = " + i);
sProxyPhones[i] = new PhoneProxy(phone);
}
mProxyController = ProxyController.getInstance(context, sProxyPhones,
mUiccController, sCommandsInterfaces);
// Set the default phone in base class.
// FIXME: This is a first best guess at what the defaults will be. It
// FIXME: needs to be done in a more controlled manner in the future.
sProxyPhone = sProxyPhones[0];
sCommandsInterface = sCommandsInterfaces[0];
// Ensure that we have a default SMS app. Requesting the app with
// updateIfNeeded set to true is enough to configure a default SMS app.
ComponentName componentName =
SmsApplication.getDefaultSmsApplication(context, true /* updateIfNeeded */);
String packageName = "NONE";
if (componentName != null) {
packageName = componentName.getPackageName();
}
Rlog.i(LOG_TAG, "defaultSmsApplication: " + packageName);
// Set up monitor to watch for changes to SMS packages
SmsApplication.initSmsPackageMonitor(context);
sMadeDefaults = true;
Rlog.i(LOG_TAG, "Creating SubInfoRecordUpdater ");
sSubInfoRecordUpdater = new SubscriptionInfoUpdater(context,
sProxyPhones, sCommandsInterfaces);
SubscriptionController.getInstance().updatePhonesAvailability(sProxyPhones);
}
}
}
PhoneFactory中只有makeDefaultPhone方法会改变sMadeDefaults布尔值。于是我就主动调用了PhoneFactory的makeDefaultPhone方法,但是会出现异常,java.lang.RuntimeException: failed to set system property,属于底层报错,这一下懵逼了。
通过在网上查找资料发现,在com.android.phone.PhoneApp中的onCreate方法中调用了,PhoneApp的源码位于(packages\services\telephony\src\com\android\phone),在AndroidManifest.xml中的文件配置如下:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
package="com.android.phone"
android:versionCode="2"
android:versionName="02.01.1608231404"
coreApp="true"
android:sharedUserId="android.uid.phone"
android:sharedUserLabel="@string/phoneAppLabel"
>
对于android:persistent="true" 的应用是在Android开机时启动的,PhoneApp是com,android.phone的Application,所以按理说一开机的时候就会调用PhoneFactory中的makeDefaultPhone方法。所以可以通过共享com.android.phone中的PhoneFactory对象调用getPhone方法。
修改AndroidManifest.xml 文件,具体修改如下,注意android:sharedUserId="android.uid.phone" 和android:process="com.android.phone"部分,之后进行签名。
相同的shareduserid指可以访问对方apk的数据库,文件等,若需要访问内存中的数据,则需要跑在相同的进程中。指定process则可以使其跑在同一进程中,才可以获取进程内的数据。