// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/BaseFragment.java
// public class BaseFragment extends Fragment {
// 
//     protected @Inject Bus bus;
//     protected @Inject PersistentState persistentState;
// 
//     public BaseFragment() {
//         super();
//         BaseApplication.inject(this);
//     }
// 
//     @Override
//     public void onCreate(Bundle savedInstanceState) {
//         super.onCreate(savedInstanceState);
//         bus.register(this);
//     }
// 
//     @Override
//     public void onViewCreated(View view, Bundle savedInstanceState) {
//         super.onViewCreated(view, savedInstanceState);
//         ButterKnife.inject(this, view);
// 
//     }
// 
//     @Override
//     public void onDestroy() {
//         super.onDestroy();
//         ButterKnife.reset(this);
//         bus.unregister(this);
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/eventbus/RangeScanCompleteEvent.java
// public class RangeScanCompleteEvent implements OttoEvent {
// 
//     private final List<Beacon> beacons;
//     private final Calendar timestamp;
// 
//     public RangeScanCompleteEvent(Calendar timestamp, Collection<Beacon> beacons) {
//         this.timestamp = timestamp;
//         this.beacons = new ArrayList<>(beacons);
//     }
// 
//     public List<Beacon> getBeacons() {
//         return beacons;
//     }
// 
//     public Calendar getTimestamp() {
//         return timestamp;
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/eventbus/RequestBeaconScanEvent.java
// public class RequestBeaconScanEvent implements OttoEvent {
// 
//     private Region region;
// 
//     public RequestBeaconScanEvent(Region region) {
//         this.region = region;
//     }
// 
//     public Region getRegion() {
//         return region;
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/util/Utils.java
// public class Utils {
// 
//     private static DecimalFormat rangeFormatter = new DecimalFormat("#,##0.0");
// 
//     public static String formatRange(double rangeInMeters) {
//         return rangeFormatter.format(rangeInMeters);
//     }
// 
//     public static boolean isSameBeacon(Beacon entry, Region selectedBeacon) {
//         if (entry == null || selectedBeacon == null) return false;
//         return (entry.getId1().equals(selectedBeacon.getId1())
//                 && entry.getId2().equals(selectedBeacon.getId2())
//                 && entry.getId3().equals(selectedBeacon.getId3()));
//     }
// 
//     public static Proximity proximityFromDistance(double accuracy) {
//         if (accuracy < 0.0D) {
//             return Proximity.UNKNOWN;
//         }
//         if (accuracy < 0.5D) {
//             return Proximity.IMMEDIATE;
//         }
//         if (accuracy <= 3.0D) {
//             return Proximity.NEAR;
//         }
//         return Proximity.FAR;
//     }
// 
//     private static double computeAccuracy(Beacon beacon) {
// //            if (beacon.getRssi() == 0) {
// //                return -1.0D;
// //            }
// //
// //            double ratio = beacon.getRssi() / beacon.getTxPower();
// //            double rssiCorrection = 0.96D + Math.pow(Math.abs(beacon.getRssi()), 3.0D) % 10.0D / 150.0D;
// //
// //            if (ratio <= 1.0D) {
// //                return Math.pow(ratio, 9.98D) * rssiCorrection;
// //            }
// //            return (0.103D + 0.89978D * Math.pow(ratio, 7.71D)) * rssiCorrection;
// 
//         return 0;
//     }
// 
// 
//     public static enum Proximity {
//         UNKNOWN,
//         IMMEDIATE,
//         NEAR,
//         FAR;
//     }
// }


