android 9.0 系统铃声加载过程及添加卡二默认铃声

1,扫描系统内部存储、外部存储的铃声

以下是部分log;从log开始跟这个流程。

2019-12-06 10:18:55.493 3256-3256/? V/MediaScannerReceiver: onReceive action = android.intent.action.MEDIA_MOUNTED
2019-12-06 10:18:55.498 3256-3256/? D/MediaScannerReceiver: action: android.intent.action.MEDIA_MOUNTED path: /storage/emulated/0
2019-12-06 10:18:55.498 3256-3256/? V/MediaScannerReceiver: Mounted before boot completed with path: /storage/emulated/0
2019-12-06 10:20:08.781 3256-3256/? V/MediaScannerReceiver: onReceive action = android.intent.action.BOOT_COMPLETED
2019-12-06 10:20:08.781 3256-3256/? V/MediaScannerReceiver: onReceive BOOT_COMPLETED,begin to scan internal and external storage.
2019-12-06 10:20:08.811 3256-3256/? V/MediaScannerReceiver: Check whether all storage mounted,have waited 0ms
2019-12-06 10:20:08.813 3256-3256/? V/MediaScannerReceiver: isAllStorageMounted: path = /storage/emulated/0, state = mounted
2019-12-06 10:20:08.813 3256-3256/? V/MediaScannerReceiver: All storages have mountedor check time out, begin to scan.
2019-12-06 10:20:08.837 3256-3256/? D/MediaScannerService: onCreate: CpuCoreNum = 4, isLowRamDevice = false, mIsThreadPoolEnable = true
2019-12-06 10:20:08.844 3256-5165/? D/MediaScannerService: PrescanTask.doInBackground() mVolumn = internal
2019-12-06 10:20:08.860 3256-5168/? D/MediaScannerService: PrescanTask.doInBackground() mVolumn = external
2019-12-06 10:20:08.891 3256-5164/? V/MediaProvider: insertInternal: retrun MediaScannerUricontent://media/none/media_scanner

vendor/mediatek/proprietary/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java

  1.1 监听开机广播,等待所有存储都挂完毕后开始扫描

public void onReceive(Context context, Intent intent) {
          final String action = intent.getAction();
          Log.v(TAG, "onReceive action = " + action);
          if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
              Log.v(TAG,
              "onReceive BOOT_COMPLETED,begin to scan internal and external storage.");
              // Scan both internal and external storage
              //开始扫描内部存储及外部存储
              scan(context, MediaProvider.INTERNAL_VOLUME);
              /// M: only do scan external until all storages have been mounted or check time out.
              scanUntilAllStorageMounted(context);
              sIsBootComplete = true;
              ........
         } 
              ........
}

scan函数启动了MediaScannerService这个服务,负责扫描业务。

139      private void scan(Context context, String volume) {
140          Bundle args = new Bundle();
141          args.putString("volume", volume);
142          context.startService(
143                  new Intent(context, MediaScannerService.class).putExtras(args));
144      }

vendor/mediatek/proprietary/packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerService.java

在MediaScannerService被开启的时候,会先执行service的生命周期方法。

oncCeate,并在onStartCommand执行的时候开启异步任务PrescanTask处理扫描操作。

          @Override
          protected void onPostExecute(Void arg) {
             // start MediaScanner instance to scan mounted external SD card
              int what = MSG_SCAN_DIRECTORY;
              Message msg = mServiceHandler.obtainMessage(what, mStartId, -1, mBundle);
              mServiceHandler.sendMessage(msg);
              synchronized (MediaScannerService.this) {
                  mPrescanTaskList.remove(this);
              }
              super.onPostExecute(arg);
          }


           public void handleMessage(Message msg) {
                  。。。。。。。。
                  case MSG_SCAN_DIRECTORY:
                      handleScanDirectory(msg);
                     break;
                   。。。。。。。。
            }

         private void handleScanDirectory(Message msg) {
                  。。。。。。。。
                  scan(directories, volume);
                  。。。。。。。。
         }

最终是在scan方法中调用MediaScaner的scanDirectories方法扫描铃声文件

