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.timedetector.ManualTimeSuggestion; 20 import android.app.timedetector.TimeDetector; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.text.format.DateFormat; 24 import android.widget.TimePicker; 25 26 import androidx.annotation.LayoutRes; 27 import androidx.annotation.StringRes; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.BaseFragment; 31 import com.android.car.ui.toolbar.MenuItem; 32 33 import java.util.Calendar; 34 import java.util.Collections; 35 import java.util.List; 36 37 /** 38 * Sets the system time. 39 */ 40 public class TimePickerFragment extends BaseFragment { 41 private static final int MILLIS_IN_SECOND = 1000; 42 43 private TimePicker mTimePicker; 44 private MenuItem mOkButton; 45 46 @Override getToolbarMenuItems()47 public List<MenuItem> getToolbarMenuItems() { 48 return Collections.singletonList(mOkButton); 49 } 50 51 @Override onCreate(Bundle savedInstanceState)52 public void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 55 mOkButton = new MenuItem.Builder(getContext()) 56 .setTitle(android.R.string.ok) 57 .setOnClickListener(i -> { 58 Calendar c = Calendar.getInstance(); 59 c.set(Calendar.HOUR_OF_DAY, mTimePicker.getHour()); 60 c.set(Calendar.MINUTE, mTimePicker.getMinute()); 61 c.set(Calendar.SECOND, 0); 62 c.set(Calendar.MILLISECOND, 0); 63 long when = Math.max(c.getTimeInMillis(), DatetimeSettingsFragment.MIN_DATE); 64 if (when / MILLIS_IN_SECOND < Integer.MAX_VALUE) { 65 TimeDetector timeDetector = 66 getContext().getSystemService(TimeDetector.class); 67 ManualTimeSuggestion manualTimeSuggestion = 68 TimeDetector.createManualTimeSuggestion(when, "Settings: Set time"); 69 timeDetector.suggestManualTime(manualTimeSuggestion); 70 getContext().sendBroadcast(new Intent(Intent.ACTION_TIME_CHANGED)); 71 } 72 getFragmentController().goBack(); 73 }) 74 .build(); 75 } 76 77 @Override 78 @LayoutRes getLayoutId()79 protected int getLayoutId() { 80 return R.layout.time_picker; 81 } 82 83 @Override 84 @StringRes getTitleId()85 protected int getTitleId() { 86 return R.string.time_picker_title; 87 } 88 89 @Override onActivityCreated(Bundle savedInstanceState)90 public void onActivityCreated(Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 93 mTimePicker = (TimePicker) getView().findViewById(R.id.time_picker); 94 mTimePicker.setIs24HourView(is24Hour()); 95 } 96 is24Hour()97 private boolean is24Hour() { 98 return DateFormat.is24HourFormat(getContext()); 99 } 100 } 101