Make final factors configurable.
1. Add configuration support for the ongoing rewards (that are normally less than 1 ARC). 2. Fix key/configuration of instant rewards 3. Fix UI string issue Bug: 158300259 Bug: 189850067 Bug: 191876714 Bug: 191877052 Test: Use UI to change factors. Check Settings via adb and TARE dumpsys Change-Id: I571e188faa6915ea74826b725c9883a825ab152a
This commit is contained in:
@@ -16,6 +16,8 @@
|
||||
|
||||
package com.android.settings.development.tare;
|
||||
|
||||
import static android.app.tare.EconomyManager.CAKE_IN_ARC;
|
||||
|
||||
import android.annotation.NonNull;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Dialog;
|
||||
@@ -26,7 +28,10 @@ import android.text.InputType;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
@@ -35,39 +40,35 @@ import com.android.settings.Utils;
|
||||
* Dialog Fragment for changing tare factor values
|
||||
*/
|
||||
public class TareFactorDialogFragment extends DialogFragment {
|
||||
|
||||
private static final String TAG = "TareDialogFragment";
|
||||
|
||||
// This follows the order in strings.xml:tare_units array.
|
||||
private static final int UNIT_IDX_ARC = 0;
|
||||
private static final int UNIT_IDX_CAKE = 1;
|
||||
|
||||
private final String mFactorKey;
|
||||
private final String mFactorTitle;
|
||||
private final int mFactorValue;
|
||||
private final long mFactorValue;
|
||||
private final int mFactorPolicy;
|
||||
private int mFactorEditedValue;
|
||||
private final TareFactorController mTareFactorController;
|
||||
|
||||
private EditText mFactorValueView;
|
||||
private TareFactorController mTareFactorController;
|
||||
private Spinner mUnitSpinner;
|
||||
|
||||
/**
|
||||
* @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
|
||||
* @param currentValue the current value set for the Factor
|
||||
*/
|
||||
public TareFactorDialogFragment(@NonNull String title, @NonNull String key, int defaultValue,
|
||||
public TareFactorDialogFragment(@NonNull String title, @NonNull String key, long currentValue,
|
||||
int factorPolicy, TareFactorController tareFactorController) {
|
||||
mFactorTitle = title;
|
||||
mFactorKey = key;
|
||||
mFactorValue = defaultValue;
|
||||
mFactorValue = currentValue;
|
||||
mFactorPolicy = factorPolicy;
|
||||
mTareFactorController = tareFactorController;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current value of the Factor
|
||||
*/
|
||||
private String getFactorValue() {
|
||||
return Integer.toString(mFactorValue);
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
@@ -78,15 +79,18 @@ public class TareFactorDialogFragment extends DialogFragment {
|
||||
.setPositiveButton(R.string.tare_dialog_confirm_button_title, (dialog, which) -> {
|
||||
|
||||
final String stringValue = mFactorValueView.getText().toString();
|
||||
mFactorEditedValue = mFactorValue;
|
||||
long newVal = mFactorValue;
|
||||
try {
|
||||
mFactorEditedValue = Integer.parseInt(stringValue);
|
||||
newVal = Long.parseLong(stringValue);
|
||||
if (mUnitSpinner.getSelectedItemPosition() == UNIT_IDX_ARC) {
|
||||
// Convert ARC to cake
|
||||
newVal *= CAKE_IN_ARC;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "Error converting '" + stringValue + "' to integer. Using "
|
||||
Log.e(TAG, "Error parsing '" + stringValue + "'. Using "
|
||||
+ mFactorValue + " instead", e);
|
||||
}
|
||||
mTareFactorController.updateValue(mFactorKey, mFactorEditedValue,
|
||||
mFactorPolicy);
|
||||
mTareFactorController.updateValue(mFactorKey, newVal, mFactorPolicy);
|
||||
})
|
||||
.setNegativeButton(android.R.string.cancel, (dialog, which) -> {
|
||||
// When the negative button is clicked do nothing
|
||||
@@ -102,12 +106,58 @@ public class TareFactorDialogFragment extends DialogFragment {
|
||||
private View createDialogView() {
|
||||
final LayoutInflater layoutInflater = (LayoutInflater) getActivity()
|
||||
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
View layout = layoutInflater.inflate(R.layout.dialog_edittext, null);
|
||||
View layout = layoutInflater.inflate(R.layout.dialog_edittext_dropdown, null);
|
||||
mFactorValueView = layout.findViewById(R.id.edittext);
|
||||
mFactorValueView.setInputType(InputType.TYPE_CLASS_NUMBER);
|
||||
mFactorValueView.setText(getFactorValue());
|
||||
Utils.setEditTextCursorPosition(mFactorValueView);
|
||||
|
||||
mUnitSpinner = layout.findViewById(R.id.spinner);
|
||||
final String[] units = getResources().getStringArray(R.array.tare_units);
|
||||
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>(
|
||||
getActivity(), android.R.layout.simple_spinner_item, units);
|
||||
mUnitSpinner.setAdapter(spinnerArrayAdapter);
|
||||
|
||||
final int unitIdx;
|
||||
if (mFactorValue % CAKE_IN_ARC == 0) {
|
||||
mFactorValueView.setText(String.format("%d", mFactorValue / CAKE_IN_ARC));
|
||||
unitIdx = UNIT_IDX_ARC;
|
||||
} else {
|
||||
mFactorValueView.setText(String.format("%d", mFactorValue));
|
||||
unitIdx = UNIT_IDX_CAKE;
|
||||
}
|
||||
mUnitSpinner.setSelection(unitIdx);
|
||||
mUnitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
||||
private int mSelectedPosition = unitIdx;
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
if (mSelectedPosition == position) {
|
||||
return;
|
||||
}
|
||||
mSelectedPosition = position;
|
||||
final String stringValue = mFactorValueView.getText().toString();
|
||||
|
||||
try {
|
||||
long newVal = Long.parseLong(stringValue);
|
||||
if (mUnitSpinner.getSelectedItemPosition() == UNIT_IDX_ARC) {
|
||||
// Convert cake to ARC
|
||||
newVal /= CAKE_IN_ARC;
|
||||
} else {
|
||||
// Convert ARC to cake
|
||||
newVal *= CAKE_IN_ARC;
|
||||
}
|
||||
mFactorValueView.setText(String.format("%d", newVal));
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "Error parsing '" + stringValue + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
Utils.setEditTextCursorPosition(mFactorValueView);
|
||||
return layout;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user