1 /* 2 * Copyright (C) 2023 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.permissioncontroller.permission.ui.wear 18 19 import android.content.Context 20 import android.text.format.DateFormat 21 import androidx.annotation.IntDef 22 import com.android.permissioncontroller.R 23 import java.time.ZonedDateTime 24 import java.time.temporal.ChronoUnit 25 import java.util.Locale 26 27 object WearUtils { 28 @Retention(AnnotationRetention.SOURCE) 29 @IntDef(value = [LAST_24H_TODAY, LAST_24H_YESTERDAY, LAST_7D, NOT_IN_LAST_7D]) 30 annotation class AppPermsLastAccessType 31 32 const val LAST_24H_TODAY = 1 33 const val LAST_24H_YESTERDAY = 2 34 const val LAST_7D = 3 35 const val NOT_IN_LAST_7D = 4 36 37 /** Get the preference summary in app permission groups and permission apps screens for Wear. */ 38 @JvmStatic getPreferenceSummarynull39 fun getPreferenceSummary(context: Context, lastAccessTime: Long?): String { 40 val summaryTimestamp = getPermissionLastAccessSummaryTimestamp(lastAccessTime, context) 41 val res = context.resources 42 return when (summaryTimestamp.second) { 43 LAST_24H_TODAY -> 44 res.getString(R.string.wear_app_perms_24h_access, summaryTimestamp.first) 45 LAST_24H_YESTERDAY -> 46 res.getString(R.string.wear_app_perms_24h_access_yest, summaryTimestamp.first) 47 LAST_7D -> 48 res.getString( 49 R.string.wear_app_perms_7d_access, 50 summaryTimestamp.third, 51 summaryTimestamp.first 52 ) 53 else -> "" 54 } 55 } 56 57 @JvmStatic getPermissionLastAccessSummaryTimestampnull58 private fun getPermissionLastAccessSummaryTimestamp( 59 lastAccessTime: Long?, 60 context: Context 61 ): Triple<String, Int, String> { 62 val midnightToday = 63 (ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).toEpochSecond() * 1000L) 64 val midnightYesterday = 65 ZonedDateTime.now().minusDays(1).truncatedTo(ChronoUnit.DAYS).toEpochSecond() * 1000L 66 val isLastAccessToday = (lastAccessTime != null && midnightToday <= lastAccessTime) 67 val isLastAccessTodayOrYesterday = 68 (lastAccessTime != null && midnightYesterday <= lastAccessTime) 69 var lastAccessTimeFormatted = "" 70 var lastAccessDateFormatted = "" 71 @AppPermsLastAccessType var lastAccessType = NOT_IN_LAST_7D 72 if (lastAccessTime != null) { 73 lastAccessTimeFormatted = DateFormat.getTimeFormat(context).format(lastAccessTime) 74 lastAccessDateFormatted = DateFormat.getDateFormat(context).format(lastAccessTime) 75 lastAccessType = 76 if (isLastAccessToday) LAST_24H_TODAY 77 else if (isLastAccessTodayOrYesterday) LAST_24H_YESTERDAY else LAST_7D 78 } 79 return Triple(lastAccessTimeFormatted, lastAccessType, lastAccessDateFormatted) 80 } 81 <lambda>null82 fun String.capitalize(): String = replaceFirstChar { 83 if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() 84 } 85 } 86