设置双卡不同的来电默认铃声

文章目录

    • 需求具体描述
    • 客制化
      • 双卡铃音功能实现

适用于Android L及之后的版本


需求具体描述

  • 可以分别设置双卡sim1,sim2的来电默认铃声

客制化

双卡铃音功能实现

1. alps/build/target/product/core_base.mk

添加SIM2 的系统属性ro.config.ringtone_sim2并置默认来电铃声(ro.config.ringtone是给SIM1 用):

    PRODUCT_PROPERTY_OVERRIDES += \
        ro.config.ringtone=Broken_Chord.ogg \
        ro.config.ringtone_sim2=Decorative_Pattern.ogg

2. alps/frameworks/base/core/java/android/provider/Settings.java

添加卡2 的铃声属性字段RINGTONE_SIM2,这个值与前面在common.mk 中定义的属性的
最后一个字段相同, 如果定义为ro.config.ringtone_sim2, 则RINGTONE_SIM2
RINGTONE_SIM2 = "ringtone_sim2"

    public static final String RINGTONE = "ringtone";
    public static final Uri DEFAULT_RINGTONE_URI = getUriFor(RINGTONE);
    // bailu modify start
    public static final String RINGTONE_SIM2 = "ringtone_sim2";
    public static final Uri DEFAULT_RINGTONE_SIM2_URI = getUriFor(RINGTONE_SIM2);
    // bailu modify end

