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