Home | History | Annotate | Download | only in nfc
      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.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.res.Resources;
     23 import android.os.Bundle;
     24 import android.support.v7.preference.PreferenceManager;
     25 import android.support.v7.preference.PreferenceScreen;
     26 import android.view.Menu;
     27 import android.view.MenuInflater;
     28 import android.view.MenuItem;
     29 import android.view.View;
     30 import android.view.ViewGroup;
     31 
     32 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
     33 import com.android.settings.R;
     34 import com.android.settings.SettingsPreferenceFragment;
     35 import com.android.settings.search.BaseSearchIndexProvider;
     36 import com.android.settings.search.Indexable;
     37 import com.android.settings.search.SearchIndexableRaw;
     38 
     39 import java.util.ArrayList;
     40 import java.util.List;
     41 
     42 public class PaymentSettings extends SettingsPreferenceFragment implements Indexable {
     43     public static final String TAG = "PaymentSettings";
     44 
     45     static final String PAYMENT_KEY = "payment";
     46 
     47     private PaymentBackend mPaymentBackend;
     48 
     49     @Override
     50     public int getMetricsCategory() {
     51         return MetricsEvent.NFC_PAYMENT;
     52     }
     53 
     54     @Override
     55     protected int getPreferenceScreenResId() {
     56         return R.xml.nfc_payment_settings;
     57     }
     58 
     59     @Override
     60     public void onCreate(Bundle icicle) {
     61         super.onCreate(icicle);
     62 
     63         mPaymentBackend = new PaymentBackend(getActivity());
     64         setHasOptionsMenu(true);
     65 
     66         final PreferenceScreen screen = getPreferenceScreen();
     67 
     68         List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
     69         if (appInfos != null && appInfos.size() > 0) {
     70             NfcPaymentPreference preference =
     71                     new NfcPaymentPreference(getPrefContext(), mPaymentBackend);
     72             preference.setKey(PAYMENT_KEY);
     73             screen.addPreference(preference);
     74             NfcForegroundPreference foreground = new NfcForegroundPreference(getPrefContext(),
     75                     mPaymentBackend);
     76             screen.addPreference(foreground);
     77         }
     78     }
     79 
     80     @Override
     81     public void onViewCreated(View view, Bundle savedInstanceState) {
     82         super.onViewCreated(view, savedInstanceState);
     83         ViewGroup contentRoot = (ViewGroup) getListView().getParent();
     84         View emptyView = getActivity().getLayoutInflater().inflate(
     85                 R.layout.nfc_payment_empty, contentRoot, false);
     86         contentRoot.addView(emptyView);
     87         setEmptyView(emptyView);
     88     }
     89 
     90     @Override
     91     public void onResume() {
     92         super.onResume();
     93         mPaymentBackend.onResume();
     94     }
     95 
     96     @Override
     97     public void onPause() {
     98         super.onPause();
     99         mPaymentBackend.onPause();
    100     }
    101 
    102     @Override
    103     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    104         super.onCreateOptionsMenu(menu, inflater);
    105         MenuItem menuItem = menu.add(R.string.nfc_payment_how_it_works);
    106         Intent howItWorksIntent = new Intent(getActivity(), HowItWorks.class);
    107         menuItem.setIntent(howItWorksIntent);
    108         menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);
    109     }
    110 
    111     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
    112         new BaseSearchIndexProvider() {
    113             @Override
    114             public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
    115                 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
    116                 final Resources res = context.getResources();
    117 
    118                 // Add fragment title
    119                 SearchIndexableRaw data = new SearchIndexableRaw(context);
    120                 data.key = PAYMENT_KEY;
    121                 data.title = res.getString(R.string.nfc_payment_settings_title);
    122                 data.screenTitle = res.getString(R.string.nfc_payment_settings_title);
    123                 data.keywords = res.getString(R.string.keywords_payment_settings);
    124                 result.add(data);
    125                 return result;
    126             }
    127 
    128             @Override
    129             public List<String> getNonIndexableKeys(Context context) {
    130                 final List<String> nonVisibleKeys = super.getNonIndexableKeys(context);
    131                 final PackageManager pm = context.getPackageManager();
    132                 if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
    133                     return nonVisibleKeys;
    134                 }
    135                 nonVisibleKeys.add(PAYMENT_KEY);
    136                 return nonVisibleKeys;
    137             }
    138         };
    139 }
    140