Spherical/app/src/main/java/de/trac/spherical/MainActivity.java

285 lines
9.3 KiB
Java
Raw Normal View History

2017-09-12 14:54:41 +02:00
package de.trac.spherical;
import android.Manifest;
2017-09-12 18:11:58 +02:00
import android.content.Intent;
import android.content.pm.PackageManager;
2017-09-18 02:37:17 +02:00
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
2017-09-12 19:50:59 +02:00
import android.net.Uri;
2017-09-18 02:37:17 +02:00
import android.os.AsyncTask;
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;
import android.support.v4.app.ActivityCompat;
2017-09-18 02:37:17 +02:00
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
2017-09-18 02:37:17 +02:00
import android.support.v4.view.GestureDetectorCompat;
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;
import android.view.GestureDetector;
2017-09-14 20:39:33 +02:00
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
2017-09-12 19:50:59 +02:00
import android.widget.Toast;
import java.io.IOException;
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;
import de.trac.spherical.rendering.PhotoSphereSurfaceView;
2017-09-13 01:11:40 +02:00
2017-09-18 02:37:17 +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";
private static final int PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 387;
2017-09-18 02:37:17 +02:00
private FragmentManager fm;
//UI
2017-09-14 03:06:09 +02:00
private FloatingActionButton fab;
2017-09-14 20:39:33 +02:00
private Toolbar toolbar;
2017-09-18 02:37:17 +02:00
private GestureDetectorCompat gestureDetector;
2017-09-12 18:11:58 +02:00
2017-09-19 15:34:03 +02:00
private ProgressFragment progressFragment = new ProgressFragment();
private FlatFragment flatFragment = new FlatFragment();
private SphereFragment sphereFragment = new SphereFragment();
private ImageFragment currentlyShownImageFragment;
2017-09-18 02:37:17 +02:00
//Cache
private Intent cachedIntent;
2017-09-18 02:37:17 +02:00
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
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-18 02:37:17 +02:00
setupUI();
fm = getSupportFragmentManager();
2017-09-18 02:37:17 +02:00
handleIntent(getIntent());
}
2017-09-19 15:34:03 +02:00
private void showProgressFragment() {
2017-09-18 02:37:17 +02:00
fm.beginTransaction().replace(R.id.container_fragment, progressFragment, "prog").commit();
2017-09-19 15:34:03 +02:00
this.currentlyShownImageFragment = null;
2017-09-18 02:37:17 +02:00
}
2017-09-19 15:34:03 +02:00
private void showFlatImageFragment() {
2017-09-18 02:37:17 +02:00
fm.beginTransaction().replace(R.id.container_fragment, flatFragment, "flat").commit();
2017-09-19 15:34:03 +02:00
this.currentlyShownImageFragment = flatFragment;
2017-09-18 02:37:17 +02:00
}
2017-09-19 15:34:03 +02:00
private void showSphereFragment() {
2017-09-18 02:37:17 +02:00
fm.beginTransaction().replace(R.id.container_fragment, sphereFragment, "sphere").commit();
2017-09-19 15:34:03 +02:00
this.currentlyShownImageFragment = sphereFragment;
2017-09-18 02:37:17 +02:00
}
private void setupUI() {
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);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
2017-09-14 20:39:33 +02:00
lp.topMargin += getStatusBarHeight();
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) {
PhotoSphereSurfaceView.USE_TOUCH = !PhotoSphereSurfaceView.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
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);
}
// Detect gestures like single taps.
2017-09-18 02:37:17 +02:00
gestureDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
2017-09-14 20:39:33 +02:00
displayUI(!fab.isShown());
return true;
}
2017-09-14 13:16:25 +02:00
});
}
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-19 15:34:03 +02:00
showProgressFragment();
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
}
}
private void checkPermissionAndHandleSentImage(Intent intent) {
int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (status == PackageManager.PERMISSION_GRANTED) {
2017-09-19 15:34:03 +02:00
handleSentImageIntent(intent);
2017-09-18 02:37:17 +02:00
return;
}
// Cache intent and request permission
this.cachedIntent = intent;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_READ_EXTERNAL_STORAGE);
}
@Override
2017-09-18 02:37:17 +02:00
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-19 15:34:03 +02:00
handleSentImageIntent(cachedIntent);
} else {
2017-09-15 01:30:36 +02:00
Toast.makeText(this, R.string.toast_missing_permission, Toast.LENGTH_LONG).show();
}
}
}
}
2017-09-14 20:39:33 +02:00
private void displayUI(boolean display) {
if (display) {
fab.show();
toolbar.setVisibility(View.VISIBLE);
2017-09-14 20:39:33 +02:00
} else {
fab.setVisibility(View.INVISIBLE);
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();
2017-09-19 15:34:03 +02:00
inflater.inflate(R.menu.menu_main, menu);
2017-09-14 20:39:33 +02:00
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
2017-09-19 15:34:03 +02:00
case R.id.menu_about:
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
/**
2017-09-18 02:37:17 +02:00
* Distinguish type of sent bitmap. Images with the MIME type of a photosphere will be directly
* displayed, while images with MIME type bitmap/* 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-19 15:34:03 +02:00
if (intent == null) {
throw new AssertionError("Intent is null!");
}
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-18 02:37:17 +02:00
Log.d(TAG, "START LOADING BITMAP");
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
metadata = PhotoSphereParser.parse(getContentResolver().openInputStream(imageUri));
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, "FINISHED LOADING BITMAP");
2017-09-12 19:50:59 +02:00
switch (type) {
2017-09-12 20:30:44 +02:00
case MIME_PHOTO_SPHERE:
2017-09-18 02:37:17 +02:00
displayPhotoSphere();
2017-09-12 20:30:44 +02:00
break;
2017-09-12 19:50:59 +02:00
default:
2017-09-18 02:37:17 +02:00
if (metadata != null) {
displayPhotoSphere();
} else {
displayFlatImage();
}
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();
}
}
/**
* Display a photo sphere.
*/
2017-09-19 15:34:03 +02:00
public void displayPhotoSphere() {
showSphereFragment();
currentlyShownImageFragment.updateBitmap(bitmap);
2017-09-13 01:10:29 +02:00
}
2017-09-12 20:30:44 +02:00
/**
2017-09-18 02:37:17 +02:00
* Display a flat bitmap.
2017-09-12 20:30:44 +02:00
*/
2017-09-19 15:34:03 +02:00
public void displayFlatImage() {
showFlatImageFragment();
currentlyShownImageFragment.updateBitmap(bitmap);
2017-09-12 20:30:44 +02:00
}
2017-09-18 02:37:17 +02:00
private int getStatusBarHeight() {
2017-09-14 20:39:33 +02:00
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
2017-09-18 02:37:17 +02:00
public GestureDetectorCompat getGestureDetector() {
return gestureDetector;
}
public Bitmap getBitmap() {
return bitmap;
}
private class HandleSentImageTask extends AsyncTask<Intent, Void, Void> {
@Override
protected Void doInBackground(Intent... params) {
handleSentImageIntent(params[0]);
2017-09-19 15:34:03 +02:00
2017-09-18 02:37:17 +02:00
return null;
}
}
2017-09-12 14:54:41 +02:00
}