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
17package com.android.settings.nfc;
18
19import android.app.ActionBar;
20import android.app.Activity;
21import android.app.Fragment;
22import android.nfc.NfcAdapter;
23import android.os.Bundle;
24import android.os.Handler;
25import android.preference.PreferenceActivity;
26import android.view.Gravity;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.CompoundButton;
31import android.widget.ImageView;
32import android.widget.Switch;
33import com.android.settings.R;
34
35public 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
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
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
80    private void initView(View view) {
81        mActionBarSwitch.setOnCheckedChangeListener(this);
82        mActionBarSwitch.setChecked(mNfcAdapter.isNdefPushEnabled());
83    }
84
85    @Override
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