159      private void scan(String[] directories, String volumeName) {
160          Uri uri = Uri.parse("file://" + directories[0]);
161          // don't sleep while scanning
162          mWakeLock.acquire();
163  
164          try {
165              ContentValues values = new ContentValues();
166              values.put(MediaStore.MEDIA_SCANNER_VOLUME, volumeName);
167              Uri scanUri = getContentResolver().insert(MediaStore.getMediaScannerUri(), values);
168  
169              sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_STARTED, uri));
170  
171              try {
172                  if (volumeName.equals(MediaProvider.EXTERNAL_VOLUME)) {
173                      openDatabase(volumeName);
174                  }
175  
176                  try (MediaScanner scanner = new MediaScanner(this, volumeName)) {
177                      scanner.scanDirectories(directories);//MediaScanner扫描内部存储铃声文件
178                  }
179              } catch (Exception e) {
180                  Log.e(TAG, "exception in MediaScanner.scan()", e);
181              }
182  
183              getContentResolver().delete(scanUri, null, null);
184  
185          } catch (Exception ex) {
186              Log.e(TAG, "exception in MediaScanner.scan()", ex);
187          } finally {
188              sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_FINISHED, uri));
189              mWakeLock.release();
190              if (MediaUtils.LOG_SCAN) {
191                  Log.d(TAG, "scan(): volumeName = " + volumeName
192                      + ", directories = " + Arrays.toString(directories));
193              }
194          }
195      }

frameworks\base\media\java\android\media\MediaScanner.java

    public void scanDirectories(String[] directories) {
            ......
            for (int i = 0; i < directories.length; i++) {
                processDirectory(directories[i], mClient);//mClient is MyMediaScannerClient
            }
            ......
        }

这个processDirectory是定义在MediaScanner.cpp中的方法,最终会调用

doProcessDirectoryEntry(){
            //这里的client就是processDirectory(directories[i], mClient)传进来的mClient 
            status_t status = client.scanFile(path, statbuf.st_mtime, 0, true /*isDirectory*/, childNoMedia);
}

即最终处理扫描铃声的逻辑交给了MyMediaScannerClient。

04          @Override
605          public void scanFile(String path, long lastModified, long fileSize,
606                  boolean isDirectory, boolean noMedia) {
607              // This is the callback funtion from native codes.
608              // Log.v(TAG, "scanFile: "+path);
609              doScanFile(path, null, lastModified, fileSize, isDirectory, false, noMedia);
610          }
611  
612          public Uri doScanFile(String path, String mimeType, long lastModified,
613                  long fileSize, boolean isDirectory, boolean scanAlways, boolean noMedia) {
614              Uri result = null;
615  //            long t1 = System.currentTimeMillis();
616              try {
617                  FileEntry entry = beginFile(path, mimeType, lastModified,
618                          fileSize, isDirectory, noMedia);
619  
620                  if (entry == null) {
621                      return null;
622                  }
             result = endFile(entry, ringtones, notifications, alarms, music, podcasts);//处理电话铃声、通知铃声、闹钟铃声
            }
private static final String RINGTONES_DIR = "/ringtones/";
private static final String NOTIFICATIONS_DIR = "/notifications/";
private static final String ALARMS_DIR = "/alarms/"; 
private static final String SYSTEM_SOUNDS_DIR = "/system/media/audio";



1207      private static boolean isSystemSoundWithMetadata(String path) {
1208          if (path.startsWith(SYSTEM_SOUNDS_DIR + ALARMS_DIR)
1209                  || path.startsWith(SYSTEM_SOUNDS_DIR + RINGTONES_DIR)
1210                  || path.startsWith(SYSTEM_SOUNDS_DIR + NOTIFICATIONS_DIR)
1211                  || path.startsWith(PRODUCT_SOUNDS_DIR + ALARMS_DIR)
1212                  || path.startsWith(PRODUCT_SOUNDS_DIR + RINGTONES_DIR)
1213                  || path.startsWith(PRODUCT_SOUNDS_DIR + NOTIFICATIONS_DIR)) {
1214              return true;
1215          }
1216          return false;
1217      }

铃声文件/system/media/audio/ringtones/

在endFile中将以上路径下的遍历铃声文件添加到数据库,当遍历到的铃声文件名字跟配置的电话铃声名字一致的时候,将needToSetSettings 设置成true,表示这个铃声需要设置成默认铃声。

默认铃声的名字 mDefaultRingtoneFilename  :SystemProperties.get("ro.config.ringtone");

build\target\product\full_base.mk

# Additional settings used in all AOSP builds
PRODUCT_PROPERTY_OVERRIDES := \
    ro.config.ringtone=Ring_Synth_04.ogg \
    ro.config.notification_sound=pixiedust.ogg
 private static final String DEFAULT_RINGTONE_PROPERTY_PREFIX = "ro.config.";  
 //Settings.System.RINGTONE 
 //public static final String RINGTONE = "ringtone";//定义在Settings.java

private void setDefaultRingtoneFileNames() {
        mDefaultRingtoneFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.RINGTONE);
        mDefaultNotificationFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.NOTIFICATION_SOUND);
        mDefaultAlarmAlertFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.ALARM_ALERT);
		//lgy add for double simcard ringing
        mDefaultRingtoneSim2Filename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX 
				+ Settings.System.RINGTONE_SIM2);					
    }
 if (ringtones && ((mWasEmptyPriorToScan && !mDefaultRingtoneSet) ||
                    mMediaScannerClientEx.doesSettingEmpty("ringtone_set", mContext))) {
                if (TextUtils.isEmpty(mDefaultRingtoneFilename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultRingtoneFilename)) {//文件路径中是否有跟默认铃声文件名一样的铃声。
                    needToSetSettings = true;
                }
   }

