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.Builder setEnableP2p(boolean enable) {
     59             mParameters.mEnableP2p = enable;
     60             return this;
     61         }
     62 
     63         public NfcDiscoveryParameters build() {
     64             if (mParameters.mEnableReaderMode &&
     65                     (mParameters.mEnableLowPowerDiscovery || mParameters.mEnableP2p)) {
     66                 throw new IllegalStateException("Can't enable LPTD/P2P and reader mode " +
     67                         "simultaneously");
     68             }
     69             return mParameters;
     70         }
     71     }
     72 
     73     static final int NFC_POLL_DEFAULT = -1;
     74 
     75     // NOTE: when adding a new field, don't forget to update equals() and toString() below
     76     private int mTechMask = 0;
     77     private boolean mEnableLowPowerDiscovery = true;
     78     private boolean mEnableReaderMode = false;
     79     private boolean mEnableHostRouting = false;
     80     private boolean mEnableP2p = false;
     81 
     82     public NfcDiscoveryParameters() {}
     83 
     84     public int getTechMask() {
     85         return mTechMask;
     86     }
     87 
     88     public boolean shouldEnableLowPowerDiscovery() {
     89         return mEnableLowPowerDiscovery;
     90     }
     91 
     92     public boolean shouldEnableReaderMode() {
     93         return mEnableReaderMode;
     94     }
     95 
     96     public boolean shouldEnableHostRouting() {
     97         return mEnableHostRouting;
     98     }
     99 
    100     public boolean shouldEnableDiscovery() {
    101         return mTechMask != 0 || mEnableHostRouting;
    102     }
    103 
    104     public boolean shouldEnableP2p() {
    105         return mEnableP2p;
    106     }
    107 
    108     @Override
    109     public boolean equals(Object obj) {
    110         if (obj == this) {
    111             return true;
    112         }
    113 
    114         if ((obj == null) || (obj.getClass() != this.getClass())) {
    115             return false;
    116         }
    117         NfcDiscoveryParameters params = (NfcDiscoveryParameters) obj;
    118         return mTechMask == params.mTechMask &&
    119                 (mEnableLowPowerDiscovery == params.mEnableLowPowerDiscovery) &&
    120                 (mEnableReaderMode == params.mEnableReaderMode) &&
    121                 (mEnableHostRouting == params.mEnableHostRouting)
    122                 && (mEnableP2p == params.mEnableP2p);
    123     }
    124 
    125     @Override
    126     public String toString() {
    127         StringBuilder sb = new StringBuilder();
    128         if (mTechMask == NFC_POLL_DEFAULT) {
    129             sb.append("mTechMask: default\n");
    130         } else {
    131             sb.append("mTechMask: " + Integer.toString(mTechMask) + "\n");
    132         }
    133         sb.append("mEnableLPD: " + Boolean.toString(mEnableLowPowerDiscovery) + "\n");
    134         sb.append("mEnableReader: " + Boolean.toString(mEnableReaderMode) + "\n");
    135         sb.append("mEnableHostRouting: " + Boolean.toString(mEnableHostRouting) + "\n");
    136         sb.append("mEnableP2p: " + Boolean.toString(mEnableP2p));
    137         return sb.toString();
    138     }
    139 
    140     public static NfcDiscoveryParameters.Builder newBuilder() {
    141         return new Builder();
    142     }
    143 
    144     public static NfcDiscoveryParameters getDefaultInstance() {
    145         return new NfcDiscoveryParameters();
    146     }
    147 
    148     public static NfcDiscoveryParameters getNfcOffParameters() {
    149         return new NfcDiscoveryParameters();
    150     }
    151 }
    152