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.net.Uri;
     22 import android.os.Bundle;
     23 import android.os.Handler;
     24 import android.os.Message;
     25 import android.preference.Preference;
     26 import android.preference.PreferenceManager;
     27 import android.preference.PreferenceScreen;
     28 import android.provider.Settings;
     29 import android.text.TextUtils;
     30 import android.util.Log;
     31 import android.view.LayoutInflater;
     32 import android.view.Menu;
     33 import android.view.MenuInflater;
     34 import android.view.MenuItem;
     35 import android.view.View;
     36 import android.view.View.OnClickListener;
     37 import android.view.ViewGroup;
     38 import android.widget.ImageView;
     39 import android.widget.RadioButton;
     40 import android.widget.TextView;
     41 
     42 import com.android.internal.content.PackageMonitor;
     43 import com.android.settings.HelpUtils;
     44 import com.android.settings.R;
     45 import com.android.settings.SettingsPreferenceFragment;
     46 import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
     47 
     48 import java.util.List;
     49 
     50 public class PaymentSettings extends SettingsPreferenceFragment implements
     51         OnClickListener {
     52     public static final String TAG = "PaymentSettings";
     53     private LayoutInflater mInflater;
     54     private PaymentBackend mPaymentBackend;
     55     private final PackageMonitor mSettingsPackageMonitor = new SettingsPackageMonitor();
     56 
     57 
     58     @Override
     59     public void onCreate(Bundle icicle) {
     60         super.onCreate(icicle);
     61 
     62         mPaymentBackend = new PaymentBackend(getActivity());
     63         mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     64         setHasOptionsMenu(true);
     65     }
     66 
     67     public void refresh() {
     68         PreferenceManager manager = getPreferenceManager();
     69         PreferenceScreen screen = manager.createPreferenceScreen(getActivity());
     70         // Get all payment services
     71         List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
     72         if (appInfos != null && appInfos.size() > 0) {
     73             // Add all payment apps
     74             for (PaymentAppInfo appInfo : appInfos) {
     75                 PaymentAppPreference preference =
     76                         new PaymentAppPreference(getActivity(), appInfo, this);
     77                 preference.setTitle(appInfo.caption);
     78                 if (appInfo.banner != null) {
     79                     screen.addPreference(preference);
     80                 } else {
     81                     // Ignore, no banner
     82                     Log.e(TAG, "Couldn't load banner drawable of service " + appInfo.componentName);
     83                 }
     84             }
     85         }
     86         TextView emptyText = (TextView) getView().findViewById(R.id.nfc_payment_empty_text);
     87         TextView learnMore = (TextView) getView().findViewById(R.id.nfc_payment_learn_more);
     88         ImageView emptyImage = (ImageView) getView().findViewById(R.id.nfc_payment_tap_image);
     89         if (screen.getPreferenceCount() == 0) {
     90             emptyText.setVisibility(View.VISIBLE);
     91             learnMore.setVisibility(View.VISIBLE);
     92             emptyImage.setVisibility(View.VISIBLE);
     93             getListView().setVisibility(View.GONE);
     94         } else {
     95             emptyText.setVisibility(View.GONE);
     96             learnMore.setVisibility(View.GONE);
     97             emptyImage.setVisibility(View.GONE);
     98             getListView().setVisibility(View.VISIBLE);
     99         }
    100         setPreferenceScreen(screen);
    101     }
    102 
    103     @Override
    104     public View onCreateView(LayoutInflater inflater, ViewGroup container,
    105             Bundle savedInstanceState) {
    106         super.onCreateView(inflater, container, savedInstanceState);
    107         View v = mInflater.inflate(R.layout.nfc_payment, container, false);
    108         TextView learnMore = (TextView) v.findViewById(R.id.nfc_payment_learn_more);
    109         learnMore.setOnClickListener(new OnClickListener() {
    110             @Override
    111             public void onClick(View v) {
    112                 String helpUrl;
    113                 if (!TextUtils.isEmpty(helpUrl = getResources().getString(
    114                         R.string.help_url_nfc_payment))) {
    115                     final Uri fullUri = HelpUtils.uriWithAddedParameters(
    116                             PaymentSettings.this.getActivity(), Uri.parse(helpUrl));
    117                     Intent intent = new Intent(Intent.ACTION_VIEW, fullUri);
    118                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
    119                             | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    120                     startActivity(intent);
    121                 } else {
    122                     Log.e(TAG, "Help url not set.");
    123                 }
    124             }
    125         });
    126         return v;
    127     }
    128 
    129     @Override
    130     public void onClick(View v) {
    131         if (v.getTag() instanceof PaymentAppInfo) {
    132             PaymentAppInfo appInfo = (PaymentAppInfo) v.getTag();
    133             if (appInfo.componentName != null) {
    134                 mPaymentBackend.setDefaultPaymentApp(appInfo.componentName);
    135             }
    136             refresh();
    137         }
    138     }
    139 
    140     @Override
    141     public void onResume() {
    142         super.onResume();
    143         mSettingsPackageMonitor.register(getActivity(), getActivity().getMainLooper(), false);
    144         refresh();
    145     }
    146 
    147     @Override
    148     public void onPause() {
    149         mSettingsPackageMonitor.unregister();
    150         super.onPause();
    151     }
    152 
    153     @Override
    154     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    155         super.onCreateOptionsMenu(menu, inflater);
    156         String searchUri = Settings.Secure.getString(getContentResolver(),
    157                 Settings.Secure.PAYMENT_SERVICE_SEARCH_URI);
    158         if (!TextUtils.isEmpty(searchUri)) {
    159             MenuItem menuItem = menu.add(R.string.nfc_payment_menu_item_add_service);
    160             menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    161             menuItem.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse(searchUri)));
    162         }
    163     }
    164 
    165     private final Handler mHandler = new Handler() {
    166         @Override
    167         public void dispatchMessage(Message msg) {
    168             refresh();
    169         }
    170     };
    171 
    172     private class SettingsPackageMonitor extends PackageMonitor {
    173         @Override
    174         public void onPackageAdded(String packageName, int uid) {
    175            mHandler.obtainMessage().sendToTarget();
    176         }
    177 
    178         @Override
    179         public void onPackageAppeared(String packageName, int reason) {
    180             mHandler.obtainMessage().sendToTarget();
    181         }
    182 
    183         @Override
    184         public void onPackageDisappeared(String packageName, int reason) {
    185             mHandler.obtainMessage().sendToTarget();
    186         }
    187 
    188         @Override
    189         public void onPackageRemoved(String packageName, int uid) {
    190             mHandler.obtainMessage().sendToTarget();
    191         }
    192     }
    193 
    194     public static class PaymentAppPreference extends Preference {
    195         private final OnClickListener listener;
    196         private final PaymentAppInfo appInfo;
    197 
    198         public PaymentAppPreference(Context context, PaymentAppInfo appInfo,
    199                 OnClickListener listener) {
    200             super(context);
    201             setLayoutResource(R.layout.nfc_payment_option);
    202             this.appInfo = appInfo;
    203             this.listener = listener;
    204         }
    205 
    206         @Override
    207         protected void onBindView(View view) {
    208             super.onBindView(view);
    209 
    210             view.setOnClickListener(listener);
    211             view.setTag(appInfo);
    212 
    213             RadioButton radioButton = (RadioButton) view.findViewById(android.R.id.button1);
    214             radioButton.setChecked(appInfo.isDefault);
    215 
    216             ImageView banner = (ImageView) view.findViewById(R.id.banner);
    217             banner.setImageDrawable(appInfo.banner);
    218         }
    219     }
    220 }
    221