1 /* 2 * Copyright (C) 2013 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.Activity; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.res.Resources; 24 import android.nfc.NfcAdapter; 25 import android.os.Bundle; 26 import android.support.v7.preference.PreferenceManager; 27 import android.support.v7.preference.PreferenceScreen; 28 import android.view.Menu; 29 import android.view.MenuInflater; 30 import android.view.MenuItem; 31 import android.view.View; 32 import android.view.ViewGroup; 33 34 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 35 import com.android.settings.R; 36 import com.android.settings.search.BaseSearchIndexProvider; 37 import com.android.settings.search.Indexable; 38 import com.android.settings.search.SearchIndexableRaw; 39 import com.android.settings.SettingsPreferenceFragment; 40 import com.android.settings.dashboard.SummaryLoader; 41 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo; 42 43 import java.util.Arrays; 44 import java.util.ArrayList; 45 import java.util.List; 46 47 public class PaymentSettings extends SettingsPreferenceFragment implements Indexable { 48 public static final String TAG = "PaymentSettings"; 49 50 static final String PAYMENT_KEY = "payment"; 51 52 private PaymentBackend mPaymentBackend; 53 54 @Override getMetricsCategory()55 public int getMetricsCategory() { 56 return MetricsEvent.NFC_PAYMENT; 57 } 58 59 @Override onCreate(Bundle icicle)60 public void onCreate(Bundle icicle) { 61 super.onCreate(icicle); 62 63 mPaymentBackend = new PaymentBackend(getActivity()); 64 setHasOptionsMenu(true); 65 66 PreferenceManager manager = getPreferenceManager(); 67 PreferenceScreen screen = manager.createPreferenceScreen(getActivity()); 68 69 List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos(); 70 if (appInfos != null && appInfos.size() > 0) { 71 NfcPaymentPreference preference = 72 new NfcPaymentPreference(getPrefContext(), mPaymentBackend); 73 preference.setKey(PAYMENT_KEY); 74 screen.addPreference(preference); 75 NfcForegroundPreference foreground = new NfcForegroundPreference(getPrefContext(), 76 mPaymentBackend); 77 screen.addPreference(foreground); 78 } 79 setPreferenceScreen(screen); 80 } 81 82 @Override onViewCreated(View view, Bundle savedInstanceState)83 public void onViewCreated(View view, Bundle savedInstanceState) { 84 super.onViewCreated(view, savedInstanceState); 85 ViewGroup contentRoot = (ViewGroup) getListView().getParent(); 86 View emptyView = getActivity().getLayoutInflater().inflate( 87 R.layout.nfc_payment_empty, contentRoot, false); 88 contentRoot.addView(emptyView); 89 setEmptyView(emptyView); 90 } 91 92 @Override onResume()93 public void onResume() { 94 super.onResume(); 95 mPaymentBackend.onResume(); 96 } 97 98 @Override onPause()99 public void onPause() { 100 super.onPause(); 101 mPaymentBackend.onPause(); 102 } 103 104 @Override onCreateOptionsMenu(Menu menu, MenuInflater inflater)105 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 106 super.onCreateOptionsMenu(menu, inflater); 107 MenuItem menuItem = menu.add(R.string.nfc_payment_how_it_works); 108 Intent howItWorksIntent = new Intent(getActivity(), HowItWorks.class); 109 menuItem.setIntent(howItWorksIntent); 110 menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER); 111 } 112 113 private static class SummaryProvider implements SummaryLoader.SummaryProvider { 114 115 private final Context mContext; 116 private final SummaryLoader mSummaryLoader; 117 SummaryProvider(Context context, SummaryLoader summaryLoader)118 public SummaryProvider(Context context, SummaryLoader summaryLoader) { 119 mContext = context; 120 mSummaryLoader = summaryLoader; 121 } 122 123 @Override setListening(boolean listening)124 public void setListening(boolean listening) { 125 if (listening && NfcAdapter.getDefaultAdapter(mContext) != null) { 126 PaymentBackend paymentBackend = new PaymentBackend(mContext); 127 paymentBackend.refresh(); 128 PaymentAppInfo app = paymentBackend.getDefaultApp(); 129 if (app != null) { 130 mSummaryLoader.setSummary(this, mContext.getString(R.string.payment_summary, 131 app.label)); 132 } 133 } 134 } 135 } 136 137 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY 138 = new SummaryLoader.SummaryProviderFactory() { 139 @Override 140 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, 141 SummaryLoader summaryLoader) { 142 return new SummaryProvider(activity, summaryLoader); 143 } 144 }; 145 146 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 147 new BaseSearchIndexProvider() { 148 @Override 149 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { 150 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); 151 final Resources res = context.getResources(); 152 153 // Add fragment title 154 SearchIndexableRaw data = new SearchIndexableRaw(context); 155 data.key = PAYMENT_KEY; 156 data.title = res.getString(R.string.nfc_payment_settings_title); 157 data.screenTitle = res.getString(R.string.nfc_payment_settings_title); 158 data.keywords = res.getString(R.string.keywords_payment_settings); 159 result.add(data); 160 return result; 161 } 162 163 @Override 164 public List<String> getNonIndexableKeys(Context context) { 165 final PackageManager pm = context.getPackageManager(); 166 if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) return null; 167 final List<String> nonVisibleKeys = new ArrayList<String>(); 168 nonVisibleKeys.add(PAYMENT_KEY); 169 return nonVisibleKeys; 170 } 171 }; 172 } 173