package android.config.ip;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemProperties;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import android.view.View;
public class config extends Activity {
/** Called when the activity is first created. */
private EditText IpText;
private EditText NetmaskText;
private Button Config, exit;
String Filename;
File file = null;
// TextView ipView, netmaskView;
int filelength;
private int CreatFile()
{
file = new File("/data/data/android.config.ip/ipconfig");
if(!file.exists())
{
try
{
file.createNewFile();
Log.i("write", "Creat file success");
return 1;
}catch(IOException e)
{
Log.i("write", "Creat file fail");
return -1;
}
}
else
{
Log.i("write", "file is exist");
return 0;
}
}
private void writeFile(File fileName, String info) {
try {
OutputStream fout = new BufferedOutputStream(new FileOutputStream(fileName));
byte[] bytes = info.getBytes();
fout.write(bytes);
fout.close();
Log.i("write","Write file sucess");
} catch (Exception err)
{
Log.i("write","Write file fail");
}
}
private String readFile(File fileName)
{
String Strtmp = null;
try {
InputStream fin = new BufferedInputStream(new FileInputStream(fileName));
filelength = fin.available();
byte[] bytes = new byte[filelength];
fin.read(bytes);
Log.i("write","Read file success");
Strtmp = new String(bytes);
return Strtmp;
} catch (Exception err)
{
Log.i("write","Read file fail");
return "";
}
}
private boolean CheckIPFormat(String ip,boolean isMask)
{
//判断格式是否正确
String[] arrIp = ip.split("\\.");
int tmpint;
if(arrIp.length !=4)
{
//Log.i("write", "A:" + String.valueOf(arrIp.length));
return false;
}
for(int i=0;i<4;i++)
{
tmpint = Integer.parseInt(arrIp[i],10);
if(!isMask && (i==0) && (tmpint<1 || tmpint>223))return false;
if(tmpint<0 || tmpint>255)
{
//Log.i("write", "B:" + String.valueOf(tmpint));
return false;
}
}
return true;
}
private boolean ConfigIpMask()
{
SystemProperties.set("ctl.start","setip");
try
{
Thread.sleep(1000);
}catch(Exception ex){
Log.i("write","Excetion: " + ex.getMessage());
}
String ret = SystemProperties.get("init.svc.setip","");
if(ret != null && ret.equals("stopped"))
{
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ipView = (TextView)findViewById(R.id.iptext);
// netmaskView = (TextView)findViewById(R.id.netmasktext);
IpText = (EditText)findViewById(R.id.ip);
NetmaskText = (EditText)findViewById(R.id.netmask);
// ipView.setText("IP: ");
// netmaskView.setText("mask: ");
//从/data/data/android.config.ip/ipconfig文件中读取ip和mask并显示出来
if(CreatFile() == 0)
{
String IpMaskStr = readFile(file);
if(IpMaskStr != null && IpMaskStr.length()>0)
{
try{
String arrCfg[] = IpMaskStr.split(" ");
IpText.setText(arrCfg[0]);
NetmaskText.setText(arrCfg[1]);
//SystemProperties.set("ctl.start","setip");
if(!ConfigIpMask()) Toast.makeText(config.this, "config ip & mask error", Toast.LENGTH_SHORT).show();
}catch(Exception err)
{
Log.i("write", "display ip & mask error");
}
}
}
//输入ip和netmask后,按下config按钮,获取ip和mask的值
Config = (Button)findViewById(R.id.config);
Config.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
IpText = (EditText)findViewById(R.id.ip);
NetmaskText = (EditText)findViewById(R.id.netmask);
//取得两个控件的值
String IpStr = IpText.getText().toString();
String NetmaskStr = NetmaskText.getText().toString();
if(!CheckIPFormat(IpStr,false) || !CheckIPFormat(NetmaskStr,true))
{
//show message box
Toast.makeText(config.this, "error input:xxx.xxx.xxx.xxx", Toast.LENGTH_SHORT).show();
}
else
{
//把ip和netmask写入文件中
writeFile(file, IpStr.concat(" ").concat(NetmaskStr));
//调用setip服务修改IP和mask
//SystemProperties.set("ctl.start","setip");
if(!ConfigIpMask()) Toast.makeText(config.this, "config ip & mask error", Toast.LENGTH_SHORT).show();
else finish();
}
}
});
//按下exit按钮,设置原IP和mask
exit = (Button)findViewById(R.id.exit);
exit.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}
);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页