From 5ebb245618f0887b7329887e903e7543534de855 Mon Sep 17 00:00:00 2001 From: Joshua Date: Mon, 19 Jul 2021 16:54:35 +0000 Subject: [PATCH] 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 --- res/values/strings.xml | 2 + .../tare/TareFactorDialogFragment.java | 109 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 src/com/android/settings/development/tare/TareFactorDialogFragment.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 6f2cd04750b..3b2eeb2ed71 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -13804,4 +13804,6 @@ @string/tare_alarmmanager @string/tare_jobscheduler + + Confirm diff --git a/src/com/android/settings/development/tare/TareFactorDialogFragment.java b/src/com/android/settings/development/tare/TareFactorDialogFragment.java new file mode 100644 index 00000000000..d6ef22d7ed7 --- /dev/null +++ b/src/com/android/settings/development/tare/TareFactorDialogFragment.java @@ -0,0 +1,109 @@ +/* + * 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; + } +}