1 /* 2 * Copyright (C) 2019 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.car.settings.datausage; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.text.BidiFormatter; 22 import android.text.TextDirectionHeuristics; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.ListPreference; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.FragmentController; 29 30 import java.util.TimeZone; 31 import java.util.stream.IntStream; 32 33 /** 34 * ListPreference used to pick the date on which the data warning/limit cycle should end. 35 */ 36 public class CycleResetDayOfMonthPickerPreferenceController extends 37 DataWarningAndLimitBasePreferenceController<ListPreference> { 38 39 @VisibleForTesting 40 static final int MIN_DAY = 1; 41 @VisibleForTesting 42 static final int MAX_DAY = 31; 43 CycleResetDayOfMonthPickerPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44 public CycleResetDayOfMonthPickerPreferenceController(Context context, String preferenceKey, 45 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 46 super(context, preferenceKey, fragmentController, uxRestrictions); 47 } 48 49 @Override getPreferenceType()50 protected Class<ListPreference> getPreferenceType() { 51 return ListPreference.class; 52 } 53 54 @Override onCreateInternal()55 protected void onCreateInternal() { 56 CharSequence[] list = IntStream.rangeClosed(MIN_DAY, MAX_DAY) 57 .mapToObj(i-> String.valueOf(i)).toArray(CharSequence[]::new); 58 getPreference().setEntryValues(list); 59 getPreference().setEntries(list); 60 } 61 62 @Override updateState(ListPreference preference)63 protected void updateState(ListPreference preference) { 64 String prefix = getContext().getString(R.string.cycle_reset_day_of_month_picker_subtitle); 65 int cycleDayOfMonth = getNetworkPolicyEditor().getPolicyCycleDay(getNetworkTemplate()); 66 if (cycleDayOfMonth >= MIN_DAY && cycleDayOfMonth <= MAX_DAY) { 67 String cycleDayOfMonthString = String.valueOf(cycleDayOfMonth); 68 getPreference().setValue(cycleDayOfMonthString); 69 getPreference().setSummary(BidiFormatter.getInstance().unicodeWrap( 70 String.join(" ", prefix, cycleDayOfMonthString), 71 TextDirectionHeuristics.LOCALE)); 72 } 73 } 74 75 @Override handlePreferenceChanged(ListPreference preference, Object newValue)76 protected boolean handlePreferenceChanged(ListPreference preference, Object newValue) { 77 onDayOfMonthPicked(Integer.valueOf((String) newValue)); 78 return true; 79 } 80 onDayOfMonthPicked(int dayOfMonth)81 private void onDayOfMonthPicked(int dayOfMonth) { 82 getNetworkPolicyEditor().setPolicyCycleDay(getNetworkTemplate(), dayOfMonth, 83 TimeZone.getDefault().getID()); 84 } 85 } 86