package com.example.qrcodesample;

import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    // インテントリクエストコード
    private static final int REQUEST_SCAN = 0;
 
    private static String resultStr;
    private TextView textResult;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // 読み取り結果表示領域
        textResult = (TextView)findViewById(R.id.result);
    }
 
    public void clickEventScan(View v) {
        // バーコードスキャナの呼び出し
        callBarcodeScanner();
    }
 
    private void callBarcodeScanner() {
        Intent intentScan = new Intent("jp.gr.java_conf.qr_code.SCAN");
 
        try {
            startActivityForResult(intentScan, REQUEST_SCAN);
        }
        catch(ActivityNotFoundException e) {
        }
    }
 
    @Override
    protected void onActivityResult(int request, int result, Intent intent) {
        switch(request) {
            case REQUEST_SCAN:
                if(result == Activity.RESULT_OK) {
                    resultStr = intent.getStringExtra("SCAN_RESULT");
                    textResult.setText(resultStr);
                }
                break;
            default:
                break;
        }
    }
}
