tested and improved ConfigurationHandler

This commit is contained in:
paul-loedige 2021-01-01 23:11:27 +01:00
parent 68baa9e06f
commit 51b0cae86f
4 changed files with 150 additions and 107 deletions

View File

@ -0,0 +1,5 @@
{
"address": "192.168.2.203",
"port" : 8080,
"favorites" : [ ]
}

View File

@ -11,9 +11,13 @@ import android.view.MenuItem;
import android.view.Menu; import android.view.Menu;
import android.widget.EditText; import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import com.example.aped.communication.Communicator; import com.example.aped.communication.Communicator;
import com.example.aped.ui.all_IOs.AllIOsFragment;
import com.example.aped.ui.all_IOs.MainListViewAdapter;
import com.example.aped.utils.ConfigurationHandler;
import com.example.aped.utils.ExternalStorageHandler; import com.example.aped.utils.ExternalStorageHandler;
import com.example.aped.utils.IFAVORITES; import com.example.aped.utils.IFAVORITES;
import com.example.aped.utils.IXML; import com.example.aped.utils.IXML;
@ -55,10 +59,11 @@ public class MainActivity extends AppCompatActivity {
public IXML xml; public IXML xml;
/** zur Verwendung von uebergabe anstatt der direkten Einbindung.*/ /** zur Verwendung von uebergabe anstatt der direkten Einbindung.*/
public Communicator uebergabe; public Communicator uebergabe;
/** Handler for the custom user configurations. **/
public ConfigurationHandler configurationHandler;
/** zur Verwendung von favorite anstatt der direkten Einbindung.*/ /** zur Verwendung von favorite anstatt der direkten Einbindung.*/
public IFAVORITES favorite = new TestFavorites(); public IFAVORITES favorite = new TestFavorites();
/** allgemeines.*/ /** allgemeines.*/
private String ip = "192.168.1.220";
/** /**
* *
* @param savedInstanceState * @param savedInstanceState
@ -68,8 +73,9 @@ public class MainActivity extends AppCompatActivity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
//checks that the permission to read and write the xml is granted //checks that the permission to read and write the xml is granted
ensurePermissions(); ensurePermissions();
setXML(); setupConfigs();
uebergabe = new Communicator(ip, 8080, this); uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(), this);
uebergabe.downloadXML();
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
@ -87,7 +93,6 @@ public class MainActivity extends AppCompatActivity {
NavigationUI.setupActionBarWithNavController(this, NavigationUI.setupActionBarWithNavController(this,
navController, mAppBarConfiguration); navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController); NavigationUI.setupWithNavController(navigationView, navController);
//xml = new TestXML();
} }
/** Fügt Elemente zur Aktionsleiste hinzu, wenn diese vorhanden ist.*/ /** Fügt Elemente zur Aktionsleiste hinzu, wenn diese vorhanden ist.*/
@Override @Override
@ -114,7 +119,7 @@ public class MainActivity extends AppCompatActivity {
alert.setMessage("Please enter the ip address of the " alert.setMessage("Please enter the ip address of the "
+ "device to be connected"); + "device to be connected");
final EditText input = new EditText(MainActivity.this); final EditText input = new EditText(MainActivity.this);
input.setText(ip); input.setText(configurationHandler.getAddress());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT); LinearLayout.LayoutParams.MATCH_PARENT);
@ -125,11 +130,11 @@ public class MainActivity extends AppCompatActivity {
public void onClick(final DialogInterface dialog, public void onClick(final DialogInterface dialog,
final int which) { final int which) {
// Write your code here to execute after dialog // Write your code here to execute after dialog
ip = input.getText().toString(); configurationHandler.setAddress(input.getText().toString());
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
ip + " IP connect", configurationHandler.getAddress() + " IP connect",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
uebergabe = new Communicator(ip, 8080, uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(),
MainActivity.this); MainActivity.this);
} }
}); });
@ -162,6 +167,8 @@ public class MainActivity extends AppCompatActivity {
break; break;
case R.id.action_download_xml: case R.id.action_download_xml:
ListView idListView = (ListView) findViewById(R.id.idListView);
idListView.setAdapter(new MainListViewAdapter(this));
Toast.makeText(this, "Downloaded .xml", Toast.makeText(this, "Downloaded .xml",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
break; break;
@ -238,32 +245,52 @@ public class MainActivity extends AppCompatActivity {
} }
} }
private void setXML() { private void setupConfigs() {
try { try {
String xmlPath = ExternalStorageHandler. String externalConfigPath = ExternalStorageHandler.getExternalPrivateStorageDir(MainActivity.this);
getExternalPrivateStorageDir(MainActivity.this); File xmlFile = new File(externalConfigPath,"config.xml");
File xmlFile = new File(xmlPath, "config.xml"); File configFile = new File(externalConfigPath, "config.json");
if(!xmlFile.exists()){
//create the default XML config by using the default.xml from the assets folder setDefaultXML(xmlFile);
if (xmlFile.exists()) {
xmlFile.delete();
} }
if(!configFile.exists()){
setDefaultConfig(configFile);
}
xml = new XMLHandler(xmlFile);
configurationHandler = new ConfigurationHandler(configFile.getPath());
} catch (FileNotFoundException e) {
Log.e("Main Activity", "failed to find the external private storage during CONFIG SETUP: " + e.getMessage());
}
}
private void setDefaultXML(File xmlFile){
try{
InputStream inputStream = getAssets().open( InputStream inputStream = getAssets().open(
"XML/Test.xml"); "XML/default.xml");
byte[] buffer = new byte[inputStream.available()]; byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer); inputStream.read(buffer);
inputStream.close(); inputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream(xmlFile); FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
fileOutputStream.write(buffer); fileOutputStream.write(buffer);
fileOutputStream.close(); fileOutputStream.close();
} catch (Exception e) {
Log.e("MainActivity", "error while transferring the default.xml into the config.xml: " + e.getMessage());
}
}
xml = new XMLHandler(xmlFile); private void setDefaultConfig(File xmlFile){
} catch (FileNotFoundException e) { try{
Log.e("Main Activity", "Exception while reading the " InputStream inputStream = getAssets().open(
+ "external private storage dir: " + e.getMessage()); "defaultConfig.json");
} catch (ParserConfigurationException | IOException | SAXException e) { byte[] buffer = new byte[inputStream.available()];
Log.e("Main Activity", "XMLHandler failed to initialise: " inputStream.read(buffer);
+ e.getMessage()); inputStream.close();
FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
fileOutputStream.write(buffer);
fileOutputStream.close();
} catch (Exception e) {
Log.e("MainActivity", "error while transferring the defaultConfig.json into the config.json: " + e.getMessage());
} }
} }
} }

