Skip to content

Commit

Permalink
bug fix, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearfog committed Oct 26, 2023
1 parent 410ed0d commit 34adfca
Show file tree
Hide file tree
Showing 21 changed files with 367 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import org.nuclearfog.twidda.backend.api.Connection;
import org.nuclearfog.twidda.backend.api.ConnectionException;
import org.nuclearfog.twidda.backend.api.ConnectionManager;
import org.nuclearfog.twidda.model.lists.Domains;

/**
* background executor to load/block domains
* background executor to block/unblock domains
*
* @author nuclearfog
*/
Expand All @@ -31,23 +30,19 @@ public DomainAction(Context context) {
protected Result doInBackground(@NonNull Param param) {
try {
switch (param.mode) {
case Param.MODE_LOAD:
Domains result = connection.getDomainBlocks(param.cursor);
return new Result(Result.MODE_LOAD, param.index, result, param.domain, null);

case Param.MODE_BLOCK:
connection.blockDomain(param.domain);
return new Result(Result.MODE_BLOCK, param.index, null, param.domain, null);
return new Result(Result.MODE_BLOCK, param.domain, null);

case Param.MODE_UNBLOCK:
connection.unblockDomain(param.domain);
return new Result(Result.MODE_UNBLOCK, param.index, null, param.domain, null);
return new Result(Result.MODE_UNBLOCK, param.domain, null);

default:
return null;
}
} catch (ConnectionException exception) {
return new Result(Result.ERROR, param.index, null, param.domain, exception);
return new Result(Result.ERROR, param.domain, exception);
}
}

Expand All @@ -56,22 +51,15 @@ protected Result doInBackground(@NonNull Param param) {
*/
public static class Param {

public static final int MODE_LOAD = 1;
public static final int MODE_BLOCK = 2;
public static final int MODE_UNBLOCK = 3;

public static final long NO_CURSOR = 0L;

final String domain;
final long cursor;
final int mode;
final int index;

public Param(int mode, int index, long cursor, String domain) {
public Param(int mode, String domain) {
this.mode = mode;
this.cursor = cursor;
this.domain = domain;
this.index = index;
}
}

Expand All @@ -81,25 +69,19 @@ public Param(int mode, int index, long cursor, String domain) {
public static class Result {

public static final int ERROR = -1;
public static final int MODE_LOAD = 4;
public static final int MODE_BLOCK = 5;
public static final int MODE_UNBLOCK = 6;

public final int mode;
public final int index;
@Nullable
public final Domains domains;
@Nullable
public final ConnectionException exception;
@Nullable
public final String domain;

Result(int mode, int index, @Nullable Domains domains, @Nullable String domain, @Nullable ConnectionException exception) {
this.domains = domains;
Result(int mode, @Nullable String domain, @Nullable ConnectionException exception) {
this.domain = domain;
this.exception = exception;
this.mode = mode;
this.index = index;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.nuclearfog.twidda.backend.async;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import org.nuclearfog.twidda.backend.api.Connection;
import org.nuclearfog.twidda.backend.api.ConnectionException;
import org.nuclearfog.twidda.backend.api.ConnectionManager;
import org.nuclearfog.twidda.model.lists.Domains;

/**
* Async loader class to load blocked domains
*
* @author nuclearfog
*/
public class DomainLoader extends AsyncExecutor<DomainLoader.Param, DomainLoader.Result> {

private Connection connection;

/**
*
*/
public DomainLoader(Context context) {
connection = ConnectionManager.getDefaultConnection(context);
}


@Override
protected Result doInBackground(@NonNull Param param) {
try {
Domains domains = connection.getDomainBlocks(param.cursor);
return new Result(domains, param.index, null);
} catch (ConnectionException exception) {
return new Result(null, param.index, exception);
}
}

/**
*
*/
public static class Param {

public static final long NO_CURSOR = 0L;

final long cursor;
final int index;

public Param(long cursor, int index) {
this.cursor = cursor;
this.index = index;
}
}

/**
*
*/
public static class Result {

public final int index;
@Nullable
public final Domains domains;
@Nullable
public final ConnectionException exception;

Result(@Nullable Domains domains, int index, @Nullable ConnectionException exception) {
this.domains = domains;
this.index = index;
this.exception = exception;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.nuclearfog.twidda.backend.api.ConnectionManager;
import org.nuclearfog.twidda.backend.helper.ConnectionResult;
import org.nuclearfog.twidda.backend.helper.update.ConnectionUpdate;
import org.nuclearfog.twidda.backend.helper.update.PushUpdate;
import org.nuclearfog.twidda.config.Configuration;
import org.nuclearfog.twidda.config.GlobalSettings;
import org.nuclearfog.twidda.database.AppDatabase;
Expand Down Expand Up @@ -66,14 +65,9 @@ protected Result doInBackground(@NonNull Param param) {
database.saveLogin(account);
// save instance information
database.saveInstance(instance);
// transfer push configuration
if (settings.pushEnabled()) {
try {
connection.updatePush(new PushUpdate(settings.getWebPush()));
} catch (ConnectionException exception) {
settings.setPushEnabled(false);
}
}
// disable push for new login
settings.setPushEnabled(false);
settings.setWebPush(null);
return new Result(Result.MODE_LOGIN, account, null, null);

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ private ErrorUtils() {
*
* @param exception connection exception
*/
public static void showErrorMessage(Context context, @Nullable ConnectionException exception) {
if (context != null) {
String errorMessage = getErrorMessage(context, exception);
if (!errorMessage.isEmpty()) {
Toast.makeText(context.getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
}
public static void showErrorMessage(@NonNull Context context, @Nullable ConnectionException exception) {
String errorMessage = getErrorMessage(context, exception);
if (!errorMessage.isEmpty()) {
Toast.makeText(context.getApplicationContext(), errorMessage, Toast.LENGTH_SHORT).show();
}
}

Expand Down
68 changes: 34 additions & 34 deletions app/src/main/java/org/nuclearfog/twidda/config/GlobalSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -581,25 +581,38 @@ public WebPush getWebPush() {
/**
* save web push configuration
*
* @param webPush web push information
*/
public void setWebPush(WebPush webPush) {
Editor edit = settings.edit();
edit.putLong(PUSH_ID, webPush.getId());
edit.putString(PUSH_SERVER_KEY, webPush.getServerKey());
edit.putString(PUSH_SERVER_HOST, webPush.getHost());
edit.putString(PUSH_PUBLIC_KEY, webPush.getPublicKey());
edit.putString(PUSH_PRIVATE_KEY, webPush.getPrivateKey());
edit.putString(PUSH_AUTH_KEY, webPush.getAuthSecret());
edit.putBoolean(PUSH_ALERT_MENTION, webPush.alertMentionEnabled());
edit.putBoolean(PUSH_ALERT_REPOST, webPush.alertRepostEnabled());
edit.putBoolean(PUSH_ALERT_FAVORITE, webPush.alertFavoriteEnabled());
edit.putBoolean(PUSH_ALERT_FOLLOWING, webPush.alertFollowingEnabled());
edit.putBoolean(PUSH_ALERT_REQUEST_FOLLOW, webPush.alertFollowRequestEnabled());
edit.putBoolean(PUSH_ALERT_STATUS_POST, webPush.alertNewStatusEnabled());
edit.putBoolean(PUSH_ALERT_STATUS_EDIT, webPush.alertStatusChangeEnabled());
edit.putBoolean(PUSH_ALERT_POLL, webPush.alertPollEnabled());
edit.apply();
* @param webPush web push information or null to remove existing configuration
*/
public void setWebPush(@Nullable WebPush webPush) {
if (webPush != null) {
this.webPush = new ConfigPush(webPush);
Editor edit = settings.edit();
edit.putLong(PUSH_ID, webPush.getId());
edit.putString(PUSH_SERVER_KEY, webPush.getServerKey());
edit.putString(PUSH_SERVER_HOST, webPush.getHost());
edit.putString(PUSH_PUBLIC_KEY, webPush.getPublicKey());
edit.putString(PUSH_PRIVATE_KEY, webPush.getPrivateKey());
edit.putString(PUSH_AUTH_KEY, webPush.getAuthSecret());
edit.putBoolean(PUSH_ALERT_MENTION, webPush.alertMentionEnabled());
edit.putBoolean(PUSH_ALERT_REPOST, webPush.alertRepostEnabled());
edit.putBoolean(PUSH_ALERT_FAVORITE, webPush.alertFavoriteEnabled());
edit.putBoolean(PUSH_ALERT_FOLLOWING, webPush.alertFollowingEnabled());
edit.putBoolean(PUSH_ALERT_REQUEST_FOLLOW, webPush.alertFollowRequestEnabled());
edit.putBoolean(PUSH_ALERT_STATUS_POST, webPush.alertNewStatusEnabled());
edit.putBoolean(PUSH_ALERT_STATUS_EDIT, webPush.alertStatusChangeEnabled());
edit.putBoolean(PUSH_ALERT_POLL, webPush.alertPollEnabled());
edit.apply();
} else {
this.webPush.clear();
Editor edit = settings.edit();
edit.remove(PUSH_ID);
edit.remove(PUSH_SERVER_KEY);
edit.remove(PUSH_SERVER_HOST);
edit.remove(PUSH_PUBLIC_KEY);
edit.remove(PUSH_PRIVATE_KEY);
edit.remove(PUSH_AUTH_KEY);
edit.apply();
}
}

/**
Expand Down Expand Up @@ -1028,15 +1041,7 @@ private void initialize() {
proxyPort = settings.getString(PROXY_PORT, "");
proxyUser = settings.getString(PROXY_USER, "");
proxyPass = settings.getString(PROXY_PASS, "");
// login informations
initLogin();
initWebpush();
}

/**
* initialize login information
*/
private void initLogin() {
// init login information
String oauthToken = settings.getString(OAUTH_TOKEN, "");
String oauthSecret = settings.getString(OAUTH_SECRET, "");
String consumerToken = settings.getString(CONSUMER_TOKEN, "");
Expand All @@ -1046,12 +1051,7 @@ private void initLogin() {
int apiId = settings.getInt(CURRENT_API, 0);
long userId = settings.getLong(CURRENT_ID, 0L);
login = new ConfigAccount(userId, oauthToken, oauthSecret, consumerToken, consumerSecret, bearerToken, hostname, apiId);
}

/**
*
*/
private void initWebpush() {
// init web push information
long pushID = settings.getLong(PUSH_ID, 0L);
String pushServerKey = settings.getString(PUSH_SERVER_KEY, "");
String pushServerHost = settings.getString(PUSH_SERVER_HOST, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,16 @@ public boolean equals(@Nullable Object obj) {
WebPush push = (WebPush) obj;
return getId() == push.getId() && getHost().equals(push.getHost());
}

/**
* clear user related information
*/
public void clear() {
id = 0L;
host = "";
serverKey = "";
publicKey = "";
privateKey = "";
authKey = "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import org.nuclearfog.twidda.database.DatabaseAdapter.MediaTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.NotificationTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.PollTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.PushTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.StatusRegisterTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.StatusTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.UserRegisterTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.UserTable;
import org.nuclearfog.twidda.database.DatabaseAdapter.PushTable;
import org.nuclearfog.twidda.database.impl.DatabaseAccount;
import org.nuclearfog.twidda.database.impl.DatabaseEmoji;
import org.nuclearfog.twidda.database.impl.DatabaseHashtag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ else if (type == ConfirmDialog.PROFILE_MUTE) {
// confirmed domain block
else if (type == ConfirmDialog.DOMAIN_BLOCK_ADD) {
String url = Uri.parse(user.getProfileUrl()).getHost();
DomainAction.Param param = new DomainAction.Param(DomainAction.Param.MODE_BLOCK, 0, DomainAction.Param.NO_CURSOR, url);
DomainAction.Param param = new DomainAction.Param(DomainAction.Param.MODE_BLOCK, url);
domainAction.execute(param, domainCallback);
}
}
Expand Down
Loading

0 comments on commit 34adfca

Please sign in to comment.