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     public void onCreate(Bundle icicle) {
     56         super.onCreate(icicle);
     57 
     58         mPaymentBackend = new PaymentBackend(getActivity());
     59         setHasOptionsMenu(true);
     60 
     61         PreferenceManager manager = getPreferenceManager();
     62         PreferenceScreen screen = manager.createPreferenceScreen(getActivity());
     63 
     64         List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
     65         if (appInfos != null && appInfos.size() > 0) {
     66             NfcPaymentPreference preference =
     67                     new NfcPaymentPreference(getPrefContext(), mPaymentBackend);
     68             preference.setKey(PAYMENT_KEY);
     69             screen.addPreference(preference);
     70             NfcForegroundPreference foreground = new NfcForegroundPreference(getPrefContext(),
     71                     mPaymentBackend);
     72             screen.addPreference(foreground);
     73         }
     74         setPreferenceScreen(screen);
     75     }
     76 
     77     @Override
     78     public void onViewCreated(View view, Bundle savedInstanceState) {
     79         super.onViewCreated(view, savedInstanceState);
     80         ViewGroup contentRoot = (ViewGroup) getListView().getParent();
     81         View emptyView = getActivity().getLayoutInflater().inflate(
     82                 R.layout.nfc_payment_empty, contentRoot, false);
     83         contentRoot.addView(emptyView);
     84         setEmptyView(emptyView);
     85     }
     86 
     87     @Override
     88     public void onResume() {
     89         super.onResume();
     90         mPaymentBackend.onResume();
     91     }
     92 
     93     @Override
     94     public void onPause() {
     95         super.onPause();
     96         mPaymentBackend.onPause();
     97     }
     98 
     99     @Override
    100     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    101         super.onCreateOptionsMenu(menu, inflater);
    102         MenuItem menuItem = menu.add(R.string.nfc_payment_how_it_works);
    103         Intent howItWorksIntent = new Intent(getActivity(), HowItWorks.class);
    104         menuItem.setIntent(howItWorksIntent);
    105         menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);
    106     }
    107 
    108     public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
    109         new BaseSearchIndexProvider() {
    110             @Override
    111             public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
    112                 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
    113                 final Resources res = context.getResources();
    114 
    115                 // Add fragment title
    116                 SearchIndexableRaw data = new SearchIndexableRaw(context);
    117                 data.key = PAYMENT_KEY;
    118                 data.title = res.getString(R.string.nfc_payment_settings_title);
    119                 data.screenTitle = res.getString(R.string.nfc_payment_settings_title);
    120                 data.keywords = res.getString(R.string.keywords_payment_settings);
    121                 result.add(data);
    122                 return result;
    123             }
    124 
    125             @Override
    126             public List<String> getNonIndexableKeys(Context context) {
    127                 final List<String> nonVisibleKeys = super.getNonIndexableKeys(context);
    128                 final PackageManager pm = context.getPackageManager();
    129                 if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
    130                     return nonVisibleKeys;
    131                 }
    132                 nonVisibleKeys.add(PAYMENT_KEY);
    133                 return nonVisibleKeys;
    134             }
    135         };
    136 }
    137