blob: 0b7e5054512a4adadd5bf9ba9660a03f8067f663 [file] [log] [blame]
David Dong3c2ce2f2021-11-15 18:19:23 -08001/*
2 * Copyright 2021 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 androidx.core.google.shortcuts.converters;
18
19import static androidx.annotation.RestrictTo.Scope.LIBRARY;
20
21import android.content.Context;
22import android.util.Log;
23
24import androidx.annotation.NonNull;
25import androidx.annotation.RestrictTo;
David Dong6960a622021-12-08 19:03:34 -080026import androidx.annotation.VisibleForTesting;
David Dong3c2ce2f2021-11-15 18:19:23 -080027import androidx.appsearch.app.GenericDocument;
28import androidx.appsearch.builtintypes.Timer;
29import androidx.core.google.shortcuts.utils.ConverterUtils;
30import androidx.core.util.Preconditions;
31
32import com.google.firebase.appindexing.Indexable;
33
David Dong3c2ce2f2021-11-15 18:19:23 -080034/**
35 * Convert for the {@link Timer} built-in type.
36 *
37 * @hide
38 */
39@RestrictTo(LIBRARY)
40public class TimerConverter implements AppSearchDocumentConverter {
41 private static final String TAG = "TimerConverter";
42
43 // Keys from the AppSearch document
44 private static final String NAME_KEY = "name";
45 private static final String DURATION_MILLIS_KEY = "durationMillis";
46 private static final String REMAINING_TIME_MILLIS_KEY = "remainingTimeMillis";
47 private static final String RINGTONE_KEY = "ringtone";
David Dong6960a622021-12-08 19:03:34 -080048 private static final String STATUS_KEY = "status";
David Dong3c2ce2f2021-11-15 18:19:23 -080049 private static final String VIBRATE_KEY = "vibrate";
David Dong6960a622021-12-08 19:03:34 -080050 private static final String START_TIME_MILLIS_KEY = "startTimeMillis";
51 private static final String START_TIME_MILLIS_IN_ELAPSED_REALTIME_KEY =
52 "startTimeMillisInElapsedRealtime";
David Dong3c2ce2f2021-11-15 18:19:23 -080053
54 // Keys for Indexables
55 private static final String MESSAGE_KEY = "message";
56 private static final String LENGTH_KEY = "length";
57 private static final String REMAINING_TIME_KEY = "remainingTime";
58 private static final String EXPIRE_TIME_KEY = "expireTime";
David Dong6960a622021-12-08 19:03:34 -080059 private static final String EXPIRE_TIME_CORRECTED_BY_START_TIME_IN_ELAPSED_REALTIME_KEY =
60 "expireTimeCorrectedByStartTimeInElapsedRealtime";
61 private static final String TIMER_STATUS_KEY = "timerStatus";
David Dong3c2ce2f2021-11-15 18:19:23 -080062
63 // Enums for TimerStatus
64 private static final String STARTED = "Started";
65 private static final String PAUSED = "Paused";
66 private static final String EXPIRED = "Expired";
67 private static final String MISSED = "Missed";
68 private static final String RESET = "Reset";
69 private static final String UNKNOWN = "Unknown";
70
David Dong6960a622021-12-08 19:03:34 -080071 private static final String TIMER_INDEXABLE_TYPE = "Timer";
72
David Dong6960a622021-12-08 19:03:34 -080073 private final TimeModel mTimeModel;
74
75 public TimerConverter() {
76 this(new TimeModel());
77 }
78
79 @VisibleForTesting
80 TimerConverter(TimeModel timeModel) {
81 mTimeModel = timeModel;
82 }
83
David Dong3c2ce2f2021-11-15 18:19:23 -080084 @Override
85 @NonNull
86 public Indexable.Builder convertGenericDocument(@NonNull Context context,
87 @NonNull GenericDocument timer) {
88 Preconditions.checkNotNull(context);
89 Preconditions.checkNotNull(timer);
90
91 Indexable.Builder indexableBuilder = ConverterUtils.buildBaseIndexableFromGenericDocument(
David Dong6960a622021-12-08 19:03:34 -080092 context, TIMER_INDEXABLE_TYPE, timer);
David Dong3c2ce2f2021-11-15 18:19:23 -080093
94 indexableBuilder
95 .put(MESSAGE_KEY, timer.getPropertyString(NAME_KEY))
96 .put(LENGTH_KEY, timer.getPropertyLong(DURATION_MILLIS_KEY))
97 .put(REMAINING_TIME_KEY, timer.getPropertyLong(REMAINING_TIME_MILLIS_KEY))
98 .put(RINGTONE_KEY, timer.getPropertyString(RINGTONE_KEY))
99 .put(VIBRATE_KEY, timer.getPropertyBoolean(VIBRATE_KEY));
100
David Dong6960a622021-12-08 19:03:34 -0800101 int timerStatus = (int) timer.getPropertyLong(STATUS_KEY);
David Dong3c2ce2f2021-11-15 18:19:23 -0800102 switch (timerStatus) {
103 case Timer.STATUS_UNKNOWN:
104 indexableBuilder.put(TIMER_STATUS_KEY, UNKNOWN);
105 break;
106 case Timer.STATUS_STARTED:
107 indexableBuilder.put(TIMER_STATUS_KEY, STARTED);
108 break;
109 case Timer.STATUS_PAUSED:
110 indexableBuilder.put(TIMER_STATUS_KEY, PAUSED);
111 break;
112 case Timer.STATUS_EXPIRED:
113 indexableBuilder.put(TIMER_STATUS_KEY, EXPIRED);
114 break;
115 case Timer.STATUS_MISSED:
116 indexableBuilder.put(TIMER_STATUS_KEY, MISSED);
117 break;
118 case Timer.STATUS_RESET:
119 indexableBuilder.put(TIMER_STATUS_KEY, RESET);
120 break;
121 default:
122 indexableBuilder.put(TIMER_STATUS_KEY, UNKNOWN);
David Dong6960a622021-12-08 19:03:34 -0800123 Log.w(TAG, "Invalid time status: " + timerStatus + ", defaulting to "
David Dong3c2ce2f2021-11-15 18:19:23 -0800124 + "Timer.STATUS_UNKNOWN");
125 }
126
David Dong6960a622021-12-08 19:03:34 -0800127 if (timerStatus == Timer.STATUS_STARTED) {
128 long startTime = timer.getPropertyLong(START_TIME_MILLIS_KEY);
129 long remainingTime = timer.getPropertyLong(REMAINING_TIME_MILLIS_KEY);
130
131 long expireTime = remainingTime + startTime;
David Dong105f1122021-12-21 16:59:17 -0800132 indexableBuilder.put(EXPIRE_TIME_KEY,
133 ConverterUtils.convertTimestampToISO8601Format(expireTime, null));
David Dong6960a622021-12-08 19:03:34 -0800134
135 long startTimeInElapsedRealtime =
136 timer.getPropertyLong(START_TIME_MILLIS_IN_ELAPSED_REALTIME_KEY);
137 if (startTimeInElapsedRealtime >= 0) {
138 // If startTime in elapsed realtime is set, use that to calculate expire time as
139 // well.
140 long elapsedTime = mTimeModel.getSystemClockElapsedRealtime()
141 - startTimeInElapsedRealtime;
142 long expireTimeFromElapsedRealtime =
143 mTimeModel.getSystemCurrentTimeMillis() - elapsedTime + remainingTime;
144 indexableBuilder.put(EXPIRE_TIME_CORRECTED_BY_START_TIME_IN_ELAPSED_REALTIME_KEY,
David Dong105f1122021-12-21 16:59:17 -0800145 ConverterUtils.convertTimestampToISO8601Format(
146 expireTimeFromElapsedRealtime, null));
David Dong6960a622021-12-08 19:03:34 -0800147 }
David Dong3c2ce2f2021-11-15 18:19:23 -0800148 }
149 return indexableBuilder;
150 }
David Dong3c2ce2f2021-11-15 18:19:23 -0800151}