Handle disallowed NFC beam restriction

Don't enable the beam setting and toggle if it is disallowed for
the current user.

Bug: 17387303
Change-Id: Ifdfe049bef281454c978a37acb49c59758344ae6
This commit is contained in:
Amith Yamasani
2014-09-09 12:07:11 -07:00
parent 4fc50daefa
commit 25a1e95ed6
2 changed files with 18 additions and 4 deletions

View File

@@ -18,12 +18,15 @@ package com.android.settings.nfc;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.Fragment; import android.app.Fragment;
import android.content.Context;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserManager;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Switch; import android.widget.Switch;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsActivity; import com.android.settings.SettingsActivity;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
@@ -34,6 +37,7 @@ public class AndroidBeam extends Fragment
private NfcAdapter mNfcAdapter; private NfcAdapter mNfcAdapter;
private SwitchBar mSwitchBar; private SwitchBar mSwitchBar;
private CharSequence mOldActivityTitle; private CharSequence mOldActivityTitle;
private boolean mBeamDisallowed;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@@ -45,6 +49,8 @@ public class AndroidBeam extends Fragment
actionBar.setTitle(R.string.android_beam_settings_title); actionBar.setTitle(R.string.android_beam_settings_title);
mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity()); mNfcAdapter = NfcAdapter.getDefaultAdapter(getActivity());
mBeamDisallowed = ((UserManager) getActivity().getSystemService(Context.USER_SERVICE))
.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM);
} }
@Override @Override
@@ -62,8 +68,9 @@ public class AndroidBeam extends Fragment
SettingsActivity activity = (SettingsActivity) getActivity(); SettingsActivity activity = (SettingsActivity) getActivity();
mSwitchBar = activity.getSwitchBar(); mSwitchBar = activity.getSwitchBar();
mSwitchBar.setChecked(mNfcAdapter.isNdefPushEnabled()); mSwitchBar.setChecked(!mBeamDisallowed && mNfcAdapter.isNdefPushEnabled());
mSwitchBar.addOnSwitchChangeListener(this); mSwitchBar.addOnSwitchChangeListener(this);
mSwitchBar.setEnabled(!mBeamDisallowed);
mSwitchBar.show(); mSwitchBar.show();
} }

View File

@@ -21,10 +21,11 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.nfc.NfcAdapter; import android.nfc.NfcAdapter;
import android.os.UserManager;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
import com.android.settings.R; import com.android.settings.R;
/** /**
@@ -38,6 +39,7 @@ public class NfcEnabler implements Preference.OnPreferenceChangeListener {
private final PreferenceScreen mAndroidBeam; private final PreferenceScreen mAndroidBeam;
private final NfcAdapter mNfcAdapter; private final NfcAdapter mNfcAdapter;
private final IntentFilter mIntentFilter; private final IntentFilter mIntentFilter;
private boolean mBeamDisallowed;
private final BroadcastReceiver mReceiver = new BroadcastReceiver() { private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override @Override
@@ -56,6 +58,8 @@ public class NfcEnabler implements Preference.OnPreferenceChangeListener {
mSwitch = switchPreference; mSwitch = switchPreference;
mAndroidBeam = androidBeam; mAndroidBeam = androidBeam;
mNfcAdapter = NfcAdapter.getDefaultAdapter(context); mNfcAdapter = NfcAdapter.getDefaultAdapter(context);
mBeamDisallowed = ((UserManager) mContext.getSystemService(Context.USER_SERVICE))
.hasUserRestriction(UserManager.DISALLOW_OUTGOING_BEAM);
if (mNfcAdapter == null) { if (mNfcAdapter == null) {
// NFC is not supported // NFC is not supported
@@ -64,6 +68,9 @@ public class NfcEnabler implements Preference.OnPreferenceChangeListener {
mIntentFilter = null; mIntentFilter = null;
return; return;
} }
if (mBeamDisallowed) {
mAndroidBeam.setEnabled(false);
}
mIntentFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED); mIntentFilter = new IntentFilter(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED);
} }
@@ -110,8 +117,8 @@ public class NfcEnabler implements Preference.OnPreferenceChangeListener {
case NfcAdapter.STATE_ON: case NfcAdapter.STATE_ON:
mSwitch.setChecked(true); mSwitch.setChecked(true);
mSwitch.setEnabled(true); mSwitch.setEnabled(true);
mAndroidBeam.setEnabled(true); mAndroidBeam.setEnabled(!mBeamDisallowed);
if (mNfcAdapter.isNdefPushEnabled()) { if (mNfcAdapter.isNdefPushEnabled() && !mBeamDisallowed) {
mAndroidBeam.setSummary(R.string.android_beam_on_summary); mAndroidBeam.setSummary(R.string.android_beam_on_summary);
} else { } else {
mAndroidBeam.setSummary(R.string.android_beam_off_summary); mAndroidBeam.setSummary(R.string.android_beam_off_summary);