Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android FM模塊學習之源碼分析(六)

Android FM模塊學習之源碼分析(六)

編輯:關於Android編程

現在是2015年1月啦,得改口說去年了,去年抽時間整理了一些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);
        }


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved