android开发笔记之系统属性(ro.com.google.clientidbase.ms)随卡适配

客户要求

客户提了一个小需求,就是要将系统属性(ro.com.google.clientidbase.ms)随卡适配。
具体要求是:
如果插中国的卡,系统属性ro.com.google.clientidbase.ms值为android-orange-cn。
如果插法国的卡,系统属性ro.com.google.clientidbase.ms值为android-orange-fr。
如果插西班牙的卡,系统属性ro.com.google.clientidbase.ms值为android-orange-es。

需求实现

这个功能,怎么样,是不是非常简单。
哈哈,如果你这样认为,我担心你搞不定此问题。此问题不是那么好实现的。
但是,我们公司还是有牛人啊,将其实现了。

大概分析实现流程

监听到插入sim卡手机后,读取sim卡的国家缩写码,再将此读取到的sim卡的国家缩写码赋值给系统属性ro.com.google.clientidbase.ms。

感觉是不是太简单了,如果你这样觉得,那是因为你不知道实现此问题的关键所在。ro开头的系统属性,我们一般是只有读取的权限,是没有写的权限噢。
所以,此问题的关键点:

就在与给系统属性ro.com.google.clientidbase.ms赋值的写的权限。

具体实现

(1)监听到插入sim卡后,读取sim卡的国家缩写码,将系统属性gsm.operator.googleclientbase值设置为true。

在\frameworks\base\telephony\java\com\android\internal\telephony\TelephonyProperties.java文件中,定义系统属性gsm.operator.googleclientbase和gsm.sim.operator.iso-country:

/** ISO country code equivalent for the SIM provider's country code*/
static String PROPERTY_ICC_OPERATOR_ISO_COUNTRY = "gsm.sim.operator.iso-country";

//add codes begin
static final String PROPERTY_GOOGLE_CLIENTIDBASE_SET = "gsm.operator.googleclientbase";
//add codes end

在\frameworks\base\telephony\java\android\telephony\TelephonyManager.java文件中,将读取sim卡的国家缩写码保存到gsm.sim.operator.iso-country中,再将系统属性gsm.operator.googleclientbase值设置为true,从而触发init.rc中的服务。

public void setSimCountryIsoForPhone(int phoneId, String iso) {
    setTelephonyProperty(phoneId,
             TelephonyProperties.PROPERTY_ICC_OPERATOR_ISO_COUNTRY, iso);
    //add codes begin
    if (!android.text.TextUtils.isEmpty(iso)) {
            SystemProperties.set(TelephonyProperties.PROPERTY_GOOGLE_CLIENTIDBASE_SET, "true");
    }
    //add codes end
}

(2)init.rc定义给系统属性ro.com.google.clientidbase.ms赋值的服务:
当系统属性gsm.operator.googleclientbase值设置为true,从而触发init.rc中的服务client_search,并指定此服务为/system/bin/googleclientidbaseprop。
至于为什么在init.rc来执行给系统属性ro.com.google.clientidbase.ms赋值的操作,还是因为权限的问题,此文件可以有root权限,这其实是可以从此文件的路径可以看出。

\system\core\rootdir\init.rc

#add codes begin
#当系统属性设置为true时,启动服务client_search
on property:gsm.operator.googleclientbase=true
    start client_search

#给服务client_search指定bin文件和添加对应权限
service client_search /system/bin/googleclientidbaseprop
    user root
    group root
    disabled
    oneshot
#add codes end

(3)googleclientidbaseprop应用,主要功能是将从gsm.sim.operator.iso-country读取到的sim卡的国家缩写码赋值给ro.com.google.clientidbase.ms。

这其实就是一个我们平时的应用,只是,他为一个bin执行文件。
apps\GoogleClientIdBase\Android.mk

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
    googleclientidbaseprop_main.cpp
LOCAL_CFLAGS += -Wall -Werror -Wunused -Wunreachable-code
LOCAL_SHARED_LIBRARIES := \
    libcutils \
    liblog \
    libutils
LOCAL_MODULE:= googleclientidbaseprop
ifdef TARGET_32_BIT_SURFACEFLINGER
LOCAL_32_BIT_ONLY := true
endif
include $(BUILD_EXECUTABLE)

apps\GoogleClientIdBase\googleclientidbaseprop_main.cpp

#undef NDEBUG
#define LOG_TAG "Googleclientidbaseprop"
#include <cutils/properties.h>
#include <utils/Log.h>
#include <string.h>

using namespace android;

#define PROPERTY_OPERATOR_ISO_COUNTRY "gsm.sim.operator.iso-country"
#define RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP "ro.com.google.clientidbase.ms"

int main()
{
    char value[PROPERTY_VALUE_MAX] = "";
    char value_ms[PROPERTY_VALUE_MAX] = "";
    char propValue[PROPERTY_VALUE_MAX] = "android-orange-";
    property_get(PROPERTY_OPERATOR_ISO_COUNTRY, value, "");
    property_get(RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP, value_ms, "");
    ALOGD("First Get prop %s: %s", RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP, value_ms);
    if(!(0 == strcmp("", value_ms))) return 0;

    if (!(0 == strcmp("", value))) {
        strncat(propValue, value, strlen(value));
        property_set(RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP, propValue);
    }
    memset(value, 0, PROPERTY_VALUE_MAX);
    property_get(RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP, value, "");
    ALOGD("Get prop %s: %s", RO_COM_GOOGLE_CLIENTIDBAEE_MS_PROP, value);
    return 0;
}

在*.mk文件中,将此应用加进来:

PRODUCT_PACKAGES += googleclientidbaseprop

(4)添加对应的权限

4.1 预置要随卡适配的系统属性的读写权限:
在文件device/qcom/sepolicy/common/property_contexts中,定义要修改的系统属性对应的属性名:

ro.com.google.clientidbase.ms       u:object_r:googleclient_base_prop:s0

再在文件device/qcom/sepolicy/common/property.te中,定义对应要修改的系统属性为系统属性:

type googleclient_base_prop, property_type;

4.2 init.rc的服务googleclientidbaseprop 读写权限:

再在device/qcom/sepolicy/common/file_contexts文件中,定义应用的执行名字:

/system/bin/googleclientidbaseprop u:object_r:googleclientidbaseprop_exec:s0

再在device/qcom/sepolicy/common/googleclientidbaseprop.te文件中,定义此服务的相关权限:

type googleclientidbaseprop, domain;
type googleclientidbaseprop_exec, exec_type, file_type;

init_daemon_domain(googleclientidbaseprop)

allow googleclientidbaseprop system_file:file execute_no_trans;
# Property Service write
set_prop(googleclientidbaseprop, googleclient_base_prop)

4.3 adb shell系统属性的读写权限:
这就是要在device/qcom/sepolicy/common/shell.te文件中添加随卡适配的系统属性的读写权限。

allow shell googleclient_base_prop:file { read getattr open };

可以看出,此系统权限的相关设置是比较复杂的,这也是此功能的实现的关键所在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hfreeman2008

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值