Minor code cleanup

This commit is contained in:
vanitasvitae 2017-09-22 18:07:41 +02:00
parent f0ccbe0c26
commit e1ffec96a5
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
4 changed files with 75 additions and 67 deletions

View File

@ -14,14 +14,13 @@ import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
/** /**
* Created by vanitas on 17.09.17. * Fragment containing an ImageView which displays the unfolded image.
*/ */
public class FlatFragment extends ImageFragment { public class FlatFragment extends ImageFragment {
private static final String TAG = "SphericalFFrag"; private static final String TAG = "SphericalFFrag";
private SubsamplingScaleImageView imageView; private SubsamplingScaleImageView imageView;
private Bitmap bitmap;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
@ -62,7 +61,6 @@ public class FlatFragment extends ImageFragment {
if (imageView == null) { if (imageView == null) {
return; return;
} }
this.bitmap = bitmap;
imageView.setImage(ImageSource.cachedBitmap(bitmap)); imageView.setImage(ImageSource.cachedBitmap(bitmap));
} }
} }

View File

@ -6,9 +6,9 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
@ -69,21 +69,9 @@ public class MainActivity extends AppCompatActivity {
handleIntent(getIntent()); handleIntent(getIntent());
} }
private void showProgressFragment() { /**
fm.beginTransaction().replace(R.id.container_fragment, progressFragment, "prog").commit(); * Initialize the user interface.
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() { private void setupUI() {
// Prepare UI // Prepare UI
toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar = (Toolbar) findViewById(R.id.toolbar);
@ -116,6 +104,10 @@ public class MainActivity extends AppCompatActivity {
}); });
} }
/**
* Handle an incoming intent. Distinguish between actions and pass the intent down to respective methods.
* @param intent incoming intent.
*/
private void handleIntent(Intent intent) { private void handleIntent(Intent intent) {
switch (intent.getAction()) { switch (intent.getAction()) {
//Image was sent into the app //Image was sent into the app
@ -131,6 +123,11 @@ public class MainActivity extends AppCompatActivity {
} }
} }
/**
* Check, if we are allowed to access external storage. If we are, then handle the intent.
* Otherwise cache the intent and prompt the user to grant us access.
* @param intent incoming intent.
*/
private void checkPermissionAndHandleSentImage(Intent intent) { private void checkPermissionAndHandleSentImage(Intent intent) {
int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE); int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (status == PackageManager.PERMISSION_GRANTED) { if (status == PackageManager.PERMISSION_GRANTED) {
@ -145,7 +142,7 @@ public class MainActivity extends AppCompatActivity {
} }
@Override @Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) { switch (requestCode) {
case PERMISSION_REQUEST_READ_EXTERNAL_STORAGE: { case PERMISSION_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty. // If request is cancelled, the result arrays are empty.
@ -159,40 +156,6 @@ public class MainActivity extends AppCompatActivity {
} }
} }
private void displayUI(boolean display) {
if (display) {
fab.show();
toolbar.setVisibility(View.VISIBLE);
} else {
fab.setVisibility(View.INVISIBLE);
toolbar.setVisibility(View.GONE);
}
}
@Override
public void onResume() {
super.onResume();
displayUI(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Toast.makeText(this, R.string.toast_not_yet_implemented, Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
/** /**
* Distinguish type of sent bitmap. Images with the MIME type of a photosphere will be directly * 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}. * displayed, while images with MIME type bitmap/* are being manually tested using {@link PhotoSphereParser}.
@ -239,6 +202,59 @@ public class MainActivity extends AppCompatActivity {
} }
} }
/**
* Show/hide the FAB and toolbar.
* @param display show/hide
*/
private void displayUI(boolean display) {
if (display) {
fab.show();
toolbar.setVisibility(View.VISIBLE);
} else {
fab.setVisibility(View.INVISIBLE);
toolbar.setVisibility(View.GONE);
}
}
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;
}
@Override
public void onResume() {
super.onResume();
displayUI(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Toast.makeText(this, R.string.toast_not_yet_implemented, Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
/** /**
* Display a photo sphere. * Display a photo sphere.
*/ */
@ -255,6 +271,11 @@ public class MainActivity extends AppCompatActivity {
currentlyShownImageFragment.updateBitmap(bitmap); currentlyShownImageFragment.updateBitmap(bitmap);
} }
/**
* Convenience method because android sux.
* Returns the height of the status bar in dp.
* @return height of status bar.
*/
private int getStatusBarHeight() { private int getStatusBarHeight() {
int result = 0; int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
@ -271,14 +292,4 @@ public class MainActivity extends AppCompatActivity {
public Bitmap getBitmap() { public Bitmap getBitmap() {
return bitmap; return bitmap;
} }
private class HandleSentImageTask extends AsyncTask<Intent, Void, Void> {
@Override
protected Void doInBackground(Intent... params) {
handleSentImageIntent(params[0]);
return null;
}
}
} }

View File

@ -15,14 +15,13 @@ import android.widget.FrameLayout;
import de.trac.spherical.rendering.PhotoSphereSurfaceView; import de.trac.spherical.rendering.PhotoSphereSurfaceView;
/** /**
* Created by vanitas on 17.09.17. * Fragment containing a PhotoSphereSurfaceView which displays the image projected on a sphere.
*/ */
public class SphereFragment extends ImageFragment implements View.OnTouchListener { public class SphereFragment extends ImageFragment implements View.OnTouchListener {
private static final String TAG = "SphericalSFrag"; private static final String TAG = "SphericalSFrag";
private PhotoSphereSurfaceView surfaceView; private PhotoSphereSurfaceView surfaceView;
private Bitmap bitmap;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
@ -74,7 +73,6 @@ public class SphereFragment extends ImageFragment implements View.OnTouchListene
if (surfaceView == null) { if (surfaceView == null) {
return; return;
} }
this.bitmap = bitmap;
surfaceView.setBitmap(bitmap); surfaceView.setBitmap(bitmap);
} }
} }

View File

@ -7,6 +7,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="false" android:fitsSystemWindows="false"
android:background="@android:color/background_dark"
tools:context="de.trac.spherical.MainActivity"> tools:context="de.trac.spherical.MainActivity">
<android.support.v7.widget.Toolbar <android.support.v7.widget.Toolbar