现在是2015年1月啦,得改口说去年了,去年抽时间整理了一些FM模块的主要源码类的东西,今年再整理一下几个次要的类的源码。这样来看FM上层的东西不是太多。
请看android\vendor\qcom\opensource\fm\fmapp2\src\com\caf\fmradio\Settings.java
protected void onCreate(BundlesavedInstanceState) 从FMRadio.java用使用Intent跳转携带参数过来,在onCreate获取携带数据。
<span style="font-family:KaiTi_GB2312;font-size:18px;">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()); } }</span>
private PreferenceScreen createPreferenceHierarchy() 初始化创建收音机设置里显示控件
<span style="font-family:KaiTi_GB2312;font-size:18px;">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; } </span><span style="font-family:KaiTi_GB2312;font-size:18px;"> </span>
public void clearStationList()清除显示列表的电台。
<span style="font-family:KaiTi_GB2312;font-size:18px;">public void clearStationList() { SharedPreferences sp = getSharedPreferences(FMRadio.SCAN_STATION_PREFS_NAME, 0); SharedPreferences.Editor editor = sp.edit(); editor.clear(); editor.commit(); }</span>
public void onSharedPreferenceChanged(SharedPreferencessharedPreferences, String key) ListPerences的监听事件,电台信息改变
<span style="font-family:KaiTi_GB2312;font-size:18px;">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(); } } }</span>
public boolean onPreferenceClick(Preferencepreference) 收音机恢复出厂设置监听事件
,后fm播放状态显示对话框,调用restoreSettingsDefault()存入初始化数据。
<span style="font-family:KaiTi_GB2312;font-size:18px;">public boolean onPreferenceClick(Preference preference) { boolean handled = false; if (preference == mRestoreDefaultPreference) { showDialog(RESTORE_FACTORY_DEFAULT_INT); } return handled; }</span>
ListPreferences属性注册:
<span style="font-family:KaiTi_GB2312;font-size:18px;"> 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(); } }</span>
onResume()方法
<span style="font-family:KaiTi_GB2312;font-size:18px;"> protected void onResume() { super.onResume(); PreferenceScreen preferenceScreen = getPreferenceScreen(); SharedPreferences sharedPreferences = null; if (preferenceScreen != null) { sharedPreferences = preferenceScreen.getSharedPreferences(); } if (sharedPreferences != null) { sharedPreferences.registerOnSharedPreferenceChangeListener(this); } }</span>
haredPreferences.registerOnSharedPreferenceChangeListener(this);进行注册
protected void onPause()方法
<span style="font-family:KaiTi_GB2312;font-size:18px;">protected void onPause() { super.onPause(); PreferenceScreen preferenceScreen = getPreferenceScreen(); SharedPreferences sharedPreferences = null; if (preferenceScreen != null) { sharedPreferences = preferenceScreen.getSharedPreferences(); } if (sharedPreferences != null) { sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); } }</span>
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);卸载注册
private void setBandSummary(int index) 设置ListPreference控件选择地区频率范围后现实在控件上
<span style="font-family:KaiTi_GB2312;font-size:18px;">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); } }</span>
private void displayToast(String msg)实现Toast提示
<span style="font-family:KaiTi_GB2312;font-size:18px;">private void displayToast(String msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); }</span>
private void sendSettingsChangedIntent(intaction) 发送广播action活动状态,在FMRadio.java的registerFMSettingListner()方法接受(每次走FMRadio.java的activity都会接受下广播)。
<span style="font-family:KaiTi_GB2312;font-size:18px;">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); }</span>
原文地址:http://blog.csdn.net/tfslovexizi/article/details/42393371