Don't overwrite built-in profiles

This commit is contained in:
Manuel Fuhr 2021-11-17 14:34:16 +01:00
parent 67bbc3d2ac
commit f29616eefc

View file

@ -36,28 +36,31 @@ public class BImportActivity extends Activity {
mTextFilename = findViewById(R.id.editTextFilename); mTextFilename = findViewById(R.id.editTextFilename);
mButtonImport = findViewById(R.id.buttonImport); mButtonImport = findViewById(R.id.buttonImport);
mButtonImport.setEnabled(false); mButtonImport.setEnabled(false);
mButtonImport.setOnClickListener(view -> { mButtonImport.setOnClickListener(view -> importProfile());
String filename = mTextFilename.getText().toString();
if (isValidProfileFilename(filename)) {
writeProfile(filename, mProfileData);
} else {
displayMessage("ERROR: File extention must be \".brf\"\n");
}
});
mTextProfile = findViewById(R.id.editTextProfile); mTextProfile = findViewById(R.id.editTextProfile);
Intent intent = getIntent(); Intent intent = getIntent();
String action = intent.getAction(); String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) { if (Intent.ACTION_VIEW.equals(action)) {
importProfile(intent); parseIntent(intent);
} }
} }
private boolean isValidProfileFilename(String filename) { private boolean isBuiltinProfile(String filename) {
return filename.endsWith(".brf"); String[] builtinProfiles = new ServerConfig(this).getProfiles();
for (String builtinProfile : builtinProfiles) {
if (filename.equals(builtinProfile)) {
return true;
}
}
return false;
} }
private void importProfile(Intent intent) { private boolean isInvalidProfileFilename(String filename) {
return !filename.endsWith(".brf");
}
private void parseIntent(Intent intent) {
if (intent.getData() == null) { if (intent.getData() == null) {
return; return;
} }
@ -78,7 +81,7 @@ public class BImportActivity extends Activity {
} }
} }
// is the file extention ".brf" in the file name // is the file extention ".brf" in the file name
if (filename == null || !isValidProfileFilename(filename)) { if (filename == null || isInvalidProfileFilename(filename)) {
resultMessage.append("ERROR: File extention must be \".brf\"\n"); resultMessage.append("ERROR: File extention must be \".brf\"\n");
displayMessage(resultMessage.toString()); displayMessage(resultMessage.toString());
return; return;
@ -105,7 +108,7 @@ public class BImportActivity extends Activity {
} }
mProfileData = sb.toString(); mProfileData = sb.toString();
} catch (IOException e) { } catch (IOException e) {
resultMessage.append(String.format("ERROR: failed to load profile content (%S)", e.getMessage())); resultMessage.append(String.format("ERROR: failed to load profile content (%s)", e.getMessage()));
displayMessage(resultMessage.toString()); displayMessage(resultMessage.toString());
} }
@ -124,6 +127,20 @@ public class BImportActivity extends Activity {
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); Toast.makeText(this, message, Toast.LENGTH_LONG).show();
} }
boolean importProfile() {
String filename = mTextFilename.getText().toString();
if (isInvalidProfileFilename(filename)) {
displayMessage("ERROR: File extention must be \".brf\"\n");
return false;
} else if (isBuiltinProfile(filename)) {
displayMessage("ERROR: Built-in profile exists\n");
return false;
}
writeProfile(filename, mProfileData);
return true;
}
void writeProfile(String filename, String profileData) { void writeProfile(String filename, String profileData) {
File baseDir = ConfigHelper.getBaseDir(this); File baseDir = ConfigHelper.getBaseDir(this);