import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.InjectView;
import com.squareup.otto.Subscribe;
import com.trifork.ibeacon.BaseFragment;
import com.trifork.ibeacon.R;
import com.trifork.ibeacon.eventbus.RangeScanCompleteEvent;
import com.trifork.ibeacon.eventbus.RequestBeaconScanEvent;
import com.trifork.ibeacon.util.Utils;
import org.altbeacon.beacon.Beacon;
    public void onBeaconDataReceived(RangeScanCompleteEvent event) {
        Beacon beacon = event.getBeacons().get(0);
        double distanceM = beacon.getDistance();
        Utils.Proximity proximity = Utils.proximityFromDistance(beacon.getDistance());

        rangeView.setText(com.trifork.ibeacon.util.Utils.formatRange(distanceM) + "m");
        proximityView.setText(proximity.toString());

        // Calculate new pause based on current RSSI
        // Convert to positive numbers to not make my head hurt.
        float txPower = -beacon.getTxPower();
        float rssi = -beacon.getRssi();
        if (rssi <= txPower) {
            pause = MIN_PAUSE_MS;
        } else if (rssi >= 100) {
            pause = MAX_PAUSE_MS;
        } else {
            float factor = ((rssi - txPower)/(100 - txPower));
            pause = (int) (MIN_PAUSE_MS + (MAX_PAUSE_MS - MIN_PAUSE_MS) * factor);
        }
    }

    private void startScan() {
        bus.post(new RequestBeaconScanEvent(persistentState.getSelectedRegion()));
        if (persistentState.getSelectedRegion() != null) {
            handler.post(toneRunner);
        }
    }

    private void stopSound() {
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/BaseFragment.java
// public class BaseFragment extends Fragment {
// 
//     protected @Inject Bus bus;
//     protected @Inject PersistentState persistentState;
// 
//     public BaseFragment() {
//         super();
//         BaseApplication.inject(this);
//     }
// 
//     @Override
//     public void onCreate(Bundle savedInstanceState) {
//         super.onCreate(savedInstanceState);
//         bus.register(this);
//     }
// 
//     @Override
//     public void onViewCreated(View view, Bundle savedInstanceState) {
//         super.onViewCreated(view, savedInstanceState);
//         ButterKnife.inject(this, view);
// 
//     }
// 
//     @Override
//     public void onDestroy() {
//         super.onDestroy();
//         ButterKnife.reset(this);
//         bus.unregister(this);
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/eventbus/RangeScanCompleteEvent.java
// public class RangeScanCompleteEvent implements OttoEvent {
// 
//     private final List<Beacon> beacons;
//     private final Calendar timestamp;
// 
//     public RangeScanCompleteEvent(Calendar timestamp, Collection<Beacon> beacons) {
//         this.timestamp = timestamp;
//         this.beacons = new ArrayList<>(beacons);
//     }
// 
//     public List<Beacon> getBeacons() {
//         return beacons;
//     }
// 
//     public Calendar getTimestamp() {
//         return timestamp;
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/eventbus/RequestBeaconScanEvent.java
// public class RequestBeaconScanEvent implements OttoEvent {
// 
//     private Region region;
// 
//     public RequestBeaconScanEvent(Region region) {
//         this.region = region;
//     }
// 
//     public Region getRegion() {
//         return region;
//     }
// }
// 
// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/util/Utils.java
// public class Utils {
// 
//     private static DecimalFormat rangeFormatter = new DecimalFormat("#,##0.0");
// 
//     public static String formatRange(double rangeInMeters) {
//         return rangeFormatter.format(rangeInMeters);
//     }
// 
//     public static boolean isSameBeacon(Beacon entry, Region selectedBeacon) {
//         if (entry == null || selectedBeacon == null) return false;
//         return (entry.getId1().equals(selectedBeacon.getId1())
//                 && entry.getId2().equals(selectedBeacon.getId2())
//                 && entry.getId3().equals(selectedBeacon.getId3()));
//     }
// 
//     public static Proximity proximityFromDistance(double accuracy) {
//         if (accuracy < 0.0D) {
//             return Proximity.UNKNOWN;
//         }
//         if (accuracy < 0.5D) {
//             return Proximity.IMMEDIATE;
//         }
//         if (accuracy <= 3.0D) {
//             return Proximity.NEAR;
//         }
//         return Proximity.FAR;
//     }
// 
//     private static double computeAccuracy(Beacon beacon) {
// //            if (beacon.getRssi() == 0) {
// //                return -1.0D;
// //            }
// //
// //            double ratio = beacon.getRssi() / beacon.getTxPower();
// //            double rssiCorrection = 0.96D + Math.pow(Math.abs(beacon.getRssi()), 3.0D) % 10.0D / 150.0D;
// //
// //            if (ratio <= 1.0D) {
// //                return Math.pow(ratio, 9.98D) * rssiCorrection;
// //            }
// //            return (0.103D + 0.89978D * Math.pow(ratio, 7.71D)) * rssiCorrection;
// 
//         return 0;
//     }
// 
// 
//     public static enum Proximity {
//         UNKNOWN,
//         IMMEDIATE,
//         NEAR,
//         FAR;
//     }
// }


// Path: TriforkIBeaconDemo/src/main/java/com/trifork/ibeacon/ui/RangingFragment.java
import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.InjectView;
import com.squareup.otto.Subscribe;
import com.trifork.ibeacon.BaseFragment;
import com.trifork.ibeacon.R;
import com.trifork.ibeacon.eventbus.RangeScanCompleteEvent;
import com.trifork.ibeacon.eventbus.RequestBeaconScanEvent;
import com.trifork.ibeacon.util.Utils;
import org.altbeacon.beacon.Beacon;

    public void onBeaconDataReceived(RangeScanCompleteEvent event) {
        Beacon beacon = event.getBeacons().get(0);
        double distanceM = beacon.getDistance();
        Utils.Proximity proximity = Utils.proximityFromDistance(beacon.getDistance());

        rangeView.setText(com.trifork.ibeacon.util.Utils.formatRange(distanceM) + "m");
        proximityView.setText(proximity.toString());

        // Calculate new pause based on current RSSI
        // Convert to positive numbers to not make my head hurt.
        float txPower = -beacon.getTxPower();
        float rssi = -beacon.getRssi();
        if (rssi <= txPower) {
            pause = MIN_PAUSE_MS;
        } else if (rssi >= 100) {
            pause = MAX_PAUSE_MS;
        } else {
            float factor = ((rssi - txPower)/(100 - txPower));
            pause = (int) (MIN_PAUSE_MS + (MAX_PAUSE_MS - MIN_PAUSE_MS) * factor);
        }
    }

    private void startScan() {
        bus.post(new RequestBeaconScanEvent(persistentState.getSelectedRegion()));
        if (persistentState.getSelectedRegion() != null) {
            handler.post(toneRunner);
        }
    }

    private void stopSound() {
        handler.removeCallbacks(toneRunner);