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