Code Conventions

This commit is contained in:
m_broelemann 2021-01-06 08:38:38 +01:00
parent a121811b12
commit 85c67f525b

View File

@ -18,7 +18,6 @@ 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;
import com.example.aped.utils.TestFavorites;
import com.example.aped.utils.XMLHandler; import com.example.aped.utils.XMLHandler;
import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
@ -52,9 +51,7 @@ public class MainActivity extends AppCompatActivity {
public ConfigurationHandler configurationHandler; public ConfigurationHandler configurationHandler;
/** zur Verwendung von favorite anstatt der direkten Einbindung.*/ /** zur Verwendung von favorite anstatt der direkten Einbindung.*/
public IFAVORITES favorite; public IFAVORITES favorite;
/** allgemeines.*/
/** /**
*
* @param savedInstanceState * @param savedInstanceState
*/ */
@Override @Override
@ -64,7 +61,8 @@ public class MainActivity extends AppCompatActivity {
ensurePermissions(); ensurePermissions();
setupConfigs(); setupConfigs();
favorite = configurationHandler; favorite = configurationHandler;
uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(), this); uebergabe = new Communicator(configurationHandler.getAddress(),
configurationHandler.getPort(), this);
uebergabe.downloadXML(); uebergabe.downloadXML();
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar); Toolbar toolbar = findViewById(R.id.toolbar);
@ -106,23 +104,32 @@ public class MainActivity extends AppCompatActivity {
MaterialAlertDialogBuilder alert = new MaterialAlertDialogBuilder alert = new
MaterialAlertDialogBuilder(MainActivity.this); MaterialAlertDialogBuilder(MainActivity.this);
//alert.setTitle(R.string.action_set_connection); //alert.setTitle(R.string.action_set_connection);
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.alert_dialog_ip_pot_eingabe,null); View view2 = LayoutInflater.from(MainActivity.this).inflate(R.
EditText ipAdress = (EditText) view2.findViewById(R.id.editTextIp); layout.alert_dialog_ip_pot_eingabe, null);
EditText ipAdress = (EditText) view2.findViewById(R.id.
editTextIp);
ipAdress.setText(configurationHandler.getAddress()); ipAdress.setText(configurationHandler.getAddress());
EditText port = (EditText) view2.findViewById(R.id.editTextPort); EditText port = (EditText) view2.findViewById(R.id.
port.setText(String.valueOf(configurationHandler.getPort()) ); editTextPort);
port.setText(String.valueOf(configurationHandler.getPort()));
alert.setView(view2); alert.setView(view2);
alert.setPositiveButton("set", alert.setPositiveButton("set",
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
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
configurationHandler.setAddress(ipAdress.getText().toString()); configurationHandler.setAddress(ipAdress.
configurationHandler.setPort(Integer.parseInt(port.getText().toString())); getText().toString());
configurationHandler.setPort(Integer.parseInt(
port.getText().toString()));
Toast.makeText(getApplicationContext(), Toast.makeText(getApplicationContext(),
configurationHandler.getAddress() + ":" + configurationHandler.getPort() + " is set", configurationHandler.getAddress()
+ ":" + configurationHandler.
getPort() + " is set",
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
uebergabe = new Communicator(configurationHandler.getAddress(), configurationHandler.getPort(), uebergabe = new Communicator(
configurationHandler.getAddress(),
configurationHandler.getPort(),
MainActivity.this); MainActivity.this);
} }
}); });
@ -145,22 +152,23 @@ public class MainActivity extends AppCompatActivity {
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
String externalPrivateStorageDir = null; String externalPrivateStorageDir = null;
try { try {
externalPrivateStorageDir = ExternalStorageHandler.getExternalPrivateStorageDir(this); externalPrivateStorageDir = ExternalStorageHandler.
getExternalPrivateStorageDir(this);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e("MainActivity","error while opening the config.xml for adjustment: " + e.getMessage()); Log.e("MainActivity", "error while opening the "
+ "config.xml for adjustment: " + e.getMessage());
} }
File xmlFile = new File(externalPrivateStorageDir, "config.xml"); File xmlFile = new File(externalPrivateStorageDir,
Uri uri = FileProvider.getUriForFile(this,this.getApplicationContext().getPackageName() + ".provider", xmlFile); "config.xml");
Uri uri = FileProvider.getUriForFile(this,
this.getApplicationContext().getPackageName()
+ ".provider", xmlFile);
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"text/xml"); intent.setDataAndType(uri, "text/xml");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent); startActivity(intent);
/**
* xml anstatt beispieltext muss übergeben werden
* rückgabe der xml fehlt noch
*/
break; break;
case R.id.action_download_xml: case R.id.action_download_xml:
@ -255,25 +263,27 @@ public class MainActivity extends AppCompatActivity {
private void setupConfigs() { private void setupConfigs() {
try { try {
String externalConfigPath = ExternalStorageHandler.getExternalPrivateStorageDir(MainActivity.this); String externalConfigPath = ExternalStorageHandler.
File xmlFile = new File(externalConfigPath,"config.xml"); getExternalPrivateStorageDir(MainActivity.this);
File xmlFile = new File(externalConfigPath, "config.xml");
File configFile = new File(externalConfigPath, "config.json"); File configFile = new File(externalConfigPath, "config.json");
if(!xmlFile.exists()){ if (!xmlFile.exists()) {
setDefaultXML(xmlFile); setDefaultXML(xmlFile);
} }
if(!configFile.exists()){ if (!configFile.exists()) {
setDefaultConfig(configFile); setDefaultConfig(configFile);
} }
xml = new XMLHandler(xmlFile); xml = new XMLHandler(xmlFile);
configurationHandler = new ConfigurationHandler(configFile.getPath()); configurationHandler = new ConfigurationHandler(configFile.getPath());
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
Log.e("Main Activity", "failed to find the external private storage during CONFIG SETUP: " + e.getMessage()); Log.e("Main Activity", "failed to find the external "
+ "private storage during CONFIG SETUP: " + e.getMessage());
} }
} }
private void setDefaultXML(File xmlFile){ private void setDefaultXML(final File xmlFile) {
try{ try {
InputStream inputStream = getAssets().open( InputStream inputStream = getAssets().open(
"XML/default.xml"); "XML/default.xml");
byte[] buffer = new byte[inputStream.available()]; byte[] buffer = new byte[inputStream.available()];
@ -283,12 +293,13 @@ public class MainActivity extends AppCompatActivity {
fileOutputStream.write(buffer); fileOutputStream.write(buffer);
fileOutputStream.close(); fileOutputStream.close();
} catch (Exception e) { } catch (Exception e) {
Log.e("MainActivity", "error while transferring the default.xml into the config.xml: " + e.getMessage()); Log.e("MainActivity", "error while transferring the "
+ "default.xml into the config.xml: " + e.getMessage());
} }
} }
private void setDefaultConfig(File xmlFile){ private void setDefaultConfig(final File xmlFile) {
try{ try {
InputStream inputStream = getAssets().open( InputStream inputStream = getAssets().open(
"defaultConfig.json"); "defaultConfig.json");
byte[] buffer = new byte[inputStream.available()]; byte[] buffer = new byte[inputStream.available()];
@ -298,7 +309,9 @@ public class MainActivity extends AppCompatActivity {
fileOutputStream.write(buffer); fileOutputStream.write(buffer);
fileOutputStream.close(); fileOutputStream.close();
} catch (Exception e) { } catch (Exception e) {
Log.e("MainActivity", "error while transferring the defaultConfig.json into the config.json: " + e.getMessage()); Log.e("MainActivity", "error while transferring the "
+ "defaultConfig.json into the config.json: "
+ e.getMessage());
} }
} }
} }