Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android Music 中如何添加設置雙卡鈴聲的菜單

android Music 中如何添加設置雙卡鈴聲的菜單

編輯:關於Android編程

1,打開情景模式的設置雙卡鈴聲的feature:MTK_MULTISIM_ROINGTONE_SUPPORT,Music中卻不能設置雙卡鈴聲。 希望能在Music添加菜單“Us as SIM1/SIM2 ringtone”

1,string.xml,添加新的string ringtone_as_sim1_menu和ringtone_as_sim2_menu :

Use as SIM1 ringtone
Use as SIM2 ringtone

2,TrackBrowserActivity.java:
1),添加:
import com.mediatek.telephony.SimInfoManager;
import com.mediatek.common.featureoption.FeatureOption;


2),menu根據插卡情況添加Us as SIM1 ringtone/Us as SIM2 ringtone, 參考//start modify和//end modify之間的修改
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {
....
int isDrm = 0;
if (MusicFeatureOption.IS_SUPPORT_DRM) {
isDrm = mTrackCursor.getInt(mTrackCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.IS_DRM));
int drmMethod = mTrackCursor.getInt(mTrackCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DRM_METHOD));
if (canDispalyRingtone(isDrm, drmMethod)) {
//start modify
if(FeatureOption.MTK_GEMINI_SUPPORT &&FeatureOption.MTK_MULTISIM_RINGTONE_SUPPORT&&(SimInfoManager.getInsertedSimCount(this)==2)){
menu.add(0, USE_AS_SIM1_RINGTONE, 0, R.string.ringtone_as_sim1_menu);
menu.add(0, USE_AS_SIM2_RINGTONE, 0, R.string.ringtone_as_sim2_menu);
}else //end modify
menu.add(0, USE_AS_RINGTONE, 0, R.string.ringtone_menu);
}
} else {
//start modify
if(FeatureOption.MTK_GEMINI_SUPPORT &&(SIMInfoWrapper.getDefault().getInsertedSimCount()==2)){
menu.add(0, USE_AS_SIM1_RINGTONE, 0, R.string.ringtone_as_sim1_menu);
menu.add(0, USE_AS_SIM2_RINGTONE, 0, R.string.ringtone_as_sim2_menu);
}else //end modify
menu.add(0, USE_AS_RINGTONE, 0, R.string.ringtone_menu);
}
....
}


3),添加USE_AS_SIM1_RINGTONE/USE_AS_SIM2_RINGTONE case的處理,參考//start modify和//end modify之間的修改
public boolean onContextItemSelected(MenuItem item) {
....
switch (item.getItemId()) {
....
case USE_AS_RINGTONE:
// Set the system setting to make this the current ringtone
MusicUtils.setRingtone(this, mSelectedId);
return true;
//start modify
case USE_AS_SIM1_RINGTONE:
// Set the system setting to make this the current ringtone
MusicUtils.setRingtone(this, mSelectedId,0);
return true;
case USE_AS_SIM2_RINGTONE:
// Set the system setting to make this the current ringtone
MusicUtils.setRingtone(this, mSelectedId,1);
return true;
//end modify
....
}
3,MusicUtils.java:
1),
import com.mediatek.audioprofile.AudioProfileManager;
import com.mediatek.telephony.SimInfoManager;
import com.mediatek.common.featureoption.FeatureOption;

2),添加USE_AS_SIM1_RINGTONE/USE_AS_SIM2_RINGTONE的定義,並修改CHILD_MENU_BASE:
public interface Defs {
....
/// M: add for drm
public final static int DRM_INFO = 15;
public final static int USE_AS_SIM1_RINGTONE = 16;
public final static int USE_AS_SIM2_RINGTONE = 17;
//public final static int CHILD_MENU_BASE = 16; // this should be the last item
public final static int CHILD_MENU_BASE = 18;


2)增加新的setRingtone()參數中增加slotId
a)請查詢下packages\apps\settings\src\com\mediatek\audioprofile\DefaultRingtonePreference.java 的onClick()是否有“ setSimId(simList.get(0).mSimId);” 這句,這裡表示使用單卡的時候,也加上SIM ID,如果有這句話請參考本條如下修改,如果沒有下一條 b)的修改,:
static void setRingtone(Context context, long id, int slotID) {
ContentResolver resolver = context.getContentResolver();
// Set the flag in the database to mark this as a ringtone
Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
try {
ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
values.put(MediaStore.Audio.Media.IS_ALARM, "1");
resolver.update(ringUri, values, null, null);
} catch (UnsupportedOperationException ex) {
// most likely the card just got unmounted
MusicLogUtils.e(TAG, "couldn't set ringtone flag for id " + id);
return;
}

String[] cols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};
/// M: use selectionArgs replace set query value in where @{
String where = MediaStore.Audio.Media._ID + "=?";
String[] whereArgs = new String[]{String.valueOf(id)};
Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols, where , whereArgs, null);
/// @}



