Files
app_Settings/src/com/android/settings/safetycenter/SafetyCenterManagerWrapper.java
Marie Matheson ce321f0dd0 Merge SafetyCenterStatusHolder into SafetyCenterManagerWrapper
Test: SettingsUnitTests
Bug: 220384387
Change-Id: I3886d3cf4717b5c7efd99ebd8ffb6a039e3565a4
2022-02-22 16:36:17 +00:00

82 lines
2.8 KiB
Java

/*
* 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.safetycenter;
import android.content.Context;
import android.safetycenter.SafetyCenterManager;
import android.safetycenter.SafetySourceData;
import android.util.Log;
import com.android.internal.annotations.VisibleForTesting;
/** A wrapper for the SafetyCenterManager system service. */
public class SafetyCenterManagerWrapper {
private static final String TAG = "SafetyCenterManagerWrapper";
@VisibleForTesting
public static SafetyCenterManagerWrapper sInstance;
private SafetyCenterManagerWrapper() {}
/** Returns an instance of {@link SafetyCenterManagerWrapper}. */
public static SafetyCenterManagerWrapper get() {
if (sInstance == null) {
sInstance = new SafetyCenterManagerWrapper();
}
return sInstance;
}
/** Sends updated safety source data to Safety Center. */
public void sendSafetyCenterUpdate(Context context, SafetySourceData safetySourceData) {
SafetyCenterManager safetyCenterManager =
context.getSystemService(SafetyCenterManager.class);
if (safetyCenterManager == null) {
Log.e(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null");
return;
}
try {
safetyCenterManager.sendSafetyCenterUpdate(safetySourceData);
} catch (Exception e) {
Log.e(TAG, "Failed to send SafetySourceData", e);
return;
}
}
/** Returns true is SafetyCenter page is enabled, false otherwise. */
public boolean isEnabled(Context context) {
if (context == null) {
Log.e(TAG, "Context is null at SafetyCenterManagerWrapper#isEnabled");
return false;
}
SafetyCenterManager safetyCenterManager =
context.getSystemService(SafetyCenterManager.class);
if (safetyCenterManager == null) {
Log.w(TAG, "System service SAFETY_CENTER_SERVICE (SafetyCenterManager) is null");
return false;
}
try {
return safetyCenterManager.isSafetyCenterEnabled();
} catch (RuntimeException e) {
Log.e(TAG, "Calling isSafetyCenterEnabled failed.", e);
return false;
}
}
}