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.nfc.NfcAdapter;
20 import android.os.Bundle;
21 import android.os.UserHandle;
22 import android.os.UserManager;
23 import android.view.LayoutInflater;
24 import android.view.Menu;
25 import android.view.MenuInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.Switch;
29 
30 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
31 import com.android.settingslib.HelpUtils;
32 import com.android.settings.core.InstrumentedPreferenceFragment;
33 import com.android.settings.R;
34 import com.android.settings.SettingsActivity;
35 import com.android.settings.ShowAdminSupportDetailsDialog;
36 import com.android.settings.widget.SwitchBar;
37 import com.android.settingslib.RestrictedLockUtils;
38 
39 import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
40 
41 public class AndroidBeam extends InstrumentedPreferenceFragment
42         implements SwitchBar.OnSwitchChangeListener {
43     private View mView;
44     private NfcAdapter mNfcAdapter;
45     private SwitchBar mSwitchBar;
46     private CharSequence mOldActivityTitle;
47     private boolean mBeamDisallowedByBase;
48     private boolean mBeamDisallowedByOnlyAdmin;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     public void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
54         setHasOptionsMenu(true);
55     }
56 
57     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)58     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
59         super.onCreateOptionsMenu(menu, inflater);
60         HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_beam,
61                 getClass().getName());
62     }
63 
64     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)65     public View onCreateView(LayoutInflater inflater, ViewGroup container,
66             Bundle savedInstanceState) {
67         final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(
68                 getActivity(), UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId());
69         final UserManager um = UserManager.get(getActivity());
70         mBeamDisallowedByBase = RestrictedLockUtils.hasBaseUserRestriction(getActivity(),
71                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId());
72         if (!mBeamDisallowedByBase && admin != null) {
73             View view = inflater.inflate(R.layout.admin_support_details_empty_view, null);
74             ShowAdminSupportDetailsDialog.setAdminSupportDetails(getActivity(), view, admin, false);
75             view.setVisibility(View.VISIBLE);
76             mBeamDisallowedByOnlyAdmin = true;
77             return view;
78         }
79         mView = inflater.inflate(R.layout.android_beam, container, false);
80         return mView;
81     }
82 
83     @Override
onActivityCreated(Bundle savedInstanceState)84     public void onActivityCreated(Bundle savedInstanceState) {
85         super.onActivityCreated(savedInstanceState);
86 
87         SettingsActivity activity = (SettingsActivity) getActivity();
88 
89         mOldActivityTitle = activity.getActionBar().getTitle();
90 
91         mSwitchBar = activity.getSwitchBar();
92         if (mBeamDisallowedByOnlyAdmin) {
93             mSwitchBar.hide();
94         } else {
95             mSwitchBar.setChecked(!mBeamDisallowedByBase && mNfcAdapter.isNdefPushEnabled());
96             mSwitchBar.addOnSwitchChangeListener(this);
97             mSwitchBar.setEnabled(!mBeamDisallowedByBase);
98             mSwitchBar.show();
99         }
100     }
101 
102     @Override
onDestroyView()103     public void onDestroyView() {
104         super.onDestroyView();
105         if (mOldActivityTitle != null) {
106             getActivity().getActionBar().setTitle(mOldActivityTitle);
107         }
108         if (!mBeamDisallowedByOnlyAdmin) {
109             mSwitchBar.removeOnSwitchChangeListener(this);
110             mSwitchBar.hide();
111         }
112     }
113 
114     @Override
onSwitchChanged(Switch switchView, boolean desiredState)115     public void onSwitchChanged(Switch switchView, boolean desiredState) {
116         boolean success = false;
117         mSwitchBar.setEnabled(false);
118         if (desiredState) {
119             success = mNfcAdapter.enableNdefPush();
120         } else {
121             success = mNfcAdapter.disableNdefPush();
122         }
123         if (success) {
124             mSwitchBar.setChecked(desiredState);
125         }
126         mSwitchBar.setEnabled(true);
127     }
128 
129     @Override
getMetricsCategory()130     public int getMetricsCategory() {
131         return MetricsEvent.NFC_BEAM;
132     }
133 }
134