Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.nfc;
     18 
     19 /**
     20  * Parameters for enabling NFC tag discovery and polling,
     21  * and host card emulation.
     22  */
     23 public final class NfcDiscoveryParameters {
     24 
     25     public static class Builder {
     26 
     27         private NfcDiscoveryParameters mParameters;
     28 
     29         private Builder() {
     30             mParameters = new NfcDiscoveryParameters();
     31         }
     32 
     33         public NfcDiscoveryParameters.Builder setTechMask(int techMask) {
     34             mParameters.mTechMask = techMask;
     35             return this;
     36         }
     37 
     38         public NfcDiscoveryParameters.Builder setEnableLowPowerDiscovery(boolean enable) {
     39             mParameters.mEnableLowPowerDiscovery = enable;
     40             return this;
     41         }
     42 
     43         public NfcDiscoveryParameters.Builder setEnableReaderMode(boolean enable) {
     44             mParameters.mEnableReaderMode = enable;
     45 
     46             if (enable) {
     47                 mParameters.mEnableLowPowerDiscovery = false;
     48             }
     49 
     50             return this;
     51         }
     52 
     53         public NfcDiscoveryParameters.Builder setEnableHostRouting(boolean enable) {
     54             mParameters.mEnableHostRouting = enable;
     55             return this;
     56         }
     57 
     58         public NfcDiscoveryParameters build() {
     59             if (mParameters.mEnableReaderMode && mParameters.mEnableLowPowerDiscovery) {
     60                 throw new IllegalStateException("Can't enable LPTD and reader mode simultaneously");
     61             }
     62             return mParameters;
     63         }
     64     }
     65 
     66     // Polling technology masks
     67     static final int NFC_POLL_DEFAULT = -1;
     68     static final int NFC_POLL_A = 0x01;
     69     static final int NFC_POLL_B = 0x02;
     70     static final int NFC_POLL_F = 0x04;
     71     static final int NFC_POLL_ISO15693 = 0x08;
     72     static final int NFC_POLL_B_PRIME = 0x10;
     73     static final int NFC_POLL_KOVIO = 0x20;
     74 
     75     private int mTechMask = 0;
     76     private boolean mEnableLowPowerDiscovery = true;
     77     private boolean mEnableReaderMode = false;
     78     private boolean mEnableHostRouting = false;
     79 
     80     public NfcDiscoveryParameters() {}
     81 
     82     public int getTechMask() {
     83         return mTechMask;
     84     }
     85 
     86     public boolean shouldEnableLowPowerDiscovery() {
     87         return mEnableLowPowerDiscovery;
     88     }
     89 
     90     public boolean shouldEnableReaderMode() {
     91         return mEnableReaderMode;
     92     }
     93 
     94     public boolean shouldEnableHostRouting() {
     95         return mEnableHostRouting;
     96     }
     97 
     98     public boolean shouldEnableDiscovery() {
     99         return mTechMask != 0 || mEnableHostRouting;
    100     }
    101 
    102     @Override
    103     public boolean equals(Object obj) {
    104         if (obj == this) {
    105             return true;
    106         }
    107 
    108         if ((obj == null) || (obj.getClass() != this.getClass())) {
    109             return false;
    110         }
    111         NfcDiscoveryParameters params = (NfcDiscoveryParameters) obj;
    112         return mTechMask == params.mTechMask &&
    113                 (mEnableLowPowerDiscovery == params.mEnableLowPowerDiscovery) &&
    114                 (mEnableReaderMode == params.mEnableReaderMode) &&
    115                 (mEnableHostRouting == params.mEnableHostRouting);
    116     }
    117 
    118     @Override
    119     public String toString() {
    120         StringBuilder sb = new StringBuilder();
    121         if (mTechMask == NFC_POLL_DEFAULT) {
    122             sb.append("mTechMask: default\n");
    123         } else {
    124             sb.append("mTechMask: " + Integer.toString(mTechMask) + "\n");
    125         }
    126         sb.append("mEnableLPD: " + Boolean.toString(mEnableLowPowerDiscovery) + "\n");
    127         sb.append("mEnableReader: " + Boolean.toString(mEnableReaderMode) + "\n");
    128         sb.append("mEnableHostRouting: " + Boolean.toString(mEnableHostRouting));
    129         return sb.toString();
    130     }
    131 
    132     public static NfcDiscoveryParameters.Builder newBuilder() {
    133         return new Builder();
    134     }
    135 
    136     public static NfcDiscoveryParameters getDefaultInstance() {
    137         return new NfcDiscoveryParameters();
    138     }
    139 
    140     public static NfcDiscoveryParameters getNfcOffParameters() {
    141         return new NfcDiscoveryParameters();
    142     }
    143 }
    144