Changes suggested by Codacy

This commit is contained in:
Paul Hawke 2017-07-22 14:12:35 -05:00
parent 9c987efbd2
commit eaffed2471
4 changed files with 54 additions and 26 deletions

View file

@ -35,8 +35,8 @@ class CategoriesRenderer extends Renderer<CategoryItem> {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
CategoryItem item = getContent(); CategoryItem item = getContent();
item.selected = !item.selected; item.setSelected(!item.isSelected());
checkedView.setChecked(item.selected); checkedView.setChecked(item.isSelected());
if (listener != null) { if (listener != null) {
listener.categoryClicked(item); listener.categoryClicked(item);
} }
@ -47,8 +47,8 @@ class CategoriesRenderer extends Renderer<CategoryItem> {
@Override @Override
public void render() { public void render() {
CategoryItem item = getContent(); CategoryItem item = getContent();
checkedView.setChecked(item.selected); checkedView.setChecked(item.isSelected());
checkedView.setText(item.name); checkedView.setText(item.getName());
} }
interface CategoryClickedListener { interface CategoryClickedListener {

View file

@ -42,13 +42,18 @@ import java.util.concurrent.TimeUnit;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.category.CategoriesRenderer.CategoryClickedListener;
import fr.free.nrw.commons.upload.MwVolleyApi; import fr.free.nrw.commons.upload.MwVolleyApi;
import timber.log.Timber; import timber.log.Timber;
import static android.view.KeyEvent.ACTION_UP;
import static android.view.KeyEvent.KEYCODE_BACK;
import static fr.free.nrw.commons.category.CategoryContentProvider.AUTHORITY;
/** /**
* Displays the category suggestion and selection screen. Category search is initiated here. * Displays the category suggestion and selection screen. Category search is initiated here.
*/ */
public class CategorizationFragment extends Fragment implements CategoriesRenderer.CategoryClickedListener { public class CategorizationFragment extends Fragment implements CategoryClickedListener {
public static final int SEARCH_CATS_LIMIT = 25; public static final int SEARCH_CATS_LIMIT = 25;
@BindView(R.id.categoriesListBox) RecyclerView categoriesList; @BindView(R.id.categoriesListBox) RecyclerView categoriesList;
@ -74,7 +79,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@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_categorization, container, false); View rootView = inflater.inflate(R.layout.fragment_categorization, container, false);
ButterKnife.bind(this, rootView); ButterKnife.bind(this, rootView);
@ -94,7 +100,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
categoriesCache = new HashMap<>(); categoriesCache = new HashMap<>();
} else { } else {
items = savedInstanceState.getParcelableArrayList("currentCategories"); items = savedInstanceState.getParcelableArrayList("currentCategories");
categoriesCache = (HashMap<String, ArrayList<String>>) savedInstanceState.getSerializable("categoriesCache"); categoriesCache = (HashMap<String, ArrayList<String>>) savedInstanceState
.getSerializable("categoriesCache");
} }
categoriesAdapter = adapterFactory.create(items); categoriesAdapter = adapterFactory.create(items);
@ -123,7 +130,7 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
rootView.setOnKeyListener(new View.OnKeyListener() { rootView.setOnKeyListener(new View.OnKeyListener() {
@Override @Override
public boolean onKey(View v, int keyCode, KeyEvent event) { public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) { if (event.getAction() == ACTION_UP && keyCode == KEYCODE_BACK) {
backButtonDialog(); backButtonDialog();
return true; return true;
} }
@ -168,8 +175,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
int count = categoriesAdapter.getItemCount(); int count = categoriesAdapter.getItemCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
CategoryItem item = categoriesAdapter.getItem(i); CategoryItem item = categoriesAdapter.getItem(i);
if (item.selected) { if (item.isSelected()) {
selectedCategories.add(item.name); selectedCategories.add(item.getName());
numberSelected++; numberSelected++;
} }
} }
@ -177,7 +184,9 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
//If no categories selected, display warning to user //If no categories selected, display warning to user
if (numberSelected == 0) { if (numberSelected == 0) {
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(getActivity())
.setMessage("Images without categories are rarely usable. Are you sure you want to submit without selecting categories?") .setMessage("Images without categories are rarely usable. "
+ "Are you sure you want to submit without selecting "
+ "categories?")
.setTitle("No Categories Selected") .setTitle("No Categories Selected")
.setPositiveButton("No, go back", new DialogInterface.OnClickListener() { .setPositiveButton("No, go back", new DialogInterface.OnClickListener() {
@Override @Override
@ -209,7 +218,7 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
setHasOptionsMenu(true); setHasOptionsMenu(true);
onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity(); onCategoriesSaveHandler = (OnCategoriesSaveHandler) getActivity();
getActivity().setTitle(R.string.categories_activity_title); getActivity().setTitle(R.string.categories_activity_title);
client = getActivity().getContentResolver().acquireContentProviderClient(CategoryContentProvider.AUTHORITY); client = getActivity().getContentResolver().acquireContentProviderClient(AUTHORITY);
} }
public HashMap<String, ArrayList<String>> getCategoriesCache() { public HashMap<String, ArrayList<String>> getCategoriesCache() {
@ -285,7 +294,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
} }
/** /**
* Merges nearby categories, categories suggested based on title, and recent categories... without duplicates. * Merges nearby categories, categories suggested based on title, and recent categories...
* without duplicates.
* *
* @return a list containing merged categories * @return a list containing merged categories
*/ */
@ -316,7 +326,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
mergedItems.addAll(recentItems); mergedItems.addAll(recentItems);
Timber.d("Adding recent items: %s", recentItems); Timber.d("Adding recent items: %s", recentItems);
//Needs to be an ArrayList and not a List unless we want to modify a big portion of preexisting code // Needs to be an ArrayList and not a List unless we want to modify a big portion
// of preexisting code
ArrayList<String> mergedItemsList = new ArrayList<>(mergedItems); ArrayList<String> mergedItemsList = new ArrayList<>(mergedItems);
Timber.d("Merged item list: %s", mergedItemsList); Timber.d("Merged item list: %s", mergedItemsList);
@ -336,9 +347,9 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
int count = categoriesAdapter.getItemCount(); int count = categoriesAdapter.getItemCount();
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
CategoryItem item = categoriesAdapter.getItem(i); CategoryItem item = categoriesAdapter.getItem(i);
if (item.selected) { if (item.isSelected()) {
items.add(item); items.add(item);
existingKeys.add(item.name); existingKeys.add(item.getName());
} }
} }
for (String category : categories) { for (String category : categories) {
@ -442,7 +453,7 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
int numberOfItems = categoriesAdapter.getItemCount(); int numberOfItems = categoriesAdapter.getItemCount();
for (int i = 0; i < numberOfItems; i++) { for (int i = 0; i < numberOfItems; i++) {
CategoryItem item = categoriesAdapter.getItem(i); CategoryItem item = categoriesAdapter.getItem(i);
if (item.selected) { if (item.isSelected()) {
count++; count++;
} }
} }
@ -451,7 +462,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
public void backButtonDialog() { public void backButtonDialog() {
new AlertDialog.Builder(getActivity()) new AlertDialog.Builder(getActivity())
.setMessage("Are you sure you want to go back? The image will not have any categories saved.") .setMessage("Are you sure you want to go back? The image will not "
+ "have any categories saved.")
.setTitle("Warning") .setTitle("Warning")
.setPositiveButton("No", new DialogInterface.OnClickListener() { .setPositiveButton("No", new DialogInterface.OnClickListener() {
@Override @Override
@ -471,8 +483,8 @@ public class CategorizationFragment extends Fragment implements CategoriesRender
@Override @Override
public void categoryClicked(CategoryItem item) { public void categoryClicked(CategoryItem item) {
if (item.selected) { if (item.isSelected()) {
new CategoryCountUpdater(item.name, client).executeOnExecutor(executor); new CategoryCountUpdater(item.getName(), client).executeOnExecutor(executor);
} }
} }

View file

@ -44,7 +44,8 @@ public class CategoryContentProvider extends ContentProvider {
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
@Override @Override
public Cursor query(@NonNull Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { public Cursor query(@NonNull Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(Category.Table.TABLE_NAME); queryBuilder.setTables(Category.Table.TABLE_NAME);
@ -55,7 +56,8 @@ public class CategoryContentProvider extends ContentProvider {
switch(uriType) { switch(uriType) {
case CATEGORIES: case CATEGORIES:
cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder); cursor = queryBuilder.query(db, projection, selection, selectionArgs,
null, null, sortOrder);
break; break;
case CATEGORIES_ID: case CATEGORIES_ID:
cursor = queryBuilder.query(db, cursor = queryBuilder.query(db,
@ -128,7 +130,8 @@ public class CategoryContentProvider extends ContentProvider {
@SuppressWarnings("ConstantConditions") @SuppressWarnings("ConstantConditions")
@Override @Override
public int update(@NonNull Uri uri, ContentValues contentValues, String selection, String[] selectionArgs) { public int update(@NonNull Uri uri, ContentValues contentValues, String selection,
String[] selectionArgs) {
/* /*
SQL Injection warnings: First, note that we're not exposing this to the outside world (exported="false") SQL Injection warnings: First, note that we're not exposing this to the outside world (exported="false")
Even then, we should make sure to sanitize all user input appropriately. Input that passes through ContentValues Even then, we should make sure to sanitize all user input appropriately. Input that passes through ContentValues
@ -149,7 +152,8 @@ public class CategoryContentProvider extends ContentProvider {
Category.Table.COLUMN_ID + " = ?", Category.Table.COLUMN_ID + " = ?",
new String[] { String.valueOf(id) } ); new String[] { String.valueOf(id) } );
} else { } else {
throw new IllegalArgumentException("Parameter `selection` should be empty when updating an ID"); throw new IllegalArgumentException(
"Parameter `selection` should be empty when updating an ID");
} }
break; break;
default: default:

View file

@ -4,8 +4,8 @@ import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
class CategoryItem implements Parcelable { class CategoryItem implements Parcelable {
public final String name; private final String name;
public boolean selected; private boolean selected;
public static Creator<CategoryItem> CREATOR = new Creator<CategoryItem>() { public static Creator<CategoryItem> CREATOR = new Creator<CategoryItem>() {
@Override @Override
@ -29,6 +29,18 @@ class CategoryItem implements Parcelable {
selected = in.readInt() == 1; selected = in.readInt() == 1;
} }
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
@Override @Override
public int describeContents() { public int describeContents() {
return 0; return 0;