1
0
Fork 0
mirror of https://github.com/vanitasvitae/Spherical synced 2024-12-26 21:08:01 +01:00

Merge branch 'feature_fragments'

This commit is contained in:
vanitasvitae 2017-09-19 15:34:42 +02:00
commit f0ccbe0c26
16 changed files with 338 additions and 120 deletions

View file

@ -0,0 +1,68 @@
package de.trac.spherical;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
/**
* Created by vanitas on 17.09.17.
*/
public class FlatFragment extends ImageFragment {
private static final String TAG = "SphericalFFrag";
private SubsamplingScaleImageView imageView;
private Bitmap bitmap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_flat, parent, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
setHasOptionsMenu(true);
imageView = (SubsamplingScaleImageView) view.findViewById(R.id.image_view);
updateBitmap(getMainActivity().getBitmap());
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_flat, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_force_sphere:
getMainActivity().displayPhotoSphere();
return true;
}
return super.onOptionsItemSelected(item);
}
private MainActivity getMainActivity() {
return (MainActivity) getActivity();
}
@Override
public void updateBitmap(Bitmap bitmap) {
if (imageView == null) {
return;
}
this.bitmap = bitmap;
imageView.setImage(ImageSource.cachedBitmap(bitmap));
}
}

View file

@ -1,23 +0,0 @@
package de.trac.spherical;
import android.content.Intent;
import android.os.AsyncTask;
/**
* Created by vanitas on 15.09.17.
*/
public class HandleImageTask extends AsyncTask<Intent, Void, Void> {
private MainActivity mainActivity;
public HandleImageTask(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
@Override
protected Void doInBackground(Intent... params) {
mainActivity.handleSentImageIntent(params[0]);
return null;
}
}

View file

@ -0,0 +1,13 @@
package de.trac.spherical;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
/**
* Created by vanitas on 19.09.17.
*/
public abstract class ImageFragment extends Fragment {
public abstract void updateBitmap(Bitmap bitmap);
}

View file

@ -3,13 +3,17 @@ package de.trac.spherical;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
@ -21,38 +25,66 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import de.trac.spherical.parser.PhotoSphereMetadata;
import de.trac.spherical.parser.PhotoSphereParser;
import de.trac.spherical.rendering.PhotoSphereSurfaceView;
public class MainActivity extends AppCompatActivity {
public static final String TAG = "Spherical";
public static final String MIME_PHOTO_SPHERE = "application/vnd.google.panorama360+jpg";
public static final String MIME_IMAGE = "image/*";
private static final int PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 387;
private PhotoSphereSurfaceView surfaceView;
private FragmentManager fm;
//UI
private FloatingActionButton fab;
private Toolbar toolbar;
private GestureDetectorCompat gestureDetector;
private ProgressFragment progressFragment = new ProgressFragment();
private FlatFragment flatFragment = new FlatFragment();
private SphereFragment sphereFragment = new SphereFragment();
private ImageFragment currentlyShownImageFragment;
//Cache
private Intent cachedIntent;
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupUI();
fm = getSupportFragmentManager();
handleIntent(getIntent());
}
private void showProgressFragment() {
fm.beginTransaction().replace(R.id.container_fragment, progressFragment, "prog").commit();
this.currentlyShownImageFragment = null;
}
private void showFlatImageFragment() {
fm.beginTransaction().replace(R.id.container_fragment, flatFragment, "flat").commit();
this.currentlyShownImageFragment = flatFragment;
}
private void showSphereFragment() {
fm.beginTransaction().replace(R.id.container_fragment, sphereFragment, "sphere").commit();
this.currentlyShownImageFragment = sphereFragment;
}
private void setupUI() {
// Prepare UI
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@ -73,14 +105,8 @@ public class MainActivity extends AppCompatActivity {
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
// Initialize renderer and setup surface view.
LinearLayout container = (LinearLayout) findViewById(R.id.container);
surfaceView = new PhotoSphereSurfaceView(this);
container.addView(surfaceView);
// Detect gestures like single taps.
final GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
gestureDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent event) {
displayUI(!fab.isShown());
@ -88,21 +114,13 @@ public class MainActivity extends AppCompatActivity {
}
});
surfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
switch (intent.getAction()) {
//Image was sent into the app
case Intent.ACTION_SEND:
showProgressFragment();
checkPermissionAndHandleSentImage(intent);
break;
@ -116,25 +134,24 @@ public class MainActivity extends AppCompatActivity {
private void checkPermissionAndHandleSentImage(Intent intent) {
int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (status == PackageManager.PERMISSION_GRANTED) {
new HandleImageTask(this).doInBackground(intent);
handleSentImageIntent(intent);
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
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
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) {
new HandleImageTask(this).doInBackground(cachedIntent);
handleSentImageIntent(cachedIntent);
} else {
Toast.makeText(this, R.string.toast_missing_permission, Toast.LENGTH_LONG).show();
}
@ -161,14 +178,14 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_force_sphere:
case R.id.menu_about:
Toast.makeText(this, R.string.toast_not_yet_implemented, Toast.LENGTH_SHORT).show();
return true;
}
@ -177,11 +194,14 @@ public class MainActivity extends AppCompatActivity {
}
/**
* 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 PhotoSphereParser}.
* 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}.
* @param intent incoming intent.
*/
void handleSentImageIntent(Intent intent) {
if (intent == null) {
throw new AssertionError("Intent is null!");
}
String type = intent.getType();
if (type != null) {
@ -191,13 +211,26 @@ public class MainActivity extends AppCompatActivity {
return;
}
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");
switch (type) {
case MIME_PHOTO_SPHERE:
displayPhotoSphere(imageUri);
displayPhotoSphere();
break;
default:
displayMaybePhotoSphere(imageUri);
if (metadata != null) {
displayPhotoSphere();
} else {
displayFlatImage();
}
break;
}
@ -206,72 +239,23 @@ public class MainActivity extends AppCompatActivity {
}
}
/**
* 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 = PhotoSphereParser.getXMLContent(inputStream);
PhotoSphereMetadata metadata = PhotoSphereParser.parse(xml);
if (metadata == null || !metadata.isUsePanoramaViewer()) {
displayFlatImage(getContentResolver().openInputStream(uri));
} else {
displayPhotoSphere(getContentResolver().openInputStream(uri), metadata);
}
} catch (FileNotFoundException e) {
Log.e(TAG, "File not found.", e);
Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e(TAG, "IOException: ", e);
Toast.makeText(this, R.string.toast_io_error, Toast.LENGTH_SHORT).show();
}
}
/**
* Display a photo sphere.
* @param uri
*/
private void displayPhotoSphere(Uri uri) {
try {
InputStream inputStream = getContentResolver().openInputStream(uri);
String xml = PhotoSphereParser.getXMLContent(inputStream);
PhotoSphereMetadata metadata = PhotoSphereParser.parse(xml);
if (metadata == null) {
Log.e(TAG, "Metadata is null. Fall back to flat image.");
displayFlatImage(getContentResolver().openInputStream(uri));
}
displayPhotoSphere(getContentResolver().openInputStream(uri), metadata);
} catch (FileNotFoundException e) {
Log.e(TAG, "File not found.", e);
Toast.makeText(this, R.string.toast_file_not_found, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e(TAG, "IOException: ", e);
Toast.makeText(this, R.string.toast_io_error, Toast.LENGTH_SHORT).show();
}
}
private void displayPhotoSphere(InputStream inputStream, PhotoSphereMetadata metadata) {
surfaceView.setBitmap(BitmapFactory.decodeStream(inputStream));
Log.d(TAG, "Display Photo PhotoSphere!");
public void displayPhotoSphere() {
showSphereFragment();
currentlyShownImageFragment.updateBitmap(bitmap);
}
/**
* Display a flat image.
* @param inputStream
* Display a flat bitmap.
*/
private void displayFlatImage(InputStream inputStream) {
Log.d(TAG, "Display Flat Image!");
displayPhotoSphere(inputStream, new PhotoSphereMetadata());
public void displayFlatImage() {
showFlatImageFragment();
currentlyShownImageFragment.updateBitmap(bitmap);
}
public int getStatusBarHeight() {
private int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
@ -280,4 +264,21 @@ public class MainActivity extends AppCompatActivity {
return result;
}
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]);
return null;
}
}
}

View file

@ -0,0 +1,22 @@
package de.trac.spherical;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by vanitas on 18.09.17.
*/
public class ProgressFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_progress, parent, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
}

View file

@ -0,0 +1,80 @@
package de.trac.spherical;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import de.trac.spherical.rendering.PhotoSphereSurfaceView;
/**
* Created by vanitas on 17.09.17.
*/
public class SphereFragment extends ImageFragment implements View.OnTouchListener {
private static final String TAG = "SphericalSFrag";
private PhotoSphereSurfaceView surfaceView;
private Bitmap bitmap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_sphere, parent, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
setHasOptionsMenu(true);
FrameLayout fragmentRoot = (FrameLayout) view.findViewById(R.id.container_sphere);
surfaceView = new PhotoSphereSurfaceView(getContext());
// Initialize renderer and setup surface view.
fragmentRoot.addView(surfaceView);
surfaceView.setOnTouchListener(this);
updateBitmap(getMainActivity().getBitmap());
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return getMainActivity().getGestureDetector().onTouchEvent(event);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_sphere, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_force_flat:
getMainActivity().displayFlatImage();
return true;
}
return super.onOptionsItemSelected(item);
}
private MainActivity getMainActivity() {
return (MainActivity) getActivity();
}
@Override
public void updateBitmap(Bitmap bitmap) {
if (surfaceView == null) {
return;
}
this.bitmap = bitmap;
surfaceView.setBitmap(bitmap);
}
}

