import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import com.android.settings.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
/*
* acivity for setting channel info
*/
public class CmChannelSetting extends Activity {
private Intent mAddChannelIntent;
public HashMap<String, Object> map;
public List<HashMap<String, Object>> listitem;
public ListView listview;
public CmChannelSetting CmAddChannel;
private CmChannelSettingAdapter listAdapter;
private Intent mDeleintent;
private MessageReceiver receiver;
private final String ACTION_RECV_MSG = "RECEIVE_MESSAGE";
public static List<String> idArray = new ArrayList<String>();
private Handler mHandler = null;
public static final int REFRESH = 0x000001;
public static final String FILE_PATH = "/data/misc/wimax/sequansd.xml";
public static ArrayList<String> idDelete = new ArrayList<String>();
private List<Channel> mChannels;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.channelsetting);
mAddChannelIntent = new Intent(this, CmAddChannel.class);
listitem = new ArrayList<HashMap<String, Object>>();
listview = (ListView) findViewById(R.id.lv_channelinfo);
CmAddChannel = new CmChannelSetting();
mDeleintent = new Intent(CmChannelSetting.this,
ReadWriteXmlintentservice.class);
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.d("jcmtest", "handleMessage>>>handleMessage");
if (msg.what == REFRESH) {
removeMessages(REFRESH);
Log.d("jcmtest", "handleMessage");
idArray.clear();
listitem.clear();
for (Channel person : mChannels) {
map = new HashMap<String, Object>();
map.put("frequency", person.getFrequency());
map.put("bandwidth", person.getBandwidth());
map.put("index", person.getId());
listitem.add(map);
idArray.add(person.getId());
Log.d("jcmtest",
"person.getFrequency()=="
+ person.getFrequency()
+ "person.getBandwidth()="
+ person.getBandwidth());
}
listAdapter = new CmChannelSettingAdapter(
CmChannelSetting.this, listitem,
R.layout.channellistitem,
new String[] { "frequency", "bandwidth", "index" },
new int[] { R.id.frequency, R.id.bandwidth,
R.id.index });
listview.setAdapter(listAdapter);
idDelete.clear();
listview.setOnItemClickListener(getMenu);
listAdapter.notifyDataSetChanged();
}
super.handleMessage(msg);
}
};
new ReadThread().start();
idArray.clear();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter filter = new IntentFilter(ACTION_RECV_MSG);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new MessageReceiver();
registerReceiver(receiver, filter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(receiver);
}
OnItemClickListener getMenu = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
ViewHolder holder = (ViewHolder) view.getTag();
holder.rbt1.toggle();
CmChannelSettingAdapter.getIsSelected().put(position,
holder.rbt1.isChecked());
if (holder.rbt1.isChecked() == true) {
idDelete.add(holder.index.getText().toString());
Log.d("jcmtest", "add==========="
+ holder.index.getText().toString());
} else {
Log.d("jcmtest", "DELETE==========="
+ holder.index.getText().toString());
idDelete.remove(holder.index.getText().toString());
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, getString(R.string.menu_option_add));
menu.add(0, 2, 2, getString(R.string.menu_option_delete));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if (item.getItemId() == 1) {
startActivityForResult(mAddChannelIntent, 9);
} else if (item.getItemId() == 2) {
if (idDelete.isEmpty() && idDelete.size() == 0) {
} else {
mDeleintent.setAction("deleteChannel");
mDeleintent.putExtra(
"com.neolink.action.ReadWriteXmlintentservice",
"deleteChannel");
mDeleintent.putStringArrayListExtra("position", idDelete);
startService(mDeleintent);
idDelete.clear();
}
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 9 && data != null) {
map = new HashMap<String, Object>();
map.put("frequency", data.getExtra("frequency", "0"));
map.put("bandwidth", data.getExtra("bandwidth", "0"));
map.put("index", data.getExtra("index", "0"));
idArray.add(data.getExtra("index", "0").toString());
listitem.add(map);
}
idDelete.clear();
listAdapter.notifyDataSetChanged();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String message = intent.getStringExtra("finishedDelete");
if (message.equals("sucess")) {
new ReadThread().start();
Toast.makeText(context, "message delete finished",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "message delete failed",
Toast.LENGTH_SHORT).show();
}
}
}
}
public class ReadThread extends Thread {
public void run() {
mChannels = new ArrayList<Channel>();
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
Document document = null;
factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
document = builder.parse(new File(FILE_PATH));
Element root = document.getDocumentElement();
NodeList nodes = root.getElementsByTagName("ndss");
for (int i = 0; i < nodes.getLength(); i++) {
Element channelElement = (Element) (nodes.item(i));
int lenth = channelElement.getElementsByTagName("channel")
.getLength();
for (int i1 = 0; i1 < lenth; i1++) {
Channel channel = new Channel();
Element channelData = (Element) channelElement
.getElementsByTagName("channel").item(i1);
channel.setId(channelData.getAttribute("id"));
channel.setBandwidth(channelData
.getAttribute("bandwidth"));
channel.setFft(channelData.getAttribute("fft"));
channel.setFrequency(channelData
.getAttribute("frequency"));
mChannels.add(channel);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} finally {
}
Message msg = new Message();
msg.what = REFRESH;
mHandler.sendMessage(msg);
Log.d("jcmtest", "ReadThread");
}
}
}
package com.android.settings.wimax;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.http.util.EncodingUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.TypeInfo;
import org.xml.sax.SAXException;
import android.app.IntentService;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class ReadWriteXmlintentservice extends IntentService {
public static final String ACTION_NEW_CHANNEL = "addChannel";
public static final String ACTION_DELE_CHANNEL = "deleteChannel";
public static final String ACTION_READ_XML = "readxml";
public static final String ACTION_AUTHENTICATION_XML = "Authentication";
public static final String ACTION_NEW_ACTION = "com.neolink.action.ReadWriteXmlintentservice";
public static final String NDSSTAG = "ndss";
private static final String ACTION_RECV_MSG = "RECEIVE_MESSAGE";
public static final String ENCODING = "UTF-8";
public static final String FILE_PATH = "/data/misc/wimax/sequansd.xml";
public Intent ReceIntent = null;
public ReadWriteXmlintentservice() {
super("ReadWriteXmlintentservice");
}
@Override
public IBinder onBind(Intent intent) {
System.out.println("onBind");
return super.onBind(intent);
}
@Override
public void onCreate() {
System.out.println("onCreate");
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
System.out.println("onStart");
super.onStart(intent, startId);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void setIntentRedelivery(boolean enabled) {
super.setIntentRedelivery(enabled);
System.out.println("setIntentRedelivery");
}
@Override
protected void onHandleIntent(Intent intent) {
String action = intent.getStringExtra(ACTION_NEW_ACTION);
if (action != null) {
ReceIntent = intent;
Log.e("jcmtest", "person.action==" + action);
if (action.equals(ACTION_DELE_CHANNEL)) {
new Thread(new Runnable() {
public void run() {
onDeleChannel(ReceIntent,
ReceIntent.getIntExtra("deleteItem", 0));
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(ACTION_RECV_MSG);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra("finishedDelete", "sucess");
sendBroadcast(broadcastIntent);
}
}).start();
} else if (action.equals(ACTION_NEW_CHANNEL)) {
new Thread(new Runnable() {
public void run() {
onaddChannel(ReceIntent);
}
}).start();
} else if (action.equals(ACTION_AUTHENTICATION_XML)) {
new Thread(new Runnable() {
public void run() {
onChangeAuthentication(ReceIntent);
}
}).start();
onChangeAuthentication(intent);
}
}
}
private List<Channel> onChangeAuthentication(Intent intent) {
// TODO Auto-generated method stub
Log.d("jcmtest", "onChangeAuthentication");
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
int eapLenth = document.getElementsByTagName("eap").getLength();
if (eapLenth == 0 || eapLenth < 0) {
if (onCreateEap() < 0) {
Log.d("jcmtest", "===create node error!!!!!!!!!!!!");
}
}
Element prefixelement = (Element) document.getElementsByTagName("prefix").item(0);
prefixelement.setTextContent(intent.getStringExtra("prefix"));
Element realmelement = (Element) document.getElementsByTagName("realm").item(0);
realmelement.setTextContent(intent.getStringExtra("realm"));
Element usernameelement = (Element) document.getElementsByTagName("username").item(0);
usernameelement.setTextContent(intent.getStringExtra("username"));
Element outerNaiTypelement = (Element) document .getElementsByTagName("outerNaiType").item(0);
outerNaiTypelement.setTextContent(intent.getStringExtra("outerNaiType"));
Element passwordelement = (Element) document.getElementsByTagName("password").item(0);
passwordelement.setTextContent(intent.getStringExtra("passwordelement"));
Element eapelement = (Element) (Element) document
.getElementsByTagName("eap").item(0);
Element ttlselement = (Element) (Element) document
.getElementsByTagName("ttls").item(0);
if ("Disable".equals(intent.getStringExtra("authentype"))){
eapelement.setAttribute("active", "0");
}else if ("TLS".equals(intent.getStringExtra("authentype"))) {
eapelement.setAttribute("active", "1");
ttlselement.setAttribute("active", "0");
} if ("TTLS".equals(intent.getStringExtra("authentype"))) {
eapelement.setAttribute("active", "1");
ttlselement.setAttribute("active", "1");
}
if (intent.getStringExtra("certificationpath") != null) {
if ("root".equals(intent.getStringExtra("certificate"))) {
Element certificateElement = (Element) document
.getElementsByTagName("root-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("root-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}else if ("user".equals(intent.getStringExtra("certificate"))){
Element certificateElement = (Element) document
.getElementsByTagName("user-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("user-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}else if ("userkey".equals(intent.getStringExtra("certificate"))){
Element certificateElement = (Element) document
.getElementsByTagName("userkey-certificate").item(0);
if (certificateElement==null){
certificateElement = document
.createElement("userkey-certificate");
certificateElement.setAttribute("encoding", "pem");
eapelement.insertBefore(certificateElement, eapelement.getElementsByTagName("nai").item(0));
}
certificateElement.setTextContent(readFileData(intent
.getStringExtra("certificationpath")));
}
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
Log.d("jcmtest", "the node is exist ?===*************4****");
out.close();
} catch (Exception ex) {
// System.out.println(ex.getMessage());
ex.printStackTrace();
Log.d("jcmtest", "ex.getMessage() = "+ex.getMessage());
} finally {
Log.d("jcmtest", "finally");
this.stopSelf();
}
return null;
}
@Override
public void onDestroy() {
System.out.println("onDestroy");
super.onDestroy();
}
public int onaddChannel(Intent intent) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element channelElement = document.createElement("channel");
channelElement.setAttribute("id", intent.getExtra("index", 00)
.toString());
channelElement.setAttribute("bandwidth",
intent.getExtra("bandwidth", "00").toString());
channelElement.setAttribute("fft", "1024");
channelElement.setAttribute("frequency",
intent.getExtra("frequency", "00").toString());
Element ndssElement = (Element) document.getElementsByTagName(
"ndss").item(0);
ndssElement.appendChild(channelElement);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// 设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
this.stopSelf();
}
return 0;
}
public int onDeleChannel(Intent intent, int flag) {
try {
int lenth = 0;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element ndssElement = (Element) document.getElementsByTagName(
"ndss").item(0);
Element root = document.getDocumentElement();
NodeList nodes = root.getElementsByTagName("ndss");
for (int i = 0; i < nodes.getLength(); i++) {
Element channelElement = (Element) (nodes.item(i));
int oldlenth = channelElement.getElementsByTagName("channel")
.getLength();
lenth = oldlenth;
ArrayList<String> infoList = new ArrayList<String>();
infoList = intent.getStringArrayListExtra("position");
for (String temp : infoList) {
for (int i1 = 0; i1 < lenth; i1++) {
Element channelData = (Element) channelElement
.getElementsByTagName("channel").item(i1);
if (intent != null) {
if (temp.equals(channelData.getAttribute("id"))) {
ndssElement.removeChild(channelData);
}
}
int currentLenth = channelElement.getElementsByTagName(
"channel").getLength();
lenth = currentLenth;
}
}
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
// 设置编码类型
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
out.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
this.stopSelf();
Log.d("jcmtest", "delete .......finished");
}
return 0;
}
public String readFileData(String fileName) {
String result = "";
try {
File file = new File(fileName);
if (file.length() > 2000) {
Toast.makeText(this, "the large file will read fail",
Toast.LENGTH_SHORT).show();
}
Log.d("jcmtest", "readFileData===************0****");
FileInputStream fin = new FileInputStream(file);// large file is not
// read success
int lenght = fin.available();
Log.d("jcmtest", "readFileData===************0****");
byte[] buffer = new byte[lenght];
fin.read(buffer);
result = EncodingUtils.getString(buffer, ENCODING);
Log.d("jcmtest", "readFileData===************1****");
fin.close();
} catch (Exception e) {
e.printStackTrace();
Log.d("jcmtest", "readFileData********error");
Log.d("jcmtest", e.getMessage());
}finally{
}
return result;
}
public int onCreateEap() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(FILE_PATH));
Element eapElement = document.createElement("eap");
eapElement.setAttribute("active", "1");
eapElement.setAttribute("options", "8");
eapElement.setAttribute("strict", "0");
Element provisioningElement = (Element) document
.getElementsByTagName("provisioning").item(0);
provisioningElement.appendChild(eapElement);
Element certificateElement = document
.createElement("root-certificate");
certificateElement.setAttribute("encoding", "pem");
Element neweapElement = (Element) document.getElementsByTagName(
"eap").item(0);
Log.d("jcmtest", "readFileData********1");
neweapElement.appendChild(certificateElement);
Element naiElement = document.createElement("nai");
neweapElement.insertBefore(naiElement, certificateElement);
Element newnaiElement = (Element) document.getElementsByTagName(
"nai").item(0);
Log.d("jcmtest", "readFileData********2");
Element prefixElement = document.createElement("prefix");
newnaiElement.appendChild(prefixElement);
Element realmElement = document.createElement("realm");
newnaiElement.insertBefore(realmElement, prefixElement);
Element usernameElement = document.createElement("username");
newnaiElement.insertBefore(usernameElement, realmElement);
Element ttlsElement = document.createElement("ttls");
ttlsElement.setAttribute("active", "1");
neweapElement.insertBefore(ttlsElement, certificateElement);
Log.d("jcmtest", "readFileData********3");
Element outerNaiTypemElement = document
.createElement("outerNaiType");
ttlsElement.appendChild(outerNaiTypemElement);
Element passwordeElement = document.createElement("password");
ttlsElement.insertBefore(passwordeElement, outerNaiTypemElement);
Element typeeElement = document.createElement("type");
ttlsElement.insertBefore(typeeElement, passwordeElement);
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
Log.d("jcmtest", "readFileData********4");
File file = new File(FILE_PATH);
FileOutputStream out = new FileOutputStream(file);
StreamResult xmlResult = new StreamResult(out);
transformer.transform(domSource, xmlResult);
Log.d("jcmtest", "readFileData********5");
} catch (Exception ex) {
System.out.println(ex.getMessage());
Log.d("jcmtest", "readFileData********6");
return -1;
}
return 0;
}
}