David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.core.google.shortcuts.converters; |
| 18 | |
| 19 | import static androidx.annotation.RestrictTo.Scope.LIBRARY; |
| 20 | |
| 21 | import android.content.Context; |
| 22 | import android.util.Log; |
| 23 | |
| 24 | import androidx.annotation.NonNull; |
| 25 | import androidx.annotation.RestrictTo; |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 26 | import androidx.annotation.VisibleForTesting; |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 27 | import androidx.appsearch.app.GenericDocument; |
| 28 | import androidx.appsearch.builtintypes.Timer; |
| 29 | import androidx.core.google.shortcuts.utils.ConverterUtils; |
| 30 | import androidx.core.util.Preconditions; |
| 31 | |
| 32 | import com.google.firebase.appindexing.Indexable; |
| 33 | |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 34 | /** |
| 35 | * Convert for the {@link Timer} built-in type. |
| 36 | * |
| 37 | * @hide |
| 38 | */ |
| 39 | @RestrictTo(LIBRARY) |
| 40 | public 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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 48 | private static final String STATUS_KEY = "status"; |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 49 | private static final String VIBRATE_KEY = "vibrate"; |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 50 | private static final String START_TIME_MILLIS_KEY = "startTimeMillis"; |
| 51 | private static final String START_TIME_MILLIS_IN_ELAPSED_REALTIME_KEY = |
| 52 | "startTimeMillisInElapsedRealtime"; |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 53 | |
| 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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 59 | 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 Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 62 | |
| 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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 71 | private static final String TIMER_INDEXABLE_TYPE = "Timer"; |
| 72 | |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 73 | 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 Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 84 | @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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 92 | context, TIMER_INDEXABLE_TYPE, timer); |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 93 | |
| 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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 101 | int timerStatus = (int) timer.getPropertyLong(STATUS_KEY); |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 102 | 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 Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 123 | Log.w(TAG, "Invalid time status: " + timerStatus + ", defaulting to " |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 124 | + "Timer.STATUS_UNKNOWN"); |
| 125 | } |
| 126 | |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 127 | 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 Dong | 105f112 | 2021-12-21 16:59:17 -0800 | [diff] [blame] | 132 | indexableBuilder.put(EXPIRE_TIME_KEY, |
| 133 | ConverterUtils.convertTimestampToISO8601Format(expireTime, null)); |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 134 | |
| 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 Dong | 105f112 | 2021-12-21 16:59:17 -0800 | [diff] [blame] | 145 | ConverterUtils.convertTimestampToISO8601Format( |
| 146 | expireTimeFromElapsedRealtime, null)); |
David Dong | 6960a62 | 2021-12-08 19:03:34 -0800 | [diff] [blame] | 147 | } |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 148 | } |
| 149 | return indexableBuilder; |
| 150 | } |
David Dong | 3c2ce2f | 2021-11-15 18:19:23 -0800 | [diff] [blame] | 151 | } |