View File

@ -21,80 +21,6 @@ import java.util.List;
*/ */
public class ConfigurationHandler implements IFAVORITES { public class ConfigurationHandler implements IFAVORITES {
/**
* Subclass for storing the configuration data in an OOP conform manner.
*/
private class ConfigData {
/** The address of the raspberry. **/
private String address;
/** The port of the raspberry. **/
private int port;
/** The names of the favorite devices. **/
private List<String> favoriteNames;
/**
* Getter for the address variable.
* @return the address of the raspberry
*/
public String getAddress() {
return address;
}
/**
* Setter for the address variable.
* @param pAddress the new address for the raspberry
*/
public void setAddress(final String pAddress) {
this.address = address;
}
/**
* Getter for the port variable.
* @return the port of the raspberry
*/
public int getPort() {
return port;
}
/**
* Setter for the port variable.
* @param pPort the new port for the raspberry
*/
public void setPort(final int pPort) {
this.port = pPort;
}
/**
* Getter for the favoriteNames variable.
* @return the favorite device names
*/
public List<String> getFavoriteNames() {
return favoriteNames;
}
/**
* Setter for the favoriteNames variable.
* @param pFavoriteNames the new favorite device names
*/
public void setFavoriteNames(final List<String> pFavoriteNames) {
this.favoriteNames = pFavoriteNames;
}
/**
* Method to convert an object of {@link ConfigData} class to String.
* @return a String in JSON format
*/
@NonNull
@Override
public String toString() {
return "{\n"
+ "\"address\":\"" + address + "\"\n"
+ "\"port\":\"" + port + "\"\n"
+ "\"favorites\":" + favoriteNames.toString() + "\"\n"
+ "}";
}
}
/** The current configurations. **/ /** The current configurations. **/
private ConfigData configurations; private ConfigData configurations;
/** The file path of the configuration file. **/ /** The file path of the configuration file. **/
@ -106,6 +32,7 @@ public class ConfigurationHandler implements IFAVORITES {
*/ */
public ConfigurationHandler(final String configFilePath) { public ConfigurationHandler(final String configFilePath) {
this.filePath = configFilePath; this.filePath = configFilePath;
configurations = new ConfigData();
readJsonFile(); readJsonFile();
} }
@ -203,7 +130,7 @@ public class ConfigurationHandler implements IFAVORITES {
new FileReader(filePath)); new FileReader(filePath));
String line; String line;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
jsonData += line + "\n"; jsonData += line;
} }
bufferedReader.close(); bufferedReader.close();
JSONObject jsonObject = new JSONObject(jsonData); JSONObject jsonObject = new JSONObject(jsonData);
@ -253,4 +180,78 @@ public class ConfigurationHandler implements IFAVORITES {
} }
//endregion //endregion
/**
* Subclass for storing the configuration data in an OOP conform manner.
*/
private class ConfigData {
/** The address of the raspberry. **/
private String address = "";
/** The port of the raspberry. **/
private int port = -1;
/** The names of the favorite devices. **/
private List<String> favoriteNames = new ArrayList<>();
/**
* Getter for the address variable.
* @return the address of the raspberry
*/
public String getAddress() {
return address;
}
/**
* Setter for the address variable.
* @param pAddress the new address for the raspberry
*/
public void setAddress(final String pAddress) {
this.address = pAddress;
}
/**
* Getter for the port variable.
* @return the port of the raspberry
*/
public int getPort() {
return port;
}
/**
* Setter for the port variable.
* @param pPort the new port for the raspberry
*/
public void setPort(final int pPort) {
this.port = pPort;
}
/**
* Getter for the favoriteNames variable.
* @return the favorite device names
*/
public List<String> getFavoriteNames() {
return favoriteNames;
}
/**
* Setter for the favoriteNames variable.
* @param pFavoriteNames the new favorite device names
*/
public void setFavoriteNames(final List<String> pFavoriteNames) {
this.favoriteNames = pFavoriteNames;
}
/**
* Method to convert an object of {@link ConfigData} class to String.
* @return a String in JSON format
*/
@NonNull
@Override
public String toString() {
return "{\n"
+ "\"address\":\"" + address + "\",\n"
+ "\"port\":\"" + port + "\",\n"
+ "\"favorites\":" + favoriteNames.toString() + "\n"
+ "}";
}
}
} }

View File

@ -24,8 +24,9 @@ import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import kotlin.NotImplementedError; /**
* Class to handle operations on the config.xml files.
*/
public class XMLHandler implements IXML { public class XMLHandler implements IXML {
/** the root of the XMl file. **/ /** the root of the XMl file. **/
@ -38,12 +39,21 @@ public class XMLHandler implements IXML {
* @throws IOException the XML file could not be accessed * @throws IOException the XML file could not be accessed
* @throws SAXException the XML parse failed * @throws SAXException the XML parse failed
*/ */
public XMLHandler(final File xmlFile) public XMLHandler(final File xmlFile) {
throws ParserConfigurationException, IOException, SAXException { try {
//parse the root document from the XML file DocumentBuilderFactory factory = DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); .newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
root = builder.parse(xmlFile); root = builder.parse(xmlFile);
} catch (ParserConfigurationException e) {
Log.e("XMLHandler", "error while parsing the XML file: "
+ e.getMessage());
} catch (IOException e) {
Log.e("XMLHandler", "error while reading from the XML file: "
+ e.getMessage());
} catch (SAXException e) {
Log.e("XMLHandler", "general SAX error: " + e.getMessage());
}
} }
/** /**