Skip to content

Commit

Permalink
debug: added assertions and null testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Elman committed Sep 21, 2020
1 parent 611030a commit 23ea786
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Date;
import java.util.Objects;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
Expand Down Expand Up @@ -146,9 +147,6 @@ public void start(Context c) {
public class CatalogImportTask extends AsyncTask<Void, Integer, Void> {
private long bytesToBeImported;

private CatalogImportTask() {
}

@Override
protected void onPreExecute() {
// Proxy the call to the Activity
Expand Down Expand Up @@ -176,7 +174,7 @@ protected Void doInBackground(Void... ignore) {
if (SharedObjects.getInstance().preferences == null) {
// Prepare Shared Objects
SharedObjects.getInstance().preferences = PreferenceManager.getDefaultSharedPreferences
(getActivity().getApplicationContext());
(Objects.requireNonNull(getActivity()).getApplicationContext());
}

SharedPreferences preferences = SharedObjects.getInstance().preferences;
Expand All @@ -190,7 +188,7 @@ protected Void doInBackground(Void... ignore) {
PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
if (pm != null) {
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, S.WAKE_LOCK_TAG);
wakeLock.acquire(10*60*1000L /*10 minutes*/);
wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/);

if (S.DEBUG)
Log.d(S.TAG, "Wake Lock acquired.");
Expand All @@ -199,6 +197,7 @@ protected Void doInBackground(Void... ignore) {
// Open file for size check
try {
// Fix for Windows backslashes
assert settingCatalogLocation != null;
settingCatalogLocation = settingCatalogLocation.replace("\\", "/");
sourceCatalog = new File(settingCatalogLocation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import com.holdingscythe.pocketamcreader.catalog.MoviesDataProvider;
import com.holdingscythe.pocketamcreader.filters.Filter;
import com.holdingscythe.pocketamcreader.filters.FilterField;
import com.holdingscythe.pocketamcreader.filters.FilterOperator;
import com.holdingscythe.pocketamcreader.filters.Filters;
import com.holdingscythe.pocketamcreader.settings.SettingsConstants;
import com.holdingscythe.pocketamcreader.utils.SharedObjects;
Expand Down Expand Up @@ -75,8 +74,6 @@ public class MovieDetailFragment extends Fragment implements OnClickListener {
private Extras mExtras;
private Filters mFilters;
private String mPicturesFolder;
private int mDeviceWidthPixels;
private int mActionBarHeight;
private int mHeroImageHeight;

private Pattern regExpMultivaluedCleaner;
Expand Down Expand Up @@ -112,7 +109,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// Prepare custom font for the toolbar
final Typeface tf = Typeface.createFromAsset(getContext().getAssets(), S.DEFAULT_FONT);
final Typeface tf = Typeface.createFromAsset(Objects.requireNonNull(getContext()).getAssets(), S.DEFAULT_FONT);

// Setup filters
mFilters = new Filters();
Expand All @@ -122,11 +119,13 @@ public void onActivityCreated(Bundle savedInstanceState) {
if (SharedObjects.getInstance().preferences == null) {
// Prepare Shared Objects
SharedObjects.getInstance().preferences = PreferenceManager.getDefaultSharedPreferences
(getActivity().getApplicationContext());
(Objects.requireNonNull(getActivity()).getApplicationContext());
}

SharedPreferences preferences = SharedObjects.getInstance().preferences;
String settingMultivaluedSeparator = preferences.getString(SettingsConstants.KEY_PREF_DETAIL_SEPARATOR, ",/");
String settingMultivaluedSeparator =
Utils.coalesce(preferences.getString(SettingsConstants.KEY_PREF_DETAIL_SEPARATOR,
",/"), ",/");
mPicturesFolder = preferences.getString(SettingsConstants.KEY_PREF_PICTURES_FOLDER, "/");

if (getArguments() != null && getArguments().containsKey(ARG_MOVIE_ID)) {
Expand All @@ -149,7 +148,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
Uri uri = Uri.withAppendedPath(S.CONTENT_URI, "_id/" + getArguments().getString(ARG_MOVIE_ID, "0"));
Cursor cursor = moviesDataProvider.fetchMovie(uri);
cursor.moveToFirst();
mMovie = new Movie(cursor, getView(), this, getActivity());
mMovie = new Movie(cursor, getView(), this, Objects.requireNonNull(getActivity()));
cursor.close();

// Fetch custom fields
Expand All @@ -168,7 +167,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
moviesDataProvider.closeDatabase();

// Set collapsing toolbar title
CollapsingToolbarLayout collapsingToolbar = getView().findViewById(R.id.collapsingToolbar);
CollapsingToolbarLayout collapsingToolbar = Objects.requireNonNull(getView()).findViewById(R.id.collapsingToolbar);
AppBarLayout appBarLayout = getView().findViewById(R.id.appBarLayout);

if (collapsingToolbar != null && appBarLayout != null) {
Expand All @@ -194,8 +193,8 @@ public void onActivityCreated(Bundle savedInstanceState) {
(int) getResources().getDimension(R.dimen.detail_hero_img_height);
}

mDeviceWidthPixels = SharedObjects.getInstance().deviceWidthPixels;
mActionBarHeight = SharedObjects.getInstance().actionBarHeight;
int mDeviceWidthPixels = SharedObjects.getInstance().deviceWidthPixels;
int mActionBarHeight = SharedObjects.getInstance().actionBarHeight;
mHeroImageHeight = SharedObjects.getInstance().heroImageHeight;

// Set custom font to the toolbar
Expand Down Expand Up @@ -244,7 +243,7 @@ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange + verticalOffset == 0) {
// when collapsingToolbar is collapsed, display actionbar title
int color = Utils.getColorFromColorTag(mMovie.getColorTag());
collapsingToolbar.setCollapsedTitleTextColor(ContextCompat.getColor(getContext(), color));
collapsingToolbar.setCollapsedTitleTextColor(ContextCompat.getColor(Objects.requireNonNull(getContext()), color));
collapsingToolbar.setTitle(mMovie.getTitle());
isShow = true;
} else if (isShow) {
Expand Down Expand Up @@ -326,25 +325,25 @@ public void onClick(View view) {
break;

case R.id.Category:
chooseMultivaluedFieldValue(Movies.FILTER_CATEGORY, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_CATEGORY, view);
break;
case R.id.Director:
chooseMultivaluedFieldValue(Movies.FILTER_DIRECTOR, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_DIRECTOR, view);
break;
case R.id.Actors:
chooseMultivaluedFieldValue(Movies.FILTER_ACTORS, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_ACTORS, view);
break;
case R.id.Producer:
chooseMultivaluedFieldValue(Movies.FILTER_PRODUCER, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_PRODUCER, view);
break;
case R.id.Writer:
chooseMultivaluedFieldValue(Movies.FILTER_WRITER, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_WRITER, view);
break;
case R.id.Composer:
chooseMultivaluedFieldValue(Movies.FILTER_COMPOSER, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_COMPOSER, view);
break;
case R.id.Country:
chooseMultivaluedFieldValue(Movies.FILTER_COUNTRY, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_COUNTRY, view);
break;

case R.id.MediaLabel:
Expand All @@ -365,10 +364,10 @@ public void onClick(View view) {
break;

case R.id.Languages:
chooseMultivaluedFieldValue(Movies.FILTER_LANGUAGES, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_LANGUAGES, view);
break;
case R.id.Subtitles:
chooseMultivaluedFieldValue(Movies.FILTER_SUBTITLES, Movies.FILTER_OPERATOR_CONTAINS, view);
chooseMultivaluedFieldValue(Movies.FILTER_SUBTITLES, view);
break;
case R.id.VideoFormat:
filterClick(new Filter(Movies.FILTER_VIDEO_FORMAT, Movies.FILTER_OPERATOR_EQUALS,
Expand Down Expand Up @@ -477,19 +476,20 @@ public void onClick(View view) {
/**
* Prepare select to pick from available values in multivalued field
*/
private void chooseMultivaluedFieldValue(final FilterField field, final FilterOperator operator, View v) {
private void chooseMultivaluedFieldValue(final FilterField field, View v) {
final String[] availableValues = separateMultivaluedField(v);

// If only one field is available, make direct filter
if (availableValues.length == 1) {
filterClick(new Filter(field, operator, availableValues[0]));
filterClick(new Filter(field, Movies.FILTER_OPERATOR_CONTAINS, availableValues[0]));
} else if (availableValues.length > 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(mFilters.getFilterFieldHumanName(field.resId) + " " + getString(operator.resId));
builder.setTitle(mFilters.getFilterFieldHumanName(field.resId) + " " + getString(Movies.FILTER_OPERATOR_CONTAINS.resId));
builder.setItems(availableValues, (dialog, item) -> filterClick(new Filter(field,
operator, availableValues[item])));
Movies.FILTER_OPERATOR_CONTAINS, availableValues[item])));
builder.setNegativeButton(getString(R.string.dialog_negative),
(dialog, whichButton) -> {});
(dialog, whichButton) -> {
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.Objects;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import io.github.inflationx.calligraphy3.CalligraphyConfig;
Expand Down Expand Up @@ -112,8 +114,8 @@ protected void onCreate(Bundle savedInstanceState) {
mTwoPane = SharedObjects.getInstance().twoPane = true;

// In two-pane mode, list items should be given the 'activated' state when touched.
((MovieListFragment) getSupportFragmentManager()
.findFragmentById(R.id.movie_list_container))
((MovieListFragment) Objects.requireNonNull(getSupportFragmentManager()
.findFragmentById(R.id.movie_list_container)))
.setActivateOnItemClick(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.SearchView;
Expand Down Expand Up @@ -218,7 +219,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
if (SharedObjects.getInstance().preferences == null) {
// Prepare Shared Objects
SharedObjects.getInstance().preferences = PreferenceManager.getDefaultSharedPreferences
(getActivity().getApplicationContext());
(Objects.requireNonNull(getActivity()).getApplicationContext());
}

// Setup filters from bundle if available
Expand All @@ -232,6 +233,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
} else {
mFilters = new Filters();
}
assert mFilters != null;
mFilters.setContext(getActivity());

// Setup filter query from bundle
Expand All @@ -244,10 +246,10 @@ public void onActivityCreated(Bundle savedInstanceState) {
mMoviesDataProvider.setFilters(mFilters);

// Prepare recycler view
mRecyclerView = (FastScrollRecyclerView) getView().findViewById(R.id.movie_list_recycler);
mRecyclerView = (FastScrollRecyclerView) Objects.requireNonNull(getView()).findViewById(R.id.movie_list_recycler);

// Define layout managers
mLinearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
mLinearLayoutManager = new LinearLayoutManager(Objects.requireNonNull(getActivity()).getApplicationContext());
int gridSpan = (getActivity().getResources().getConfiguration().orientation == Configuration
.ORIENTATION_PORTRAIT ? GRID_SPAN : GRID_SPAN_LANDSCAPE);
mGridLayoutManager = new GridLayoutManager(getActivity().getApplicationContext(), gridSpan);
Expand Down Expand Up @@ -413,14 +415,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
mViewType = LIST;
prepareMoviesAdapter();
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.setAdapter(mMoviesAdapter);
} else {
// Change to grid view
mViewType = GRID;
prepareMoviesAdapter();
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setAdapter(mMoviesAdapter);
}
mRecyclerView.setAdapter(mMoviesAdapter);

// Set filter query from previous view
if (!mFilterQuery.equals(""))
Expand Down Expand Up @@ -667,7 +668,7 @@ private void refreshList() {
if (mMoviesDataProvider != null && mRecyclerView != null) {
if (SharedObjects.getInstance().preferences == null) {
SharedObjects.getInstance().preferences = PreferenceManager.getDefaultSharedPreferences
(getActivity().getApplicationContext());
(Objects.requireNonNull(getActivity()).getApplicationContext());
}

// Close search field
Expand All @@ -676,7 +677,7 @@ private void refreshList() {

// Swap cursor
mMoviesAdapter.stopImageLoader();
mMoviesAdapter.loadConfiguration(getActivity().getBaseContext());
mMoviesAdapter.loadConfiguration(Objects.requireNonNull(getActivity()).getBaseContext());
Cursor oldCursor = mMoviesAdapter.swapCursor(mMoviesDataProvider.fetchMovies(S.CONTENT_URI));
if (oldCursor != null)
oldCursor.close();
Expand Down Expand Up @@ -711,7 +712,8 @@ private void addFilter() {
addFilterSecondStep(Movies.availableFilterFields[item]);
}
});
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {});
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Expand All @@ -724,7 +726,8 @@ private void addFilterSecondStep(final FilterField field) {
builder.setTitle(mFilters.getFilterFieldHumanName(field.resId));
builder.setItems(mFilters.getAvailableOperators(field),
(dialog, item) -> addFilterThirdStep(field, field.type.operators[item]));
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {});
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
Expand All @@ -742,7 +745,7 @@ private void addFilterThirdStep(final FilterField field, final FilterOperator op

// Date picker is handled different way
if (field.type == Movies.FILTER_TYPE_DATE) {
new DatePickerDialog(getActivity(), new FilterDatePickerListener(field, operator),
new DatePickerDialog(Objects.requireNonNull(getActivity()), new FilterDatePickerListener(field, operator),
mDpYear, mDpMonth, mDpDay).show();
return;
}
Expand All @@ -761,7 +764,7 @@ private void addFilterThirdStep(final FilterField field, final FilterOperator op
});
} else {
// Prepare view with text input
View filterView = getActivity().getLayoutInflater().inflate(R.layout.filter_input, null);
View filterView = Objects.requireNonNull(getActivity()).getLayoutInflater().inflate(R.layout.filter_input, null);
final EditText filterValueView = filterView.findViewById(R.id.filterValue);
if (field.type == Movies.FILTER_TYPE_NUMBER) {
DigitsKeyListener digitsKeyListener = new DigitsKeyListener(false, true);
Expand All @@ -778,21 +781,22 @@ private void addFilterThirdStep(final FilterField field, final FilterOperator op
}

// Negative button is for both options
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {});
builder.setNegativeButton(getString(R.string.dialog_negative), (dialog, whichButton) -> {
});

AlertDialog alertDialog = builder.create();

alertDialog.setOnShowListener(dialog -> {
if (field.type == Movies.FILTER_TYPE_TEXT || field.type == Movies.FILTER_TYPE_NUMBER) {
InputMethodManager imm =
(InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
(InputMethodManager) Objects.requireNonNull(getActivity()).getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
alertDialog.setOnDismissListener(dialog -> {
if (field.type == Movies.FILTER_TYPE_TEXT || field.type == Movies.FILTER_TYPE_NUMBER) {
InputMethodManager imm =
(InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);
(InputMethodManager) Objects.requireNonNull(getActivity()).getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
});
Expand Down Expand Up @@ -846,7 +850,7 @@ private void removeFilters() {
*/
private void prepareMoviesAdapter() {
mMoviesAdapter = new MoviesAdapter(
getActivity().getBaseContext(),
Objects.requireNonNull(getActivity()).getBaseContext(),
mMoviesDataProvider.query(S.CONTENT_URI, SharedObjects.getInstance().moviesProjection, null, null,
null),
mViewType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ private void init(Cursor c) {

/**
* This method will move the Cursor to the correct position and call
* {@link #onBindViewHolderCursor(android.support.v7.widget.RecyclerView.ViewHolder,
* android.database.Cursor)}.
*
* @param holder {@inheritDoc}
* @param i {@inheritDoc}
Expand All @@ -104,7 +102,6 @@ public void onBindViewHolder(@NonNull VH holder, int i) {
/**
* See {@link android.widget.CursorAdapter#bindView(android.view.View, android.content.Context,
* android.database.Cursor)},
* {@link #onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder, int)}
*
* @param holder View holder.
* @param cursor The cursor from which to get the data. The cursor is already
Expand Down
Loading

0 comments on commit 23ea786

Please sign in to comment.