1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development.tare; 18 19 import static android.app.tare.EconomyManager.CAKE_IN_ARC; 20 21 import android.annotation.NonNull; 22 import android.app.AlertDialog; 23 import android.app.Dialog; 24 import android.app.DialogFragment; 25 import android.content.Context; 26 import android.os.Bundle; 27 import android.text.InputType; 28 import android.util.Log; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.widget.AdapterView; 32 import android.widget.ArrayAdapter; 33 import android.widget.EditText; 34 import android.widget.Spinner; 35 36 import com.android.settings.R; 37 import com.android.settings.Utils; 38 39 /** 40 * Dialog Fragment for changing tare factor values 41 */ 42 public class TareFactorDialogFragment extends DialogFragment { 43 private static final String TAG = "TareDialogFragment"; 44 45 // This follows the order in strings.xml:tare_units array. 46 private static final int UNIT_IDX_ARC = 0; 47 private static final int UNIT_IDX_CAKE = 1; 48 49 private final String mFactorKey; 50 private final String mFactorTitle; 51 private final long mFactorValue; 52 private final int mFactorPolicy; 53 private final TareFactorController mTareFactorController; 54 55 private EditText mFactorValueView; 56 private Spinner mUnitSpinner; 57 58 /** 59 * @param title the title that will show at the top of the Dialog for the Factor 60 * @param key the key of the Factor being initialized. 61 * @param currentValue the current value set for the Factor 62 */ TareFactorDialogFragment(@onNull String title, @NonNull String key, long currentValue, int factorPolicy, TareFactorController tareFactorController)63 public TareFactorDialogFragment(@NonNull String title, @NonNull String key, long currentValue, 64 int factorPolicy, TareFactorController tareFactorController) { 65 mFactorTitle = title; 66 mFactorKey = key; 67 mFactorValue = currentValue; 68 mFactorPolicy = factorPolicy; 69 mTareFactorController = tareFactorController; 70 } 71 72 @NonNull 73 @Override onCreateDialog(Bundle savedInstanceState)74 public Dialog onCreateDialog(Bundle savedInstanceState) { 75 AlertDialog.Builder builder = new AlertDialog.Builder( 76 getActivity()) 77 .setTitle(mFactorTitle) 78 .setView(createDialogView()) 79 .setPositiveButton(R.string.tare_dialog_confirm_button_title, (dialog, which) -> { 80 81 final String stringValue = mFactorValueView.getText().toString(); 82 long newVal = mFactorValue; 83 try { 84 newVal = Long.parseLong(stringValue); 85 if (mUnitSpinner.getSelectedItemPosition() == UNIT_IDX_ARC) { 86 // Convert ARC to cake 87 newVal *= CAKE_IN_ARC; 88 } 89 } catch (NumberFormatException e) { 90 Log.e(TAG, "Error parsing '" + stringValue + "'. Using " 91 + mFactorValue + " instead", e); 92 } 93 mTareFactorController.updateValue(mFactorKey, newVal, mFactorPolicy); 94 }) 95 .setNegativeButton(android.R.string.cancel, (dialog, which) -> { 96 // When the negative button is clicked do nothing 97 }); 98 99 return builder.create(); 100 } 101 102 /** 103 * Creates a view for the factor Dialog that currently 104 * is linked to the basic dialog_edittext.xml layout. 105 */ createDialogView()106 private View createDialogView() { 107 final LayoutInflater layoutInflater = (LayoutInflater) getActivity() 108 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 109 View layout = layoutInflater.inflate(R.layout.dialog_edittext_dropdown, null); 110 mFactorValueView = layout.findViewById(R.id.edittext); 111 mFactorValueView.setInputType(InputType.TYPE_CLASS_NUMBER); 112 113 mUnitSpinner = layout.findViewById(R.id.spinner); 114 final String[] units = getResources().getStringArray(R.array.tare_units); 115 ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<>( 116 getActivity(), android.R.layout.simple_spinner_item, units); 117 mUnitSpinner.setAdapter(spinnerArrayAdapter); 118 119 final int unitIdx; 120 if (mFactorValue % CAKE_IN_ARC == 0) { 121 mFactorValueView.setText(String.format("%d", mFactorValue / CAKE_IN_ARC)); 122 unitIdx = UNIT_IDX_ARC; 123 } else { 124 mFactorValueView.setText(String.format("%d", mFactorValue)); 125 unitIdx = UNIT_IDX_CAKE; 126 } 127 mUnitSpinner.setSelection(unitIdx); 128 mUnitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 129 private int mSelectedPosition = unitIdx; 130 131 @Override 132 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 133 if (mSelectedPosition == position) { 134 return; 135 } 136 mSelectedPosition = position; 137 final String stringValue = mFactorValueView.getText().toString(); 138 139 try { 140 long newVal = Long.parseLong(stringValue); 141 if (mUnitSpinner.getSelectedItemPosition() == UNIT_IDX_ARC) { 142 // Convert cake to ARC 143 newVal /= CAKE_IN_ARC; 144 } else { 145 // Convert ARC to cake 146 newVal *= CAKE_IN_ARC; 147 } 148 mFactorValueView.setText(String.format("%d", newVal)); 149 } catch (NumberFormatException e) { 150 Log.e(TAG, "Error parsing '" + stringValue + "'", e); 151 } 152 } 153 154 @Override 155 public void onNothingSelected(AdapterView<?> parent) { 156 157 } 158 }); 159 160 Utils.setEditTextCursorPosition(mFactorValueView); 161 return layout; 162 } 163 } 164