1
0
Fork 0
mirror of https://github.com/vanitasvitae/Spherical synced 2024-11-24 13:22:08 +01:00

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;
/**
* Created by vanitas on 17.09.17.
* Fragment containing an ImageView which displays the unfolded image.
*/
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) {
@ -62,7 +61,6 @@ public class FlatFragment extends ImageFragment {
if (imageView == null) {
return;
}
this.bitmap = bitmap;
imageView.setImage(ImageSource.cachedBitmap(bitmap));
}
}

View file

@ -6,9 +6,9 @@ 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;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentManager;
@ -69,21 +69,9 @@ public class MainActivity extends AppCompatActivity {
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;
}
/**
* Initialize the user interface.
*/
private void setupUI() {
// Prepare UI
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) {
switch (intent.getAction()) {
//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) {
int status = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (status == PackageManager.PERMISSION_GRANTED) {
@ -145,7 +142,7 @@ public class MainActivity extends AppCompatActivity {
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_READ_EXTERNAL_STORAGE: {
// 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
* 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.
*/
@ -255,6 +271,11 @@ public class MainActivity extends AppCompatActivity {
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() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
@ -271,14 +292,4 @@ public class MainActivity extends AppCompatActivity {
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

@ -15,14 +15,13 @@ import android.widget.FrameLayout;
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 {
private static final String TAG = "SphericalSFrag";
private PhotoSphereSurfaceView surfaceView;
private Bitmap bitmap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
@ -74,7 +73,6 @@ public class SphereFragment extends ImageFragment implements View.OnTouchListene
if (surfaceView == null) {
return;
}
this.bitmap = bitmap;
surfaceView.setBitmap(bitmap);
}
}

View file

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