package com.greenaddress.abcore;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.EditText;

import org.apache.commons.compress.utils.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class BitcoinConfEditActivity extends AppCompatActivity {
    private final static String TAG = BitcoinConfEditActivity.class.getName();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bitcoin_conf_edit);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // save file
        OutputStream f = null;
        try {
            f = new FileOutputStream(Utils.getBitcoinConf(this));
            IOUtils.copy(new ByteArrayInputStream(((EditText) findViewById(R.id.editText))
                    .getText().toString().getBytes("UTF-8")), f);

        } catch (final IOException e) {
            Log.i(TAG, e.getMessage());
        } finally {
            IOUtils.closeQuietly(f);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // load file
        try {
            DownloadInstallCoreIntentService.configureCore(this);
            final InputStream f = new FileInputStream(Utils.getBitcoinConf(this));
            ((EditText) findViewById(R.id.editText))
                    .setText(new String(IOUtils.toByteArray(
                            f)));
            IOUtils.closeQuietly(f);
        } catch (final IOException e) {
            Log.i(TAG, e.getMessage());
        }
    }
}

@@ -46,17 +46,179 @@
 oid.
-os.Bundle
+Manifest;%0Aimport android.content.pm.PackageManager;%0Aimport android.os.Bundle;%0Aimport android.support.v4.app.ActivityCompat;%0Aimport android.support.v4.content.ContextCompat
 ;%0Aim
@@ -961,24 +961,499 @@
 .onPause();%0A
+        if (ContextCompat.checkSelfPermission(this,%0A                Manifest.permission.WRITE_EXTERNAL_STORAGE)%0A                != PackageManager.PERMISSION_GRANTED &&%0A                !ActivityCompat.shouldShowRequestPermissionRationale(this,%0A                    Manifest.permission.WRITE_EXTERNAL_STORAGE))%0A                ActivityCompat.requestPermissions(this,%0A                        new String%5B%5D%7BManifest.permission.WRITE_EXTERNAL_STORAGE%7D,%0A                        0);%0A%0A
         // s

add permission for EXTERNALSTORAGE (for Android M onwards)