Loading src/com/android/settings/SettingsApplication.java +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import android.util.FeatureFlagUtils; import androidx.window.embedding.SplitController; import com.android.settings.activityembedding.ActivityEmbeddingRulesController; import com.android.settings.core.instrumentation.ElapsedTimeUtils; import com.android.settings.homepage.SettingsHomepageActivity; import com.android.settings.spa.SettingsSpaEnvironment; import com.android.settingslib.applications.AppIconCacheManager; Loading @@ -42,6 +43,7 @@ public class SettingsApplication extends Application { @Override public void onCreate() { super.onCreate(); ElapsedTimeUtils.assignSuwFinishedTimeStamp(this.getApplicationContext()); // Set Spa environment. setSpaEnvironment(); Loading src/com/android/settings/core/instrumentation/ElapsedTimeUtils.java 0 → 100644 +89 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.core.instrumentation; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import java.util.Optional; /** Class for calculating the elapsed time of the metrics. */ public class ElapsedTimeUtils { private static final String TAG = "ElapsedTimeUtils"; private static final String ELAPSED_TIME_PREF_FILENAME = "elapsed_time_info"; private static final String SUW_FINISHED_TIME_MS = "suw_finished_time_ms"; private static Optional<Long> sSuwFinishedTimeStamp = Optional.empty();; @VisibleForTesting static final long DEFAULT_SETUP_TIME = -1L; /** * Keeps the timestamp after SetupWizard finished. * * @param timestamp The timestamp of the SetupWizard finished. */ public static void storeSuwFinishedTimestamp(@NonNull Context context, long timestamp) { final SharedPreferences sharedPrefs = getSharedPrefs(context); if (!sharedPrefs.contains(SUW_FINISHED_TIME_MS)) { sSuwFinishedTimeStamp = Optional.of(timestamp); sharedPrefs.edit().putLong(SUW_FINISHED_TIME_MS, timestamp).apply(); } } /** * Retrieves the preference value of SUW_FINISHED_TIME_MS and * assigns to sSuwFinishedTimeStamp. */ public static void assignSuwFinishedTimeStamp(@NonNull Context context) { final SharedPreferences sharedPrefs = getSharedPrefs(context); if (sharedPrefs.contains(SUW_FINISHED_TIME_MS) && !sSuwFinishedTimeStamp.isPresent()) { sSuwFinishedTimeStamp = Optional.of(getSuwFinishedTimestamp(context)); } } /** * Gets the elapsed time by (timestamp - time of SetupWizard finished). * @param timestamp The timestamp of the current time. * @return The elapsed time after device setup finished. */ public static long getElapsedTime(long timestamp) { if (!sSuwFinishedTimeStamp.isPresent()) { Log.w(TAG, "getElapsedTime: sSuwFinishedTimeStamp is null"); return DEFAULT_SETUP_TIME; } if (sSuwFinishedTimeStamp.get() != DEFAULT_SETUP_TIME) { final long elapsedTime = timestamp - sSuwFinishedTimeStamp.get(); return elapsedTime > 0L ? elapsedTime : DEFAULT_SETUP_TIME; } return DEFAULT_SETUP_TIME; } @VisibleForTesting static long getSuwFinishedTimestamp(Context context) { return getSharedPrefs(context).getLong(SUW_FINISHED_TIME_MS, DEFAULT_SETUP_TIME); } private static SharedPreferences getSharedPrefs(Context context) { return context .getApplicationContext() .getSharedPreferences(ELAPSED_TIME_PREF_FILENAME, Context.MODE_PRIVATE); } private ElapsedTimeUtils() {} } src/com/android/settings/core/instrumentation/StatsLogWriter.java +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.PAGE_VISIBLE /* action */, pageId, /* target pageId */ "" /* changedPreferenceKey */, latency /* changedPreferenceIntValue */); latency /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -41,7 +42,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.PAGE_HIDE /* action */, pageId, "" /* changedPreferenceKey */, visibleTime /* changedPreferenceIntValue */); visibleTime /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -51,7 +53,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.ACTION_SETTINGS_TILE_CLICK /* action */, SettingsEnums.PAGE_UNKNOWN /* pageId */, key /* changedPreferenceKey */, 0 /* changedPreferenceIntValue */); 0 /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -61,7 +64,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE /* action */, SettingsEnums.PAGE_UNKNOWN /* pageId */, key /* changedPreferenceKey */, value /* changedPreferenceIntValue */); value /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading Loading @@ -107,6 +111,7 @@ public class StatsLogWriter implements LogWriter { action, pageId, key, value); value, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } } src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java +4 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; import com.android.settings.core.instrumentation.ElapsedTimeUtils; import java.time.Duration; /** Receives broadcasts to start or stop the periodic fetching job. */ Loading Loading @@ -77,6 +79,8 @@ public final class BootBroadcastReceiver extends BroadcastReceiver { recheckIntent.setClass(context, BootBroadcastReceiver.class); mHandler.postDelayed(() -> context.sendBroadcast(recheckIntent), RESCHEDULE_FOR_BOOT_ACTION); } else if (ACTION_SETUP_WIZARD_FINISHED.equals(action)) { ElapsedTimeUtils.storeSuwFinishedTimestamp(context, System.currentTimeMillis()); } } Loading tests/robotests/src/com/android/settings/core/instrumentation/ElapsedTimeUtilsTest.java 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.core.instrumentation; import static com.google.common.truth.Truth.assertThat; import android.content.Context; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class ElapsedTimeUtilsTest { private Context mContext; @Before public void setUp() { mContext = RuntimeEnvironment.application; } @Test public void storeSuwFinishedTimestamp_firstTimeAccess_shouldStoreTimestamp() { final long timestamp = 1000000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, timestamp); final long result = ElapsedTimeUtils.getSuwFinishedTimestamp(mContext); assertThat(result).isEqualTo(timestamp); } @Test public void storeSuwFinishedTimestamp_NotFirstTimeAccess_shouldNotStoreTimestamp() { final long timestamp = 1000000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, timestamp); ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, 2000000L); final long result = ElapsedTimeUtils.getSuwFinishedTimestamp(mContext); assertThat(result).isEqualTo(timestamp); } @Test public void getElapsedTime_valueInPrefs_positiveElapsedTime_returnElapsedTime() { final long suwTime = 1000L; final long actionTime = 2000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, suwTime); final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(actionTime - suwTime); } @Test public void getElapsedTime_valueInPrefs_negativeElapsedTime_returnDefault() { final long suwTime = 2000L; final long actionTime = 1000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, suwTime); final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(ElapsedTimeUtils.DEFAULT_SETUP_TIME); } @Test public void getElapsedTime_noValueInPrefs_returnDefault() { final long actionTime = 1000L; final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(ElapsedTimeUtils.DEFAULT_SETUP_TIME); } } Loading
src/com/android/settings/SettingsApplication.java +2 −0 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import android.util.FeatureFlagUtils; import androidx.window.embedding.SplitController; import com.android.settings.activityembedding.ActivityEmbeddingRulesController; import com.android.settings.core.instrumentation.ElapsedTimeUtils; import com.android.settings.homepage.SettingsHomepageActivity; import com.android.settings.spa.SettingsSpaEnvironment; import com.android.settingslib.applications.AppIconCacheManager; Loading @@ -42,6 +43,7 @@ public class SettingsApplication extends Application { @Override public void onCreate() { super.onCreate(); ElapsedTimeUtils.assignSuwFinishedTimeStamp(this.getApplicationContext()); // Set Spa environment. setSpaEnvironment(); Loading
src/com/android/settings/core/instrumentation/ElapsedTimeUtils.java 0 → 100644 +89 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.core.instrumentation; import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.VisibleForTesting; import java.util.Optional; /** Class for calculating the elapsed time of the metrics. */ public class ElapsedTimeUtils { private static final String TAG = "ElapsedTimeUtils"; private static final String ELAPSED_TIME_PREF_FILENAME = "elapsed_time_info"; private static final String SUW_FINISHED_TIME_MS = "suw_finished_time_ms"; private static Optional<Long> sSuwFinishedTimeStamp = Optional.empty();; @VisibleForTesting static final long DEFAULT_SETUP_TIME = -1L; /** * Keeps the timestamp after SetupWizard finished. * * @param timestamp The timestamp of the SetupWizard finished. */ public static void storeSuwFinishedTimestamp(@NonNull Context context, long timestamp) { final SharedPreferences sharedPrefs = getSharedPrefs(context); if (!sharedPrefs.contains(SUW_FINISHED_TIME_MS)) { sSuwFinishedTimeStamp = Optional.of(timestamp); sharedPrefs.edit().putLong(SUW_FINISHED_TIME_MS, timestamp).apply(); } } /** * Retrieves the preference value of SUW_FINISHED_TIME_MS and * assigns to sSuwFinishedTimeStamp. */ public static void assignSuwFinishedTimeStamp(@NonNull Context context) { final SharedPreferences sharedPrefs = getSharedPrefs(context); if (sharedPrefs.contains(SUW_FINISHED_TIME_MS) && !sSuwFinishedTimeStamp.isPresent()) { sSuwFinishedTimeStamp = Optional.of(getSuwFinishedTimestamp(context)); } } /** * Gets the elapsed time by (timestamp - time of SetupWizard finished). * @param timestamp The timestamp of the current time. * @return The elapsed time after device setup finished. */ public static long getElapsedTime(long timestamp) { if (!sSuwFinishedTimeStamp.isPresent()) { Log.w(TAG, "getElapsedTime: sSuwFinishedTimeStamp is null"); return DEFAULT_SETUP_TIME; } if (sSuwFinishedTimeStamp.get() != DEFAULT_SETUP_TIME) { final long elapsedTime = timestamp - sSuwFinishedTimeStamp.get(); return elapsedTime > 0L ? elapsedTime : DEFAULT_SETUP_TIME; } return DEFAULT_SETUP_TIME; } @VisibleForTesting static long getSuwFinishedTimestamp(Context context) { return getSharedPrefs(context).getLong(SUW_FINISHED_TIME_MS, DEFAULT_SETUP_TIME); } private static SharedPreferences getSharedPrefs(Context context) { return context .getApplicationContext() .getSharedPreferences(ELAPSED_TIME_PREF_FILENAME, Context.MODE_PRIVATE); } private ElapsedTimeUtils() {} }
src/com/android/settings/core/instrumentation/StatsLogWriter.java +10 −5 Original line number Diff line number Diff line Loading @@ -31,7 +31,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.PAGE_VISIBLE /* action */, pageId, /* target pageId */ "" /* changedPreferenceKey */, latency /* changedPreferenceIntValue */); latency /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -41,7 +42,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.PAGE_HIDE /* action */, pageId, "" /* changedPreferenceKey */, visibleTime /* changedPreferenceIntValue */); visibleTime /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -51,7 +53,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.ACTION_SETTINGS_TILE_CLICK /* action */, SettingsEnums.PAGE_UNKNOWN /* pageId */, key /* changedPreferenceKey */, 0 /* changedPreferenceIntValue */); 0 /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading @@ -61,7 +64,8 @@ public class StatsLogWriter implements LogWriter { SettingsEnums.ACTION_SETTINGS_PREFERENCE_CHANGE /* action */, SettingsEnums.PAGE_UNKNOWN /* pageId */, key /* changedPreferenceKey */, value /* changedPreferenceIntValue */); value /* changedPreferenceIntValue */, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } @Override Loading Loading @@ -107,6 +111,7 @@ public class StatsLogWriter implements LogWriter { action, pageId, key, value); value, ElapsedTimeUtils.getElapsedTime(System.currentTimeMillis())); } }
src/com/android/settings/fuelgauge/batteryusage/BootBroadcastReceiver.java +4 −0 Original line number Diff line number Diff line Loading @@ -23,6 +23,8 @@ import android.os.Handler; import android.os.Looper; import android.util.Log; import com.android.settings.core.instrumentation.ElapsedTimeUtils; import java.time.Duration; /** Receives broadcasts to start or stop the periodic fetching job. */ Loading Loading @@ -77,6 +79,8 @@ public final class BootBroadcastReceiver extends BroadcastReceiver { recheckIntent.setClass(context, BootBroadcastReceiver.class); mHandler.postDelayed(() -> context.sendBroadcast(recheckIntent), RESCHEDULE_FOR_BOOT_ACTION); } else if (ACTION_SETUP_WIZARD_FINISHED.equals(action)) { ElapsedTimeUtils.storeSuwFinishedTimestamp(context, System.currentTimeMillis()); } } Loading
tests/robotests/src/com/android/settings/core/instrumentation/ElapsedTimeUtilsTest.java 0 → 100644 +88 −0 Original line number Diff line number Diff line /* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.settings.core.instrumentation; import static com.google.common.truth.Truth.assertThat; import android.content.Context; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class ElapsedTimeUtilsTest { private Context mContext; @Before public void setUp() { mContext = RuntimeEnvironment.application; } @Test public void storeSuwFinishedTimestamp_firstTimeAccess_shouldStoreTimestamp() { final long timestamp = 1000000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, timestamp); final long result = ElapsedTimeUtils.getSuwFinishedTimestamp(mContext); assertThat(result).isEqualTo(timestamp); } @Test public void storeSuwFinishedTimestamp_NotFirstTimeAccess_shouldNotStoreTimestamp() { final long timestamp = 1000000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, timestamp); ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, 2000000L); final long result = ElapsedTimeUtils.getSuwFinishedTimestamp(mContext); assertThat(result).isEqualTo(timestamp); } @Test public void getElapsedTime_valueInPrefs_positiveElapsedTime_returnElapsedTime() { final long suwTime = 1000L; final long actionTime = 2000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, suwTime); final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(actionTime - suwTime); } @Test public void getElapsedTime_valueInPrefs_negativeElapsedTime_returnDefault() { final long suwTime = 2000L; final long actionTime = 1000L; ElapsedTimeUtils.storeSuwFinishedTimestamp(mContext, suwTime); final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(ElapsedTimeUtils.DEFAULT_SETUP_TIME); } @Test public void getElapsedTime_noValueInPrefs_returnDefault() { final long actionTime = 1000L; final long result = ElapsedTimeUtils.getElapsedTime(actionTime); assertThat(result).isEqualTo(ElapsedTimeUtils.DEFAULT_SETUP_TIME); } }