1 /* 2 * Copyright (C) 2016 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 package com.google.android.car.kitchensink.setting; 17 18 import android.app.TimePickerDialog; 19 import android.car.CarApiUtil; 20 import android.car.settings.GarageModeSettingsObserver; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.preference.Preference; 25 import android.preference.PreferenceFragment; 26 import android.preference.PreferenceScreen; 27 import android.provider.Settings; 28 import android.text.format.DateFormat; 29 import android.util.Log; 30 import android.widget.TimePicker; 31 32 import com.google.android.car.kitchensink.R; 33 34 import java.sql.Time; 35 36 import static android.car.settings.CarSettings.Global.KEY_GARAGE_MODE_MAINTENANCE_WINDOW; 37 import static android.car.settings.CarSettings.Global.KEY_GARAGE_MODE_ENABLED; 38 import static android.car.settings.CarSettings.Global.KEY_GARAGE_MODE_WAKE_UP_TIME; 39 import static android.car.settings.GarageModeSettingsObserver.GARAGE_MODE_ENABLED_URI; 40 import static android.car.settings.GarageModeSettingsObserver.GARAGE_MODE_WAKE_UP_TIME_URI; 41 import static android.car.settings.GarageModeSettingsObserver.GARAGE_MODE_MAINTENANCE_WINDOW_URI; 42 43 public class GarageModeSettingsFragment extends PreferenceFragment implements 44 TimePickerDialog.OnTimeSetListener, Preference.OnPreferenceChangeListener { 45 46 private static final String TAG = "GarageModeSettings"; 47 private Preference mTimePreference; 48 private Preference mGarageSwitchPreference; 49 private Preference mGarageLimitPreference; 50 51 private int mGarageTimeHour; 52 private int mGarageTimeMin; 53 54 private GarageModeSettingsObserver mContentObserver; 55 56 @Override 57 public void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 addPreferencesFromResource(R.xml.garage_mode_prefs); 60 mTimePreference = findPreference(KEY_GARAGE_MODE_WAKE_UP_TIME); 61 mTimePreference.setOnPreferenceChangeListener(this); 62 mGarageSwitchPreference = findPreference(KEY_GARAGE_MODE_ENABLED); 63 mGarageSwitchPreference.setOnPreferenceChangeListener(this); 64 mGarageLimitPreference = findPreference(KEY_GARAGE_MODE_MAINTENANCE_WINDOW); 65 mGarageLimitPreference.setOnPreferenceChangeListener(this); 66 refreshUI(KEY_GARAGE_MODE_ENABLED, KEY_GARAGE_MODE_WAKE_UP_TIME, KEY_GARAGE_MODE_MAINTENANCE_WINDOW); 67 mContentObserver = new GarageModeSettingsObserver(getContext(), new Handler()) { 68 @Override 69 public void onChange(boolean selfChange, Uri uri) { 70 onSettingsChangedInternal(uri); 71 } 72 }; 73 } 74 75 @Override 76 public void onResume() { 77 super.onResume(); 78 mContentObserver.register(); 79 } 80 81 @Override 82 public void onPause() { 83 super.onPause(); 84 mContentObserver.unregister(); 85 } 86 87 private void refreshUI(String... keys) { 88 for (String key : keys) { 89 try { 90 switch (key) { 91 case KEY_GARAGE_MODE_ENABLED: 92 mGarageSwitchPreference.setDefaultValue( 93 Settings.Global.getInt(getContext().getContentResolver(), key) 94 == 1); 95 break; 96 case KEY_GARAGE_MODE_WAKE_UP_TIME: 97 int time[] = CarApiUtil.decodeGarageTimeSetting( 98 Settings.Global.getString(getContext().getContentResolver(), 99 KEY_GARAGE_MODE_WAKE_UP_TIME)); 100 mTimePreference.setSummary( 101 DateFormat.getTimeFormat(getContext()).format( 102 new Time(time[0], time[1], 0))); 103 mGarageTimeHour = time[0]; 104 mGarageTimeMin = time[1]; 105 break; 106 case KEY_GARAGE_MODE_MAINTENANCE_WINDOW: 107 int limitMinutes = Settings.Global.getInt(getContext().getContentResolver(), 108 key) / 60 / 1000; 109 mGarageLimitPreference.setSummary( 110 getString(R.string.garage_time_limit_summary, limitMinutes)); 111 break; 112 } 113 } catch (Settings.SettingNotFoundException e) { 114 Log.e(TAG, "Settings not found " + key); 115 } 116 } 117 } 118 119 @Override 120 public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { 121 if (preference == mTimePreference) { 122 TimePickerDialogFragment dialog = 123 TimePickerDialogFragment.newInstance(mGarageTimeHour, mGarageTimeMin); 124 dialog.setTimeSetListener(this); 125 dialog.show(getFragmentManager(), "time"); 126 return true; 127 } 128 return super.onPreferenceTreeClick(screen, preference); 129 } 130 131 @Override 132 public boolean onPreferenceChange(Preference preference, Object newValue) { 133 if (preference == mGarageSwitchPreference) { 134 updateGarageSwitch((boolean) newValue); 135 refreshUI(KEY_GARAGE_MODE_ENABLED); 136 return true; 137 } else if (preference == mGarageLimitPreference) { 138 updateGarageTimeLimit(Integer.valueOf((String) newValue)); 139 refreshUI(KEY_GARAGE_MODE_MAINTENANCE_WINDOW); 140 return true; 141 } 142 143 return false; 144 } 145 146 private void updateGarageSwitch(boolean newValue) { 147 Settings.Global.putInt(getContext().getContentResolver(), 148 KEY_GARAGE_MODE_ENABLED, newValue ? 1 : 0); 149 } 150 151 private void updateGarageTimeLimit(int newValue) { 152 Settings.Global.putInt(getContext().getContentResolver(), 153 KEY_GARAGE_MODE_MAINTENANCE_WINDOW, newValue * 60 * 1000); 154 } 155 156 private void updateGarageTime(String time) { 157 Settings.Global.putString(getContext().getContentResolver(), 158 KEY_GARAGE_MODE_WAKE_UP_TIME, time); 159 } 160 161 @Override 162 public void onTimeSet(TimePicker timePicker, int hour, int minute) { 163 updateGarageTime(CarApiUtil.encodeGarageTimeSetting(hour, minute)); 164 refreshUI(KEY_GARAGE_MODE_WAKE_UP_TIME); 165 } 166 167 private void onSettingsChangedInternal(Uri uri) { 168 Log.d(TAG, "Content Observer onChange: " + uri); 169 if (uri.equals(GARAGE_MODE_ENABLED_URI)) { 170 refreshUI(KEY_GARAGE_MODE_ENABLED); 171 } else if (uri.equals(GARAGE_MODE_WAKE_UP_TIME_URI)) { 172 refreshUI(KEY_GARAGE_MODE_WAKE_UP_TIME); 173 } else if (uri.equals(GARAGE_MODE_MAINTENANCE_WINDOW_URI)) { 174 refreshUI(KEY_GARAGE_MODE_MAINTENANCE_WINDOW); 175 } 176 } 177 }