View file

@ -150,6 +150,11 @@ public class PhotoSphereParser {
return meta;
}
public static PhotoSphereMetadata parse(InputStream inputStream) throws IOException {
String xml = getXMLContent(inputStream);
return parse(xml);
}
private static void throwIfUnexpectedEOF(int actual, int expected) throws EOFException {
if (actual != expected) {
throw new EOFException("Unexpected EOF!");

View file

@ -22,14 +22,16 @@
app:theme="@style/AppTheme.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat" />
<LinearLayout
android:id="@+id/container"
<FrameLayout
android:id="@+id/container_fragment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
</LinearLayout>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
@ -43,7 +45,7 @@
android:layout_marginRight="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/container"
app:layout_anchor="@id/container_fragment"
app:layout_anchorGravity="bottom|right|end" />
</RelativeLayout>

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:indeterminate="true"/>
</RelativeLayout>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_sphere"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_about"
android:title="@string/menu_about"
app:showAsAction="never" />
</menu>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_force_flat"
android:title="@string/menu_force_flat"
app:showAsAction="never" />
</menu>

View file

@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="menu_force_sphere">Erzwinge Kugelansicht</string>
<string name="menu_force_sphere">Kugelansicht</string>
<string name="toast_file_not_found">Datei nicht gefunden.</string>
<string name="toast_io_error">Ein Fehler ist aufgetreten: IO-Error.</string>
<string name="toast_missing_permission">Foto kann nicht angezeigt werden: Fehlende Berechtigung für externen Speicher.</string>
<string name="toast_not_yet_implemented">Noch nicht implementiert!</string>
<string name="toast_prompt_share_image">Teile ein Bild mit der App!</string>
<string name="menu_about">Über Spherical</string>
<string name="menu_force_flat">Flache Ansicht</string>
</resources>

View file

@ -3,7 +3,9 @@
<string name="toast_prompt_share_image">Share an image with the app!</string>
<string name="toast_file_not_found">File not found.</string>
<string name="toast_io_error">An Error has occurred: IO-Error.</string>
<string name="menu_force_sphere">Show as Sphere</string>
<string name="menu_force_sphere">Sphere View</string>
<string name="toast_not_yet_implemented">Not yet implemented!</string>
<string name="toast_missing_permission">Cannot display photo: Missing permissions to access external storage.</string>
<string name="menu_force_flat">Flat View</string>
<string name="menu_about">About Spherical</string>
</resources>