android 获取iccid imsi

南宫阳冰
2023-12-01

网上关于这些挺多的,不过没找到关于双卡的以及未插卡的异常处理,我的需求是在settings里面添加菜单显示iccid imsi,记录一下

(一)在设置-关于手机显示iccid imsi

1.在packages/apps/Settings / res/xml/my_device_info.xml添加菜单

    <!-- Device imsi display -->
    <Preference
        android:key="imsi"
        android:order="34"
        android:title="@string/imsi"
        settings:controller="com.android.settings.deviceinfo.imsi.ImsiInfoPreferenceController"/>

    <!-- Device imsi display -->
    <Preference
        android:key="iccid"
        android:order="36"
        android:title="@string/iccid"
      settings:controller="com.android.settings.deviceinfo.iccid.IccidInfoPreferenceController"/>

2.在packages/apps/Settings / src/com/android/settings/deviceinfo/aboutphone/MyDeviceInfoFragment.java添加ImsiInfoPreferenceController,IccidInfoPreferenceController

//wangdonghai add begin
import com.android.settings.deviceinfo.imsi.ImsiInfoPreferenceController;
import com.android.settings.deviceinfo.iccid.IccidInfoPreferenceController;
//wangdonghai add end
import java.util.ArrayList;
import java.util.List;
import android.util.Log;

