summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings
diff options
context:
space:
mode:
authorMartijn Coenen <maco@google.com>2015-04-29 07:29:30 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2015-04-29 07:29:32 +0000
commit01d13c043a7d9f2aef357e17f2d6a36c678006e6 (patch)
treef4c366445f02ce4514a13d5553e8b4b1a02bde6b /src/com/android/settings
parenta16257dbd5599135320f4263a2cb438b7a469084 (diff)
parentfe58b534f6a1bf63dadb18dae13c59ed8a014eec (diff)
downloadpackages_apps_Settings-01d13c043a7d9f2aef357e17f2d6a36c678006e6.tar.gz
packages_apps_Settings-01d13c043a7d9f2aef357e17f2d6a36c678006e6.tar.bz2
packages_apps_Settings-01d13c043a7d9f2aef357e17f2d6a36c678006e6.zip
Merge "New Tap & Pay UX." into mnc-dev
Diffstat (limited to 'src/com/android/settings')
-rw-r--r--src/com/android/settings/nfc/NfcForegroundPreference.java68
-rw-r--r--src/com/android/settings/nfc/NfcPaymentPreference.java211
-rw-r--r--src/com/android/settings/nfc/PaymentBackend.java184
-rw-r--r--src/com/android/settings/nfc/PaymentDefaultDialog.java6
-rw-r--r--src/com/android/settings/nfc/PaymentSettings.java193
5 files changed, 472 insertions, 190 deletions
diff --git a/src/com/android/settings/nfc/NfcForegroundPreference.java b/src/com/android/settings/nfc/NfcForegroundPreference.java
new file mode 100644
index 000000000..4f4398f3f
--- /dev/null
+++ b/src/com/android/settings/nfc/NfcForegroundPreference.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.nfc;
+
+import android.content.Context;
+import com.android.settings.DropDownPreference;
+import com.android.settings.R;
+
+public class NfcForegroundPreference extends DropDownPreference implements
+ DropDownPreference.Callback, PaymentBackend.Callback {
+
+ private final PaymentBackend mPaymentBackend;
+ public NfcForegroundPreference(Context context, PaymentBackend backend) {
+ super(context);
+ mPaymentBackend = backend;
+ mPaymentBackend.registerCallback(this);
+ setCallback(this);
+ refresh();
+ }
+
+ @Override
+ public void onPaymentAppsChanged() {
+ refresh();
+ }
+
+ void refresh() {
+ PaymentBackend.PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
+ boolean foregroundMode = mPaymentBackend.isForegroundMode();
+ setPersistent(false);
+ setTitle(getContext().getString(R.string.nfc_payment_open_app));
+ CharSequence favorOpen;
+ CharSequence favorDefault;
+ clearItems();
+ if (defaultApp == null) {
+ favorOpen = getContext().getString(R.string.nfc_payment_favor_open_default_unknown);
+ favorDefault = getContext().getString(R.string.nfc_payment_favor_default_default_unknown);
+ } else {
+ favorOpen = getContext().getString(R.string.nfc_payment_favor_open, defaultApp.label);
+ favorDefault = getContext().getString(R.string.nfc_payment_favor_default, defaultApp.label);
+ }
+ addItem(favorOpen.toString(), true);
+ addItem(favorDefault.toString(), false);
+ if (foregroundMode) {
+ setSelectedValue(true);
+ } else {
+ setSelectedValue(false);
+ }
+ }
+
+ @Override
+ public boolean onItemSelected(int pos, Object value) {
+ mPaymentBackend.setForegroundMode((Boolean) value);
+ return true;
+ }
+}
diff --git a/src/com/android/settings/nfc/NfcPaymentPreference.java b/src/com/android/settings/nfc/NfcPaymentPreference.java
new file mode 100644
index 000000000..e24a651c2
--- /dev/null
+++ b/src/com/android/settings/nfc/NfcPaymentPreference.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.settings.nfc;
+
+import android.app.AlertDialog;
+import android.content.ActivityNotFoundException;
+import android.content.Context;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.preference.DialogPreference;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.CompoundButton;
+import android.widget.ImageView;
+import android.widget.RadioButton;
+import com.android.settings.R;
+import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
+
+import java.util.List;
+
+public class NfcPaymentPreference extends DialogPreference implements
+ DialogInterface.OnClickListener, PaymentBackend.Callback, View.OnClickListener {
+
+ private static final String TAG = "NfcPaymentPreference";
+
+ private final NfcPaymentAdapter mAdapter;
+ private final Context mContext;
+ private final LayoutInflater mLayoutInflater;
+ private final PaymentBackend mPaymentBackend;
+
+ // Fields below only modified on UI thread
+ private ImageView mSettingsButtonView;
+
+ public NfcPaymentPreference(Context context, PaymentBackend backend) {
+ super(context, null);
+ mPaymentBackend = backend;
+ mContext = context;
+ backend.registerCallback(this);
+ mAdapter = new NfcPaymentAdapter();
+ setDialogTitle(context.getString(R.string.nfc_payment_pay_with));
+ mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ setWidgetLayoutResource(R.layout.preference_widget_settings);
+
+ refresh();
+ }
+
+ @Override
+ protected void onBindView(View view) {
+ super.onBindView(view);
+
+ mSettingsButtonView = (ImageView) view.findViewById(R.id.settings_button);
+ mSettingsButtonView.setOnClickListener(this);
+
+ updateSettingsVisibility();
+ }
+
+ /**
+ * MUST be called on UI thread.
+ */
+ public void refresh() {
+ List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
+ PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
+ if (appInfos != null) {
+ PaymentAppInfo[] apps = appInfos.toArray(new PaymentAppInfo[appInfos.size()]);
+ mAdapter.updateApps(apps, defaultApp);
+ }
+ setTitle(R.string.nfc_payment_default);
+ if (defaultApp != null) {
+ setSummary(mContext.getString(R.string.nfc_payment_app_and_desc,
+ defaultApp.label, defaultApp.description));
+ } else {
+ setSummary(mContext.getString(R.string.nfc_payment_default_not_set));
+ }
+ updateSettingsVisibility();
+ }
+
+ @Override
+ protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
+ super.onPrepareDialogBuilder(builder);
+
+ builder.setSingleChoiceItems(mAdapter, 0, this);
+ }
+
+ @Override
+ public void onPaymentAppsChanged() {
+ refresh();
+ }
+
+ @Override
+ public void onClick(View view) {
+ PaymentAppInfo defaultAppInfo = mPaymentBackend.getDefaultApp();
+ if (defaultAppInfo != null && defaultAppInfo.settingsComponent != null) {
+ Intent settingsIntent = new Intent(Intent.ACTION_MAIN);
+ settingsIntent.setComponent(defaultAppInfo.settingsComponent);
+ settingsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ try {
+ mContext.startActivity(settingsIntent);
+ } catch (ActivityNotFoundException e) {
+ Log.e(TAG, "Settings activity not found.");
+ }
+ }
+ }
+
+ void updateSettingsVisibility() {
+ if (mSettingsButtonView != null) {
+ PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
+ if (defaultApp == null || defaultApp.settingsComponent == null) {
+ mSettingsButtonView.setVisibility(View.GONE);
+ } else {
+ mSettingsButtonView.setVisibility(View.VISIBLE);
+
+ }
+ }
+ }
+
+ class NfcPaymentAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener,
+ View.OnClickListener {
+ // Only modified on UI thread
+ private PaymentAppInfo[] appInfos;
+
+ public NfcPaymentAdapter() {
+ }
+
+ public void updateApps(PaymentAppInfo[] appInfos, PaymentAppInfo currentDefault) {
+ // Clone app infos, only add those with a banner
+ this.appInfos = appInfos;
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getCount() {
+ return appInfos.length;
+ }
+
+ @Override
+ public PaymentAppInfo getItem(int i) {
+ return appInfos[i];
+ }
+
+ @Override
+ public long getItemId(int i) {
+ return appInfos[i].componentName.hashCode();
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ ViewHolder holder;
+ PaymentAppInfo appInfo = appInfos[position];
+ if (convertView == null) {
+ convertView = mLayoutInflater.inflate(
+ R.layout.nfc_payment_option, parent, false);
+ holder = new ViewHolder();
+ holder.imageView = (ImageView) convertView.findViewById(R.id.banner);
+ holder.radioButton = (RadioButton) convertView.findViewById(R.id.button);
+ convertView.setTag(holder);
+ } else {
+ holder = (ViewHolder) convertView.getTag();
+ }
+ holder.imageView.setImageDrawable(appInfo.banner);
+ holder.imageView.setTag(appInfo);
+ holder.imageView.setOnClickListener(this);
+
+ // Prevent checked callback getting called on recycled views
+ holder.radioButton.setOnCheckedChangeListener(null);
+ holder.radioButton.setChecked(appInfo.isDefault);
+ holder.radioButton.setOnCheckedChangeListener(this);
+ holder.radioButton.setTag(appInfo);
+ return convertView;
+ }
+
+ public class ViewHolder {
+ public ImageView imageView;
+ public RadioButton radioButton;
+ }
+
+ @Override
+ public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
+ PaymentAppInfo appInfo = (PaymentAppInfo) compoundButton.getTag();
+ makeDefault(appInfo);
+ }
+
+ @Override
+ public void onClick(View view) {
+ PaymentAppInfo appInfo = (PaymentAppInfo) view.getTag();
+ makeDefault(appInfo);
+ }
+
+ void makeDefault(PaymentAppInfo appInfo) {
+ if (!appInfo.isDefault) {
+ mPaymentBackend.setDefaultPaymentApp(appInfo.componentName);
+ }
+ getDialog().dismiss();
+ }
+ }
+}
diff --git a/src/com/android/settings/nfc/PaymentBackend.java b/src/com/android/settings/nfc/PaymentBackend.java
index 25572a72a..52e3f7e12 100644
--- a/src/com/android/settings/nfc/PaymentBackend.java
+++ b/src/com/android/settings/nfc/PaymentBackend.java
@@ -16,15 +16,22 @@
package com.android.settings.nfc;
-import android.content.ComponentName;
-import android.content.Context;
+import android.app.Activity;
+import android.content.*;
import android.content.pm.PackageManager;
+import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.nfc.NfcAdapter;
import android.nfc.cardemulation.ApduServiceInfo;
import android.nfc.cardemulation.CardEmulation;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.os.UserHandle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
+import android.util.Log;
+import com.android.internal.content.PackageMonitor;
import java.util.ArrayList;
import java.util.List;
@@ -32,47 +39,135 @@ import java.util.List;
public class PaymentBackend {
public static final String TAG = "Settings.PaymentBackend";
+ public interface Callback {
+ void onPaymentAppsChanged();
+ }
+
public static class PaymentAppInfo {
- CharSequence caption;
+ CharSequence label;
+ CharSequence description;
Drawable banner;
boolean isDefault;
public ComponentName componentName;
+ public ComponentName settingsComponent;
}
private final Context mContext;
private final NfcAdapter mAdapter;
private final CardEmulation mCardEmuManager;
+ private final PackageMonitor mSettingsPackageMonitor = new SettingsPackageMonitor();
+ // Fields below only modified on UI thread
+ private ArrayList<PaymentAppInfo> mAppInfos;
+ private PaymentAppInfo mDefaultAppInfo;
+ private ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
public PaymentBackend(Context context) {
mContext = context;
mAdapter = NfcAdapter.getDefaultAdapter(context);
mCardEmuManager = CardEmulation.getInstance(mAdapter);
+ refresh();
}
- public List<PaymentAppInfo> getPaymentAppInfos() {
+ public void onPause() {
+ mSettingsPackageMonitor.unregister();
+ mContext.unregisterReceiver(mReceiver);
+ }
+
+ public void onResume() {
+ mSettingsPackageMonitor.register(mContext, mContext.getMainLooper(), false);
+ // Register broadcast receiver for dynamic resource updates
+ IntentFilter filter = new IntentFilter(CardEmulation.ACTION_REQUEST_SERVICE_RESOURCES);
+ mContext.registerReceiver(mReceiver, filter);
+ }
+
+ public void refresh() {
PackageManager pm = mContext.getPackageManager();
List<ApduServiceInfo> serviceInfos =
mCardEmuManager.getServices(CardEmulation.CATEGORY_PAYMENT);
- List<PaymentAppInfo> appInfos = new ArrayList<PaymentAppInfo>();
-
- if (serviceInfos == null) return appInfos;
+ ArrayList<PaymentAppInfo> appInfos = new ArrayList<PaymentAppInfo>();
- ComponentName defaultApp = getDefaultPaymentApp();
+ if (serviceInfos == null) {
+ makeCallbacks();
+ return;
+ }
+ ComponentName defaultAppName = getDefaultPaymentApp();
+ PaymentAppInfo foundDefaultApp = null;
for (ApduServiceInfo service : serviceInfos) {
PaymentAppInfo appInfo = new PaymentAppInfo();
- appInfo.banner = service.loadBanner(pm);
- appInfo.caption = service.getDescription();
- if (appInfo.caption == null) {
- appInfo.caption = service.loadLabel(pm);
+ appInfo.label = service.loadLabel(pm);
+ if (appInfo.label == null) {
+ appInfo.label = service.loadAppLabel(pm);
+ }
+ appInfo.isDefault = service.getComponent().equals(defaultAppName);
+ if (appInfo.isDefault) {
+ foundDefaultApp = appInfo;
}
- appInfo.isDefault = service.getComponent().equals(defaultApp);
appInfo.componentName = service.getComponent();
+ String settingsActivity = service.getSettingsActivityName();
+ if (settingsActivity != null) {
+ appInfo.settingsComponent = new ComponentName(appInfo.componentName.getPackageName(),
+ settingsActivity);
+ } else {
+ appInfo.settingsComponent = null;
+ }
+ if (service.hasDynamicResources()) {
+ appInfo.description = "";
+ appInfo.banner = null;
+ sendBroadcastForResources(appInfo);
+ } else {
+ appInfo.description = service.getDescription();
+ appInfo.banner = service.loadBanner(pm);
+ }
appInfos.add(appInfo);
}
+ mAppInfos = appInfos;
+ mDefaultAppInfo = foundDefaultApp;
+ makeCallbacks();
+ }
+
+ public void registerCallback(Callback callback) {
+ mCallbacks.add(callback);
+ }
+
+ public void unregisterCallback(Callback callback) {
+ mCallbacks.remove(callback);
+ }
+
+ public List<PaymentAppInfo> getPaymentAppInfos() {
+ return mAppInfos;
+ }
+
+ public PaymentAppInfo getDefaultApp() {
+ return mDefaultAppInfo;
+ }
+
+ void makeCallbacks() {
+ for (Callback callback : mCallbacks) {
+ callback.onPaymentAppsChanged();
+ }
+ }
+
+ Drawable loadDrawableForPackage(String pkgName, int drawableResId) {
+ PackageManager pm = mContext.getPackageManager();
+ try {
+ Resources res = pm.getResourcesForApplication(pkgName);
+ Drawable banner = res.getDrawable(drawableResId);
+ return banner;
+ } catch (Resources.NotFoundException e) {
+ return null;
+ } catch (PackageManager.NameNotFoundException e) {
+ return null;
+ }
+ }
- return appInfos;
+ void sendBroadcastForResources(PaymentAppInfo appInfo) {
+ Intent broadcastIntent = new Intent(CardEmulation.ACTION_REQUEST_SERVICE_RESOURCES);
+ broadcastIntent.setPackage(appInfo.componentName.getPackageName());
+ broadcastIntent.putExtra(CardEmulation.EXTRA_SERVICE_COMPONENT, appInfo.componentName);
+ mContext.sendOrderedBroadcastAsUser(broadcastIntent, UserHandle.CURRENT,
+ null, mReceiver, null, Activity.RESULT_OK, null, null);
}
boolean isForegroundMode() {
@@ -103,5 +198,66 @@ public class PaymentBackend {
Settings.Secure.putString(mContext.getContentResolver(),
Settings.Secure.NFC_PAYMENT_DEFAULT_COMPONENT,
app != null ? app.flattenToString() : null);
+ refresh();
+ }
+
+ private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Bundle results = getResultExtras(false);
+ if (results != null) {
+ String desc = results.getString(CardEmulation.EXTRA_DESCRIPTION);
+ int resId = results.getInt(CardEmulation.EXTRA_BANNER_RES_ID, -1);
+ // Find corresponding component
+ PaymentAppInfo matchingAppInfo = null;
+ for (PaymentAppInfo appInfo : mAppInfos) {
+ if (appInfo.componentName.equals(
+ intent.getParcelableExtra(CardEmulation.EXTRA_SERVICE_COMPONENT))) {
+ matchingAppInfo = appInfo;
+ }
+ }
+ if (matchingAppInfo != null && (desc != null || resId != -1)) {
+ if (desc != null) {
+ matchingAppInfo.description = desc;
+ }
+ if (resId != -1) {
+ matchingAppInfo.banner = loadDrawableForPackage(
+ matchingAppInfo.componentName.getPackageName(), resId);
+ }
+ makeCallbacks();
+ }
+ } else {
+ Log.e(TAG, "Didn't find results extra.");
+ }
+
+ }
+ };
+ private final Handler mHandler = new Handler() {
+ @Override
+ public void dispatchMessage(Message msg) {
+ refresh();
+ }
+ };
+
+ private class SettingsPackageMonitor extends PackageMonitor {
+ @Override
+ public void onPackageAdded(String packageName, int uid) {
+ mHandler.obtainMessage().sendToTarget();
+ }
+
+ @Override
+ public void onPackageAppeared(String packageName, int reason) {
+ mHandler.obtainMessage().sendToTarget();
+ }
+
+ @Override
+ public void onPackageDisappeared(String packageName, int reason) {
+ mHandler.obtainMessage().sendToTarget();
+ }
+
+ @Override
+ public void onPackageRemoved(String packageName, int uid) {
+ mHandler.obtainMessage().sendToTarget();
+ }
}
} \ No newline at end of file
diff --git a/src/com/android/settings/nfc/PaymentDefaultDialog.java b/src/com/android/settings/nfc/PaymentDefaultDialog.java
index 33ac94743..949f87d8b 100644
--- a/src/com/android/settings/nfc/PaymentDefaultDialog.java
+++ b/src/com/android/settings/nfc/PaymentDefaultDialog.java
@@ -111,13 +111,13 @@ public final class PaymentDefaultDialog extends AlertActivity implements
if (defaultPaymentApp == null) {
String formatString = getString(R.string.nfc_payment_set_default);
String msg = String.format(formatString,
- sanitizePaymentAppCaption(requestedPaymentApp.caption.toString()));
+ sanitizePaymentAppCaption(requestedPaymentApp.label.toString()));
p.mMessage = msg;
} else {
String formatString = getString(R.string.nfc_payment_set_default_instead_of);
String msg = String.format(formatString,
- sanitizePaymentAppCaption(requestedPaymentApp.caption.toString()),
- sanitizePaymentAppCaption(defaultPaymentApp.caption.toString()));
+ sanitizePaymentAppCaption(requestedPaymentApp.label.toString()),
+ sanitizePaymentAppCaption(defaultPaymentApp.label.toString()));
p.mMessage = msg;
}
p.mPositiveButtonText = getString(R.string.yes);
diff --git a/src/com/android/settings/nfc/PaymentSettings.java b/src/com/android/settings/nfc/PaymentSettings.java
index 4f04d73a6..ca38d9206 100644
--- a/src/com/android/settings/nfc/PaymentSettings.java
+++ b/src/com/android/settings/nfc/PaymentSettings.java
@@ -16,47 +16,24 @@
package com.android.settings.nfc;
-import android.content.Context;
-import android.content.Intent;
-import android.net.Uri;
import android.os.Bundle;
-import android.os.Handler;
-import android.os.Message;
-import android.preference.Preference;
-import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
-import android.preference.SwitchPreference;
-import android.provider.Settings;
-import android.text.TextUtils;
-import android.util.Log;
-import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
-import android.view.View.OnClickListener;
import android.view.ViewGroup;
-import android.widget.ImageView;
-import android.widget.RadioButton;
-import android.widget.TextView;
-
-import com.android.internal.content.PackageMonitor;
import com.android.internal.logging.MetricsLogger;
-import com.android.settings.HelpUtils;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
import java.util.List;
-public class PaymentSettings extends SettingsPreferenceFragment implements
- OnClickListener, OnPreferenceChangeListener {
+public class PaymentSettings extends SettingsPreferenceFragment {
public static final String TAG = "PaymentSettings";
- private LayoutInflater mInflater;
private PaymentBackend mPaymentBackend;
- private final PackageMonitor mSettingsPackageMonitor = new SettingsPackageMonitor();
-
@Override
protected int getMetricsCategory() {
@@ -68,180 +45,50 @@ public class PaymentSettings extends SettingsPreferenceFragment implements
super.onCreate(icicle);
mPaymentBackend = new PaymentBackend(getActivity());
- mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setHasOptionsMenu(true);
}
- public void refresh() {
+ @Override
+ public void onViewCreated(View view, Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+ ViewGroup contentRoot = (ViewGroup) getListView().getParent();
+ View emptyView = getActivity().getLayoutInflater().inflate(
+ R.layout.nfc_payment_empty, contentRoot, false);
+ contentRoot.addView(emptyView);
+ getListView().setEmptyView(emptyView);
+
PreferenceManager manager = getPreferenceManager();
PreferenceScreen screen = manager.createPreferenceScreen(getActivity());
- // Get all payment services
List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
if (appInfos != null && appInfos.size() > 0) {
- // Add all payment apps
- for (PaymentAppInfo appInfo : appInfos) {
- PaymentAppPreference preference =
- new PaymentAppPreference(getActivity(), appInfo, this);
- preference.setTitle(appInfo.caption);
- if (appInfo.banner != null) {
- screen.addPreference(preference);
- } else {
- // Ignore, no banner
- Log.e(TAG, "Couldn't load banner drawable of service " + appInfo.componentName);
- }
- }
- }
- TextView emptyText = (TextView) getView().findViewById(R.id.nfc_payment_empty_text);
- TextView learnMore = (TextView) getView().findViewById(R.id.nfc_payment_learn_more);
- ImageView emptyImage = (ImageView) getView().findViewById(R.id.nfc_payment_tap_image);
- if (screen.getPreferenceCount() == 0) {
- emptyText.setVisibility(View.VISIBLE);
- learnMore.setVisibility(View.VISIBLE);
- emptyImage.setVisibility(View.VISIBLE);
- getListView().setVisibility(View.GONE);
- } else {
- SwitchPreference foreground = new SwitchPreference(getActivity());
- boolean foregroundMode = mPaymentBackend.isForegroundMode();
- foreground.setPersistent(false);
- foreground.setTitle(getString(R.string.nfc_payment_favor_foreground));
- foreground.setChecked(foregroundMode);
- foreground.setOnPreferenceChangeListener(this);
+ NfcPaymentPreference preference =
+ new NfcPaymentPreference(getActivity(), mPaymentBackend);
+ screen.addPreference(preference);
+ NfcForegroundPreference foreground = new NfcForegroundPreference(getActivity(),
+ mPaymentBackend);
screen.addPreference(foreground);
- emptyText.setVisibility(View.GONE);
- learnMore.setVisibility(View.GONE);
- emptyImage.setVisibility(View.GONE);
- getListView().setVisibility(View.VISIBLE);
}
setPreferenceScreen(screen);
}
@Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- super.onCreateView(inflater, container, savedInstanceState);
- View v = mInflater.inflate(R.layout.nfc_payment, container, false);
- TextView learnMore = (TextView) v.findViewById(R.id.nfc_payment_learn_more);
- learnMore.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String helpUrl;
- if (!TextUtils.isEmpty(helpUrl = getResources().getString(
- R.string.help_url_nfc_payment))) {
- final Uri fullUri = HelpUtils.uriWithAddedParameters(
- PaymentSettings.this.getActivity(), Uri.parse(helpUrl));
- Intent intent = new Intent(Intent.ACTION_VIEW, fullUri);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- startActivity(intent);
- } else {
- Log.e(TAG, "Help url not set.");
- }
- }
- });
- return v;
- }
-
- @Override
- public void onClick(View v) {
- if (v.getTag() instanceof PaymentAppInfo) {
- PaymentAppInfo appInfo = (PaymentAppInfo) v.getTag();
- if (appInfo.componentName != null) {
- mPaymentBackend.setDefaultPaymentApp(appInfo.componentName);
- }
- refresh();
- }
- }
-
- @Override
public void onResume() {
super.onResume();
- mSettingsPackageMonitor.register(getActivity(), getActivity().getMainLooper(), false);
- refresh();
+ mPaymentBackend.onResume();
}
@Override
public void onPause() {
- mSettingsPackageMonitor.unregister();
super.onPause();
+ mPaymentBackend.onPause();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
- String searchUri = Settings.Secure.getString(getContentResolver(),
- Settings.Secure.PAYMENT_SERVICE_SEARCH_URI);
- if (!TextUtils.isEmpty(searchUri)) {
- MenuItem menuItem = menu.add(R.string.nfc_payment_menu_item_add_service);
- menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM);
- menuItem.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse(searchUri)));
- }
- }
-
- private final Handler mHandler = new Handler() {
- @Override
- public void dispatchMessage(Message msg) {
- refresh();
- }
- };
-
- private class SettingsPackageMonitor extends PackageMonitor {
- @Override
- public void onPackageAdded(String packageName, int uid) {
- mHandler.obtainMessage().sendToTarget();
- }
-
- @Override
- public void onPackageAppeared(String packageName, int reason) {
- mHandler.obtainMessage().sendToTarget();
- }
-
- @Override
- public void onPackageDisappeared(String packageName, int reason) {
- mHandler.obtainMessage().sendToTarget();
- }
-
- @Override
- public void onPackageRemoved(String packageName, int uid) {
- mHandler.obtainMessage().sendToTarget();
- }
- }
-
- public static class PaymentAppPreference extends Preference {
- private final OnClickListener listener;
- private final PaymentAppInfo appInfo;
-
- public PaymentAppPreference(Context context, PaymentAppInfo appInfo,
- OnClickListener listener) {
- super(context);
- setLayoutResource(R.layout.nfc_payment_option);
- this.appInfo = appInfo;
- this.listener = listener;
- }
-
- @Override
- protected void onBindView(View view) {
- super.onBindView(view);
-
- RadioButton radioButton = (RadioButton) view.findViewById(android.R.id.button1);
- radioButton.setChecked(appInfo.isDefault);
- radioButton.setOnClickListener(listener);
- radioButton.setTag(appInfo);
-
- ImageView banner = (ImageView) view.findViewById(R.id.banner);
- banner.setImageDrawable(appInfo.banner);
- banner.setOnClickListener(listener);
- banner.setTag(appInfo);
- }
- }
-
- @Override
- public boolean onPreferenceChange(Preference preference, Object newValue) {
- if (preference instanceof SwitchPreference) {
- mPaymentBackend.setForegroundMode(((Boolean) newValue).booleanValue());
- return true;
- } else {
- return false;
- }
+ MenuItem menuItem = menu.add(R.string.nfc_payment_how_it_works);
+ menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
+ // TODO link to tutorial screen
}
}