OSDN Git Service

resolve merge conflicts of 56a2b529373b to nyc-dev am: d7dcc3e227
[android-x86/frameworks-base.git] / cmds / svc / src / com / android / commands / svc / NfcCommand.java
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
17 package com.android.commands.svc;
18
19 import android.content.Context;
20 import android.content.pm.IPackageManager;
21 import android.content.pm.PackageManager;
22 import android.nfc.INfcAdapter;
23 import android.os.RemoteException;
24 import android.os.ServiceManager;
25
26 public 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                         INfcAdapter nfc = INfcAdapter.Stub
63                                 .asInterface(ServiceManager.getService(Context.NFC_SERVICE));
64                         try {
65                             if (flag) {
66                                 nfc.enable();
67                             } else
68                                 nfc.disable(true);
69                         } catch (RemoteException e) {
70                             System.err.println("NFC operation failed: " + e);
71                         }
72                     } else {
73                         System.err.println("NFC feature not supported.");
74                     }
75                 } catch (RemoteException e) {
76                     System.err.println("RemoteException while calling PackageManager, is the "
77                             + "system running?");
78                 }
79                 return;
80             }
81         }
82         System.err.println(longHelp());
83     }
84
85 }