Working dirty solution

master
Tomasz Półgrabia 2016-10-26 23:22:47 +02:00
parent 6d1059f865
commit cb0bfa8e7a
9 changed files with 190 additions and 22 deletions

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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 +
'}';
}
}

View File

@ -24,11 +24,9 @@ import java.util.List;
public class PanoramioAdapter extends ArrayAdapter<PanoramioImageInfo> {
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<PanoramioImageInfo> photosDescriptions) {
super(activity, location_item, photosDescriptions);
aq = NetUtils.createProxyAQueryInstance(activity);
public PanoramioAdapter(FragmentActivity ctx, int location_item, List<PanoramioImageInfo> photosDescriptions) {
super(ctx, location_item, photosDescriptions);
}
@Override

View File

@ -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;

View File

@ -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();
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;
}
}

View File

@ -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);
}
}
}

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout
android:id="@+id/fragments"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/swipe_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
@ -11,5 +11,12 @@
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="pl.tpolgrabia.urbanexplorer.MainActivity" android:orientation="horizontal">
<LinearLayout android:id="@+id/fragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</pl.tpolgrabia.urbanexplorer.views.SwipeFrameLayout>

View File

@ -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);
}
}