1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.threadsample;
18 
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.support.v4.app.Fragment;
22 import android.support.v4.app.ShareCompat;
23 import android.support.v4.content.LocalBroadcastManager;
24 import android.util.Log;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 
32 public class PhotoFragment extends Fragment implements View.OnClickListener {
33     // Constants
34     private static final String LOG_TAG = "ImageDownloaderThread";
35     private static final String PHOTO_URL_KEY = "com.example.android.threadsample.PHOTO_URL_KEY";
36 
37     PhotoView mPhotoView;
38 
39     String mURLString;
40 
41     ShareCompat.IntentBuilder mShareCompatIntentBuilder;
42 
43     /**
44      * Converts the stored URL string to a URL, and then tries to download the picture from that
45      * URL.
46      */
loadPhoto()47     public void loadPhoto() {
48         // If setPhoto() was called to store a URL, proceed
49         if (mURLString != null) {
50 
51             // Handles invalid URLs
52             try {
53 
54                 // Converts the URL string to a valid URL
55                 URL localURL = new URL(mURLString);
56 
57                 /*
58                  * setImageURL(url,false,null) attempts to download and decode the picture at
59                  * at "url" without caching and without providing a Drawable. The result will be
60                  * a BitMap stored in the PhotoView for this Fragment.
61                  */
62                 mPhotoView.setImageURL(localURL, false, null);
63 
64             // Catches an invalid URL format
65             } catch (MalformedURLException localMalformedURLException) {
66                 localMalformedURLException.printStackTrace();
67             }
68         }
69     }
70     /**
71      * Returns the stored URL string
72      * @return The URL of the picture being shown by this Fragment, in String format
73      */
getURLString()74     public String getURLString() {
75         return mURLString;
76     }
77 
78     /*
79      * This callback is invoked when users click on a displayed image. The input argument is
80      * a handle to the View object that was clicked
81      */
82     @Override
onClick(View view)83     public void onClick(View view) {
84 
85         // Sends a broadcast intent to zoom the image
86         Intent localIntent = new Intent(Constants.ACTION_ZOOM_IMAGE);
87         LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(localIntent);
88     }
89 
90     /*
91      * This callback is invoked when the Fragment is created.
92      */
93     @Override
onCreate(Bundle bundle)94     public void onCreate(Bundle bundle) {
95         super.onCreate(bundle);
96     }
97 
98     /*
99      * This callback is invoked as the Fragment's View is being constructed.
100      */
101     @Override
onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle)102     public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
103         super.onCreateView(inflater, viewGroup, bundle);
104 
105         /*
106          * Creates a View from the specified layout file. The layout uses the parameters specified
107          * in viewGroup, but is not attached to any parent
108          */
109         View localView = inflater.inflate(R.layout.photo, viewGroup, false);
110 
111         // Gets a handle to the PhotoView View in the layout
112         mPhotoView = ((PhotoView) localView.findViewById(R.id.photoView));
113 
114         /*
115          * The click listener becomes this class (PhotoFragment). The onClick() method in this
116          * class is invoked when users click a photo.
117          */
118         mPhotoView.setOnClickListener(this);
119 
120         // If the bundle argument contains data, uses it as a URL for the picture to display
121         if (bundle != null) {
122             mURLString = bundle.getString(PHOTO_URL_KEY);
123         }
124 
125         if (mURLString != null)
126             loadPhoto();
127 
128         // Returns the resulting View
129         return localView;
130     }
131 
132     /*
133      * This callback is invoked as the Fragment's View is being destroyed
134      */
135     @Override
onDestroyView()136     public void onDestroyView() {
137         // Logs the destroy operation
138         Log.d(LOG_TAG, "onDestroyView");
139 
140         // If the View object still exists, delete references to avoid memory leaks
141         if (mPhotoView != null) {
142 
143             mPhotoView.setOnClickListener(null);
144             this.mPhotoView = null;
145         }
146 
147         // Always call the super method last
148         super.onDestroyView();
149     }
150 
151     /*
152      * This callback is invoked when the Fragment is no longer attached to its Activity.
153      * Sets the URL for the Fragment to null
154      */
155     @Override
onDetach()156     public void onDetach() {
157         // Logs the detach
158         Log.d(LOG_TAG, "onDetach");
159 
160         // Removes the reference to the URL
161         mURLString = null;
162 
163         // Always call the super method last
164         super.onDetach();
165     }
166 
167     /*
168      * This callback is invoked if the system asks the Fragment to save its state. This allows the
169      * the system to restart the Fragment later on.
170      */
171     @Override
onSaveInstanceState(Bundle bundle)172     public void onSaveInstanceState(Bundle bundle) {
173         // Always call the super method first
174         super.onSaveInstanceState(bundle);
175 
176         // Puts the current URL for the picture being shown into the saved state
177         bundle.putString(PHOTO_URL_KEY, mURLString);
178     }
179 
180     /**
181      * Sets the photo for this Fragment, by storing a URL that points to a picture
182      * @param urlString A String representation of the URL pointing to the picture
183      */
setPhoto(String urlString)184     public void setPhoto(String urlString) {
185         mURLString = urlString;
186     }
187 }
188