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 ImageView mImageView; 39 private NfcAdapter mNfcAdapter; 40 private Switch mActionBarSwitch; 41 42 43 @Override onCreate(Bundle savedInstanceState)44 public void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 Activity activity = getActivity(); 47 48 mActionBarSwitch = new Switch(activity); 49 50 if (activity instanceof PreferenceActivity) { 51 PreferenceActivity preferenceActivity = (PreferenceActivity) activity; 52 if (preferenceActivity.onIsHidingHeaders() || !preferenceActivity.onIsMultiPane()) { 53 final int padding = activity.getResources().getDimensionPixelSize( 54 R.dimen.action_bar_switch_padding); 55 mActionBarSwitch.setPadding(0, 0, padding, 0); 56 activity.getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, 57 ActionBar.DISPLAY_SHOW_CUSTOM); 58 activity.getActionBar().setCustomView(mActionBarSwitch, new ActionBar.LayoutParams( 59 ActionBar.LayoutParams.WRAP_CONTENT, 60 ActionBar.LayoutParams.WRAP_CONTENT, 61 Gravity.CENTER_VERTICAL | Gravity.RIGHT)); 62 activity.getActionBar().setTitle(R.string.android_beam_settings_title); 63 } 64 } 65 66 mActionBarSwitch.setOnCheckedChangeListener(this); 67 68 mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); 69 mActionBarSwitch.setChecked(mNfcAdapter.isNdefPushEnabled()); 70 } 71 72 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)73 public View onCreateView(LayoutInflater inflater, ViewGroup container, 74 Bundle savedInstanceState) { 75 mView = inflater.inflate(R.layout.android_beam, container, false); 76 initView(mView); 77 return mView; 78 } 79 initView(View view)80 private void initView(View view) { 81 mActionBarSwitch.setOnCheckedChangeListener(this); 82 mActionBarSwitch.setChecked(mNfcAdapter.isNdefPushEnabled()); 83 } 84 85 @Override onCheckedChanged(CompoundButton buttonView, boolean desiredState)86 public void onCheckedChanged(CompoundButton buttonView, boolean desiredState) { 87 boolean success = false; 88 mActionBarSwitch.setEnabled(false); 89 if (desiredState) { 90 success = mNfcAdapter.enableNdefPush(); 91 } else { 92 success = mNfcAdapter.disableNdefPush(); 93 } 94 if (success) { 95 mActionBarSwitch.setChecked(desiredState); 96 } 97 mActionBarSwitch.setEnabled(true); 98 } 99 } 100