2017-09-12 14:54:41 +02:00
|
|
|
package de.trac.spherical;
|
|
|
|
|
2017-09-13 15:05:38 +02:00
|
|
|
import android.content.Context;
|
2017-09-12 18:11:58 +02:00
|
|
|
import android.content.Intent;
|
2017-09-13 15:05:38 +02:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2017-09-12 19:50:59 +02:00
|
|
|
import android.net.Uri;
|
2017-09-13 01:11:40 +02:00
|
|
|
import android.opengl.GLSurfaceView;
|
2017-09-12 14:54:41 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
|
import android.os.Bundle;
|
2017-09-12 18:11:58 +02:00
|
|
|
import android.util.Log;
|
2017-09-12 19:50:59 +02:00
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2017-09-13 01:10:29 +02:00
|
|
|
import de.trac.spherical.parser.PhotoSphereMetadata;
|
2017-09-12 19:50:59 +02:00
|
|
|
import de.trac.spherical.parser.SphereParser;
|
2017-09-12 14:54:41 +02:00
|
|
|
|
2017-09-13 01:11:40 +02:00
|
|
|
import de.trac.spherical.rendering.Renderer;
|
2017-09-13 15:05:38 +02:00
|
|
|
import de.trac.spherical.rendering.SphereSurfaceView;
|
2017-09-13 01:11:40 +02:00
|
|
|
|
2017-09-12 14:54:41 +02:00
|
|
|
public class MainActivity extends AppCompatActivity {
|
|
|
|
|
2017-09-12 18:11:58 +02:00
|
|
|
public static final String TAG = "Spherical";
|
2017-09-12 20:30:44 +02:00
|
|
|
|
|
|
|
public static final String MIME_PHOTO_SPHERE = "application/vnd.google.panorama360+jpg";
|
|
|
|
public static final String MIME_IMAGE = "image/*";
|
2017-09-12 19:50:59 +02:00
|
|
|
|
2017-09-13 15:05:38 +02:00
|
|
|
private SphereSurfaceView surfaceView;
|
|
|
|
private Renderer renderer;
|
2017-09-12 18:11:58 +02:00
|
|
|
|
2017-09-12 14:54:41 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-09-13 15:05:38 +02:00
|
|
|
|
|
|
|
// Initialize renderer and setup surface view.
|
|
|
|
surfaceView = new SphereSurfaceView(this);
|
|
|
|
renderer = new Renderer(surfaceView);
|
|
|
|
setContentView(surfaceView);
|
2017-09-12 18:11:58 +02:00
|
|
|
|
|
|
|
Intent intent = getIntent();
|
2017-09-12 19:50:59 +02:00
|
|
|
switch (intent.getAction()) {
|
2017-09-12 20:30:44 +02:00
|
|
|
//Image was sent into the app
|
2017-09-12 19:50:59 +02:00
|
|
|
case Intent.ACTION_SEND:
|
2017-09-12 20:30:44 +02:00
|
|
|
handleSentImageIntent(intent);
|
|
|
|
break;
|
2017-09-12 19:50:59 +02:00
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
//App was launched via launcher icon
|
|
|
|
//TODO: Remove later together with launcher intent filter
|
2017-09-12 19:50:59 +02:00
|
|
|
default:
|
|
|
|
Toast.makeText(this, R.string.prompt_share_image, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
/**
|
|
|
|
* Distinguish type of sent image. Images with the MIME type of a photosphere will be directly
|
|
|
|
* displayed, while images with MIME type image/* are being manually tested using {@link SphereParser}.
|
|
|
|
* @param intent incoming intent.
|
|
|
|
*/
|
|
|
|
private void handleSentImageIntent(Intent intent) {
|
2017-09-12 18:11:58 +02:00
|
|
|
String type = intent.getType();
|
2017-09-12 19:50:59 +02:00
|
|
|
if (type != null) {
|
2017-09-12 20:30:44 +02:00
|
|
|
|
|
|
|
Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
|
|
|
|
if (imageUri == null) {
|
|
|
|
Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-12 19:50:59 +02:00
|
|
|
switch (type) {
|
2017-09-12 20:30:44 +02:00
|
|
|
case MIME_PHOTO_SPHERE:
|
|
|
|
displayPhotoSphere(imageUri);
|
|
|
|
break;
|
2017-09-12 19:50:59 +02:00
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
case MIME_IMAGE:
|
|
|
|
displayMaybePhotoSphere(imageUri);
|
2017-09-12 19:50:59 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-09-12 20:30:44 +02:00
|
|
|
|
2017-09-12 19:50:59 +02:00
|
|
|
} else {
|
2017-09-12 20:30:44 +02:00
|
|
|
Toast.makeText(this, "TODO: Figure out what to do :D", Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check, whether the sent photo is a photo sphere and display either a sphere, or a plain image.
|
|
|
|
* @param uri
|
|
|
|
*/
|
|
|
|
private void displayMaybePhotoSphere(Uri uri) {
|
|
|
|
try {
|
|
|
|
InputStream inputStream = getContentResolver().openInputStream(uri);
|
|
|
|
String xml = SphereParser.getXMLContent(inputStream);
|
2017-09-13 01:10:29 +02:00
|
|
|
PhotoSphereMetadata metadata = SphereParser.parse(xml);
|
2017-09-12 20:30:44 +02:00
|
|
|
|
2017-09-13 01:42:03 +02:00
|
|
|
inputStream = getContentResolver().openInputStream(uri);
|
|
|
|
|
2017-09-13 01:10:29 +02:00
|
|
|
if (metadata.isUsePanoramaViewer()) {
|
2017-09-13 01:42:03 +02:00
|
|
|
displayPhotoSphere(inputStream, metadata);
|
2017-09-12 20:30:44 +02:00
|
|
|
} else {
|
2017-09-13 01:42:03 +02:00
|
|
|
displayFlatImage(inputStream);
|
2017-09-12 20:30:44 +02:00
|
|
|
}
|
2017-09-13 01:10:29 +02:00
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2017-09-12 19:50:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
/**
|
|
|
|
* Display a photo sphere.
|
|
|
|
* @param uri
|
|
|
|
*/
|
|
|
|
private void displayPhotoSphere(Uri uri) {
|
2017-09-12 19:50:59 +02:00
|
|
|
try {
|
|
|
|
InputStream inputStream = getContentResolver().openInputStream(uri);
|
|
|
|
String xml = SphereParser.getXMLContent(inputStream);
|
2017-09-13 01:10:29 +02:00
|
|
|
PhotoSphereMetadata metadata = SphereParser.parse(xml);
|
2017-09-12 19:50:59 +02:00
|
|
|
|
2017-09-13 01:10:29 +02:00
|
|
|
displayPhotoSphere(getContentResolver().openInputStream(uri), metadata);
|
2017-09-12 18:11:58 +02:00
|
|
|
|
2017-09-12 19:50:59 +02:00
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
Log.e(TAG, "File not found.", e);
|
|
|
|
Toast.makeText(this, R.string.file_not_found, Toast.LENGTH_SHORT).show();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(TAG, "IOException: ", e);
|
|
|
|
Toast.makeText(this, R.string.ioerror, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2017-09-12 14:54:41 +02:00
|
|
|
}
|
2017-09-12 20:30:44 +02:00
|
|
|
|
2017-09-13 01:10:29 +02:00
|
|
|
private void displayPhotoSphere(InputStream inputStream, PhotoSphereMetadata metadata) {
|
2017-09-13 15:05:38 +02:00
|
|
|
renderer.setBitmap(BitmapFactory.decodeStream(inputStream));
|
2017-09-13 01:10:29 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
/**
|
|
|
|
* Display a flat image.
|
2017-09-13 01:42:03 +02:00
|
|
|
* @param inputStream
|
2017-09-12 20:30:44 +02:00
|
|
|
*/
|
2017-09-13 01:42:03 +02:00
|
|
|
private void displayFlatImage(InputStream inputStream) {
|
2017-09-13 15:05:38 +02:00
|
|
|
Toast.makeText(this, "Not yet implemented", Toast.LENGTH_SHORT).show();
|
2017-09-12 20:30:44 +02:00
|
|
|
}
|
2017-09-12 14:54:41 +02:00
|
|
|
}
|