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