Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2011 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 android.nfc;
     18 
     19 import android.app.Activity;
     20 import android.app.Fragment;
     21 import android.app.FragmentManager;
     22 
     23 /**
     24  * Used by {@link NfcActivityManager} to attach to activity life-cycle.
     25  * @hide
     26  */
     27 public final class NfcFragment extends Fragment {
     28     static final String FRAGMENT_TAG = "android.nfc.NfcFragment";
     29 
     30     // only used on UI thread
     31     static boolean sIsInitialized = false;
     32     static NfcActivityManager sNfcActivityManager;
     33 
     34     /**
     35      * Attach NfcFragment to an activity (if not already attached).
     36      */
     37     public static void attach(Activity activity) {
     38         FragmentManager manager = activity.getFragmentManager();
     39         if (manager.findFragmentByTag(FRAGMENT_TAG) == null) {
     40             manager.beginTransaction().add(new NfcFragment(), FRAGMENT_TAG).commit();
     41         }
     42     }
     43 
     44     /**
     45      * Remove NfcFragment from activity.
     46      */
     47     public static void remove(Activity activity) {
     48         FragmentManager manager = activity.getFragmentManager();
     49         Fragment fragment = manager.findFragmentByTag(FRAGMENT_TAG);
     50         if (fragment != null) {
     51             // We allow state loss at this point, because the state is only
     52             // lost when activity is being paused *AND* subsequently destroyed.
     53             // In that case, the app will setup foreground dispatch again anyway.
     54             manager.beginTransaction().remove(fragment).commitAllowingStateLoss();
     55         }
     56     }
     57 
     58     @Override
     59     public void onAttach(Activity activity) {
     60         super.onAttach(activity);
     61         if (!sIsInitialized) {
     62             sIsInitialized = true;
     63             NfcAdapter adapter = NfcAdapter.getDefaultAdapter(
     64                     activity.getApplicationContext());
     65             if (adapter != null) {
     66                 sNfcActivityManager = adapter.mNfcActivityManager;
     67             }
     68         }
     69     }
     70 
     71     @Override
     72     public void onResume() {
     73         super.onResume();
     74         if (sNfcActivityManager != null) {
     75             sNfcActivityManager.onResume(getActivity());
     76         }
     77     }
     78 
     79     @Override
     80     public void onPause() {
     81         super.onPause();
     82         if (sNfcActivityManager != null) {
     83             sNfcActivityManager.onPause(getActivity());
     84         }
     85     }
     86 
     87     @Override
     88     public void onDestroy() {
     89         super.onDestroy();
     90         if (sNfcActivityManager != null) {
     91             sNfcActivityManager.onDestroy(getActivity());
     92         }
     93     }
     94 
     95 
     96 }
     97