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

Commit 144f222a authored by Joshua Lewis's avatar Joshua Lewis Committed by Android (Google) Code Review
Browse files

Merge "Create Dialog to edit TARE factors"

parents 813d923c 5ebb2456
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -13804,4 +13804,6 @@
        <item>@string/tare_alarmmanager</item>
        <item>@string/tare_jobscheduler</item>
    </string-array>
    <!-- TARE dialog button to proceed with a value change [CHAR LIMIT=none] -->
    <string name="tare_dialog_confirm_button_title">Confirm</string>
</resources>
+109 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 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.development.tare;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import com.android.settings.R;
import com.android.settings.Utils;

/**
 * Dialog Fragment for changing tare factor values
 */
public class TareFactorDialogFragment extends DialogFragment {

    private static final String TAG = "TareDialogFragment";

    private final String mFactorKey;
    private final String mFactorTitle;
    private final int mFactorValue;
    private int mFactorEditedValue;

    private EditText mFactorValueView;

    /**
     * @param title        the title that will show at the top of the Dialog for the Factor
     * @param key          the key of the Factor being initialized.
     * @param defaultValue the initial value set for the Factor before any changes
     */
    public TareFactorDialogFragment(@NonNull String title, @NonNull String key, int defaultValue) {
        mFactorTitle = title;
        mFactorKey = key;
        mFactorValue = defaultValue;
    }

    /**
     * Gets the current value of the Factor
     */
    private String getFactorValue() {
        // TODO: Get value from locally cached copy
        return Integer.toString(mFactorEditedValue);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                getActivity())
                .setTitle(mFactorTitle)
                .setView(createDialogView())
                .setPositiveButton(R.string.tare_dialog_confirm_button_title, (dialog, which) -> {

                    final String stringValue = mFactorValueView.getText().toString();
                    mFactorEditedValue = mFactorValue;
                    try {
                        mFactorEditedValue = Integer.parseInt(stringValue);
                    } catch (NumberFormatException e) {
                        Log.e(TAG, "Error converting '" + stringValue + "' to integer. Using "
                                + mFactorValue + " instead", e);
                    }
                    // TODO: Update csv with new factor value
                })
                .setNegativeButton(android.R.string.cancel, (dialog, which) -> {
                    // TODO: Add proper dismiss for negative button press
                });

        return builder.create();
    }

    /**
     * Creates a view for the factor Dialog that currently
     * is linked to the basic dialog_edittext.xml layout.
     */
    private View createDialogView() {
        final LayoutInflater layoutInflater = (LayoutInflater) getActivity()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.dialog_edittext, null);
        mFactorValueView = layout.findViewById(R.id.edittext);
        mFactorValueView.setInputType(InputType.TYPE_CLASS_NUMBER);
        mFactorValueView.setText(getFactorValue());
        Utils.setEditTextCursorPosition(mFactorValueView);

        return layout;
    }
}