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 com.android.settings.nfc;
     18 
     19 import android.app.ActionBar;
     20 import android.app.Fragment;
     21 import android.content.Context;
     22 import android.nfc.NfcAdapter;
     23 import android.os.Bundle;
     24 import android.os.UserManager;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.widget.Switch;
     29 
     30 import com.android.settings.R;
     31 import com.android.settings.SettingsActivity;
     32 import com.android.settings.widget.SwitchBar;
     33 
     34 public class AndroidBeam extends Fragment
     35         implements SwitchBar.OnSwitchChangeListener {
     36     private View mView;
     37     private NfcAdapter mNfcAdapter;
     38     private SwitchBar mSwitchBar;
     39     private CharSequence mOldActivityTitle;
     40     private boolean mBeamDisallowed;
     41 
     42     @Override
     43     public void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45 
     46         final ActionBar actionBar = getActivity().getActionBar();
     47 
     48         mOldActivityTitle = actionBar.getTitle();
     49         actionBar.setTitle(R.string.android_beam_settings_title);
     50 
     51         mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
     52         mBeamDisallowed = ((UserManager) getActivity().getSystemService(Context.USER_SERVICE))
     53                 .hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM);
     54     }
     55 
     56     @Override
     57     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     58             Bundle savedInstanceState) {
     59         mView = inflater.inflate(R.layout.android_beam, container, false);
     60 
     61         return mView;
     62     }
     63 
     64     @Override
     65     public void onActivityCreated(Bundle savedInstanceState) {
     66         super.onActivityCreated(savedInstanceState);
     67 
     68         SettingsActivity activity = (SettingsActivity) getActivity();
     69 
     70         mSwitchBar = activity.getSwitchBar();
     71         mSwitchBar.setChecked(!mBeamDisallowed && mNfcAdapter.isNdefPushEnabled());
     72         mSwitchBar.addOnSwitchChangeListener(this);
     73         mSwitchBar.setEnabled(!mBeamDisallowed);
     74         mSwitchBar.show();
     75     }
     76 
     77     @Override
     78     public void onDestroyView() {
     79         super.onDestroyView();
     80         if (mOldActivityTitle != null) {
     81             getActivity().getActionBar().setTitle(mOldActivityTitle);
     82         }
     83         mSwitchBar.removeOnSwitchChangeListener(this);
     84         mSwitchBar.hide();
     85     }
     86 
     87     @Override
     88     public void onSwitchChanged(Switch switchView, boolean desiredState) {
     89         boolean success = false;
     90         mSwitchBar.setEnabled(false);
     91         if (desiredState) {
     92             success = mNfcAdapter.enableNdefPush();
     93         } else {
     94             success = mNfcAdapter.disableNdefPush();
     95         }
     96         if (success) {
     97             mSwitchBar.setChecked(desiredState);
     98         }
     99         mSwitchBar.setEnabled(true);
    100     }
    101 }
    102