tested and improved ConfigurationHandler
This commit is contained in:
		
							parent
							
								
									68baa9e06f
								
							
						
					
					
						commit
						51b0cae86f
					
				
							
								
								
									
										5
									
								
								APED/app/src/main/assets/defaultConfig.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								APED/app/src/main/assets/defaultConfig.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,5 @@
 | 
			
		||||
{
 | 
			
		||||
  "address": "192.168.2.203",
 | 
			
		||||
  "port" : 8080,
 | 
			
		||||
  "favorites" : [ ]
 | 
			
		||||
}
 | 
			
		||||
@ -11,9 +11,13 @@ import android.view.MenuItem;
 | 
			
		||||
import android.view.Menu;
 | 
			
		||||
import android.widget.EditText;
 | 
			
		||||
import android.widget.LinearLayout;
 | 
			
		||||
import android.widget.ListView;
 | 
			
		||||
import android.widget.Toast;
 | 
			
		||||
 | 
			
		||||
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.IFAVORITES;
 | 
			
		||||
import com.example.aped.utils.IXML;
 | 
			
		||||
@ -55,10 +59,11 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
    public  IXML xml;
 | 
			
		||||
    /** zur Verwendung von uebergabe anstatt der direkten Einbindung.*/
 | 
			
		||||
    public Communicator uebergabe;
 | 
			
		||||
    /** Handler for the custom user configurations. **/
 | 
			
		||||
    public ConfigurationHandler configurationHandler;
 | 
			
		||||
    /** zur Verwendung von favorite anstatt der direkten Einbindung.*/
 | 
			
		||||
    public  IFAVORITES favorite = new TestFavorites();
 | 
			
		||||
    /** allgemeines.*/
 | 
			
		||||
    private String ip = "192.168.1.220";
 | 
			
		||||
    /**
 | 
			
		||||
     *
 | 
			
		||||
     * @param savedInstanceState
 | 
			
		||||
@ -68,8 +73,9 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
        //checks that the permission to read and write the xml is granted
 | 
			
		||||
        ensurePermissions();
 | 
			
		||||
        setXML();
 | 
			
		||||
        uebergabe = new Communicator(ip, 8080, this);
 | 
			
		||||
        setupConfigs();
 | 
			
		||||
        uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(), this);
 | 
			
		||||
        uebergabe.downloadXML();
 | 
			
		||||
        setContentView(R.layout.activity_main);
 | 
			
		||||
        Toolbar toolbar = findViewById(R.id.toolbar);
 | 
			
		||||
        setSupportActionBar(toolbar);
 | 
			
		||||
@ -87,7 +93,6 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        NavigationUI.setupActionBarWithNavController(this,
 | 
			
		||||
                                        navController, mAppBarConfiguration);
 | 
			
		||||
        NavigationUI.setupWithNavController(navigationView, navController);
 | 
			
		||||
        //xml = new TestXML();
 | 
			
		||||
    }
 | 
			
		||||
    /** Fügt Elemente zur Aktionsleiste hinzu, wenn diese vorhanden ist.*/
 | 
			
		||||
    @Override
 | 
			
		||||
@ -114,7 +119,7 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
                alert.setMessage("Please enter the ip address of the "
 | 
			
		||||
                        + "device to be connected");
 | 
			
		||||
                final EditText input = new EditText(MainActivity.this);
 | 
			
		||||
                input.setText(ip);
 | 
			
		||||
                input.setText(configurationHandler.getAddress());
 | 
			
		||||
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
 | 
			
		||||
                        LinearLayout.LayoutParams.MATCH_PARENT,
 | 
			
		||||
                        LinearLayout.LayoutParams.MATCH_PARENT);
 | 
			
		||||
@ -125,11 +130,11 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
                            public void onClick(final DialogInterface dialog,
 | 
			
		||||
                                                final int which) {
 | 
			
		||||
                                // Write your code here to execute after dialog
 | 
			
		||||
                                ip = input.getText().toString();
 | 
			
		||||
                                configurationHandler.setAddress(input.getText().toString());
 | 
			
		||||
                                Toast.makeText(getApplicationContext(),
 | 
			
		||||
                                        ip + " IP connect",
 | 
			
		||||
                                        configurationHandler.getAddress() + " IP connect",
 | 
			
		||||
                                        Toast.LENGTH_SHORT).show();
 | 
			
		||||
                                uebergabe = new Communicator(ip, 8080,
 | 
			
		||||
                                uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(),
 | 
			
		||||
                                        MainActivity.this);
 | 
			
		||||
                            }
 | 
			
		||||
                        });
 | 
			
		||||
@ -162,6 +167,8 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
                break;
 | 
			
		||||
 | 
			
		||||
            case R.id.action_download_xml:
 | 
			
		||||
                ListView idListView = (ListView) findViewById(R.id.idListView);
 | 
			
		||||
                idListView.setAdapter(new MainListViewAdapter(this));
 | 
			
		||||
                    Toast.makeText(this, "Downloaded .xml",
 | 
			
		||||
                        Toast.LENGTH_SHORT).show();
 | 
			
		||||
                break;
 | 
			
		||||
