-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
272 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 104 additions & 16 deletions
120
app/src/main/java/de/timonback/android/whatisthatplace/activity/VisionFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.