package kr.ac.kaist.resl.sensorservice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
public class MainActivity extends Activity implements CompoundButton.OnCheckedChangeListener {
//Default URL and Period
public static String URL = "http://bjack.kaist.ac.kr/NewLilliput/MobileAdaptor";
public static int period = 10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isConfFileExist = isFileExist("/SensorService/Configuration");
if( isConfFileExist == false )
{
//Make default configuration
String conf = URL + "\n" + period;
writeToSDFile("Configuration", conf);
}
else
{
try {
String prevConf = readFromSDFile("Configuration");
String[] confs = prevConf.split("\n");
String prevURL = confs[0];
String prevPeriod = confs[1];
MainActivity.URL = prevURL;
MainActivity.period = Integer.parseInt(prevPeriod);
} catch (IOException e) {
e.printStackTrace();
}
}
Switch sensorSwitch = (Switch)findViewById(R.id.sensorSwitch);
Switch locationSwitch = (Switch)findViewById(R.id.locationSwitch);
Switch connectivitySwitch = (Switch)findViewById(R.id.connectivitySwitch);
ActivityManager am = (ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> rs = am.getRunningServices(200);
for( int i = 0 ; i < rs.size() ; i++ )
{
ActivityManager.RunningServiceInfo rsi = rs.get(i);
String serviceName = rsi.service.getClassName();
if( serviceName.equals("kr.ac.kaist.resl.sensorservice.SensorService"))
{
stopService(new Intent(this, SensorService.class));
sensorSwitch.setChecked(false);
}
else if( serviceName.equals("kr.ac.kaist.resl.sensorservice.LocationService"))
{
stopService(new Intent(this, LocationService.class));
locationSwitch.setChecked(false);
}
else if( serviceName.equals("kr.ac.kaist.resl.sensorservice.BluetoothService"))
{
stopService(new Intent(this, BluetoothService.class));
connectivitySwitch.setChecked(false);
}
}
sensorSwitch.setOnCheckedChangeListener(this);
locationSwitch.setOnCheckedChangeListener(this);
connectivitySwitch.setOnCheckedChangeListener(this);
EditText urlText = (EditText)findViewById(R.id.remoteURLEditText);
urlText.setText(MainActivity.URL);
urlText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
MainActivity.URL = s.toString();
try {
String prevConf = readFromSDFile("Configuration");
String[] confs = prevConf.split("\n");
String prevPeriod = confs[1];
String newConf = s.toString() + "\n" + prevPeriod;
writeToSDFile("Configuration", newConf);
} catch (IOException e) {
e.printStackTrace();
}
}
});
EditText periodText = (EditText)findViewById(R.id.periodNumber);
periodText.setText(String.valueOf(MainActivity.period));
periodText.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before,int count) {}
@Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if( !str.equals("") && !str.equals("0"))
{
try
{
MainActivity.period = Integer.parseInt(s.toString());
String prevConf = readFromSDFile("Configuration");
String[] confs = prevConf.split("\n");
String prevURL = confs[0];
String newConf = prevURL + "\n" + s.toString();
writeToSDFile("Configuration", newConf);
}
catch( NumberFormatException e )
{
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int curID = buttonView.getId();
EditText urlText = (EditText)findViewById(R.id.remoteURLEditText);
String url = urlText.getText().toString();
MainActivity.URL = url;
EditText periodText = (EditText)findViewById(R.id.periodNumber);
if( periodText.getText().toString().equals("") )
{
Toast.makeText(getApplicationContext(), "Need period", Toast.LENGTH_SHORT).show();
periodText.setText("5");
if( isChecked)
{
buttonView.setChecked(false);
}else
{
buttonView.setChecked(true);
}
return;
}
MainActivity.period = Integer.parseInt(periodText.getText().toString());
if( period == 0 )
{
Toast.makeText(getApplicationContext(), "Period has to be more than 0", Toast.LENGTH_SHORT).show();
periodText.setText("5");
if( isChecked)
{
buttonView.setChecked(false);
}else
{
buttonView.setChecked(true);
}
return;
}
if( curID == R.id.sensorSwitch )
{
if( isChecked)
{
startService(new Intent(this, SensorService.class));
}else
{
stopService(new Intent(this, SensorService.class));
}
}else if( curID == R.id.locationSwitch )
{
if( isChecked)
{
startService(new Intent(this, LocationService.class));
}else
{
stopService(new Intent(this, LocationService.class));
}
}else if( curID == R.id.connectivitySwitch )
{
if( isChecked)
{
startService(new Intent(this, BluetoothService.class));
}else
{
stopService(new Intent(this, BluetoothService.class));
}
}
}
public boolean isFileExist(String path)
{
File root = android.os.Environment.getExternalStorageDirectory();
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File confFile = new File (root.getAbsolutePath() + path);
if( confFile.isFile() )
{
return true;
}
else
{
return false;
}
}
public String readFromSDFile(String fileName) throws IOException
{
File root = android.os.Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath() + "/SensorService/" + fileName);
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String line = "";
String seg;
while( ( seg = reader.readLine() ) != null )
{
line += seg + "\n";
}
reader.close();
return line;
}
private void writeToSDFile(String fileName, String message){
// Find the root of the external storage.
// See http://developer.android.com/guide/topics/data/data- storage.html#filesExternal
File root = android.os.Environment.getExternalStorageDirectory();
// See http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder
File dir = new File (root.getAbsolutePath() + "/SensorService");
dir.mkdirs();
File file = new File(dir, fileName);
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.print(message);
pw.flush();
pw.close();
f.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}