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

Use Butterknife, properly display progressFragment

This commit is contained in:
Paul Schaub 2018-02-20 14:56:53 +01:00
parent 7932d00cdc
commit 92c84e8281
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
9 changed files with 177 additions and 139 deletions

View file

@ -14,7 +14,6 @@ android {
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
@ -30,4 +29,6 @@ dependencies {
//External libs
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}

View file

@ -3,6 +3,7 @@
package="de.trac.spherical">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<application
android:allowBackup="false"

View file

@ -14,18 +14,18 @@ public class BroadcastHelper {
private static final String INTENT_ACTION = "de.spherical.internal";
private static final String INTENT_KEY_NAME = "broadcast_type";
public static final IntentFilter INTENT_FILTER = new IntentFilter(INTENT_ACTION); // TODO: useful?
public static final IntentFilter INTENT_FILTER = new IntentFilter(INTENT_ACTION);
static public void broadcast(Context context, BroadcastType type) {
public static void broadcast(Context context, BroadcastType type) {
Intent intent = new Intent(INTENT_ACTION);
intent.putExtra(INTENT_KEY_NAME, type.name());
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
static public BroadcastType getBroadcastType(Intent intent) {
public static BroadcastType getBroadcastType(Intent intent) {
if(!INTENT_ACTION.equals(intent.getAction()))
throw new IllegalArgumentException("Not a valid intent");
return (BroadcastType) intent.getSerializableExtra(INTENT_KEY_NAME);
return BroadcastType.valueOf(intent.getSerializableExtra(INTENT_KEY_NAME).toString());
}
}

View file

@ -13,6 +13,9 @@ import android.view.ViewGroup;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Fragment containing an ImageView which displays the unfolded image.
*/
@ -20,20 +23,25 @@ public class FlatFragment extends ImageFragment {
private static final String TAG = "SphericalFFrag";
private SubsamplingScaleImageView imageView;
@BindView(R.id.image_view)
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);
View view = inflater.inflate(R.layout.fragment_flat, parent, false);
ButterKnife.bind(this, view);
return view;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Log.d(TAG, "onViewCreated");
updateBitmap(bitmap);
BroadcastHelper.broadcast(getContext(), BroadcastHelper.BroadcastType.PROGRESS_FINISHED);
setHasOptionsMenu(true);
imageView = (SubsamplingScaleImageView) view.findViewById(R.id.image_view);
updateBitmap(getMainActivity().getBitmap());
}
@Override
@ -44,19 +52,14 @@ public class FlatFragment extends ImageFragment {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_force_sphere:
getMainActivity().displayPhotoSphere();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void updateBitmap(Bitmap bitmap) {
if (imageView == null) {
return;
this.bitmap = bitmap;
if (imageView != null && bitmap != null) {
imageView.setImage(ImageSource.cachedBitmap(bitmap));
}
imageView.setImage(ImageSource.cachedBitmap(bitmap));
}
}

View file

@ -6,8 +6,4 @@ import android.support.v4.app.Fragment;
public abstract class ImageFragment extends Fragment {
public abstract void updateBitmap(Bitmap bitmap);
public MainActivity getMainActivity() {
return (MainActivity) getActivity();
}
}

View file

@ -2,14 +2,10 @@ package de.trac.spherical;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
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.annotation.NonNull;
@ -33,32 +29,45 @@ import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import de.trac.spherical.parser.PhotoSphereMetadata;
import butterknife.BindView;
import butterknife.ButterKnife;
import de.trac.spherical.parser.PhotoSphereParser;
import static de.trac.spherical.BroadcastHelper.BroadcastType.PROGRESS_FINISHED;
import static de.trac.spherical.BroadcastHelper.BroadcastType.PROGRESS_START;
import de.trac.spherical.rendering.PhotoSphereSurfaceView;
import de.trac.spherical.util.LoadImageTask;
public class MainActivity extends AppCompatActivity {
public class MainActivity extends AppCompatActivity implements LoadImageTask.FinishedCallback {
public static final String TAG = "Spherical";
public static final String MIME_PHOTO_SPHERE = "application/vnd.google.panorama360+jpg";
private static final int PERMISSION_REQUEST_READ_EXTERNAL_STORAGE = 387;
// UI
private FloatingActionButton actionButton;
private Toolbar toolbar;
@BindView(R.id.fab)
FloatingActionButton actionButton;
@BindView(R.id.toolbar)
Toolbar toolbar;
private GestureDetectorCompat gestureDetector;
// Cache
private Intent cachedIntent;
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
private LoadImageTask.Result image;
@Override
public void onImageLoadingFinished(LoadImageTask.Result result) {
this.image = result;
if (result.getMetadata() == null) {
showFragment(FragmentType.FLAT);
} else {
showFragment(FragmentType.SPHERE);
}
((ImageFragment) currentFragment).updateBitmap(result.getBitmap());
}
// Fragments
private enum FragmentType {
@ -76,6 +85,7 @@ public class MainActivity extends AppCompatActivity {
private FragmentManager fragmentManager;
private Map<FragmentType, Fragment> fragments;
private Fragment currentFragment;
// Broadcast handling.
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@ -83,19 +93,22 @@ public class MainActivity extends AppCompatActivity {
public void onReceive(Context context, Intent intent) {
switch (BroadcastHelper.getBroadcastType(intent)) {
case PROGRESS_START:
fragmentManager.beginTransaction().add(R.id.container_fragment, fragments.get(FragmentType.PROGRESS)).commitAllowingStateLoss();
break;
case PROGRESS_FINISHED:
fragmentManager.beginTransaction().remove(fragments.get(FragmentType.PROGRESS)).commitAllowingStateLoss();
break;
default:
break;
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
setupUI();
// Init fragments.
@ -122,25 +135,28 @@ public class MainActivity extends AppCompatActivity {
*/
private void setupUI() {
// Prepare UI
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Dirty hacks
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) toolbar.getLayoutParams();
lp.topMargin += getStatusBarHeight();
toolbar.bringToFront();
actionButton = (FloatingActionButton) findViewById(R.id.fab);
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sphereFragment.toggleUseTouchInput();
setUIVisibility(false);
}
});
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);
}
actionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoSphereSurfaceView photoSphereSurfaceView =
((SphereFragment)fragments.get(FragmentType.SPHERE)).getSurfaceView();
photoSphereSurfaceView.setUseTouchInput(!photoSphereSurfaceView.getUseTouchInput());
setUIVisibility(false);
}
});
// Detect gestures like single taps.
gestureDetector = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() {
@Override
@ -157,7 +173,8 @@ public class MainActivity extends AppCompatActivity {
* @param intent incoming intent.
*/
private void handleIntent(Intent intent) {
switch (intent.getAction()) {
String action = intent.getAction() != null ? intent.getAction() : "";
switch (action) {
//Image was sent into the app
case Intent.ACTION_SEND:
showFragment(FragmentType.SPHERE);
@ -227,7 +244,8 @@ public class MainActivity extends AppCompatActivity {
}
// process image asynchronous.
new LoadImageTask(this, getContentResolver(), imageUri).execute();
BroadcastHelper.broadcast(this, BroadcastHelper.BroadcastType.PROGRESS_START);
new LoadImageTask(getContentResolver(), imageUri, type, this).execute();
}
/**
@ -249,7 +267,8 @@ public class MainActivity extends AppCompatActivity {
* @param type fragment to be shown
*/
private void showFragment(FragmentType type) {
fragmentManager.beginTransaction().replace(R.id.container_fragment, fragments.get(type), type.tag).commit();
currentFragment = fragments.get(type);
fragmentManager.beginTransaction().replace(R.id.container_fragment, currentFragment, type.tag).commit();
}
@Override
@ -271,27 +290,20 @@ public class MainActivity extends AppCompatActivity {
case R.id.menu_about:
Toast.makeText(this, R.string.toast_not_yet_implemented, Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_force_sphere:
showFragment(FragmentType.SPHERE);
((ImageFragment) currentFragment).updateBitmap(image.getBitmap());
return true;
case R.id.menu_force_flat:
showFragment(FragmentType.FLAT);
((ImageFragment) currentFragment).updateBitmap(image.getBitmap());
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Display a photo sphere.
*/
public void displayPhotoSphere() {
showFragment(FragmentType.SPHERE);
currentlyShownImageFragment.updateBitmap(bitmap);
}
/**
* Display a flat bitmap.
*/
public void displayFlatImage() {
showFragment(FragmentType.FLAT);
currentlyShownImageFragment.updateBitmap(bitmap);
}
/**
* Convenience method because android sux.
* Returns the height of the status bar in dp.
@ -310,59 +322,4 @@ public class MainActivity extends AppCompatActivity {
return gestureDetector;
}
/**
* Dedicated async tasks to load an image.
* Takes a progress reporter and informs it about the changes.
*/
private static class LoadImageTask extends AsyncTask<Void, Void, Void> {
private ContentResolver contentResolver;
private Uri uri;
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
private Context context;
LoadImageTask(Context context, ContentResolver contentResolver, Uri uri) {
this.context = context;
this.contentResolver = contentResolver;
this.uri = uri;
}
@Override
protected void onPreExecute() {
BroadcastHelper.broadcast(context, PROGRESS_START);
}
@Override
protected Void doInBackground(Void... params) {
try {
bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
metadata = PhotoSphereParser.parse(contentResolver.openInputStream(uri));
} catch (IOException | OutOfMemoryError e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
BroadcastHelper.broadcast(context, PROGRESS_FINISHED);
switch (type) {
case MIME_PHOTO_SPHERE:
showFragment(FragmentType.SPHERE);
break;
default:
if (metadata != null) {
displayPhotoSphere();
} else {
displayFlatImage();
}
break;
}
}
}
}

View file

@ -2,6 +2,7 @@ package de.trac.spherical;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.GestureDetectorCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
@ -12,6 +13,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import de.trac.spherical.rendering.PhotoSphereSurfaceView;
/**
@ -21,31 +24,37 @@ public class SphereFragment extends ImageFragment implements View.OnTouchListene
private static final String TAG = "SphericalSFrag";
@BindView(R.id.container_sphere)
FrameLayout frameLayout;
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);
View view = inflater.inflate(R.layout.fragment_sphere, parent, false);
ButterKnife.bind(this, view);
return view;
}
@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);
frameLayout.addView(surfaceView);
surfaceView.setOnTouchListener(this);
updateBitmap(getMainActivity().getBitmap());
updateBitmap(bitmap);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return getMainActivity().getGestureDetector().onTouchEvent(event);
MainActivity activity = ((MainActivity)getActivity());
if (activity != null) {
return activity.getGestureDetector().onTouchEvent(event);
}
return false;
}
@Override
@ -56,20 +65,15 @@ public class SphereFragment extends ImageFragment implements View.OnTouchListene
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_force_flat:
getMainActivity().displayFlatImage();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void updateBitmap(Bitmap bitmap) {
if (surfaceView == null) {
return;
this.bitmap = bitmap;
if (surfaceView != null && bitmap != null) {
surfaceView.setBitmap(bitmap);
}
surfaceView.setBitmap(bitmap);
}
public PhotoSphereSurfaceView getSurfaceView() {

View file

@ -203,12 +203,12 @@ public class PhotoSphereRenderer implements GLSurfaceView.Renderer {
// Tell the main activity we are going to do an expensive operation.
BroadcastHelper.broadcast(surfaceView.getContext(), BroadcastHelper.BroadcastType.PROGRESS_START);
// Check if requestedBitmap needs to be downsampled.
// Check if requestedBitmap needs to be down-sampled.
int [] maxTextureSize = new int[1];
glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxTextureSize, 0);
float maxSize = Math.max(requestedBitmap.getWidth(), requestedBitmap.getHeight());
if(maxSize > maxTextureSize[0]) { // TODO: implement tiling technique
Log.w(TAG, "Image too big, exceeding " + maxTextureSize[0] + " : will be downsampled");
Log.w(TAG, "Image too big, exceeding " + maxTextureSize[0] + " : will be down-sampled");
int newWidth = (int) (requestedBitmap.getWidth() * maxTextureSize[0] / maxSize);
int newHeight = (int) (requestedBitmap.getHeight() * maxTextureSize[0] / maxSize);
requestedBitmap = Bitmap.createScaledBitmap(requestedBitmap, newWidth, newHeight, true);

View file

@ -0,0 +1,76 @@
package de.trac.spherical.util;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import java.io.IOException;
import de.trac.spherical.parser.PhotoSphereMetadata;
import de.trac.spherical.parser.PhotoSphereParser;
/**
* Dedicated async tasks to load an image.
* Takes a progress reporter and informs it about the changes.
*/
public class LoadImageTask extends AsyncTask<Void, Void, LoadImageTask.Result> {
private ContentResolver contentResolver;
private Uri uri;
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
private final String type;
private final FinishedCallback callback;
public LoadImageTask(ContentResolver contentResolver, Uri uri, String type, FinishedCallback callback) {
this.callback = callback;
this.contentResolver = contentResolver;
this.uri = uri;
this.type = type;
}
@Override
protected Result doInBackground(Void... params) {
try {
bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
metadata = PhotoSphereParser.parse(contentResolver.openInputStream(uri));
return new Result(bitmap, metadata);
} catch (IOException | OutOfMemoryError e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Result result) {
callback.onImageLoadingFinished(result);
}
public interface FinishedCallback {
void onImageLoadingFinished(Result result);
}
public static class Result {
private Bitmap bitmap;
private PhotoSphereMetadata metadata;
public Result(Bitmap bitmap, PhotoSphereMetadata metadata) {
this.bitmap = bitmap;
this.metadata = metadata;
}
public Bitmap getBitmap() {
return bitmap;
}
public PhotoSphereMetadata getMetadata() {
return metadata;
}
}
}