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