Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6451b7b9 authored by android-build-team Robot's avatar android-build-team Robot Committed by Android (Google) Code Review
Browse files

Merge "Migrate PaymentSettings to DashboardFragment"

parents ecb74eb2 ff244c0e
Loading
Loading
Loading
Loading
+15 −0
Original line number Original line Diff line number Diff line
@@ -16,5 +16,20 @@


<PreferenceScreen
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="nfc_payment_settings_screen"
    android:title="@string/nfc_payment_settings_title">
    android:title="@string/nfc_payment_settings_title">

    <com.android.settings.nfc.NfcPaymentPreference
        android:key="nfc_payment"
        android:title="@string/nfc_payment_default"
        android:dialogTitle="@string/nfc_payment_pay_with"
        android:widgetLayout="@layout/preference_widget_gear"
        settings:controller="com.android.settings.nfc.NfcPaymentPreferenceController"/>

    <DropDownPreference
        android:key="nfc_foreground"
        android:title="@string/nfc_payment_use_default"
        settings:controller="com.android.settings.nfc.NfcForegroundPreferenceController"/>

</PreferenceScreen>
</PreferenceScreen>
+2 −11
Original line number Original line Diff line number Diff line
@@ -19,11 +19,8 @@ import android.content.Context;
import android.database.ContentObserver;
import android.database.ContentObserver;
import android.net.Uri;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.os.Handler;
import android.os.Handler;
import android.provider.Settings;
import android.provider.Settings;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;


import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
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.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume;
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
public abstract class BaseNfcPreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin, LifecycleObserver, OnResume, OnPause {
        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
    @Override
    public boolean isAvailable() {
    public boolean isAvailable() {
        return mNfcAdapter != null;
        return mNfcAdapter != null;
+0 −65
Original line number Original line Diff line number Diff line
/*
 * 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;
    }
}
+129 −0
Original line number Original line Diff line number Diff line
/*
 * 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);
        }
    }
}
 No newline at end of file
+27 −172
Original line number Original line Diff line number Diff line
@@ -16,203 +16,58 @@
package com.android.settings.nfc;
package com.android.settings.nfc;


import android.app.AlertDialog;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Context;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.AttributeSet;
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 com.android.settings.R;
import com.android.settings.nfc.PaymentBackend.PaymentAppInfo;
import com.android.settingslib.CustomDialogPreference;
import com.android.settingslib.CustomDialogPreference;


import java.util.List;
import androidx.preference.PreferenceViewHolder;

public class NfcPaymentPreference extends CustomDialogPreference implements
        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_gear);

        refresh();
    }

    @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);
        }
        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
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
            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() {
public class NfcPaymentPreference extends CustomDialogPreference {
        if (mSettingsButtonView != null) {
            PaymentAppInfo defaultApp = mPaymentBackend.getDefaultApp();
            if (defaultApp == null || defaultApp.settingsComponent == null) {
                mSettingsButtonView.setVisibility(View.GONE);
            } else {
                mSettingsButtonView.setVisibility(View.VISIBLE);


            }
    private Listener mListener;
        }
    }


    class NfcPaymentAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener,
    interface Listener {
            View.OnClickListener {
        void onBindViewHolder(PreferenceViewHolder view);
        // Only modified on UI thread
        private PaymentAppInfo[] appInfos;


        public NfcPaymentAdapter() {
        void onPrepareDialogBuilder(AlertDialog.Builder builder,
                DialogInterface.OnClickListener listener);
    }
    }


        public void updateApps(PaymentAppInfo[] appInfos, PaymentAppInfo currentDefault) {
    public NfcPaymentPreference(Context context, AttributeSet attrs, int defStyleAttr,
            // Clone app infos, only add those with a banner
            int defStyleRes) {
            this.appInfos = appInfos;
        super(context, attrs, defStyleAttr, defStyleRes);
            notifyDataSetChanged();
    }
    }


        @Override
    public NfcPaymentPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        public int getCount() {
        super(context, attrs, defStyleAttr);
            return appInfos.length;
    }
    }


        @Override
    public NfcPaymentPreference(Context context, AttributeSet attrs) {
        public PaymentAppInfo getItem(int i) {
        super(context, attrs);
            return appInfos[i];
    }
    }


        @Override
    void initialize(Listener listener) {
        public long getItemId(int i) {
        mListener = listener;
            return appInfos[i].componentName.hashCode();
    }
    }


    @Override
    @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    public void onBindViewHolder(PreferenceViewHolder view) {
            ViewHolder holder;
        super.onBindViewHolder(view);
            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
        if (mListener != null) {
            holder.radioButton.setOnCheckedChangeListener(null);
            mListener.onBindViewHolder(view);
            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
    @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder,
            PaymentAppInfo appInfo = (PaymentAppInfo) compoundButton.getTag();
            DialogInterface.OnClickListener listener) {
            makeDefault(appInfo);
        super.onPrepareDialogBuilder(builder, listener);
        }

        @Override
        public void onClick(View view) {
            PaymentAppInfo appInfo = (PaymentAppInfo) view.getTag();
            makeDefault(appInfo);
        }


        void makeDefault(PaymentAppInfo appInfo) {
        if (mListener != null) {
            if (!appInfo.isDefault) {
            mListener.onPrepareDialogBuilder(builder, listener);
                mPaymentBackend.setDefaultPaymentApp(appInfo.componentName);
            }
            Dialog dialog = getDialog();
            if (dialog != null)
                dialog.dismiss();
        }
        }
    }
    }
}
}
Loading