Hides screen attention when attention service is not avaliable.

If a wrong attention package is pushed to users devices, the screen
attention won't work. In this case, we should also hide the settings
as well.

Test: atest AdaptiveSleepPreferenceControllerTest
Bug: 148099790
Change-Id: Ieb4fd1008856024c23624f0eab3dfbfc3fc4ee3b
This commit is contained in:
Yi Jiang
2020-02-13 16:52:43 -08:00
parent 7d932e4ef6
commit 7f4a4ce85d
2 changed files with 32 additions and 1 deletions

View File

@@ -17,8 +17,12 @@ import static android.provider.Settings.Secure.ADAPTIVE_SLEEP;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.provider.Settings;
import android.service.attention.AttentionService;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
@@ -62,10 +66,24 @@ public class AdaptiveSleepPreferenceController extends TogglePreferenceControlle
public static int isControllerAvailable(Context context) {
return context.getResources().getBoolean(
com.android.internal.R.bool.config_adaptive_sleep_available)
&& isAttentionServiceAvailable(context)
? AVAILABLE_UNSEARCHABLE
: UNSUPPORTED_ON_DEVICE;
}
private static boolean isAttentionServiceAvailable(Context context) {
final PackageManager packageManager = context.getPackageManager();
final String resolvePackage = packageManager.getAttentionServicePackageName();
if (TextUtils.isEmpty(resolvePackage)) {
return false;
}
final Intent intent = new Intent(AttentionService.SERVICE_INTERFACE).setPackage(
resolvePackage);
final ResolveInfo resolveInfo = packageManager.resolveService(intent,
PackageManager.MATCH_SYSTEM_ONLY);
return resolveInfo != null && resolveInfo.serviceInfo != null;
}
static boolean hasSufficientPermission(PackageManager packageManager) {
final String attentionPackage = packageManager.getAttentionServicePackageName();
return attentionPackage != null && packageManager.checkPermission(

View File

@@ -16,17 +16,22 @@
package com.android.settings.display;
import static android.provider.Settings.System.ADAPTIVE_SLEEP;
import static android.provider.Settings.Secure.ADAPTIVE_SLEEP;
import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_DEVICE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.provider.Settings;
@@ -75,6 +80,14 @@ public class AdaptiveSleepPreferenceControllerTest {
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
}
@Test
public void isControllerAvailable_ServiceUnavailable_returnUnsupported() {
doReturn(null).when(mPackageManager).resolveService(isA(Intent.class), anyInt());
assertThat(AdaptiveSleepPreferenceController.isControllerAvailable(mContext)).isEqualTo(
UNSUPPORTED_ON_DEVICE);
}
@Test
public void onPreferenceChange_turnOn_returnOn() {
mController.onPreferenceChange(null, true);