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.Activity;
     21 import android.app.Fragment;
     22 import android.nfc.NfcAdapter;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.preference.PreferenceActivity;
     26 import android.view.Gravity;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.widget.CompoundButton;
     31 import android.widget.ImageView;
     32 import android.widget.Switch;
     33 import com.android.settings.R;
     34 
     35 public class AndroidBeam extends Fragment
     36         implements CompoundButton.OnCheckedChangeListener {
     37     private View mView;
     38     private NfcAdapter mNfcAdapter;
     39     private Switch mActionBarSwitch;
     40     private CharSequence mOldActivityTitle;
     41 
     42     @Override
     43     public void onCreate(Bundle savedInstanceState) {
     44         super.onCreate(savedInstanceState);
     45         Activity activity = getActivity();
     46 
     47         mActionBarSwitch = new Switch(activity);
     48 
     49         if (activity instanceof PreferenceActivity) {
     50             final int padding = activity.getResources().getDimensionPixelSize(
     51                     R.dimen.action_bar_switch_padding);
     52             mActionBarSwitch.setPaddingRelative(0, 0, padding, 0);
     53             activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
     54                     ActionBar.DISPLAY_SHOW_CUSTOM);
     55             activity.getActionBar().setCustomView(mActionBarSwitch, new ActionBar.LayoutParams(
     56                     ActionBar.LayoutParams.WRAP_CONTENT,
     57                     ActionBar.LayoutParams.WRAP_CONTENT,
     58                     Gravity.CENTER_VERTICAL | Gravity.END));
     59             mOldActivityTitle = activity.getActionBar().getTitle();
     60             activity.getActionBar().setTitle(R.string.android_beam_settings_title);
     61         }
     62 
     63         mActionBarSwitch.setOnCheckedChangeListener(this);
     64 
     65         mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
     66         mActionBarSwitch.setChecked(mNfcAdapter.isNdefPushEnabled());
     67     }
     68 
     69     @Override
     70     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     71             Bundle savedInstanceState) {
     72         mView = inflater.inflate(R.layout.android_beam, container, false);
     73         initView(mView);
     74         return mView;
     75     }
     76 
     77     @Override
     78     public void onDestroyView() {
     79         super.onDestroyView();
     80         getActivity().getActionBar().setCustomView(null);
     81         if (mOldActivityTitle != null) {
     82             getActivity().getActionBar().setTitle(mOldActivityTitle);
     83         }
     84     }
     85 
     86     private void initView(View view) {
     87         mActionBarSwitch.setOnCheckedChangeListener(this);
     88         mActionBarSwitch.setChecked(mNfcAdapter.isNdefPushEnabled());
     89     }
     90 
     91     @Override
     92     public void onCheckedChanged(CompoundButton buttonView, boolean desiredState) {
     93         boolean success = false;
     94         mActionBarSwitch.setEnabled(false);
     95         if (desiredState) {
     96             success = mNfcAdapter.enableNdefPush();
     97         } else {
     98             success = mNfcAdapter.disableNdefPush();
     99         }
    100         if (success) {
    101             mActionBarSwitch.setChecked(desiredState);
    102         }
    103         mActionBarSwitch.setEnabled(true);
    104     }
    105 }
    106