Android FM模块学习之四源码分析(3)
接着看FM模块的其他几个次要的类的源码。这样来看FM上层的东西不是太多。
请看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\Settings.java
protected void onCreate(BundlesavedInstanceState)
从FMRadio.java用使用Intent跳转携带参数过来,在onCreate获取携带数据。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent != null) {
mRxMode = intent.getBooleanExtra(RX_MODE, false);
}
mPrefs = new FmSharedPreferences(this);
if (mPrefs != null) {
setPreferenceScreen(createPreferenceHierarchy());
}
}
private PreferenceScreen createPreferenceHierarchy() 初始化创建收音机设置里显示控件
private PreferenceScreen createPreferenceHierarchy() {
int index = 0;
if (mPrefs == null) {
return null;
}
// Root
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
this); summaryBandItems = getResources().getStringArray(
R.array.regional_band_summary);
chSpacingItems = getResources().getStringArray(
R.array.channel_spacing_entries);
mBandPreference = new ListPreference(this);
mBandPreference.setEntries(R.array.regional_band_entries);
mBandPreference.setEntryValues(R.array.regional_band_values);
mBandPreference.setDialogTitle(R.string.sel_band_menu);
mBandPreference.setKey(REGIONAL_BAND_KEY);
mBandPreference.setTitle(R.string.regional_band);index = FmSharedPreferences.getCountry();
Log.d(LOGTAG, "createPreferenceHierarchy: Country: " + index);
// Get the preference and list the value.
if ((index < 0) || (index >= summaryBandItems.length)) {
index = 0;
}
mBandPreference.setValueIndex(index);
root.addPreference(mBandPreference); mChannelSpacingPref = new ListPreference(this);
mChannelSpacingPref.setEntries(R.array.channel_spacing_entries);
mChannelSpacingPref.setEntryValues(R.array.channel_spacing_val);
mChannelSpacingPref.setDialogTitle(R.string.sel_chanl_spacing);
mChannelSpacingPref.setTitle(R.string.chanl_spacing);
mChannelSpacingPref.setKey(CHAN_SPACING_KEY); mUserBandMinPref = new EditTextPreference(this);
mUserBandMinPref.setKey(USER_DEFINED_BAND_MIN_KEY);
mUserBandMinPref.setTitle(R.string.usr_def_band_min);
mUserBandMinPref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL);
mUserBandMinPref.setDialogTitle(R.string.usr_def_band_min); mUserBandMaxPref = new EditTextPreference(this);
mUserBandMaxPref.setKey(USER_DEFINED_BAND_MAX_KEY);
mUserBandMaxPref.setTitle(R.string.usr_def_band_max);
mUserBandMaxPref.setDialogTitle(R.string.usr_def_band_max);
mUserBandMaxPref.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL); setBandSummary(index); if (mRxMode) {
// Audio Output (Stereo or Mono)
summaryAudioModeItems = getResources().getStringArray(
R.array.ster_mon_entries);
mAudioPreference = new ListPreference(this);
mAudioPreference.setEntries(R.array.ster_mon_entries);
mAudioPreference.setEntryValues(R.array.ster_mon_values);
mAudioPreference.setDialogTitle(R.string.sel_audio_output);
mAudioPreference.setKey(AUDIO_OUTPUT_KEY);
mAudioPreference.setTitle(R.string.aud_output_mode);
boolean audiomode = FmSharedPreferences.getAudioOutputMode();
if (audiomode) {
index = 0;
} else {
index = 1;
}
Log.d(LOGTAG, "createPreferenceHierarchy: audiomode: " + audiomode);
mAudioPreference.setSummary(summaryAudioModeItems[index]);
mAudioPreference.setValueIndex(index); // AF Auto Enable (Checkbox)
mAfPref = new CheckBoxPreference(this);
mAfPref.setKey(AUTO_AF);
mAfPref.setTitle(R.string.auto_select_af);
mAfPref.setSummaryOn(R.string.auto_select_af_enabled);
mAfPref.setSummaryOff(R.string.auto_select_af_disabled);
boolean bAFAutoSwitch = FmSharedPreferences.getAutoAFSwitch();
Log.d(LOGTAG, "createPreferenceHierarchy: bAFAutoSwitch: "
+ bAFAutoSwitch);
mAfPref.setChecked(bAFAutoSwitch);
root.addPreference(mAfPref); if(FMRadio.RECORDING_ENABLE) {
summaryRecordItems = getResources().getStringArray(
R.array.record_durations_entries);
int nRecordDuration = 0;
mRecordDurPreference = new ListPreference(this);
mRecordDurPreference.setEntries(R.array.record_durations_entries);
mRecordDurPreference.setEntryValues(R.array.record_duration_values);
mRecordDurPreference.setDialogTitle(R.string.sel_rec_dur);
mRecordDurPreference.setKey(RECORD_DURATION_KEY);
mRecordDurPreference.setTitle(R.string.record_dur);
nRecordDuration = FmSharedPreferences.getRecordDuration();
Log.d(LOGTAG, "createPreferenceHierarchy: recordDuration: "
+ nRecordDuration);
switch(nRecordDuration) {
case FmSharedPreferences.RECORD_DUR_INDEX_0_VAL:
index = 0;
break;
case FmSharedPreferences.RECORD_DUR_INDEX_1_VAL:
index = 1;
break;
case FmSharedPreferences.RECORD_DUR_INDEX_2_VAL:
index = 2;
break;
case FmSharedPreferences.RECORD_DUR_INDEX_3_VAL:
index = 3;
break;
}
// Get the preference and list the value.
if ((index < 0) || (index >= summaryRecordItems.length)) {
index = 0;
}
Log.d(LOGTAG, "createPreferenceHierarchy: recordDurationSummary: "
+ summaryRecordItems[index]);
mRecordDurPreference.setSummary(summaryRecordItems[index]);
mRecordDurPreference.setValueIndex(index);
root.addPreference(mRecordDurPreference);
}
}
mRestoreDefaultPreference = new Preference(this);
mRestoreDefaultPreference.setTitle(
R.string.settings_revert_defaults_title);
mRestoreDefaultPreference.setKey(RESTORE_FACTORY_DEFAULT);
mRestoreDefaultPreference
.setSummary(R.string.settings_revert_defaults_summary);
mRestoreDefaultPreference.setOnPreferenceClickListener(this);
root.addPreference(mRestoreDefaultPreference);
return root;
}
public void clearStationList()清除显示列表的电台。
public void clearStationList() {
SharedPreferences sp = getSharedPreferences(FMRadio.SCAN_STATION_PREFS_NAME, 0);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
public void onSharedPreferenceChanged(SharedPreferencessharedPreferences, String key) ListPerences的监听事件,电台信息改变
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
int mTunedFreq = 0;
boolean bStatus = false;
if (key.equals(REGIONAL_BAND_KEY)) {
int curListIndex = FmSharedPreferences.getCurrentListIndex();
PresetList curList = FmSharedPreferences.getStationList(curListIndex);
String valueStr = sharedPreferences.getString(key, "");
int index = 0;
if(valueStr != null) {
index = mBandPreference.findIndexOfValue(valueStr);
}
if((index < 0) || (index >= summaryBandItems.length)) {
index = 0;
mBandPreference.setValueIndex(0);
}else if((index + 1) == summaryBandItems.length) {
mChannelSpacingPref.setEnabled(true);
}else {
mChannelSpacingPref.setEnabled(false);
}
Log.d(LOGTAG, "onSharedPreferenceChanged: Country Change: "
+ index);
FmSharedPreferences.setCountry(index);
setBandSummary(index);
sendSettingsChangedIntent(FM_BAND_CHANGED);
if (curList != null) {
curList.clear();
}
clearStationList();
}else if(key.equals(CHAN_SPACING_KEY)) {
int curListIndex = FmSharedPreferences.getCurrentListIndex();
PresetList curList = FmSharedPreferences.getStationList(curListIndex);
String valStr = mChannelSpacingPref.getValue();
int index = 0;
if(valStr != null) {
index = mChannelSpacingPref.findIndexOfValue(valStr);
}
if ((index < 0) || (index >= chSpacingItems.length)) {
index = 0;
mChannelSpacingPref.setValueIndex(0);
}
mChannelSpacingPref.setSummary(chSpacingItems[index]);
FmSharedPreferences.setChSpacing(2 - index);
sendSettingsChangedIntent(FM_CHAN_SPACING_CHANGED);
if(curList != null) {
curList.clear();
}
clearStationList();}else if(key.equals(USER_DEFINED_BAND_MIN_KEY)) {
String valStr = mUserBandMinPref.getText();
double freq = 0;
try {
freq = Double.parseDouble(valStr) * 1000;
}catch(NumberFormatException e) {
e.printStackTrace();
return;
}
max_freq = FmSharedPreferences.getUpperLimit();
min_freq = FmSharedPreferences.getLowerLimit();
if((freq > 0) && (freq < max_freq) && (freq >= 76000)) {
FmSharedPreferences.setLowerLimit((int)freq);
sendSettingsChangedIntent(FM_BAND_CHANGED);
setBandSummary(summaryBandItems.length - 1);
clearStationList();
}else {
Toast.makeText(this, getString(R.string.user_defind_band_msg),
Toast.LENGTH_SHORT).show();
} }else if(key.equals(USER_DEFINED_BAND_MAX_KEY)) {
String valStr = mUserBandMaxPref.getText();
double freq = 0;
try {
freq = Double.parseDouble(valStr) * 1000;
}catch(NumberFormatException e) {
e.printStackTrace();
return;
}
min_freq = FmSharedPreferences.getLowerLimit();
max_freq = FmSharedPreferences.getUpperLimit();
if((freq > 0) && (freq > min_freq) && (freq <= 108000)) {
FmSharedPreferences.setUpperLimit((int)freq);
sendSettingsChangedIntent(FM_BAND_CHANGED);
setBandSummary(summaryBandItems.length - 1);
clearStationList();
}else {
Toast.makeText(this, getString(R.string.user_defind_band_msg),
Toast.LENGTH_SHORT).show();
}
}else {
if(mRxMode) { if (key.equals(AUTO_AF)) {
boolean bAFAutoSwitch = mAfPref.isChecked();
Log.d(LOGTAG, "onSharedPreferenceChanged: Auto AF Enable: "
+ bAFAutoSwitch);
FmSharedPreferences.setAutoAFSwitch(bAFAutoSwitch);
mPrefs.Save();
sendSettingsChangedIntent(FM_AF_OPTION_CHANGED);
}else if(key.equals(RECORD_DURATION_KEY)) {
if(FMRadio.RECORDING_ENABLE) {
String valueStr = mRecordDurPreference.getValue();
int index = 0;
if (valueStr != null) {
index = mRecordDurPreference.findIndexOfValue(valueStr);
}
if ((index < 0) || (index >= summaryRecordItems.length)) {
index = 0;
mRecordDurPreference.setValueIndex(index);
}
Log.d(LOGTAG, "onSharedPreferenceChanged: recorddur: "
+ summaryRecordItems[index]);
mRecordDurPreference.setSummary(summaryRecordItems[index]);
FmSharedPreferences.setRecordDuration(index);
}
} else if (key.equals(AUDIO_OUTPUT_KEY)) {String valueStr = mAudioPreference.getValue();
int index = 0;
if (valueStr != null) {
index = mAudioPreference.findIndexOfValue(valueStr);
}
if (index != 1) {
if (index != 0) {
index = 0;
/* It shud be 0(Stereo) or 1(Mono) */
mAudioPreference.setValueIndex(index);
}
}
Log.d(LOGTAG, "onSharedPreferenceChanged: audiomode: "
+ summaryAudioModeItems[index]);
mAudioPreference.setSummary(summaryAudioModeItems[index]);
if (index == 0) {
// Stereo
FmSharedPreferences.setAudioOutputMode(true);
}else {
// Mono
FmSharedPreferences.setAudioOutputMode(false);
}
mPrefs.Save();
sendSettingsChangedIntent(FM_AUDIO_MODE_CHANGED);
}
}
}
if (mPrefs != null) {
if(bStatus) {
mPrefs.Save();
}else {
mTunedFreq = FmSharedPreferences.getTunedFrequency();
if (mTunedFreq > FmSharedPreferences.getUpperLimit() ||
mTunedFreq < FmSharedPreferences.getLowerLimit()) {
FmSharedPreferences.setTunedFrequency(
FmSharedPreferences.getLowerLimit());
}
mPrefs.Save();
}
}
}
public boolean onPreferenceClick(Preferencepreference) 收音机恢复出厂设置监听事件,后fm播放状态显示对话框,调用restoreSettingsDefault()存入初始化数据。
public boolean onPreferenceClick(Preference preference) {
boolean handled = false;
if (preference == mRestoreDefaultPreference) {
showDialog(RESTORE_FACTORY_DEFAULT_INT);
}
return handled;
}
ListPreferences属性注册:
private void restoreSettingsDefault() {
if (mPrefs != null) {
if (Locale.getDefault().equals(Locale.CHINA)) {
mBandPreference
.setValueIndex(FmSharedPreferences.REGIONAL_BAND_CHINA);
}else{
mBandPreference
.setValueIndex(FmSharedPreferences.REGIONAL_BAND_INDONESIA);
}
if (mRxMode) { mAudioPreference.setValueIndex(0);
if (FMRadio.RECORDING_ENABLE) {
mRecordDurPreference.setValueIndex(0);
}
mAfPref.setChecked(true);
FmSharedPreferences.SetDefaults();
}else {
if (Locale.getDefault().equals(Locale.CHINA)) {
FmSharedPreferences
.setCountry(FmSharedPreferences.REGIONAL_BAND_CHINA);
}else{FmSharedPreferences
.setCountry(FmSharedPreferences.REGIONAL_BAND_INDONESIA);
}
}
mPrefs.Save();
}
}
onResume()方法
protected void onResume() {
super.onResume();
PreferenceScreen preferenceScreen = getPreferenceScreen();
SharedPreferences sharedPreferences = null;
if (preferenceScreen != null) {
sharedPreferences = preferenceScreen.getSharedPreferences();
}
if (sharedPreferences != null) {
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
}
}
haredPreferences.registerOnSharedPreferenceChangeListener(this);进行注册
protected void onPause()方法
protected void onPause() {
super.onPause();
PreferenceScreen preferenceScreen = getPreferenceScreen();
SharedPreferences sharedPreferences = null;
if (preferenceScreen != null) {
sharedPreferences = preferenceScreen.getSharedPreferences();
}
if (sharedPreferences != null) {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}
}
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);卸载注册
private void setBandSummary(int index) 设置ListPreference控件选择地区频率范围后现实在控件上
private void setBandSummary(int index) {
if((index + 1) == summaryBandItems.length) {
min_freq = FmSharedPreferences.getLowerLimit();
max_freq = FmSharedPreferences.getUpperLimit();
chan_spacing = FmSharedPreferences.getChSpacing();
if(chan_spacing < 0) {
chan_spacing = 0;
}
mBandPreference.setSummary(summaryBandItems[index] + "( "
+ (min_freq / 1000.0) +"Mhz To " + (max_freq / 1000.0) +
"Mhz)");
mChannelSpacingPref.setValueIndex(2 - chan_spacing);
mChannelSpacingPref.setSummary(chSpacingItems[2 - chan_spacing]);
mChannelSpacingPref.setEnabled(true);
mUserBandMinPref.setEnabled(true);mUserBandMaxPref.setEnabled(true);
mUserBandMinPref.setSummary((min_freq / 1000.0) + "Mhz");
mUserBandMaxPref.setSummary((max_freq / 1000.0) + "Mhz");
}else {
mBandPreference.setSummary(summaryBandItems[index]);
mChannelSpacingPref.setEnabled(false);
mUserBandMinPref.setEnabled(false);
mUserBandMaxPref.setEnabled(false);
}
}
private void displayToast(String msg)实现Toast提示
private void displayToast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
private void sendSettingsChangedIntent(intaction)
发送广播action活动状态,
在FMRadio.java的registerFMSettingListner()方法接受(每次走
FMRadio.java的activity都会接受下广播)。
private void sendSettingsChangedIntent(int action) {
Intent intent = new Intent(ACTION_FM_SETTING);
intent.putExtra("state", action);
Log.d(LOGTAG, "Sending FM SETTING Change intent for = " + action);
getApplicationContext().sendBroadcast(intent);
} 总结:比较简单,过一遍即可。
Android FM模块学习之四源码分析(3)的更多相关文章
- Android FM 模块学习之四 源码解析(1)
Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 前一章我们了解了FM手动调频,接下 ...
- Android FM模块学习之四源码解析(一)
转自:http://blog.csdn.net/tfslovexizi/article/details/41516149?utm_source=tuicool&utm_medium=refer ...
- Android FM模块学习之四源码学习(2)
前几章我们分析了FM模块的几个主要的类文件,今天要分析的是:FMTransceiver.java // 某些工程中名称为FMRadioService.java public class FmTra ...
- Android Small插件化框架源码分析
Android Small插件化框架源码分析 目录 概述 Small如何使用 插件加载流程 待改进的地方 一.概述 Small是一个写得非常简洁的插件化框架,工程源码位置:https://github ...
- socketserver模块使用与源码分析
socketserver模块使用与源码分析 前言 在前面的学习中我们其实已经可以通过socket模块来建立我们的服务端,并且还介绍了关于TCP协议的粘包问题.但是还有一个非常大的问题就是我们所编写的S ...
- DotNetty网络通信框架学习之源码分析
DotNetty网络通信框架学习之源码分析 有关DotNetty框架,网上的详细资料不是很多,有不多的几个博友做了简单的介绍,也没有做深入的探究,我也根据源码中提供的demo做一下记录,方便后期查阅. ...
- Android图片处理神器BitmapFun源码分析
作为一名Android开发人员,相信大家对图片OOM的问题已经耳熟能详了,关于图片缓存和解决OOM的开源项目也是相当的多,被大家熟知的就是Universal_image_loader和Volley了, ...
- Android View事件分发-从源码分析
View事件分发-从源码分析 学习自 <Android开发艺术探索> https://blog.csdn.net/qian520ao/article/details/78555397?lo ...
- [置顶]
【Android实战】----从Retrofit源码分析到Java网络编程以及HTTP权威指南想到的
一.简介 接上一篇[Android实战]----基于Retrofit实现多图片/文件.图文上传中曾说非常想搞明白为什么Retrofit那么屌.最近也看了一些其源码分析的文章以及亲自查看了源码,发现其对 ...
随机推荐
- mysql5.6中 order by 多个字段排序问题
今天用order by排序 后面跟了多个字段,如sql语句: SELECT a.id,a.loginname,a.address,u.id,u.`name`,u.address FROM admin_ ...
- SQL GROUP BY 后排序
由于GROUP BY 使用Sum函数后 ID等唯一值就无法查询出来了,所以想按照ID排序也就不可以了. 这时可以使用一个MIN 或者MAX函数来取得一个最小或者最大的ID 这样就可以实现以其中一条ID ...
- 阻抗计算公式、polar si9000(教程)
给初学者的一直有很多人问我阻抗怎么计算的. 人家问多了,我想给大家整理个材料,于己于人都是个方便.如果大家还有什么问题或者文档有什么错误,欢迎讨论与指教!在计算阻抗之前,我想很有必要理解这儿阻抗的意义 ...
- JQ写下拉列表项目移动(内附效果图和源代码)
效果图如下: 实现功能:点击第一个按钮,让选中的对象从左边移动到右边: 点击第二个按钮,让左边的所有对象移动到右边: 点击第三个按钮,让选中的对象从右边边移动到左边: 点击第四个按钮,让右边的所有 ...
- 编译安装php的配置参数详细解析
./configure --prefix=/usr/local/php --enable-opcache --enable-fpm --enable-sockets --enable-mysqlnd ...
- Nginx 简介
一.介绍 Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器. Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD ...
- jquery总结01-基本概念和选择器
dom元素和jquery元素的区别 dom元素支持dom元素自带的属性和方法,jquery元素支持jquery元素自带的属性和方法 dom var div = doc ...
- 【HDU5955】Guessing the Dice Roll/马尔科夫
先从阿里机器学习算法岗网络笔试题说起:甲乙两人进行一个猜硬币的游戏.每个人有一个目标序列,由裁判来抛硬币.谁先得到裁判抛出的一串连续结果,谁赢. 甲的目标序列是正正正,乙的目标序列是反正正.那么如果裁 ...
- i.BIO方式的SSL通道流程
前面已经讲解了BIO通道的整体流程,对于SSL的流程是插在通道中的,在BIO通道的初始化的时候,根据Connector配置的SSLEnabled属性进行SSL的逻辑. 主要集中的位置在JIOEndpo ...
- MultiDex到底有多坑
google为什么要引入MultiDex? dex指令是用16位寄存器来保存dex中的方法数,所以限制了在apk 中最大的方法数为65535,当超过这个最大值在编译的时候会报 方法数超标的错误. 如何 ...