mt6735 FileManager中点击音乐文件设置为来电铃声
时间:10-02
整理:3721RD
点击:
[Audio App]FileManager 中点击音乐文件设置为来电铃声
[DESCRIPTION]
希望在FileManager中点击歌曲,增加选项可以设置歌曲为来电铃声这个feature。
[SOLUTION]
以下是JB.SP版本参考例子,在6589JB.sp版本上验证成功。其它版本请类似参考。
Step1:androidManifest.xml
Addusepermission
<uses-permission android:name="android.permission.WRITE_SETTING"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Step2:resource
res\menu\popup_menu.xml
<item android:id="@+id/set_ringtone"
android:title="@string/set_ringtone"/>
res\values\strings.xml
<string name="set_ringtone">Use As Ringtone</string>
UseAsRingtone
多国语言需要添加在对应的values_xxxx\strings.xml中
Step3:import
FileManagerOperationActivity.java
importandroid.provider.MediaStore;
importandroid.database.Cursor;
importandroid.content.ContentResolver;
importandroid.content.ContentValues;
importandroid.provider.Settings;
importandroid.widget.Toast;
importandroid.content.ContentUris;
importandroid.media.MediaFile;
Step4:显示、隐藏setRingtone菜单
updateEditPopupMenu()方法改为如下
privatevoidupdateEditPopupMenu(){
finalMenumenu=mEditPopupMenu.getMenu();
intselectedCount=mAdapter.getCheckedItEMSCount();
//remove(disable)protectioninfoicon
menu.removeItem(R.id.protection_info);
if(selectedCount==0){
menu.findItem(R.id.rename).setEnabled(false);
menu.findItem(R.id.details).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
}elseif(selectedCount==1){
//enabledetailsicon
menu.findItem(R.id.details).setEnabled(true);
//enablerenameicon
if(mAdapter.getCheckedFileInfoItemsList().get(0).getFile().canWrite()){
menu.findItem(R.id.rename).setEnabled(true);
}
//add
//onlyaudiofilecanenablesetringtone
FileInfofileInfo1=mAdapter.getCheckedFileInfoItemsList().get(0);
if(!fileInfo1.isDrmFile()
&&!fileInfo1.isDirectory()
&&IsMusic(fileInfo1)){
LogUtils.d(TAG,"needsetringtone");
menu.findItem(R.id.set_ringtone).setEnabled(true);
}else{
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
//enableprotectioninfoicon
FileInfofileInfo=mAdapter.getCheckedFileInfoItemsList().get(0);
if(fileInfo.isDrmFile()){
Stringpath=fileInfo.getFileAbsolutePath();
if(DrmManager.getInstance().checkDrmObjectType(path)){
StringmimeType=DrmManager.getInstance().getOriginalMimeType(path);
if(mimeType!=null&&mimeType.trim().length()!=0){
menu.add(0,R.id.protection_info,0,
com.mediatek.internal.R.string.drm_protectioninfo_title);
}
}
}
}else{
//disabledetailsicon
menu.findItem(R.id.details).setEnabled(false);
//disablerenameicon
menu.findItem(R.id.rename).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
}
Step5:.判断选择文件是否是audio文件
定义IsMusic方法
privatebooleanIsMusic(FileInfofileinfo){
StringmimeType=fileinfo.getFileMimeType(mService);
intfileType=MediaFile.getFileTypeForMimeType(mimeType);
if(mimeType.startsWith("audio/")||MediaFile.isAudioFileType(fileType)){
returntrue;
}else{
returnfalse;
}
}
Step6:定义设置ringtone方法
新增方法setRingtone
publicvoidsetRingtone(MenuItemmenuItem){
LogUtils.d(TAG,"startsetRingtone");
FileInfofileinfo=mAdapter.getCheckedFileInfoItemsList().get(0);
ContentResolverresolver=getApplicationContext().getContentResolver();
Stringfilepath=fileinfo.getFileAbsolutePath();
Longfileid=0L;
//Urifileuri=Urifileuri=getUriForPath(filepath);
Urifileuri=MediaStore.Audio.Media.getContentUriForPath(filepath);
LogUtils.d(TAG,"setRingtoneuri"+fileuri);
LogUtils.d(TAG,"setRingtonepath"+filepath);
Cursorfile_cursor=resolver.query(
fileuri,
null,
MediaStore.MediaColumns.DATA+"=?",
newString[]{filepath},
null);
if(file_cursor.moveToFirst()&&file_cursor.getCount()>0){
String_id=file_cursor.getString(0);
fileid=Long.valueOf(_id);
}
UriringUri=ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
fileid);
try{
ContentValuesvalues=newContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE,"1");
values.put(MediaStore.Audio.Media.IS_ALARM,"1");
resolver.update(ringUri,values,null,null);
}catch(UnsupportedOperationExceptionex){
LogUtils.d(TAG,"couldn'tsetringtoneflagforid"+ringUri);
return;
}
LogUtils.d(TAG,"setRingtoneringUri"+ringUri.toString());
String[]cols=newString[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};
///M:useselectionArgsreplacesetqueryvalueinwhere@{
Stringwhere=MediaStore.Audio.Media._ID+"=?";
String[]whereArgs=newString[]{String.valueOf(fileid)};
Cursorcursor=resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols,
where,
whereArgs,
null);
///@}
try{
if(cursor!=null&&cursor.getCount()==1){
//Setthesystemsettingtomakethisthecurrentringtone
cursor.moveToFirst();
Settings.System.putString(resolver,Settings.System.RINGTONE,ringUri.toString());
Toast.makeText(getApplicationContext(),"setRingtoneSuccess",Toast.LENGTH_SHORT).show();
}
}finally{
if(cursor!=null){
cursor.close();
}
}
}
Step7:重新定义MediaStore.javagetContentUriForPath方法
之所以定义getContentUriForPath,是因为当时开发该feature的时候,发现getContentUriForPath这个函数有bug,当
路径是/storage/sdcard1/时候,无法正确返回uri,返回来的竟然是Internal_content_uri。
publicstaticUrigetContentUriForPath(Stringpath){
return((path.startsWith(Environment.getrootDirectory()+"/media"))?
INTERNAL_CONTENT_URI:EXTERNAL_CONTENT_URI);
}
Step8:响应setRingtone
在onMenuItemCLIck方法里面增加
caseR.id.set_ringtone:
setRingtone(menuItem);
break;
最后编译
./mkmm./mediatek/packages/apps/FileManager/
[DESCRIPTION]
希望在FileManager中点击歌曲,增加选项可以设置歌曲为来电铃声这个feature。
[SOLUTION]
以下是JB.SP版本参考例子,在6589JB.sp版本上验证成功。其它版本请类似参考。
Step1:androidManifest.xml
Addusepermission
<uses-permission android:name="android.permission.WRITE_SETTING"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Step2:resource
res\menu\popup_menu.xml
<item android:id="@+id/set_ringtone"
android:title="@string/set_ringtone"/>
res\values\strings.xml
<string name="set_ringtone">Use As Ringtone</string>
UseAsRingtone
多国语言需要添加在对应的values_xxxx\strings.xml中
Step3:import
FileManagerOperationActivity.java
importandroid.provider.MediaStore;
importandroid.database.Cursor;
importandroid.content.ContentResolver;
importandroid.content.ContentValues;
importandroid.provider.Settings;
importandroid.widget.Toast;
importandroid.content.ContentUris;
importandroid.media.MediaFile;
Step4:显示、隐藏setRingtone菜单
updateEditPopupMenu()方法改为如下
privatevoidupdateEditPopupMenu(){
finalMenumenu=mEditPopupMenu.getMenu();
intselectedCount=mAdapter.getCheckedItEMSCount();
//remove(disable)protectioninfoicon
menu.removeItem(R.id.protection_info);
if(selectedCount==0){
menu.findItem(R.id.rename).setEnabled(false);
menu.findItem(R.id.details).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
}elseif(selectedCount==1){
//enabledetailsicon
menu.findItem(R.id.details).setEnabled(true);
//enablerenameicon
if(mAdapter.getCheckedFileInfoItemsList().get(0).getFile().canWrite()){
menu.findItem(R.id.rename).setEnabled(true);
}
//add
//onlyaudiofilecanenablesetringtone
FileInfofileInfo1=mAdapter.getCheckedFileInfoItemsList().get(0);
if(!fileInfo1.isDrmFile()
&&!fileInfo1.isDirectory()
&&IsMusic(fileInfo1)){
LogUtils.d(TAG,"needsetringtone");
menu.findItem(R.id.set_ringtone).setEnabled(true);
}else{
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
//enableprotectioninfoicon
FileInfofileInfo=mAdapter.getCheckedFileInfoItemsList().get(0);
if(fileInfo.isDrmFile()){
Stringpath=fileInfo.getFileAbsolutePath();
if(DrmManager.getInstance().checkDrmObjectType(path)){
StringmimeType=DrmManager.getInstance().getOriginalMimeType(path);
if(mimeType!=null&&mimeType.trim().length()!=0){
menu.add(0,R.id.protection_info,0,
com.mediatek.internal.R.string.drm_protectioninfo_title);
}
}
}
}else{
//disabledetailsicon
menu.findItem(R.id.details).setEnabled(false);
//disablerenameicon
menu.findItem(R.id.rename).setEnabled(false);
//add
menu.findItem(R.id.set_ringtone).setEnabled(false);
}
}
Step5:.判断选择文件是否是audio文件
定义IsMusic方法
privatebooleanIsMusic(FileInfofileinfo){
StringmimeType=fileinfo.getFileMimeType(mService);
intfileType=MediaFile.getFileTypeForMimeType(mimeType);
if(mimeType.startsWith("audio/")||MediaFile.isAudioFileType(fileType)){
returntrue;
}else{
returnfalse;
}
}
Step6:定义设置ringtone方法
新增方法setRingtone
publicvoidsetRingtone(MenuItemmenuItem){
LogUtils.d(TAG,"startsetRingtone");
FileInfofileinfo=mAdapter.getCheckedFileInfoItemsList().get(0);
ContentResolverresolver=getApplicationContext().getContentResolver();
Stringfilepath=fileinfo.getFileAbsolutePath();
Longfileid=0L;
//Urifileuri=Urifileuri=getUriForPath(filepath);
Urifileuri=MediaStore.Audio.Media.getContentUriForPath(filepath);
LogUtils.d(TAG,"setRingtoneuri"+fileuri);
LogUtils.d(TAG,"setRingtonepath"+filepath);
Cursorfile_cursor=resolver.query(
fileuri,
null,
MediaStore.MediaColumns.DATA+"=?",
newString[]{filepath},
null);
if(file_cursor.moveToFirst()&&file_cursor.getCount()>0){
String_id=file_cursor.getString(0);
fileid=Long.valueOf(_id);
}
UriringUri=ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
fileid);
try{
ContentValuesvalues=newContentValues(2);
values.put(MediaStore.Audio.Media.IS_RINGTONE,"1");
values.put(MediaStore.Audio.Media.IS_ALARM,"1");
resolver.update(ringUri,values,null,null);
}catch(UnsupportedOperationExceptionex){
LogUtils.d(TAG,"couldn'tsetringtoneflagforid"+ringUri);
return;
}
LogUtils.d(TAG,"setRingtoneringUri"+ringUri.toString());
String[]cols=newString[]{
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE
};
///M:useselectionArgsreplacesetqueryvalueinwhere@{
Stringwhere=MediaStore.Audio.Media._ID+"=?";
String[]whereArgs=newString[]{String.valueOf(fileid)};
Cursorcursor=resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
cols,
where,
whereArgs,
null);
///@}
try{
if(cursor!=null&&cursor.getCount()==1){
//Setthesystemsettingtomakethisthecurrentringtone
cursor.moveToFirst();
Settings.System.putString(resolver,Settings.System.RINGTONE,ringUri.toString());
Toast.makeText(getApplicationContext(),"setRingtoneSuccess",Toast.LENGTH_SHORT).show();
}
}finally{
if(cursor!=null){
cursor.close();
}
}
}
Step7:重新定义MediaStore.javagetContentUriForPath方法
之所以定义getContentUriForPath,是因为当时开发该feature的时候,发现getContentUriForPath这个函数有bug,当
路径是/storage/sdcard1/时候,无法正确返回uri,返回来的竟然是Internal_content_uri。
publicstaticUrigetContentUriForPath(Stringpath){
return((path.startsWith(Environment.getrootDirectory()+"/media"))?
INTERNAL_CONTENT_URI:EXTERNAL_CONTENT_URI);
}
Step8:响应setRingtone
在onMenuItemCLIck方法里面增加
caseR.id.set_ringtone:
setRingtone(menuItem);
break;
最后编译
./mkmm./mediatek/packages/apps/FileManager/