@SearchIndexable
public class MyDeviceInfoFragment extends DashboardFragment
        implements DeviceNamePreferenceController.DeviceNamePreferenceHost {

    private static final String LOG_TAG = "MyDeviceInfoFragment";
    private static final String KEY_MY_DEVICE_INFO_HEADER = "my_device_info_header";
    // Add for Local system update
    private static String KEY_RECOVERY_SYSTEM_UPDATE = "RecoverySystemUpdate";

    private BuildNumberPreferenceController mBuildNumberPreferenceController;

    @Override
    public int getMetricsCategory() {
        return SettingsEnums.DEVICEINFO;
    }

    @Override
    public int getHelpResource() {
        return R.string.help_uri_about;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        use(ImeiInfoPreferenceController.class).setHost(this /* parent */);
        use(DeviceNamePreferenceController.class).setHost(this /* parent */);
        //wangdonghai add begin 
        use(ImsiInfoPreferenceController.class).setHost(this /* parent */);
        use(IccidInfoPreferenceController.class).setHost(this /* parent */);
        //wangdonghai add  end 
        mBuildNumberPreferenceController = use(BuildNumberPreferenceController.class);
        mBuildNumberPreferenceController.setHost(this /* parent */);
    }

3,新建IccidInfoPreferenceController.java

/*
 * Copyright (C) 2017 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.
 */

/*
* add new file by wangdonghai for SHR_M123_A00-1183 at 20210303
* */
package com.android.settings.deviceinfo.iccid;

import static android.telephony.TelephonyManager.PHONE_TYPE_CDMA;

import android.content.Context;
import android.os.UserManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import androidx.annotation.VisibleForTesting;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.slices.Sliceable;
import com.android.settingslib.Utils;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller that manages preference for single and multi sim devices.
 */
public class IccidInfoPreferenceController extends BasePreferenceController {

    private String TAG = "IccidInfoPreferenceController";
    private boolean isDebug = false;
    private final boolean mIsMultiSim;
    private PreferenceScreen mPreferenceScreen;
    private final TelephonyManager mTelephonyManager;
    private final List<Preference> mPreferenceList = new ArrayList<>();
    private Fragment mFragment;

    public IccidInfoPreferenceController(Context context, String key) {
        super(context, key);
        mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        mIsMultiSim = mTelephonyManager.getPhoneCount() > 1;
    }

    public void setHost(Fragment fragment) {
        mFragment = fragment;
    }

    @Override
    public void displayPreference(PreferenceScreen screen) {
        super.displayPreference(screen);
        final Preference preference = screen.findPreference(getPreferenceKey());
        mPreferenceScreen = screen;
        mPreferenceList.add(preference);
        updatePreference(preference, 0 /* simSlot */);

        final int imsiPreferenceOrder = preference.getOrder();
        // Add additional preferences for each sim in the device
        for (int simSlotNumber = 1; simSlotNumber < mTelephonyManager.getPhoneCount();
                simSlotNumber++) {
            final Preference multiSimPreference = createNewPreference(screen.getContext());
            multiSimPreference.setOrder(imsiPreferenceOrder + simSlotNumber);
            multiSimPreference.setKey(getPreferenceKey() + simSlotNumber);
            multiSimPreference.setVisible(isAvailable());
            screen.addPreference(multiSimPreference);
            mPreferenceList.add(multiSimPreference);
            multiSimPreference.setTitle(mContext.getString(R.string.iccid_multi_sim, simSlotNumber));
            updatePreference(multiSimPreference, simSlotNumber);
        }
    }

    @Override
    public void updateState(Preference preference) {
        if (preference == null) {
            return;
        }
        int size = mPreferenceList.size();
        for (int i = 0; i < size; i++) {
            Preference pref = mPreferenceList.get(i);
            updatePreference(pref, i);
        }
    }

    @Override
    public int getAvailabilityStatus() {

		return AVAILABLE ;
    }

    @Override
    public boolean useDynamicSliceSummary() {
        return true;
    }

    private void updatePreference(Preference preference, int simSlot) {
        preference.setTitle(getTitleForGsmPhone(simSlot));
        if (SubscriptionManager.getSubId(simSlot) == null){
            mPreferenceScreen.removePreference(preference);
            return;
        }
        int subId =  SubscriptionManager.getSubId(simSlot)[0];            
        /*这里特别注意getSubId(simSlot)是返回第一个卡槽里面所有插过的卡的subid,每插入一个卡都 
        会有一个subid,getSubId(simSlot)[0]就是获取当前插入的卡的subid,未插卡也会返回之前插入        
       的卡的第一个subid,每次重新插卡,都会把插入的卡的subid放在第一位,也就是 
       getSubId(simSlot)[0]数组的第一位*/
        String iccid = mTelephonyManager.getSimSerialNumber(subId);
        if (iccid == null || iccid.isEmpty()){
            mPreferenceScreen.removePreference(preference);
            return ;
        }
        //AT&T MX mccmnc=33401,334090,22201,334050
        String mccmnc = mTelephonyManager.getSimOperator();
        Boolean att = iccid.startsWith("33401") ||  iccid.startsWith("334090")||  iccid.startsWith("33409") ||  iccid.startsWith("22201") || iccid.startsWith("334050");
        if(att){
            preference.setSummary(iccid);
        }else {
            mPreferenceScreen.removePreference(preference);
        }
        if(isDebug){
            Log.i(TAG,"wdh  updatePreference simSlot == "+simSlot);
            Log.i(TAG,"wdh  updatePreference subId == "+subId);
            Log.i(TAG,"wdh  updatePreference  getTitleForGsmPhone(simSlot) == "+getTitleForGsmPhone(simSlot) );
            Log.i(TAG,"wdh  updatePreference mccmnc == "+mccmnc);
            Log.i(TAG,"wdh  updatePreference  iccid== "+iccid);
        }

    }

    private CharSequence getTitleForGsmPhone(int simSlot) {
        return mIsMultiSim ? mContext.getString(R.string.iccid_multi_sim, simSlot + 1)
                : mContext.getString(R.string.iccid_multi_sim, simSlot);
    }

    private CharSequence getTitleForCdmaPhone(int simSlot) {
        return mIsMultiSim ? mContext.getString(R.string.iccid_multi_sim, simSlot + 1)
                : mContext.getString(R.string.iccid_multi_sim, simSlot);
    }

    private CharSequence getTitle(int simSlot) {
        final int phoneType = getPhoneType(simSlot);
        return phoneType == PHONE_TYPE_CDMA ? getTitleForCdmaPhone(simSlot + 1)
                : getTitleForGsmPhone(simSlot);
    }

    private int getPhoneType(int slotIndex) {
        SubscriptionInfo subInfo = SubscriptionManager.from(mContext)
                .getActiveSubscriptionInfoForSimSlotIndex(slotIndex);
        return mTelephonyManager.getCurrentPhoneType(subInfo != null ? subInfo.getSubscriptionId()
                : SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
    }

    @VisibleForTesting
    Preference createNewPreference(Context context) {
        return new Preference(context);
    }
}

 类似资料: