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