3. alps/frameworks/base/media/java/android/media/RingtoneManager.java

  1. 添加SIM2的type:
    public static final int TYPE_RINGTONE = 1;
    public static final int TYPE_NOTIFICATION = 2;
    public static final int TYPE_ALARM = 4;
    public static final int TYPE_VIDEO_CALL = 8;
    public static final int TYPE_SIP_CALL = 16;
    // bailu modify start
    public static final int TYPE_RINGTONE_SIM2 = 32;
    public static final String KEY_DEFAULT_RINGTONE_SIM2 = "mtk_audioprofile_default_ringtone_sim2";
    // bailu modify end
  1. 返回获取的sim2的uri:
    public static Uri getDefaultRingtoneUri(Context context, int type) {
        ...
        switch (type) {
            case TYPE_RINGTONE:
                uriString = Settings.System.getString(resolver, KEY_DEFAULT_RINGTONE);
                break;
            // bailu modify start
            case TYPE_RINGTONE_SIM2:
                uriString = Settings.System.getString(resolver, KEY_DEFAULT_RINGTONE_SIM2);
                break;
            // bailu modify end

4. alps/frameworks/base/media/java/android/media/MediaScanner.java

  1. 添加读取到的卡2的默认铃声相关定义:
    // bailu modify start
    private String mDefaultRingtoneSim2Filename;
    private boolean mDefaultRingtoneSim2Set;
    private static final String RINGTONE_SIM2_SET = "ringtone_sim2_set";
    // bailu modify end
  1. core_base.mk中定义的sim2 铃声的名字读出来:
    private void setDefaultRingtoneFileNames() {
        mDefaultRingtoneFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.RINGTONE);
        // bailu modify start
        mDefaultRingtoneSim2Filename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.RINGTONE_SIM2);
        // bailu modify end
        ...
    }
  1. 添加isSim2Ringtone来区别设置的是sim1还是sim2,然后将扫描到的文件名和上面读到的sim2默认铃声文件名对比,如果是sim2 的ringtone,则设置sim2默认值。
    private Uri endFile(FileEntry entry, boolean ringtones, boolean notifications,
                boolean alarms, boolean music, boolean podcasts)
                throws RemoteException {
        // bailu modify start
        boolean isSim2Ringtone = false;
        // bailu modify end
        ...
         if (notifications && ((mWasEmptyPriorToScan && !mDefaultNotificationSet) ||
                        doesSettingEmpty(NOTIFICATION_SET))) {
            ...
            // bailu modify start
            } else if (ringtones && ((mWasEmptyPriorToScan && (!mDefaultRingtoneSet||!mDefaultRingtoneSim2Set)) ||
                        doesSettingEmpty(RINGTONE_SET)||doesSettingEmpty(RINGTONE_SIM2_SET))) {
                if (TextUtils.isEmpty(mDefaultRingtoneFilename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultRingtoneFilename)) {
                    needToSetSettings = true;
                    /// M: Adds log to debug setting ringtones.
                    Log.v(TAG, "endFile: needToSetRingtone=true.");
                } else if(TextUtils.isEmpty(mDefaultRingtoneSim2Filename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultRingtoneSim2Filename)) {
                    needToSetSettings = true;
                    isSim2Ringtone = true;
                    Log.v(TAG, "endFile: needToSetRingtone=true.sim2 default ringtone");
                }
            // bailu modify end
            } else if (alarms && ((mWasEmptyPriorToScan && !mDefaultAlarmSet) ||
                        doesSettingEmpty(ALARM_SET))) {
            ...
        }
        ...
        ///M: Set ringtone if it wasn't set before. @{
        if(needToSetSettings) {
            if (notifications && doesSettingEmpty(NOTIFICATION_SET)) {
            ...
            // bailu modify start
            } else if (ringtones && (doesSettingEmpty(RINGTONE_SET)||doesSettingEmpty(RINGTONE_SIM2_SET))) {
                if (!isSim2Ringtone) {
                    setSettingIfNotSet(Settings.System.RINGTONE, tableUri, rowId);
                    setSettingIfNotSet(Settings.System.VIDEO_CALL, tableUri, rowId); 
                    setSettingIfNotSet(Settings.System.SIP_CALL, tableUri, rowId);
                    setProfileSettings(RingtoneManager.TYPE_RINGTONE, tableUri, rowId);
                    setProfileSettings(RingtoneManager.TYPE_VIDEO_CALL, tableUri, rowId);
                    setProfileSettings(RingtoneManager.TYPE_SIP_CALL, tableUri, rowId);
                    mDefaultRingtoneSet = true;
                    setSettingFlag(RINGTONE_SET);
                    Log.v(TAG, "endFile: set ringtone. uri=" + tableUri + ", rowId=" + rowId);
                } else {
                    setSettingIfNotSet(Settings.System.RINGTONE_SIM2, tableUri, rowId);
                    setProfileSettings(RingtoneManager.TYPE_RINGTONE_SIM2, tableUri, rowId);
                    mDefaultRingtoneSim2Set = true;
                    setSettingFlag(RINGTONE_SIM2_SET);
                    Log.v(TAG, "endFile: set sim2 ringtone. uri=" + tableUri + ", rowId=" + rowId);
                }
            // bailu modify end
            } else if (alarms && doesSettingEmpty(ALARM_SET)) {
            ...
        }
  1. 添加设置sim2铃声
    private void setProfileSettings(int type, Uri uri, long rowId) {
        if (type == RingtoneManager.TYPE_NOTIFICATION) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_NOTIFICATION, uri, rowId);
        } else if (type == RingtoneManager.TYPE_RINGTONE) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_RINGTONE, uri, rowId);
        // bailu modify start
        } else if (type == RingtoneManager.TYPE_RINGTONE_SIM2) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_RINGTONE_SIM2, uri, rowId);
        // bailu modify end
        } else if (type == RingtoneManager.TYPE_VIDEO_CALL) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_VIDEO_CALL, uri, rowId);
        } else if (type == RingtoneManager.TYPE_SIP_CALL) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_SIP_CALL, uri, rowId);
        } else if (type == RingtoneManager.TYPE_ALARM) {
            setSettingIfNotSet(RingtoneManager.KEY_DEFAULT_ALARM, uri, rowId);
        }
    }

