/*
* slcl-android, an Android frontend for slcl
* Copyright (C) 2023-2024 Xavier Del Campo Romero
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.slcl;
import org.slcl.core.Cookie;
import org.slcl.core.Login;
import org.slcl.Directory;
import org.slcl.InternalFile;
import java.net.URLEncoder;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
public final class LoginActivity extends Activity {
private class LoginParams {
public LoginParams(final boolean https, String url,
final String username, final String password)
{
this.https = https;
this.username = username;
this.url = url;
this.password = password;
}
final boolean https;
final String url, username, password;
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText url = (EditText)findViewById(R.id.url);
final EditText username = (EditText)findViewById(R.id.username);
final EditText password = (EditText)findViewById(R.id.password);
final CheckBox https = (CheckBox)findViewById(R.id.https);
url.setText("192.168.1.180:50683");
username.setText("a");
password.setText("a");
https.setChecked(false);
try {
final String urlstr = url.getText().toString();
final String encusername = URLEncoder.encode(
username.getText().toString(), "UTF-8");
final String encpassword = URLEncoder.encode(
password.getText().toString(), "UTF-8");
final LoginTask t = new LoginTask();
LoginParams p = new LoginParams(https.isChecked(),
urlstr, encusername, encpassword);
t.execute(p);
}
catch (final IOException e) {
//error.setText("Exception: " + e.getMessage());
}
}
});
}
// https://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3
private final class LoginTask
extends AsyncTask
{
ProgressDialog dialog;
@Override
protected void onPreExecute()
{
dialog = dialog.show(LoginActivity.this, "ProgressDialog",
"Logging into server");
}
@Override
protected String doInBackground(final LoginParams... params)
{
try {
final LoginParams p = params[0];
final Login l = new Login();
final String cookie = l.login(p.https, p.url, p.username,
p.password);
final InternalFile f = new InternalFile(
getApplicationContext(), "login");
final Cookie c = new Cookie(p.username, cookie);
c.store(f.getFile());
return "";
} catch (final IOException e) {
System.out.println(e.getMessage());
return e.getMessage();
}
}
@Override
protected void onProgressUpdate(final String... text)
{
}
@Override
protected void onPostExecute(final String result)
{
Intent intent = new Intent(LoginActivity.this, Directory.class);
dialog.dismiss();
startActivity(intent);
// TODO: review errors.
//error.setText("Exception: " + e.getMessage());
}
}
}