#916 Change remote account settings - Server connection

This commit is contained in:
Stefan Niedermann 2021-03-15 23:37:29 +01:00
parent a447542fc1
commit 669d457f5c
4 changed files with 122 additions and 8 deletions

View file

@ -13,8 +13,10 @@ import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
@ -27,8 +29,11 @@ import it.niedermann.owncloud.notes.LockedActivity;
import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder;
import it.niedermann.owncloud.notes.databinding.ActivityManageAccountsBinding;
import it.niedermann.owncloud.notes.shared.model.LocalAccount;
import it.niedermann.owncloud.notes.exception.ExceptionDialogFragment;
import it.niedermann.owncloud.notes.persistence.NotesClient;
import it.niedermann.owncloud.notes.persistence.NotesDatabase;
import it.niedermann.owncloud.notes.shared.model.LocalAccount;
import it.niedermann.owncloud.notes.shared.model.ServerSettings;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
@ -90,34 +95,71 @@ public class ManageAccountsActivity extends LockedActivity {
}
private void onChangeNotesPath(@NonNull LocalAccount localAccount) {
final NotesClient client = NotesClient.newInstance(localAccount.getPreferredApiVersion(), getApplicationContext());
final EditText editText = new EditText(this);
editText.setEnabled(false);
final View wrapper = createDialogViewWrapper(editText);
new BrandedAlertDialogBuilder(this)
final AlertDialog dialog = new BrandedAlertDialogBuilder(this)
.setTitle(R.string.settings_notes_path)
.setMessage("Folder to store your notes in your Nextcloud")
.setView(wrapper)
.setNeutralButton(android.R.string.cancel, null)
.setPositiveButton(R.string.action_edit_save, (v, d) -> {
Toast.makeText(this, "Submitted " + editText.getText(), Toast.LENGTH_LONG).show();
})
.setPositiveButton(R.string.action_edit_save, (v, d) -> new Thread(() -> {
try {
final ServerSettings newSettings = client.putServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), new ServerSettings(editText.getText().toString(), null));
Toast.makeText(this, "New notes path: " + newSettings.getNotesPath(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
}).start())
.show();
new Thread(() -> {
try {
final ServerSettings oldSettings = client.getServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()));
editText.setText(oldSettings.getNotesPath());
editText.setEnabled(true);
} catch (Exception e) {
dialog.dismiss();
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
}).start();
}
private void onChangeFileSuffix(@NonNull LocalAccount localAccount) {
final NotesClient client = NotesClient.newInstance(localAccount.getPreferredApiVersion(), getApplicationContext());
final Spinner spinner = new Spinner(this);
spinner.setEnabled(false);
final View wrapper = createDialogViewWrapper(spinner);
final ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.settings_file_suffixes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
new BrandedAlertDialogBuilder(this)
final AlertDialog dialog = new BrandedAlertDialogBuilder(this)
.setTitle(R.string.settings_file_suffix)
.setMessage("File extension for new notes in your Nextcloud")
.setView(wrapper)
.setNeutralButton(android.R.string.cancel, null)
.setPositiveButton("Save", (v, d) -> {
Toast.makeText(this, "Submitted " + spinner.getSelectedItem(), Toast.LENGTH_LONG).show();
new Thread(() -> {
try {
final ServerSettings newSettings = client.putServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), new ServerSettings(null, spinner.getSelectedItem().toString()));
Toast.makeText(this, "New file suffix: " + newSettings.getNotesPath(), Toast.LENGTH_LONG).show();
} catch (Exception e) {
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
}).start();
})
.show();
new Thread(() -> {
try {
final ServerSettings oldSettings = client.getServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()));
// TODO
// spinner.setSelection(adapteroldSettings.getNotesPath());
spinner.setEnabled(true);
} catch (Exception e) {
dialog.dismiss();
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
}).start();
}
@NonNull

View file

@ -29,6 +29,7 @@ import it.niedermann.owncloud.notes.shared.model.ApiVersion;
import it.niedermann.owncloud.notes.shared.model.CloudNote;
import it.niedermann.owncloud.notes.shared.model.ServerResponse.NoteResponse;
import it.niedermann.owncloud.notes.shared.model.ServerResponse.NotesResponse;
import it.niedermann.owncloud.notes.shared.model.ServerSettings;
@SuppressWarnings("WeakerAccess")
@WorkerThread
@ -61,6 +62,8 @@ public abstract class NotesClient {
public static final String JSON_CATEGORY = "category";
public static final String JSON_ETAG = "etag";
public static final String JSON_MODIFIED = "modified";
public static final String JSON_SETTINGS_NOTES_PATH = "notesPath";
public static final String JSON_SETTINGS_FILE_SUFFIX = "fileSuffix";
public static final ApiVersion[] SUPPORTED_API_VERSIONS = new ApiVersion[]{
new ApiVersion(1, 0),
@ -96,6 +99,14 @@ public abstract class NotesClient {
abstract void deleteNote(SingleSignOnAccount ssoAccount, long noteId) throws Exception;
public ServerSettings getServerSettings(SingleSignOnAccount ssoAccount) throws Exception {
throw new UnsupportedOperationException("Not available in this API version");
}
public ServerSettings putServerSettings(SingleSignOnAccount ssoAccount, @NonNull ServerSettings settings) throws Exception {
throw new UnsupportedOperationException("Not available in this API version");
}
/**
* This entity class is used to return relevant data of the HTTP reponse.
*/

View file

@ -15,6 +15,7 @@ import java.util.Map;
import it.niedermann.owncloud.notes.shared.model.CloudNote;
import it.niedermann.owncloud.notes.shared.model.ServerResponse.NoteResponse;
import it.niedermann.owncloud.notes.shared.model.ServerResponse.NotesResponse;
import it.niedermann.owncloud.notes.shared.model.ServerSettings;
@WorkerThread
public class NotesClientV1 extends NotesClient {
@ -32,7 +33,7 @@ public class NotesClientV1 extends NotesClient {
}
private NoteResponse putNote(SingleSignOnAccount ssoAccount, CloudNote note, String path, String method) throws Exception {
JSONObject paramObject = new JSONObject();
final JSONObject paramObject = new JSONObject();
paramObject.accumulate(JSON_TITLE, note.getTitle());
paramObject.accumulate(JSON_CONTENT, note.getContent());
paramObject.accumulate(JSON_MODIFIED, note.getModified().getTimeInMillis() / 1000);
@ -57,4 +58,17 @@ public class NotesClientV1 extends NotesClient {
protected String getApiPath() {
return API_PATH;
}
@Override
public ServerSettings getServerSettings(SingleSignOnAccount ssoAccount) throws Exception {
return ServerSettings.from(new JSONObject(this.requestServer(ssoAccount, "settings", METHOD_GET, null, null, null).getContent()));
}
@Override
public ServerSettings putServerSettings(SingleSignOnAccount ssoAccount, @NonNull ServerSettings settings) throws Exception {
final JSONObject paramObject = new JSONObject();
paramObject.accumulate(JSON_SETTINGS_NOTES_PATH, settings.getNotesPath());
paramObject.accumulate(JSON_SETTINGS_FILE_SUFFIX, settings.getFileSuffix());
return ServerSettings.from(new JSONObject(this.requestServer(ssoAccount, "settings", METHOD_PUT, null, paramObject, null).getContent()));
}
}

View file

@ -0,0 +1,47 @@
package it.niedermann.owncloud.notes.shared.model;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.Serializable;
import static it.niedermann.owncloud.notes.persistence.NotesClient.JSON_SETTINGS_FILE_SUFFIX;
import static it.niedermann.owncloud.notes.persistence.NotesClient.JSON_SETTINGS_NOTES_PATH;
public class ServerSettings implements Serializable {
private String notesPath = "";
private String fileSuffix = "";
public ServerSettings(String notesPath, String fileSuffix) {
setNotesPath(notesPath);
setFileSuffix(fileSuffix);
}
public static ServerSettings from(JSONObject settings) throws JSONException {
String notesPath = "";
if (settings.has(JSON_SETTINGS_NOTES_PATH)) {
notesPath = settings.getString(JSON_SETTINGS_NOTES_PATH);
}
String fileSuffix = "";
if (settings.has(JSON_SETTINGS_FILE_SUFFIX)) {
fileSuffix = settings.getString(JSON_SETTINGS_FILE_SUFFIX);
}
return new ServerSettings(notesPath, fileSuffix);
}
public String getNotesPath() {
return notesPath;
}
public void setNotesPath(String notesPath) {
this.notesPath = notesPath;
}
public String getFileSuffix() {
return fileSuffix;
}
public void setFileSuffix(String fileSuffix) {
this.fileSuffix = fileSuffix;
}
}