设置默认铃声的Uri:setRingtoneIfNotSet 将对应的数据库的默认值初始化为匹配上铃声的tableUri+rowId

 if(needToSetSettings) {	
if (ringtones &&
                        mMediaScannerClientEx.doesSettingEmpty("ringtone_set", mContext)) {
                    setRingtoneIfNotSet(Settings.System.RINGTONE, tableUri, rowId);
                    mDefaultRingtoneSet = true;
                    mMediaScannerClientEx.setSettingFlag("ringtone_set", mContext);
}
}

到此为止,默认铃声已经添加上了。

以下ringtone_sim2是我添加的sim卡二的默认铃声。

以下是log:可以参考

卡一铃声匹配上时的log

2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java needToSetSettings =true
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java notifications =false
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java ringtones =true
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java tableUri =content://media/internal/audio/media
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java rowId =148
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java mDefaultRingtoneFilename =Ring_Synth_04.ogg
2019-12-06 10:20:43.898 3256-5164/? D/lgy_debug: MediaScanner.java mDefaultRingtoneSim2Filename =FreeFlight.ogg
2019-12-06 10:20:43.900 3256-5164/? D/lgy_debug: MediaScanner.java setRingtoneIfNotSet  44444444444 = ringtone

卡二:ringtone_sim2铃声匹配上的log:

2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java needToSetSettings =true
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java notifications =false
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java ringtones =true
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java tableUri =content://media/internal/audio/media
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java rowId =166
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java mDefaultRingtoneFilename =Ring_Synth_04.ogg
2019-12-06 10:20:51.571 3256-5164/? D/lgy_debug: MediaScanner.java mDefaultRingtoneSim2Filename =FreeFlight.ogg
2019-12-06 10:20:51.573 3256-5164/? D/lgy_debug: MediaScanner.java setRingtoneIfNotSet  44444444444 = ringtone_sim2
2019-12-06 10:20:51.573 3256-5164/? D/lgy_debug: MediaScanner.java setRingtoneIfNotSet  rowId = 166
2019-12-06 10:20:51.573 3256-5164/? D/lgy_debug: MediaScanner.java setRingtoneIfNotSet  uri = content://media/internal/audio/media
2019-12-06 10:20:51.614 3256-5164/? D/MediaScannerClientExImpl: setSettingFlag set:ringtone_sim2_set
2019-12-06 10:20:51.642 3256-5164/? D/lgy_debug: MediaScanner.java  333333333333
 

2 添加卡二铃声的步骤就是跟着卡一铃声添加就可以了。

添加卡二铃声数据库字段 

frameworks\base\core\java\android\provider\Settings.java


        /**lgy add */
        public static final String RINGTONE_SIM2 = "ringtone_sim2";
        public static final Uri DEFAULT_RINGTONE_SIM2_URI = getUriFor(RINGTONE_SIM2);
        public static final String RINGTONE_SIM2_CACHE = "ringtone_sim2_cache";
        public static final Uri RINGTONE_SIM2_CACHE_URI = getUriFor(RINGTONE_SIM2_CACHE);
        /**lgy add */

