Created
July 19, 2016 13:45
-
-
Save jimitjaishwal/41041373e1d4df45518736768376299e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.android.jimitjaishwal.movies.app.Details; | |
| import android.content.ContentUris; | |
| import android.content.ContentValues; | |
| import android.content.Intent; | |
| import android.database.Cursor; | |
| import android.net.Uri; | |
| import android.os.Bundle; | |
| import android.support.annotation.Nullable; | |
| import android.support.v4.app.Fragment; | |
| import android.support.v4.app.LoaderManager; | |
| import android.support.v4.content.CursorLoader; | |
| import android.support.v4.content.Loader; | |
| import android.util.Log; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.ImageButton; | |
| import android.widget.ImageView; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieModel; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieReviewResponse; | |
| import com.android.jimitjaishwal.movies.app.Models.MovieReviews; | |
| import com.android.jimitjaishwal.movies.app.Models.VideoResponse; | |
| import com.android.jimitjaishwal.movies.app.Models.Videos; | |
| import com.android.jimitjaishwal.movies.app.MovieDataBase.MovieContract; | |
| import com.android.jimitjaishwal.movies.app.R; | |
| import com.android.jimitjaishwal.movies.app.Service.MDB_Service; | |
| import com.squareup.picasso.Picasso; | |
| import java.util.ArrayList; | |
| import retrofit2.Call; | |
| import retrofit2.Callback; | |
| import retrofit2.Response; | |
| import retrofit2.Retrofit; | |
| import retrofit2.converter.gson.GsonConverterFactory; | |
| /** | |
| * A placeholder fragment containing a simple view. | |
| */ | |
| public class Details_ActivityFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> { | |
| private static final String LOG = Details_ActivityFragment.class.getSimpleName(); | |
| private final String BASE_URL = "http://api.themoviedb.org/3/"; | |
| private final String api_Key = "149c3ccaa00eeb61331e4da7734fd1af"; | |
| private ArrayList<MovieReviews> reviewsArrayList; | |
| ArrayList<Videos> videosArrayList; | |
| ImageView imageView; | |
| ImageButton imageButton; | |
| private static final int CURSOR_LOADER_ID = 1; | |
| View rootView; | |
| public static final String[] COLUMNS = { | |
| MovieContract.Movies.TABLE_NAME + "." + MovieContract.Movies._ID, | |
| MovieContract.Movies.COLUMN_MOVIE_ID, | |
| MovieContract.Movies.COLUMN_BACKDROP_PATH | |
| }; | |
| public static final String[] TRAILER_COLUMNS = { | |
| MovieContract.MovieTrailers.TABLE_NAME + "." + MovieContract.MovieTrailers._ID, | |
| MovieContract.MovieTrailers.COLUMN_MOVIE_ID, | |
| MovieContract.MovieTrailers.COLUMN_TRAILER_KEY | |
| }; | |
| public static final int TRAILER_ID = 0; | |
| public static final int TRAILER_MOVIE_ID = 1; | |
| public static final int TRAILER_MOVIE_KEY = 2; | |
| public static final int _ID = 0; | |
| public static final int COLUMN_MOVIE_ID = 1; | |
| public static final int COLUMN_BACKDROP_PATH = 2; | |
| public Details_ActivityFragment() { | |
| } | |
| @Override | |
| public void onCreate(@Nullable Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| } | |
| @Override | |
| public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
| Bundle savedInstanceState) { | |
| rootView = inflater.inflate(R.layout.fragment_details_, container, false); | |
| return rootView; | |
| } | |
| @Override | |
| public void onActivityCreated(@Nullable Bundle savedInstanceState) { | |
| super.onActivityCreated(savedInstanceState); | |
| getLoaderManager().initLoader(CURSOR_LOADER_ID, null, this); | |
| } | |
| public void getMovieReviews(MovieModel movies) { | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| MDB_Service service = retrofit.create(MDB_Service.class); | |
| Call<MovieReviewResponse> call = service.getMovieReviews(movies.getId(), api_Key); | |
| call.enqueue(new Callback<MovieReviewResponse>() { | |
| @Override | |
| public void onResponse(Call<MovieReviewResponse> call, Response<MovieReviewResponse> response) { | |
| reviewsArrayList = response.body().getResults(); | |
| } | |
| @Override | |
| public void onFailure(Call<MovieReviewResponse> call, Throwable t) { | |
| } | |
| }); | |
| } | |
| public void getMovieTrailers(Cursor c) { | |
| Retrofit retrofit = new Retrofit.Builder() | |
| .baseUrl(BASE_URL) | |
| .addConverterFactory(GsonConverterFactory.create()) | |
| .build(); | |
| MDB_Service service = retrofit.create(MDB_Service.class); | |
| Call<VideoResponse> call = | |
| service.getMovieVideos(c.getInt(COLUMN_MOVIE_ID), api_Key); | |
| call.enqueue(new Callback<VideoResponse>() { | |
| @Override | |
| public void onResponse(Call<VideoResponse> call, Response<VideoResponse> response) { | |
| videosArrayList = response.body().getResults(); | |
| for (final Videos videos : videosArrayList) { | |
| long videoRowId; | |
| Cursor videoCursor = getActivity().getContentResolver() | |
| .query(MovieContract.MovieTrailers.CONTENT_URI, | |
| new String[]{MovieContract.MovieTrailers._ID}, | |
| MovieContract.MovieTrailers.COLUMN_TRAILER_KEY + " = ?", | |
| new String[]{videos.getKey()}, | |
| null | |
| ); | |
| if (videoCursor.moveToFirst()) { | |
| int columnIndex = videoCursor.getColumnIndex(MovieContract.MovieTrailers._ID); | |
| videoRowId = videoCursor.getLong(columnIndex); | |
| } else { | |
| ContentValues values = new ContentValues(); | |
| values.put(MovieContract.MovieTrailers.COLUMN_MOVIE_ID, videos.getMovieId()); | |
| values.put(MovieContract.MovieTrailers.COLUMN_TRAILER_NAME, videos.getName()); | |
| values.put(MovieContract.MovieTrailers.COLUMN_TRAILER_KEY, videos.getKey()); | |
| Uri insertedUri = getActivity().getContentResolver().insert(MovieContract.MovieTrailers.CONTENT_URI, values); | |
| videoRowId = ContentUris.parseId(insertedUri); | |
| } | |
| Log.d("LOG_TAG", "Movie Table Rows " + videoRowId + " Rows inserted"); | |
| videoCursor.close(); | |
| } | |
| } | |
| @Override | |
| public void onFailure(Call<VideoResponse> call, Throwable t) { | |
| } | |
| }); | |
| } | |
| public void watchYoutubeVideo(Cursor videos) { | |
| String base_url = "http://www.youtube.com/watch?v=" + videos.getString(TRAILER_MOVIE_KEY); | |
| Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(base_url)); | |
| startActivity(intent); | |
| } | |
| public long addMovieReviewsIntoDatabase(int movieId, String id, String author, String Content) { | |
| long reviewID; | |
| Cursor reviewCursor = getActivity().getContentResolver().query( | |
| MovieContract.Reviews.CONTENT_URI, | |
| new String[]{MovieContract.Reviews._ID}, | |
| MovieContract.Reviews.COLUMN_MOVIE_ID + "=?", | |
| new String[]{id}, | |
| null | |
| ); | |
| if (reviewCursor.moveToFirst()) { | |
| int reviewIndex = reviewCursor.getColumnIndex(MovieContract.Reviews._ID); | |
| reviewID = reviewCursor.getInt(reviewIndex); | |
| } else { | |
| ContentValues values = new ContentValues(); | |
| values.put(MovieContract.Reviews.COLUMN_MOVIE_ID, movieId); | |
| values.put(MovieContract.Reviews.COLUMN_AUTHOR, author); | |
| values.put(MovieContract.Reviews.COLUMN_CONTENT, Content); | |
| Uri buildUri = getActivity().getContentResolver().insert(MovieContract.Reviews.CONTENT_URI, values); | |
| reviewID = ContentUris.parseId(buildUri); | |
| } | |
| reviewCursor.close(); | |
| return reviewID; | |
| } | |
| @Override | |
| public Loader<Cursor> onCreateLoader(int id, Bundle args) { | |
| Intent i = getActivity().getIntent(); | |
| if (i == null) { | |
| return null; | |
| } | |
| return new CursorLoader(getActivity(), | |
| i.getData(), | |
| COLUMNS, | |
| null, | |
| null, | |
| null); | |
| } | |
| @Override | |
| public void onLoadFinished(Loader<Cursor> loader, final Cursor data) { | |
| if (data != null && data.moveToFirst()){ | |
| getMovieTrailers(data); | |
| String backDropPath = data.getString(COLUMN_BACKDROP_PATH); | |
| imageView = (ImageView) rootView.findViewById(R.id.backDrown_path); | |
| String baseUrl = "http://image.tmdb.org/t/p/w500" + | |
| backDropPath; | |
| Log.d(LOG, "onLoadFinished: " + baseUrl); | |
| Picasso.with(getActivity()).load(baseUrl).into(imageView); | |
| imageButton = (ImageButton) rootView.findViewById(R.id.play_Button); | |
| imageButton.setOnClickListener(new View.OnClickListener() { | |
| @Override | |
| public void onClick(View v) { | |
| watchYoutubeVideo(data); | |
| String base_url = "http://www.youtube.com/watch?v=" + data.getString(2); | |
| Log.d(LOG, "watchYoutubeVideo: " + base_url); | |
| } | |
| }); | |
| String base_url = "http://www.youtube.com/watch?v=" + data.getString(2); | |
| Log.d(LOG, "watchYoutubeVideo: " + base_url); | |
| } | |
| } | |
| @Override | |
| public void onLoaderReset(Loader<Cursor> loader) { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment