Android 设置SIM卡界面定制

Android 设置SIM卡界面定制

REQ: ODM定制需求,设置,SIM卡界面需要显示运营商和SIM卡电话号码。效果如下图

sdaasdaffad

采用方案

修改网络配置xml文件,增加LayoutPreference选项,在该选项下,新增layout布局文件,显示相关运营商和电话号码信息。

其中在layout中,引入了新增加的圆角xml文件,使其更美观。

在加载网络xml文件的MobileNetworkSettings.java文件,find id信息,然后做相关setText操作。

获取运营商信息时,telephonyManager有接口可以直接调用。获取双卡槽电话号码时,通过
SubscriptionManager subscriptionManager = SubscriptionManager.from(getContext());
List subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();获取赋值。

Modify仓库信息

A:packages/apps/Settings/res/drawable/btn_shape.xml
A:packages/apps/Settings/res/layout/phone_number_settings_pic.xml
M:packages/apps/Settings/res/xml/mobile_network_settings.xml
M:packages/apps/Settings/src/com/android/settings/network/telephony/MobileNetworkSettings.java
A:packages/apps/Settings/src/com/android/settings/network/telephony/PhoneNumberPreferenceController.java
M:vendor/xxx/project_name/config.mk

具体修改

//overlay控制,公共仓库提交修改,单个odm控制此功能开启
vendor/xxx/project_name/config.mk

#add by hhuiming  on 2023-02-13
PRODUCT_PRODUCT_PROPERTIES += ro.feature.show_phonenumber_support=true
#add by hhuiming end
//为layout增加圆角背景,可参考如下链接https://blog.csdn.net/zsl20200217/article/details/128766934
A:packages/apps/Settings/res/drawable/btn_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
 
    <!-- 实心长方形 -->
    <solid android:color="#d3d3d3" />
 
    <!-- 设置弧度 -->
    <corners android:radius="15dp" />
 
</shape>
//自定义新增布局
A:packages/apps/Settings/res/layout/phone_number_settings_pic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginStart="20dp"
        android:layout_marginTop="40dp"
	android:background="@drawable/btn_shape">
        <!--ImageView
            android:id="@+id/sim1_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher_settings" 
	    android:layout_marginTop="20dp"
            android:layout_marginStart="10dp"/-->
	<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SIM 1"
            android:layout_marginTop="20dp"
            android:textSize="25dp"
	    android:textColor="#000000"
            android:layout_marginStart="10dp"/>
        <TextView
            android:id="@+id/opeator_sim1_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknown"
	    android:textColor="#000000"
            android:layout_marginTop="20dp"
	    android:layout_marginStart="10dp"/>
        <TextView
            android:id="@+id/sim1_phone_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknown"
	    android:textColor="#000000"
            android:layout_marginTop="20dp"
	    android:layout_marginStart="10dp"
	    android:layout_marginBottom="10dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_marginTop="40dp"
	android:layout_marginStart="10dp"
        android:layout_marginEnd="20dp"
	android:background="@drawable/btn_shape">
        <!--ImageView
            android:id="@+id/sim2_pic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher_settings" 
	    android:layout_marginTop="20dp"
            android:layout_marginStart="10dp"/-->
	<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="SIM 2"
            android:layout_marginTop="20dp"
            android:textSize="25dp"
	    android:textColor="#000000"
            android:layout_marginStart="10dp"/>
        <TextView
            android:id="@+id/opeator_sim2_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknown"
	    android:textColor="#000000"
            android:layout_marginTop="20dp"
	    android:layout_marginStart="10dp"/>
        <TextView
            android:id="@+id/sim2_phone_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Unknown"
            android:textColor="#000000"
            android:layout_marginTop="20dp"
            android:layout_marginStart="10dp"
	    android:layout_marginBottom="10dp"/>
    </LinearLayout>
</LinearLayout>
//网络配置xml,新增LayoutPreference。增加对应的PreferenceController,控制该布局的显示与隐藏,功能开启与关闭
M:packages/apps/Settings/res/xml/mobile_network_settings.xml
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="mobile_network_pref_screen">
    <com.android.settingslib.widget.LayoutPreference
        android:key="phone_number_settings_preview"
        android:layout="@layout/phone_number_settings_pic"
        android:selectable="false"
        settings:searchable="false"
        settings:controller="com.android.settings.network.telephony.PhoneNumberPreferenceController"/>
//新增获运营商,电话号码相关逻辑
M:packages/apps/Settings/src/com/android/settings/network/telephony/MobileNetworkSettings.java
import com.android.settingslib.widget.LayoutPreference;
import android.widget.TextView;
import android.telephony.SubscriptionInfo;
import android.os.SystemProperties;


//add by hhuiming on 2023-02-13
public static final boolean FEATURE_SHOW_PHONE_NUMBER_SUPPORT = SystemProperties.getBoolean("ro.feature.show_phonenumber_support", false);
private static final String PREF_PHONE_NUMBER_PREVIEW = "phone_number_settings_preview";
LayoutPreference phoneNumberPreview;
private TextView opeator_sim1_name,sim1_phone_number,opeator_sim2_name,sim2_phone_number;
//add by hhuiming end