5. alps/frameworks/base/media/java/com/mediatek/audioprofile/AudioProfileManager.java

  1. 定义给sim2ringtone用的index,同时修改ringtonetype的个数,由原来的4改成5
    // mDefaultRingtone index
    /// bailu modify start
    private static final int DEFAULT_RINGTONE_TYPE_CONUT = 5;
    private static final int DEFAULT_RINGER_SIM2_INDEX = ProfileSettings.ringer_sim2_stream.ordinal();
    // bailu modify end
  1. 情景模式读铃声默认值时,添加读取sim2的默认值:
    private void readDefaultRingtones() {
        ...
        // mDefaultRingtone[3] sip ringtone
        uriString = Settings.System.getStringForUser(mContentResolver,
                AudioProfileManager.KEY_DEFAULT_SIP_CALL,mUserId);
        uri = (uriString == null ? null : Uri.parse(uriString));
        mDefaultRingtone.set(DEFAULT_SIPCALL_INDEX, uri);
        
        /// bailu modify start
        // mDefaultRingtone[4] vioce ringtone sim2
        uriString = Settings.System.getString(mContentResolver, AudioProfileManager.KEY_DEFAULT_RINGTONE_SIM2);
        uri = (uriString == null ? null : Uri.parse(uriString));
        mDefaultRingtone.set(DEFAULT_RINGER_SIM2_INDEX , uri);
        // bailu modify end
  1. 根据sim卡persist值来设置uri
    private void readPersistedSettings(String profileKey, long simId) {
        ...
        name = AudioProfileManager.getStreamUriKey(profileKey,
                AudioProfileManager.TYPE_RINGTONE, simId);
                
        /// bailu modify start
        Uri voiceCallUri;
        int slotId = SubscriptionManager.getSlotId(Integer.parseInt(String.valueOf(simId)));
        if (slotId == 1) {
            voiceCallUri = getPersistedValue(name, initValues,mDefaultRingtone.get(DEFAULT_RINGER_SIM2_INDEX));
        } else {
            voiceCallUri = getPersistedValue(name, initValues,mDefaultRingtone.get(DEFAULT_RINGER_INDEX));
        }
        // bailu modify end
        
        name = AudioProfileManager.getStreamUriKey(profileKey,
                AudioProfileManager.TYPE_NOTIFICATION);
        ...
    }
  1. reset时,设置sim2的uri值
    public void reset() {
        ...
        if(SystemProperties.get("ro.mtk_multisim_ringtone").equals("1")){
            String outdoorKey = mPredefinedKeys.get(Scenario.OUTDOOR.ordinal());
            long subId=SubscriptionManager.getSubId(0)[0];
            String uriString =
              Settings.System.getString(mContentResolver,
                                        AudioProfileManager.KEY_DEFAULT_RINGTONE);
            Uri uri = (uriString == null ? null : Uri.parse(uriString));
            persistRingtoneUriToDatabase(generalKey, AudioProfileManager.TYPE_RINGTONE, subId,
                                         uri);
            persistRingtoneUriToDatabase(outdoorKey, AudioProfileManager.TYPE_RINGTONE, subId,
                                         uri);
            subId=SubscriptionManager.getSubId(1)[0];
            
            // bailu modify start
            uriString = Settings.System.getString(mContentResolver,
                                                  AudioProfileManager.KEY_DEFAULT_RINGTONE_SIM2);
            // bailu modify end
            
            uri = (uriString == null ? null : Uri.parse(uriString));
            persistRingtoneUriToDatabase(generalKey, AudioProfileManager.TYPE_RINGTONE, subId,
                                         uri);
            persistRingtoneUriToDatabase(outdoorKey, AudioProfileManager.TYPE_RINGTONE, subId,
                                         uri);
        }
        ...
    }

6. alps/frameworks/base/media/java/com/mediatek/audioprofile/AudioProfileManager.java

  1. 定义SIM2的铃音key
    public static final String KEY_DEFAULT_RINGTONE_SIM2 = "mtk_audioprofile_default_ringtone_sim2";
  1. 增加SIM2的profile参数ringer_sim2_stream
    public enum ProfileSettings {
        ringer_stream, notification_stream, videocall_Stream, sipcall_Stream, ringer_sim2_stream, // bailu modify
        ringer_volume, notification_volume, alarm_volume,
        vibration_enabled, dtmftone_enabled, soundeffect_enbled, hapticfeedback_enabled,
        lockscreensound_enabled
    }

你可能感兴趣的:(Android-系统客制化)