Java 5 / Java 8 language suggestions plus general cleanup.

This commit is contained in:
Paul Hawke 2017-09-03 17:27:20 -05:00
parent 13e84a82b8
commit 8cbf56fa7b
6 changed files with 62 additions and 76 deletions

View file

@ -31,16 +31,13 @@ class CategoriesRenderer extends Renderer<CategoryItem> {
@Override @Override
protected void hookListeners(View view) { protected void hookListeners(View view) {
view.setOnClickListener(new View.OnClickListener() { view.setOnClickListener(v -> {
@Override
public void onClick(View v) {
CategoryItem item = getContent(); CategoryItem item = getContent();
item.setSelected(!item.isSelected()); item.setSelected(!item.isSelected());
checkedView.setChecked(item.isSelected()); checkedView.setChecked(item.isSelected());
if (listener != null) { if (listener != null) {
listener.categoryClicked(item); listener.categoryClicked(item);
} }
}
}); });
} }

View file

@ -127,7 +127,7 @@ public class Contribution extends Media {
} }
public String getPageContents() { public String getPageContents() {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH); SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
buffer buffer

View file

@ -33,7 +33,7 @@ public class CategoryModifier extends PageModifier {
JSONArray categories; JSONArray categories;
categories = params.optJSONArray(PARAM_CATEGORIES); categories = params.optJSONArray(PARAM_CATEGORIES);
StringBuffer categoriesString = new StringBuffer(); StringBuilder categoriesString = new StringBuilder();
for(int i=0; i < categories.length(); i++) { for(int i=0; i < categories.length(); i++) {
String category = categories.optString(i); String category = categories.optString(i);
categoriesString.append("\n[[Category:").append(category).append("]]"); categoriesString.append("\n[[Category:").append(category).append("]]");

View file

@ -48,7 +48,7 @@ public class ModifierSequence {
} }
public String getEditSummary() { public String getEditSummary() {
StringBuffer editSummary = new StringBuffer(); StringBuilder editSummary = new StringBuilder();
for(PageModifier modifier: modifiers) { for(PageModifier modifier: modifiers) {
editSummary.append(modifier.getEditSumary()).append(" "); editSummary.append(modifier.getEditSumary()).append(" ");
} }

View file

@ -1,7 +1,6 @@
package fr.free.nrw.commons.upload; package fr.free.nrw.commons.upload;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
@ -39,13 +38,10 @@ import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.settings.Prefs; import fr.free.nrw.commons.settings.Prefs;
import timber.log.Timber; import timber.log.Timber;
public class SingleUploadFragment extends Fragment { import static android.view.MotionEvent.ACTION_DOWN;
private SharedPreferences prefs; import static android.view.MotionEvent.ACTION_UP;
private String license;
public interface OnUploadActionInitiated { public class SingleUploadFragment extends Fragment {
void uploadActionInitiated(String title, String description);
}
@BindView(R.id.titleEdit) EditText titleEdit; @BindView(R.id.titleEdit) EditText titleEdit;
@BindView(R.id.descEdit) EditText descEdit; @BindView(R.id.descEdit) EditText descEdit;
@ -53,6 +49,8 @@ public class SingleUploadFragment extends Fragment {
@BindView(R.id.share_license_summary) TextView licenseSummaryView; @BindView(R.id.share_license_summary) TextView licenseSummaryView;
@BindView(R.id.licenseSpinner) Spinner licenseSpinner; @BindView(R.id.licenseSpinner) Spinner licenseSpinner;
private SharedPreferences prefs;
private String license;
private OnUploadActionInitiated uploadActionInitiatedHandler; private OnUploadActionInitiated uploadActionInitiatedHandler;
private TitleTextWatcher textWatcher = new TitleTextWatcher(); private TitleTextWatcher textWatcher = new TitleTextWatcher();
@ -89,7 +87,7 @@ public class SingleUploadFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_single_upload, null); View rootView = inflater.inflate(R.layout.fragment_single_upload, container, false);
ButterKnife.bind(this, rootView); ButterKnife.bind(this, rootView);
@ -146,7 +144,8 @@ public class SingleUploadFragment extends Fragment {
super.onDestroyView(); super.onDestroyView();
} }
@OnItemSelected(R.id.licenseSpinner) void onLicenseSelected(AdapterView<?> parent, View view, int position, long id) { @OnItemSelected(R.id.licenseSpinner)
void onLicenseSelected(AdapterView<?> parent, View view, int position, long id) {
String licenseName = parent.getItemAtPosition(position).toString(); String licenseName = parent.getItemAtPosition(position).toString();
// Set selected color to white because it should be readable on random images. // Set selected color to white because it should be readable on random images.
@ -177,10 +176,9 @@ public class SingleUploadFragment extends Fragment {
editor.commit(); editor.commit();
} }
@OnTouch(R.id.share_license_summary)
boolean showLicence(View view, MotionEvent motionEvent) {
@OnTouch(R.id.share_license_summary) boolean showLicence(View view, MotionEvent motionEvent) { if (motionEvent.getActionMasked() == ACTION_DOWN) {
if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
Intent intent = new Intent(); Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW); intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Utils.licenseUrlFor(license))); intent.setData(Uri.parse(Utils.licenseUrlFor(license)));
@ -191,7 +189,8 @@ public class SingleUploadFragment extends Fragment {
} }
} }
@OnClick(R.id.titleDescButton) void setTitleDescButton() { @OnClick(R.id.titleDescButton)
void setTitleDescButton() {
//Retrieve last title and desc entered //Retrieve last title and desc entered
SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity()); SharedPreferences titleDesc = PreferenceManager.getDefaultSharedPreferences(getActivity());
String title = titleDesc.getString("Title", ""); String title = titleDesc.getString("Title", "");
@ -205,57 +204,41 @@ public class SingleUploadFragment extends Fragment {
/** /**
* Copied from https://stackoverflow.com/a/26269435/8065933 * Copied from https://stackoverflow.com/a/26269435/8065933
*/ */
@OnTouch @OnTouch(R.id.titleEdit)
(R.id.titleEdit) boolean titleInfo(View view, MotionEvent motionEvent) { boolean titleInfo(View view, MotionEvent motionEvent) {
//Should replace right with end to support different right-to-left languages as well //Should replace right with end to support different right-to-left languages as well
final int value = titleEdit.getRight() - titleEdit.getCompoundDrawables()[2].getBounds().width(); final int value = titleEdit.getRight() - titleEdit.getCompoundDrawables()[2].getBounds().width();
if (motionEvent.getAction() == motionEvent.ACTION_UP && motionEvent.getRawX() >= value) { if (motionEvent.getAction() == ACTION_UP && motionEvent.getRawX() >= value) {
new AlertDialog.Builder(getContext())
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); .setTitle(R.string.media_detail_title)
builder.setTitle(R.string.media_detail_title); .setMessage(R.string.title_info)
builder.setMessage(R.string.title_info); .setCancelable(true)
builder.setCancelable(true); .setNeutralButton(android.R.string.ok, (dialog, id) -> dialog.cancel())
builder.setNeutralButton(android.R.string.ok, .create()
new DialogInterface.OnClickListener() { .show();
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true; return true;
} }
return false; return false;
} }
@OnTouch @OnTouch(R.id.descEdit)
(R.id.descEdit) boolean descriptionInfo(View view, MotionEvent motionEvent) { boolean descriptionInfo(View view, MotionEvent motionEvent) {
final int value = descEdit.getRight() - descEdit.getCompoundDrawables()[2].getBounds().width(); final int value = descEdit.getRight() - descEdit.getCompoundDrawables()[2].getBounds().width();
if (motionEvent.getAction() == motionEvent.ACTION_UP && motionEvent.getRawX() >= value) { if (motionEvent.getAction() == ACTION_UP && motionEvent.getRawX() >= value) {
new AlertDialog.Builder(getContext())
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); .setTitle(R.string.media_detail_description)
builder.setTitle(R.string.media_detail_description); .setMessage(R.string.description_info)
builder.setMessage(R.string.description_info); .setCancelable(true)
builder.setCancelable(true); .setNeutralButton(android.R.string.ok, (dialog, id) -> dialog.cancel())
builder.setNeutralButton(android.R.string.ok, .create()
new DialogInterface.OnClickListener() { .show();
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
return true; return true;
} }
return false; return false;
} }
private void setLicenseSummary(String license) { private void setLicenseSummary(String license) {
licenseSummaryView.setText(getString(R.string.share_license_summary, getString(Utils.licenseNameFor(license)))); licenseSummaryView.setText(getString(R.string.share_license_summary, getString(Utils.licenseNameFor(license))));
} }
@ -279,12 +262,18 @@ public class SingleUploadFragment extends Fragment {
} }
} }
public interface OnUploadActionInitiated {
void uploadActionInitiated(String title, String description);
}
private class TitleTextWatcher implements TextWatcher { private class TitleTextWatcher implements TextWatcher {
@Override @Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { } public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override @Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { } public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) {

View file

@ -15,7 +15,7 @@ public class FileUtils {
* @return the content of the file * @return the content of the file
*/ */
public static String readFromResource(String fileName) throws IOException { public static String readFromResource(String fileName) throws IOException {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
BufferedReader reader = null; BufferedReader reader = null;
try { try {
reader = new BufferedReader( reader = new BufferedReader(