From cb0bfa8e7a2875dd97bd7e5f48ee654684d1e8d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Wed, 26 Oct 2016 23:22:47 +0200 Subject: [PATCH] Working dirty solution --- .../urbanexplorer/AppConstants.java | 1 + .../urbanexplorer/MainActivity.java | 28 ++++++-- .../events/PhotoInfoUpdateEvent.java | 41 +++++++++++ .../fragments/PanoramioAdapter.java | 6 +- .../fragments/PanoramioShowerFragment.java | 69 ++++++++++++++++--- .../PanoramioItemLongClickHandler.java | 25 ++++++- .../handlers/PanoramioSwitchHandler.java | 9 +++ app/src/main/res/layout/activity_main.xml | 9 ++- .../utils/PanoramioUtils.java | 24 +++++++ 9 files changed, 190 insertions(+), 22 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/events/PhotoInfoUpdateEvent.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index abfeb0c..448bae5 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -22,4 +22,5 @@ public class AppConstants { static final String SAVED_CONFIG_KEY = "SAVED_CONFIG_KEY"; public static final String GOOGLE_API_KEY = "AIzaSyBAJoK-pu_qnQ0U8EGjM1Zkz_g8oJV4w2g"; public static final Locale DEF_APP_LOCALE = Locale.ENGLISH; + public static final double PANORAMIO_SHOWER_SIDEBAR_THRESHOLD = 5.0; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index f3faf93..eca4f22 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -1,7 +1,11 @@ package pl.tpolgrabia.urbanexplorer; +import android.app.Activity; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; +import android.graphics.Point; +import android.graphics.PointF; import android.location.LocationManager; import android.os.Bundle; import android.os.Looper; @@ -10,6 +14,7 @@ import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.GestureDetectorCompat; import android.support.v7.app.ActionBarActivity; +import android.util.DisplayMetrics; import android.view.*; import com.nostra13.universalimageloader.core.DisplayImageOptions; import org.greenrobot.eventbus.EventBus; @@ -42,6 +47,7 @@ public class MainActivity extends ActionBarActivity { public static DisplayImageOptions options; public static DisplayImageOptions rectOptions; + private GestureDetectorCompat gestureDetector; private MainActivityState currFrag = MainActivityState.PANORAMIO; private StandardLocationListener locationCallback; @@ -193,10 +199,7 @@ public class MainActivity extends ActionBarActivity { this.currFrag = MainActivityState.PANORAMIO_SHOWER; FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ctx = fragmentManager.beginTransaction(); - PanoramioShowerFragment panoramioShower = new PanoramioShowerFragment(); - Bundle arguments = new Bundle(); - arguments.putSerializable(PanoramioShowerFragment.PANORAMIO_PHOTO_ARG_KEY, photoInfo); - panoramioShower.setArguments(arguments); + PanoramioShowerFragment panoramioShower = createShowerFragment(photoInfo); ctx.setCustomAnimations(R.anim.slide_in_down, R.anim.slide_out_down, @@ -216,6 +219,14 @@ public class MainActivity extends ActionBarActivity { } + public static PanoramioShowerFragment createShowerFragment(PanoramioImageInfo photoInfo) { + PanoramioShowerFragment panoramioShower = new PanoramioShowerFragment(); + Bundle arguments = new Bundle(); + arguments.putSerializable(PanoramioShowerFragment.PANORAMIO_PHOTO_ARG_KEY, photoInfo); + panoramioShower.setArguments(arguments); + return panoramioShower; + } + private void switchFragment() { if (currFrag == oldFrag) { @@ -261,7 +272,7 @@ public class MainActivity extends ActionBarActivity { } private void updateSwipeHandler() { - SwipeFrameLayout swipeFragments = (SwipeFrameLayout) findViewById(R.id.fragments); + SwipeFrameLayout swipeFragments = (SwipeFrameLayout) findViewById(R.id.swipe_frag); swipeFragments.setCustomInterceptor(new CustomInterceptor() { @Override public void handle(MotionEvent ev) { @@ -407,4 +418,11 @@ public class MainActivity extends ActionBarActivity { progressDlg.dismiss(); } + public void addFragment(Fragment fragment, String tag) { + FragmentManager fragMgr = getSupportFragmentManager(); + FragmentTransaction tx = fragMgr.beginTransaction(); + tx.add(R.id.fragments, fragment, tag); + tx.commit(); + } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/PhotoInfoUpdateEvent.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/PhotoInfoUpdateEvent.java new file mode 100644 index 0000000..47cd7bc --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/PhotoInfoUpdateEvent.java @@ -0,0 +1,41 @@ +package pl.tpolgrabia.urbanexplorer.events; + +import pl.tpolgrabia.panoramiobindings.dto.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler; + +/** + * Created by tpolgrabia on 26.10.16. + */ +public class PhotoInfoUpdateEvent { + private Object source; + private PanoramioImageInfo photoInfo; + + public PhotoInfoUpdateEvent(Object source, PanoramioImageInfo photoInfo) { + this.source = source; + this.photoInfo = photoInfo; + } + + public Object getSource() { + return source; + } + + public void setSource(Object source) { + this.source = source; + } + + public PanoramioImageInfo getPhotoInfo() { + return photoInfo; + } + + public void setPhotoInfo(PanoramioImageInfo photoInfo) { + this.photoInfo = photoInfo; + } + + @Override + public String toString() { + return "PhotoInfoUpdateEvent{" + + "source=" + source + + ", photoInfo=" + photoInfo + + '}'; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java index c6517d9..67cbb32 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioAdapter.java @@ -24,11 +24,9 @@ import java.util.List; public class PanoramioAdapter extends ArrayAdapter { public static final int MAX_OWNERNAME_LENGTH = 10; public static final int MAX_PANORAMIO_DESCRIPTION_LENGTH = 96; - private final AQuery aq; - public PanoramioAdapter(FragmentActivity activity, int location_item, List photosDescriptions) { - super(activity, location_item, photosDescriptions); - aq = NetUtils.createProxyAQueryInstance(activity); + public PanoramioAdapter(FragmentActivity ctx, int location_item, List photosDescriptions) { + super(ctx, location_item, photosDescriptions); } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java index bfcd85f..c9e6264 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/PanoramioShowerFragment.java @@ -9,19 +9,26 @@ import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import com.nostra13.universalimageloader.core.ImageLoader; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.panoramiobindings.dto.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; -import pl.tpolgrabia.panoramiobindings.dto.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.events.PhotoInfoUpdateEvent; /** * A simple {@link Fragment} subclass. */ public class PanoramioShowerFragment extends Fragment { + private static final Logger lg = LoggerFactory.getLogger(PanoramioShowerFragment.class); + public static final String PANORAMIO_PHOTO_ARG_KEY = "PANORAMIO_PHOTO_ARG_KEY"; public static final String TAG = "PANORAMIO_TAG"; @@ -35,6 +42,12 @@ public class PanoramioShowerFragment extends Fragment { // Required empty public constructor } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EventBus.getDefault().register(this); + lg.debug("Registering..."); + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -49,39 +62,49 @@ public class PanoramioShowerFragment extends Fragment { final PanoramioImageInfo imageInfo = (PanoramioImageInfo) arguments.getSerializable(PANORAMIO_PHOTO_ARG_KEY); + if (imageInfo == null) { + return inflatedView; + } + + setContent(inflatedView, imageInfo); + + return inflatedView; + } + + private void setContent(View view, final PanoramioImageInfo imageInfo) { if (imageInfo != null) { ImageLoader.getInstance().displayImage( imageInfo.getPhotoFileUrl(), - (ImageView) inflatedView.findViewById(R.id.photo_container), + (ImageView) view.findViewById(R.id.photo_container), MainActivity.rectOptions); - photoTitle = (TextView)inflatedView.findViewById(R.id.photo_title); + photoTitle = (TextView)view.findViewById(R.id.photo_title); photoTitle.setText(imageInfo.getPhotoTitle()); - photoUploadDate = (TextView)inflatedView.findViewById(R.id.photo_upload); + photoUploadDate = (TextView)view.findViewById(R.id.photo_upload); photoUploadDate.setText(imageInfo.getUploadDate()); - photoAuthor = (TextView)inflatedView.findViewById(R.id.photo_author); + photoAuthor = (TextView)view.findViewById(R.id.photo_author); photoAuthor.setText(imageInfo.getOwnerName()); - photoUrl = (TextView)inflatedView.findViewById(R.id.photo_url); + photoUrl = (TextView)view.findViewById(R.id.photo_url); photoUrl.setText(imageInfo.getPhotoUrl()); photoUrl.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Intent intent = new Intent(android.content.Intent.ACTION_VIEW, + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(imageInfo.getPhotoUrl())); startActivity(intent); return true; } }); - photoLocation = (TextView)inflatedView.findViewById(R.id.photo_location); + photoLocation = (TextView)view.findViewById(R.id.photo_location); photoLocation.setText(imageInfo.getLatitude() + "," + imageInfo.getLongitude()); photoLocation.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { - Intent intent = new Intent(android.content.Intent.ACTION_VIEW, + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=" + imageInfo.getLatitude() + "," + imageInfo.getLongitude())); startActivity(intent); @@ -90,14 +113,38 @@ public class PanoramioShowerFragment extends Fragment { }); } + } - return inflatedView; + @Subscribe + public void handlePhotoInfoUpdate(PhotoInfoUpdateEvent event) { + lg.debug("Photo event: {}", event); + if (getView() == null) { + lg.debug("View is not available"); + return; + } + + final PanoramioImageInfo photoInfo = event.getPhotoInfo(); + if (photoInfo == null) { + lg.debug("Photo info is not available"); + return; + } + + setContent(getView(), photoInfo); + + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( + 0, + LinearLayout.LayoutParams.MATCH_PARENT, + 1); + getView().setLayoutParams(params); } @Override public void onDestroy() { super.onDestroy(); + EventBus.getDefault().unregister(this); + lg.debug("Unregistering..."); + FragmentActivity acc = getActivity(); if (acc != null) { MainActivity mainActivity = (MainActivity)acc; diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java index 2c5ca10..477297d 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java @@ -1,10 +1,18 @@ package pl.tpolgrabia.urbanexplorer.handlers; import android.view.View; +import android.view.ViewGroup; import android.widget.AdapterView; +import android.widget.LinearLayout; import android.widget.ListView; +import org.greenrobot.eventbus.EventBus; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.panoramiobindings.utils.PanoramioUtils; +import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.panoramiobindings.dto.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.events.PhotoInfoUpdateEvent; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioAdapter; @@ -12,6 +20,9 @@ import pl.tpolgrabia.urbanexplorer.fragments.PanoramioAdapter; * Created by tpolgrabia on 21.09.16. */ public class PanoramioItemLongClickHandler implements AdapterView.OnItemLongClickListener { + + private static final Logger lg = LoggerFactory.getLogger(PanoramioItemLongClickHandler.class); + private HomeFragment homeFragment; private final ListView finalLocations; @@ -25,7 +36,19 @@ public class PanoramioItemLongClickHandler implements AdapterView.OnItemLongClic PanoramioAdapter panAdapter = (PanoramioAdapter) finalLocations.getAdapter(); PanoramioImageInfo photoInfo = panAdapter.getItem(pos); MainActivity activity = (MainActivity) homeFragment.getActivity(); - activity.switchToPhoto(photoInfo); + double dims = PanoramioUtils.calcDiag(activity); + if (dims >= AppConstants.PANORAMIO_SHOWER_SIDEBAR_THRESHOLD) { + lg.debug("Sending panoramio image event"); + EventBus.getDefault().post(new PhotoInfoUpdateEvent(this, photoInfo)); + LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( + 0, + LinearLayout.LayoutParams.MATCH_PARENT, + 1); + View layout = homeFragment.getView(); + layout.setLayoutParams(params); + } else { + activity.switchToPhoto(photoInfo); + } return false; } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java index 12d5ca5..03c1406 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java @@ -2,8 +2,12 @@ package pl.tpolgrabia.urbanexplorer.handlers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.tpolgrabia.panoramiobindings.utils.PanoramioUtils; +import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; +import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; /** * Created by tpolgrabia on 21.09.16. @@ -21,5 +25,10 @@ public class PanoramioSwitchHandler implements Runnable { public void run() { lg.debug("Switching to home fragment"); mainActivity.switchFragment(new HomeFragment(), HomeFragment.TAG); + double diagInches = PanoramioUtils.calcDiag(mainActivity); + if (diagInches >= AppConstants.PANORAMIO_SHOWER_SIDEBAR_THRESHOLD) { + mainActivity.addFragment(MainActivity.createShowerFragment(null), PanoramioShowerFragment.TAG); + // mainActivity.addFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG); + } } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 2e0322c..802a468 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,8 +1,8 @@ + + + + diff --git a/panoramiobindings/src/main/java/pl/tpolgrabia/panoramiobindings/utils/PanoramioUtils.java b/panoramiobindings/src/main/java/pl/tpolgrabia/panoramiobindings/utils/PanoramioUtils.java index bc55fab..8f57c59 100644 --- a/panoramiobindings/src/main/java/pl/tpolgrabia/panoramiobindings/utils/PanoramioUtils.java +++ b/panoramiobindings/src/main/java/pl/tpolgrabia/panoramiobindings/utils/PanoramioUtils.java @@ -1,6 +1,10 @@ package pl.tpolgrabia.panoramiobindings.utils; +import android.app.Activity; import android.content.Context; +import android.graphics.PointF; +import android.util.DisplayMetrics; +import android.view.Display; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxStatus; @@ -146,4 +150,24 @@ public class PanoramioUtils { } + public static PointF calcDims(Activity ctx) { + DisplayMetrics metrics = calcMetrics(ctx); + int w = metrics.widthPixels; + int h = metrics.heightPixels; + float wi = w / metrics.xdpi; + float hi = h / metrics.ydpi; + return new PointF(wi, hi); + } + + public static DisplayMetrics calcMetrics(Activity ctx) { + DisplayMetrics metrics = new DisplayMetrics(); + final Display display = ctx.getWindowManager().getDefaultDisplay(); + display.getMetrics(metrics); + return metrics; + } + + public static double calcDiag(Activity ctx) { + PointF dims = calcDims(ctx); + return Math.sqrt(dims.x*dims.x + dims.y*dims.y); + } }