From fc23e5e30a4c438338c228cb3fa70519d4614ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sat, 27 Aug 2016 12:56:37 +0200 Subject: [PATCH] Loading example images from panoramio using universal image loader. --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 3 + .../urbanexplorer/AppConstants.java | 8 ++ .../urbanexplorer/MainActivity.java | 23 ++++ .../urbanexplorer/dto/PanoramioImageInfo.java | 72 +++++++++++++ .../urbanexplorer/fragments/HomeFragment.java | 98 +++++++++++++++++- .../fragments/PanoramioAdapter.java | 44 ++++++++ app/src/main/res/drawable/ic_launcher.png | Bin 0 -> 3418 bytes app/src/main/res/layout/fragment_home.xml | 47 ++++++++- app/src/main/res/layout/location_item.xml | 18 ++++ 10 files changed, 308 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/PanoramioImageInfo.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java create mode 100644 app/src/main/res/drawable/ic_launcher.png create mode 100644 app/src/main/res/layout/location_item.xml diff --git a/app/build.gradle b/app/build.gradle index 5065fda..b000daf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,4 +25,6 @@ dependencies { compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:support-v4:21.0.3' compile 'com.android.support:recyclerview-v7:21.0.3' + compile 'com.googlecode.android-query:android-query:0.25.9' + compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5' } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 32e23bc..c10098e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,5 +18,8 @@ + + + diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java new file mode 100644 index 0000000..c6701da --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -0,0 +1,8 @@ +package pl.tpolgrabia.urbanexplorer; + +/** + * Created by tpolgrabia on 27.08.16. + */ +public class AppConstants { + public static final String GOOGLE_API_KEY = "AIzaSyDAnmEK6cgovRrefUuYojL1pxPEbIBLZUw"; +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index fe9cabd..cb14140 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -8,12 +8,19 @@ import android.support.v7.app.ActionBarActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.widget.Toast; +import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache; +import com.nostra13.universalimageloader.core.DisplayImageOptions; +import com.nostra13.universalimageloader.core.ImageLoader; +import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; +import com.nostra13.universalimageloader.core.assist.ImageScaleType; +import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; public class MainActivity extends ActionBarActivity { private static final int LOCATION_SETTINGS_REQUEST_ID = 1; private static final String CLASS_TAG = MainActivity.class.getSimpleName(); + public static DisplayImageOptions options; @Override protected void onCreate(Bundle savedInstanceState) { @@ -22,6 +29,22 @@ public class MainActivity extends ActionBarActivity { // Toolbar toolbar = (Toolbar) findViewById(R.id.navbar); // setSupportActionBar(toolbar); + // UNIVERSAL IMAGE LOADER SETUP + DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() + .cacheOnDisc(true).cacheInMemory(true) + .imageScaleType(ImageScaleType.EXACTLY) + .displayer(new FadeInBitmapDisplayer(300)).build(); + + options = defaultOptions; + + ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder( + this) + .defaultDisplayImageOptions(defaultOptions) + .memoryCache(new WeakMemoryCache()) + .discCacheSize(100 * 1024 * 1024).build(); + + ImageLoader.getInstance().init(config); + } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/PanoramioImageInfo.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/PanoramioImageInfo.java new file mode 100644 index 0000000..2e08775 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/PanoramioImageInfo.java @@ -0,0 +1,72 @@ +package pl.tpolgrabia.urbanexplorer.dto; + +import java.util.Date; + +/** + * Created by tpolgrabia on 27.08.16. + */ +public class PanoramioImageInfo { + private Double height; + private String ownerName; + private Long ownerId; + private String photoFileUrl; + private String photoTitle; + private Date uploadDate; + private Double width; + + public Double getHeight() { + return height; + } + + public void setHeight(Double height) { + this.height = height; + } + + public String getOwnerName() { + return ownerName; + } + + public void setOwnerName(String ownerName) { + this.ownerName = ownerName; + } + + public Long getOwnerId() { + return ownerId; + } + + public void setOwnerId(Long ownerId) { + this.ownerId = ownerId; + } + + public String getPhotoFileUrl() { + return photoFileUrl; + } + + public void setPhotoFileUrl(String photoFileUrl) { + this.photoFileUrl = photoFileUrl; + } + + public String getPhotoTitle() { + return photoTitle; + } + + public void setPhotoTitle(String photoTitle) { + this.photoTitle = photoTitle; + } + + public Date getUploadDate() { + return uploadDate; + } + + public void setUploadDate(Date uploadDate) { + this.uploadDate = uploadDate; + } + + public Double getWidth() { + return width; + } + + public void setWidth(Double width) { + this.width = width; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index 1a075ab..19b5b0e 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -12,10 +12,21 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - +import android.widget.ArrayAdapter; +import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import com.androidquery.AQuery; +import com.androidquery.callback.AjaxCallback; +import com.androidquery.callback.AjaxStatus; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.PanoramioImageInfo; + +import java.util.ArrayList; +import java.util.List; import static android.content.Context.LOCATION_SERVICE; @@ -34,6 +45,8 @@ public class HomeFragment extends Fragment implements LocationListener { private LocationManager locationService; private String locationProvider; private boolean locationServicesActivated = false; + private AQuery aq; + private View inflatedView; public HomeFragment() { // Required empty public constructor @@ -42,6 +55,7 @@ public class HomeFragment extends Fragment implements LocationListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + aq = new AQuery(getActivity()); locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); @@ -53,14 +67,92 @@ public class HomeFragment extends Fragment implements LocationListener { return; } - Toast.makeText(getActivity(), "Created", Toast.LENGTH_LONG).show(); + } + + private Double safeParseDouble(CharSequence text) { + if (text == null) { + return null; + } + + try { + return Double.parseDouble(text.toString()); + } catch (NumberFormatException e) { + Log.w(CLASS_TAG, "Wrong number format", e); + return null; + } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_home, container, false); + inflatedView = inflater.inflate(R.layout.fragment_home, container, false); + // getActivity().findViewById(R.id.update_places).setOnClickListener( +// new View.OnClickListener() { +// @Override +// public void onClick(View view) { +// Location location = locationService.getLastKnownLocation(locationProvider); +// aq.ajax("https://maps.googleapis.com/maps/api/place/nearbysearch/output?" + +// "key=" + AppConstants.GOOGLE_API_KEY +// + "&location=" + location.getLatitude() + "," + location.getLongitude() +// + "&radius" + safeParseDouble(aq.id(R.id.location_range).getText()) +// + "&rankby=distance", +// JSONObject.class, +// new AjaxCallback() { +// @Override +// public void callback(String url, JSONObject object, AjaxStatus status) { +// object +// } +// }); +// } +// } +// ); + + final ListView locations = (ListView)inflatedView.findViewById(R.id.locations); + inflatedView.findViewById(R.id.update_places).setOnClickListener( + new View.OnClickListener() { + @Override + public void onClick(View view) { + Location location = locationService.getLastKnownLocation(locationProvider); + aq.ajax("http://www.panoramio.com/map/get_panoramas.php?set=public" + + "&from=0&to=20&minx=-180&miny=-90&maxx=180&maxy=90&size=medium&mapfilter=true", + JSONObject.class, + new AjaxCallback() { + @Override + public void callback(String url, JSONObject object, AjaxStatus status) { + try { + Log.d(CLASS_TAG, "Query code: " + status.getCode() + + ", error: " + status.getError() + ", message: " + status.getMessage()); + if (object == null) { + return; + } + + JSONArray photos = object.getJSONArray("photos"); + List photosInfos = new ArrayList(); + int n = photos.length(); + for (int i = 0; i < n; i++) { + JSONObject photo = photos.getJSONObject(i); + PanoramioImageInfo info = new PanoramioImageInfo(); + info.setPhotoTitle(photo.getString("photo_title")); + info.setPhotoFileUrl(photo.getString("photo_file_url")); + info.setWidth(photo.getDouble("width")); + info.setHeight(photo.getDouble("height")); + photosInfos.add(info); + } + ArrayAdapter adapter = new PanoramioAdapter(getActivity(), + R.layout.location_item, + photosInfos); + locations.setAdapter(adapter); + } catch (JSONException e) { + Log.w(CLASS_TAG, "Json not supported format", e); + } + } + }); + } + } + ); + + return inflatedView; } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java new file mode 100644 index 0000000..1a80440 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java @@ -0,0 +1,44 @@ +package pl.tpolgrabia.urbanexplorer.fragments; + +import android.content.Context; +import android.graphics.Bitmap; +import android.support.v4.app.FragmentActivity; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.ImageView; +import android.widget.TextView; +import com.androidquery.AQuery; +import com.nostra13.universalimageloader.core.ImageLoader; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.PanoramioImageInfo; + +import java.util.List; + +/** + * Created by tpolgrabia on 27.08.16. + */ +public class PanoramioAdapter extends ArrayAdapter { + private final AQuery aq; + + public PanoramioAdapter(FragmentActivity activity, int location_item, List photosDescriptions) { + super(activity, location_item, photosDescriptions); + aq = new AQuery(activity); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + View itemView = inflater.inflate(R.layout.location_item, parent, false); + TextView locDesc = (TextView) itemView.findViewById(R.id.location_description); + locDesc.setText(getItem(position).getPhotoTitle()); + final String photoUrl = getItem(position).getPhotoFileUrl(); + ImageView photoImg = (ImageView) itemView.findViewById(R.id.photo_img); + // photoImg.setImageBitmap(bm); + // photoImg.setImageBitmap(BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_launcher)); + ImageLoader.getInstance().displayImage(photoUrl, photoImg, MainActivity.options); + return itemView; + } +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher.png b/app/src/main/res/drawable/ic_launcher.png new file mode 100644 index 0000000000000000000000000000000000000000..cde69bcccec65160d92116f20ffce4fce0b5245c GIT binary patch literal 3418 zcmZ{nX*|@A^T0p5j$I+^%FVhdvMbgt%d+mG98ubwNv_tpITppba^GiieBBZGI>I89 zGgm8TA>_)DlEu&W;s3#ZUNiH4&CF{a%siTjzG;eOzQB6{003qKeT?}z_5U*{{kgZ; zdV@U&tqa-&4FGisjMN8o=P}$t-`oTM2oeB5d9mHPgTYJx4jup)+5a;Tke$m708DocFzDL>U$$}s6FGiy_I1?O zHXq`q884|^O4Q*%V#vwxqCz-#8i`Gu)2LeB0{%%VKunOF%9~JcFB9MM>N00M`E~;o zBU%)O5u-D6NF~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