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