150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly/*
250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Copyright (C) 2010 The Android Open Source Project
350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly *
450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * you may not use this file except in compliance with the License.
650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * You may obtain a copy of the License at
750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly *
850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly *
1050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Unless required by applicable law or agreed to in writing, software
1150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
1250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * See the License for the specific language governing permissions and
1450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * limitations under the License.
1550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */
1650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly
1750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellypackage android.nfc;
1850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly
1950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellyimport android.content.Context;
2050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly
2150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly/**
2250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * High level manager used to obtain an instance of an {@link NfcAdapter}.
2350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * <p>
2450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Use {@link android.content.Context#getSystemService(java.lang.String)}
2550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * with {@link Context#NFC_SERVICE} to create an {@link NfcManager},
2650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * then call {@link #getDefaultAdapter} to obtain the {@link NfcAdapter}.
2750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * <p>
2850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * Alternately, you can just call the static helper
2950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * {@link NfcAdapter#getDefaultAdapter(android.content.Context)}.
3050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly *
313aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <div class="special reference">
323aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <h3>Developer Guides</h3>
333aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <p>For more information about using NFC, read the
343aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * <a href="{@docRoot}guide/topics/nfc/index.html">Near Field Communication</a> developer guide.</p>
353aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez * </div>
363aef8e1d1b2f0b87d470bcccf37ba4ebb6560c45Joe Fernandez *
3750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * @see Context#getSystemService
3850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly * @see NfcAdapter#getDefaultAdapter(android.content.Context)
3950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly */
4050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pellypublic final class NfcManager {
4150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    private final NfcAdapter mAdapter;
4250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly
4350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    /**
4450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     * @hide
4550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     */
4650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    public NfcManager(Context context) {
4750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        NfcAdapter adapter;
48bb951c893973691554f49d2e725985125f866b27Jeff Hamilton        context = context.getApplicationContext();
49b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly        if (context == null) {
50b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly            throw new IllegalArgumentException(
51b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly                    "context not associated with any application (using a mock context?)");
52b04cce0eb5917db436b7db39eb67c064df2e015aNick Pelly        }
5350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        try {
54bb951c893973691554f49d2e725985125f866b27Jeff Hamilton            adapter = NfcAdapter.getNfcAdapter(context);
5550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        } catch (UnsupportedOperationException e) {
5650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly            adapter = null;
5750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        }
5850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        mAdapter = adapter;
5950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    }
6050b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly
6150b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    /**
6250b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     * Get the default NFC Adapter for this device.
6350b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     *
6450b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     * @return the default NFC Adapter
6550b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly     */
6650b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    public NfcAdapter getDefaultAdapter() {
6750b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly        return mAdapter;
6850b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly    }
6950b4d8f643f31b37e9872f562fb869059cf79c8aNick Pelly}
70