<repo_name>emittam/PamphletMaker<file_sep>/app/src/main/java/net/emittam/pamphletmaker/utils/BindingUtils.java
package net.emittam.pamphletmaker.utils;

import android.databinding.BindingAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import net.emittam.pamphletmaker.R;

/**
 * Created by kato-h on 16/11/26.
 */

public class BindingUtils {
    @BindingAdapter({"imageUrl"})
    public static void loadImage(ImageView view, String url) {
        Picasso.with(view.getContext()).load(url).placeholder(R.drawable.noimage).resize(480, 270).centerInside().into(view);
    }
}
<file_sep>/app/src/main/java/net/emittam/pamphletmaker/ImageCollectBroadcastReceiver.java
package net.emittam.pamphletmaker;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by kato-h on 16/11/26.
 */

public class ImageCollectBroadcastReceiver extends BroadcastReceiver {

    private static final String NEW_PHOTO_ACTION = "android.hardware.action.NEW_PICTURE";

    public static final String SAVE_IMAGE_URL_KEY = "image.url.set";

    public static final String SAVE_FILE_PATH_KEY = "save.filepath.key";

    @Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(context);
        String saveKey = defaultPref.getString(SAVE_FILE_PATH_KEY, null);

        int permissionCheck = ContextCompat.checkSelfPermission(
                context,
                Manifest.permission.READ_EXTERNAL_STORAGE
        );

        if (intent.getAction().equals(NEW_PHOTO_ACTION)
                && saveKey != null
                && android.content.pm.PackageManager.PERMISSION_GRANTED == permissionCheck) {
            SharedPreferences prefs = context.getSharedPreferences(saveKey, Context.MODE_PRIVATE);
            Set<String> savedSet = prefs.getStringSet(SAVE_IMAGE_URL_KEY, new HashSet<String>());
            savedSet = new HashSet<>(savedSet);
            String[] CONTENT_PROJECTION = {
                    MediaStore.Images.Media.DATA,
            };

            Cursor c = context.getContentResolver().query(intent.getData(), CONTENT_PROJECTION, null, null, null);
            if (c == null || !c.moveToFirst()) {
                if (c != null) {
                    c.close();
                }
            }
            String localPath = c.getString(c.getColumnIndex(MediaStore.Images.Media.DATA));
            c.close();

            String url = "file://" + localPath;
            if (!savedSet.contains(url)) {
                savedSet.add(url);
            }
            SharedPreferences.Editor editor = prefs.edit();
            editor.putStringSet(SAVE_IMAGE_URL_KEY, savedSet);
            editor.commit();
        }
    }
}
<file_sep>/app/src/main/java/net/emittam/pamphletmaker/PamphletViewerActivity.java
package net.emittam.pamphletmaker;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

/**
 * Created by kato-h on 16/11/26.
 */

public class PamphletViewerActivity extends AppCompatActivity {

    // X軸最低スワイプ距離
    private static final int SWIPE_MIN_DISTANCE = 50;

    // X軸最低スワイプスピード
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    // Y軸の移動距離　これ以上なら横移動を判定しない
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private GestureDetector mGestureDetector;

    private int mCurrentPage = 0;
    private String mKey;
    private String mText;

    public static void startActivity(Activity base, String key, String text) {
        Intent intent = new Intent(base, PamphletViewerActivity.class);
        intent.putExtra("key", key);
        intent.putExtra("text", text);
        base.startActivity(intent);
    }

    private static final int NOTIFICATION_ID = 998;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("パンフレット");

        setContentView(R.layout.activity_pamphlet_viewer);
        mKey = getIntent().getStringExtra("key");
        mText = getIntent().getStringExtra("text");

        LeftPamphletViewerFragment fragment = LeftPamphletViewerFragment.createInstance(mCurrentPage);
        LeftPamphletViewerModel model = ImageRepository.getInstance(getApplicationContext(), mKey).getLeftModel(mCurrentPage);
        model.setText(mText);
        fragment.setViewModel(model);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.pamphlet_fragment, fragment);
        transaction.commit();
        mGestureDetector = new GestureDetector(this, mOnGestureListener);

        NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
        manager.cancel(NOTIFICATION_ID);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return mGestureDetector.onTouchEvent(event);
    }

    private final GestureDetector.SimpleOnGestureListener mOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
        // フリックイベント
        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
            Log.d("debug", "onFling");
            try {
                float distance_x = Math.abs((event1.getX() - event2.getX()));
                float velocity_x = Math.abs(velocityX);

                // Y軸の移動距離が大きすぎる場合
                if (Math.abs(event1.getY() - event2.getY()) > SWIPE_MAX_OFF_PATH) {

                }
                // 開始位置から終了位置の移動距離が指定値より大きい
                // X軸の移動速度が指定値より大きい
                else if  (event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //Toast.makeText(PamphletViewerActivity.this, "左から右", Toast.LENGTH_SHORT).show();
                    if (mCurrentPage >= 0) {
                        mCurrentPage--;
                        LeftPamphletViewerFragment fragment = LeftPamphletViewerFragment.createInstance(mCurrentPage);
                        LeftPamphletViewerModel model = ImageRepository.getInstance(getApplicationContext(), mKey).getLeftModel(mCurrentPage);
                        model.setText(mText);
                        fragment.setViewModel(model);
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.pamphlet_fragment, fragment);
                        transaction.commit();
                    }

                }
                // 終了位置から開始位置の移動距離が指定値より大きい
                // X軸の移動速度が指定値より大きい
                else if (event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    //Toast.makeText(PamphletViewerActivity.this, "右から左", Toast.LENGTH_SHORT).show();
                    if (mCurrentPage < ImageRepository.getInstance(getApplicationContext(), mKey).getImageCount() / 2) {
                        mCurrentPage++;
                        LeftPamphletViewerFragment fragment = LeftPamphletViewerFragment.createInstance(mCurrentPage);
                        LeftPamphletViewerModel model = ImageRepository.getInstance(getApplicationContext(), mKey).getLeftModel(mCurrentPage);
                        model.setText(mText);
                        fragment.setViewModel(model);
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.pamphlet_fragment, fragment);
                        transaction.commit();
                    }
                }

            } catch (Exception e) {
                // TODO
            }

            return false;
        }
    };
}
<file_sep>/app/src/main/java/net/emittam/pamphletmaker/MainActivity.java
package net.emittam.pamphletmaker;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.BaseObservable;
import android.databinding.DataBindingUtil;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import net.emittam.pamphletmaker.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    public class MainPresenter extends BaseObservable {
        public void onButtonClick() {
//            SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
//            SharedPreferences.Editor editor = defaultPref.edit();
//            editor.putString(ImageCollectBroadcastReceiver.SAVE_FILE_PATH_KEY, "test2");
//            editor.commit();
//            Toast.makeText(MainActivity.this, "好きなカメラアプリで写真を撮ってください", Toast.LENGTH_LONG).show();
//            finish();
            SelectorActivity.startActivity(MainActivity.this, false);
        }

        public void onTestButtonClick() {
            SelectorActivity.startActivity(MainActivity.this, true);
        }
    }

    private MainPresenter mMainPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("パンフレットメーカー");
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mMainPresenter = new MainPresenter();
        binding.setPresenter(mMainPresenter);
    }
}
