Skip to content

Commit

Permalink
Integrated VisionFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed Dec 8, 2017
1 parent b6f61ea commit 1ce4948
Show file tree
Hide file tree
Showing 10 changed files with 272 additions and 99 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies {
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
implementation 'com.google.guava:guava:23.3-android'
implementation 'io.github.luizgrp.sectionedrecyclerviewadapter:sectionedrecyclerviewadapter:1.1.3'
implementation 'com.google.android.gms:play-services-maps:11.4.0'

implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:recyclerview-v7:26.1.0'
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:supportsRtl="true"
android:hardwareAccelerated="true"
android:theme="@style/AppTheme">

<activity
android:name=".activity.MainActivity"
android:label="@string/app_name"
Expand All @@ -34,6 +35,13 @@
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyD26X-CzeqyqPbtHDgMWu-YstQTSJLps_Y"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package de.timonback.android.whatisthatplace.activity;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.common.collect.ListMultimap;
import com.google.common.collect.MultimapBuilder;
Expand All @@ -25,11 +29,20 @@
import de.timonback.android.whatisthatplace.activity.provider.ImageProvider;
import de.timonback.android.whatisthatplace.component.gallery.GalleryItem;
import de.timonback.android.whatisthatplace.component.gallery.GallerySection;
import de.timonback.android.whatisthatplace.model.knowledge.KnowledgeResult;
import de.timonback.android.whatisthatplace.model.vision.VisionResult;
import de.timonback.android.whatisthatplace.service.ServiceProvider;
import de.timonback.android.whatisthatplace.util.MyParamCallable;
import io.github.luizgrp.sectionedrecyclerviewadapter.SectionedRecyclerViewAdapter;

public class GalleryFragment extends Fragment {
public class GalleryFragment extends Fragment implements GallerySection.OnGalleryItemClickListener {
public interface OnChangeFragmentListener {
void onFragmentChange(String path);
}

public static final String LOG_NAME = GalleryFragment.class.getName();

private OnChangeFragmentListener contextCallback;
private SectionedRecyclerViewAdapter sectionAdapter;

@Override
Expand Down Expand Up @@ -61,6 +74,37 @@ public int getSpanSize(int position) {
return view;
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment GalleryFragment.
*/
public static GalleryFragment newInstance() {
GalleryFragment fragment = new GalleryFragment();

Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

try {
contextCallback = (OnChangeFragmentListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + " must implement OnChangeFragmentListener");
}
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

private void prepareData() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

Expand All @@ -83,29 +127,16 @@ private void prepareData() {
galleryItems.add(new GalleryItem(galleryFile.getName(), galleryFile));
}

GallerySection section = new GallerySection(getActivity(), entry.getKey(), galleryItems);
GallerySection section = new GallerySection(getActivity(), entry.getKey(), galleryItems, this);
sectionAdapter.addSection(section);
}
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment GalleryFragment.
*/
public static GalleryFragment newInstance() {
GalleryFragment fragment = new GalleryFragment();

Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
public void clicked(GalleryItem item) {
final Context context = getContext();
final File file = item.getImageFile();

contextCallback.onFragmentChange(file.getPath());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.timonback.android.whatisthatplace.activity;

import android.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
Expand All @@ -10,10 +12,10 @@

import de.timonback.android.whatisthatplace.R;
import de.timonback.android.whatisthatplace.component.CameraComponent;
import de.timonback.android.whatisthatplace.component.gallery.GallerySection;

public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements GalleryFragment.OnChangeFragmentListener {
private static final String LOG_NAME = MainActivity.class.getName();

private CameraComponent cameraComponent = null;

@Override
Expand All @@ -22,18 +24,10 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

cameraComponent = new CameraComponent();
//gVisionComponent = new GVisionComponent();

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if (savedInstanceState == null) {
GalleryFragment gallyerFragment = GalleryFragment.newInstance();

getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, gallyerFragment).commit();
}

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -50,11 +44,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
return true;
}

@Override
protected void onDestroy() {
super.onDestroy();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
Expand All @@ -68,4 +57,40 @@ public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

@Override
public void onFragmentChange(String imagePath) {
VisionFragment visionFrag = (VisionFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_vision);

if (visionFrag != null) {
// We're in two-pane layout...
if(imagePath == null) {
getSupportFragmentManager().beginTransaction().detach(visionFrag);
}

// Call a method in the ArticleFragment to update its content
visionFrag.updateImage(imagePath);
} else {
// Otherwise, we're in the one-pane layout and must swap frags...

if(imagePath != null) {
// Create fragment and give it an argument for the selected article
VisionFragment newFragment = new VisionFragment();
Bundle args = new Bundle();
args.putString(VisionFragment.ARG_IMAGE_PATH, imagePath);
newFragment.setArguments(args);

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_vision, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,141 @@
package de.timonback.android.whatisthatplace.activity;


import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.squareup.picasso.Picasso;

import java.io.File;

import de.timonback.android.whatisthatplace.R;
import de.timonback.android.whatisthatplace.model.knowledge.KnowledgeResult;
import de.timonback.android.whatisthatplace.model.vision.Landmark;
import de.timonback.android.whatisthatplace.model.vision.Location;
import de.timonback.android.whatisthatplace.model.vision.VisionResult;
import de.timonback.android.whatisthatplace.service.ServiceProvider;
import de.timonback.android.whatisthatplace.util.MyParamCallable;


public class VisionFragment extends Fragment {
public class VisionFragment extends Fragment implements OnMapReadyCallback {
private final static String LOG_NAME = VisionFragment.class.getName();

public final static String ARG_IMAGE_PATH = "imagePath";

private String imagePath;
private GoogleMap map = null;

public VisionFragment() {
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment VisionFragment.
*/
// TODO: Rename and change types and number of parameters
public static VisionFragment newInstance(String param1, String param2) {
public static VisionFragment newInstance(String imagePath) {
VisionFragment fragment = new VisionFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
args.putString(ARG_IMAGE_PATH, imagePath);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
imagePath = getArguments().getString(ARG_IMAGE_PATH);
} else {
getActivity().onBackPressed();
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_vision, container, false);
}

@Override
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

updateView();
}

public void updateImage(String path) {
imagePath = path;

updateView();
}

private void updateView() {
final Context context = getContext();
final File file = new File(imagePath);

ImageView imageView = getActivity().findViewById(R.id.visionImage);
Picasso.with(context).load(file).fit().centerInside().into(imageView);

ServiceProvider.getVisionService(context).analyse(Uri.fromFile(file), new MyParamCallable<VisionResult>() {
@Override
public void call(VisionResult param) {
if (param.getLandmarks().isEmpty()) {
Toast.makeText(context, "no landmark identified", Toast.LENGTH_SHORT).show();
return;
}

Landmark landmark = param.getLandmarks().get(0);
if(!landmark.getLocations().isEmpty() && map != null) {
Location location = landmark.getLocations().get(0);

LatLng landmarkPosition = new LatLng(location.getLatitude(), location.getLongitude());
map.moveCamera(CameraUpdateFactory.zoomTo(15));
map.moveCamera(CameraUpdateFactory.newLatLng(landmarkPosition));
map.addMarker(new MarkerOptions().position(landmarkPosition));
}

String mid = landmark.getMid();
ServiceProvider.getKnowledgeService(context).getKnowledgeInfo(mid, new MyParamCallable<KnowledgeResult>() {
@Override
public void call(KnowledgeResult param) {
if (param.getItemListElement().isEmpty()) {
Toast.makeText(context, "Google seems to not know this place...", Toast.LENGTH_SHORT).show();
return;
}
String name = param.getItemListElement().get(0).getResult().getName();
String description = param.getItemListElement().get(0).getResult().getDescription();
Toast.makeText(context, "This is " + name, Toast.LENGTH_SHORT).show();
Toast.makeText(context, description, Toast.LENGTH_SHORT).show();


TextView nameView = getActivity().findViewById(R.id.visionName);
nameView.setText(name);
TextView descriptionView = getActivity().findViewById(R.id.visionDescription);
descriptionView.setText(description);
}
});
}
});
}

@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
map.getUiSettings().setZoomControlsEnabled(true);
}
}
Loading

0 comments on commit 1ce4948

Please sign in to comment.