Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Tidy codebase #1784

Merged
merged 15 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions vector/src/main/java/im/vector/ErrorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import im.vector.activity.CommonActivityUtils;
import im.vector.store.LoginStorage;

/**
* Listen to error threw by the Matrix client when querying the API
*/
public class ErrorListener implements ApiFailureCallback {
private static final String LOG_TAG = ErrorListener.class.getSimpleName();

Expand All @@ -48,57 +51,23 @@ public ErrorListener(MXSession session, Activity activity) {
public void onNetworkError(final Exception e) {
Log.e(LOG_TAG, "Network error: " + e.getMessage());

// do not trigger toaster if the application is in background
// Do not trigger toaster if the application is in background
if (!VectorApp.isAppInBackground()) {
UnrecognizedCertificateException unrecCertEx = CertUtil.getCertificateException(e);
if (unrecCertEx == null) {
handleNetworkError(e);
return;
}
else {
handleCertError(unrecCertEx, e);
}

final Fingerprint fingerprint = unrecCertEx.getFingerprint();
Log.d(LOG_TAG, "Found fingerprint: SHA-256: " + fingerprint.getBytesAsHexString());

mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
UnrecognizedCertHandler.show(mSession.getHomeServerConfig(), fingerprint, true, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
LoginStorage loginStorage = Matrix.getInstance(mActivity.getApplicationContext()).getLoginStorage();
loginStorage.replaceCredentials(mSession.getHomeServerConfig());
}

@Override
public void onIgnore() {
handleNetworkError(e);
}

@Override
public void onReject() {
Log.d(LOG_TAG, "Found fingerprint: reject fingerprint");
CommonActivityUtils.logout(mActivity, Arrays.asList(mSession), true, null);
}
});
}
});
}
}

private void handleNetworkError(Exception e) {
if (!VectorApp.isAppInBackground()) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, mActivity.getString(R.string.network_error), Toast.LENGTH_SHORT).show();
}
});
}
}

@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "Matrix error: " + e.errcode + " - " + e.error);

// The access token was not recognized: log out
if (MatrixError.UNKNOWN_TOKEN.equals(e.errcode)) {
CommonActivityUtils.logout(mActivity);
Expand All @@ -109,4 +78,41 @@ public void onMatrixError(MatrixError e) {
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "Unexpected error: " + e.getMessage());
}

private void handleNetworkError(Exception e) {
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(mActivity, mActivity.getString(R.string.network_error), Toast.LENGTH_SHORT).show();
}
});
}

private void handleCertError(UnrecognizedCertificateException unrecCertEx, final Exception e) {
final Fingerprint fingerprint = unrecCertEx.getFingerprint();
Log.d(LOG_TAG, "Found fingerprint: SHA-256: " + fingerprint.getBytesAsHexString());

mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
UnrecognizedCertHandler.show(mSession.getHomeServerConfig(), fingerprint, true, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
LoginStorage loginStorage = Matrix.getInstance(mActivity.getApplicationContext()).getLoginStorage();
loginStorage.replaceCredentials(mSession.getHomeServerConfig());
}

@Override
public void onIgnore() {
handleNetworkError(e);
}

@Override
public void onReject() {
CommonActivityUtils.logout(mActivity, Arrays.asList(mSession), true, null);
}
});
}
});
}
}
7 changes: 5 additions & 2 deletions vector/src/main/java/im/vector/EventEmitter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package im.vector;


import android.os.Handler;
import android.os.Looper;

Expand All @@ -9,6 +8,10 @@
import java.util.HashSet;
import java.util.Set;

