1c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly/*
2c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * Copyright (C) 2011 The Android Open Source Project
3c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly *
4c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
5c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * you may not use this file except in compliance with the License.
6c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * You may obtain a copy of the License at
7c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly *
8c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
9c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly *
10c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * Unless required by applicable law or agreed to in writing, software
11c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
12c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * See the License for the specific language governing permissions and
14c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * limitations under the License.
15c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly */
16c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
17c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pellypackage android.nfc;
18c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
19c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pellyimport android.app.Activity;
20c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pellyimport android.app.Fragment;
21c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pellyimport android.app.FragmentManager;
22c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
23c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly/**
24c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * Used by {@link NfcActivityManager} to attach to activity life-cycle.
25c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly * @hide
26c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly */
27c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pellypublic final class NfcFragment extends Fragment {
28c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    static final String FRAGMENT_TAG = "android.nfc.NfcFragment";
29c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
30c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    // only used on UI thread
31c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    static boolean sIsInitialized = false;
32c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    static NfcActivityManager sNfcActivityManager;
33c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
34c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    /**
35c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly     * Attach NfcFragment to an activity (if not already attached).
36c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly     */
37c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    public static void attach(Activity activity) {
38c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        FragmentManager manager = activity.getFragmentManager();
39c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        if (manager.findFragmentByTag(FRAGMENT_TAG) == null) {
40c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            manager.beginTransaction().add(new NfcFragment(), FRAGMENT_TAG).commit();
41c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        }
42c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    }
43c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
44c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    /**
45c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly     * Remove NfcFragment from activity.
46c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly     */
47c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    public static void remove(Activity activity) {
48c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        FragmentManager manager = activity.getFragmentManager();
49c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        Fragment fragment = manager.findFragmentByTag(FRAGMENT_TAG);
50c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        if (fragment != null) {
51deb0f20c3d1abf68c7e0642b6c7e3fa3cc8b36beMartijn Coenen            // We allow state loss at this point, because the state is only
52deb0f20c3d1abf68c7e0642b6c7e3fa3cc8b36beMartijn Coenen            // lost when activity is being paused *AND* subsequently destroyed.
53deb0f20c3d1abf68c7e0642b6c7e3fa3cc8b36beMartijn Coenen            // In that case, the app will setup foreground dispatch again anyway.
54deb0f20c3d1abf68c7e0642b6c7e3fa3cc8b36beMartijn Coenen            manager.beginTransaction().remove(fragment).commitAllowingStateLoss();
55c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        }
56c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    }
57c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
58c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    @Override
59c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    public void onAttach(Activity activity) {
60c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        super.onAttach(activity);
61c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        if (!sIsInitialized) {
62c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            sIsInitialized = true;
63c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            NfcAdapter adapter = NfcAdapter.getDefaultAdapter(
64c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly                    activity.getApplicationContext());
65c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            if (adapter != null) {
66c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly                sNfcActivityManager = adapter.mNfcActivityManager;
67c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            }
68c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        }
69c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    }
70c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
71c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    @Override
72c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    public void onResume() {
73c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        super.onResume();
74c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        if (sNfcActivityManager != null) {
75c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            sNfcActivityManager.onResume(getActivity());
76c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        }
77c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    }
78c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly
79c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    @Override
80c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    public void onPause() {
81c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        super.onPause();
82c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        if (sNfcActivityManager != null) {
83c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly            sNfcActivityManager.onPause(getActivity());
84c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly        }
85c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly    }
863433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen
873433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen    @Override
883433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen    public void onDestroy() {
893433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen        super.onDestroy();
903433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen        if (sNfcActivityManager != null) {
913433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen            sNfcActivityManager.onDestroy(getActivity());
923433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen        }
933433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen    }
943433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen
953433a8ae5abf7c518277e2e00c141e1dec83600cMartijn Coenen
96c84c89a6cacaf16c1ba41f57cc1aecdb150e85f9Nick Pelly}
97