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
17package com.android.settings.nfc;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.nfc.NfcAdapter;
23import android.os.Bundle;
24import android.support.v7.preference.PreferenceManager;
25import android.support.v7.preference.PreferenceScreen;
26import android.view.Menu;
27import android.view.MenuInflater;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.ViewGroup;
31
32import com.android.internal.logging.MetricsProto.MetricsEvent;
33import com.android.settings.R;
34import com.android.settings.SettingsPreferenceFragment;
35import com.android.settings.dashboard.SummaryLoader;
36import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
37
38import java.util.List;
39
40public class PaymentSettings extends SettingsPreferenceFragment {
41    public static final String TAG = "PaymentSettings";
42    private PaymentBackend mPaymentBackend;
43
44    @Override
45    protected int getMetricsCategory() {
46        return MetricsEvent.NFC_PAYMENT;
47    }
48
49    @Override
50    public void onCreate(Bundle icicle) {
51        super.onCreate(icicle);
52
53        mPaymentBackend = new PaymentBackend(getActivity());
54        setHasOptionsMenu(true);
55
56        PreferenceManager manager = getPreferenceManager();
57        PreferenceScreen screen = manager.createPreferenceScreen(getActivity());
58
59        List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
60        if (appInfos != null && appInfos.size() > 0) {
61            NfcPaymentPreference preference =
62                    new NfcPaymentPreference(getPrefContext(), mPaymentBackend);
63            preference.setKey("payment");
64            screen.addPreference(preference);
65            NfcForegroundPreference foreground = new NfcForegroundPreference(getPrefContext(),
66                    mPaymentBackend);
67            screen.addPreference(foreground);
68        }
69        setPreferenceScreen(screen);
70    }
71
72    @Override
73    public void onViewCreated(View view, Bundle savedInstanceState) {
74        super.onViewCreated(view, savedInstanceState);
75        ViewGroup contentRoot = (ViewGroup) getListView().getParent();
76        View emptyView = getActivity().getLayoutInflater().inflate(
77                R.layout.nfc_payment_empty, contentRoot, false);
78        contentRoot.addView(emptyView);
79        setEmptyView(emptyView);
80    }
81
82    @Override
83    public void onResume() {
84        super.onResume();
85        mPaymentBackend.onResume();
86    }
87
88    @Override
89    public void onPause() {
90        super.onPause();
91        mPaymentBackend.onPause();
92    }
93
94    @Override
95    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
96        super.onCreateOptionsMenu(menu, inflater);
97        MenuItem menuItem = menu.add(R.string.nfc_payment_how_it_works);
98        Intent howItWorksIntent = new Intent(getActivity(), HowItWorks.class);
99        menuItem.setIntent(howItWorksIntent);
100        menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_NEVER);
101    }
102
103    private static class SummaryProvider implements SummaryLoader.SummaryProvider {
104
105        private final Context mContext;
106        private final SummaryLoader mSummaryLoader;
107
108        public SummaryProvider(Context context, SummaryLoader summaryLoader) {
109            mContext = context;
110            mSummaryLoader = summaryLoader;
111        }
112
113        @Override
114        public void setListening(boolean listening) {
115            if (listening && NfcAdapter.getDefaultAdapter(mContext) != null) {
116                PaymentBackend paymentBackend = new PaymentBackend(mContext);
117                paymentBackend.refresh();
118                PaymentAppInfo app = paymentBackend.getDefaultApp();
119                if (app != null) {
120                    mSummaryLoader.setSummary(this, mContext.getString(R.string.payment_summary,
121                            app.label));
122                }
123            }
124        }
125    }
126
127    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
128            = new SummaryLoader.SummaryProviderFactory() {
129        @Override
130        public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
131                                                                   SummaryLoader summaryLoader) {
132            return new SummaryProvider(activity, summaryLoader);
133        }
134    };
135}
136