package org.zeroxlab.momodict;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.TextView;

import org.zeroxlab.momodict.reader.Reader;

import java.io.File;

public class FileImportActivity extends AppCompatActivity {

    private Button mButton;
    private TextView mText;

    private boolean mExists = false;

    private static final int REQ_CODE_READ_EXTERNAL = 0x42;

    private static final String sEXT = Environment.getExternalStorageDirectory().getPath();
    private static final String sPATH = sEXT + "/test-dict.tar.bz2";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_import);
        initViews();
    }

    @Override
    public void onStart() {
        super.onStart();
        checkPermission();
    }

    private void initViews() {
        mButton = (Button) findViewById(R.id.btn_1);
        mText = (TextView) findViewById(R.id.text_1);

        File dict = new File(sPATH);
        mExists = dict.exists();
        mText.setText(mExists
                ? String.format("Using file: %s", sPATH)
                : String.format("File %s not exists", sPATH));
        mButton.setEnabled(mExists);

        mButton.setOnClickListener((v) -> {
            Reader reader = new Reader(getCacheDir().getPath(), sPATH);
            reader.parse(this);
            Intent intent = new Intent();
            //intent.setData(Uri.parse(sPATH));
            //setResult(Activity.RESULT_OK, intent);
            //finish();
        });
    }

    private void checkPermission() {
        int readPermission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (!(readPermission == PackageManager.PERMISSION_GRANTED)) {
            mButton.setEnabled(false);
            ActivityCompat.requestPermissions(
                    this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
                    REQ_CODE_READ_EXTERNAL);
        }
    }

    @Override
    public void onRequestPermissionsResult(int reqCode, String[] permissions, int[] response) {
        if (reqCode == REQ_CODE_READ_EXTERNAL
                && response[0] == PackageManager.PERMISSION_GRANTED) {
            mButton.setEnabled(mExists);
        }
    }
}

@@ -50,16 +50,45 @@
 nifest;%0A
+import android.app.Activity;%0A
 import a
@@ -106,24 +106,24 @@
 ent.Intent;%0A
-
 import andro
@@ -152,16 +152,40 @@
 anager;%0A
+import android.net.Uri;%0A
 import a
@@ -1639,263 +1639,29 @@
     
-Reader reader = new Reader(getCacheDir().getPath(), sPATH);%0A            reader.parse(this);%0A            Intent intent = new Intent();%0A            //intent.setData(Uri.parse(sPATH));%0A            //setResult(Activity.RESULT_OK, intent);%0A            //finish
+onImportButtonClicked
 ();%0A
@@ -2434,16 +2434,16 @@
 xists);%0A
-
         
@@ -2449,11 +2449,949 @@
  %7D%0A    %7D
+%0A%0A    private void onImportButtonClicked() %7B%0A        //Observable.just(0)%0A        //        .subscribeOn(Schedulers.io())%0A        //        .concatMap((i) -%3E %7B%0A        //            return Observable.just(i);%0A        //        %7D)%0A        //        .observeOn(MainThread.this)%0A        mButton.setEnabled(false);%0A        mText.setText(%22Importing.....%22);%0A        Runnable runnable = new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Reader reader = new Reader(getCacheDir().getPath(), sPATH);%0A                reader.parse(FileImportActivity.this);%0A                Intent intent = new Intent();%0A                //mButton.setEnabled(true);%0A                //mText.setText(%22Imported%22);%0A                intent.setData(Uri.parse(sPATH));%0A                setResult(Activity.RESULT_OK, intent);%0A                finish();%0A            %7D%0A        %7D;%0A        Thread t = new Thread(runnable);%0A        t.start();%0A    %7D
 %0A%7D%0A

disable button to give a UI feedback