From b3dd53a5df82d75da05a5735f3660aea453174c5 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Thu, 20 Oct 2016 05:49:07 +0200 Subject: [PATCH] Lint fixes --- .../apps/abit/AbstractItemListFragment.java | 2 +- .../dissem/apps/abit/AddressListFragment.java | 5 - .../apps/abit/CreateAddressActivity.java | 43 +++--- .../ch/dissem/apps/abit/DetailActivity.java | 2 +- .../java/ch/dissem/apps/abit/Identicon.java | 8 +- .../ch/dissem/apps/abit/InputWifFragment.java | 55 ++++---- .../dissem/apps/abit/MessageListFragment.java | 17 +-- .../ch/dissem/apps/abit/SettingsFragment.java | 38 +++-- .../abit/adapter/AddressSelectorAdapter.java | 14 +- .../abit/adapter/SwipeableMessageAdapter.java | 26 ++-- .../dialog/AddIdentityDialogFragment.java | 133 ++++++++---------- .../apps/abit/listener/WifiReceiver.java | 12 +- .../abit/notification/ErrorNotification.java | 2 +- .../dissem/apps/abit/pow/ServerPowEngine.java | 54 +++---- .../repository/AndroidAddressRepository.java | 16 +-- .../repository/AndroidMessageRepository.java | 6 +- .../apps/abit/repository/SqlHelper.java | 8 +- .../apps/abit/service/ProofOfWorkService.java | 4 +- .../apps/abit/service/ServicePowEngine.java | 6 +- .../ch/dissem/apps/abit/util/Preferences.java | 3 - 20 files changed, 192 insertions(+), 262 deletions(-) diff --git a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.java b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.java index 5432210..df96b43 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/AbstractItemListFragment.java @@ -37,7 +37,7 @@ public abstract class AbstractItemListFragment extends ListFragment implement * A dummy implementation of the {@link ListSelectionListener} interface that does * nothing. Used only when this fragment is not attached to an activity. */ - private static ListSelectionListener dummyCallbacks = plaintext -> { + private static final ListSelectionListener dummyCallbacks = plaintext -> { }; /** * The fragment's current callback object, which is notified of list item diff --git a/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.java b/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.java index 5168422..57aa3dd 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/AddressListFragment.java @@ -144,11 +144,6 @@ public class AddressListFragment extends AbstractItemListFragment { + setResult(Activity.RESULT_CANCELED); + finish(); }); final Button ok = (Button) findViewById(R.id.do_import); - ok.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - String addressText = String.valueOf(address.getText()).trim(); - try { - BitmessageAddress bmAddress = new BitmessageAddress(addressText); - bmAddress.setAlias(label.getText().toString()); + ok.setOnClickListener(v -> { + 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); - } - - setResult(Activity.RESULT_OK); - finish(); - } catch (RuntimeException e) { - address.setError(getString(R.string.error_illegal_address)); + BitmessageContext bmc = Singleton.getBitmessageContext + (CreateAddressActivity.this); + bmc.addContact(bmAddress); + if (subscribe.isChecked()) { + bmc.addSubscribtion(bmAddress); } + + setResult(Activity.RESULT_OK); + finish(); + } catch (RuntimeException e) { + address.setError(getString(R.string.error_illegal_address)); } }); } diff --git a/app/src/main/java/ch/dissem/apps/abit/DetailActivity.java b/app/src/main/java/ch/dissem/apps/abit/DetailActivity.java index fb94ef2..f725c46 100644 --- a/app/src/main/java/ch/dissem/apps/abit/DetailActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/DetailActivity.java @@ -12,7 +12,7 @@ import com.mikepenz.materialize.MaterializeBuilder; /** * @author Christian Basler */ -public class DetailActivity extends AppCompatActivity { +public abstract class DetailActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/ch/dissem/apps/abit/Identicon.java b/app/src/main/java/ch/dissem/apps/abit/Identicon.java index a8da2c3..fc6122c 100644 --- a/app/src/main/java/ch/dissem/apps/abit/Identicon.java +++ b/app/src/main/java/ch/dissem/apps/abit/Identicon.java @@ -31,10 +31,10 @@ public class Identicon extends Drawable { private static final int CENTER_COLUMN = 5; private final Paint paint; - private int color; - private int background; - private boolean[][] fields; - private boolean chan; + private final int color; + private final int background; + private final boolean[][] fields; + private final boolean chan; private final TextPaint textPaint; public Identicon(BitmessageAddress input) { diff --git a/app/src/main/java/ch/dissem/apps/abit/InputWifFragment.java b/app/src/main/java/ch/dissem/apps/abit/InputWifFragment.java index 8ccdf5d..d1498ed 100644 --- a/app/src/main/java/ch/dissem/apps/abit/InputWifFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/InputWifFragment.java @@ -28,7 +28,6 @@ import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; -import com.github.angads25.filepicker.controller.DialogSelectionListener; import com.github.angads25.filepicker.model.DialogConfigs; import com.github.angads25.filepicker.model.DialogProperties; import com.github.angads25.filepicker.view.FilePickerDialog; @@ -61,19 +60,16 @@ public class InputWifFragment extends Fragment { View view = inflater.inflate(R.layout.fragment_import_input, container, false); wifData = (TextView) view.findViewById(R.id.wif_input); - view.findViewById(R.id.next).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Bundle bundle = new Bundle(); - bundle.putString(WIF_DATA, wifData.getText().toString()); + view.findViewById(R.id.next).setOnClickListener(v -> { + Bundle bundle = new Bundle(); + bundle.putString(WIF_DATA, wifData.getText().toString()); - ImportIdentitiesFragment fragment = new ImportIdentitiesFragment(); - fragment.setArguments(bundle); + ImportIdentitiesFragment fragment = new ImportIdentitiesFragment(); + fragment.setArguments(bundle); - getFragmentManager().beginTransaction() - .replace(R.id.content, fragment) - .commit(); - } + getFragmentManager().beginTransaction() + .replace(R.id.content, fragment) + .commit(); }); return view; } @@ -93,26 +89,23 @@ public class InputWifFragment extends Fragment { properties.extensions = null; FilePickerDialog dialog = new FilePickerDialog(getActivity(), properties); dialog.setTitle(getString(R.string.select_file_title)); - dialog.setDialogSelectionListener(new DialogSelectionListener() { - @Override - public void onSelectedFilePaths(String[] files) { - if (files.length > 0) { - try (InputStream in = new FileInputStream(files[0])) { - ByteArrayOutputStream data = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - //noinspection ConstantConditions - while ((length = in.read(buffer)) != -1) { - data.write(buffer, 0, length); - } - wifData.setText(data.toString("UTF-8")); - } catch (IOException e) { - Toast.makeText( - getActivity(), - R.string.error_loading_data, - Toast.LENGTH_SHORT - ).show(); + dialog.setDialogSelectionListener(files -> { + if (files.length > 0) { + try (InputStream in = new FileInputStream(files[0])) { + ByteArrayOutputStream data = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + //noinspection ConstantConditions + while ((length = in.read(buffer)) != -1) { + data.write(buffer, 0, length); } + wifData.setText(data.toString("UTF-8")); + } catch (IOException e) { + Toast.makeText( + getActivity(), + R.string.error_loading_data, + Toast.LENGTH_SHORT + ).show(); } } }); 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 9328ce2..0d22d98 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageListFragment.java @@ -130,15 +130,12 @@ public class MessageListFragment extends Fragment implements ListHolder { // Show the dummy content as text in a TextView. FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id .fab_compose_message); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Intent intent = new Intent(getActivity().getApplicationContext(), - ComposeMessageActivity.class); - intent.putExtra(ComposeMessageActivity.EXTRA_IDENTITY, Singleton.getIdentity - (getActivity())); - startActivity(intent); - } + fab.setOnClickListener(view -> { + Intent intent = new Intent(getActivity().getApplicationContext(), + ComposeMessageActivity.class); + intent.putExtra(ComposeMessageActivity.EXTRA_IDENTITY, Singleton.getIdentity + (getActivity())); + startActivity(intent); }); // touch guard manager (this class is required to suppress scrolling while swipe-dismiss @@ -173,7 +170,7 @@ public class MessageListFragment extends Fragment implements ListHolder { } @Override - public void onItemViewClicked(View v, boolean pinned) { + public void onItemViewClicked(View v) { int position = recyclerView.getChildAdapterPosition(v); adapter.setSelectedPosition(position); if (position != RecyclerView.NO_POSITION) { 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 60ecbeb..c905f65 100644 --- a/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/SettingsFragment.java @@ -36,8 +36,8 @@ import static ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE; * @author Christian Basler */ public class SettingsFragment - extends PreferenceFragment - implements SharedPreferences.OnSharedPreferenceChangeListener { + extends PreferenceFragment + implements SharedPreferences.OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -46,27 +46,21 @@ public class SettingsFragment addPreferencesFromResource(R.xml.preferences); Preference about = findPreference("about"); - about.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - new LibsBuilder() - .withActivityTitle(getActivity().getString(R.string.about)) - .withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR) - .withAboutIconShown(true) - .withAboutVersionShown(true) - .withAboutDescription(getString(R.string.about_app)) - .start(getActivity()); - return true; - } + about.setOnPreferenceClickListener(preference -> { + new LibsBuilder() + .withActivityTitle(getActivity().getString(R.string.about)) + .withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR) + .withAboutIconShown(true) + .withAboutVersionShown(true) + .withAboutDescription(getString(R.string.about_app)) + .start(getActivity()); + return true; }); Preference status = findPreference("status"); - status.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - startActivity(new Intent(getActivity(), StatusActivity.class)); - return true; - } + status.setOnPreferenceClickListener(preference -> { + startActivity(new Intent(getActivity(), StatusActivity.class)); + return true; }); } @@ -74,7 +68,7 @@ public class SettingsFragment public void onAttach(Context ctx) { super.onAttach(ctx); PreferenceManager.getDefaultSharedPreferences(ctx) - .registerOnSharedPreferenceChangeListener(this); + .registerOnSharedPreferenceChangeListener(this); } @Override @@ -97,4 +91,4 @@ public class SettingsFragment break; } } -} \ No newline at end of file +} diff --git a/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java b/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java index 4103097..f35a52a 100644 --- a/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/adapter/AddressSelectorAdapter.java @@ -21,7 +21,6 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.TextView; import java.util.ArrayList; @@ -69,19 +68,16 @@ public class AddressSelectorAdapter static class ViewHolder extends RecyclerView.ViewHolder { public Selectable data; - public CheckBox checkbox; - public TextView address; + public final CheckBox checkbox; + public final TextView address; private ViewHolder(View v) { super(v); checkbox = (CheckBox) v.findViewById(R.id.checkbox); address = (TextView) v.findViewById(R.id.address); - checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { - @Override - public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { - if (data != null) { - data.selected = isChecked; - } + checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> { + if (data != null) { + data.selected = isChecked; } }); } 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 2e4775b..53db232 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 @@ -59,8 +59,8 @@ public class SwipeableMessageAdapter private List data = Collections.emptyList(); private EventListener eventListener; - private View.OnClickListener itemViewOnClickListener; - private View.OnClickListener swipeableViewContainerOnClickListener; + private final View.OnClickListener itemViewOnClickListener; + private final View.OnClickListener swipeableViewContainerOnClickListener; private Label label; private int selectedPosition; @@ -75,12 +75,12 @@ public class SwipeableMessageAdapter void onItemArchived(Plaintext item); - void onItemViewClicked(View v, boolean pinned); + void onItemViewClicked(View v); } @SuppressWarnings("WeakerAccess") static class ViewHolder extends AbstractSwipeableItemViewHolder { - public FrameLayout container; + public final FrameLayout container; public final ImageView avatar; public final TextView sender; public final TextView subject; @@ -102,18 +102,8 @@ public class SwipeableMessageAdapter } public SwipeableMessageAdapter() { - itemViewOnClickListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - onItemViewClick(v); - } - }; - swipeableViewContainerOnClickListener = new View.OnClickListener() { - @Override - public void onClick(View v) { - onSwipeableViewContainerClick(v); - } - }; + itemViewOnClickListener = this::onItemViewClick; + swipeableViewContainerOnClickListener = this::onSwipeableViewContainerClick; // SwipeableItemAdapter requires stable ID, and also // have to implement the getItemId() method appropriately. @@ -127,14 +117,14 @@ public class SwipeableMessageAdapter private void onItemViewClick(View v) { if (eventListener != null) { - eventListener.onItemViewClicked(v, true); // pinned + eventListener.onItemViewClicked(v); } } private void onSwipeableViewContainerClick(View v) { if (eventListener != null) { eventListener.onItemViewClicked( - RecyclerViewAdapterUtils.getParentViewHolderItemView(v), false); // not pinned + RecyclerViewAdapterUtils.getParentViewHolderItemView(v)); } } diff --git a/app/src/main/java/ch/dissem/apps/abit/dialog/AddIdentityDialogFragment.java b/app/src/main/java/ch/dissem/apps/abit/dialog/AddIdentityDialogFragment.java index 710ead0..59e16b6 100644 --- a/app/src/main/java/ch/dissem/apps/abit/dialog/AddIdentityDialogFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/dialog/AddIdentityDialogFragment.java @@ -19,7 +19,6 @@ package ch.dissem.apps.abit.dialog; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; -import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; @@ -61,56 +60,47 @@ public class AddIdentityDialogFragment extends AppCompatDialogFragment { getDialog().setTitle(R.string.add_identity); View view = inflater.inflate(R.layout.dialog_add_identity, container, false); final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup); - view.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - final Context ctx = getActivity().getBaseContext(); - switch (radioGroup.getCheckedRadioButtonId()) { - case R.id.create_identity: - Toast.makeText(ctx, - R.string.toast_long_running_operation, - Toast.LENGTH_SHORT).show(); - new AsyncTask<Void, Void, BitmessageAddress>() { - @Override - protected BitmessageAddress doInBackground(Void... args) { - return bmc.createIdentity(false, Pubkey.Feature.DOES_ACK); - } + view.findViewById(R.id.ok).setOnClickListener(v -> { + final Context ctx = getActivity().getBaseContext(); + switch (radioGroup.getCheckedRadioButtonId()) { + case R.id.create_identity: + Toast.makeText(ctx, + R.string.toast_long_running_operation, + Toast.LENGTH_SHORT).show(); + new AsyncTask<Void, Void, BitmessageAddress>() { + @Override + protected BitmessageAddress doInBackground(Void... args) { + return bmc.createIdentity(false, Pubkey.Feature.DOES_ACK); + } - @Override - protected void onPostExecute(BitmessageAddress chan) { - Toast.makeText(ctx, - R.string.toast_identity_created, - Toast.LENGTH_SHORT).show(); - MainActivity mainActivity = MainActivity.getInstance(); - if (mainActivity != null) { - mainActivity.addIdentityEntry(chan); - } + @Override + protected void onPostExecute(BitmessageAddress chan) { + Toast.makeText(ctx, + R.string.toast_identity_created, + Toast.LENGTH_SHORT).show(); + MainActivity mainActivity = MainActivity.getInstance(); + if (mainActivity != null) { + mainActivity.addIdentityEntry(chan); } - }.execute(); - break; - case R.id.import_identity: - startActivity(new Intent(ctx, ImportIdentityActivity.class)); - break; - case R.id.add_chan: - addChanDialog(); - break; - case R.id.add_deterministic_address: - new DeterministicIdentityDialogFragment().show(getFragmentManager(), - "dialog"); - break; - default: - return; - } - dismiss(); + } + }.execute(); + break; + case R.id.import_identity: + startActivity(new Intent(ctx, ImportIdentityActivity.class)); + break; + case R.id.add_chan: + addChanDialog(); + break; + case R.id.add_deterministic_address: + new DeterministicIdentityDialogFragment().show(getFragmentManager(), + "dialog"); + break; + default: + return; } + dismiss(); }); - view.findViewById(R.id.dismiss) - .setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - } - }); + view.findViewById(R.id.dismiss).setOnClickListener(v -> dismiss()); return view; } @@ -123,34 +113,31 @@ public class AddIdentityDialogFragment extends AppCompatDialogFragment { new AlertDialog.Builder(activity) .setTitle(R.string.add_chan) .setView(dialogView) - .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - TextView passphrase = (TextView) dialogView.findViewById(R.id.passphrase); - Toast.makeText(ctx, R.string.toast_long_running_operation, - Toast.LENGTH_SHORT).show(); - new AsyncTask<String, Void, BitmessageAddress>() { - @Override - protected BitmessageAddress doInBackground(String... args) { - String pass = args[0]; - BitmessageAddress chan = bmc.createChan(pass); - chan.setAlias(pass); - bmc.addresses().save(chan); - return chan; - } + .setPositiveButton(R.string.ok, (dialogInterface, i) -> { + TextView passphrase = (TextView) dialogView.findViewById(R.id.passphrase); + Toast.makeText(ctx, R.string.toast_long_running_operation, + Toast.LENGTH_SHORT).show(); + new AsyncTask<String, Void, BitmessageAddress>() { + @Override + protected BitmessageAddress doInBackground(String... args) { + String pass = args[0]; + BitmessageAddress chan = bmc.createChan(pass); + chan.setAlias(pass); + bmc.addresses().save(chan); + return chan; + } - @Override - protected void onPostExecute(BitmessageAddress chan) { - Toast.makeText(ctx, - R.string.toast_chan_created, - Toast.LENGTH_SHORT).show(); - MainActivity mainActivity = MainActivity.getInstance(); - if (mainActivity != null) { - mainActivity.addIdentityEntry(chan); - } + @Override + protected void onPostExecute(BitmessageAddress chan) { + Toast.makeText(ctx, + R.string.toast_chan_created, + Toast.LENGTH_SHORT).show(); + MainActivity mainActivity = MainActivity.getInstance(); + if (mainActivity != null) { + mainActivity.addIdentityEntry(chan); } - }.execute(passphrase.getText().toString()); - } + } + }.execute(passphrase.getText().toString()); }) .setNegativeButton(R.string.cancel, null) .show(); diff --git a/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java b/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java index ccd81ac..329d903 100644 --- a/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java +++ b/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java @@ -29,11 +29,13 @@ import ch.dissem.bitmessage.BitmessageContext; public class WifiReceiver extends BroadcastReceiver { @Override public void onReceive(Context ctx, Intent intent) { - if (Preferences.isWifiOnly(ctx)) { - BitmessageContext bmc = Singleton.getBitmessageContext(ctx); + if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { + if (Preferences.isWifiOnly(ctx)) { + BitmessageContext bmc = Singleton.getBitmessageContext(ctx); - if (isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) { - bmc.shutdown(); + if (isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) { + bmc.shutdown(); + } } } } @@ -43,7 +45,7 @@ public class WifiReceiver extends BroadcastReceiver { if (netInfo == null || !netInfo.isConnectedOrConnecting()) { return false; } - switch (netInfo.getType()){ + switch (netInfo.getType()) { case ConnectivityManager.TYPE_ETHERNET: case ConnectivityManager.TYPE_WIFI: return false; diff --git a/app/src/main/java/ch/dissem/apps/abit/notification/ErrorNotification.java b/app/src/main/java/ch/dissem/apps/abit/notification/ErrorNotification.java index 0efdc66..e7a0654 100644 --- a/app/src/main/java/ch/dissem/apps/abit/notification/ErrorNotification.java +++ b/app/src/main/java/ch/dissem/apps/abit/notification/ErrorNotification.java @@ -31,7 +31,7 @@ import ch.dissem.apps.abit.R; public class ErrorNotification extends AbstractNotification { public static final int ERROR_NOTIFICATION_ID = 4; - private NotificationCompat.Builder builder; + private final NotificationCompat.Builder builder; public ErrorNotification(Context ctx) { super(ctx); diff --git a/app/src/main/java/ch/dissem/apps/abit/pow/ServerPowEngine.java b/app/src/main/java/ch/dissem/apps/abit/pow/ServerPowEngine.java index 1dd81bb..e8812ea 100644 --- a/app/src/main/java/ch/dissem/apps/abit/pow/ServerPowEngine.java +++ b/app/src/main/java/ch/dissem/apps/abit/pow/ServerPowEngine.java @@ -17,14 +17,12 @@ package ch.dissem.apps.abit.pow; import android.content.Context; -import android.support.annotation.NonNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import java.util.concurrent.ThreadFactory; import ch.dissem.apps.abit.service.Singleton; import ch.dissem.apps.abit.synchronization.SyncAdapter; @@ -42,7 +40,7 @@ import static ch.dissem.bitmessage.utils.Singleton.cryptography; * @author Christian Basler */ public class ServerPowEngine implements ProofOfWorkEngine, InternalContext - .ContextHolder { + .ContextHolder { private static final Logger LOG = LoggerFactory.getLogger(ServerPowEngine.class); private final Context ctx; @@ -52,40 +50,34 @@ public class ServerPowEngine implements ProofOfWorkEngine, InternalContext public ServerPowEngine(Context ctx) { this.ctx = ctx; - pool = Executors.newCachedThreadPool(new ThreadFactory() { - @Override - public Thread newThread(@NonNull Runnable r) { - Thread thread = Executors.defaultThreadFactory().newThread(r); - thread.setPriority(Thread.MIN_PRIORITY); - return thread; - } + pool = Executors.newCachedThreadPool(r -> { + Thread thread = Executors.defaultThreadFactory().newThread(r); + thread.setPriority(Thread.MIN_PRIORITY); + return thread; }); } @Override public void calculateNonce(final byte[] initialHash, final byte[] target, Callback callback) { - pool.execute(new Runnable() { - @Override - public void run() { - BitmessageAddress identity = Singleton.getIdentity(ctx); - if (identity == null) throw new RuntimeException("No Identity for calculating POW"); + pool.execute(() -> { + BitmessageAddress identity = Singleton.getIdentity(ctx); + if (identity == null) throw new RuntimeException("No Identity for calculating POW"); - ProofOfWorkRequest request = new ProofOfWorkRequest(identity, initialHash, - CALCULATE, target); - SyncAdapter.startPowSync(ctx); - try { - CryptoCustomMessage<ProofOfWorkRequest> cryptoMsg = new CryptoCustomMessage<> - (request); - cryptoMsg.signAndEncrypt( - identity, - cryptography().createPublicKey(identity.getPublicDecryptionKey()) - ); - context.getNetworkHandler().send( - Preferences.getTrustedNode(ctx), Preferences.getTrustedNodePort(ctx), - cryptoMsg); - } catch (Exception e) { - LOG.error(e.getMessage(), e); - } + ProofOfWorkRequest request = new ProofOfWorkRequest(identity, initialHash, + CALCULATE, target); + SyncAdapter.startPowSync(ctx); + try { + CryptoCustomMessage<ProofOfWorkRequest> cryptoMsg = new CryptoCustomMessage<> + (request); + cryptoMsg.signAndEncrypt( + identity, + cryptography().createPublicKey(identity.getPublicDecryptionKey()) + ); + context.getNetworkHandler().send( + Preferences.getTrustedNode(ctx), Preferences.getTrustedNodePort(ctx), + cryptoMsg); + } catch (Exception e) { + LOG.error(e.getMessage(), e); } }); } diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidAddressRepository.java b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidAddressRepository.java index b585aeb..5557112 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidAddressRepository.java +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidAddressRepository.java @@ -169,14 +169,10 @@ public class AndroidAddressRepository implements AddressRepository { @Override public void save(BitmessageAddress address) { - try { - if (exists(address)) { - update(address); - } else { - insert(address); - } - } catch (IOException e) { - LOG.error(e.getMessage(), e); + if (exists(address)) { + update(address); + } else { + insert(address); } } @@ -191,7 +187,7 @@ public class AndroidAddressRepository implements AddressRepository { } } - private void update(BitmessageAddress address) throws IOException { + private void update(BitmessageAddress address) { try { SQLiteDatabase db = sql.getWritableDatabase(); // Create a new map of values, where column names are the keys @@ -224,7 +220,7 @@ public class AndroidAddressRepository implements AddressRepository { } } - private void insert(BitmessageAddress address) throws IOException { + private void insert(BitmessageAddress address) { try { SQLiteDatabase db = sql.getWritableDatabase(); // Create a new map of values, where column names are the keys diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java index f430f66..8c6aad8 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java @@ -275,14 +275,12 @@ public class AndroidMessageRepository extends AbstractMessageRepository { db.setTransactionSuccessful(); } catch (SQLiteConstraintException e) { LOG.trace(e.getMessage(), e); - } catch (IOException e) { - LOG.error(e.getMessage(), e); } finally { db.endTransaction(); } } - private void insert(SQLiteDatabase db, Plaintext message) throws IOException { + private void insert(SQLiteDatabase db, Plaintext message) { ContentValues values = new ContentValues(); values.put(COLUMN_IV, message.getInventoryVector() == null ? null : message .getInventoryVector().getHash()); @@ -302,7 +300,7 @@ public class AndroidMessageRepository extends AbstractMessageRepository { message.setId(id); } - private void update(SQLiteDatabase db, Plaintext message) throws IOException { + private void update(SQLiteDatabase db, Plaintext message) { ContentValues values = new ContentValues(); values.put(COLUMN_IV, message.getInventoryVector() == null ? null : message .getInventoryVector().getHash()); diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/SqlHelper.java b/app/src/main/java/ch/dissem/apps/abit/repository/SqlHelper.java index 87a2bc2..5e6a68c 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/SqlHelper.java +++ b/app/src/main/java/ch/dissem/apps/abit/repository/SqlHelper.java @@ -27,10 +27,10 @@ import ch.dissem.apps.abit.util.Assets; */ public class SqlHelper extends SQLiteOpenHelper { // If you change the database schema, you must increment the database version. - public static final int DATABASE_VERSION = 5; - public static final String DATABASE_NAME = "jabit.db"; + private static final int DATABASE_VERSION = 5; + private static final String DATABASE_NAME = "jabit.db"; - protected final Context ctx; + private final Context ctx; public SqlHelper(Context ctx) { super(ctx, DATABASE_NAME, null, DATABASE_VERSION); @@ -65,7 +65,7 @@ public class SqlHelper extends SQLiteOpenHelper { } } - protected void executeMigration(SQLiteDatabase db, String name) { + private void executeMigration(SQLiteDatabase db, String name) { for (String statement : Assets.readSqlStatements(ctx, "db/migration/" + name + ".sql")) { db.execSQL(statement); } diff --git a/app/src/main/java/ch/dissem/apps/abit/service/ProofOfWorkService.java b/app/src/main/java/ch/dissem/apps/abit/service/ProofOfWorkService.java index c01f5c3..3b41df7 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/ProofOfWorkService.java +++ b/app/src/main/java/ch/dissem/apps/abit/service/ProofOfWorkService.java @@ -37,9 +37,9 @@ import static ch.dissem.apps.abit.notification.ProofOfWorkNotification.ONGOING_N */ public class ProofOfWorkService extends Service { // Object to use as a thread-safe lock - private static ProofOfWorkEngine engine = new MultiThreadedPOWEngine(); - private static boolean calculating; + private static final ProofOfWorkEngine engine = new MultiThreadedPOWEngine(); private static final Queue<PowItem> queue = new LinkedList<>(); + private static boolean calculating; private ProofOfWorkNotification notification; @Override diff --git a/app/src/main/java/ch/dissem/apps/abit/service/ServicePowEngine.java b/app/src/main/java/ch/dissem/apps/abit/service/ServicePowEngine.java index 7211dcf..b7f0a84 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/ServicePowEngine.java +++ b/app/src/main/java/ch/dissem/apps/abit/service/ServicePowEngine.java @@ -38,14 +38,14 @@ public class ServicePowEngine implements ProofOfWorkEngine { private final Context ctx; private static final Object lock = new Object(); - private Queue<PowItem> queue = new LinkedList<>(); + private final Queue<PowItem> queue = new LinkedList<>(); private PowBinder service; public ServicePowEngine(Context ctx) { this.ctx = ctx; } - private ServiceConnection connection = new ServiceConnection() { + private final ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { synchronized (lock) { @@ -75,4 +75,4 @@ public class ServicePowEngine implements ProofOfWorkEngine { } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java index 0653559..e5d52ba 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java +++ b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java @@ -20,9 +20,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import java.io.IOException; import java.net.InetAddress;