Some improvements suggested by Codacy
This commit is contained in:
parent
c7200d06bc
commit
5bc1bc2a47
@ -27,6 +27,9 @@ import android.widget.EditText;
|
|||||||
import android.widget.Switch;
|
import android.widget.Switch;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
@ -43,6 +46,8 @@ import ch.dissem.bitmessage.entity.payload.V4Pubkey;
|
|||||||
import static android.util.Base64.URL_SAFE;
|
import static android.util.Base64.URL_SAFE;
|
||||||
|
|
||||||
public class CreateAddressActivity extends AppCompatActivity {
|
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 static final Pattern KEY_VALUE_PATTERN = Pattern.compile("^([a-zA-Z]+)=(.*)$");
|
||||||
private byte[] pubkeyBytes;
|
private byte[] pubkeyBytes;
|
||||||
|
|
||||||
@ -77,6 +82,9 @@ public class CreateAddressActivity extends AppCompatActivity {
|
|||||||
case "pubkey":
|
case "pubkey":
|
||||||
pubkeyBytes = Base64.decode(value, URL_SAFE);
|
pubkeyBytes = Base64.decode(value, URL_SAFE);
|
||||||
break;
|
break;
|
||||||
|
default:
|
||||||
|
LOG.debug("Unknown attribute: " + key + "=" + value);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,10 +100,16 @@ public class CreateAddressActivity extends AppCompatActivity {
|
|||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
final Button ok = (Button) findViewById(R.id.do_import);
|
findViewById(R.id.do_import).setOnClickListener(new View.OnClickListener() {
|
||||||
ok.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
onOK(address, label, subscribe);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void onOK(TextView address, EditText label, Switch subscribe) {
|
||||||
String addressText = String.valueOf(address.getText()).trim();
|
String addressText = String.valueOf(address.getText()).trim();
|
||||||
try {
|
try {
|
||||||
BitmessageAddress bmAddress = new BitmessageAddress(addressText);
|
BitmessageAddress bmAddress = new BitmessageAddress(addressText);
|
||||||
@ -124,6 +138,7 @@ public class CreateAddressActivity extends AppCompatActivity {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
pubkey = null;
|
pubkey = null;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (pubkey != null) {
|
if (pubkey != null) {
|
||||||
bmAddress.setPubkey(pubkey);
|
bmAddress.setPubkey(pubkey);
|
||||||
@ -138,8 +153,6 @@ public class CreateAddressActivity extends AppCompatActivity {
|
|||||||
address.setError(getString(R.string.error_illegal_address));
|
address.setError(getString(R.string.error_illegal_address));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getAddress(Uri uri) {
|
private String getAddress(Uri uri) {
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
|
@ -277,6 +277,10 @@ public class MainActivity extends AppCompatActivity
|
|||||||
.withShowDrawerOnFirstLaunch(true)
|
.withShowDrawerOnFirstLaunch(true)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
loadDrawerItemsAsynchronously();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadDrawerItemsAsynchronously() {
|
||||||
new AsyncTask<Void, Void, List<BitmessageAddress>>() {
|
new AsyncTask<Void, Void, List<BitmessageAddress>>() {
|
||||||
@Override
|
@Override
|
||||||
protected List<BitmessageAddress> doInBackground(Void... params) {
|
protected List<BitmessageAddress> doInBackground(Void... params) {
|
||||||
|
@ -57,7 +57,7 @@ import ch.dissem.bitmessage.ports.MessageRepository;
|
|||||||
import static android.text.util.Linkify.WEB_URLS;
|
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_ADDRESS_PATTERN;
|
||||||
import static ch.dissem.apps.abit.util.Constants.BITMESSAGE_URL_SCHEMA;
|
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.avatar.setImageDrawable(new Identicon(message.getFrom()));
|
||||||
viewHolder.status.setImageResource(Assets.getStatusDrawable(message.getStatus()));
|
viewHolder.status.setImageResource(Assets.getStatusDrawable(message.getStatus()));
|
||||||
viewHolder.sender.setText(message.getFrom().toString());
|
viewHolder.sender.setText(message.getFrom().toString());
|
||||||
viewHolder.extract.setText(normalizeWhitespaces(message.getText()));
|
viewHolder.extract.setText(prepareMessageExtract(message.getText()));
|
||||||
viewHolder.item = message;
|
viewHolder.item = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,13 +80,6 @@ public class MessageListFragment extends Fragment implements ListHolder {
|
|||||||
private MessageRepository messageRepo;
|
private MessageRepository messageRepo;
|
||||||
private boolean activateOnItemClick;
|
private boolean activateOnItemClick;
|
||||||
|
|
||||||
/**
|
|
||||||
* Mandatory empty constructor for the fragment manager to instantiate the
|
|
||||||
* fragment (e.g. upon screen orientation changes).
|
|
||||||
*/
|
|
||||||
public MessageListFragment() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
@ -120,7 +120,7 @@ public class SettingsFragment
|
|||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
case PREFERENCE_TRUSTED_NODE:
|
case PREFERENCE_TRUSTED_NODE: {
|
||||||
String node = sharedPreferences.getString(PREFERENCE_TRUSTED_NODE, null);
|
String node = sharedPreferences.getString(PREFERENCE_TRUSTED_NODE, null);
|
||||||
if (node != null) {
|
if (node != null) {
|
||||||
SyncAdapter.startSync(getActivity());
|
SyncAdapter.startSync(getActivity());
|
||||||
@ -128,13 +128,18 @@ public class SettingsFragment
|
|||||||
SyncAdapter.stopSync(getActivity());
|
SyncAdapter.stopSync(getActivity());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PREFERENCE_SERVER_POW:
|
}
|
||||||
|
case PREFERENCE_SERVER_POW: {
|
||||||
|
String node = sharedPreferences.getString(PREFERENCE_TRUSTED_NODE, null);
|
||||||
|
if (node != null) {
|
||||||
if (sharedPreferences.getBoolean(PREFERENCE_SERVER_POW, false)) {
|
if (sharedPreferences.getBoolean(PREFERENCE_SERVER_POW, false)) {
|
||||||
SyncAdapter.startPowSync(getActivity());
|
SyncAdapter.startPowSync(getActivity());
|
||||||
} else {
|
} else {
|
||||||
SyncAdapter.stopPowSync(getActivity());
|
SyncAdapter.stopPowSync(getActivity());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ import ch.dissem.bitmessage.entity.Plaintext;
|
|||||||
import ch.dissem.bitmessage.entity.valueobject.Label;
|
import ch.dissem.bitmessage.entity.valueobject.Label;
|
||||||
|
|
||||||
import static ch.dissem.apps.abit.repository.AndroidMessageRepository.LABEL_ARCHIVE;
|
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
|
* Adapted from the basic swipeable example by Haruki Hasegawa. See
|
||||||
@ -182,8 +182,8 @@ public class SwipeableMessageAdapter
|
|||||||
holder.status.setContentDescription(
|
holder.status.setContentDescription(
|
||||||
holder.status.getContext().getString(Assets.getStatusString(item.getStatus())));
|
holder.status.getContext().getString(Assets.getStatusString(item.getStatus())));
|
||||||
holder.sender.setText(item.getFrom().toString());
|
holder.sender.setText(item.getFrom().toString());
|
||||||
holder.subject.setText(normalizeWhitespaces(item.getSubject()));
|
holder.subject.setText(prepareMessageExtract(item.getSubject()));
|
||||||
holder.extract.setText(normalizeWhitespaces(item.getText()));
|
holder.extract.setText(prepareMessageExtract(item.getText()));
|
||||||
if (item.isUnread()) {
|
if (item.isUnread()) {
|
||||||
holder.sender.setTypeface(Typeface.DEFAULT_BOLD);
|
holder.sender.setTypeface(Typeface.DEFAULT_BOLD);
|
||||||
holder.subject.setTypeface(Typeface.DEFAULT_BOLD);
|
holder.subject.setTypeface(Typeface.DEFAULT_BOLD);
|
||||||
|
@ -24,8 +24,19 @@ import java.util.regex.Pattern;
|
|||||||
public class Strings {
|
public class Strings {
|
||||||
private final static Pattern WHITESPACES = Pattern.compile("\\s+");
|
private final static Pattern WHITESPACES = Pattern.compile("\\s+");
|
||||||
|
|
||||||
public static String normalizeWhitespaces(CharSequence string) {
|
private static CharSequence trim(CharSequence string, int length) {
|
||||||
string = string.subSequence(0, Math.min(string.length(), 200));
|
if (string.length() <= length) {
|
||||||
return WHITESPACES.matcher(string).replaceAll(" ");
|
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(" ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user