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 import android.util.proto.ProtoOutputStream;
20 
21 /**
22  * Parameters for enabling NFC tag discovery and polling,
23  * and host card emulation.
24  */
25 public final class NfcDiscoveryParameters {
26 
27     public static class Builder {
28 
29         private NfcDiscoveryParameters mParameters;
30 
Builder()31         private Builder() {
32             mParameters = new NfcDiscoveryParameters();
33         }
34 
setTechMask(int techMask)35         public NfcDiscoveryParameters.Builder setTechMask(int techMask) {
36             mParameters.mTechMask = techMask;
37             return this;
38         }
39 
setEnableLowPowerDiscovery(boolean enable)40         public NfcDiscoveryParameters.Builder setEnableLowPowerDiscovery(boolean enable) {
41             mParameters.mEnableLowPowerDiscovery = enable;
42             return this;
43         }
44 
setEnableReaderMode(boolean enable)45         public NfcDiscoveryParameters.Builder setEnableReaderMode(boolean enable) {
46             mParameters.mEnableReaderMode = enable;
47 
48             if (enable) {
49                 mParameters.mEnableLowPowerDiscovery = false;
50             }
51 
52             return this;
53         }
54 
setEnableHostRouting(boolean enable)55         public NfcDiscoveryParameters.Builder setEnableHostRouting(boolean enable) {
56             mParameters.mEnableHostRouting = enable;
57             return this;
58         }
59 
setEnableP2p(boolean enable)60         public NfcDiscoveryParameters.Builder setEnableP2p(boolean enable) {
61             mParameters.mEnableP2p = enable;
62             return this;
63         }
64 
build()65         public NfcDiscoveryParameters build() {
66             if (mParameters.mEnableReaderMode &&
67                     (mParameters.mEnableLowPowerDiscovery || mParameters.mEnableP2p)) {
68                 throw new IllegalStateException("Can't enable LPTD/P2P and reader mode " +
69                         "simultaneously");
70             }
71             return mParameters;
72         }
73     }
74 
75     static final int NFC_POLL_DEFAULT = -1;
76 
77     // NOTE: when adding a new field, don't forget to update equals() and toString() below
78     private int mTechMask = 0;
79     private boolean mEnableLowPowerDiscovery = true;
80     private boolean mEnableReaderMode = false;
81     private boolean mEnableHostRouting = false;
82     private boolean mEnableP2p = false;
83 
NfcDiscoveryParameters()84     public NfcDiscoveryParameters() {}
85 
getTechMask()86     public int getTechMask() {
87         return mTechMask;
88     }
89 
shouldEnableLowPowerDiscovery()90     public boolean shouldEnableLowPowerDiscovery() {
91         return mEnableLowPowerDiscovery;
92     }
93 
shouldEnableReaderMode()94     public boolean shouldEnableReaderMode() {
95         return mEnableReaderMode;
96     }
97 
shouldEnableHostRouting()98     public boolean shouldEnableHostRouting() {
99         return mEnableHostRouting;
100     }
101 
shouldEnableDiscovery()102     public boolean shouldEnableDiscovery() {
103         return mTechMask != 0 || mEnableHostRouting;
104     }
105 
shouldEnableP2p()106     public boolean shouldEnableP2p() {
107         return mEnableP2p;
108     }
109 
110     @Override
equals(Object obj)111     public boolean equals(Object obj) {
112         if (obj == this) {
113             return true;
114         }
115 
116         if ((obj == null) || (obj.getClass() != this.getClass())) {
117             return false;
118         }
119         NfcDiscoveryParameters params = (NfcDiscoveryParameters) obj;
120         return mTechMask == params.mTechMask &&
121                 (mEnableLowPowerDiscovery == params.mEnableLowPowerDiscovery) &&
122                 (mEnableReaderMode == params.mEnableReaderMode) &&
123                 (mEnableHostRouting == params.mEnableHostRouting)
124                 && (mEnableP2p == params.mEnableP2p);
125     }
126 
127     @Override
toString()128     public String toString() {
129         StringBuilder sb = new StringBuilder();
130         if (mTechMask == NFC_POLL_DEFAULT) {
131             sb.append("mTechMask: default\n");
132         } else {
133             sb.append("mTechMask: " + Integer.toString(mTechMask) + "\n");
134         }
135         sb.append("mEnableLPD: " + Boolean.toString(mEnableLowPowerDiscovery) + "\n");
136         sb.append("mEnableReader: " + Boolean.toString(mEnableReaderMode) + "\n");
137         sb.append("mEnableHostRouting: " + Boolean.toString(mEnableHostRouting) + "\n");
138         sb.append("mEnableP2p: " + Boolean.toString(mEnableP2p));
139         return sb.toString();
140     }
141 
142     /** Dumps DiscoveryParamsProto for debugging. */
dumpDebug(ProtoOutputStream proto)143     void dumpDebug(ProtoOutputStream proto) {
144         proto.write(DiscoveryParamsProto.TECH_MASK, mTechMask);
145         proto.write(DiscoveryParamsProto.ENABLE_LPD, mEnableLowPowerDiscovery);
146         proto.write(DiscoveryParamsProto.ENABLE_READER, mEnableReaderMode);
147         proto.write(DiscoveryParamsProto.ENABLE_HOST_ROUTING, mEnableHostRouting);
148         proto.write(DiscoveryParamsProto.ENABLE_P2P, mEnableP2p);
149     }
150 
newBuilder()151     public static NfcDiscoveryParameters.Builder newBuilder() {
152         return new Builder();
153     }
154 
getDefaultInstance()155     public static NfcDiscoveryParameters getDefaultInstance() {
156         return new NfcDiscoveryParameters();
157     }
158 
getNfcOffParameters()159     public static NfcDiscoveryParameters getNfcOffParameters() {
160         return new NfcDiscoveryParameters();
161     }
162 }
163