OSDN Git Service

Profiles : Settings
[android-x86/packages-apps-Settings.git] / src / com / android / settings / profiles / NFCProfileWriter.java
1 /*
2  * Copyright (C) 2012 The CyanogenMod 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.settings.profiles;
18
19 import java.util.UUID;
20
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;
33
34 import com.android.settings.R;
35
36 /**
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.
40  */
41 public class NFCProfileWriter extends Activity {
42
43     private static final String TAG = "NFCProfileWriter";
44
45     static final String EXTRA_PROFILE_UUID = "PROFILE_UUID";
46
47     private NfcAdapter mNfcAdapter;
48
49     private IntentFilter[] mWriteTagFilters;
50
51     private Profile mProfile;
52
53     private ProfileManager mProfileManager;
54
55
56     @Override
57     public void onCreate(Bundle savedInstanceState) {
58         super.onCreate(savedInstanceState);
59         mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
60         mProfileManager = (ProfileManager) getSystemService(Context.PROFILE_SERVICE);
61
62         setContentView(R.layout.nfc_writer);
63         setTitle(R.string.profile_write_nfc_tag);
64     }
65
66     @Override
67     public void onResume() {
68         super.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());
73             enableTagWriteMode();
74         }
75     }
76
77
78     @Override
79     protected void onPause() {
80         super.onPause();
81         disableTagWriteMode();
82     }
83
84     private PendingIntent getPendingIntent() {
85         return PendingIntent.getActivity(this, 0,
86                 new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
87     }
88
89     private void disableTagWriteMode() {
90         mNfcAdapter.disableForegroundDispatch(this);
91     }
92
93     private void enableTagWriteMode() {
94         IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
95         mWriteTagFilters = new IntentFilter[] {
96             tagDetected
97         };
98         mNfcAdapter.enableForegroundDispatch(this, getPendingIntent(), mWriteTagFilters, null);
99     }
100
101     @Override
102     protected void onNewIntent(Intent intent) {
103         // Tag writing mode
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);
109             } else {
110                 Toast.makeText(this, R.string.profile_write_failed, Toast.LENGTH_LONG).show();
111             }
112             finish();
113         }
114     }
115 }