1 /* 2 * Copyright (C) 2015 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 package com.android.settings.nfc; 17 18 import android.content.Context; 19 import android.support.v7.preference.DropDownPreference; 20 import android.support.v7.preference.Preference; 21 22 import com.android.settings.R; 23 24 public class NfcForegroundPreference extends DropDownPreference implements 25 PaymentBackend.Callback, Preference.OnPreferenceChangeListener { 26 27 private final PaymentBackend mPaymentBackend; 28 public NfcForegroundPreference(Context context, PaymentBackend backend) { 29 super(context); 30 mPaymentBackend = backend; 31 mPaymentBackend.registerCallback(this); 32 33 setTitle(getContext().getString(R.string.nfc_payment_use_default)); 34 setEntries(new CharSequence[] { 35 getContext().getString(R.string.nfc_payment_favor_open), 36 getContext().getString(R.string.nfc_payment_favor_default) 37 }); 38 setEntryValues(new CharSequence[] { "1", "0" }); 39 refresh(); 40 setOnPreferenceChangeListener(this); 41 } 42 43 @Override 44 public void onPaymentAppsChanged() { 45 refresh(); 46 } 47 48 void refresh() { 49 boolean foregroundMode = mPaymentBackend.isForegroundMode(); 50 if (foregroundMode) { 51 setValue("1"); 52 } else { 53 setValue("0"); 54 } 55 setSummary(getEntry()); 56 } 57 58 @Override 59 public boolean onPreferenceChange(Preference preference, Object newValue) { 60 String newValueString = (String) newValue; 61 setSummary(getEntries()[findIndexOfValue(newValueString)]); 62 mPaymentBackend.setForegroundMode(Integer.parseInt(newValueString) != 0); 63 return true; 64 } 65 } 66