2017-09-12 14:54:41 +02:00
|
|
|
package de.trac.spherical;
|
|
|
|
|
2017-09-15 01:17:35 +02:00
|
|
|
import android.Manifest;
|
2017-09-12 18:11:58 +02:00
|
|
|
import android.content.Intent;
|
2017-09-15 01:17:35 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2017-09-13 15:05:38 +02:00
|
|
|
import android.graphics.BitmapFactory;
|
2017-09-12 19:50:59 +02:00
|
|
|
import android.net.Uri;
|
2017-09-15 00:56:08 +02:00
|
|
|
import android.os.Build;
|
2017-09-14 20:39:33 +02:00
|
|
|
import android.os.Bundle;
|
2017-09-14 03:06:09 +02:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-09-15 01:17:35 +02:00
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
|
import android.support.v4.content.ContextCompat;
|
2017-09-12 14:54:41 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-09-14 20:39:33 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2017-09-12 18:11:58 +02:00
|
|
|
import android.util.Log;
|
2017-09-14 02:23:37 +02:00
|
|
|
import android.view.GestureDetector;
|
2017-09-14 20:39:33 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2017-09-14 02:23:37 +02:00
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
2017-09-15 00:56:08 +02:00
|
|
|
import android.view.Window;
|
|
|
|
import android.view.WindowManager;
|
2017-09-14 03:06:09 +02:00
|
|
|
import android.widget.LinearLayout;
|
2017-09-15 00:56:08 +02:00
|
|
|
import android.widget.RelativeLayout;
|
2017-09-12 19:50:59 +02:00
|
|
|
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-13 23:44:58 +02:00
|
|
|
import de.trac.spherical.parser.PhotoSphereParser;
|
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-15 01:17:35 +02:00
|
|
|
private static final int PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 387;
|
|
|
|
|
2017-09-13 15:05:38 +02:00
|
|
|
private SphereSurfaceView surfaceView;
|
|
|
|
private Renderer renderer;
|
2017-09-14 03:06:09 +02:00
|
|
|
private FloatingActionButton fab;
|
2017-09-14 20:39:33 +02:00
|
|
|
private Toolbar toolbar;
|
2017-09-12 18:11:58 +02:00
|
|
|
|
2017-09-15 01:17:35 +02:00
|
|
|
private Intent cachedIntent;
|
|
|
|
|
2017-09-12 14:54:41 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2017-09-14 03:06:09 +02:00
|
|
|
setContentView(R.layout.activity_main);
|
2017-09-13 15:05:38 +02:00
|
|
|
|
2017-09-14 20:39:33 +02:00
|
|
|
// Prepare UI
|
|
|
|
toolbar = (Toolbar) findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
2017-09-14 21:11:57 +02:00
|
|
|
getSupportActionBar().setDisplayShowTitleEnabled(false);
|
2017-09-15 00:56:08 +02:00
|
|
|
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
|
2017-09-14 20:39:33 +02:00
|
|
|
lp.topMargin += getStatusBarHeight();
|
2017-09-15 00:56:08 +02:00
|
|
|
toolbar.bringToFront();
|
2017-09-14 03:06:09 +02:00
|
|
|
fab = (FloatingActionButton) findViewById(R.id.fab);
|
|
|
|
fab.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
SphereSurfaceView.USE_TOUCH = !SphereSurfaceView.USE_TOUCH;
|
2017-09-14 20:39:33 +02:00
|
|
|
displayUI(false);
|
2017-09-14 03:06:09 +02:00
|
|
|
}
|
|
|
|
});
|
2017-09-12 18:11:58 +02:00
|
|
|
|
2017-09-15 00:56:08 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
Window w = getWindow();
|
|
|
|
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:39:33 +02:00
|
|
|
// Initialize renderer and setup surface view.
|
|
|
|
LinearLayout container = (LinearLayout) findViewById(R.id.container);
|
|
|
|
surfaceView = new SphereSurfaceView(this);
|
|
|
|
container.addView(surfaceView);
|
|
|
|
renderer = new Renderer(surfaceView);
|
|
|
|
|
2017-09-14 02:23:37 +02:00
|
|
|
// Detect gestures like single taps.
|
2017-09-14 13:16:25 +02:00
|
|
|
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
|
2017-09-14 02:23:37 +02:00
|
|
|
@Override
|
|
|
|
public boolean onSingleTapConfirmed(MotionEvent event) {
|
2017-09-14 20:39:33 +02:00
|
|
|
displayUI(!fab.isShown());
|
2017-09-14 02:23:37 +02:00
|
|
|
return true;
|
|
|
|
}
|
2017-09-14 13:16:25 +02:00
|
|
|
|
2017-09-14 02:23:37 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
surfaceView.setOnTouchListener(new View.OnTouchListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
2017-09-14 13:16:25 +02:00
|
|
|
return gestureDetector.onTouchEvent(event);
|
2017-09-14 02:23:37 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-15 01:17:35 +02:00
|
|
|
handleIntent(getIntent());
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent) {
|
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-15 01:17:35 +02:00
|
|
|
checkPermissionAndHandleSentImage(intent);
|
2017-09-12 20:30:44 +02:00
|
|
|
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:
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_prompt_share_image, Toast.LENGTH_LONG).show();
|
2017-09-12 19:50:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 01:17:35 +02:00
|
|
|
private void checkPermissionAndHandleSentImage(Intent intent) {
|
|
|
|
int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
|
|
|
|
if (status == PackageManager.PERMISSION_GRANTED) {
|
2017-09-15 01:40:27 +02:00
|
|
|
new HandleImageTask(this).doInBackground(intent);
|
2017-09-15 01:17:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache intent and request permission
|
|
|
|
this.cachedIntent = intent;
|
|
|
|
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
|
|
|
|
PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRequestPermissionsResult(int requestCode,
|
|
|
|
String permissions[], int[] grantResults) {
|
|
|
|
switch (requestCode) {
|
|
|
|
case PERMISSION_REQUEST_READ_EXTERNAL_STORAGE: {
|
|
|
|
// If request is cancelled, the result arrays are empty.
|
|
|
|
if (grantResults.length > 0
|
|
|
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
2017-09-15 01:40:27 +02:00
|
|
|
new HandleImageTask(this).doInBackground(cachedIntent);
|
2017-09-15 01:17:35 +02:00
|
|
|
} else {
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_missing_permission, Toast.LENGTH_LONG).show();
|
2017-09-15 01:17:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 20:39:33 +02:00
|
|
|
private void displayUI(boolean display) {
|
|
|
|
if (display) {
|
|
|
|
fab.show();
|
2017-09-15 00:56:08 +02:00
|
|
|
toolbar.setVisibility(View.VISIBLE);
|
2017-09-14 20:39:33 +02:00
|
|
|
} else {
|
|
|
|
fab.setVisibility(View.INVISIBLE);
|
2017-09-15 00:56:08 +02:00
|
|
|
toolbar.setVisibility(View.GONE);
|
2017-09-14 20:39:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:36:43 +02:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2017-09-14 20:39:33 +02:00
|
|
|
displayUI(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.main_menu, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_force_sphere:
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_not_yet_implemented, Toast.LENGTH_SHORT).show();
|
2017-09-14 20:39:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
2017-09-14 13:36:43 +02:00
|
|
|
}
|
|
|
|
|
2017-09-12 20:30:44 +02:00
|
|
|
/**
|
|
|
|
* Distinguish type of sent image. Images with the MIME type of a photosphere will be directly
|
2017-09-13 23:44:58 +02:00
|
|
|
* displayed, while images with MIME type image/* are being manually tested using {@link PhotoSphereParser}.
|
2017-09-12 20:30:44 +02:00
|
|
|
* @param intent incoming intent.
|
|
|
|
*/
|
2017-09-15 01:40:27 +02:00
|
|
|
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) {
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show();
|
2017-09-12 20:30:44 +02:00
|
|
|
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-13 14:51:35 +02:00
|
|
|
default:
|
2017-09-12 20:30:44 +02:00
|
|
|
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);
|
2017-09-13 23:44:58 +02:00
|
|
|
String xml = PhotoSphereParser.getXMLContent(inputStream);
|
|
|
|
PhotoSphereMetadata metadata = PhotoSphereParser.parse(xml);
|
2017-09-12 20:30:44 +02:00
|
|
|
|
2017-09-13 14:51:35 +02:00
|
|
|
if (metadata == null || !metadata.isUsePanoramaViewer()) {
|
|
|
|
displayFlatImage(getContentResolver().openInputStream(uri));
|
2017-09-12 20:30:44 +02:00
|
|
|
} else {
|
2017-09-13 14:51:35 +02:00
|
|
|
displayPhotoSphere(getContentResolver().openInputStream(uri), metadata);
|
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) {
|
2017-09-15 01:30:36 +02:00
|
|
|
Log.e(TAG, "File not found.", e);
|
|
|
|
Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show();
|
2017-09-12 20:30:44 +02:00
|
|
|
} catch (IOException e) {
|
2017-09-15 01:30:36 +02:00
|
|
|
Log.e(TAG, "IOException: ", e);
|
|
|
|
Toast.makeText(this, R.string.toast_io_error, Toast.LENGTH_SHORT).show();
|
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);
|
2017-09-13 23:44:58 +02:00
|
|
|
String xml = PhotoSphereParser.getXMLContent(inputStream);
|
|
|
|
PhotoSphereMetadata metadata = PhotoSphereParser.parse(xml);
|
2017-09-12 19:50:59 +02:00
|
|
|
|
2017-09-13 14:51:35 +02:00
|
|
|
if (metadata == null) {
|
|
|
|
Log.e(TAG, "Metadata is null. Fall back to flat image.");
|
|
|
|
displayFlatImage(getContentResolver().openInputStream(uri));
|
|
|
|
}
|
|
|
|
|
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);
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show();
|
2017-09-12 19:50:59 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(TAG, "IOException: ", e);
|
2017-09-15 01:30:36 +02:00
|
|
|
Toast.makeText(this, R.string.toast_io_error, Toast.LENGTH_SHORT).show();
|
2017-09-12 19:50:59 +02:00
|
|
|
}
|
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 14:51:35 +02:00
|
|
|
Log.d(TAG, "Display Photo Sphere!");
|
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 14:51:35 +02:00
|
|
|
Log.d(TAG, "Display Flat Image!");
|
2017-09-15 00:56:08 +02:00
|
|
|
displayPhotoSphere(inputStream, new PhotoSphereMetadata());
|
2017-09-12 20:30:44 +02:00
|
|
|
}
|
2017-09-14 02:23:37 +02:00
|
|
|
|
2017-09-14 20:39:33 +02:00
|
|
|
public int getStatusBarHeight() {
|
|
|
|
int result = 0;
|
|
|
|
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
|
|
|
|
if (resourceId > 0) {
|
|
|
|
result = getResources().getDimensionPixelSize(resourceId);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-12 14:54:41 +02:00
|
|
|
}
|