@ -238,32 +245,52 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void setXML() {
 | 
			
		||||
    private void setupConfigs() {
 | 
			
		||||
        try {
 | 
			
		||||
            String xmlPath = ExternalStorageHandler.
 | 
			
		||||
                    getExternalPrivateStorageDir(MainActivity.this);
 | 
			
		||||
            File xmlFile = new File(xmlPath, "config.xml");
 | 
			
		||||
            String externalConfigPath = ExternalStorageHandler.getExternalPrivateStorageDir(MainActivity.this);
 | 
			
		||||
        File xmlFile = new File(externalConfigPath,"config.xml");
 | 
			
		||||
        File configFile = new File(externalConfigPath, "config.json");
 | 
			
		||||
        if(!xmlFile.exists()){
 | 
			
		||||
            setDefaultXML(xmlFile);
 | 
			
		||||
        }
 | 
			
		||||
        if(!configFile.exists()){
 | 
			
		||||
            setDefaultConfig(configFile);
 | 
			
		||||
        }
 | 
			
		||||
        xml = new XMLHandler(xmlFile);
 | 
			
		||||
        configurationHandler = new ConfigurationHandler(configFile.getPath());
 | 
			
		||||
 | 
			
		||||
            //create the default XML config by using the default.xml from the assets folder
 | 
			
		||||
            if (xmlFile.exists()) {
 | 
			
		||||
                xmlFile.delete();
 | 
			
		||||
            }
 | 
			
		||||
        } 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(
 | 
			
		||||
                    "XML/Test.xml");
 | 
			
		||||
                    "XML/default.xml");
 | 
			
		||||
            byte[] buffer = new byte[inputStream.available()];
 | 
			
		||||
            inputStream.read(buffer);
 | 
			
		||||
            inputStream.close();
 | 
			
		||||
            FileOutputStream fileOutputStream = new FileOutputStream(xmlFile);
 | 
			
		||||
            fileOutputStream.write(buffer);
 | 
			
		||||
            fileOutputStream.close();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
           Log.e("MainActivity", "error while transferring the default.xml into the config.xml: " + e.getMessage());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
            xml = new XMLHandler(xmlFile);
 | 
			
		||||
        } catch (FileNotFoundException e) {
 | 
			
		||||
            Log.e("Main Activity", "Exception while reading the "
 | 
			
		||||
                    + "external private storage dir: " + e.getMessage());
 | 
			
		||||
        } catch (ParserConfigurationException | IOException | SAXException e) {
 | 
			
		||||
            Log.e("Main Activity", "XMLHandler failed to initialise: "
 | 
			
		||||
                    + e.getMessage());
 | 
			
		||||
    private void setDefaultConfig(File xmlFile){
 | 
			
		||||
        try{
 | 
			
		||||
            InputStream inputStream = getAssets().open(
 | 
			
		||||
                    "defaultConfig.json");
 | 
			
		||||
            byte[] buffer = new byte[inputStream.available()];
 | 
			
		||||
            inputStream.read(buffer);
 | 
			
		||||
            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());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -21,80 +21,6 @@ import java.util.List;
 | 
			
		||||
 */
 | 
			
		||||
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. **/
 | 
			
		||||
    private ConfigData configurations;
 | 
			
		||||
    /** The file path of the configuration file. **/
 | 
			
		||||
@ -106,6 +32,7 @@ public class ConfigurationHandler implements IFAVORITES {
 | 
			
		||||
     */
 | 
			
		||||
    public ConfigurationHandler(final String configFilePath) {
 | 
			
		||||
        this.filePath = configFilePath;
 | 
			
		||||
        configurations = new ConfigData();
 | 
			
		||||
        readJsonFile();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -203,7 +130,7 @@ public class ConfigurationHandler implements IFAVORITES {
 | 
			
		||||
                    new FileReader(filePath));
 | 
			
		||||
            String line;
 | 
			
		||||
            while ((line = bufferedReader.readLine()) != null) {
 | 
			
		||||
                jsonData += line + "\n";
 | 
			
		||||
                jsonData += line;
 | 
			
		||||
            }
 | 
			
		||||
            bufferedReader.close();
 | 
			
		||||
            JSONObject jsonObject = new JSONObject(jsonData);
 | 
			
		||||
@ -253,4 +180,78 @@ public class ConfigurationHandler implements IFAVORITES {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //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"
 | 
			
		||||
                    + "}";
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -24,8 +24,9 @@ import javax.xml.xpath.XPathExpression;
 | 
			
		||||
import javax.xml.xpath.XPathExpressionException;
 | 
			
		||||
import javax.xml.xpath.XPathFactory;
 | 
			
		||||
 | 
			
		||||
import kotlin.NotImplementedError;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Class to handle operations on the config.xml files.
 | 
			
		||||
 */
 | 
			
		||||
public class XMLHandler implements IXML {
 | 
			
		||||
 | 
			
		||||
    /** 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 SAXException the XML parse failed
 | 
			
		||||
     */
 | 
			
		||||
    public XMLHandler(final File xmlFile)
 | 
			
		||||
        throws ParserConfigurationException, IOException, SAXException {
 | 
			
		||||
        //parse the root document from the XML file
 | 
			
		||||
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 | 
			
		||||
        DocumentBuilder builder = factory.newDocumentBuilder();
 | 
			
		||||
        root = builder.parse(xmlFile);
 | 
			
		||||
    public XMLHandler(final File xmlFile) {
 | 
			
		||||
        try {
 | 
			
		||||
            DocumentBuilderFactory factory = DocumentBuilderFactory
 | 
			
		||||
                    .newInstance();
 | 
			
		||||
            DocumentBuilder builder = factory.newDocumentBuilder();
 | 
			
		||||
            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());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user