try {
if (cursor != null && cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();

AudioProfileManager mProfileManager = (AudioProfileManager) context.getSystemService(Context.AUDIOPROFILE_SERVICE);
String mActiveProfileKey = mProfileManager.getActiveProfileKey();


List simList = SIMInfo.getInsertedSIMList(this.getContext());
int simNum = simList.size();

if (FeatureOption.MTK_MULTISIM_RINGTONE_SUPPORT&&(slotId==-1)&&(simNum==1 )) {

String uriKey=mActiveProfileKey + SUFFIX_RINGER_URI+SUFFIX_SIM_ID+simList.get(0).mSimId;
Settings.System.putString(resolver,uriKey, ringUri.toString());
}
else if(FeatureOption.MTK_MULTISIM_RINGTONE_SUPPORT&&(slotId!=-1)){
long simId = SimInfoManager.getIdBySlot(context, slotId);
mProfileManager.setRingtoneUri(mActiveProfileKey, AudioProfileManager.TYPE_RINGTONE, simId, ringUri);
}
else{
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
}
String message = context.getString(R.string.ringtone_set, cursor.getString(2));
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
} finally {
....
}
}
b)packages\apps\settings\src\com\mediatek\audioprofile\DefaultRingtonePreference.java 的onClick()是否有“ setSimId(simList.get(0).mSimId);” 這句,如果沒有這句話請參考本條如下修改:
static void setRingtone(Context context, long id, int slotID) {
ContentResolver resolver = context.getContentResolver();
// Set the flag in the database to mark this as a ringtone
Uri ringUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
try {
ContentValues values = new ContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE, "1");
values.put(MediaStore.Audio.Media.IS_ALARM, "1");
resolver.update(ringUri, values, null, null);
} catch (UnsupportedOperationException ex) {
// most likely the card just got unmounted
MusicLogUtils.e(TAG, "couldn't set ringtone flag for id " + id);
return;
}

String[] cols = new String[] {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};
/// M: use selectionArgs replace set query value in where @{
String where = MediaStore.Audio.Media._ID + "=?";
String[] whereArgs = new String[]{String.valueOf(id)};
Cursor cursor = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols, where , whereArgs, null);
/// @}



try {
if (cursor != null && cursor.getCount() == 1) {
// Set the system setting to make this the current ringtone
cursor.moveToFirst();

AudioProfileManager mProfileManager = (AudioProfileManager) context.getSystemService(Context.AUDIOPROFILE_SERVICE);
String mActiveProfileKey = mProfileManager.getActiveProfileKey();

if(FeatureOption.MTK_MULTISIM_RINGTONE_SUPPORT&&(slotId!=-1)){
long simId = SimInfoManager.getIdBySlot(context, slotId);
mProfileManager.setRingtoneUri(mActiveProfileKey, AudioProfileManager.TYPE_RINGTONE, simId, ringUri);
}
else{
Settings.System.putString(resolver, Settings.System.RINGTONE, ringUri.toString());
}
String message = context.getString(R.string.ringtone_set, cursor.getString(2));
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
} finally {
....
}
}


3)原來的setRingtone(Context context, long id)改成調用 setRingtone(Context context, long id, int slotID),但是slotId為-1:
static void setRingtone(Context context, long id) {
setRingtone(context, id,-1);
}

由於KK版本中刪除了個別接口,目前的做法是恢復JB版本的接口。
對於KK版本中Music設置雙卡鈴聲,請繼續做如下修改:
1、在MusicUtils.java中開始處添加如下代碼:
import java.util.List;
import android.provider.Telephony.SIMInfo;

2、確認在Music的Android.mk中添加telephony-common的library:
LOCAL_JAVA_LIBRARIES += mediatek-framework \
telephony-common

3、在mediatek\frameworks\base\telephony\java\com\mediatek\telephony\SimInfoManager.java中添加如下方法:
/**
* Given a slot, return the Id of the SIM which is currently inserted in that slot
* @param ctx
* @param simSlotId the slot which the SIM is inserted
* @return the index of the SIM card in database, 0 indicate that no SIM card is inserted
*/
public static long getIdBySlot(Context ctx, int simSlotId) {
logd("[getIdBySlot]+ simSlotId:" + simSlotId);
SimInfoRecord simInfo = getSimInfoBySlot(ctx, simSlotId);
if (simInfo != null) {
logd("[getIdBySlot]- simInfoId:" + simInfo.mSimInfoId);
return simInfo.mSimInfoId;
}
logd("[getIdBySlot]- null info, return 0");
return 0;
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved