• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
20 
21 import android.app.settings.SettingsEnums;
22 import android.content.Context;
23 import android.content.pm.PackageManager;
24 import android.nfc.NfcAdapter;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.os.UserManager;
28 import android.view.LayoutInflater;
29 import android.view.Menu;
30 import android.view.MenuInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.ImageView;
34 import android.widget.Switch;
35 import android.widget.TextView;
36 
37 import com.android.settings.R;
38 import com.android.settings.SettingsActivity;
39 import com.android.settings.core.InstrumentedFragment;
40 import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
41 import com.android.settings.widget.SettingsMainSwitchBar;
42 import com.android.settingslib.HelpUtils;
43 import com.android.settingslib.RestrictedLockUtilsInternal;
44 import com.android.settingslib.widget.OnMainSwitchChangeListener;
45 
46 public class AndroidBeam extends InstrumentedFragment
47         implements OnMainSwitchChangeListener {
48     private View mView;
49     private NfcAdapter mNfcAdapter;
50     private SettingsMainSwitchBar mSwitchBar;
51     private CharSequence mOldActivityTitle;
52     private boolean mBeamDisallowedByBase;
53     private boolean mBeamDisallowedByOnlyAdmin;
54 
55     @Override
onCreate(Bundle savedInstanceState)56     public void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         final Context context = getActivity();
59         mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
60         final PackageManager pm = context.getPackageManager();
61         if (mNfcAdapter == null || !pm.hasSystemFeature(PackageManager.FEATURE_NFC_BEAM))
62             getActivity().finish();
63         setHasOptionsMenu(true);
64     }
65 
66     @Override
onCreateOptionsMenu(Menu menu, MenuInflater inflater)67     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
68         super.onCreateOptionsMenu(menu, inflater);
69         HelpUtils.prepareHelpMenuItem(getActivity(), menu, R.string.help_uri_beam,
70                 getClass().getName());
71     }
72 
73     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)74     public View onCreateView(LayoutInflater inflater, ViewGroup container,
75             Bundle savedInstanceState) {
76         final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced(
77                 getActivity(), UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId());
78         final UserManager um = UserManager.get(getActivity());
79         mBeamDisallowedByBase = RestrictedLockUtilsInternal.hasBaseUserRestriction(getActivity(),
80                 UserManager.DISALLOW_OUTGOING_BEAM, UserHandle.myUserId());
81         if (!mBeamDisallowedByBase && admin != null) {
82             new ActionDisabledByAdminDialogHelper(getActivity())
83                     .prepareDialogBuilder(UserManager.DISALLOW_OUTGOING_BEAM, admin).show();
84             mBeamDisallowedByOnlyAdmin = true;
85             return new View(getContext());
86         }
87         mView = inflater.inflate(R.layout.preference_footer, container, false);
88 
89         ImageView iconInfo = mView.findViewById(android.R.id.icon);
90         iconInfo.setImageResource(R.drawable.ic_info_outline_24dp);
91         TextView textInfo = mView.findViewById(android.R.id.title);
92         textInfo.setText(R.string.android_beam_explained);
93 
94         return mView;
95     }
96 
97     @Override
onActivityCreated(Bundle savedInstanceState)98     public void onActivityCreated(Bundle savedInstanceState) {
99         super.onActivityCreated(savedInstanceState);
100         SettingsActivity activity = (SettingsActivity) getActivity();
101 
102         mOldActivityTitle = activity.getActionBar().getTitle();
103 
104         mSwitchBar = activity.getSwitchBar();
105         if (mBeamDisallowedByOnlyAdmin) {
106             mSwitchBar.hide();
107         } else {
108             mSwitchBar.setChecked(!mBeamDisallowedByBase && mNfcAdapter.isNdefPushEnabled());
109             mSwitchBar.addOnSwitchChangeListener(this);
110             mSwitchBar.setEnabled(!mBeamDisallowedByBase);
111             mSwitchBar.show();
112         }
113 
114         activity.setTitle(R.string.android_beam_settings_title);
115     }
116 
117     @Override
onDestroyView()118     public void onDestroyView() {
119         super.onDestroyView();
120         if (mOldActivityTitle != null) {
121             getActivity().getActionBar().setTitle(mOldActivityTitle);
122         }
123         if (!mBeamDisallowedByOnlyAdmin) {
124             mSwitchBar.removeOnSwitchChangeListener(this);
125             mSwitchBar.hide();
126         }
127     }
128 
129     @Override
onSwitchChanged(Switch switchView, boolean desiredState)130     public void onSwitchChanged(Switch switchView, boolean desiredState) {
131         boolean success = false;
132         mSwitchBar.setEnabled(false);
133         if (desiredState) {
134             success = mNfcAdapter.enableNdefPush();
135         } else {
136             success = mNfcAdapter.disableNdefPush();
137         }
138         if (success) {
139             mSwitchBar.setChecked(desiredState);
140         }
141         mSwitchBar.setEnabled(true);
142     }
143 
144     @Override
getMetricsCategory()145     public int getMetricsCategory() {
146         return SettingsEnums.NFC_BEAM;
147     }
148 }
149