package chaoran.business.adapter;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2021-02-03          FXY        Created
 **********************************************
 */

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import chaoran.business.activity.ResultListener;
import chaoran.business.strategy.Strategy;

/**
 * 群创科技
 */
public class HoriemtechAdapter implements Adapter {
    private Context context;
    private Strategy strategy;
    private ResultListener resultListener;

    public HoriemtechAdapter(Context context, ResultListener resultListener) {
        this.context = context;
        this.resultListener = resultListener;
        strategy = new Receiver();
    }

    @Override
    public void start() {
        strategy.executeStrategy(resultListener);
    }

    @Override
    public void stop() {
        strategy.exclusiveStrategy();
    }

    public class Receiver extends BroadcastReceiver implements Strategy {

        @Override
        public void onReceive(Context context, Intent intent) {
            resultListener.result(intent.getStringExtra("value"));
        }

        @Override
        public void executeStrategy(ResultListener resultListener) {
            IntentFilter filter = new IntentFilter();
            filter.addAction("mmi.scan.mode.notify");
            context.registerReceiver(this, filter);
        }

        @Override
        public void exclusiveStrategy() {
            context.unregisterReceiver(this);
        }
    }
}
