Migrate PaymentSettings to DashboardFragment
- Move preference related logic to controllers. - Add some test cases for controllers. Test: manual Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.nfc make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.core atest SettingsGatewayTest UniquePreferenceTest Change-Id: I061a194c170f63fab51974f26c24be43d67d6f6f
This commit is contained in:
@@ -19,11 +19,8 @@ import android.content.Context;
|
||||
import android.database.ContentObserver;
|
||||
import android.net.Uri;
|
||||
import android.nfc.NfcAdapter;
|
||||
import android.nfc.NfcManager;
|
||||
import android.os.Handler;
|
||||
import android.provider.Settings;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.core.PreferenceControllerMixin;
|
||||
import com.android.settingslib.core.AbstractPreferenceController;
|
||||
@@ -31,7 +28,8 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnPause;
|
||||
import com.android.settingslib.core.lifecycle.events.OnResume;
|
||||
|
||||
import java.util.List;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
public abstract class BaseNfcPreferenceController extends AbstractPreferenceController
|
||||
implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
|
||||
@@ -65,13 +63,6 @@ public abstract class BaseNfcPreferenceController extends AbstractPreferenceCont
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateNonIndexableKeys(List<String> keys) {
|
||||
if (!isAvailable()) {
|
||||
keys.add(getPreferenceKey());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return mNfcAdapter != null;
|
||||
|
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* 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 androidx.preference.DropDownPreference;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
|
||||
public class NfcForegroundPreference extends DropDownPreference implements
|
||||
PaymentBackend.Callback, Preference.OnPreferenceChangeListener {
|
||||
|
||||
private final PaymentBackend mPaymentBackend;
|
||||
public NfcForegroundPreference(Context context, PaymentBackend backend) {
|
||||
super(context);
|
||||
mPaymentBackend = backend;
|
||||
mPaymentBackend.registerCallback(this);
|
||||
|
||||
setTitle(getContext().getString(R.string.nfc_payment_use_default));
|
||||
setEntries(new CharSequence[] {
|
||||
getContext().getString(R.string.nfc_payment_favor_open),
|
||||
getContext().getString(R.string.nfc_payment_favor_default)
|
||||
});
|
||||
setEntryValues(new CharSequence[] { "1", "0" });
|
||||
refresh();
|
||||
setOnPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaymentAppsChanged() {
|
||||
refresh();
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
boolean foregroundMode = mPaymentBackend.isForegroundMode();
|
||||
if (foregroundMode) {
|
||||
setValue("1");
|
||||
} else {
|
||||
setValue("0");
|
||||
}
|
||||
setSummary(getEntry());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
String newValueString = (String) newValue;
|
||||
setSummary(getEntries()[findIndexOfValue(newValueString)]);
|
||||
mPaymentBackend.setForegroundMode(Integer.parseInt(newValueString) != 0);
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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 android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.core.BasePreferenceController;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.preference.DropDownPreference;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
public class NfcForegroundPreferenceController extends BasePreferenceController implements
|
||||
PaymentBackend.Callback, Preference.OnPreferenceChangeListener,
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private DropDownPreference mPreference;
|
||||
private PaymentBackend mPaymentBackend;
|
||||
|
||||
public NfcForegroundPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
}
|
||||
|
||||
public void setPaymentBackend(PaymentBackend backend) {
|
||||
mPaymentBackend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
if (mPaymentBackend != null) {
|
||||
mPaymentBackend.registerCallback(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
if (mPaymentBackend != null) {
|
||||
mPaymentBackend.unregisterCallback(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
final PackageManager pm = mContext.getPackageManager();
|
||||
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
|
||||
return DISABLED_UNSUPPORTED;
|
||||
}
|
||||
if (mPaymentBackend == null) {
|
||||
return DISABLED_UNSUPPORTED;
|
||||
}
|
||||
final List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
|
||||
return (appInfos != null && !appInfos.isEmpty())
|
||||
? AVAILABLE
|
||||
: DISABLED_UNSUPPORTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = (DropDownPreference) screen.findPreference(getPreferenceKey());
|
||||
if (mPreference == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
mPreference.setEntries(new CharSequence[] {
|
||||
mContext.getText(R.string.nfc_payment_favor_open),
|
||||
mContext.getText(R.string.nfc_payment_favor_default)
|
||||
});
|
||||
mPreference.setEntryValues(new CharSequence[] {"1", "0"});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaymentAppsChanged() {
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
if (preference instanceof DropDownPreference) {
|
||||
((DropDownPreference) preference).setValue(
|
||||
mPaymentBackend.isForegroundMode() ? "1" : "0");
|
||||
}
|
||||
super.updateState(preference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
return mPreference.getEntry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||
if (!(preference instanceof DropDownPreference)) {
|
||||
return false;
|
||||
}
|
||||
final DropDownPreference pref = (DropDownPreference) preference;
|
||||
final String newValueString = (String) newValue;
|
||||
pref.setSummary(pref.getEntries()[pref.findIndexOfValue(newValueString)]);
|
||||
mPaymentBackend.setForegroundMode(Integer.parseInt(newValueString) != 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateNonIndexableKeys(List<String> keys) {
|
||||
final String key = getPreferenceKey();
|
||||
if (!TextUtils.isEmpty(key)) {
|
||||
keys.add(key);
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,80 +16,49 @@
|
||||
package com.android.settings.nfc;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.app.Dialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
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 android.util.AttributeSet;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
|
||||
import com.android.settingslib.CustomDialogPreference;
|
||||
|
||||
import java.util.List;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
public class NfcPaymentPreference extends CustomDialogPreference implements
|
||||
PaymentBackend.Callback, View.OnClickListener {
|
||||
public class NfcPaymentPreference extends CustomDialogPreference {
|
||||
|
||||
private static final String TAG = "NfcPaymentPreference";
|
||||
private Listener mListener;
|
||||
|
||||
private final NfcPaymentAdapter mAdapter;
|
||||
private final Context mContext;
|
||||
private final LayoutInflater mLayoutInflater;
|
||||
private final PaymentBackend mPaymentBackend;
|
||||
interface Listener {
|
||||
void onBindViewHolder(PreferenceViewHolder view);
|
||||
|
||||
// Fields below only modified on UI thread
|
||||
private ImageView mSettingsButtonView;
|
||||
void onPrepareDialogBuilder(AlertDialog.Builder builder,
|
||||
DialogInterface.OnClickListener listener);
|
||||
}
|
||||
|
||||
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_gear);
|
||||
public NfcPaymentPreference(Context context, AttributeSet attrs, int defStyleAttr,
|
||||
int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
}
|
||||
|
||||
refresh();
|
||||
public NfcPaymentPreference(Context context, AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
public NfcPaymentPreference(Context context, AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
}
|
||||
|
||||
void initialize(Listener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
super.onBindViewHolder(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);
|
||||
if (mListener != null) {
|
||||
mListener.onBindViewHolder(view);
|
||||
}
|
||||
setTitle(R.string.nfc_payment_default);
|
||||
if (defaultApp != null) {
|
||||
setSummary(defaultApp.label);
|
||||
} else {
|
||||
setSummary(mContext.getString(R.string.nfc_payment_default_not_set));
|
||||
}
|
||||
updateSettingsVisibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,122 +66,8 @@ public class NfcPaymentPreference extends CustomDialogPreference implements
|
||||
DialogInterface.OnClickListener listener) {
|
||||
super.onPrepareDialogBuilder(builder, listener);
|
||||
|
||||
builder.setSingleChoiceItems(mAdapter, 0, listener);
|
||||
}
|
||||
|
||||
@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.setContentDescription(appInfo.label);
|
||||
holder.imageView.setOnClickListener(this);
|
||||
|
||||
// Prevent checked callback getting called on recycled views
|
||||
holder.radioButton.setOnCheckedChangeListener(null);
|
||||
holder.radioButton.setChecked(appInfo.isDefault);
|
||||
holder.radioButton.setContentDescription(appInfo.label);
|
||||
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);
|
||||
}
|
||||
Dialog dialog = getDialog();
|
||||
if (dialog != null)
|
||||
dialog.dismiss();
|
||||
if (mListener != null) {
|
||||
mListener.onPrepareDialogBuilder(builder, listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
257
src/com/android/settings/nfc/NfcPaymentPreferenceController.java
Normal file
257
src/com/android/settings/nfc/NfcPaymentPreferenceController.java
Normal file
@@ -0,0 +1,257 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.app.Dialog;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
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.core.BasePreferenceController;
|
||||
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
|
||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
|
||||
public class NfcPaymentPreferenceController extends BasePreferenceController implements
|
||||
PaymentBackend.Callback, View.OnClickListener, NfcPaymentPreference.Listener,
|
||||
LifecycleObserver, OnStart, OnStop {
|
||||
|
||||
private static final String TAG = "NfcPaymentController";
|
||||
|
||||
private final NfcPaymentAdapter mAdapter;
|
||||
private PaymentBackend mPaymentBackend;
|
||||
private NfcPaymentPreference mPreference;
|
||||
private ImageView mSettingsButtonView;
|
||||
|
||||
public NfcPaymentPreferenceController(Context context, String key) {
|
||||
super(context, key);
|
||||
mAdapter = new NfcPaymentAdapter(context);
|
||||
}
|
||||
|
||||
public void setPaymentBackend(PaymentBackend backend) {
|
||||
mPaymentBackend = backend;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
if (mPaymentBackend != null) {
|
||||
mPaymentBackend.registerCallback(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop() {
|
||||
if (mPaymentBackend != null) {
|
||||
mPaymentBackend.unregisterCallback(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
final PackageManager pm = mContext.getPackageManager();
|
||||
if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
|
||||
return DISABLED_UNSUPPORTED;
|
||||
}
|
||||
if (mPaymentBackend == null) {
|
||||
mPaymentBackend = new PaymentBackend(mContext);
|
||||
}
|
||||
final List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
|
||||
return (appInfos != null && !appInfos.isEmpty())
|
||||
? AVAILABLE
|
||||
: DISABLED_UNSUPPORTED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void displayPreference(PreferenceScreen screen) {
|
||||
super.displayPreference(screen);
|
||||
mPreference = (NfcPaymentPreference) screen.findPreference(getPreferenceKey());
|
||||
if (mPreference != null) {
|
||||
mPreference.initialize(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(PreferenceViewHolder view) {
|
||||
mSettingsButtonView = (ImageView) view.findViewById(R.id.settings_button);
|
||||
mSettingsButtonView.setOnClickListener(this);
|
||||
|
||||
updateSettingsVisibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(Preference preference) {
|
||||
final List<PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
|
||||
if (appInfos != null) {
|
||||
final PaymentAppInfo[] apps = appInfos.toArray(new PaymentAppInfo[appInfos.size()]);
|
||||
mAdapter.updateApps(apps);
|
||||
}
|
||||
super.updateState(preference);
|
||||
updateSettingsVisibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
final PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
|
||||
if (defaultApp != null) {
|
||||
return defaultApp.label;
|
||||
} else {
|
||||
return mContext.getText(R.string.nfc_payment_default_not_set);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareDialogBuilder(AlertDialog.Builder builder,
|
||||
DialogInterface.OnClickListener listener) {
|
||||
builder.setSingleChoiceItems(mAdapter, 0, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPaymentAppsChanged() {
|
||||
updateState(mPreference);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
final PaymentAppInfo defaultAppInfo = mPaymentBackend.getDefaultApp();
|
||||
if (defaultAppInfo != null && defaultAppInfo.settingsComponent != null) {
|
||||
final 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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateSettingsVisibility() {
|
||||
if (mSettingsButtonView != null) {
|
||||
final PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
|
||||
if (defaultApp == null || defaultApp.settingsComponent == null) {
|
||||
mSettingsButtonView.setVisibility(View.GONE);
|
||||
} else {
|
||||
mSettingsButtonView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class NfcPaymentAdapter extends BaseAdapter implements
|
||||
CompoundButton.OnCheckedChangeListener, View.OnClickListener {
|
||||
private final LayoutInflater mLayoutInflater;
|
||||
|
||||
// Only modified on UI thread
|
||||
private PaymentAppInfo[] appInfos;
|
||||
|
||||
public NfcPaymentAdapter(Context context) {
|
||||
mLayoutInflater = (LayoutInflater) context.getSystemService(
|
||||
Context.LAYOUT_INFLATER_SERVICE);
|
||||
}
|
||||
|
||||
public void updateApps(PaymentAppInfo[] appInfos) {
|
||||
// Clone app infos, only add those with a banner
|
||||
this.appInfos = appInfos;
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return (appInfos != null) ? appInfos.length : 0;
|
||||
}
|
||||
|
||||
@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;
|
||||
final PaymentAppInfo appInfo = appInfos[position];
|
||||
if (convertView == null) {
|
||||
convertView = mLayoutInflater.inflate(
|
||||
R.layout.nfc_payment_option, parent, false);
|
||||
holder = new ViewHolder();
|
||||
holder.imageView = convertView.findViewById(R.id.banner);
|
||||
holder.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.setContentDescription(appInfo.label);
|
||||
holder.imageView.setOnClickListener(this);
|
||||
|
||||
// Prevent checked callback getting called on recycled views
|
||||
holder.radioButton.setOnCheckedChangeListener(null);
|
||||
holder.radioButton.setChecked(appInfo.isDefault);
|
||||
holder.radioButton.setContentDescription(appInfo.label);
|
||||
holder.radioButton.setOnCheckedChangeListener(this);
|
||||
holder.radioButton.setTag(appInfo);
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
private void makeDefault(PaymentAppInfo appInfo) {
|
||||
if (!appInfo.isDefault) {
|
||||
mPaymentBackend.setDefaultPaymentApp(appInfo.componentName);
|
||||
}
|
||||
final Dialog dialog = mPreference.getDialog();
|
||||
if (dialog != null) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -25,7 +25,9 @@ import android.nfc.NfcAdapter;
|
||||
import android.nfc.cardemulation.ApduServiceInfo;
|
||||
import android.nfc.cardemulation.CardEmulation;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.UserHandle;
|
||||
import android.provider.Settings;
|
||||
import android.provider.Settings.SettingNotFoundException;
|
||||
|
||||
@@ -57,7 +59,7 @@ public class PaymentBackend {
|
||||
// Fields below only modified on UI thread
|
||||
private ArrayList<PaymentAppInfo> mAppInfos;
|
||||
private PaymentAppInfo mDefaultAppInfo;
|
||||
private ArrayList<Callback> mCallbacks = new ArrayList<Callback>();
|
||||
private ArrayList<Callback> mCallbacks = new ArrayList<>();
|
||||
|
||||
public PaymentBackend(Context context) {
|
||||
mContext = context;
|
||||
@@ -102,7 +104,8 @@ public class PaymentBackend {
|
||||
appInfo.componentName = service.getComponent();
|
||||
String settingsActivity = service.getSettingsActivityName();
|
||||
if (settingsActivity != null) {
|
||||
appInfo.settingsComponent = new ComponentName(appInfo.componentName.getPackageName(),
|
||||
appInfo.settingsComponent = new ComponentName(
|
||||
appInfo.componentName.getPackageName(),
|
||||
settingsActivity);
|
||||
} else {
|
||||
appInfo.settingsComponent = null;
|
||||
@@ -162,7 +165,7 @@ public class PaymentBackend {
|
||||
|
||||
void setForegroundMode(boolean foreground) {
|
||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
||||
Settings.Secure.NFC_PAYMENT_FOREGROUND, foreground ? 1 : 0) ;
|
||||
Settings.Secure.NFC_PAYMENT_FOREGROUND, foreground ? 1 : 0);
|
||||
}
|
||||
|
||||
ComponentName getDefaultPaymentApp() {
|
||||
@@ -182,14 +185,23 @@ public class PaymentBackend {
|
||||
refresh();
|
||||
}
|
||||
|
||||
private final Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void dispatchMessage(Message msg) {
|
||||
refresh();
|
||||
}
|
||||
};
|
||||
|
||||
private class SettingsPackageMonitor extends PackageMonitor {
|
||||
private Handler mHandler;
|
||||
|
||||
@Override
|
||||
public void register(Context context, Looper thread, UserHandle user,
|
||||
boolean externalStorage) {
|
||||
if (mHandler == null) {
|
||||
mHandler = new Handler(thread) {
|
||||
@Override
|
||||
public void dispatchMessage(Message msg) {
|
||||
refresh();
|
||||
}
|
||||
};
|
||||
}
|
||||
super.register(context, thread, user, externalStorage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPackageAdded(String packageName, int uid) {
|
||||
mHandler.obtainMessage().sendToTarget();
|
||||
@@ -210,4 +222,4 @@ public class PaymentBackend {
|
||||
mHandler.obtainMessage().sendToTarget();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -19,10 +19,8 @@ package com.android.settings.nfc;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import android.provider.SearchIndexableResource;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
@@ -31,23 +29,25 @@ import android.view.ViewGroup;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsPreferenceFragment;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.search.BaseSearchIndexProvider;
|
||||
import com.android.settings.search.Indexable;
|
||||
import com.android.settings.search.SearchIndexableRaw;
|
||||
import com.android.settingslib.search.SearchIndexable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@SearchIndexable
|
||||
public class PaymentSettings extends SettingsPreferenceFragment implements Indexable {
|
||||
public class PaymentSettings extends DashboardFragment {
|
||||
public static final String TAG = "PaymentSettings";
|
||||
|
||||
static final String PAYMENT_KEY = "payment";
|
||||
|
||||
private PaymentBackend mPaymentBackend;
|
||||
|
||||
@Override
|
||||
protected String getLogTag() {
|
||||
return TAG;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMetricsCategory() {
|
||||
return MetricsEvent.NFC_PAYMENT;
|
||||
@@ -59,24 +59,13 @@ public class PaymentSettings extends SettingsPreferenceFragment implements Index
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mPaymentBackend = new PaymentBackend(getActivity());
|
||||
setHasOptionsMenu(true);
|
||||
|
||||
final PreferenceScreen screen = getPreferenceScreen();
|
||||
|
||||
List<PaymentBackend.PaymentAppInfo> appInfos = mPaymentBackend.getPaymentAppInfos();
|
||||
if (appInfos != null && appInfos.size() > 0) {
|
||||
NfcPaymentPreference preference =
|
||||
new NfcPaymentPreference(getPrefContext(), mPaymentBackend);
|
||||
preference.setKey(PAYMENT_KEY);
|
||||
screen.addPreference(preference);
|
||||
NfcForegroundPreference foreground = new NfcForegroundPreference(getPrefContext(),
|
||||
mPaymentBackend);
|
||||
screen.addPreference(foreground);
|
||||
}
|
||||
use(NfcPaymentPreferenceController.class).setPaymentBackend(mPaymentBackend);
|
||||
use(NfcForegroundPreferenceController.class).setPaymentBackend(mPaymentBackend);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -111,31 +100,19 @@ public class PaymentSettings extends SettingsPreferenceFragment implements Index
|
||||
}
|
||||
|
||||
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
|
||||
final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
|
||||
final Resources res = context.getResources();
|
||||
|
||||
// Add fragment title
|
||||
SearchIndexableRaw data = new SearchIndexableRaw(context);
|
||||
data.key = PAYMENT_KEY;
|
||||
data.title = res.getString(R.string.nfc_payment_settings_title);
|
||||
data.screenTitle = res.getString(R.string.nfc_payment_settings_title);
|
||||
data.keywords = res.getString(R.string.keywords_payment_settings);
|
||||
result.add(data);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getNonIndexableKeys(Context context) {
|
||||
final List<String> nonVisibleKeys = super.getNonIndexableKeys(context);
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
if (pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
|
||||
return nonVisibleKeys;
|
||||
new BaseSearchIndexProvider() {
|
||||
@Override
|
||||
public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
|
||||
boolean enabled) {
|
||||
final SearchIndexableResource sir = new SearchIndexableResource(context);
|
||||
sir.xmlResId = R.xml.nfc_payment_settings;
|
||||
return Arrays.asList(sir);
|
||||
}
|
||||
nonVisibleKeys.add(PAYMENT_KEY);
|
||||
return nonVisibleKeys;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isPageSearchEnabled(Context context) {
|
||||
final PackageManager pm = context.getPackageManager();
|
||||
return pm.hasSystemFeature(PackageManager.FEATURE_NFC);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user