Commit baa6b13a authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix coding style bugs

parent 48c08f41
......@@ -13,23 +13,26 @@ import chat.rocket.android.R;
import chat.rocket.android.helper.OnBackPressListener;
import hugo.weaving.DebugLog;
public abstract class AbstractWebViewFragment extends AbstractFragment implements OnBackPressListener {
public abstract class AbstractWebViewFragment extends AbstractFragment
implements OnBackPressListener {
private WebView webview;
private WebViewClient mWebViewClient = new WebViewClient() {
private boolean mError;
private WebViewClient webviewClient = new WebViewClient() {
private boolean error;
@Override public void onPageStarted(WebView webview, String url, Bitmap favicon) {
mError = false;
error = false;
}
@Override public void onPageFinished(WebView webview, String url) {
if (!mError) onPageLoaded(webview, url);
if (!error) {
onPageLoaded(webview, url);
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
mError = true;
this.error = true;
}
@Override public boolean shouldOverrideUrlLoading(WebView webview, String url) {
......@@ -37,7 +40,8 @@ public abstract class AbstractWebViewFragment extends AbstractFragment implement
|| super.shouldOverrideUrlLoading(webview, url);
}
@DebugLog @Override
@DebugLog
@Override
public void onFormResubmission(WebView view, Message dontResend, Message resend) {
//resend POST request without confirmation.
resend.sendToTarget();
......@@ -61,7 +65,7 @@ public abstract class AbstractWebViewFragment extends AbstractFragment implement
settings.setJavaScriptEnabled(true);
}
webview.setHorizontalScrollBarEnabled(false);
webview.setWebViewClient(mWebViewClient);
webview.setWebViewClient(webviewClient);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
WebView.setWebContentsDebuggingEnabled(true);
......
......@@ -11,6 +11,7 @@ import chat.rocket.android.fragment.AbstractWebViewFragment;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.MeteorLoginServiceConfiguration;
import chat.rocket.android.model.ServerConfig;
import java.nio.charset.Charset;
import jp.co.crowdworks.realm_java_helpers.RealmHelper;
import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;
import okhttp3.HttpUrl;
......@@ -26,6 +27,9 @@ public class GitHubOAuthWebViewFragment extends AbstractWebViewFragment {
private String url;
private boolean resultOK;
/**
* create new Fragment with ServerConfig-ID.
*/
public static Fragment create(final String serverConfigId) {
Bundle args = new Bundle();
args.putString("server_config_id", serverConfigId);
......@@ -70,7 +74,7 @@ public class GitHubOAuthWebViewFragment extends AbstractWebViewFragment {
.put("credentialToken", "github" + System.currentTimeMillis())
.put("isCordova", true)
.toString()
.getBytes(), Base64.NO_WRAP);
.getBytes(Charset.forName("UTF-8")), Base64.NO_WRAP);
return new HttpUrl.Builder().scheme("https")
.host("github.com")
......@@ -123,8 +127,9 @@ public class GitHubOAuthWebViewFragment extends AbstractWebViewFragment {
if (url.contains(hostname) && url.contains("_oauth/github?close")) {
webview.loadUrl(
"javascript:window._rocketchet_hook.handleConfig(document.getElementById('config').innerText);");
final String jsHookUrl = "javascript:"
+ "window._rocketchet_hook.handleConfig(document.getElementById('config').innerText);";
webview.loadUrl(jsHookUrl);
}
}
......@@ -133,17 +138,17 @@ public class GitHubOAuthWebViewFragment extends AbstractWebViewFragment {
}
private static final class JSInterface {
private final JSInterfaceCallback mCallback;
private final JSInterfaceCallback jsInterfaceCallback;
public JSInterface(JSInterfaceCallback callback) {
mCallback = callback;
JSInterface(JSInterfaceCallback callback) {
jsInterfaceCallback = callback;
}
@JavascriptInterface public void handleConfig(String config) {
try {
mCallback.hanldeResult(new JSONObject(config));
} catch (Exception e) {
mCallback.hanldeResult(null);
jsInterfaceCallback.hanldeResult(new JSONObject(config));
} catch (Exception exception) {
jsInterfaceCallback.hanldeResult(null);
}
}
}
......
......@@ -5,6 +5,7 @@ import android.widget.TextView;
import chat.rocket.android.R;
/**
* Just show "Authenticating..."
*/
public class AuthenticatingFragment extends AbstractServerConfigFragment {
@Override protected int getLayout() {
......@@ -14,6 +15,6 @@ public class AuthenticatingFragment extends AbstractServerConfigFragment {
@Override protected void onSetupView() {
TextView caption = (TextView) rootView.findViewById(R.id.txt_caption);
caption.setVisibility(View.VISIBLE);
caption.setText("Authenticationg...");
caption.setText("Authenticating...");
}
}
\ No newline at end of file
......@@ -14,6 +14,7 @@ import chat.rocket.android.model.ServerConfigCredential;
import chat.rocket.android.renderer.ServerConfigCredentialRenderer;
import io.realm.Realm;
import io.realm.RealmResults;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
......@@ -56,7 +57,9 @@ public class LoginFragment extends AbstractServerConfigFragment {
btnEmail.setOnClickListener(view -> {
final CharSequence username = txtUsername.getText();
final CharSequence passwd = txtPasswd.getText();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(passwd)) return;
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(passwd)) {
return;
}
RealmHelperBolts.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject()
......@@ -75,10 +78,11 @@ public class LoginFragment extends AbstractServerConfigFragment {
}
private void showErrorIfNeeded() {
ServerConfig config = RealmHelper.executeTransactionForRead(realm -> realm.where(ServerConfig.class)
.equalTo("id", serverConfigId)
.isNotNull("credential.errorMessage")
.findFirst());
ServerConfig config = RealmHelper.executeTransactionForRead(realm ->
realm.where(ServerConfig.class)
.equalTo("id", serverConfigId)
.isNotNull("credential.errorMessage")
.findFirst());
if (config != null) {
ServerConfigCredential credential = config.getCredential();
......@@ -105,14 +109,14 @@ public class LoginFragment extends AbstractServerConfigFragment {
} catch (NoSuchAlgorithmException exception) {
return null;
}
messageDigest.update(orig.getBytes());
messageDigest.update(orig.getBytes(Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder();
for (byte b : messageDigest.digest()) {
sb.append(String.format("%02x", b & 0xff));
stringBuilder.append(String.format("%02x", b & 0xff));
}
return sb.toString();
return stringBuilder.toString();
}
private void onRenderAuthProviders(List<MeteorLoginServiceConfiguration> authProviders) {
......
......@@ -9,6 +9,7 @@ import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;
import org.json.JSONException;
import org.json.JSONObject;
@SuppressWarnings("PMD.ShortVariable")
public class ServerConfigCredential extends RealmObject {
public static final String TYPE_EMAIL = "email";
public static final String TYPE_TWITTER = "twitter";
......
......@@ -21,7 +21,9 @@ abstract class AbstractRenderer<T> {
protected boolean shouldHandle(View target, Condition additionalCondition, Optional optional,
String key) {
if (target == null || object == null) {
if (optional != null) optional.onNoData(key);
if (optional != null) {
optional.onNoData(key);
}
return false;
}
......
......@@ -5,11 +5,17 @@ import android.widget.TextView;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfigCredential;
/**
* renderer class for ServerConfigCredential.
*/
public class ServerConfigCredentialRenderer extends AbstractRenderer<ServerConfigCredential> {
public ServerConfigCredentialRenderer(Context context, ServerConfigCredential credential) {
super(context, credential);
}
/**
* Inject username into TextView.
*/
public ServerConfigCredentialRenderer usernameInto(TextView textView) {
if (!shouldHandle(textView)) {
return this;
......
......@@ -129,8 +129,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
}
socketExists = true;
final ServerConfig config = RealmHelper.executeTransactionForRead(
realm -> realm.where(ServerConfig.class).equalTo("id", serverConfigId).findFirst());
final ServerConfig config = RealmHelper.executeTransactionForRead(realm ->
realm.where(ServerConfig.class).equalTo("id", serverConfigId).findFirst());
prepareWebSocket(config);
return webSocketAPI.connect(config.getSession()).onSuccessTask(task -> {
......@@ -180,7 +180,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
for (Class clazz : REGISTERABLE_CLASSES) {
try {
Constructor ctor = clazz.getConstructor(Context.class, String.class, RocketChatWebSocketAPI.class);
Constructor ctor = clazz.getConstructor(Context.class, String.class,
RocketChatWebSocketAPI.class);
Object obj = ctor.newInstance(appContext, serverConfigId, webSocketAPI);
if (obj instanceof Registerable) {
......
......@@ -102,25 +102,30 @@ public class RocketChatWebSocketAPI {
.put("credentialToken", credential.getCredentialToken())
.put("credentialSecret", credential.getCredentialSecret()));
}
}
catch (JSONException e) {
return Task.forError(e);
} catch (JSONException exception) {
return Task.forError(exception);
}
return ddpClient.rpc("login", new JSONArray().put(param), generateId("login"));
}
/**
* Login with token.
*/
public Task<DDPClientCallback.RPC> loginWithToken(final String token) {
JSONObject param = new JSONObject();
try {
param.put("resume", token);
} catch (JSONException e) {
return Task.forError(e);
} catch (JSONException exception) {
return Task.forError(exception);
}
return ddpClient.rpc("login", new JSONArray().put(param), generateId("login-token"));
}
/**
* Logout.
*/
public Task<DDPClientCallback.RPC> logout() {
return ddpClient.rpc("logout", null, generateId("logout"));
}
......
......@@ -27,12 +27,6 @@
<property name="eachLine" value="true"/>
</module>
<module name="SuppressionCommentFilter">
<property name="offCommentFormat" value="CHECKSTYLE:OFF (\w+)"/>
<property name="onCommentFormat" value="CHECKSTYLE:ON (\w+)"/>
<property name="checkFormat" value="$1"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="config/quality/checkstyle/checkstyle-suppressions.xml"/>
</module>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment