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
17package com.android.car.settings.datetime;
18
19import android.app.AlarmManager;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Bundle;
23import android.widget.TimePicker;
24
25import com.android.car.settings.common.CarSettingActivity;
26import com.android.car.settings.R;
27
28import java.util.Calendar;
29
30/**
31 * Sets the system time.
32 */
33public class TimePickerActivity extends CarSettingActivity {
34    private static final int MILLIS_IN_SECOND = 1000;
35
36    private TimePicker mTimePicker;
37
38    @Override
39    protected void onCreate(Bundle savedInstanceState) {
40        super.onCreate(savedInstanceState);
41
42        setContentView(R.layout.time_picker);
43
44        mTimePicker = (TimePicker) findViewById(R.id.time_picker);
45
46        findViewById(R.id.confirm).setOnClickListener(v -> {
47            Calendar c = Calendar.getInstance();
48
49            c.set(Calendar.HOUR_OF_DAY, mTimePicker.getHour());
50            c.set(Calendar.MINUTE, mTimePicker.getMinute());
51            c.set(Calendar.SECOND, 0);
52            c.set(Calendar.MILLISECOND, 0);
53            long when = Math.max(c.getTimeInMillis(), DatetimeSettingsActivity.MIN_DATE);
54            if (when / MILLIS_IN_SECOND < Integer.MAX_VALUE) {
55                ((AlarmManager) getSystemService(Context.ALARM_SERVICE)).setTime(when);
56                sendBroadcast(new Intent(Intent.ACTION_TIME_CHANGED));
57            }
58            finish();
59        });
60    }
61}
62