2 * Copyright (C) 2012 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.profiles;
19 import java.util.UUID;
21 import android.app.Activity;
22 import android.app.PendingIntent;
23 import android.app.Profile;
24 import android.app.ProfileManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.IntentFilter;
28 import android.nfc.NfcAdapter;
29 import android.nfc.Tag;
30 import android.os.Bundle;
31 import android.util.Log;
32 import android.widget.Toast;
34 import com.android.settings.R;
37 * Activity to support writing a profile to an NFC tag.
38 * The mime type is "cm/profile" and the payload is the raw bytes of the profile's
39 * UUID. The payload was intentionally kept small to support writing on 46-byte tags.
41 public class NFCProfileWriter extends Activity {
43 private static final String TAG = "NFCProfileWriter";
45 static final String EXTRA_PROFILE_UUID = "PROFILE_UUID";
47 private NfcAdapter mNfcAdapter;
49 private IntentFilter[] mWriteTagFilters;
51 private Profile mProfile;
53 private ProfileManager mProfileManager;
57 public void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59 mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
60 mProfileManager = (ProfileManager) getSystemService(Context.PROFILE_SERVICE);
62 setContentView(R.layout.nfc_writer);
63 setTitle(R.string.profile_write_nfc_tag);
67 public void onResume() {
69 String profileUuid = getIntent().getStringExtra(EXTRA_PROFILE_UUID);
70 if (profileUuid != null) {
71 mProfile = mProfileManager.getProfile(UUID.fromString(profileUuid));
72 Log.d(TAG, "Profile to write: " + mProfile.getName());
79 protected void onPause() {
81 disableTagWriteMode();
84 private PendingIntent getPendingIntent() {
85 return PendingIntent.getActivity(this, 0,
86 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
89 private void disableTagWriteMode() {
90 mNfcAdapter.disableForegroundDispatch(this);
93 private void enableTagWriteMode() {
94 IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
95 mWriteTagFilters = new IntentFilter[] {
98 mNfcAdapter.enableForegroundDispatch(this, getPendingIntent(), mWriteTagFilters, null);
102 protected void onNewIntent(Intent intent) {
104 if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
105 Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
106 if (NFCProfileUtils.writeTag(NFCProfileUtils.getProfileAsNdef(mProfile), detectedTag)) {
107 Toast.makeText(this, R.string.profile_write_success, Toast.LENGTH_LONG).show();
108 NFCProfileUtils.vibrate(this);
110 Toast.makeText(this, R.string.profile_write_failed, Toast.LENGTH_LONG).show();