1 /* 2 * Copyright (C) 2017 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.datetime; 18 19 import android.app.AlarmManager; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.widget.Button; 24 import android.widget.TimePicker; 25 26 import com.android.car.settings.R; 27 import com.android.car.settings.common.BaseFragment; 28 29 import java.util.Calendar; 30 31 /** 32 * Sets the system time. 33 */ 34 public class TimePickerFragment extends BaseFragment { 35 private static final int MILLIS_IN_SECOND = 1000; 36 37 private TimePicker mTimePicker; 38 39 public static TimePickerFragment getInstance() { 40 TimePickerFragment timePickerFragment = new TimePickerFragment(); 41 Bundle bundle = BaseFragment.getBundle(); 42 bundle.putInt(EXTRA_TITLE_ID, R.string.time_picker_title); 43 bundle.putInt(EXTRA_LAYOUT, R.layout.time_picker); 44 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button); 45 timePickerFragment.setArguments(bundle); 46 return timePickerFragment; 47 } 48 49 @Override 50 public void onActivityCreated(Bundle savedInstanceState) { 51 super.onActivityCreated(savedInstanceState); 52 53 mTimePicker = (TimePicker) getView().findViewById(R.id.time_picker); 54 55 Button button = (Button) getActivity().findViewById(R.id.action_button1); 56 button.setText(android.R.string.ok); 57 button.setOnClickListener(v -> { 58 Calendar c = Calendar.getInstance(); 59 60 c.set(Calendar.HOUR_OF_DAY, mTimePicker.getHour()); 61 c.set(Calendar.MINUTE, mTimePicker.getMinute()); 62 c.set(Calendar.SECOND, 0); 63 c.set(Calendar.MILLISECOND, 0); 64 long when = Math.max(c.getTimeInMillis(), DatetimeSettingsFragment.MIN_DATE); 65 if (when / MILLIS_IN_SECOND < Integer.MAX_VALUE) { 66 ((AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE)).setTime(when); 67 getContext().sendBroadcast(new Intent(Intent.ACTION_TIME_CHANGED)); 68 } 69 getFragmentController().goBack(); 70 }); 71 } 72 } 73