From 5bc1bc2a4746074dd7fdfe670225c1f3f742d694 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 5 May 2017 07:27:24 +0200 Subject: [PATCH] Some improvements suggested by Codacy --- .../apps/abit/CreateAddressActivity.java | 99 +++++++++++-------- .../ch/dissem/apps/abit/MainActivity.java | 4 + .../apps/abit/MessageDetailFragment.java | 4 +- .../dissem/apps/abit/MessageListFragment.java | 7 -- .../ch/dissem/apps/abit/SettingsFragment.java | 17 ++-- .../abit/adapter/SwipeableMessageAdapter.java | 6 +- .../ch/dissem/apps/abit/util/Strings.java | 17 +++- 7 files changed, 90 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.java b/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.java index 6ed8060..c493a0c 100644 --- a/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/CreateAddressActivity.java @@ -27,6 +27,9 @@ import android.widget.EditText; import android.widget.Switch; import android.widget.TextView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.regex.Matcher; @@ -43,6 +46,8 @@ import ch.dissem.bitmessage.entity.payload.V4Pubkey; import static android.util.Base64.URL_SAFE; public class CreateAddressActivity extends AppCompatActivity { + private static final Logger LOG = LoggerFactory.getLogger(CreateAddressActivity.class); + private static final Pattern KEY_VALUE_PATTERN = Pattern.compile("^([a-zA-Z]+)=(.*)$"); private byte[] pubkeyBytes; @@ -77,6 +82,9 @@ public class CreateAddressActivity extends AppCompatActivity { case "pubkey": pubkeyBytes = Base64.decode(value, URL_SAFE); break; + default: + LOG.debug("Unknown attribute: " + key + "=" + value); + break; } } } @@ -92,55 +100,60 @@ public class CreateAddressActivity extends AppCompatActivity { finish(); } }); - final Button ok = (Button) findViewById(R.id.do_import); - ok.setOnClickListener(new View.OnClickListener() { + findViewById(R.id.do_import).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - String addressText = String.valueOf(address.getText()).trim(); - try { - BitmessageAddress bmAddress = new BitmessageAddress(addressText); - bmAddress.setAlias(label.getText().toString()); - - BitmessageContext bmc = Singleton.getBitmessageContext - (CreateAddressActivity.this); - bmc.addContact(bmAddress); - if (subscribe.isChecked()) { - bmc.addSubscribtion(bmAddress); - } - if (pubkeyBytes != null) { - try { - final Pubkey pubkey; - InputStream pubkeyStream = new ByteArrayInputStream(pubkeyBytes); - long stream = bmAddress.getStream(); - switch ((int) bmAddress.getVersion()) { - case 2: - pubkey = V2Pubkey.read(pubkeyStream, stream); - break; - case 3: - pubkey = V3Pubkey.read(pubkeyStream, stream); - break; - case 4: - pubkey = new V4Pubkey(V3Pubkey.read(pubkeyStream, stream)); - break; - default: - pubkey = null; - } - if (pubkey != null) { - bmAddress.setPubkey(pubkey); - } - } catch (Exception ignore) { - } - } - - setResult(Activity.RESULT_OK); - finish(); - } catch (RuntimeException e) { - address.setError(getString(R.string.error_illegal_address)); - } + onOK(address, label, subscribe); } }); } + + private void onOK(TextView address, EditText label, Switch subscribe) { + String addressText = String.valueOf(address.getText()).trim(); + try { + BitmessageAddress bmAddress = new BitmessageAddress(addressText); + bmAddress.setAlias(label.getText().toString()); + + BitmessageContext bmc = Singleton.getBitmessageContext + (CreateAddressActivity.this); + bmc.addContact(bmAddress); + if (subscribe.isChecked()) { + bmc.addSubscribtion(bmAddress); + } + if (pubkeyBytes != null) { + try { + final Pubkey pubkey; + InputStream pubkeyStream = new ByteArrayInputStream(pubkeyBytes); + long stream = bmAddress.getStream(); + switch ((int) bmAddress.getVersion()) { + case 2: + pubkey = V2Pubkey.read(pubkeyStream, stream); + break; + case 3: + pubkey = V3Pubkey.read(pubkeyStream, stream); + break; + case 4: + pubkey = new V4Pubkey(V3Pubkey.read(pubkeyStream, stream)); + break; + default: + pubkey = null; + break; + } + if (pubkey != null) { + bmAddress.setPubkey(pubkey); + } + } catch (Exception ignore) { + } + } + + setResult(Activity.RESULT_OK); + finish(); + } catch (RuntimeException e) { + address.setError(getString(R.string.error_illegal_address)); + } + } + private String getAddress(Uri uri) { StringBuilder result = new StringBuilder(); String schemeSpecificPart = uri.getSchemeSpecificPart(); diff --git a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java index 20e238a..590e983 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java @@ -277,6 +277,10 @@ public class MainActivity extends AppCompatActivity .withShowDrawerOnFirstLaunch(true) .build(); + loadDrawerItemsAsynchronously(); + } + + private void loadDrawerItemsAsynchronously() { new AsyncTask>() { @Override protected List doInBackground(Void... params) { diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java b/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java index ab851a9..4d31cb0 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java @@ -57,7 +57,7 @@ import ch.dissem.bitmessage.ports.MessageRepository; import static android.text.util.Linkify.WEB_URLS; import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_ADDRESS_PATTERN; import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_URL_SCHEMA; -import static ch.dissem.apps.abit.util.Strings.normalizeWhitespaces; +import static ch.dissem.apps.abit.util.Strings.prepareMessageExtract; /** @@ -263,7 +263,7 @@ public class MessageDetailFragment extends Fragment { viewHolder.avatar.setImageDrawable(new Identicon(message.getFrom())); viewHolder.status.setImageResource(Assets.getStatusDrawable(message.getStatus())); viewHolder.sender.setText(message.getFrom().toString()); - viewHolder.extract.setText(normalizeWhitespaces(message.getText())); + viewHolder.extract.setText(prepareMessageExtract(message.getText())); viewHolder.item = message; } diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java index f2bc0fb..5d43314 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java @@ -80,13 +80,6 @@ public class MessageListFragment extends Fragment implements ListHolder { private MessageRepository messageRepo; private boolean activateOnItemClick; - /** - * Mandatory empty constructor for the fragment manager to instantiate the - * fragment (e.g. upon screen orientation changes). - */ - public MessageListFragment() { - } - @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java b/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java index 111bf08..a92cc47 100644 --- a/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java @@ -120,7 +120,7 @@ public class SettingsFragment @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { switch (key) { - case PREFERENCE_TRUSTED_NODE: + case PREFERENCE_TRUSTED_NODE: { String node = sharedPreferences.getString(PREFERENCE_TRUSTED_NODE, null); if (node != null) { SyncAdapter.startSync(getActivity()); @@ -128,13 +128,18 @@ public class SettingsFragment SyncAdapter.stopSync(getActivity()); } break; - case PREFERENCE_SERVER_POW: - if (sharedPreferences.getBoolean(PREFERENCE_SERVER_POW, false)) { - SyncAdapter.startPowSync(getActivity()); - } else { - SyncAdapter.stopPowSync(getActivity()); + } + case PREFERENCE_SERVER_POW: { + String node = sharedPreferences.getString(PREFERENCE_TRUSTED_NODE, null); + if (node != null) { + if (sharedPreferences.getBoolean(PREFERENCE_SERVER_POW, false)) { + SyncAdapter.startPowSync(getActivity()); + } else { + SyncAdapter.stopPowSync(getActivity()); + } } break; + } } } } diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java b/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java index 7990c62..070092f 100644 --- a/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/SwipeableMessageAdapter.java @@ -46,7 +46,7 @@ import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.valueobject.Label; import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE; -import static ch.dissem.apps.abit.util.Strings.normalizeWhitespaces; +import static ch.dissem.apps.abit.util.Strings.prepareMessageExtract; /** * Adapted from the basic swipeable example by Haruki Hasegawa. See @@ -182,8 +182,8 @@ public class SwipeableMessageAdapter holder.status.setContentDescription( holder.status.getContext().getString(Assets.getStatusString(item.getStatus()))); holder.sender.setText(item.getFrom().toString()); - holder.subject.setText(normalizeWhitespaces(item.getSubject())); - holder.extract.setText(normalizeWhitespaces(item.getText())); + holder.subject.setText(prepareMessageExtract(item.getSubject())); + holder.extract.setText(prepareMessageExtract(item.getText())); if (item.isUnread()) { holder.sender.setTypeface(Typeface.DEFAULT_BOLD); holder.subject.setTypeface(Typeface.DEFAULT_BOLD); diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Strings.java b/app/src/main/java/ch/dissem/apps/abit/util/Strings.java index 7fef838..cc1f809 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Strings.java +++ b/app/src/main/java/ch/dissem/apps/abit/util/Strings.java @@ -24,8 +24,19 @@ import java.util.regex.Pattern; public class Strings { private final static Pattern WHITESPACES = Pattern.compile("\\s+"); - public static String normalizeWhitespaces(CharSequence string) { - string = string.subSequence(0, Math.min(string.length(), 200)); - return WHITESPACES.matcher(string).replaceAll(" "); + private static CharSequence trim(CharSequence string, int length) { + if (string.length() <= length) { + return string; + } else { + return string.subSequence(0, length); + } + } + + /** + * Trime the string to 200 characters and normalizes all whitespaces by replacing any sequence + * of whitespace characters with a single space character. + */ + public static String prepareMessageExtract(CharSequence string) { + return WHITESPACES.matcher(trim(string, 200)).replaceAll(" "); } }