1/*
2 * Copyright (C) 2015 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
17package com.android.commands.svc;
18
19import android.content.Context;
20import android.content.pm.IPackageManager;
21import android.content.pm.PackageManager;
22import android.nfc.INfcAdapter;
23import android.os.RemoteException;
24import android.os.ServiceManager;
25
26public class NfcCommand extends Svc.Command {
27
28    public NfcCommand() {
29        super("nfc");
30    }
31
32    @Override
33    public String shortHelp() {
34        return "Control NFC functions";
35    }
36
37    @Override
38    public String longHelp() {
39        return shortHelp() + "\n"
40                + "\n"
41                + "usage: svc nfc [enable|disable]\n"
42                + "         Turn NFC on or off.\n\n";
43    }
44
45    @Override
46    public void run(String[] args) {
47        boolean validCommand = false;
48        if (args.length >= 2) {
49            boolean flag = false;
50            if ("enable".equals(args[1])) {
51                flag = true;
52                validCommand = true;
53            } else if ("disable".equals(args[1])) {
54                flag = false;
55                validCommand = true;
56            }
57            if (validCommand) {
58                IPackageManager pm = IPackageManager.Stub.asInterface(
59                        ServiceManager.getService("package"));
60                try {
61                    if (pm.hasSystemFeature(PackageManager.FEATURE_NFC, 0) ||
62			pm.hasSystemFeature(PackageManager.FEATURE_NFC_HOST_CARD_EMULATION, 0)) {
63                        INfcAdapter nfc = INfcAdapter.Stub
64                                .asInterface(ServiceManager.getService(Context.NFC_SERVICE));
65                        try {
66                            if (flag) {
67                                nfc.enable();
68                            } else
69                                nfc.disable(true);
70                        } catch (RemoteException e) {
71                            System.err.println("NFC operation failed: " + e);
72                        }
73                    } else {
74                        System.err.println("NFC feature not supported.");
75                    }
76                } catch (RemoteException e) {
77                    System.err.println("RemoteException while calling PackageManager, is the "
78                            + "system running?");
79                }
80                return;
81            }
82        }
83        System.err.println(longHelp());
84    }
85
86}
87