1 /* 2 * Copyright (C) 2022 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.settings.core.instrumentation; 17 18 import android.content.Context; 19 import android.content.SharedPreferences; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.VisibleForTesting; 24 25 import java.util.Optional; 26 27 /** Class for calculating the elapsed time of the metrics. */ 28 public class ElapsedTimeUtils { 29 private static final String TAG = "ElapsedTimeUtils"; 30 private static final String ELAPSED_TIME_PREF_FILENAME = "elapsed_time_info"; 31 private static final String SUW_FINISHED_TIME_MS = "suw_finished_time_ms"; 32 private static Optional<Long> sSuwFinishedTimeStamp = Optional.empty();; 33 @VisibleForTesting 34 static final long DEFAULT_SETUP_TIME = -1L; 35 36 /** 37 * Keeps the timestamp after SetupWizard finished. 38 * 39 * @param timestamp The timestamp of the SetupWizard finished. 40 */ storeSuwFinishedTimestamp(@onNull Context context, long timestamp)41 public static void storeSuwFinishedTimestamp(@NonNull Context context, long timestamp) { 42 final SharedPreferences sharedPrefs = getSharedPrefs(context); 43 if (!sharedPrefs.contains(SUW_FINISHED_TIME_MS)) { 44 sSuwFinishedTimeStamp = Optional.of(timestamp); 45 sharedPrefs.edit().putLong(SUW_FINISHED_TIME_MS, timestamp).apply(); 46 } 47 } 48 49 /** 50 * Retrieves the preference value of SUW_FINISHED_TIME_MS and 51 * assigns to sSuwFinishedTimeStamp. 52 */ assignSuwFinishedTimeStamp(@onNull Context context)53 public static void assignSuwFinishedTimeStamp(@NonNull Context context) { 54 final SharedPreferences sharedPrefs = getSharedPrefs(context); 55 if (sharedPrefs.contains(SUW_FINISHED_TIME_MS) && !sSuwFinishedTimeStamp.isPresent()) { 56 sSuwFinishedTimeStamp = Optional.of(getSuwFinishedTimestamp(context)); 57 } 58 } 59 60 /** 61 * Gets the elapsed time by (timestamp - time of SetupWizard finished). 62 * @param timestamp The timestamp of the current time. 63 * @return The elapsed time after device setup finished. 64 */ getElapsedTime(long timestamp)65 public static long getElapsedTime(long timestamp) { 66 if (!sSuwFinishedTimeStamp.isPresent()) { 67 Log.w(TAG, "getElapsedTime: sSuwFinishedTimeStamp is null"); 68 return DEFAULT_SETUP_TIME; 69 } 70 if (sSuwFinishedTimeStamp.get() != DEFAULT_SETUP_TIME) { 71 final long elapsedTime = timestamp - sSuwFinishedTimeStamp.get(); 72 return elapsedTime > 0L ? elapsedTime : DEFAULT_SETUP_TIME; 73 } 74 return DEFAULT_SETUP_TIME; 75 } 76 77 @VisibleForTesting getSuwFinishedTimestamp(Context context)78 static long getSuwFinishedTimestamp(Context context) { 79 return getSharedPrefs(context).getLong(SUW_FINISHED_TIME_MS, DEFAULT_SETUP_TIME); 80 } 81 getSharedPrefs(Context context)82 private static SharedPreferences getSharedPrefs(Context context) { 83 return context 84 .getApplicationContext() 85 .getSharedPreferences(ELAPSED_TIME_PREF_FILENAME, Context.MODE_PRIVATE); 86 } 87 ElapsedTimeUtils()88 private ElapsedTimeUtils() {} 89 } 90