Fix Back Button Navigation Error from Settings Activity
This commit is contained in:
@@ -34,4 +34,6 @@ dependencies {
|
||||
compile 'com.android.support:support-v4:23.1.1'
|
||||
compile 'com.android.support:design:23.1.1'
|
||||
compile 'com.squareup.picasso:picasso:2.5.2'
|
||||
compile 'com.jakewharton:butterknife:7.0.1'
|
||||
compile 'com.github.justzak:dilatingdotsprogressbar:1.0.1'
|
||||
}
|
||||
|
||||
@@ -12,6 +12,9 @@ import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
/**
|
||||
* Created by Aaron Helton on 1/31/2016
|
||||
*/
|
||||
@@ -20,11 +23,11 @@ public class DetailFragment extends Fragment
|
||||
public DetailFragment() {}
|
||||
|
||||
private Movie movie;
|
||||
private TextView rating;
|
||||
private TextView release;
|
||||
private TextView overview;
|
||||
private TextView title;
|
||||
private ImageView poster;
|
||||
@Bind(R.id.rating) TextView rating;
|
||||
@Bind(R.id.releaseDate) TextView release;
|
||||
@Bind(R.id.overview) TextView overview;
|
||||
@Bind(R.id.titleView) TextView title;
|
||||
@Bind(R.id.posterImage) ImageView poster;
|
||||
private boolean viewCreated;
|
||||
private boolean displayTitle = false;
|
||||
|
||||
@@ -65,14 +68,9 @@ public class DetailFragment extends Fragment
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View root = inflater.inflate(R.layout.fragment_detail, container, false);
|
||||
rating = (TextView) root.findViewById(R.id.rating);
|
||||
overview = (TextView) root.findViewById(R.id.overview);
|
||||
release = (TextView) root.findViewById(R.id.releaseDate);
|
||||
poster = (ImageView) root.findViewById(R.id.posterImage);
|
||||
title = (TextView)root.findViewById(R.id.titleView);
|
||||
ButterKnife.bind(this, root);
|
||||
if(!displayTitle)
|
||||
title.setVisibility(View.GONE);
|
||||
|
||||
viewCreated = true;
|
||||
if(movie != null) {
|
||||
setMovieInfo(movie);
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
@@ -19,32 +18,39 @@ import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.zl.reik.dilatingdotsprogressbar.DilatingDotsProgressBar;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import butterknife.Bind;
|
||||
import butterknife.ButterKnife;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Aaron Helton on 1/30/2016
|
||||
*/
|
||||
public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItemClickListener
|
||||
{
|
||||
//Neglecting use of ButterKnife for this fragment, only one findViewById() is used.
|
||||
private static final String LOG_TAG = DiscoverFragment.class.getSimpleName();
|
||||
private static final Integer PREFERRED_COLUMN_WIDTH = 100;
|
||||
private OnMovieSelectedListener mListener;
|
||||
private RecyclerView mMoviesGridView;
|
||||
private GridLayoutManager mManager;
|
||||
private MovieAdapter mAdapter;
|
||||
private FrameLayout movieContainer;
|
||||
private String prevSort;
|
||||
private boolean loadingMovies;
|
||||
private int currentPage;
|
||||
|
||||
@Bind(R.id.movieContainer) FrameLayout movieContainer;
|
||||
@Bind(R.id.progress) DilatingDotsProgressBar dotProgress;
|
||||
|
||||
public DiscoverFragment() {}
|
||||
|
||||
@Override
|
||||
@@ -70,7 +76,7 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
View root = inflater.inflate(R.layout.fragment_discover, container, false);
|
||||
movieContainer = (FrameLayout)root.findViewById(R.id.movieContainer);
|
||||
ButterKnife.bind(this, root);
|
||||
//Create Recyclerview Programmatically so that we can get an auto-fit-like functionality
|
||||
mMoviesGridView = new RecyclerView(this.getContext()) {
|
||||
@Override
|
||||
@@ -116,6 +122,7 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
mMoviesGridView.setLayoutManager(mManager);
|
||||
mMoviesGridView.setAdapter(mAdapter);
|
||||
movieContainer.addView(mMoviesGridView);
|
||||
dotProgress.hideNow();
|
||||
addMovies();
|
||||
if(savedInstanceState != null)
|
||||
mMoviesGridView.setVerticalScrollbarPosition(savedInstanceState.getInt("SCROLL_POS"));
|
||||
@@ -145,11 +152,14 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
public void addMovies() {
|
||||
if(currentPage < 11)
|
||||
{
|
||||
dotProgress.showNow();
|
||||
loadingMovies = true;
|
||||
DiscoverTask task = new DiscoverTask() {
|
||||
@Override
|
||||
public void onPostExecute(Void result) {
|
||||
loadingMovies = false;
|
||||
dotProgress.hideNow();
|
||||
dotProgress.reset();
|
||||
}
|
||||
};
|
||||
task.execute(currentPage++);
|
||||
@@ -211,9 +221,19 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
builder.appendQueryParameter("vote_count.gte", "250");
|
||||
Uri uri = builder.build();
|
||||
String moviesJSON = NetworkUtil.getURL(new URL(uri.toString()).toString());
|
||||
JSONObject movieList = new JSONObject(moviesJSON);
|
||||
JSONObject movieList;
|
||||
if(moviesJSON != null)
|
||||
movieList = new JSONObject(moviesJSON);
|
||||
else
|
||||
return null;
|
||||
JSONArray results = movieList.getJSONArray("results");
|
||||
for(int i = 0; i < results.length(); i++) {
|
||||
|
||||
//We need to get the posters. First thing, determine the number of processors avail
|
||||
//We can use that to determine an upper limit to parallel threads.
|
||||
int currentThreadCount = 0;
|
||||
int processors = Runtime.getRuntime().availableProcessors();
|
||||
for(int i = 0; i < results.length(); i++)
|
||||
{
|
||||
Integer id = results.getJSONObject(i).getInt("id");
|
||||
String title = results.getJSONObject(i).getString("original_title");
|
||||
Bitmap poster = null;
|
||||
@@ -223,27 +243,6 @@ public class DiscoverFragment extends Fragment implements MovieAdapter.MovieItem
|
||||
} catch (IOException ex) {
|
||||
Log.e(LOG_TAG, "Unable to retrieve poster! " + ex.getMessage(), ex);
|
||||
}
|
||||
|
||||
//Retrieve default no image bitmap if no image exists for the movie
|
||||
if(poster == null) {
|
||||
InputStream stream = null;
|
||||
try {
|
||||
// get input stream
|
||||
stream = getResources().getAssets().open("noimage.png");
|
||||
poster = BitmapFactory.decodeStream(stream);
|
||||
} catch(IOException ex) {
|
||||
Log.e(LOG_TAG, "UNABLE TO DECODE DEFAULT IMAGE: "+ex.getMessage(), ex);
|
||||
poster = null;
|
||||
} finally {
|
||||
if(stream != null) {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(LOG_TAG, "Unable to close stream: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
publishProgress(new MovieAdapter.MovieReference(id, poster, title));
|
||||
}
|
||||
} catch (MalformedURLException ex) {
|
||||
|
||||
@@ -7,9 +7,6 @@ import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.squareup.picasso.Downloader;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -74,13 +71,11 @@ public class Movie implements Comparable<Movie>
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Implementation tied explicitly to Picasso. Check out other libraries!
|
||||
public static Bitmap getPoster(Context context, String posterPath) throws IOException
|
||||
{
|
||||
try {
|
||||
return Picasso.with(context).load(TMDB_POSTER_URL + posterPath).get();
|
||||
} catch(Downloader.ResponseException ex) {
|
||||
return BitmapFactory.decodeResource(context.getResources(), R.mipmap.noimage);
|
||||
}
|
||||
return NetworkUtil.getImage(TMDB_POSTER_URL + posterPath, context,
|
||||
BitmapFactory.decodeResource(context.getResources(), R.mipmap.noimage));
|
||||
}
|
||||
|
||||
private String title;
|
||||
|
||||
@@ -1,13 +1,20 @@
|
||||
package com.example.android.popularmovies;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.picasso.Downloader;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Created by Aaron Helton on 1/30/2016
|
||||
@@ -15,8 +22,85 @@ import java.net.URL;
|
||||
public final class NetworkUtil
|
||||
{
|
||||
private static final String LOG_TAG = NetworkUtil.class.getSimpleName();
|
||||
private static final ArrayList<Long> requestTimes = new ArrayList<>();
|
||||
private static final ArrayList<Long> imgRequestTimes = new ArrayList<>();
|
||||
|
||||
public static String getURL(String urlString)
|
||||
public static String getURL(String urlString) {
|
||||
long nano = System.nanoTime();
|
||||
String res = getUrlImpl(urlString);
|
||||
|
||||
//Statistics
|
||||
long time = (System.nanoTime()-nano)/1000000;
|
||||
requestTimes.add(time);
|
||||
long avg = 0;
|
||||
long min = 10000;
|
||||
long max = -1;
|
||||
Log.d(LOG_TAG, "REQUEST: " + time + "ms");
|
||||
for(Long num : requestTimes)
|
||||
{
|
||||
if(num < min) min = num;
|
||||
if(num > max) max = num;
|
||||
avg += num;
|
||||
}
|
||||
Log.d(LOG_TAG, "AVERAGE TIME: " + ((double)avg/requestTimes.size()) + "ms");
|
||||
Log.d(LOG_TAG, "MAX TIME: " + max + "ms");
|
||||
Log.d(LOG_TAG, "MIN TIME: " + min + "ms");
|
||||
return res;
|
||||
}
|
||||
|
||||
public static Bitmap getImage(String url, Context context, Bitmap defImg) {
|
||||
long nano = System.nanoTime();
|
||||
|
||||
//Tests on target device show that the straight HTTPUrlConnection version actually performs
|
||||
//faster on average than Picasso, and images being stored as an ImageView later means we
|
||||
//don't quite benefit as much from the image cache to make it worth using.
|
||||
Bitmap bm = defImageImpl2(url, context, defImg);
|
||||
|
||||
long time = (System.nanoTime()-nano)/1000000;
|
||||
imgRequestTimes.add(time);
|
||||
long avg = 0;
|
||||
long min = 10000;
|
||||
long max = -1;
|
||||
Log.d(LOG_TAG, "IMAGE REQUEST: " + time + "ms");
|
||||
for(Long num : imgRequestTimes)
|
||||
{
|
||||
if(num < min) min = num;
|
||||
if(num > max) max = num;
|
||||
avg += num;
|
||||
}
|
||||
Log.d(LOG_TAG, "AVERAGE TIME: " + ((double)avg/imgRequestTimes.size()) + "ms");
|
||||
Log.d(LOG_TAG, "MAX TIME: " + max + "ms");
|
||||
Log.d(LOG_TAG, "MIN TIME: " + min + "ms");
|
||||
return bm;
|
||||
}
|
||||
|
||||
private static Bitmap defImageImpl(String url, Context context, Bitmap defImg)
|
||||
{
|
||||
try {
|
||||
return Picasso.with(context).load(url).get();
|
||||
} catch(Downloader.ResponseException ex) {
|
||||
return defImg;
|
||||
} catch (IOException ex) {
|
||||
return defImg;
|
||||
}
|
||||
}
|
||||
|
||||
private static Bitmap defImageImpl2(String urlString, Context context, Bitmap defImg)
|
||||
{
|
||||
try {
|
||||
URL url = new URL(urlString);
|
||||
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
||||
return BitmapFactory.decodeStream(connection.getInputStream());
|
||||
} catch (MalformedURLException ex) {
|
||||
Log.e(LOG_TAG, "Malformed URL: " + urlString + ";\n" + ex.getMessage(), ex);
|
||||
return defImg;
|
||||
} catch (IOException ex) {
|
||||
Log.e(LOG_TAG, "I/O ERROR: " + ex.getMessage(), ex);
|
||||
return defImg;
|
||||
}
|
||||
}
|
||||
|
||||
private static String getUrlImpl(String urlString)
|
||||
{
|
||||
if(urlString == null || urlString.isEmpty())
|
||||
return null;
|
||||
|
||||
@@ -93,6 +93,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/divider"
|
||||
android:layout_margin="10dp"
|
||||
android:layout_marginLeft="0dp"
|
||||
android:padding="10dp"
|
||||
android:elevation="4dp"
|
||||
android:background="@color/colorPrimary"
|
||||
|
||||
@@ -78,6 +78,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="10dp"
|
||||
android:layout_marginLeft="0dp"
|
||||
android:layout_toRightOf="@id/posterImage"
|
||||
android:layout_toEndOf="@id/posterImage"
|
||||
android:layout_below="@id/ratingHeader"
|
||||
|
||||
@@ -1,9 +1,30 @@
|
||||
<FrameLayout
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/movieContainer"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:background="@color/black"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.example.android.popularmovies.DiscoverFragment"/>
|
||||
tools:context="com.example.android.popularmovies.DiscoverFragment">
|
||||
<FrameLayout
|
||||
android:id="@+id/movieContainer"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_alignParentTop="true"/>
|
||||
<com.zl.reik.dilatingdotsprogressbar.DilatingDotsProgressBar
|
||||
android:id="@+id/progress"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:radius="5dp"
|
||||
android:elevation="8dp"
|
||||
android:color="@color/white"
|
||||
app:dd_numDots="10"
|
||||
app:dd_scaleMultiplier="2"
|
||||
app:dd_animationDuration="250"
|
||||
app:dd_horizontalSpacing="10dp"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user