onCreate(){
    
    
    .......
	//add by hhuiming on 2023-02-13
	if(FEATURE_SHOW_PHONE_NUMBER_SUPPORT){
    
    
	    initOperatorAndPhoneNubmer();
	    getSimoperatorName(0);
            getSimoperatorName(1);
	    getSlotPhoneNumber();
	}
	//add by hhuiming end
	.......

	    //add by hhuiming on 2023-02-13 for ID1000707
    private void initOperatorAndPhoneNubmer(){
    
    
        phoneNumberPreview = findPreference(PREF_PHONE_NUMBER_PREVIEW);
        if(phoneNumberPreview != null){
    
    
            opeator_sim1_name = phoneNumberPreview.findViewById(R.id.opeator_sim1_name);
            sim1_phone_number = phoneNumberPreview.findViewById(R.id.sim1_phone_number);
            opeator_sim2_name = phoneNumberPreview.findViewById(R.id.opeator_sim2_name);
            sim2_phone_number = phoneNumberPreview.findViewById(R.id.sim2_phone_number);
        }
    }
    private void getSlotPhoneNumber(){
    
    
        SubscriptionManager subscriptionManager = SubscriptionManager.from(getContext());
        List<SubscriptionInfo> subsInfoList = subscriptionManager.getActiveSubscriptionInfoList();
        Log.d(LOG_TAG,"getSlotPhoneNumber Current list =" + subsInfoList);
	if(subsInfoList != null){
    
    
	    for (SubscriptionInfo subscriptionInfo : subsInfoList) {
    
    
		int slotId = subscriptionInfo.getSimSlotIndex();
		Log.d(LOG_TAG,"subscriptionInfo  =" + slotId + ", getNumber = " + subscriptionInfo.getNumber());
		switch (slotId){
    
    
		    case 0:
		        String slot_one_number= subscriptionInfo.getNumber();
			Log.d(LOG_TAG,"subscriptionInfo  slotId 0 slot_one_number" + slot_one_number + ", getNumber = " + sim1_phone_number.getText());
                        if(!sim1_phone_number.getText().equals(slot_one_number)){
    
    
			    if(slot_one_number.isEmpty()){
    
    
			    	sim1_phone_number.setText("Unknown");
                            } else {
    
    
			    	sim1_phone_number.setText(slot_one_number);
                            }
                        }
		       break;
		    case 1:
		        String slot_two_number= subscriptionInfo.getNumber();
		 	    Log.d(LOG_TAG,"subscriptionInfo  slotId 1 slot_two_number" + slot_two_number + ", getNumber = " + sim1_phone_number.getText());
                if(!sim2_phone_number.getText().equals(slot_two_number)){
    
    
			    	if(slot_two_number.isEmpty()){
    
    
			    		sim2_phone_number.setText("Unknown");
                   } else {
    
    
			    		sim2_phone_number.setText(slot_two_number);
                        }
                   }
		       break;
		    default:
		        sim1_phone_number.setText("Unknown");
			sim2_phone_number.setText("Unknown");
			break;
		}
            }
	}
    }
    private void getSimoperatorName(int phoneId) {
    
    
        int simState = mTelephonyManager.getSimState(phoneId);
	Log.d(LOG_TAG," getSimoperatorName simState = " + simState);
        switch (phoneId){
    
    
            case 0:
                if(TelephonyManager.SIM_STATE_READY == simState){
    
    
                    if(mTelephonyManager != null){
    
    
                        String sim1_name= mTelephonyManager.getSimOperatorNameForPhone(phoneId);
                        if(!opeator_sim1_name.getText().equals(sim1_name)){
    
    
                            opeator_sim1_name.setText(sim1_name);
                        }
                    }
                } else {
    
    
                    opeator_sim1_name.setText("Unknown");
                }
                break;
            case 1:
                if(TelephonyManager.SIM_STATE_READY == simState){
    
    
                    if(mTelephonyManager != null){
    
    
                        String sim2_name= mTelephonyManager.getSimOperatorNameForPhone(phoneId);
                        if(!opeator_sim2_name.getText().equals(sim2_name)){
    
    
                            opeator_sim2_name.setText(sim2_name);
                        }
                    }
                } else {
    
    
                    opeator_sim2_name.setText("Unknown");
                }
                break;
            default:
                break;
        }
    }
    //add by hhuiming end
}
//增加PreferenceController,控制新增layoutPreference 控件的显示与隐藏
A:packages/apps/Settings/src/com/android/settings/network/telephony/PhoneNumberPreferenceController.java
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.settings.network.telephony;
import android.content.Context;
import android.os.SystemProperties;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
public class PhoneNumberPreferenceController extends BasePreferenceController {
    
    
    public static final boolean FEATURE_SHOW_PHONE_NUMBER_SUPPORT = getBoolean("ro.feature.show_phonenumber_support");
    public PhoneNumberPreferenceController(Context context, String preferenceKey) {
    
    
        super(context, preferenceKey);
    }
    @Override
    public int getAvailabilityStatus() {
    
    
        return FEATURE_SHOW_PHONE_NUMBER_SUPPORT ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }
    public static boolean getBoolean(final String featureKey) {
    
    
        return SystemProperties.getBoolean(featureKey, false);
    }
}

done,完成,收工,有问题在DEBUG

猜你喜欢

转载自blog.csdn.net/weixin_45080805/article/details/129024279