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