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 0000000..cde69bc Binary files /dev/null and b/app/src/main/res/drawable/ic_launcher.png differ diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 94f3953..bdab353 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -1,14 +1,53 @@ - - - + + + + + + + + + + + + +