app/src/main/java/io/github/ilya_lebedev/popularmovies/sync/MovieFetchTask.java
/**
     * Performs the network request for fetch movie data, parse the JSON
     * from that request and inserts the new movie information into ContentProvider.
     *
     * @param context Used to access utility methods and the ContentResolver
     */
Performs the network request for fetch movie data, parse the JSON
from that request and inserts the new movie information into ContentProvider.

@param context Used to access utility methods and the ContentResolver
Performs the network request for fetch movie data parse the JSON from that request and inserts the new movie information into ContentProvider . @param context Used to access utility methods and the ContentResolver
private static void fetchNextPage(Context context) {
        /* Get pages info */
        int currentPage = PageUtils.getCurrentPage(context);
        int totalPages = PageUtils.getTotalPages(context);
        /* Get uri of data for current show mode */
        Uri uri;
        int showMode = MoviesPreferences.getMoviesShowMode(context);
        if (showMode == MoviesPreferences.SHOW_MODE_MOST_POPULAR) {
            uri = MoviesContract.MovieEntry.CONTENT_URI_MOST_POPULAR;
        } else {
            uri = MoviesContract.MovieEntry.CONTENT_URI_TOP_RATED;
        }
        /* If last update time expired, set current page to 0 and clean cache table */
        boolean isDataActual = DateUtils.isMoviesListLastUpdateActual(context);
        if (!isDataActual) {
            context.getContentResolver().delete(uri, null, null);
            currentPage = 0;
            PageUtils.setCurrentPage(context, currentPage);
        }
        /* If all pages already loaded, we don't need to do anything */
        if (currentPage == totalPages) {
            return;
        }
        URL moviesListUrl = NetworkUtils.getMoviesListUrl(context, currentPage + 1);
        ContentValues[] movieContentValues = null;
        int requestedTotalPages = 1;
        try {
            String response = NetworkUtils.getResponseFromHttpUrl(moviesListUrl);
            movieContentValues = TmdbJsonUtils.getMovieContentValuesFromJson(response);
            requestedTotalPages = TmdbJsonUtils.getTotalPagesFromJson(response);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (movieContentValues == null) {
            return;
        }
        int insertedRows = context.getContentResolver()
                .bulkInsert(uri, movieContentValues);
        if (insertedRows > 0) {
            currentPage++;
            PageUtils.setCurrentPage(context, currentPage);
            if (currentPage == 1) {
                PageUtils.setTotalPages(context, requestedTotalPages);
                long currentTime = System.currentTimeMillis();
                DateUtils.setMoviesListLastUpdateTime(context, currentTime);
            }
        }
    }
private static void fetchNextPage ( Context context ) { /* Get pages info */ int currentPage = PageUtils . getCurrentPage ( context ) ; int totalPages = PageUtils . getTotalPages ( context ) ; /* Get uri of data for current show mode */ Uri uri ; int showMode = MoviesPreferences . getMoviesShowMode ( context ) ; if ( showMode == MoviesPreferences . SHOW_MODE_MOST_POPULAR ) { uri = MoviesContract . MovieEntry . CONTENT_URI_MOST_POPULAR ; } else { uri = MoviesContract . MovieEntry . CONTENT_URI_TOP_RATED ; } /* If last update time expired, set current page to 0 and clean cache table */ boolean isDataActual = DateUtils . isMoviesListLastUpdateActual ( context ) ; if ( ! isDataActual ) { context . getContentResolver ( ) . delete ( uri , null , null ) ; currentPage = 0 ; PageUtils . setCurrentPage ( context , currentPage ) ; } /* If all pages already loaded, we don't need to do anything */ if ( currentPage == totalPages ) { return ; } URL moviesListUrl = NetworkUtils . getMoviesListUrl ( context , currentPage + 1 ) ; ContentValues [ ] movieContentValues = null ; int requestedTotalPages = 1 ; try { String response = NetworkUtils . getResponseFromHttpUrl ( moviesListUrl ) ; movieContentValues = TmdbJsonUtils . getMovieContentValuesFromJson ( response ) ; requestedTotalPages = TmdbJsonUtils . getTotalPagesFromJson ( response ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } if ( movieContentValues == null ) { return ; } int insertedRows = context . getContentResolver ( ) . bulkInsert ( uri , movieContentValues ) ; if ( insertedRows > 0 ) { currentPage ++ ; PageUtils . setCurrentPage ( context , currentPage ) ; if ( currentPage == 1 ) { PageUtils . setTotalPages ( context , requestedTotalPages ) ; long currentTime = System . currentTimeMillis ( ) ; DateUtils . setMoviesListLastUpdateTime ( context , currentTime ) ; } } }
Performs the network request for fetch movie data, parse the JSON
 from that request and inserts the new movie information into ContentProvider.
Performs the network request for fetch movie data parse the JSON from that request and inserts the new movie information into ContentProvider .