/
ExampleViewerFragment.java
129 lines (106 loc) · 3.95 KB
/
ExampleViewerFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (C) 2014 Murray Cumming
*
* This file is part of android-galaxyzoo
*
* android-galaxyzoo is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* android-galaxyzoo is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with android-galaxyzoo. If not, see <http://www.gnu.org/licenses/>.
*/
package com.murrayc.galaxyzoo.app;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;
import java.net.URLConnection;
/**
* A simple {@link android.app.Fragment} subclass.
*/
public class ExampleViewerFragment extends Fragment {
public static final String ARG_EXAMPLE_URL = "example-url";
private View mLoadingView;
private View mRootView;
private String mUriStr = null;
private ImageView mImageView = null;
public ExampleViewerFragment() {
// Required empty public constructor
}
/**
* We need to load the bitmap for the imageview in an async task.
* This is tedious. It would be far easier if ImageView had a setFromUrl(url) method that did
* the work asynchronously itself.
*
* @param strUri
* @param imageView
*/
private void loadBitmap(final String strUri, ImageView imageView) {
showLoadingView(true);
//Note: We call cancelRequest in onPause() to avoid a leak,
//as vaguely suggested by the into() documentation.
Picasso.with(getActivity()).load(strUri).into(imageView, new Callback() {
@Override
public void onSuccess() {
showLoadingView(false);
}
@Override
public void onError() {
showLoadingView(false);
Log.error("ExampleViewerFragment.loadBitmap.onError().");
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Bundle bundle = getArguments();
if (bundle != null) {
setExampleUrl(bundle.getString(ARG_EXAMPLE_URL));
}
mRootView = inflater.inflate(R.layout.fragment_example_viewer, container, false);
mImageView = (ImageView) mRootView.findViewById(R.id.imageView);
update();
return mRootView;
}
@Override
public void onPause() {
super.onPause();
//Picasso's load() documentation tells us to use cancelRequest() to avoid a leak,
//though it doesn't suggest where/when to call it:
//http://square.github.io/picasso/javadoc/com/squareup/picasso/RequestCreator.html#into-android.widget.ImageView-com.squareup.picasso.Callback-
Picasso.with(getActivity()).cancelRequest(mImageView);
}
public void update() {
if (mImageView != null) {
loadBitmap(mUriStr, mImageView);
}
}
public void setExampleUrl(final String uriStr) {
mUriStr = uriStr;
}
private void showLoadingView(boolean show) {
if (mLoadingView == null) {
mLoadingView = mRootView.findViewById(R.id.loading_spinner);
}
mLoadingView.setVisibility(show ? View.VISIBLE : View.GONE);
}
}