Skip to content

Commit

Permalink
Write server list information in account user data
Browse files Browse the repository at this point in the history
Related to #1294 and one step forward towards #65

Signed-off-by: Daniele Ricci <[email protected]>
  • Loading branch information
daniele-athome committed Apr 13, 2020
1 parent 84d882d commit 27dbb1c
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 42 deletions.
12 changes: 12 additions & 0 deletions app/src/main/java/org/kontalk/Kontalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@
import org.kontalk.authenticator.Authenticator;
import org.kontalk.authenticator.MyAccount;
import org.kontalk.client.EndpointServer;
import org.kontalk.client.ServerList;
import org.kontalk.crypto.PGP;
import org.kontalk.crypto.PersonalKey;
import org.kontalk.data.Contact;
import org.kontalk.provider.MessagesProviderClient;
import org.kontalk.reporting.ReportingManager;
import org.kontalk.service.DownloadService;
import org.kontalk.service.NetworkStateReceiver;
import org.kontalk.service.ServerListUpdater;
import org.kontalk.service.SystemBootStartup;
import org.kontalk.service.UploadService;
import org.kontalk.service.msgcenter.IPushService;
Expand Down Expand Up @@ -241,10 +243,20 @@ public void onAccountsUpdated(Account[] accounts) {

// TODO remove after a few release iterations
if (Authenticator.getDefaultServiceTermsURL(this) == null) {
// default service terms url
am.setUserData(account.getSystemAccount(),
Authenticator.DATA_SERVICE_TERMS_URL,
getString(R.string.help_default_KPN_service_terms_url));
}

// TODO remove after a few release iterations
if (Authenticator.getDefaultServerList(this) == null) {
// default server list
ServerList list = ServerListUpdater.getCurrentList(this);
am.setUserData(account.getSystemAccount(),
Authenticator.DATA_SERVER_LIST,
SystemUtils.serializeProperties(list.toProperties()));
}
}
else {
// ensure everything is cleared up
Expand Down
32 changes: 29 additions & 3 deletions app/src/main/java/org/kontalk/authenticator/Authenticator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.bouncycastle.openpgp.PGPException;
Expand All @@ -50,12 +51,14 @@
import org.kontalk.BuildConfig;
import org.kontalk.R;
import org.kontalk.client.EndpointServer;
import org.kontalk.client.ServerList;
import org.kontalk.crypto.PGP;
import org.kontalk.crypto.PersonalKey;
import org.kontalk.crypto.PersonalKeyExporter;
import org.kontalk.provider.Keyring;
import org.kontalk.ui.MainActivity;
import org.kontalk.ui.NumberValidation;
import org.kontalk.util.SystemUtils;


/**
Expand All @@ -72,6 +75,7 @@ public class Authenticator extends AbstractAccountAuthenticator {
public static final String DATA_NAME = "org.kontalk.key.name";
public static final String DATA_USER_PASSPHRASE = "org.kontalk.userPassphrase";
public static final String DATA_SERVER_URI = "org.kontalk.server";
public static final String DATA_SERVER_LIST = "org.kontalk.serverList";
public static final String DATA_SERVICE_TERMS_URL = "org.kontalk.serviceTermsURL";

@SuppressWarnings("WeakerAccess")
Expand Down Expand Up @@ -118,12 +122,34 @@ static EndpointServer getServer(AccountManager am, Account account) {
public static String getDefaultServiceTermsURL(Context context) {
AccountManager am = AccountManager.get(context);
Account account = getDefaultSystemAccount(am);
return getServiceTermsURL(am, account);
return account != null ? getServiceTermsURL(am, account) : null;
}

static String getServiceTermsURL(AccountManager am, Account account) {
return account != null ?
am.getUserData(account, Authenticator.DATA_SERVICE_TERMS_URL) : null;
return am.getUserData(account, DATA_SERVICE_TERMS_URL);
}

/** @deprecated Still used in {@link org.kontalk.Kontalk#onCreate()}. */
@Deprecated
public static ServerList getDefaultServerList(Context context) {
AccountManager am = AccountManager.get(context);
Account account = getDefaultSystemAccount(am);
return account != null ? getServerList(am, account) : null;
}

static ServerList getServerList(AccountManager am, Account account) {
String serverListData = am.getUserData(account, DATA_SERVER_LIST);
if (serverListData != null) {
Properties props = SystemUtils.unserializeProperties(serverListData);
try {
return ServerList.fromProperties(props);
}
catch (IOException e) {
// this isn't right...
return null;
}
}
return null;
}

static PersonalKey loadPersonalKey(AccountManager am, Account account)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/org/kontalk/authenticator/MyAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import android.accounts.Account
import android.accounts.AccountManager
import org.jxmpp.jid.BareJid
import org.kontalk.client.EndpointServer
import org.kontalk.client.ServerList
import org.kontalk.crypto.PersonalKey
import org.kontalk.util.XMPPUtils

Expand All @@ -37,6 +38,10 @@ class MyAccount (val systemAccount: Account, private val accountManager: Account
Authenticator.getServer(accountManager, systemAccount)
}

val serverList: ServerList by lazy {
Authenticator.getServerList(accountManager, systemAccount)
}

val serviceTermsURL: String? by lazy {
Authenticator.getServiceTermsURL(accountManager, systemAccount)
}
Expand All @@ -59,6 +64,9 @@ class MyAccount (val systemAccount: Account, private val accountManager: Account

fun isSelfJID(bareJid: BareJid): Boolean = bareJid.equals(selfJID)

// TODO
fun isNetworkJID(bareJid: BareJid): Boolean = true

/* compatibility interface for android.accounts.Account */

fun getName(): String {
Expand Down
38 changes: 27 additions & 11 deletions app/src/main/java/org/kontalk/client/EndpointServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.kontalk.client;

import java.util.Collections;
import java.util.Date;
import java.util.regex.Pattern;


Expand Down Expand Up @@ -110,6 +112,11 @@ public String getNetwork() {
* Interface for providing a server.
*/
public interface EndpointServerProvider {
/**
* Returns a server list.
*/
ServerList list();

/**
* Returns the next server that hasn't been picked yet.
*/
Expand Down Expand Up @@ -137,24 +144,33 @@ public SingleServerProvider(EndpointServer server) {
mProvided = server;
}

@Override
public ServerList list() {
return new ServerList(new Date(), Collections.singletonList(getProvidedServer()));
}

private EndpointServer getProvidedServer() {
if (mProvided == null) {
try {
return new EndpointServer(mUri);
}
catch (Exception e) {
// custom is not valid
return null;
}
}

return mProvided;
}

@Override
public EndpointServer next() {
if (mCalled) {
return null;
}
else {
mCalled = true;
if (mProvided == null) {
try {
return new EndpointServer(mUri);
}
catch (Exception e) {
// custom is not valid
return null;
}
}

return mProvided;
return getProvidedServer();
}
}

Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/org/kontalk/client/ServerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

package org.kontalk.client;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.Random;


Expand All @@ -32,6 +37,8 @@
*/
public class ServerList extends ArrayList<EndpointServer> {
private static final long serialVersionUID = 1L;
private static DateFormat sTimestampFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);

private final Date mDate;

Expand All @@ -57,6 +64,36 @@ public EndpointServer random() {
get(mSeed.nextInt(size())) : null;
}

public Properties toProperties() {
Properties prop = new Properties();
Date now = new Date();
prop.setProperty("timestamp", sTimestampFormat.format(now));

for (int i = 0; i < size(); i++) {
String item = get(i).toString();
prop.setProperty("server" + (i + 1), item);
}
return prop;
}

public static ServerList fromProperties(Properties props) throws IOException {
try {
Date date = sTimestampFormat.parse(props.getProperty("timestamp"));
ServerList list = new ServerList(date);
int i = 1;
String server;
while ((server = props.getProperty("server" + i)) != null) {
list.add(new EndpointServer(server));
i++;
}

return list;
}
catch (Exception e) {
throw new IOException("parse error", e);
}
}

/** A simple server provider backed by a server list. */
public static class ServerListProvider implements EndpointServer.EndpointServerProvider {
private ServerList mList;
Expand All @@ -67,6 +104,14 @@ public ServerListProvider(ServerList list) {
mUsed = new LinkedList<>();
}

@Override
public ServerList list() {
ServerList list = new ServerList(mList.getDate());
list.addAll(mList);
list.addAll(mUsed);
return list;
}

@Override
public EndpointServer next() {
if (mList.size() > 0) {
Expand Down
29 changes: 4 additions & 25 deletions app/src/main/java/org/kontalk/service/ServerListUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

import org.greenrobot.eventbus.EventBus;
Expand Down Expand Up @@ -58,14 +55,12 @@
*
* @author Daniele Ricci
*/
// TODO downloaded list should be saved in account user data
public class ServerListUpdater {
private static final String TAG = ServerListUpdater.class.getSimpleName();

private static ServerList sCurrentList;

private static DateFormat sTimestampFormat =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.US);

private final Context mContext;
private UpdaterListener mListener;

Expand Down Expand Up @@ -137,17 +132,16 @@ public void onServerList(ServerListEvent event) {
unregisterReceiver();

if (event.servers != null && event.servers.length > 0) {
Properties prop = new Properties();
Date now = new Date();
ServerList list = new ServerList(now);
prop.setProperty("timestamp", sTimestampFormat.format(now));

for (int i = 0; i < event.servers.length; i++) {
String item = event.servers[i];
prop.setProperty("server" + (i + 1), item);
list.add(new EndpointServer(item));
}

Properties prop = list.toProperties();

OutputStream out = null;
try {
out = new FileOutputStream(getCachedListFile(mContext));
Expand Down Expand Up @@ -196,22 +190,7 @@ private static File getCachedListFile(Context context) {
private static ServerList parseList(InputStream in) throws IOException {
Properties prop = new Properties();
prop.load(in);

try {
Date date = sTimestampFormat.parse(prop.getProperty("timestamp"));
ServerList list = new ServerList(date);
int i = 1;
String server;
while ((server = prop.getProperty("server" + i)) != null) {
list.add(new EndpointServer(server));
i++;
}

return list;
}
catch (Exception e) {
throw new IOException("parse error", e);
}
return ServerList.fromProperties(prop);
}

private static ServerList parseBuiltinList(Context context) throws IOException {
Expand Down
Loading

0 comments on commit 27dbb1c

Please sign in to comment.