/**
* Generic callback management class, runs callbacks in the UI loop
* @param <T>
*/
public class EventEmitter<T> {
private static final String LOG_TAG = EventEmitter.class.getSimpleName();

Expand All @@ -31,7 +34,7 @@ public void unregister(Listener<T> cb) {
/**
* Fires all registered callbacks on the UI thread.
*
* @param t
* @param t passed to the callback
*/
public void fire(final T t) {
final Set<Listener<T>> callbacks = new HashSet<>(mCallbacks);
Expand Down
5 changes: 2 additions & 3 deletions vector/src/main/java/im/vector/KeyRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package im.vector;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
Expand Down Expand Up @@ -109,7 +108,7 @@ public void handleKeyRequest(IncomingRoomKeyRequest keyRequest) {

requests.add(keyRequest);

if (null != mCurrentUser) {
if (null != mAlertDialog) {
// ignore for now
Log.d(LOG_TAG, "## handleKeyRequest() : Key request, but we already have a dialog open");
return;
Expand Down Expand Up @@ -240,7 +239,7 @@ private void onDisplayKeyShareDialogClose(boolean share) {
*/
private void initKeyShareDialog() {
if (null == VectorApp.getCurrentActivity()) {
// wait that an activity is ready
// wait until an activity is ready
mCurrentUser = null;
mCurrentDevice = null;
return;
Expand Down
167 changes: 13 additions & 154 deletions vector/src/main/java/im/vector/LoginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,50 +92,17 @@ public void login(Context ctx, final HomeServerConnectionConfig hsConfig, final
final SimpleApiCallback<HomeServerConnectionConfig> callback) {
final Context appCtx = ctx.getApplicationContext();

final SimpleApiCallback<Credentials> loginCallback = new SimpleApiCallback<Credentials>() {
callLogin(ctx, hsConfig, username, phoneNumber, phoneNumberCountry, password, new UnrecognizedCertApiCallback<Credentials>(hsConfig, callback) {
@Override
public void onSuccess(Credentials credentials) {
onRegistrationDone(appCtx, hsConfig, credentials, callback);
}

@Override
public void onNetworkError(final Exception e) {
UnrecognizedCertificateException unrecCertEx = CertUtil.getCertificateException(e);
if (unrecCertEx != null) {
final Fingerprint fingerprint = unrecCertEx.getFingerprint();
UnrecognizedCertHandler.show(hsConfig, fingerprint, false, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
login(appCtx, hsConfig, username, phoneNumber, phoneNumberCountry, password, callback);
}

@Override
public void onIgnore() {
callback.onNetworkError(e);
}

@Override
public void onReject() {
callback.onNetworkError(e);
}
});
} else {
callback.onNetworkError(e);
}
}

@Override
public void onUnexpectedError(Exception e) {
callback.onUnexpectedError(e);
public void onAcceptedCert() {
login(appCtx, hsConfig, username, phoneNumber, phoneNumberCountry, password, callback);
}

@Override
public void onMatrixError(MatrixError e) {
callback.onMatrixError(e);
}
};

callLogin(ctx, hsConfig, username, phoneNumber, phoneNumberCountry, password, loginCallback);
});
}

/**
Expand Down Expand Up @@ -178,48 +145,10 @@ public void getSupportedLoginFlows(Context ctx, final HomeServerConnectionConfig
final Context appCtx = ctx.getApplicationContext();
LoginRestClient client = new LoginRestClient(hsConfig);

client.getSupportedLoginFlows(new SimpleApiCallback<List<LoginFlow>>() {
@Override
public void onSuccess(List<LoginFlow> flows) {
Log.d(LOG_TAG, "getSupportedLoginFlows " + flows);
callback.onSuccess(flows);
}

@Override
public void onNetworkError(final Exception e) {
UnrecognizedCertificateException unrecCertEx = CertUtil.getCertificateException(e);
if (unrecCertEx != null) {
final Fingerprint fingerprint = unrecCertEx.getFingerprint();

UnrecognizedCertHandler.show(hsConfig, fingerprint, false, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
getSupportedLoginFlows(appCtx, hsConfig, callback);
}

@Override
public void onIgnore() {
callback.onNetworkError(e);
}

@Override
public void onReject() {
callback.onNetworkError(e);
}
});
} else {
callback.onNetworkError(e);
}
}

@Override
public void onUnexpectedError(Exception e) {
callback.onUnexpectedError(e);
}

client.getSupportedLoginFlows(new UnrecognizedCertApiCallback<List<LoginFlow>>(hsConfig, callback) {
@Override
public void onMatrixError(MatrixError e) {
callback.onMatrixError(e);
public void onAcceptedCert() {
getSupportedLoginFlows(appCtx, hsConfig, callback);
}
});
}
Expand Down Expand Up @@ -249,48 +178,15 @@ private void register(Context ctx, final HomeServerConnectionConfig hsConfig, fi
// avoid dispatching the device name
params.initial_device_display_name = ctx.getString(R.string.login_mobile_device);

client.register(params, new SimpleApiCallback<Credentials>() {
client.register(params, new UnrecognizedCertApiCallback<Credentials>(hsConfig, callback) {
@Override
public void onSuccess(Credentials credentials) {
onRegistrationDone(appCtx, hsConfig, credentials, callback);
}

@Override
public void onNetworkError(final Exception e) {
UnrecognizedCertificateException unrecCertEx = CertUtil.getCertificateException(e);
if (unrecCertEx != null) {
final Fingerprint fingerprint = unrecCertEx.getFingerprint();
Log.d(LOG_TAG, "Found fingerprint: SHA-256: " + fingerprint.getBytesAsHexString());

UnrecognizedCertHandler.show(hsConfig, fingerprint, false, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
getSupportedRegistrationFlows(appCtx, hsConfig, callback);
}

@Override
public void onIgnore() {
callback.onNetworkError(e);
}

@Override
public void onReject() {
callback.onNetworkError(e);
}
});
} else {
callback.onNetworkError(e);
}
}

@Override
public void onUnexpectedError(Exception e) {
callback.onUnexpectedError(e);
}

@Override
public void onMatrixError(MatrixError e) {
callback.onMatrixError(e);
public void onAcceptedCert() {
getSupportedRegistrationFlows(appCtx, hsConfig, callback);
}
});
}
Expand All @@ -311,47 +207,10 @@ public void submitEmailTokenValidation(final Context aCtx, final HomeServerConne
final ThreePid pid = new ThreePid(null, ThreePid.MEDIUM_EMAIL);
ThirdPidRestClient restClient = new ThirdPidRestClient(aHomeServerConfig);

pid.submitValidationToken(restClient, aToken, aClientSecret, aSid, new ApiCallback<Boolean>() {
@Override
public void onSuccess(Boolean isSuccess) {
aRespCallback.onSuccess(isSuccess);
}

@Override
public void onNetworkError(final Exception e) {
UnrecognizedCertificateException unrecCertEx = CertUtil.getCertificateException(e);
if (unrecCertEx != null) {
final Fingerprint fingerprint = unrecCertEx.getFingerprint();

UnrecognizedCertHandler.show(aHomeServerConfig, fingerprint, false, new UnrecognizedCertHandler.Callback() {
@Override
public void onAccept() {
submitEmailTokenValidation(aCtx, aHomeServerConfig, aToken, aClientSecret, aSid, aRespCallback);
}

@Override
public void onIgnore() {
aRespCallback.onNetworkError(e);
}

@Override
public void onReject() {
aRespCallback.onNetworkError(e);
}
});
} else {
aRespCallback.onNetworkError(e);
}
}

@Override
public void onUnexpectedError(Exception e) {
aRespCallback.onUnexpectedError(e);
}

pid.submitValidationToken(restClient, aToken, aClientSecret, aSid, new UnrecognizedCertApiCallback<Boolean>(aHomeServerConfig, aRespCallback) {
@Override
public void onMatrixError(MatrixError e) {
aRespCallback.onMatrixError(e);
public void onAcceptedCert() {
submitEmailTokenValidation(aCtx, aHomeServerConfig, aToken, aClientSecret, aSid, aRespCallback);
}
});
}
Expand Down
Loading