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

Commit 5ebb2456 authored by Joshua's avatar Joshua
Browse files

Create Dialog to edit TARE factors

This CL is the base framework for creating and adjusting TARE factor
values. TareFactorDialogFragment sets the basic outline for a factor
that uses a DialogBox to adjust the value. It takes the "title" or name
of the factor, the key that will be associated with the factor, and an
initial value for the factor; then sets up everything so that the factor
value can be easily changed and stored. These factors are also
registered into the settings observer class.

Bug: 195055619
Bug: 193238774

Test: Manual (Made a temporary class instance of the DialogFragment and
showed it in inactive apps onResume())

Change-Id: I3bb67717175e70eab3bbb11f4a42ee63a42f60bc
parent 756e6379
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;
    }
}