編輯:關於 Android 程式設計
需求背景:情景模式已經打開了雙卡鈴聲的 feature(MTK_MULTISIM_RINGTONE_SUPPORT),但 Music 裡卻還不能設定雙卡鈴聲。目標是在 Music 的長按選單中加上「Use as SIM1 ringtone」與「Use as SIM2 ringtone」兩個選項。
<string name="ringtone_as_sim1_menu">Use as SIM1 ringtone</string> <string name="ringtone_as_sim2_menu">Use as SIM2 ringtone</string>
1)先補上需要的 import:
import com.mediatek.telephony.SimInfoManager; import com.mediatek.common.featureoption.FeatureOption;
2)在建立選單時,依插卡情況決定顯示「單卡鈴聲」還是「SIM1/SIM2 鈴聲」(見 //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)在 onContextItemSelected 中處理新選項,多傳一個 slotId 給 setRingtone:
public boolean onContextItemSelected(MenuItem item) {
// ...
switch (item.getItemId()) {
// ...
case USE_AS_RINGTONE:
// 設為目前的鈴聲
MusicUtils.setRingtone(this, mSelectedId);
return true;
//start modify
case USE_AS_SIM1_RINGTONE:
MusicUtils.setRingtone(this, mSelectedId, 0);
return true;
case USE_AS_SIM2_RINGTONE:
MusicUtils.setRingtone(this, mSelectedId, 1);
return true;
//end modify
// ...
}
}
1)補上 import:
import com.mediatek.audioprofile.AudioProfileManager; import com.mediatek.telephony.SimInfoManager; import com.mediatek.common.featureoption.FeatureOption;
2)定義兩個新的選單常數,並把 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;
}
3)為 setRingtone 增加 slotId 參數。實作前請先確認 packages\apps\settings\src\com\mediatek\audioprofile\DefaultRingtonePreference.java 的 onClick() 裡有沒有 setSimId(simList.get(0).mSimId); 這一句。
a)若有這一句,表示單卡時也會帶上 SIM ID,請參考下面的寫法:
static void setRingtone(Context context, long id, int slotID) {
ContentResolver resolver = context.getContentResolver();
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) {
// 多數情況是卡片剛被移除
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: 用 selectionArgs 取代直接拼接 query 條件 @{
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) {
cursor.moveToFirst();
AudioProfileManager mProfileManager = (AudioProfileManager)
context.getSystemService(Context.AUDIOPROFILE_SERVICE);
String mActiveProfileKey = mProfileManager.getActiveProfileKey();
List<SIMInfo> 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)若沒有這一句,則改採下面的寫法(單卡時仍走原本的 Settings.System.RINGTONE):
static void setRingtone(Context context, long id, int slotID) {
ContentResolver resolver = context.getContentResolver();
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) {
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
};
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) {
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, long) 改成呼叫三參數版本,slotId 傳 -1:
static void setRingtone(Context context, long id) {
setRingtone(context, id, -1);
}
由於 KK 版本刪掉了部分介面,目前的做法是把 JB 版本的介面補回來。請接著做以下修改:
1、在 MusicUtils.java 開頭加上:
import java.util.List; import android.provider.Telephony.SIMInfo;
2、確認 Music 的 Android.mk 有加入 telephony-common:
LOCAL_JAVA_LIBRARIES += mediatek-framework \
telephony-common
3、在 mediatek\frameworks\base\telephony\java\com\mediatek\telephony\SimInfoManager.java 中補上這個方法:
/**
* 給定一個卡槽,回傳目前插入該卡槽的 SIM 的 Id
* @param ctx
* @param simSlotId 插入 SIM 卡的卡槽
* @return 該 SIM 卡於資料庫中的索引,0 代表沒有插入 SIM 卡
*/
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;
}
Android + SSH 開發商城 App(一):數據庫建置與環境搭建
項目簡介 本系列文章示範如何用 SSH(Struts、Spring、Hibernate)打造 Android 的後端伺服器,並完成前端 App。涵蓋的技術點相當廣泛,包含
Android VideoView 本地視頻播放與 MediaController 控制
VideoView 是把 MediaPlayer 包好的現成元件,直接放進版面就能播視頻,是三者之中最省力的做法。缺點是它內建的控制能力很少——基本上只有 start 與
RecyclerView 手勢操作:滑動刪除與拖動排序
如果還在用 ListView,其實可以考慮換掉了。RecyclerView 自動幫你回收 Item 檢視(ViewHolder 複用),允許自定義各種動畫與分隔線,也允許
Android ORM 框架之 greenDAO 應用基礎
greenDAO 簡介greenDAO 是 Android 上常見的 ORM 框架,具有高效能、使用簡潔與可加密等特色。greenDAO 3 提供兩種使用方式:Java