Notification listeners have full DND access.

Bug: 27976092
Change-Id: I9603d900d7cee5666ec3567b4f42fee6d93ae5f8
This commit is contained in:
Julia Reynolds
2016-04-13 10:02:18 -04:00
parent d4f4e2d5b5
commit 83f35ba88d
9 changed files with 325 additions and 54 deletions

View File

@@ -42,9 +42,9 @@ import java.util.List;
public abstract class ManagedServiceSettings extends EmptyTextSettings {
private final Config mConfig;
private Context mContext;
protected Context mContext;
private PackageManager mPM;
private ServiceListing mServiceListing;
protected ServiceListing mServiceListing;
private TextView mEmpty;
abstract protected Config getConfig();
@@ -111,7 +111,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
}
}
private boolean setEnabled(ComponentName service, String title, boolean enable) {
protected boolean setEnabled(ComponentName service, String title, boolean enable) {
if (!enable) {
// the simple version: disabling
mServiceListing.setEnabled(service, false);
@@ -173,6 +173,7 @@ public abstract class ManagedServiceSettings extends EmptyTextSettings {
public static class Config {
public String tag;
public String setting;
public String secondarySetting;
public String intentAction;
public String permission;
public String noun;

View File

@@ -30,6 +30,7 @@ import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Slog;
import com.android.settings.utils.ManagedServiceSettings.Config;
@@ -46,7 +47,6 @@ public class ServiceListing {
private final HashSet<ComponentName> mEnabledServices = new HashSet<ComponentName>();
private final List<ServiceInfo> mServices = new ArrayList<ServiceInfo>();
private final List<Callback> mCallbacks = new ArrayList<Callback>();
private final List<ServiceInfo> mApprovedServices = new ArrayList<ServiceInfo>();
private boolean mListening;
@@ -95,18 +95,7 @@ public class ServiceListing {
return getServices(c, null, pm);
}
public ServiceInfo findService(Context context, Config config, final ComponentName cn) {
final ServiceListing listing = new ServiceListing(context, config);
for (ServiceInfo service : mApprovedServices) {
final ComponentName serviceCN = new ComponentName(service.packageName, service.name);
if (serviceCN.equals(cn)) {
return service;
}
}
return null;
}
private static int getServices(Config c, List<ServiceInfo> list, PackageManager pm) {
protected static int getServices(Config c, List<ServiceInfo> list, PackageManager pm) {
int services = 0;
if (list != null) {
list.clear();
@@ -174,26 +163,6 @@ public class ServiceListing {
return mServices;
}
public void reloadApprovedServices() {
mApprovedServices.clear();
final String flat = Settings.Secure.getString(mContentResolver, mConfig.setting);
if (flat != null && !"".equals(flat)) {
final List<String> names = Arrays.asList(flat.split(":"));
List<ServiceInfo> services = new ArrayList<>();
getServices(mConfig, services, mContext.getPackageManager());
for (ServiceInfo service : services) {
final ComponentName componentName = service.getComponentName();
String flatCn = service.getComponentName().flattenToString();
if (names.contains(flatCn) || names.contains(componentName.getPackageName())) {
mApprovedServices.add(service);
}
}
for (Callback callback : mCallbacks) {
callback.onServicesReloaded(mApprovedServices);
}
}
}
public boolean isEnabled(ComponentName cn) {
return mEnabledServices.contains(cn);
}

View File

@@ -0,0 +1,148 @@
/*
* Copyright (C) 2016 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.utils;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.Slog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
public class ZenServiceListing {
private final ContentResolver mContentResolver;
private final Context mContext;
private final ManagedServiceSettings.Config mConfig;
private final Set<ServiceInfo> mApprovedServices = new ArraySet<ServiceInfo>();
private final List<Callback> mZenCallbacks = new ArrayList<>();
public ZenServiceListing(Context context, ManagedServiceSettings.Config config) {
mContext = context;
mConfig = config;
mContentResolver = context.getContentResolver();
}
public ServiceInfo findService(final ComponentName cn) {
for (ServiceInfo service : mApprovedServices) {
final ComponentName serviceCN = new ComponentName(service.packageName, service.name);
if (serviceCN.equals(cn)) {
return service;
}
}
return null;
}
public void addZenCallback(Callback callback) {
mZenCallbacks.add(callback);
}
public void removeZenCallback(Callback callback) {
mZenCallbacks.remove(callback);
}
public void reloadApprovedServices() {
mApprovedServices.clear();
String[] settings = {mConfig.setting, mConfig.secondarySetting};
for (String setting : settings) {
if (!TextUtils.isEmpty(setting)) {
final String flat = Settings.Secure.getString(mContentResolver, setting);
if (!TextUtils.isEmpty(flat)) {
final List<String> names = Arrays.asList(flat.split(":"));
List<ServiceInfo> services = new ArrayList<>();
getServices(mConfig, services, mContext.getPackageManager());
for (ServiceInfo service : services) {
if (matchesApprovedPackage(names, service.getComponentName())) {
mApprovedServices.add(service);
}
}
}
}
}
if (!mApprovedServices.isEmpty()) {
for (Callback callback : mZenCallbacks) {
callback.onServicesReloaded(mApprovedServices);
}
}
}
// Setting could contain: the component name of the condition provider, the package name of
// the condition provider, the component name of the notification listener.
private boolean matchesApprovedPackage(List<String> approved, ComponentName serviceOwner) {
String flatCn = serviceOwner.flattenToString();
if (approved.contains(flatCn) || approved.contains(serviceOwner.getPackageName())) {
return true;
}
for (String entry : approved) {
if (!TextUtils.isEmpty(entry)) {
ComponentName approvedComponent = ComponentName.unflattenFromString(entry);
if (approvedComponent != null && approvedComponent.getPackageName().equals(
serviceOwner.getPackageName())) {
return true;
}
}
}
return false;
}
private static int getServices(ManagedServiceSettings.Config c, List<ServiceInfo> list,
PackageManager pm) {
int services = 0;
if (list != null) {
list.clear();
}
final int user = ActivityManager.getCurrentUser();
List<ResolveInfo> installedServices = pm.queryIntentServicesAsUser(
new Intent(c.intentAction),
PackageManager.GET_SERVICES | PackageManager.GET_META_DATA,
user);
for (int i = 0, count = installedServices.size(); i < count; i++) {
ResolveInfo resolveInfo = installedServices.get(i);
ServiceInfo info = resolveInfo.serviceInfo;
if (!c.permission.equals(info.permission)) {
Slog.w(c.tag, "Skipping " + c.noun + " service "
+ info.packageName + "/" + info.name
+ ": it does not require the permission "
+ c.permission);
continue;
}
if (list != null) {
list.add(info);
}
services++;
}
return services;
}
public interface Callback {
void onServicesReloaded(Set<ServiceInfo> services);
}
}