mt6735 FM菜单选项中添加手动设置频率
时间:10-02
整理:3721RD
点击:
[DESCRIPTION]
FM菜单中不可以手工输入电台名称,需要增加该菜单以实现:手动输入频率并跳转到该频率
[SOLUTION]
一、在mediatek\packages\apps\FMRadio\res\Layout中新增fm_manualinput_layout.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fm_manualinput_layout"
androidrientation="vertical"
android:layout_width="286dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/fm_manualinput_title_bar"
style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="53dp"
android:padding="10dp"
android:singleLine="true"
android:gravity="center|left"
android:text="@string/dlg_manual_input_title"/>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#31b6e7" />
<LinearLayout
android:id="@+id/contentEdit"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="68dp"
android:gravity="center">
<EditText
android:id="@+id/fm_edit_text"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:numeric="integer"
android:maxLength="80"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#424542" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/fm_manualinput_btn_discard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_discard_manualinput"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#424542" />
<Button
android:id="@+id/fm_manualinput_btn_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_save_manualinput"/>
</LinearLayout>
</LinearLayout>
二、在mediatek\packages\apps\FMRadio\res\values的strings.xml文件中增加如下:
<string name="dlg_manual_input_title">Manual input</string>
<string name="btn_discard_manualinput">Discard</string>
<string name="btn_save_manualinput">Sure</string>
<string name="edit_manualinput_name_hint">Type channel here</string>
三、修改mediatek\packages\apps\FMRadio\res\menu\fm_menu.xml文件,
添加menu:
<item android:id="@+id/fm_manual_input"
android:title="fm_manual_input"
android:enabled="false"/>
四、修改FMRadioActivity.java
(1)在开头处:public class FMRadioActivity extends Activity implements…FMManualInputDialogFragment.OnManualInputDialogCLIckListener//增加该行
(2) 在开头全局变量定义处增加:
private static final String SEARCH = "Search";
private static final String MANUAL_INPUT = "ManualInput";
(3) public boolean onOptionsItEMSelected(MenuItem item)
case R.id.fm_menu:
……
if (isPlaying) {
menu.findItem(R.id.fm_search).setEnabled(true);
//添加以下
menu.findItem(R.id.fm_manual_input).setEnabled(true);
//添加以上
}
(4) private void refreshPopupMenuItem(boolean enabled) {
……
}
if (null != mPopupMenu) {
//添加以下
menu.findItem(R.id.fm_manual_input).setEnabled(enabled);
//添加以上
}
}
(5) public boolean onMenuItemClick(MenuItem item) {
LogUtils.d(TAG, "onMenuItemClick:" + item.getItemId());
switch (item.getItemId()) {
……
//添加以下
case R.id.fm_manual_input:
mMenuItempower.setEnabled(false);
mMenuItemChannelList.setEnabled(false);
mMenuItemOverflow.setEnabled(false);
showManualInputDialog();
//添加以上
……
}
(6) 增加该段代码:
public void showManualInputDialog() {
int freq = mService.getFrequency();
if (FMRadioUtils.isValidStation(freq)) {
LogUtils.d(TAG, "frequency frequency isValid Station");}
else
freq=FMRadioUtils.LOWEST_STATION;
FMManualInputDialogFragment newFragment = new FMManualInputDialogFragment(freq);
newFragment.show(mFragmentManager,MANUAL_INPUT);
mFragmentManager.executePendingTransactions();
}
(7) 增加该段代码:
private void dismissManualInputDialog() {
FragmentTransaction ft = mFragmentManager.beginTransaction();
Fragment fragment = mFragmentManager.findFragmentByTag(MANUAL_INPUT);
if (null != fragment) {
ft.remove(fragment);
ft.coMMItAllowingStateLoss();
}
}
(8) 增加该段代码:
public void onManualInputClick(int editName) {
tuneToStation(editName);
}
(9) public void onPause() {
if (null != df && df.getShowsDialog()) {
……
//添加以下
dismissManualInputDialog();
//添加以上
}
……
}
五、在mediatek\packages\apps\FMRadio\src目录下增加FMManualInputDialogFragment.java文件
,代码如下:
package com.mediatek.FMRadio; import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.mediatek.FMRadio.dialogs.SearchChannelsDialog;
import com.mediatek.common.featureoption.FeatureOption;
import java.io.File;
public class FMManualInputDialogFragment extends DialogFragment {
private static final String TAG = "FmRx/ManualDlg"; // log tag
private Button mButtonSure = null; // save recording file button
private Button mButtonDiscard = null; // discard recording file button
private EditText mEditText = null; // rename recording file edit text
private String mEditNameToSure = null; //recording file to save name
private int miEditNameToSure = 0;
private TextView mTitleTextView = null; // text view which to set title
private TextView mStorageWarningTextView = null; //text view which show storage warning
private OnManualInputDialogClickListener mListener = null;
int mOldChannel=0;
public FMManualInputDialogFragment(int freq) {
mOldChannel=freq;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
LogUtils.d(TAG,"onAttach");
try {
mListener = (OnManualInputDialogClickListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
/**
* set dialog style
* @param savedInstanceState save instance state
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG,">>onCreate() savedInstanceState:" + savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
LogUtils.d(TAG,"<<onCreate()");
}
/**
* inflate view and get the operation button
* @param inflater layout inflater
* @param container view group container
* @param savedInstanceState save instance state
* @return inflater view
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtils.d(TAG,"onCreateView");
View view = inflater.inflate(R.layout.manual_input_dialog, container, false);
mButtonSure = (Button) view.findViewById(R.id.fm_manualinput_btn_sure);
mButtonSure.setOnClickListener(mButtonOnClickListener);
mButtonSure.setEnabled(fasle);
mButtonDiscard = (Button) view.findViewById(R.id.fm_manualinput_btn_discard);
mButtonDiscard.setOnClickListener(mButtonOnClickListener);
// Set the recording edit text
mEditText = (EditText) view.findViewById(R.id.fm_edit_text);
mTitleTextView = (TextView)view.findViewById(R.id.fm_manualinput_title_bar);
return view;
}
/**
* set the dialog edit text and other attribute
*/
@Override
public void onResume() {
super.onResume();
LogUtils.d(TAG,">>onResume()");
// have define in fm_recorder_dialog.xml length at most 250(maxFileLength -
suffixLength)
mEditText.setSingleLine(true);
mEditText.setHint(getActivity().getResources().getString(
R.string.edit_manualinput_name_hint));
mEditText.requestFocus();
setTextChangedCallback();
getDialog().setCanceledOntouchOutside(false);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
LogUtils.d(TAG,"<<onResume()");
}
@Override
public void onSaveInstanceState(Bundle outState) {
LogUtils.d(TAG,">>onSaveInstanceState()");
Dialog dialog = getDialog();
LogUtils.d(TAG,"<<onSaveInstanceState()");
super.onSaveInstanceState(outState);
}
@Override
public void onDestroy() {
LogUtils.d(TAG,">>onDestroy()");
mEditNameToSure = null;
mListener = null;
LogUtils.d(TAG,"<<onDestroy()");
super.onDestroy();
}
/**
* This method register callback and set fiLTEr to Edit, in order to make sure that user
input
* is legal. The input can't be illegal filename and can't be too long.
*/
private void setTextChangedCallback() {
mEditText.addTextChangedListener(new TextWatcher() {
// not use, so don't need to implement it
public void afterTextChanged(Editable arg0) {
}
// not use, so don't need to implement it
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/**
* check user input whether include invalid character
*/
public void onTextChanged(CharSequence s, int start, int before, int count) {
LogUtils.d(TAG, "onTextChanged() s:" + s + ", start:" + start + ", before:" + before +
", count:" + count);
int iChannelName=0;
String channelName = s.toString().trim();
LogUtils.d(TAG, "onTextChanged channelName:" + channelName);
if(channelName.length()>0)
iChannelName=Integer.parseInt(channelName);
// Characters not allowed by file system
if (FMRadioUtils.isValidStation(iChannelName)) {
mButtonSure.setEnabled(true);
} else {
mButtonSure.setEnabled(false);
}
mEditNameToSure = mEditText.getText().toString().trim();
LogUtils.d(TAG, "onTextChanged mEditNameToSure:" + mEditNameToSure);
}
});
}
private OnClickListener mButtonOnClickListener = new OnClickListener() {
/**
* define the button operation
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.fm_manualinput_btn_sure:
String msg = null;
// Check the recording name whether exist
mEditNameToSure = mEditText.getText().toString().trim();
miEditNameToSure=Integer.parseInt(mEditNameToSure);
LogUtils.d(TAG, "save:" + miEditNameToSure);
if (null != mListener) {
mListener.onManualInputClick(miEditNameToSure);
}
dismissAllowingStateLoss();
break;
case R.id.fm_manualinput_btn_discard:
dismissAllowingStateLoss();
LogUtils.d(TAG,"Discard FM recording file. ");
break;
default:
break;
}
}
};
public interface OnManualInputDialogClickListener {
/**
* record dialog click callback
* @param recordingName user input recording name
*/
void onManualInputClick(int editName);
}
}
FM菜单中不可以手工输入电台名称,需要增加该菜单以实现:手动输入频率并跳转到该频率
[SOLUTION]
一、在mediatek\packages\apps\FMRadio\res\Layout中新增fm_manualinput_layout.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fm_manualinput_layout"
androidrientation="vertical"
android:layout_width="286dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/fm_manualinput_title_bar"
style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="53dp"
android:padding="10dp"
android:singleLine="true"
android:gravity="center|left"
android:text="@string/dlg_manual_input_title"/>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#31b6e7" />
<LinearLayout
android:id="@+id/contentEdit"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="68dp"
android:gravity="center">
<EditText
android:id="@+id/fm_edit_text"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:numeric="integer"
android:maxLength="80"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#424542" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/fm_manualinput_btn_discard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_discard_manualinput"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#424542" />
<Button
android:id="@+id/fm_manualinput_btn_sure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/btn_save_manualinput"/>
</LinearLayout>
</LinearLayout>
二、在mediatek\packages\apps\FMRadio\res\values的strings.xml文件中增加如下:
<string name="dlg_manual_input_title">Manual input</string>
<string name="btn_discard_manualinput">Discard</string>
<string name="btn_save_manualinput">Sure</string>
<string name="edit_manualinput_name_hint">Type channel here</string>
三、修改mediatek\packages\apps\FMRadio\res\menu\fm_menu.xml文件,
添加menu:
<item android:id="@+id/fm_manual_input"
android:title="fm_manual_input"
android:enabled="false"/>
四、修改FMRadioActivity.java
(1)在开头处:public class FMRadioActivity extends Activity implements…FMManualInputDialogFragment.OnManualInputDialogCLIckListener//增加该行
(2) 在开头全局变量定义处增加:
private static final String SEARCH = "Search";
private static final String MANUAL_INPUT = "ManualInput";
(3) public boolean onOptionsItEMSelected(MenuItem item)
case R.id.fm_menu:
……
if (isPlaying) {
menu.findItem(R.id.fm_search).setEnabled(true);
//添加以下
menu.findItem(R.id.fm_manual_input).setEnabled(true);
//添加以上
}
(4) private void refreshPopupMenuItem(boolean enabled) {
……
}
if (null != mPopupMenu) {
//添加以下
menu.findItem(R.id.fm_manual_input).setEnabled(enabled);
//添加以上
}
}
(5) public boolean onMenuItemClick(MenuItem item) {
LogUtils.d(TAG, "onMenuItemClick:" + item.getItemId());
switch (item.getItemId()) {
……
//添加以下
case R.id.fm_manual_input:
mMenuItempower.setEnabled(false);
mMenuItemChannelList.setEnabled(false);
mMenuItemOverflow.setEnabled(false);
showManualInputDialog();
//添加以上
……
}
(6) 增加该段代码:
public void showManualInputDialog() {
int freq = mService.getFrequency();
if (FMRadioUtils.isValidStation(freq)) {
LogUtils.d(TAG, "frequency frequency isValid Station");}
else
freq=FMRadioUtils.LOWEST_STATION;
FMManualInputDialogFragment newFragment = new FMManualInputDialogFragment(freq);
newFragment.show(mFragmentManager,MANUAL_INPUT);
mFragmentManager.executePendingTransactions();
}
(7) 增加该段代码:
private void dismissManualInputDialog() {
FragmentTransaction ft = mFragmentManager.beginTransaction();
Fragment fragment = mFragmentManager.findFragmentByTag(MANUAL_INPUT);
if (null != fragment) {
ft.remove(fragment);
ft.coMMItAllowingStateLoss();
}
}
(8) 增加该段代码:
public void onManualInputClick(int editName) {
tuneToStation(editName);
}
(9) public void onPause() {
if (null != df && df.getShowsDialog()) {
……
//添加以下
dismissManualInputDialog();
//添加以上
}
……
}
五、在mediatek\packages\apps\FMRadio\src目录下增加FMManualInputDialogFragment.java文件
,代码如下:
package com.mediatek.FMRadio; import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.mediatek.FMRadio.dialogs.SearchChannelsDialog;
import com.mediatek.common.featureoption.FeatureOption;
import java.io.File;
public class FMManualInputDialogFragment extends DialogFragment {
private static final String TAG = "FmRx/ManualDlg"; // log tag
private Button mButtonSure = null; // save recording file button
private Button mButtonDiscard = null; // discard recording file button
private EditText mEditText = null; // rename recording file edit text
private String mEditNameToSure = null; //recording file to save name
private int miEditNameToSure = 0;
private TextView mTitleTextView = null; // text view which to set title
private TextView mStorageWarningTextView = null; //text view which show storage warning
private OnManualInputDialogClickListener mListener = null;
int mOldChannel=0;
public FMManualInputDialogFragment(int freq) {
mOldChannel=freq;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
LogUtils.d(TAG,"onAttach");
try {
mListener = (OnManualInputDialogClickListener) activity;
} catch (ClassCastException e) {
e.printStackTrace();
}
}
/**
* set dialog style
* @param savedInstanceState save instance state
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LogUtils.d(TAG,">>onCreate() savedInstanceState:" + savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, 0);
LogUtils.d(TAG,"<<onCreate()");
}
/**
* inflate view and get the operation button
* @param inflater layout inflater
* @param container view group container
* @param savedInstanceState save instance state
* @return inflater view
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LogUtils.d(TAG,"onCreateView");
View view = inflater.inflate(R.layout.manual_input_dialog, container, false);
mButtonSure = (Button) view.findViewById(R.id.fm_manualinput_btn_sure);
mButtonSure.setOnClickListener(mButtonOnClickListener);
mButtonSure.setEnabled(fasle);
mButtonDiscard = (Button) view.findViewById(R.id.fm_manualinput_btn_discard);
mButtonDiscard.setOnClickListener(mButtonOnClickListener);
// Set the recording edit text
mEditText = (EditText) view.findViewById(R.id.fm_edit_text);
mTitleTextView = (TextView)view.findViewById(R.id.fm_manualinput_title_bar);
return view;
}
/**
* set the dialog edit text and other attribute
*/
@Override
public void onResume() {
super.onResume();
LogUtils.d(TAG,">>onResume()");
// have define in fm_recorder_dialog.xml length at most 250(maxFileLength -
suffixLength)
mEditText.setSingleLine(true);
mEditText.setHint(getActivity().getResources().getString(
R.string.edit_manualinput_name_hint));
mEditText.requestFocus();
setTextChangedCallback();
getDialog().setCanceledOntouchOutside(false);
getDialog().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
LogUtils.d(TAG,"<<onResume()");
}
@Override
public void onSaveInstanceState(Bundle outState) {
LogUtils.d(TAG,">>onSaveInstanceState()");
Dialog dialog = getDialog();
LogUtils.d(TAG,"<<onSaveInstanceState()");
super.onSaveInstanceState(outState);
}
@Override
public void onDestroy() {
LogUtils.d(TAG,">>onDestroy()");
mEditNameToSure = null;
mListener = null;
LogUtils.d(TAG,"<<onDestroy()");
super.onDestroy();
}
/**
* This method register callback and set fiLTEr to Edit, in order to make sure that user
input
* is legal. The input can't be illegal filename and can't be too long.
*/
private void setTextChangedCallback() {
mEditText.addTextChangedListener(new TextWatcher() {
// not use, so don't need to implement it
public void afterTextChanged(Editable arg0) {
}
// not use, so don't need to implement it
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
/**
* check user input whether include invalid character
*/
public void onTextChanged(CharSequence s, int start, int before, int count) {
LogUtils.d(TAG, "onTextChanged() s:" + s + ", start:" + start + ", before:" + before +
", count:" + count);
int iChannelName=0;
String channelName = s.toString().trim();
LogUtils.d(TAG, "onTextChanged channelName:" + channelName);
if(channelName.length()>0)
iChannelName=Integer.parseInt(channelName);
// Characters not allowed by file system
if (FMRadioUtils.isValidStation(iChannelName)) {
mButtonSure.setEnabled(true);
} else {
mButtonSure.setEnabled(false);
}
mEditNameToSure = mEditText.getText().toString().trim();
LogUtils.d(TAG, "onTextChanged mEditNameToSure:" + mEditNameToSure);
}
});
}
private OnClickListener mButtonOnClickListener = new OnClickListener() {
/**
* define the button operation
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.fm_manualinput_btn_sure:
String msg = null;
// Check the recording name whether exist
mEditNameToSure = mEditText.getText().toString().trim();
miEditNameToSure=Integer.parseInt(mEditNameToSure);
LogUtils.d(TAG, "save:" + miEditNameToSure);
if (null != mListener) {
mListener.onManualInputClick(miEditNameToSure);
}
dismissAllowingStateLoss();
break;
case R.id.fm_manualinput_btn_discard:
dismissAllowingStateLoss();
LogUtils.d(TAG,"Discard FM recording file. ");
break;
default:
break;
}
}
};
public interface OnManualInputDialogClickListener {
/**
* record dialog click callback
* @param recordingName user input recording name
*/
void onManualInputClick(int editName);
}
}