vendor\mediatek\proprietary\packages\apps\SettingsProvider\src\com\android\providers\settings\SettingsProvider.java

    public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
        final int userId = getUserIdFromUri(uri, UserHandle.getCallingUserId());
        if (userId != UserHandle.getCallingUserId()) {
            getContext().enforceCallingPermission(Manifest.permission.INTERACT_ACROSS_USERS,
                    "Access files from the settings of another user");
        }
        uri = ContentProvider.getUriWithoutUserId(uri);

        final String cacheRingtoneSetting;
        final String cacheName;
        if (Settings.System.RINGTONE_CACHE_URI.equals(uri)) {
            cacheRingtoneSetting = Settings.System.RINGTONE;
            cacheName = Settings.System.RINGTONE_CACHE;
        } else if (Settings.System.NOTIFICATION_SOUND_CACHE_URI.equals(uri)) {
            cacheRingtoneSetting = Settings.System.NOTIFICATION_SOUND;
            cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
        } else if (Settings.System.ALARM_ALERT_CACHE_URI.equals(uri)) {
            cacheRingtoneSetting = Settings.System.ALARM_ALERT;
            cacheName = Settings.System.ALARM_ALERT_CACHE;
        }  else if (Settings.System.RINGTONE_SIM2_CACHE_URI.equals(uri)) {//lgy add
            cacheRingtoneSetting = Settings.System.RINGTONE_SIM2;
            cacheName = Settings.System.RINGTONE_SIM2_CACHE;
        } else {
            throw new FileNotFoundException("Direct file access no longer supported; "
                    + "ringtone playback is available through android.media.Ringtone");
        }
private boolean mutateSystemSetting(String name, String value, int runAsUserId,
            int operation) {
        // Invalidate any relevant cache files
        String cacheName = null;
        if (Settings.System.RINGTONE.equals(name)) {
            cacheName = Settings.System.RINGTONE_CACHE;
        } else if (Settings.System.NOTIFICATION_SOUND.equals(name)) {
            cacheName = Settings.System.NOTIFICATION_SOUND_CACHE;
        } else if (Settings.System.ALARM_ALERT.equals(name)) {
            cacheName = Settings.System.ALARM_ALERT_CACHE;
        } else if (Settings.System.RINGTONE_SIM2.equals(name)) {//lgy add
            cacheName = Settings.System.RINGTONE_SIM2_CACHE;
        }

}

设置app中铃声ringtoneType种类ringtone、notification、alarm。vendor\mediatek\proprietary\packages\apps\MtkSettings\res\xml\sound_settings.xml

定义在frameworks\base\core\res\res\values\attrs.xml

我们需要添加卡二的铃声种类

    
    
        
        
            
            
            
            
            
            
            
            
			
            			
        
        
        
        
        
    

frameworks\base\media\java\android\media\RingtoneManager.java

public static final int TYPE_RINGTONE_SIM2  = 8;//添加卡二铃声类型

2.1 添加卡二铃声数据库查询类型selection 为IS_RINGTONE类型跟卡一一样都是电话铃声。这里不加的话,在设置中添加手动选择卡二铃声时会出现数据库空指针异常。

    private void setFilterColumnsList(int type) {
        List columns = mFilterColumns;
        columns.clear();
        
        if ((type & TYPE_RINGTONE) != 0) {
            columns.add(MediaStore.Audio.AudioColumns.IS_RINGTONE);
        }
        
        if ((type & TYPE_NOTIFICATION) != 0) {
            columns.add(MediaStore.Audio.AudioColumns.IS_NOTIFICATION);
        }
        
        if ((type & TYPE_ALARM) != 0) {
            columns.add(MediaStore.Audio.AudioColumns.IS_ALARM);
        }
		//lgy add 
		if ((type & TYPE_RINGTONE_SIM2) != 0) {
            columns.add(MediaStore.Audio.AudioColumns.IS_RINGTONE);
        }
    }
    private static String getSettingForType(int type) {
        if ((type & TYPE_RINGTONE) != 0) {
            return Settings.System.RINGTONE;
        } else if ((type & TYPE_NOTIFICATION) != 0) {
            return Settings.System.NOTIFICATION_SOUND;
        } else if ((type & TYPE_ALARM) != 0) {
            return Settings.System.ALARM_ALERT;
        } else if ((type & TYPE_RINGTONE_SIM2) != 0) {//lgy add
            return Settings.System.RINGTONE_SIM2;
        }else {
            return null;
        }
    }
    public static Uri getCacheForType(int type, int userId) {
        if ((type & TYPE_RINGTONE) != 0) {
            return ContentProvider.maybeAddUserId(Settings.System.RINGTONE_CACHE_URI, userId);
        } else if ((type & TYPE_NOTIFICATION) != 0) {
            return ContentProvider.maybeAddUserId(Settings.System.NOTIFICATION_SOUND_CACHE_URI,
                    userId);
        } else if ((type & TYPE_ALARM) != 0) {
            return ContentProvider.maybeAddUserId(Settings.System.ALARM_ALERT_CACHE_URI, userId);
        } else if ((type & TYPE_RINGTONE_SIM2) != 0) {//lgy
            return ContentProvider.maybeAddUserId(Settings.System.RINGTONE_SIM2_CACHE_URI, userId);
        }
        return null;
    }
    public static int getDefaultType(Uri defaultRingtoneUri) {
        defaultRingtoneUri = ContentProvider.getUriWithoutUserId(defaultRingtoneUri);
        if (defaultRingtoneUri == null) {
            return -1;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_RINGTONE_URI)) {
            return TYPE_RINGTONE;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_NOTIFICATION_URI)) {
            return TYPE_NOTIFICATION;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_ALARM_ALERT_URI)) {
            return TYPE_ALARM;
        } else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_RINGTONE_SIM2_URI)) {//lgy add
            return TYPE_RINGTONE_SIM2;
        } else {
            return -1;
        }
    }
    public static Uri getDefaultUri(int type) {
        if ((type & TYPE_RINGTONE) != 0) {
            return Settings.System.DEFAULT_RINGTONE_URI;
        } else if ((type & TYPE_NOTIFICATION) != 0) {
            return Settings.System.DEFAULT_NOTIFICATION_URI;
        } else if ((type & TYPE_ALARM) != 0) {
            return Settings.System.DEFAULT_ALARM_ALERT_URI;
        } else if ((type & TYPE_RINGTONE_SIM2) != 0) {//lgy add
            return Settings.System.DEFAULT_RINGTONE_SIM2_URI;
        } else {
            return null;
        }
    }

 

添加完卡二的基本配置后,在MediaScanner.java,扫描音乐文件时,同时默认卡二默认铃声。同卡一。

 

	/**lgy add */
	private String mDefaultRingtoneSim2Filename;
	private boolean mDefaultRingtoneSim2Set;
private void setDefaultRingtoneFileNames() {
        mDefaultRingtoneFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.RINGTONE);
        mDefaultNotificationFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.NOTIFICATION_SOUND);
        mDefaultAlarmAlertFilename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX
                + Settings.System.ALARM_ALERT);
		//lgy add 
        mDefaultRingtoneSim2Filename = SystemProperties.get(DEFAULT_RINGTONE_PROPERTY_PREFIX 
				+ Settings.System.RINGTONE_SIM2);					
    }
 private Uri endFile(FileEntry entry, boolean ringtones, boolean notifications,
                boolean alarms, boolean music, boolean podcasts)
                throws RemoteException {
            boolean needToSetSettings = false;
            // Setting a flag in order not to use bulk insert for the file related with
            // notifications, ringtones, and alarms, because the rowId of the inserted file is
            // needed.
            if (notifications && ((mWasEmptyPriorToScan && !mDefaultNotificationSet) ||
                    mMediaScannerClientEx.doesSettingEmpty("notification_set", mContext))) {
                if (TextUtils.isEmpty(mDefaultNotificationFilename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultNotificationFilename)) {
                    needToSetSettings = true;
                }
            } else if (ringtones && ((mWasEmptyPriorToScan && !mDefaultRingtoneSet) ||
                    mMediaScannerClientEx.doesSettingEmpty("ringtone_set", mContext))) {
                if (TextUtils.isEmpty(mDefaultRingtoneFilename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultRingtoneFilename)) {
                    needToSetSettings = true;
                }
            } else if (alarms && ((mWasEmptyPriorToScan && !mDefaultAlarmSet) ||
                    mMediaScannerClientEx.doesSettingEmpty("alarm_set", mContext))) {
                if (TextUtils.isEmpty(mDefaultAlarmAlertFilename) ||
                        doesPathHaveFilename(entry.mPath, mDefaultAlarmAlertFilename)) {
                    needToSetSettings = true;
                }
            } //lgy add
			else if(ringtones && ((mWasEmptyPriorToScan && !mDefaultRingtoneSim2Set) ||
                     mMediaScannerClientEx.doesSettingEmpty("ringtone_sim2_set", mContext))) {
                 if (TextUtils.isEmpty(mDefaultRingtoneSim2Filename) ||
                         doesPathHaveFilename(entry.mPath, mDefaultRingtoneSim2Filename)) {
                     needToSetSettings = true;
                 }
             }


        ................
        ................
if(needToSetSettings) {				
                if (notifications &&
                        mMediaScannerClientEx.doesSettingEmpty("notification_set", mContext)) {
                    setRingtoneIfNotSet(Settings.System.NOTIFICATION_SOUND, tableUri, rowId);
                    mDefaultNotificationSet = true;
                    mMediaScannerClientEx.setSettingFlag("notification_set", mContext);
                } else if (ringtones &&
                        mMediaScannerClientEx.doesSettingEmpty("ringtone_set", mContext)) {
                    setRingtoneIfNotSet(Settings.System.RINGTONE, tableUri, rowId);
                    mDefaultRingtoneSet = true;
                    mMediaScannerClientEx.setSettingFlag("ringtone_set", mContext);
					 	    //lgy add
					if(!TextUtils.isEmpty(mDefaultRingtoneSim2Filename) 
						&& !TextUtils.isEmpty(mDefaultRingtoneFilename)
						&& mDefaultRingtoneSim2Filename.equals(mDefaultRingtoneFilename)
						&& mMediaScannerClientEx.doesSettingEmpty("ringtone_sim2_set",mContext)){
						setRingtoneIfNotSet(Settings.System.RINGTONE_SIM2,tableUri,rowId);
						Log.d("lgy_debug","MediaScanner.java  2222222222");
						mDefaultRingtoneSim2Set = true;
						mMediaScannerClientEx.setSettingFlag("ringtone_sim2_set",mContext);
					}
             }else if(ringtones &&
                      mMediaScannerClientEx.doesSettingEmpty("ringtone_sim2_set",mContext)){
                 setRingtoneIfNotSet(Settings.System.RINGTONE_SIM2,tableUri,rowId);
                 mDefaultRingtoneSim2Set = true;
                 mMediaScannerClientEx.setSettingFlag("ringtone_sim2_set",mContext);
				 Log.d("lgy_debug","MediaScanner.java  333333333333");
                 //lgy add end
                } else if (alarms &&
                        mMediaScannerClientEx.doesSettingEmpty("alarm_set", mContext)) {
                    setRingtoneIfNotSet(Settings.System.ALARM_ALERT, tableUri, rowId);
                    mDefaultAlarmSet = true;
                    mMediaScannerClientEx.setSettingFlag("alarm_set", mContext);
                }
            }

            return result;
        }

}

