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.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();
|
||||
|
@ -277,6 +277,10 @@ public class MainActivity extends AppCompatActivity
|
||||
.withShowDrawerOnFirstLaunch(true)
|
||||
.build();
|
||||
|
||||
loadDrawerItemsAsynchronously();
|
||||
}
|
||||
|
||||
private void loadDrawerItemsAsynchronously() {
|
||||
new AsyncTask<Void, Void, List<BitmessageAddress>>() {
|
||||
@Override
|
||||
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 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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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(" ");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user