卡二铃声默认上了。卡二来电了,默认跑本身的rongtone。

需要做判断。

vendor\mediatek\proprietary\packages\services\Telecomm\src\com\android\server\telecom\RingtoneFactory.java

import android.telephony.SubscriptionManager;//lgy


    public Ringtone getRingtone(Call incomingCall) {
        // Use the default ringtone of the work profile if the contact is a work profile contact.
        Context userContext = isWorkContact(incomingCall) ?
                getWorkProfileContextForUser(mCallsManager.getCurrentUserHandle()) :
                getContextForUserHandle(mCallsManager.getCurrentUserHandle());
        Uri ringtoneUri = incomingCall.getRingtone();
        Ringtone ringtone = null;
		
		//lgy add 
		int subId = incomingCall.getSubsciptionId();
		int phoneId = SubscriptionManager.getPhoneId(subId);
		//lgy add
		
        if(ringtoneUri != null && userContext != null) {
            // Ringtone URI is explicitly specified. First, try to create a Ringtone with that.
            ringtone = RingtoneManager.getRingtone(userContext, ringtoneUri);
        }
        if(ringtone == null) {
            // Contact didn't specify ringtone or custom Ringtone creation failed. Get default
            // ringtone for user or profile.
            Context contextToUse = hasDefaultRingtoneForUser(userContext) ? userContext : mContext;
            Uri defaultRingtoneUri;
            if (UserManager.get(contextToUse).isUserUnlocked(contextToUse.getUserId())) {
                //lgy add double card ringtone
                switch(phoneId){
                    case 0:
                         defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(contextToUse,
                                RingtoneManager.TYPE_RINGTONE);
                        break;
                    case 1:
                        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(contextToUse,
                                RingtoneManager.TYPE_RINGTONE_SIM2);
                        break;
                    default:
                        defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(contextToUse,
                                RingtoneManager.TYPE_RINGTONE);
                        break;
                    }
                //lgy add double card ringtone
            } else {
                defaultRingtoneUri = Settings.System.DEFAULT_RINGTONE_URI;
            }
            if (defaultRingtoneUri == null) {
                return null;
            }
            ringtone = RingtoneManager.getRingtone(contextToUse, defaultRingtoneUri);
        }
        if (ringtone != null) {
            ringtone.setStreamType(AudioManager.STREAM_RING);
        }
        return ringtone;
    }

 

你可能感兴趣的:(源码分析)